HelloWorldX11.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*** START MAIN.H ***/
  2. /* http://www.geocities.com/jeff_louie/x11/helloworld.htm* */
  3. /*
  4. * main.h
  5. * TestX
  6. *
  7. * Created by Jeff Louie on Tue Feb 03 2004.
  8. * Copyright (c) 2004 __MyCompanyName__. All rights reserved.
  9. *
  10. */
  11. #ifndef MAIN_H
  12. #define MAIN_H 1
  13. #include <iostream>
  14. /* include the X library headers */
  15. #include <X11/Xlib.h>
  16. #include <X11/Xutil.h>
  17. #include <X11/Xos.h>
  18. class Main {
  19. public:
  20. // constructor
  21. Main(int argc, char * const argv[]);
  22. //virtual ~Main();
  23. private:
  24. /* here are our X variables */
  25. Display *dis;
  26. int screen;
  27. Window win;
  28. GC gc;
  29. /* here are our X routines declared! */
  30. void init_x();
  31. void close_x();
  32. void redraw();
  33. int delay(int i);
  34. };
  35. #endif
  36. /*** END MAIN.H ***/
  37. /*** START MAIN.CPP ***/
  38. // modified from Brian Hammond's Howdy program at
  39. // http://www.insanityengine.com/doc/x11/xintro.html
  40. // jeff louie 02.05.2004
  41. int main (int argc, char * const argv[]) {
  42. Main m(argc, argv);
  43. return 0;
  44. }
  45. //Main::~Main() {;};
  46. Main::Main (int argc, char * const argv[]) {
  47. XEvent event; // XEvent declaration
  48. KeySym key; // KeyPress Events
  49. char text[255]; // char buffer for KeyPress Events
  50. init_x();
  51. // event loop
  52. while(1) {
  53. // get the next event and stuff it into our event variable.
  54. // Note: only events we set the mask for are detected!
  55. XNextEvent(dis, &event);
  56. switch (event.type) {
  57. int x;
  58. int y;
  59. case Expose:
  60. if (event.xexpose.count==0) {
  61. redraw();
  62. }
  63. break;
  64. case KeyPress:
  65. if (XLookupString(&event.xkey,text,255,&key,0)==1) {
  66. // use the XLookupString routine to convert the invent
  67. // KeyPress data into regular text. Weird but necessary...
  68. if ((text[0]=='q') || (text[0]=='Q')) {
  69. close_x();
  70. }
  71. else {
  72. // echo key press
  73. printf("You pressed the %c key!\n",text[0]);
  74. }
  75. }
  76. break;
  77. case ButtonPress:
  78. // get cursor position
  79. x= event.xbutton.x;
  80. y= event.xbutton.y;
  81. strcpy(text,"X is FUN!");
  82. XSetForeground(dis,gc,rand()%event.xbutton.x%255);
  83. // draw text at cursor
  84. XDrawString(dis,win,gc,x,y, text, strlen(text));
  85. break;
  86. default:
  87. printf("Unhandled event.\n");
  88. }
  89. }
  90. }
  91. void Main::init_x() {
  92. unsigned long black,white;
  93. dis=XOpenDisplay(NULL);
  94. screen=DefaultScreen(dis);
  95. black=BlackPixel(dis,screen),
  96. white=WhitePixel(dis, screen);
  97. win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
  98. 300, 300, 5,black, white);
  99. XSetStandardProperties(dis,win,"Hello World","Hi",None,NULL,0,NULL);
  100. XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
  101. // get Graphics Context
  102. gc=XCreateGC(dis, win, 0,0);
  103. XSetBackground(dis,gc,white);
  104. XSetForeground(dis,gc,black);
  105. XClearWindow(dis, win);
  106. XMapRaised(dis, win);
  107. };
  108. void Main::close_x() {
  109. XFreeGC(dis, gc);
  110. XDestroyWindow(dis,win);
  111. XCloseDisplay(dis);
  112. exit(1);
  113. };
  114. void Main::redraw() {
  115. XClearWindow(dis, win);
  116. };