home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / siggraphCD / lib / libtk / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.8 KB  |  360 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <X11/keysym.h>
  40. #include "tk.h"
  41. #include "private.h"
  42.  
  43. /******************************************************************************/
  44.  
  45. void (*ExposeFunc)(int, int) = 0;
  46. void (*ReshapeFunc)(int, int) = 0;
  47. void (*DisplayFunc)(void) = 0;
  48. GLenum (*KeyDownFunc)(int, GLenum) = 0;
  49. GLenum (*MouseDownFunc)(int, int, GLenum) = 0;
  50. GLenum (*MouseUpFunc)(int, int, GLenum) = 0;
  51. GLenum (*MouseMoveFunc)(int, int, GLenum) = 0;
  52. void (*IdleFunc)(void) = 0;
  53. int lastEventType = -1;
  54. GLenum drawAllowFlag;
  55.  
  56. /******************************************************************************/
  57.  
  58. static GLenum DoNextEvent(void)
  59. {
  60.     XEvent current, ahead;
  61.     char buf[1000];
  62.     KeySym ks;
  63.     int key;
  64.  
  65.     XNextEvent(xDisplay, ¤t);
  66.     switch (current.type) {
  67.       case MappingNotify:
  68.     XRefreshKeyboardMapping((XMappingEvent *)¤t);
  69.     lastEventType = MappingNotify;
  70.     return GL_FALSE;
  71.  
  72.       case MapNotify:
  73.     lastEventType = MapNotify;
  74.     drawAllowFlag = GL_TRUE;
  75.     return GL_FALSE;
  76.  
  77.       case UnmapNotify:
  78.     lastEventType = UnmapNotify;
  79.     drawAllowFlag = GL_FALSE;
  80.     return GL_FALSE;
  81.       
  82.       case ClientMessage:
  83.     lastEventType = ClientMessage;
  84.     if (current.xclient.data.l[0] == deleteWindowAtom) {
  85.         exit(0);
  86.     }
  87.     return GL_FALSE;
  88.  
  89.       case Expose:
  90.     while (XEventsQueued(current.xexpose.display, QueuedAfterReading) > 0) {
  91.         XPeekEvent(current.xexpose.display, &ahead);
  92.         if (ahead.xexpose.window != current.xexpose.window ||
  93.         ahead.type != Expose) {
  94.         break;
  95.         }
  96.         XNextEvent(xDisplay, ¤t);
  97.     }
  98.     if (current.xexpose.count == 0) {
  99.         if (ExposeFunc) {
  100.         drawAllowFlag = GL_TRUE;
  101.         lastEventType = Expose;
  102.         (*ExposeFunc)(w.w, w.h);
  103.         if (lastEventType == ConfigureNotify) {
  104.             return GL_FALSE;
  105.         } else {
  106.             return GL_TRUE;
  107.         }
  108.         }
  109.     }
  110.     return GL_FALSE;
  111.  
  112.       case ConfigureNotify:
  113.     lastEventType = ConfigureNotify;
  114.     w.w = current.xconfigure.width;
  115.     w.h = current.xconfigure.height;
  116.     if (ReshapeFunc) {
  117.         (*ReshapeFunc)(w.w, w.h);
  118.         return GL_TRUE;
  119.     } else {
  120.         return GL_FALSE;
  121.     }
  122.  
  123.       case MotionNotify:
  124.     lastEventType = MotionNotify;
  125.     if (MouseMoveFunc) {
  126.         GLenum mask;
  127.  
  128.         mask = 0;
  129.         if (current.xmotion.state & Button1Mask) {
  130.         mask |= TK_LEFTBUTTON;
  131.         }
  132.         if (current.xmotion.state & Button2Mask) {
  133.         mask |= TK_MIDDLEBUTTON;
  134.         }
  135.         if (current.xmotion.state & Button3Mask) {
  136.         mask |= TK_RIGHTBUTTON;
  137.         }
  138.         return (*MouseMoveFunc)(current.xmotion.x, current.xmotion.y, mask);
  139.     } else {
  140.         return GL_FALSE;
  141.     }
  142.  
  143.       case ButtonPress:
  144.     lastEventType = ButtonPress;
  145.     if (MouseDownFunc) {
  146.         GLenum mask;
  147.  
  148.         mask = 0;
  149.         if (current.xbutton.button == 1) {
  150.         mask |= TK_LEFTBUTTON;
  151.         }
  152.         if (current.xbutton.button == 2) {
  153.         mask |= TK_MIDDLEBUTTON;
  154.         }
  155.         if (current.xbutton.button == 3) {
  156.         mask |= TK_RIGHTBUTTON;
  157.         }
  158.         return (*MouseDownFunc)(current.xbutton.x, current.xbutton.y, mask);
  159.     } else {
  160.         return GL_FALSE;
  161.     }
  162.       case ButtonRelease:
  163.     lastEventType = ButtonRelease;
  164.     if (MouseUpFunc) {
  165.         GLenum mask;
  166.  
  167.         mask = 0;
  168.         if (current.xbutton.button == 1) {
  169.         mask |= TK_LEFTBUTTON;
  170.         }
  171.         if (current.xbutton.button == 2) {
  172.         mask |= TK_MIDDLEBUTTON;
  173.         }
  174.         if (current.xbutton.button == 3) {
  175.         mask |= TK_RIGHTBUTTON;
  176.         }
  177.         return (*MouseUpFunc)(current.xbutton.x, current.xbutton.y, mask);
  178.     } else {
  179.         return GL_FALSE;
  180.     }
  181.  
  182.       case KeyPress:
  183.     lastEventType = KeyPress;
  184.     XLookupString(¤t.xkey, buf, sizeof(buf), &ks, 0);
  185.     switch (ks) {
  186.       case XK_0:         key = TK_0;        break;
  187.       case XK_1:         key = TK_1;        break;
  188.       case XK_2:         key = TK_2;        break;
  189.       case XK_3:         key = TK_3;        break;
  190.       case XK_4:         key = TK_4;        break;
  191.       case XK_5:         key = TK_5;        break;
  192.       case XK_6:         key = TK_6;        break;
  193.       case XK_7:         key = TK_7;        break;
  194.       case XK_8:         key = TK_8;        break;
  195.       case XK_9:         key = TK_9;        break;
  196.       case XK_A:         key = TK_A;        break;
  197.       case XK_B:         key = TK_B;        break;
  198.       case XK_C:         key = TK_C;        break;
  199.       case XK_D:         key = TK_D;        break;
  200.       case XK_E:         key = TK_E;        break;
  201.       case XK_F:         key = TK_F;        break;
  202.       case XK_G:         key = TK_G;        break;
  203.       case XK_H:         key = TK_H;        break;
  204.       case XK_I:         key = TK_I;        break;
  205.       case XK_J:         key = TK_J;        break;
  206.       case XK_K:         key = TK_K;        break;
  207.       case XK_L:         key = TK_L;        break;
  208.       case XK_M:         key = TK_M;        break;
  209.       case XK_N:         key = TK_N;        break;
  210.       case XK_O:         key = TK_O;        break;
  211.       case XK_P:         key = TK_P;        break;
  212.       case XK_Q:         key = TK_Q;        break;
  213.       case XK_R:         key = TK_R;        break;
  214.       case XK_S:         key = TK_S;        break;
  215.       case XK_T:         key = TK_T;        break;
  216.       case XK_U:         key = TK_U;        break;
  217.       case XK_V:         key = TK_V;        break;
  218.       case XK_W:         key = TK_W;        break;
  219.       case XK_X:         key = TK_X;        break;
  220.       case XK_Y:         key = TK_Y;        break;
  221.       case XK_Z:         key = TK_Z;        break;
  222.       case XK_a:         key = TK_a;        break;
  223.       case XK_b:         key = TK_b;        break;
  224.       case XK_c:         key = TK_c;        break;
  225.       case XK_d:         key = TK_d;        break;
  226.       case XK_e:         key = TK_e;        break;
  227.       case XK_f:         key = TK_f;        break;
  228.       case XK_g:         key = TK_g;        break;
  229.       case XK_h:         key = TK_h;        break;
  230.       case XK_i:         key = TK_i;        break;
  231.       case XK_j:         key = TK_j;        break;
  232.       case XK_k:         key = TK_k;        break;
  233.       case XK_l:         key = TK_l;        break;
  234.       case XK_m:         key = TK_m;        break;
  235.       case XK_n:         key = TK_n;         break;
  236.       case XK_o:         key = TK_o;        break;
  237.       case XK_p:         key = TK_p;        break;
  238.       case XK_q:         key = TK_q;        break;
  239.       case XK_r:         key = TK_r;        break;
  240.       case XK_s:         key = TK_s;        break;
  241.       case XK_t:         key = TK_t;        break;
  242.       case XK_u:         key = TK_u;        break;
  243.       case XK_v:         key = TK_v;        break;
  244.       case XK_w:         key = TK_w;        break;
  245.       case XK_x:         key = TK_x;        break;
  246.       case XK_y:         key = TK_y;        break;
  247.       case XK_z:         key = TK_z;        break;
  248.       case XK_space:    key = TK_SPACE;        break;
  249.       case XK_Return:     key = TK_RETURN;    break;
  250.       case XK_Escape:     key = TK_ESCAPE;    break;
  251.       case XK_Left:        key = TK_LEFT;        break;
  252.       case XK_Up:        key = TK_UP;        break;
  253.       case XK_Right:      key = TK_RIGHT;        break;
  254.       case XK_Down:        key = TK_DOWN;        break;
  255.       default:         key = GL_FALSE;        break;
  256.     }
  257.     if (key && KeyDownFunc) {
  258.         GLenum mask;
  259.  
  260.         mask = 0;
  261.         if (current.xkey.state & ControlMask) {
  262.         mask |= TK_CONTROL;
  263.         }
  264.         if (current.xkey.state & ShiftMask) {
  265.         mask |= TK_SHIFT;
  266.         }
  267.         return (*KeyDownFunc)(key, mask);
  268.     } else {
  269.         return GL_FALSE;
  270.     }
  271.     }
  272.     return GL_FALSE;
  273. }
  274.  
  275. void tkExec(void)
  276. {
  277.     GLenum flag;
  278.  
  279.     while (GL_TRUE) {
  280.     if (IdleFunc) {
  281.         (*IdleFunc)();
  282.         flag = GL_TRUE;
  283.         if (XPending(xDisplay)) {
  284.         flag |= DoNextEvent();
  285.         }
  286.     } else {
  287.         flag = DoNextEvent();
  288.     }
  289.     if (drawAllowFlag && DisplayFunc && flag) {
  290.         (*DisplayFunc)();
  291.     }
  292.     }
  293. }
  294.  
  295. /******************************************************************************/
  296.  
  297. void tkExposeFunc(void (*Func)(int, int))
  298. {
  299.  
  300.     ExposeFunc = Func;
  301. }
  302.  
  303. /******************************************************************************/
  304.  
  305. void tkReshapeFunc(void (*Func)(int, int))
  306. {
  307.  
  308.     ReshapeFunc = Func;
  309. }
  310.  
  311. /******************************************************************************/
  312.  
  313. void tkDisplayFunc(void (*Func)(void))
  314. {
  315.  
  316.     DisplayFunc = Func;
  317. }
  318.  
  319. /******************************************************************************/
  320.  
  321. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  322. {
  323.  
  324.     KeyDownFunc = Func;
  325. }
  326.  
  327. /******************************************************************************/
  328.  
  329. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  330. {
  331.  
  332.     MouseDownFunc = Func;
  333. }
  334.  
  335. /******************************************************************************/
  336.  
  337. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  338. {
  339.  
  340.     MouseUpFunc = Func;
  341. }
  342.  
  343. /******************************************************************************/
  344.  
  345. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  346. {
  347.  
  348.     MouseMoveFunc = Func;
  349. }
  350.  
  351. /******************************************************************************/
  352.  
  353. void tkIdleFunc(void (*Func)(void))
  354. {
  355.  
  356.     IdleFunc = Func;
  357. }
  358.  
  359. /******************************************************************************/
  360.