home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src-tk / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  8.8 KB  |  368 lines

  1. #if !defined(FX)
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <X11/keysym.h>
  6. #include "gltk.h"
  7. #include "private.h"
  8.  
  9. /******************************************************************************/
  10.  
  11. void (*ExposeFunc)(int, int) = 0;
  12. void (*ReshapeFunc)(int, int) = 0;
  13. void (*DisplayFunc)(void) = 0;
  14. GLenum (*KeyDownFunc)(int, GLenum) = 0;
  15. GLenum (*MouseDownFunc)(int, int, GLenum) = 0;
  16. GLenum (*MouseUpFunc)(int, int, GLenum) = 0;
  17. GLenum (*MouseMoveFunc)(int, int, GLenum) = 0;
  18. void (*IdleFunc)(void) = 0;
  19. int lastEventType = -1;
  20. GLenum drawAllowFlag;
  21.  
  22. /******************************************************************************/
  23.  
  24. static GLenum DoNextEvent(void)
  25. {
  26.     XEvent current, ahead;
  27.     char buf[1000];
  28.     KeySym ks;
  29.     int key;
  30.  
  31.     XNextEvent(xDisplay, ¤t);
  32.     switch (current.type) {
  33.       case MappingNotify:
  34.     XRefreshKeyboardMapping((XMappingEvent *)¤t);
  35.     lastEventType = MappingNotify;
  36.     return GL_FALSE;
  37.  
  38.       case MapNotify:
  39.     lastEventType = MapNotify;
  40.     drawAllowFlag = GL_TRUE;
  41.     return GL_FALSE;
  42.  
  43.       case UnmapNotify:
  44.     lastEventType = UnmapNotify;
  45.     drawAllowFlag = GL_FALSE;
  46.     return GL_FALSE;
  47.       
  48.       case ClientMessage:
  49.     lastEventType = ClientMessage;
  50.     if (current.xclient.data.l[0] == deleteWindowAtom) {
  51.         exit(0);
  52.     }
  53.     return GL_FALSE;
  54.  
  55.       case Expose:
  56.     while (XEventsQueued(current.xexpose.display, QueuedAfterReading) > 0) {
  57.         XPeekEvent(current.xexpose.display, &ahead);
  58.         if (ahead.xexpose.window != current.xexpose.window ||
  59.         ahead.type != Expose) {
  60.         break;
  61.         }
  62.         XNextEvent(xDisplay, ¤t);
  63.     }
  64.     if (current.xexpose.count == 0) {
  65.         if (ExposeFunc) {
  66.         (*ExposeFunc)(w.w, w.h);
  67.         if (lastEventType == ConfigureNotify &&
  68.             drawAllowFlag == GL_TRUE) {
  69.             lastEventType = Expose;
  70.             return GL_FALSE;
  71.         } else {
  72.             lastEventType = Expose;
  73.             drawAllowFlag = GL_TRUE;
  74.             return GL_TRUE;
  75.         }
  76.         }
  77.     }
  78.     return GL_FALSE;
  79.  
  80.       case ConfigureNotify:
  81.     lastEventType = ConfigureNotify;
  82.     w.w = current.xconfigure.width;
  83.     w.h = current.xconfigure.height;
  84.     if (TK_HAS_OVERLAY(w.type)) {
  85.         XResizeWindow(xDisplay, w.wOverlay, w.w, w.h);
  86.     }
  87.     if (ReshapeFunc) {
  88.         (*ReshapeFunc)(w.w, w.h);
  89.         return GL_TRUE;
  90.     } else {
  91.         return GL_FALSE;
  92.     }
  93.  
  94.       case MotionNotify:
  95.     lastEventType = MotionNotify;
  96.     if (MouseMoveFunc) {
  97.         GLenum mask;
  98.  
  99.         mask = 0;
  100.         if (current.xmotion.state & Button1Mask) {
  101.         mask |= TK_LEFTBUTTON;
  102.         }
  103.         if (current.xmotion.state & Button2Mask) {
  104.         mask |= TK_MIDDLEBUTTON;
  105.         }
  106.         if (current.xmotion.state & Button3Mask) {
  107.         mask |= TK_RIGHTBUTTON;
  108.         }
  109.         return (*MouseMoveFunc)(current.xmotion.x, current.xmotion.y, mask);
  110.     } else {
  111.         return GL_FALSE;
  112.     }
  113.  
  114.       case ButtonPress:
  115.     lastEventType = ButtonPress;
  116.     if (MouseDownFunc) {
  117.         GLenum mask;
  118.  
  119.         mask = 0;
  120.         if (current.xbutton.button == 1) {
  121.         mask |= TK_LEFTBUTTON;
  122.         }
  123.         if (current.xbutton.button == 2) {
  124.         mask |= TK_MIDDLEBUTTON;
  125.         }
  126.         if (current.xbutton.button == 3) {
  127.         mask |= TK_RIGHTBUTTON;
  128.         }
  129.         return (*MouseDownFunc)(current.xbutton.x, current.xbutton.y, mask);
  130.     } else {
  131.         return GL_FALSE;
  132.     }
  133.       case ButtonRelease:
  134.     lastEventType = ButtonRelease;
  135.     if (MouseUpFunc) {
  136.         GLenum mask;
  137.  
  138.         mask = 0;
  139.         if (current.xbutton.button == 1) {
  140.         mask |= TK_LEFTBUTTON;
  141.         }
  142.         if (current.xbutton.button == 2) {
  143.         mask |= TK_MIDDLEBUTTON;
  144.         }
  145.         if (current.xbutton.button == 3) {
  146.         mask |= TK_RIGHTBUTTON;
  147.         }
  148.         return (*MouseUpFunc)(current.xbutton.x, current.xbutton.y, mask);
  149.     } else {
  150.         return GL_FALSE;
  151.     }
  152.  
  153.       case KeyPress:
  154.     lastEventType = KeyPress;
  155.     XLookupString(¤t.xkey, buf, sizeof(buf), &ks, 0);
  156.     switch (ks) {
  157.       case XK_0:         key = TK_0;        break;
  158.       case XK_1:         key = TK_1;        break;
  159.       case XK_2:         key = TK_2;        break;
  160.       case XK_3:         key = TK_3;        break;
  161.       case XK_4:         key = TK_4;        break;
  162.       case XK_5:         key = TK_5;        break;
  163.       case XK_6:         key = TK_6;        break;
  164.       case XK_7:         key = TK_7;        break;
  165.       case XK_8:         key = TK_8;        break;
  166.       case XK_9:         key = TK_9;        break;
  167.       case XK_A:         key = TK_A;        break;
  168.       case XK_B:         key = TK_B;        break;
  169.       case XK_C:         key = TK_C;        break;
  170.       case XK_D:         key = TK_D;        break;
  171.       case XK_E:         key = TK_E;        break;
  172.       case XK_F:         key = TK_F;        break;
  173.       case XK_G:         key = TK_G;        break;
  174.       case XK_H:         key = TK_H;        break;
  175.       case XK_I:         key = TK_I;        break;
  176.       case XK_J:         key = TK_J;        break;
  177.       case XK_K:         key = TK_K;        break;
  178.       case XK_L:         key = TK_L;        break;
  179.       case XK_M:         key = TK_M;        break;
  180.       case XK_N:         key = TK_N;        break;
  181.       case XK_O:         key = TK_O;        break;
  182.       case XK_P:         key = TK_P;        break;
  183.       case XK_Q:         key = TK_Q;        break;
  184.       case XK_R:         key = TK_R;        break;
  185.       case XK_S:         key = TK_S;        break;
  186.       case XK_T:         key = TK_T;        break;
  187.       case XK_U:         key = TK_U;        break;
  188.       case XK_V:         key = TK_V;        break;
  189.       case XK_W:         key = TK_W;        break;
  190.       case XK_X:         key = TK_X;        break;
  191.       case XK_Y:         key = TK_Y;        break;
  192.       case XK_Z:         key = TK_Z;        break;
  193.       case XK_a:         key = TK_a;        break;
  194.       case XK_b:         key = TK_b;        break;
  195.       case XK_c:         key = TK_c;        break;
  196.       case XK_d:         key = TK_d;        break;
  197.       case XK_e:         key = TK_e;        break;
  198.       case XK_f:         key = TK_f;        break;
  199.       case XK_g:         key = TK_g;        break;
  200.       case XK_h:         key = TK_h;        break;
  201.       case XK_i:         key = TK_i;        break;
  202.       case XK_j:         key = TK_j;        break;
  203.       case XK_k:         key = TK_k;        break;
  204.       case XK_l:         key = TK_l;        break;
  205.       case XK_m:         key = TK_m;        break;
  206.       case XK_n:         key = TK_n;         break;
  207.       case XK_o:         key = TK_o;        break;
  208.       case XK_p:         key = TK_p;        break;
  209.       case XK_q:         key = TK_q;        break;
  210.       case XK_r:         key = TK_r;        break;
  211.       case XK_s:         key = TK_s;        break;
  212.       case XK_t:         key = TK_t;        break;
  213.       case XK_u:         key = TK_u;        break;
  214.       case XK_v:         key = TK_v;        break;
  215.       case XK_w:         key = TK_w;        break;
  216.       case XK_x:         key = TK_x;        break;
  217.       case XK_y:         key = TK_y;        break;
  218.       case XK_z:         key = TK_z;        break;
  219.       case XK_space:    key = TK_SPACE;        break;
  220.       case XK_Return:     key = TK_RETURN;    break;
  221.       case XK_Escape:     key = TK_ESCAPE;    break;
  222.       case XK_Left:        key = TK_LEFT;        break;
  223.       case XK_Up:        key = TK_UP;        break;
  224.       case XK_Right:      key = TK_RIGHT;        break;
  225.       case XK_Down:        key = TK_DOWN;        break;
  226.       default:         key = GL_FALSE;        break;
  227.     }
  228.     if (key && KeyDownFunc) {
  229.         GLenum mask;
  230.  
  231.         mask = 0;
  232.         if (current.xkey.state & ControlMask) {
  233.         mask |= TK_CONTROL;
  234.         }
  235.         if (current.xkey.state & ShiftMask) {
  236.         mask |= TK_SHIFT;
  237.         }
  238.         return (*KeyDownFunc)(key, mask);
  239.     } else {
  240.         return GL_FALSE;
  241.     }
  242.     }
  243.     return GL_FALSE;
  244. }
  245.  
  246.  
  247. #ifdef LEAVEOUT
  248. /*
  249.  * Mesa 1.2.7 and earlier:
  250.  */
  251. void tkExec(void)
  252. {
  253.     GLenum flag = 0;
  254.  
  255.     while (GL_TRUE) {
  256.     if (IdleFunc) {
  257.         (*IdleFunc)();
  258.         flag = GL_TRUE;
  259.         while /*if*/ (XPending(xDisplay)) {
  260.         flag |= DoNextEvent();
  261.         }
  262.     } else {
  263.         flag = DoNextEvent();
  264.     }
  265.     if (drawAllowFlag && DisplayFunc && flag) {
  266.         (*DisplayFunc)();
  267.     }
  268.     }
  269. }
  270. #endif
  271.  
  272. /*
  273.  * New code from Christian Wetzel <wetzel@informatik.uni-freiburg.de>
  274.  */
  275. void tkExec(void)
  276. {
  277.     GLenum flag;
  278.  
  279. /*    while  (XPending(xDisplay)) { flag |= DoNextEvent(); }*/
  280.  
  281.     while (GL_TRUE) {
  282.         if (IdleFunc) {
  283.             while  (XPending(xDisplay)) { flag |= DoNextEvent(); }
  284.             if (IdleFunc) (*IdleFunc)();
  285.             if(flag && DisplayFunc) { (*DisplayFunc)(); }
  286.             flag=0;
  287.         }
  288.         else {
  289.             flag = DoNextEvent();
  290.             if (drawAllowFlag && DisplayFunc && flag) { (*DisplayFunc)(); }}
  291.     }
  292. }
  293.  
  294.  
  295.  
  296. /******************************************************************************/
  297.  
  298. void tkExposeFunc(void (*Func)(int, int))
  299. {
  300.  
  301.     ExposeFunc = Func;
  302. }
  303.  
  304. /******************************************************************************/
  305.  
  306. void tkReshapeFunc(void (*Func)(int, int))
  307. {
  308.  
  309.     ReshapeFunc = Func;
  310. }
  311.  
  312. /******************************************************************************/
  313.  
  314. void tkDisplayFunc(void (*Func)(void))
  315. {
  316.  
  317.     DisplayFunc = Func;
  318. }
  319.  
  320. /******************************************************************************/
  321.  
  322. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  323. {
  324.  
  325.     KeyDownFunc = Func;
  326. }
  327.  
  328. /******************************************************************************/
  329.  
  330. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  331. {
  332.  
  333.     MouseDownFunc = Func;
  334. }
  335.  
  336. /******************************************************************************/
  337.  
  338. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  339. {
  340.  
  341.     MouseUpFunc = Func;
  342. }
  343.  
  344. /******************************************************************************/
  345.  
  346. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  347. {
  348.  
  349.     MouseMoveFunc = Func;
  350. }
  351.  
  352. /******************************************************************************/
  353.  
  354. void tkIdleFunc(void (*Func)(void))
  355. {
  356.  
  357.     IdleFunc = Func;
  358. }
  359.  
  360. /******************************************************************************/
  361.  
  362. #else
  363.  
  364. /* This is here to avoid ANSI C "empty source file" warnings */
  365. static void no_op () {}
  366.  
  367. #endif    /* !FX */
  368.