6d2d03c7308fd93ce98ad2a507adb021e032c53a
[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 extern void fgPlatformRotateWindow( SFG_Window *window, int rotation );
56
57 static struct touchscreen touchscreen;
58
59 unsigned int key_special(int qnxKeycode)
60 {
61     switch(qnxKeycode) {
62     case KEYCODE_F1:
63         return GLUT_KEY_F1;
64     case KEYCODE_F2:
65         return GLUT_KEY_F2;
66     case KEYCODE_F3:
67         return GLUT_KEY_F3;
68     case KEYCODE_F4:
69         return GLUT_KEY_F4;
70     case KEYCODE_F5:
71         return GLUT_KEY_F5;
72     case KEYCODE_F6:
73         return GLUT_KEY_F6;
74     case KEYCODE_F7:
75         return GLUT_KEY_F7;
76     case KEYCODE_F8:
77         return GLUT_KEY_F8;
78     case KEYCODE_F9:
79         return GLUT_KEY_F9;
80     case KEYCODE_F10:
81         return GLUT_KEY_F10;
82     case KEYCODE_F11:
83         return GLUT_KEY_F11;
84     case KEYCODE_F12:
85         return GLUT_KEY_F12;
86     case KEYCODE_PG_UP:
87         return GLUT_KEY_PAGE_UP;
88     case KEYCODE_PG_DOWN:
89         return GLUT_KEY_PAGE_DOWN;
90     case KEYCODE_HOME:
91         return GLUT_KEY_HOME;
92     case KEYCODE_END:
93         return GLUT_KEY_END;
94     case KEYCODE_INSERT:
95         return GLUT_KEY_INSERT;
96     case KEYCODE_UP:
97     //case KEYCODE_KP_UP:
98         return GLUT_KEY_UP;
99     case KEYCODE_DOWN:
100     //case KEYCODE_KP_DOWN:
101         return GLUT_KEY_DOWN;
102     case KEYCODE_LEFT:
103     //case KEYCODE_KP_LEFT:
104         return GLUT_KEY_LEFT;
105     case KEYCODE_RIGHT:
106     //case KEYCODE_KP_RIGHT:
107         return GLUT_KEY_RIGHT;
108     case KEYCODE_NUM_LOCK:
109         return GLUT_KEY_NUM_LOCK;
110     case KEYCODE_LEFT_ALT:
111         return GLUT_KEY_ALT_L;
112     case KEYCODE_RIGHT_ALT:
113         return GLUT_KEY_ALT_R;
114     case KEYCODE_LEFT_SHIFT:
115         return GLUT_KEY_SHIFT_L;
116     case KEYCODE_RIGHT_SHIFT:
117         return GLUT_KEY_SHIFT_R;
118     case KEYCODE_LEFT_CTRL:
119         return GLUT_KEY_CTRL_L;
120     case KEYCODE_RIGHT_CTRL:
121         return GLUT_KEY_CTRL_R;
122     }
123     return 0;
124 }
125
126 unsigned char key_ascii(int qnxKeycode)
127 {
128     if (qnxKeycode >= KEYCODE_PC_KEYS && qnxKeycode <= UNICODE_PRIVATE_USE_AREA_LAST) {
129         switch (qnxKeycode) {
130         case KEYCODE_BACKSPACE:
131             return 0x0008;
132         case KEYCODE_TAB:
133             return 0x0009;
134         case KEYCODE_KP_ENTER:
135         case KEYCODE_RETURN:
136             return 0x000A;
137         case KEYCODE_ESCAPE:
138             return 0x001B;
139         }
140     }
141     return qnxKeycode;
142 }
143
144 //From fg_main_x11
145 fg_time_t fgPlatformSystemTime ( void )
146 {
147 #ifdef CLOCK_MONOTONIC
148     struct timespec now;
149     clock_gettime(CLOCK_MONOTONIC, &now);
150     return now.tv_nsec/1000000 + now.tv_sec*1000;
151 #elif defined(HAVE_GETTIMEOFDAY)
152     struct timeval now;
153     gettimeofday( &now, NULL );
154     return now.tv_usec/1000 + now.tv_sec*1000;
155 #endif
156 }
157
158 /*
159  * Does the magic required to relinquish the CPU until something interesting
160  * happens.
161  */
162 void fgPlatformSleepForEvents( fg_time_t msec )
163 {
164     //XXX: Is this right? Is there a more direct way to access the context?
165     if(fgStructure.CurrentWindow && fgDisplay.pDisplay.event == NULL && bps_get_event(&fgDisplay.pDisplay.event, (int)msec) != BPS_SUCCESS) {
166         LOGW("BPS couldn't get event");
167     }
168 }
169
170 void handle_left_mouse(int x, int y, int height, int eventType, SFG_Window* window)
171 {
172     bool handled = false;
173     /* Virtual arrows PAD */
174     /* Don't interfere with existing mouse move event */
175     if (!touchscreen.in_mmotion) {
176         struct vpad_state prev_vpad = touchscreen.vpad;
177         touchscreen.vpad.left = touchscreen.vpad.right = touchscreen.vpad.up = touchscreen.vpad.down = false;
178
179         if (eventType == SCREEN_EVENT_MTOUCH_TOUCH || eventType == SCREEN_EVENT_MTOUCH_MOVE) {
180             if ((x > 0 && x < 100) && (y > (height - 100) && y < height))
181             {
182                 touchscreen.vpad.left = true;
183             }
184             if ((x > 200 && x < 300) && (y > (height - 100) && y < height))
185             {
186                 touchscreen.vpad.right = true;
187             }
188             if ((x > 100 && x < 200) && (y > (height - 100) && y < height))
189             {
190                 touchscreen.vpad.down = true;
191             }
192             if ((x > 100 && x < 200) && (y > (height - 200) && y < (height - 100)))
193             {
194                 touchscreen.vpad.up = true;
195             }
196         }
197
198         if (eventType == SCREEN_EVENT_MTOUCH_TOUCH &&
199                 (touchscreen.vpad.left || touchscreen.vpad.right || touchscreen.vpad.down || touchscreen.vpad.up)) {
200             touchscreen.vpad.on = true;
201         }
202         if (eventType == SCREEN_EVENT_MTOUCH_RELEASE) {
203             touchscreen.vpad.on = false;
204         }
205
206         if (prev_vpad.left != touchscreen.vpad.left
207                 || prev_vpad.right != touchscreen.vpad.right
208                 || prev_vpad.up != touchscreen.vpad.up
209                 || prev_vpad.down != touchscreen.vpad.down
210                 || prev_vpad.on != touchscreen.vpad.on) {
211             if (FETCH_WCB(*window, Special)) {
212                 if (prev_vpad.left == false && touchscreen.vpad.left == true) {
213                     INVOKE_WCB(*window, Special, (GLUT_KEY_LEFT, x, y));
214                 }
215                 else if (prev_vpad.right == false && touchscreen.vpad.right == true) {
216                     INVOKE_WCB(*window, Special, (GLUT_KEY_RIGHT, x, y));
217                 }
218                 else if (prev_vpad.up == false && touchscreen.vpad.up == true) {
219                     INVOKE_WCB(*window, Special, (GLUT_KEY_UP, x, y));
220                 }
221                 else if (prev_vpad.down == false && touchscreen.vpad.down == true) {
222                     INVOKE_WCB(*window, Special, (GLUT_KEY_DOWN, x, y));
223                 }
224             }
225             if (FETCH_WCB(*window, SpecialUp)) {
226                 if (prev_vpad.left == true && touchscreen.vpad.left == false) {
227                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_LEFT, x, y));
228                 }
229                 if (prev_vpad.right == true && touchscreen.vpad.right == false) {
230                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_RIGHT, x, y));
231                 }
232                 if (prev_vpad.up == true && touchscreen.vpad.up == false) {
233                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_UP, x, y));
234                 }
235                 if (prev_vpad.down == true && touchscreen.vpad.down == false) {
236                     INVOKE_WCB(*window, SpecialUp, (GLUT_KEY_DOWN, x, y));
237                 }
238             }
239             handled = true;
240         }
241     }
242
243     /* Normal mouse events */
244     if (!handled && !touchscreen.vpad.on) {
245         window->State.MouseX = x;
246         window->State.MouseY = y;
247
248         if(eventType == SCREEN_EVENT_MTOUCH_MOVE) {
249             INVOKE_WCB(*window, Motion, (x, y));
250         } else if(FETCH_WCB(*window, Mouse)) {
251             touchscreen.in_mmotion = eventType == SCREEN_EVENT_MTOUCH_TOUCH;
252             int glutTouchType = eventType == SCREEN_EVENT_MTOUCH_TOUCH ? GLUT_DOWN : GLUT_UP;
253             INVOKE_WCB(*window, Mouse, (GLUT_LEFT_BUTTON, glutTouchType, x, y));
254         }
255     }
256 }
257
258 /*
259  * Determine a GLUT modifier mask based on BlackBerry modifier info.
260  */
261 int fgPlatformGetModifiers (int mod)
262 {
263     return (((mod & KEYMOD_SHIFT) ? GLUT_ACTIVE_SHIFT : 0) |
264             ((mod & KEYMOD_CTRL) ? GLUT_ACTIVE_CTRL : 0) |
265             ((mod & KEYMOD_ALT) ? GLUT_ACTIVE_ALT : 0));
266 }
267
268 void fgPlatformProcessSingleEvent ( void )
269 {
270     if(fgStructure.CurrentWindow == NULL) {
271         //XXX Is this right? Would this just cause a whole lot of busy looping while we wait for events?
272         LOGW("fgPlatformProcessSingleEvent: Missing current window. Skipping event processing");
273         return;
274     }
275
276     if(fgDisplay.pDisplay.event == NULL)
277         /* Nothing to do */
278         return;
279
280     int domain;
281     do
282     {
283         SFG_Window* window = fgStructure.CurrentWindow;
284         domain = bps_event_get_domain(fgDisplay.pDisplay.event);
285         if (domain == screen_get_domain()) {
286             int eventType;
287             int mod;
288             screen_event_t screenEvent = screen_event_get_event(fgDisplay.pDisplay.event);
289             screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_TYPE, &eventType);
290             switch (eventType) {
291
292             //Mostly from fg_main_android
293             case SCREEN_EVENT_MTOUCH_TOUCH:
294             case SCREEN_EVENT_MTOUCH_RELEASE:
295             case SCREEN_EVENT_MTOUCH_MOVE:
296             {
297                 mtouch_event_t touchEvent;
298                 screen_get_mtouch_event(screenEvent, &touchEvent, 0);
299                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
300
301                 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));
302
303                 /* Remember the current modifiers state so user can query it from their callback */
304                 fgState.Modifiers = fgPlatformGetModifiers(mod);
305
306                 if(touchEvent.contact_id == 0) {
307                     int size[2];
308                     screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
309                     handle_left_mouse(touchEvent.x, touchEvent.y, size[1], eventType, window);
310                 }
311
312                 //Now handle mutlitouch (adapted from fg_main_windows)
313                 if (eventType == SCREEN_EVENT_MTOUCH_TOUCH) {
314                     INVOKE_WCB( *window, MultiEntry,  ( touchEvent.contact_id, GLUT_ENTERED ) );
315                     INVOKE_WCB( *window, MultiButton, ( touchEvent.contact_id, touchEvent.x, touchEvent.y, 0, GLUT_DOWN ) );
316                 } else if (eventType == SCREEN_EVENT_MTOUCH_MOVE) {
317                     INVOKE_WCB( *window, MultiMotion, ( touchEvent.contact_id, touchEvent.x, touchEvent.y ) );
318                     //XXX No motion is performed without contact, thus MultiPassive is never used
319                 } else if (eventType == SCREEN_EVENT_MTOUCH_RELEASE) {
320                     INVOKE_WCB( *window, MultiButton, ( touchEvent.contact_id, touchEvent.x, touchEvent.y, 0, GLUT_UP ) );
321                     INVOKE_WCB( *window, MultiEntry,  ( touchEvent.contact_id, GLUT_LEFT ) );
322                 }
323
324                 fgState.Modifiers = INVALID_MODIFIERS;
325                 break;
326             }
327
328             case SCREEN_EVENT_POINTER:
329             {
330                 //Based off/part taken from GamePlay3d PlatformBlackBerry
331                 static int mouse_pressed = 0;
332                 int buttons;
333                 int position[2];
334                 int wheel;
335                 // A move event will be fired unless a button state changed.
336                 bool move = true;
337                 bool left_move = false;
338                 // This is a mouse move event, it is applicable to a device with a usb mouse or simulator.
339                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_BUTTONS, &buttons);
340                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_SOURCE_POSITION, position);
341                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_MOUSE_WHEEL, &wheel);
342                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
343                 int size[2];
344                 screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
345
346                 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));
347
348                 //XXX Should multitouch be handled?
349
350                 /* Remember the current modifiers state so user can query it from their callback */
351                 fgState.Modifiers = fgPlatformGetModifiers(mod);
352
353                 // Handle left mouse. Interpret as touch if the left mouse event is not consumed.
354                 if (buttons & SCREEN_LEFT_MOUSE_BUTTON) {
355                     if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON) {
356                         left_move = true;
357                     } else {
358                         move = false;
359                         mouse_pressed |= SCREEN_LEFT_MOUSE_BUTTON;
360                         handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_TOUCH, window);
361                     }
362                 } else if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON) {
363                     move = false;
364                     mouse_pressed &= ~SCREEN_LEFT_MOUSE_BUTTON;
365                     handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_RELEASE, window);
366                 }
367
368                 // Handle right mouse.
369                 if (buttons & SCREEN_RIGHT_MOUSE_BUTTON) {
370                     if ((mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
371                         move = false;
372                         mouse_pressed |= SCREEN_RIGHT_MOUSE_BUTTON;
373                         INVOKE_WCB(*window, Mouse, (GLUT_RIGHT_BUTTON, GLUT_DOWN, position[0], position[1]));
374                     }
375                 } else if (mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) {
376                     move = false;
377                     mouse_pressed &= ~SCREEN_RIGHT_MOUSE_BUTTON;
378                     INVOKE_WCB(*window, Mouse, (GLUT_RIGHT_BUTTON, GLUT_UP, position[0], position[1]));
379                 }
380
381                 // Handle middle mouse.
382                 if (buttons & SCREEN_MIDDLE_MOUSE_BUTTON) {
383                     if ((mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
384                         move = false;
385                         mouse_pressed |= SCREEN_MIDDLE_MOUSE_BUTTON;
386                         INVOKE_WCB(*window, Mouse, (GLUT_MIDDLE_BUTTON, GLUT_DOWN, position[0], position[1]));
387                     }
388                 } else if (mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) {
389                     move = false;
390                     mouse_pressed &= ~SCREEN_MIDDLE_MOUSE_BUTTON;
391                     INVOKE_WCB(*window, Mouse, (GLUT_MIDDLE_BUTTON, GLUT_UP, position[0], position[1]));
392                 }
393
394                 // Fire a move event if none of the buttons changed.
395                 if (left_move || move) {
396                     handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_MOVE, window);
397                 }
398
399                 if (wheel) {
400                     /* Very slightly modified from fg_main_mswin.
401                      * Because we don't want MouseWheel to be called every. single. time.
402                      * That the action occurs, we mimic the Windows version with "wheel deltas"
403                      * XXX Do we even want this?
404                      * XXX If we want this, it's possible to get horizontal scroll as well.
405                      * XXX -Vertical scroll=wheel 0, horizontal=wheel 1? */
406                     fgState.MouseWheelTicks -= wheel;
407                     if (abs(fgState.MouseWheelTicks) >= WHEEL_DELTA)
408                     {
409                         int wheel_number = 0;
410                         int direction = (fgState.MouseWheelTicks > 0) ? -1 : 1;
411
412                         if (!FETCH_WCB(*window, MouseWheel) && !FETCH_WCB(*window, Mouse))
413                             break;
414
415                         //XXX fgSetWindow(window);
416
417                         while(abs(fgState.MouseWheelTicks) >= WHEEL_DELTA)
418                         {
419                             if (FETCH_WCB(*window, MouseWheel))
420                                 INVOKE_WCB(*window, MouseWheel, (wheel_number, direction, window->State.MouseX, window->State.MouseY));
421                             else /* No mouse wheel, call the mouse button callback twice */
422                             {
423                                 /*
424                                  * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
425                                  *  "    "   one                     +1 to 5, -1 to 6, ...
426                                  *
427                                  * XXX The below assumes that you have no more than 3 mouse
428                                  * XXX buttons.  Sorry.
429                                  */
430                                 int button = wheel_number * 2 + 3;
431                                 if (direction < 0)
432                                     ++button;
433                                 INVOKE_WCB(*window, Mouse, (button, GLUT_DOWN, window->State.MouseX, window->State.MouseY));
434                                 INVOKE_WCB(*window, Mouse, (button, GLUT_UP, window->State.MouseX, window->State.MouseY));
435                             }
436
437                             fgState.MouseWheelTicks -= WHEEL_DELTA * direction;
438                         }
439                     }
440                 }
441
442                 fgState.Modifiers = INVALID_MODIFIERS;
443                 break;
444             }
445
446             //Based off fg_main_android
447             case SCREEN_EVENT_KEYBOARD:
448             {
449                 int flags;
450                 int value;
451                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_FLAGS, &flags);
452                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_SYM, &value);
453                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
454
455                 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));
456
457                 /* Suppress key repeats if desired. Based off fg_main_mswin */
458                 if ((flags & KEY_REPEAT) == 0 || (fgState.KeyRepeat == GLUT_KEY_REPEAT_OFF && fgStructure.CurrentWindow->State.IgnoreKeyRepeat == GL_TRUE)) {
459                     unsigned int keypress = 0;
460                     unsigned char ascii = 0;
461
462                     /* Remember the current modifiers state so user can query it from their callback */
463                     fgState.Modifiers = fgPlatformGetModifiers(mod);
464
465                     /* Process keys */
466                     if ((keypress = key_special(value))) {
467                         if(flags & KEY_DOWN) {
468                             INVOKE_WCB(*window, Special, (keypress, window->State.MouseX, window->State.MouseY));
469                         } else {
470                             INVOKE_WCB(*window, SpecialUp, (keypress, window->State.MouseX, window->State.MouseY));
471                         }
472                     } else if((flags & KEY_SYM_VALID) && (ascii = key_ascii(value))) {
473                         if(flags & KEY_DOWN) {
474                             INVOKE_WCB(*window, Keyboard, (ascii, window->State.MouseX, window->State.MouseY));
475                         } else {
476                             INVOKE_WCB(*window, KeyboardUp, (ascii, window->State.MouseX, window->State.MouseY));
477                         }
478                     } else {
479                         LOGW("fgPlatformProcessSingleEvent: SCREEN_EVENT_KEYBOARD. Unhandled key event");
480                     }
481
482                     fgState.Modifiers = INVALID_MODIFIERS;
483                 }
484                 break;
485             }
486
487             case SCREEN_EVENT_PROPERTY:
488             case SCREEN_EVENT_IDLE:
489                 break;
490
491             default:
492                 LOGW("fgPlatformProcessSingleEvent: unknown screen event: 0x%X", SLOG2_FA_SIGNED(eventType));
493                 break;
494             }
495         } else if (domain == navigator_get_domain()) {
496             int eventType = bps_event_get_code(fgDisplay.pDisplay.event);
497             switch (eventType) {
498
499             case NAVIGATOR_WINDOW_STATE:
500             {
501                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE");
502                 navigator_window_state_t state = navigator_event_get_window_state(fgDisplay.pDisplay.event);
503                 switch (state)
504                 {
505                 case NAVIGATOR_WINDOW_FULLSCREEN:
506                     LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_FULLSCREEN");
507                     window->State.Visible = GL_TRUE;
508                     INVOKE_WCB(*window, WindowStatus, (GLUT_FULLY_RETAINED));
509                     break;
510                 case NAVIGATOR_WINDOW_THUMBNAIL:
511                     LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_THUMBNAIL");
512                     window->State.Visible = GL_TRUE;
513                     INVOKE_WCB(*window, WindowStatus, (GLUT_PARTIALLY_RETAINED));
514                     break;
515                 case NAVIGATOR_WINDOW_INVISIBLE:
516                     LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_INVISIBLE");
517                     window->State.Visible = GL_FALSE;
518                     INVOKE_WCB(*window, WindowStatus, (GLUT_HIDDEN)); //XXX Should this be GLUT_FULLY_COVERED?
519                     break;
520                 default:
521                     LOGW("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE unknown: 0x%X", SLOG2_FA_SIGNED(state));
522                     break;
523                 }
524                 break;
525             }
526
527             case NAVIGATOR_EXIT:
528             {
529                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_EXIT");
530
531                 fgPlatformMainLoopPostWork();
532
533                 /* User closed the application for good, let's kill the window */
534                 SFG_Window* window = fgStructure.CurrentWindow;
535                 if (window != NULL) {
536                     fgDestroyWindow(window);
537                 } else {
538                     LOGW("NAVIGATOR_EXIT: No current window");
539                 }
540                 break;
541             }
542
543             case NAVIGATOR_SWIPE_DOWN:
544                 /* XXX Open app menu */
545                 break;
546
547             /* Orientation is a bunch of handshakes.
548                - First the app get's asked if it wants to rotate (NAVIGATOR_ORIENTATION_CHECK)
549                - If the app wants to rotate, then it will be told what size it will be after rotate (NAVIGATOR_ORIENTATION_SIZE).
550                - Once the OS confirms that it's ready to rotate, it tells the app to handle rotation (NAVIGATOR_ORIENTATION).
551                - Once rotation is complete, the OS tells the app it's done (NAVIGATOR_ORIENTATION_DONE) */
552             case NAVIGATOR_ORIENTATION_CHECK:
553                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_ORIENTATION_CHECK");
554
555                 /* Reset sizes */
556                 window->State.pWState.newWidth = 0;
557                 window->State.pWState.newHeight = 0;
558
559                 /* Notify that we want to rotate */
560                 navigator_orientation_check_response(fgDisplay.pDisplay.event, true);
561                 break;
562
563             case NAVIGATOR_ORIENTATION:
564                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_ORIENTATION");
565
566                 /* Rotate and resize the window */
567                 fgPlatformRotateWindow(window, navigator_event_get_orientation_angle(fgDisplay.pDisplay.event));
568                 fghOnReshapeNotify(window, window->State.pWState.newWidth, window->State.pWState.newHeight, GL_FALSE);
569
570                 /* Reset sizes */
571                 window->State.pWState.newWidth = 0;
572                 window->State.pWState.newHeight = 0;
573
574                 /* Done rotating */
575                 navigator_done_orientation(fgDisplay.pDisplay.event);
576                 break;
577
578             case NAVIGATOR_BACK:
579                 /* XXX Should this be a Special/SpecialUp event? */
580                 break;
581
582             case NAVIGATOR_WINDOW_ACTIVE:
583                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_ACTIVE");
584                 INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_RESUME));
585                 break;
586
587             case NAVIGATOR_WINDOW_INACTIVE:
588                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_INACTIVE");
589                 INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_PAUSE));
590                 break;
591
592             case NAVIGATOR_ORIENTATION_DONE:
593             case NAVIGATOR_ORIENTATION_RESULT:
594                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_ORIENTATION_DONE\NAVIGATOR_ORIENTATION_RESULT");
595                 break;
596
597             case NAVIGATOR_KEYBOARD_STATE:
598                 /* XXX Should something be done with this? */
599                 break;
600
601             case NAVIGATOR_KEYBOARD_POSITION:
602                 /* TODO Invoke resize with the modified screen size (((y + height) - keyboardPos) - y).
603                  * If result is less then zero then window is covered. If == height, then no change in size. Else, change in size */
604                 break;
605
606             case NAVIGATOR_DEVICE_LOCK_STATE:
607                 break;
608
609             case NAVIGATOR_WINDOW_COVER:
610             case NAVIGATOR_WINDOW_COVER_ENTER:
611             case NAVIGATOR_WINDOW_COVER_EXIT:
612                 /* BlackBerry specific. Let app status and window status take care of everything */
613                 break;
614
615             case NAVIGATOR_APP_STATE:
616                 /* Can do the same as NAVIGATOR_WINDOW_ACTIVE/NAVIGATOR_WINDOW_INACTIVE but
617                    seems less likely to work when the app comes to the foreground. Might be a bug */
618                 break;
619
620             case NAVIGATOR_ORIENTATION_SIZE:
621                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_ORIENTATION_SIZE");
622
623                 /* Get new window size */
624                 window->State.pWState.newWidth = navigator_event_get_orientation_size_width(fgDisplay.pDisplay.event);
625                 window->State.pWState.newHeight = navigator_event_get_orientation_size_height(fgDisplay.pDisplay.event);
626                 break;
627
628             case 0: //Doesn't exist in header, but shows up when keyboard shows and resizes
629                 break;
630
631             default:
632                 LOGW("fgPlatformProcessSingleEvent: unknown navigator event: 0x%X", SLOG2_FA_SIGNED(eventType));
633                 break;
634             }
635         }
636     } while(bps_get_event(&fgDisplay.pDisplay.event, 1) == BPS_SUCCESS && fgDisplay.pDisplay.event != NULL);
637
638     /* Reset event to reduce chances of triggering something */
639     fgDisplay.pDisplay.event = NULL;
640 }
641
642 void fgPlatformMainLoopPreliminaryWork ( void )
643 {
644     LOGI("fgPlatformMainLoopPreliminaryWork");
645
646     /* Request navigator events */
647     navigator_request_events(0);
648
649     /* Allow rotation */
650     navigator_rotation_lock(false);
651
652     /* Request window events */
653     screen_request_events(fgDisplay.pDisplay.screenContext);
654 }
655
656 void fgPlatformMainLoopPostWork ( void )
657 {
658     LOGI("fgPlatformMainLoopPostWork");
659
660     /* Stop all events */
661     screen_stop_events(fgDisplay.pDisplay.screenContext);
662
663     navigator_stop_events(0);
664 }
665
666 /* deal with work list items */
667 void fgPlatformInitWork(SFG_Window* window)
668 {
669     LOGI("fgPlatformInitWork");
670
671     /* Position callback, always at 0,0 */
672     fghOnPositionNotify(window, 0, 0, GL_TRUE);
673
674     /* Get window size */
675     int size[2];
676     screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
677     fghOnReshapeNotify(window, size[0], size[1], GL_FALSE);
678
679     /* Size gets notified on window creation with size detection in mainloop above
680      * XXX CHECK: does this messages happen too early like on windows,
681      * so client code cannot have registered a callback yet and the message
682      * is thus never received by client?
683      */
684 }
685
686 void fgPlatformPosResZordWork(SFG_Window* window, unsigned int workMask)
687 {
688     if (workMask & GLUT_FULL_SCREEN_WORK)
689         fgPlatformFullScreenToggle( window );
690     if (workMask & GLUT_POSITION_WORK)
691         fgPlatformPositionWindow( window, window->State.DesiredXpos, window->State.DesiredYpos );
692     if (workMask & GLUT_SIZE_WORK)
693         fgPlatformReshapeWindow ( window, window->State.DesiredWidth, window->State.DesiredHeight );
694     if (workMask & GLUT_ZORDER_WORK)
695     {
696         if (window->State.DesiredZOrder < 0)
697             fgPlatformPushWindow( window );
698         else
699             fgPlatformPopWindow( window );
700     }
701 }
702
703 void fgPlatformVisibilityWork(SFG_Window* window)
704 {
705     /* Visibility status of window should get updated in the window message handlers
706      * For now, none of these functions called below do anything, so don't worry
707      * about it
708      */
709     SFG_Window *win = window;
710     switch (window->State.DesiredVisibility)
711     {
712     case DesireHiddenState:
713         fgPlatformHideWindow( window );
714         break;
715     case DesireIconicState:
716         /* Call on top-level window */
717         while (win->Parent)
718             win = win->Parent;
719         fgPlatformIconifyWindow( win );
720         break;
721     case DesireNormalState:
722         fgPlatformShowWindow( window );
723         break;
724     }
725 }