HelloWorldX11.cxx 2.9 KB

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