HelloWorldX11.cxx 3.1 KB

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