Added support for key modifiers
[freeglut] / src / blackberry / fg_main_blackberry.c
1 /*
2  * fg_main_blackberry.c
3  *
4  * The BlackBerry-specific windows message processing methods.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9  * Copyright (C) 2012  Sylvain Beucler
10  * Copyright (C) 2013  Vincent Simonetti
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29
30 #include <GL/freeglut.h>
31 #include "fg_internal.h"
32 #include "egl/fg_window_egl.h"
33
34 #include <slog2.h>
35 #define LOGI(...) ((void)slog2fa(NULL, 1337, SLOG2_INFO, __VA_ARGS__, SLOG2_FA_END))
36 #define LOGW(...) ((void)slog2fa(NULL, 1337, SLOG2_WARNING, __VA_ARGS__, SLOG2_FA_END))
37 #include <sys/keycodes.h>
38 #include <input/screen_helpers.h>
39 #include <bps/bps.h>
40 #include <bps/event.h>
41 #include <bps/screen.h>
42 #include <bps/navigator.h>
43
44 extern void fghOnReshapeNotify(SFG_Window *window, int width, int height, GLboolean forceNotify);
45 extern void fghOnPositionNotify(SFG_Window *window, int x, int y, GLboolean forceNotify);
46 extern void fgPlatformFullScreenToggle( SFG_Window *win );
47 extern void fgPlatformPositionWindow( SFG_Window *window, int x, int y );
48 extern void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height );
49 extern void fgPlatformPushWindow( SFG_Window *window );
50 extern void fgPlatformPopWindow( SFG_Window *window );
51 extern void fgPlatformHideWindow( SFG_Window *window );
52 extern void fgPlatformIconifyWindow( SFG_Window *window );
53 extern void fgPlatformShowWindow( SFG_Window *window );
54 extern void fgPlatformMainLoopPostWork ( void );
55
56 static struct touchscreen touchscreen;
57
58 unsigned int key_special(int qnxKeycode)
59 {
60     switch(qnxKeycode) {
61     case KEYCODE_F1:
62         return GLUT_KEY_F1;
63     case KEYCODE_F2:
64         return GLUT_KEY_F2;
65     case KEYCODE_F3:
66         return GLUT_KEY_F3;
67     case KEYCODE_F4:
68         return GLUT_KEY_F4;
69     case KEYCODE_F5:
70         return GLUT_KEY_F5;
71     case KEYCODE_F6:
72         return GLUT_KEY_F6;
73     case KEYCODE_F7:
74         return GLUT_KEY_F7;
75     case KEYCODE_F8:
76         return GLUT_KEY_F8;
77     case KEYCODE_F9:
78         return GLUT_KEY_F9;
79     case KEYCODE_F10:
80         return GLUT_KEY_F10;
81     case KEYCODE_F11:
82         return GLUT_KEY_F11;
83     case KEYCODE_F12:
84         return GLUT_KEY_F12;
85     case KEYCODE_PG_UP:
86         return GLUT_KEY_PAGE_UP;
87     case KEYCODE_PG_DOWN:
88         return GLUT_KEY_PAGE_DOWN;
89     case KEYCODE_HOME:
90         return GLUT_KEY_HOME;
91     case KEYCODE_END:
92         return GLUT_KEY_END;
93     case KEYCODE_INSERT:
94         return GLUT_KEY_INSERT;
95     case KEYCODE_UP:
96     //case KEYCODE_KP_UP:
97         return GLUT_KEY_UP;
98     case KEYCODE_DOWN:
99     //case KEYCODE_KP_DOWN:
100         return GLUT_KEY_DOWN;
101     case KEYCODE_LEFT:
102     //case KEYCODE_KP_LEFT:
103         return GLUT_KEY_LEFT;
104     case KEYCODE_RIGHT:
105     //case KEYCODE_KP_RIGHT:
106         return GLUT_KEY_RIGHT;
107     case KEYCODE_NUM_LOCK:
108         return GLUT_KEY_NUM_LOCK;
109     case KEYCODE_LEFT_ALT:
110         return GLUT_KEY_ALT_L;
111     case KEYCODE_RIGHT_ALT:
112         return GLUT_KEY_ALT_R;
113     case KEYCODE_LEFT_SHIFT:
114         return GLUT_KEY_SHIFT_L;
115     case KEYCODE_RIGHT_SHIFT:
116         return GLUT_KEY_SHIFT_R;
117     case KEYCODE_LEFT_CTRL:
118         return GLUT_KEY_CTRL_L;
119     case KEYCODE_RIGHT_CTRL:
120         return GLUT_KEY_CTRL_R;
121     }
122     return 0;
123 }
124
125 unsigned char key_ascii(int qnxKeycode)
126 {
127     if (qnxKeycode >= KEYCODE_PC_KEYS && qnxKeycode <= UNICODE_PRIVATE_USE_AREA_LAST) {
128         switch (qnxKeycode) {
129         case KEYCODE_BACKSPACE:
130             return 0x0008;
131         case KEYCODE_TAB:
132             return 0x0009;
133         case KEYCODE_KP_ENTER:
134         case KEYCODE_RETURN:
135             return 0x000A;
136         case KEYCODE_ESCAPE:
137             return 0x001B;
138         }
139     }
140     return qnxKeycode;
141 }
142
143 //From fg_main_x11
144 fg_time_t fgPlatformSystemTime ( void )
145 {
146 #ifdef CLOCK_MONOTONIC
147     struct timespec now;
148     clock_gettime(CLOCK_MONOTONIC, &now);
149     return now.tv_nsec/1000000 + now.tv_sec*1000;
150 #elif defined(HAVE_GETTIMEOFDAY)
151     struct timeval now;
152     gettimeofday( &now, NULL );
153     return now.tv_usec/1000 + now.tv_sec*1000;
154 #endif
155 }
156
157 /*
158  * Does the magic required to relinquish the CPU until something interesting
159  * happens.
160  */
161 void fgPlatformSleepForEvents( fg_time_t msec )
162 {
163     //XXX: Is this right? Is there a more direct way to access the context?
164     if(fgStructure.CurrentWindow && bps_get_event(&fgDisplay.pDisplay.event, (int)msec) != BPS_SUCCESS) {
165         LOGW("BPS couldn't get event");
166     }
167 }
168
169 void handle_left_mouse(int x, int y, int height, int eventType, SFG_Window* window)
170 {
171     bool handled = false;
172     /* Virtual arrows PAD */
173     /* Don't interfere with existing mouse move event */
174     if (!touchscreen.in_mmotion) {
175         struct vpad_state prev_vpad = touchscreen.vpad;
176         touchscreen.vpad.left = touchscreen.vpad.right = touchscreen.vpad.up = touchscreen.vpad.down = false;
177
178         if (eventType == SCREEN_EVENT_MTOUCH_TOUCH || eventType == SCREEN_EVENT_MTOUCH_MOVE) {
179             if ((x > 0 && x < 100) && (y > (height - 100) && y < height))
180             {
181                 touchscreen.vpad.left = true;
182             }
183             if ((x > 200 && x < 300) && (y > (height - 100) && y < height))
184             {
185                 touchscreen.vpad.right = true;
186             }
187             if ((x > 100 && x < 200) && (y > (height - 100) && y < height))
188             {
189                 touchscreen.vpad.down = true;
190             }
191             if ((x > 100 && x < 200) && (y > (height - 200) && y < (height - 100)))
192             {
193                 touchscreen.vpad.up = true;
194             }
195         }
196
197         if (eventType == SCREEN_EVENT_MTOUCH_TOUCH &&
198                 (touchscreen.vpad.left || touchscreen.vpad.right || touchscreen.vpad.down || touchscreen.vpad.up)) {
199             touchscreen.vpad.on = true;
200         }
201         if (eventType == SCREEN_EVENT_MTOUCH_RELEASE) {
202             touchscreen.vpad.on = false;
203         }
204
205         if (prev_vpad.left != touchscreen.vpad.left
206                 || prev_vpad.right != touchscreen.vpad.right
207                 || prev_vpad.up != touchscreen.vpad.up
208                 || prev_vpad.down != touchscreen.vpad.down
209                 || prev_vpad.on != touchscreen.vpad.on) {
210             if (FETCH_WCB(*window, Special)) {
211                 if (prev_vpad.left == false && touchscreen.vpad.left == true) {
212                     INVOKE_WCB(*window, Special, (GLUT_KEY_LEFT, x, y));
213                 }
214                 else if (prev_vpad.right == false && touchscreen.vpad.right == true) {
215                     INVOKE_WCB(*window, Special, (GLUT_KEY_RIGHT, x, y));
216                 }
217                 else if (prev_vpad.up == false && touchscreen.vpad.up == true) {
218                     INVOKE_WCB(*window, Special, (GLUT_KEY_UP, x, y));
219                 }
220                 else if (prev_vpad.down == false && touchscreen.vpad.down == true) {
221                     INVOKE_WCB(*window, Special, (GLUT_KEY_DOWN, x, y));
222                 }
223             }
224             if (FETCH_WCB(*window, SpecialUp)) {
225                 if (prev_vpad.left == true && touchscreen.vpad.left == false) {
226                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_LEFT, x, y));
227                 }
228                 if (prev_vpad.right == true && touchscreen.vpad.right == false) {
229                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_RIGHT, x, y));
230                 }
231                 if (prev_vpad.up == true && touchscreen.vpad.up == false) {
232                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_UP, x, y));
233                 }
234                 if (prev_vpad.down == true && touchscreen.vpad.down == false) {
235                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_DOWN, x, y));
236                 }
237             }
238             handled = true;
239         }
240     }
241
242     /* Normal mouse events */
243     if (!handled && !touchscreen.vpad.on) {
244         window->State.MouseX = x;
245         window->State.MouseY = y;
246
247         if(eventType == SCREEN_EVENT_MTOUCH_MOVE) {
248             INVOKE_WCB(*window, Motion, (x, y));
249         } else if(FETCH_WCB(*window, Mouse)) {
250             touchscreen.in_mmotion = eventType == SCREEN_EVENT_MTOUCH_TOUCH;
251             int glutTouchType = eventType == SCREEN_EVENT_MTOUCH_TOUCH ? GLUT_DOWN : GLUT_UP;
252             INVOKE_WCB(*window, Mouse, (GLUT_LEFT_BUTTON, glutTouchType, x, y));
253         }
254     }
255 }
256
257 /*
258  * Determine a GLUT modifier mask based on BlackBerry modifier info.
259  */
260 int fgPlatformGetModifiers (int mod)
261 {
262     return (((mod & KEYMOD_SHIFT) ? GLUT_ACTIVE_SHIFT : 0) |
263             ((mod & KEYMOD_CTRL) ? GLUT_ACTIVE_CTRL : 0) |
264             ((mod & KEYMOD_ALT) ? GLUT_ACTIVE_ALT : 0));
265 }
266
267 void fgPlatformProcessSingleEvent ( void )
268 {
269     int domain;
270     do
271     {
272         if(fgDisplay.pDisplay.event != NULL) {
273             SFG_Window* window = fgStructure.CurrentWindow;
274             if (window != NULL && window->Window.Handle != NULL) {
275                 int size[2];
276                 screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
277                 fghOnReshapeNotify(window,size[0],size[1],GL_FALSE);
278             }
279
280             domain = bps_event_get_domain(fgDisplay.pDisplay.event);
281             if (domain == screen_get_domain()) {
282                 int eventType;
283                 int mod;
284                 screen_event_t screenEvent = screen_event_get_event(fgDisplay.pDisplay.event);
285                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_TYPE, &eventType);
286                 switch (eventType) {
287
288                 //Mostly from fg_main_android
289                 case SCREEN_EVENT_MTOUCH_TOUCH:
290                 case SCREEN_EVENT_MTOUCH_RELEASE:
291                 case SCREEN_EVENT_MTOUCH_MOVE:
292                 {
293                     mtouch_event_t touchEvent;
294                     screen_get_mtouch_event(screenEvent, &touchEvent, 0);
295                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
296
297                     LOGI("fgPlatformProcessSingleEvent: SCREEN_EVENT_MTOUCH_*: Type: 0x%X, X: %d, Y: %d, Contact Id: %d, Mod: 0x%X", SLOG2_FA_SIGNED(eventType), SLOG2_FA_SIGNED(touchEvent.x), SLOG2_FA_SIGNED(touchEvent.y), SLOG2_FA_SIGNED(touchEvent.contact_id), SLOG2_FA_SIGNED(mod));
298
299                     /* Remember the current modifiers state so user can query it from their callback */
300                     fgState.Modifiers = fgPlatformGetModifiers(mod);
301
302                     if(touchEvent.contact_id == 0) {
303                         int size[2];
304                         screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
305                         handle_left_mouse(touchEvent.x, touchEvent.y, size[1], eventType, window);
306                     }
307
308                     //Now handle mutlitouch (adapted from fg_main_windows)
309                     if (eventType == SCREEN_EVENT_MTOUCH_TOUCH) {
310                         INVOKE_WCB( *window, MultiEntry,  ( touchEvent.contact_id, GLUT_ENTERED ) );
311                         INVOKE_WCB( *window, MultiButton, ( touchEvent.contact_id, touchEvent.x, touchEvent.y, 0, GLUT_DOWN ) );
312                     } else if (eventType == SCREEN_EVENT_MTOUCH_MOVE) {
313                         INVOKE_WCB( *window, MultiMotion, ( touchEvent.contact_id, touchEvent.x, touchEvent.y ) );
314                         //XXX No motion is performed without contact, thus MultiPassive is never used
315                     } else if (eventType == SCREEN_EVENT_MTOUCH_RELEASE) {
316                         INVOKE_WCB( *window, MultiButton, ( touchEvent.contact_id, touchEvent.x, touchEvent.y, 0, GLUT_UP ) );
317                         INVOKE_WCB( *window, MultiEntry,  ( touchEvent.contact_id, GLUT_LEFT ) );
318                     }
319                     break;
320                 }
321
322                 case SCREEN_EVENT_POINTER:
323                 {
324                     //Based off/part taken from GamePlay3d PlatformBlackBerry
325                     static int mouse_pressed = 0;
326                     int buttons;
327                     int position[2];
328                     int wheel;
329                     // A move event will be fired unless a button state changed.
330                     bool move = true;
331                     bool left_move = false;
332                     // This is a mouse move event, it is applicable to a device with a usb mouse or simulator.
333                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_BUTTONS, &buttons);
334                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_SOURCE_POSITION, position);
335                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_MOUSE_WHEEL, &wheel);
336                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
337                     int size[2];
338                     screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
339
340                     LOGI("fgPlatformProcessSingleEvent: SCREEN_EVENT_POINTER: Buttons: 0x%X, X: %d, Y: %d, Wheel: %d, Mod: 0x%X", SLOG2_FA_SIGNED(buttons), SLOG2_FA_SIGNED(position[0]), SLOG2_FA_SIGNED(position[1]), SLOG2_FA_SIGNED(wheel), SLOG2_FA_SIGNED(mod));
341
342                     //XXX Should multitouch be handled?
343
344                     /* Remember the current modifiers state so user can query it from their callback */
345                     fgState.Modifiers = fgPlatformGetModifiers(mod);
346
347                     // Handle left mouse. Interpret as touch if the left mouse event is not consumed.
348                     if (buttons & SCREEN_LEFT_MOUSE_BUTTON) {
349                         if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON) {
350                             left_move = true;
351                         } else {
352                             move = false;
353                             mouse_pressed |= SCREEN_LEFT_MOUSE_BUTTON;
354                             handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_TOUCH, window);
355                         }
356                     } else if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON) {
357                         move = false;
358                         mouse_pressed &= ~SCREEN_LEFT_MOUSE_BUTTON;
359                         handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_RELEASE, window);
360                     }
361
362                     // Handle right mouse.
363                     if (buttons & SCREEN_RIGHT_MOUSE_BUTTON) {
364                         if ((mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
365                             move = false;
366                             mouse_pressed |= SCREEN_RIGHT_MOUSE_BUTTON;
367                             INVOKE_WCB(*window, Mouse, (GLUT_RIGHT_BUTTON, GLUT_DOWN, position[0], position[1]));
368                         }
369                     } else if (mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) {
370                         move = false;
371                         mouse_pressed &= ~SCREEN_RIGHT_MOUSE_BUTTON;
372                         INVOKE_WCB(*window, Mouse, (GLUT_RIGHT_BUTTON, GLUT_UP, position[0], position[1]));
373                     }
374
375                     // Handle middle mouse.
376                     if (buttons & SCREEN_MIDDLE_MOUSE_BUTTON) {
377                         if ((mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
378                             move = false;
379                             mouse_pressed |= SCREEN_MIDDLE_MOUSE_BUTTON;
380                             INVOKE_WCB(*window, Mouse, (GLUT_MIDDLE_BUTTON, GLUT_DOWN, position[0], position[1]));
381                         }
382                     } else if (mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) {
383                         move = false;
384                         mouse_pressed &= ~SCREEN_MIDDLE_MOUSE_BUTTON;
385                         INVOKE_WCB(*window, Mouse, (GLUT_MIDDLE_BUTTON, GLUT_UP, position[0], position[1]));
386                     }
387
388                     // Fire a move event if none of the buttons changed.
389                     if (left_move || move) {
390                         handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_MOVE, window);
391                     }
392
393                     if (wheel) {
394                         fgState.MouseWheelTicks -= wheel;
395                         //TODO: Implement wheel support (based on fg_main_mswin... though it seems excessive)
396                     }
397                     break;
398                 }
399
400                 //Based off fg_main_android
401                 case SCREEN_EVENT_KEYBOARD:
402                 {
403                     int flags;
404                     int value;
405                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_FLAGS, &flags);
406                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_SYM, &value);
407                     screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
408
409                     LOGI("fgPlatformProcessSingleEvent: SCREEN_EVENT_KEYBOARD. Flags: 0x%X, Sym: 0x%X, Mod: 0x%X", SLOG2_FA_SIGNED(flags), SLOG2_FA_SIGNED(value), SLOG2_FA_SIGNED(mod));
410
411                     /* Suppress key repeats if desired. Based off fg_main_mswin */
412                     if ((flags & KEY_REPEAT) == 0 || (fgState.KeyRepeat == GLUT_KEY_REPEAT_OFF && fgStructure.CurrentWindow->State.IgnoreKeyRepeat == GL_TRUE)) {
413                         unsigned int keypress = 0;
414                         unsigned char ascii = 0;
415
416                         /* Remember the current modifiers state so user can query it from their callback */
417                         fgState.Modifiers = fgPlatformGetModifiers(mod);
418
419                         /* Process keys */
420                         if ((keypress = key_special(value))) {
421                             if(flags & KEY_DOWN) {
422                                 INVOKE_WCB(*window, Special, (keypress, window->State.MouseX, window->State.MouseY));
423                             } else {
424                                 INVOKE_WCB(*window, SpecialUp, (keypress, window->State.MouseX, window->State.MouseY));
425                             }
426                         } else if((flags & KEY_SYM_VALID) && (ascii = key_ascii(value))) {
427                             if(flags & KEY_DOWN) {
428                                 INVOKE_WCB(*window, Keyboard, (ascii, window->State.MouseX, window->State.MouseY));
429                             } else {
430                                 INVOKE_WCB(*window, KeyboardUp, (ascii, window->State.MouseX, window->State.MouseY));
431                             }
432                         } else {
433                             LOGW("fgPlatformProcessSingleEvent: SCREEN_EVENT_KEYBOARD. Unhandled key event");
434                         }
435                     }
436                     break;
437                 }
438
439                 case SCREEN_EVENT_PROPERTY:
440                 case SCREEN_EVENT_IDLE:
441                     break;
442
443                 default:
444                     LOGW("fgPlatformProcessSingleEvent: unknown screen event: 0x%X", SLOG2_FA_SIGNED(eventType));
445                     break;
446                 }
447             } else if (domain == navigator_get_domain()) {
448                 int eventType = bps_event_get_code(fgDisplay.pDisplay.event);
449                 switch (eventType) {
450
451                 case NAVIGATOR_WINDOW_STATE:
452                 {
453                     LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE");
454                     navigator_window_state_t state = navigator_event_get_window_state(fgDisplay.pDisplay.event);
455                     switch (state)
456                     {
457                     case NAVIGATOR_WINDOW_FULLSCREEN:
458                         LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_FULLSCREEN");
459                         INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_RESUME));
460                         break;
461                     case NAVIGATOR_WINDOW_THUMBNAIL:
462                     case NAVIGATOR_WINDOW_INVISIBLE:
463                         LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_THUMBNAIL/NAVIGATOR_WINDOW_INVISIBLE");
464                         INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_PAUSE));
465                         break;
466                     default:
467                         LOGW("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE unknown: 0x%X", SLOG2_FA_SIGNED(state));
468                         break;
469                     }
470                     break;
471                 }
472
473                 case NAVIGATOR_EXIT:
474                 {
475                     LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_EXIT");
476
477                     fgPlatformMainLoopPostWork();
478
479                     /* User closed the application for good, let's kill the window */
480                     SFG_Window* window = fgStructure.CurrentWindow;
481                     if (window != NULL) {
482                         fgDestroyWindow(window);
483                     } else {
484                         LOGW("NAVIGATOR_EXIT: No current window");
485                     }
486                     break;
487                 }
488
489                 case NAVIGATOR_SWIPE_DOWN:
490                 case NAVIGATOR_BACK:
491                 case NAVIGATOR_WINDOW_ACTIVE:
492                 case NAVIGATOR_WINDOW_INACTIVE:
493                 case NAVIGATOR_KEYBOARD_STATE:
494                 case NAVIGATOR_KEYBOARD_POSITION:
495                 case NAVIGATOR_DEVICE_LOCK_STATE:
496                 case NAVIGATOR_WINDOW_COVER:
497                 case NAVIGATOR_WINDOW_COVER_ENTER:
498                 case NAVIGATOR_WINDOW_COVER_EXIT:
499                 case NAVIGATOR_APP_STATE:
500                     //XXX Should probably do something with these
501                     break;
502
503                 default:
504                     LOGW("fgPlatformProcessSingleEvent: unknown navigator event: 0x%X", SLOG2_FA_SIGNED(eventType));
505                     break;
506                 }
507             }
508         }
509     } while(bps_get_event(&fgDisplay.pDisplay.event, 1) == BPS_SUCCESS && fgDisplay.pDisplay.event != NULL);
510
511     /* Reset event to reduce chances of triggering something */
512     fgDisplay.pDisplay.event = NULL;
513 }
514
515 void fgPlatformMainLoopPreliminaryWork ( void )
516 {
517     LOGI("fgPlatformMainLoopPreliminaryWork");
518
519     /* Request navigator events */
520     navigator_request_events(0);
521     //XXX rotation lock? navigator_rotation_lock(true);
522
523     /* Request window events */
524     screen_request_events(fgDisplay.pDisplay.screenContext);
525 }
526
527 void fgPlatformMainLoopPostWork ( void )
528 {
529     LOGI("fgPlatformMainLoopPostWork");
530
531     screen_stop_events(fgDisplay.pDisplay.screenContext);
532
533     navigator_stop_events(0);
534 }
535
536 /* deal with work list items */
537 void fgPlatformInitWork(SFG_Window* window)
538 {
539     /* notify windowStatus/visibility */
540     INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
541
542     /* Position callback, always at 0,0 */
543     fghOnPositionNotify(window, 0, 0, GL_TRUE);
544
545     /* Size gets notified on window creation with size detection in mainloop above
546      * XXX CHECK: does this messages happen too early like on windows,
547      * so client code cannot have registered a callback yet and the message
548      * is thus never received by client?
549      */
550 }
551
552 void fgPlatformPosResZordWork(SFG_Window* window, unsigned int workMask)
553 {
554     if (workMask & GLUT_FULL_SCREEN_WORK)
555         fgPlatformFullScreenToggle( window );
556     if (workMask & GLUT_POSITION_WORK)
557         fgPlatformPositionWindow( window, window->State.DesiredXpos, window->State.DesiredYpos );
558     if (workMask & GLUT_SIZE_WORK)
559         fgPlatformReshapeWindow ( window, window->State.DesiredWidth, window->State.DesiredHeight );
560     if (workMask & GLUT_ZORDER_WORK)
561     {
562         if (window->State.DesiredZOrder < 0)
563             fgPlatformPushWindow( window );
564         else
565             fgPlatformPopWindow( window );
566     }
567 }
568
569 void fgPlatformVisibilityWork(SFG_Window* window)
570 {
571     /* Visibility status of window should get updated in the window message handlers
572      * For now, none of these functions called below do anything, so don't worry
573      * about it
574      */
575     SFG_Window *win = window;
576     switch (window->State.DesiredVisibility)
577     {
578     case DesireHiddenState:
579         fgPlatformHideWindow( window );
580         break;
581     case DesireIconicState:
582         /* Call on top-level window */
583         while (win->Parent)
584             win = win->Parent;
585         fgPlatformIconifyWindow( win );
586         break;
587     case DesireNormalState:
588         fgPlatformShowWindow( window );
589         break;
590     }
591 }