android: make code 're-entrant' - i.e. NativeActivity can restart the program without...
[freeglut] / src / x11 / fg_xinput_x11.c
1 /* Written for XI1 by Nikolas Doerfler <doerflen@in.tum.de> (c) 2008 *
2  * Rewritten for XI2 by Florian Echtler <echtler@in.tum.de> (c) 2009 */
3
4 #include <GL/freeglut.h>
5
6 #include "../fg_internal.h"
7
8 #if HAVE_X11_EXTENSIONS_XINPUT2_H
9
10 #include <errno.h>
11 #include <stdarg.h>
12
13 #include <X11/Xlib.h>
14 #include <X11/extensions/XInput2.h>
15
16 /* import function from freeglut_main.c */
17 extern int fgPlatformGetModifiers( int state );
18
19 /* extension opcode for XInput */
20 int xi_opcode = -1;
21
22 /**
23  * \brief Sets window up for XI2 events.
24  */
25 void fgRegisterDevices( Display* dpy, Window* win ) {
26
27         XIEventMask mask;
28         unsigned char flags[2] = { 0, 0 };
29         int event, error;
30
31         /*Display* dpy = fgDisplay.pDisplay.Display;
32         Window* win = glutGetXWindow();*/
33
34         /* get XInput extension opcode */
35         if (!XQueryExtension( dpy, "XInputExtension", &xi_opcode, &event, &error )) { xi_opcode = -1; }
36
37         /* Select for motion events */
38         mask.deviceid = XIAllMasterDevices;
39         mask.mask_len = 2;
40         mask.mask = flags;
41
42         XISetMask(mask.mask, XI_Enter);
43         XISetMask(mask.mask, XI_Motion);
44         XISetMask(mask.mask, XI_ButtonPress);
45         XISetMask(mask.mask, XI_ButtonRelease);
46         XISetMask(mask.mask, XI_Leave);
47         /*XISetMask(mask.mask, XI_KeyPress);
48         XISetMask(mask.mask, XI_KeyRelease);
49         XISetMask(mask.mask, XI_DeviceChanged);
50         XISetMask(mask.mask, XI_RawEvent);
51         XISetMask(mask.mask, XI_FocusIn);
52         XISetMask(mask.mask, XI_FocusOut);
53         XISetMask(mask.mask, XI_HierarchyChanged);*/
54
55         XISelectEvents( dpy, *win, &mask, 1 );
56 }
57
58
59 void fgPrintXILeaveEvent(XILeaveEvent* event)
60 {
61     char* mode = "";
62                 char* detail = "";
63     int i;
64
65     printf("    windows: root 0x%lx event 0x%lx child 0x%ld\n",
66             event->root, event->event, event->child);
67     switch(event->mode)
68     {
69         case NotifyNormal:       mode = "NotifyNormal"; break;
70         case NotifyGrab:         mode = "NotifyGrab"; break;
71         case NotifyUngrab:       mode = "NotifyUngrab"; break;
72         case NotifyWhileGrabbed: mode = "NotifyWhileGrabbed"; break;
73     }
74     switch (event->detail)
75     {
76         case NotifyAncestor: detail = "NotifyAncestor"; break;
77         case NotifyVirtual: detail = "NotifyVirtual"; break;
78         case NotifyInferior: detail = "NotifyInferior"; break;
79         case NotifyNonlinear: detail = "NotifyNonlinear"; break;
80         case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
81         case NotifyPointer: detail = "NotifyPointer"; break;
82         case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
83         case NotifyDetailNone: detail = "NotifyDetailNone"; break;
84     }
85     printf("    mode: %s (detail %s)\n", mode, detail);
86     printf("    flags: %s %s\n", event->focus ? "[focus]" : "",
87                                  event->same_screen ? "[same screen]" : "");
88     printf("    buttons:");
89     for (i = 0; i < event->buttons.mask_len * 8; i++)
90         if (XIMaskIsSet(event->buttons.mask, i))
91             printf(" %d", i);
92     printf("\n");
93
94     printf("    modifiers: locked 0x%x latched 0x%x base 0x%x\n",
95             event->mods.locked, event->mods.latched,
96             event->mods.base);
97     printf("    group: locked 0x%x latched 0x%x base 0x%x\n",
98             event->group.locked, event->group.latched,
99             event->group.base);
100
101     printf("    root x/y:  %.2f / %.2f\n", event->root_x, event->root_y);
102     printf("    event x/y: %.2f / %.2f\n", event->event_x, event->event_y);
103
104 }
105
106
107 void fgPrintXIDeviceEvent(XIDeviceEvent* event)
108 {
109     double *val;
110     int i;
111
112     printf("    device: %d (%d)\n", event->deviceid, event->sourceid);
113     printf("    detail: %d\n", event->detail);
114     printf("    buttons:");
115           for (i = 0; i < event->buttons.mask_len * 8; i++)
116             if (XIMaskIsSet(event->buttons.mask, i))
117               printf(" %d", i);
118     printf("\n");
119
120     printf("    modifiers: locked 0x%x latched 0x%x base 0x%x\n",
121             event->mods.locked, event->mods.latched,
122             event->mods.base);
123     printf("    group: locked 0x%x latched 0x%x base 0x%x\n",
124             event->group.locked, event->group.latched,
125             event->group.base);
126     printf("    valuators:");
127
128     val = event->valuators.values;
129     for (i = 0; i < event->valuators.mask_len * 8; i++)
130         if (XIMaskIsSet(event->valuators.mask, i))
131             printf(" %d: %.2f", i, *val++);
132     printf("\n");
133
134     printf("    windows: root 0x%lx event 0x%lx child 0x%ld\n",
135             event->root, event->event, event->child);
136     printf("    root x/y:  %.2f / %.2f\n", event->root_x, event->root_y);
137     printf("    event x/y: %.2f / %.2f\n", event->event_x, event->event_y);
138
139 }
140
141
142 /**
143  * \brief This function is called when an Extension Event is received
144  * and calls the corresponding callback functions for these events.
145  */
146 void fgHandleExtensionEvents( XEvent* base_ev ) {
147
148         int i, button = 0;
149         XGenericEventCookie* cookie = (XGenericEventCookie*)&(base_ev->xcookie);
150
151         if ( XGetEventData( fgDisplay.pDisplay.Display, cookie ) && (cookie->type == GenericEvent) && (cookie->extension == xi_opcode) ) {
152
153                 XIDeviceEvent* event = (XIDeviceEvent*)(cookie->data);
154                 /*printf("XI2 event type: %d - %d\n", cookie->evtype, event->type );*/
155
156                 SFG_Window* window = fgWindowByHandle( event->event );
157                 if (!window) return;
158
159                 switch (cookie->evtype) {
160
161                         case XI_Enter:
162                         case XI_Leave:
163                                 fgState.Modifiers = fgPlatformGetModifiers( ((XIEnterEvent*)event)->mods.base );
164                                 INVOKE_WCB( *window, MultiEntry, (
165                                         event->deviceid, 
166                                         (event->evtype == XI_Enter ? GLUT_ENTERED : GLUT_LEFT)
167                                 ));
168                                 #if _DEBUG
169                                         fgPrintXILeaveEvent((XILeaveEvent*)event);
170                                 #endif
171                                 break;
172
173                         case XI_ButtonPress:
174                         case XI_ButtonRelease:
175                                 fgState.Modifiers = fgPlatformGetModifiers( event->mods.base );
176                                 INVOKE_WCB( *window, MultiButton, (
177                                         event->deviceid, 
178                                         event->event_x,
179                                         event->event_y,
180                                         (event->detail)-1, 
181                                         (event->evtype == XI_ButtonPress ? GLUT_DOWN : GLUT_UP)
182                                 ));
183                                 INVOKE_WCB( *window, Mouse, (
184                                         (event->detail)-1, 
185                                         (event->evtype == XI_ButtonPress ? GLUT_DOWN : GLUT_UP),
186                                         event->event_x,
187                                         event->event_y
188                                 ));
189                                 break;
190
191                         case XI_Motion:
192                                 fgState.Modifiers = fgPlatformGetModifiers( event->mods.base );
193                                 for (i = 0; i < event->buttons.mask_len; i++) if (event->buttons.mask[i]) button = 1;
194                                 if (button) {
195                                         INVOKE_WCB( *window, MultiMotion,  ( event->deviceid, event->event_x, event->event_y ) );
196                                         INVOKE_WCB( *window,           Motion,  (                  event->event_x, event->event_y ) );
197                                 } else {
198                                         INVOKE_WCB( *window, MultiPassive, ( event->deviceid, event->event_x, event->event_y ) );
199                                         INVOKE_WCB( *window,           Passive, (                  event->event_x, event->event_y ) );
200                                 }
201                                 #if _DEBUG
202                                         fgPrintXIDeviceEvent(event);
203                                 #endif
204                                 break;
205
206                         default:
207                                 #if _DEBUG
208                                         fgWarning( "Unknown XI2 device event:" );
209                                         fgPrintXIDeviceEvent( event );
210                                 #endif
211                                 break;
212                 }
213                 fgState.Modifiers = INVALID_MODIFIERS;
214         }
215         XFreeEventData( fgDisplay.pDisplay.Display, cookie );
216 }
217
218 #endif\r
219 \r