Added support for minimizing window.
[freeglut] / src / blackberry / fg_main_blackberry.c
index 20c3d1b..a1b9cbb 100644 (file)
 #include "fg_internal.h"
 #include "egl/fg_window_egl.h"
 
+#ifdef NDEBUG
+#define LOGI(...)
+#endif
+
+#ifdef __PLAYBOOK__
+#include <sys/slog.h>
+#ifndef LOGI
+#define LOGI(...) ((void)slogf(1337, _SLOG_INFO, __VA_ARGS__))
+#endif
+#define LOGW(...) ((void)slogf(1337, _SLOG_WARNING, __VA_ARGS__))
+#ifndef SLOG2_FA_SIGNED
+#define SLOG2_FA_SIGNED(x) (x)
+#endif
+#else
 #include <slog2.h>
+#ifndef LOGI
 #define LOGI(...) ((void)slog2fa(NULL, 1337, SLOG2_INFO, __VA_ARGS__, SLOG2_FA_END))
+#endif
 #define LOGW(...) ((void)slog2fa(NULL, 1337, SLOG2_WARNING, __VA_ARGS__, SLOG2_FA_END))
+#endif
 #include <sys/keycodes.h>
 #include <input/screen_helpers.h>
 #include <bps/bps.h>
@@ -165,7 +182,8 @@ fg_time_t fgPlatformSystemTime ( void )
  */
 void fgPlatformSleepForEvents( fg_time_t msec )
 {
-    if(fgStructure.CurrentWindow && fgDisplay.pDisplay.event == NULL && bps_get_event(&fgDisplay.pDisplay.event, (int)msec) != BPS_SUCCESS) {
+    if(fgStructure.CurrentWindow && fgDisplay.pDisplay.event == NULL &&
+            bps_get_event(&fgDisplay.pDisplay.event, (int)msec) != BPS_SUCCESS) {
         LOGW("BPS couldn't get event");
     }
 }
@@ -335,6 +353,10 @@ void fgPlatformProcessSingleEvent ( void )
     do
     {
         SFG_Window* window = fgStructure.CurrentWindow;
+        /* Get the keyboard height before doing anything since we otherwise don't get it until it changes */
+        if(window->State.pWState.keyboardHeight == 0) {
+            virtualkeyboard_get_height(&window->State.pWState.keyboardHeight);
+        }
         domain = bps_event_get_domain(fgDisplay.pDisplay.event);
         if (domain == screen_get_domain()) {
             int eventType;
@@ -350,7 +372,11 @@ void fgPlatformProcessSingleEvent ( void )
             {
                 mtouch_event_t touchEvent;
                 screen_get_mtouch_event(screenEvent, &touchEvent, 0);
+#ifndef __PLAYBOOK__
                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
+#else
+                mod = 0;
+#endif
 
                 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));
 
@@ -392,8 +418,12 @@ void fgPlatformProcessSingleEvent ( void )
                 // This is a mouse move event, it is applicable to a device with a usb mouse or simulator.
                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_BUTTONS, &buttons);
                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_SOURCE_POSITION, position);
+#ifndef __PLAYBOOK__
                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_MOUSE_WHEEL, &wheel);
                 screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_MODIFIERS, &mod);
+#else
+                wheel = mod = 0;
+#endif
                 int size[2];
                 screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
 
@@ -547,7 +577,7 @@ void fgPlatformProcessSingleEvent ( void )
                 break;
             }
         } else if (domain == navigator_get_domain()) {
-            int eventType = bps_event_get_code(fgDisplay.pDisplay.event);
+            unsigned int eventType = bps_event_get_code(fgDisplay.pDisplay.event);
             switch (eventType) {
 
             case NAVIGATOR_WINDOW_STATE:
@@ -600,6 +630,8 @@ void fgPlatformProcessSingleEvent ( void )
                 } else {
                     LOGW("NAVIGATOR_EXIT: No current window");
                 }
+
+                //XXX Should this be a bit more "forceful" so that it doesn't continue to loop through events?
                 break;
             }
 
@@ -619,6 +651,11 @@ void fgPlatformProcessSingleEvent ( void )
                 window->State.pWState.newWidth = 0;
                 window->State.pWState.newHeight = 0;
 
+#ifdef __PLAYBOOK__
+                /* On rotation, the keyboard is closed. This prevents two resize calls */
+                window->State.pWState.keyboardOpen = GL_FALSE;
+#endif
+
                 /* Notify that we want to rotate */
                 navigator_orientation_check_response(fgDisplay.pDisplay.event, true);
                 break;
@@ -631,7 +668,17 @@ void fgPlatformProcessSingleEvent ( void )
                 /* Rotate and resize the window */
                 fgPlatformRotateWindow(window, navigator_event_get_orientation_angle(fgDisplay.pDisplay.event));
                 fgPlatformFlushCommands();
+#ifdef __PLAYBOOK__
+                /* PlayBook doesn't indicate what the new size will be, so we need to retrieve it from the window itself */
+                window->State.pWState.newWidth = glutGet(GLUT_WINDOW_WIDTH);
+                window->State.pWState.newHeight = glutGet(GLUT_WINDOW_HEIGHT);
                 fghOnReshapeNotify(window, window->State.pWState.newWidth, window->State.pWState.newHeight, GL_FALSE);
+#else
+                if(window->State.pWState.keyboardOpen == GL_FALSE) {
+                    /* On rotation, if the keyboard is open, it will get the keyboard resize events anyway. Otherwise, handle the resize. */
+                    fghOnReshapeNotify(window, window->State.pWState.newWidth, window->State.pWState.newHeight, GL_FALSE);
+                }
+#endif
 
                 /* Reset sizes */
                 window->State.pWState.newWidth = 0;
@@ -662,49 +709,11 @@ void fgPlatformProcessSingleEvent ( void )
                 LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_ORIENTATION_DONE/NAVIGATOR_ORIENTATION_RESULT");
                 break;
 
+#ifndef __PLAYBOOK__
             case NAVIGATOR_KEYBOARD_STATE:
-            {
-                LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_KEYBOARD_STATE");
-
-                navigator_keyboard_state_t state = navigator_event_get_keyboard_state(fgDisplay.pDisplay.event);
-                switch (state)
-                {
-                case NAVIGATOR_KEYBOARD_CLOSED:
-                    LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_KEYBOARD_STATE-NAVIGATOR_KEYBOARD_CLOSED");
-                    /* NAVIGATOR_KEYBOARD_POSITION only occurs on open, so on keyboard close we need to reset the keyboard height */
-                    fgPlatformHandleKeyboardHeight(window, 0);
-                    break;
-                case NAVIGATOR_KEYBOARD_OPENING:
-                case NAVIGATOR_KEYBOARD_OPENED:
-                case NAVIGATOR_KEYBOARD_CLOSING:
-                    break;
-                case NAVIGATOR_KEYBOARD_UNRECOGNIZED:
-                    LOGW("fgPlatformProcessSingleEvent: NAVIGATOR_KEYBOARD_STATE-NAVIGATOR_KEYBOARD_UNRECOGNIZED");
-                    break;
-                default:
-                    LOGW("fgPlatformProcessSingleEvent: NAVIGATOR_KEYBOARD_STATE unknown: 0x%X", SLOG2_FA_SIGNED(state));
-                    break;
-                }
-                break;
-            }
-
             case NAVIGATOR_KEYBOARD_POSITION:
-            {
-                /* Occurs only when keyboard has opened or resizes */
-                LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_KEYBOARD_POSITION");
-
-                int keyboardOffset = navigator_event_get_keyboard_position(fgDisplay.pDisplay.event);
-                if(keyboardOffset == BPS_FAILURE) {
-                    LOGW("fgPlatformProcessSingleEvent: NAVIGATOR_KEYBOARD_POSITION: getting keyboard offset failed");
-                } else {
-                    /* keyboardOffset is the offset from the top of the screen to the top of the keyboard, AKA the size of the uncovered screen
-                       We want the height of the keyboard. So instead of determining the orientation, getting the right display size, and subtracting;
-                       we just get the keyboard height which may be slower but easier to understand and work with */
-                    virtualkeyboard_get_height(&keyboardOffset);
-                    fgPlatformHandleKeyboardHeight(window, keyboardOffset);
-                }
+                /* See virtual keyboard handling for info on why this is not used. */
                 break;
-            }
 
             case NAVIGATOR_DEVICE_LOCK_STATE:
                 break;
@@ -727,8 +736,10 @@ void fgPlatformProcessSingleEvent ( void )
                 window->State.pWState.newWidth = navigator_event_get_orientation_size_width(fgDisplay.pDisplay.event);
                 window->State.pWState.newHeight = navigator_event_get_orientation_size_height(fgDisplay.pDisplay.event);
                 break;
+#endif
 
             case 0: //Doesn't exist in header, but shows up when keyboard shows and resizes
+            case NAVIGATOR_OTHER:
                 break;
 
             default:
@@ -736,6 +747,42 @@ void fgPlatformProcessSingleEvent ( void )
                 break;
             }
         }
+        /* 
+         * BlackBerry 10 navigator provides keyboard events, but they conflict with how we handle keyboard events.
+         * Causing multiple reshape messages and can leave window state incorrectly setup.
+         */
+        else if(domain == virtualkeyboard_get_domain()) {
+            unsigned int eventType = bps_event_get_code(fgDisplay.pDisplay.event);
+            switch (eventType) {
+            case VIRTUALKEYBOARD_EVENT_VISIBLE:
+                LOGI("fgPlatformProcessSingleEvent: VIRTUALKEYBOARD_EVENT_VISIBLE");
+                if(window->State.pWState.keyboardOpen != GL_TRUE) {
+                    window->State.pWState.keyboardOpen = GL_TRUE;
+                    fgPlatformHandleKeyboardHeight(window, window->State.pWState.keyboardHeight);
+                }
+                break;
+
+            case VIRTUALKEYBOARD_EVENT_HIDDEN:
+                LOGI("fgPlatformProcessSingleEvent: VIRTUALKEYBOARD_EVENT_HIDDEN");
+                if(window->State.pWState.keyboardOpen != GL_FALSE) {
+                    window->State.pWState.keyboardOpen = GL_FALSE;
+                    fgPlatformHandleKeyboardHeight(window, 0);
+                }
+                break;
+
+            case VIRTUALKEYBOARD_EVENT_INFO:
+                LOGI("fgPlatformProcessSingleEvent: VIRTUALKEYBOARD_EVENT_INFO");
+                window->State.pWState.keyboardHeight = virtualkeyboard_event_get_height(fgDisplay.pDisplay.event);
+                if(window->State.pWState.keyboardOpen == GL_TRUE) {
+                    fgPlatformHandleKeyboardHeight(window, window->State.pWState.keyboardHeight);
+                }
+                break;
+
+            default:
+                LOGW("fgPlatformProcessSingleEvent: unknown virtualkeyboard event: 0x%X", eventType);
+                break;
+            }
+        }
     } while(bps_get_event(&fgDisplay.pDisplay.event, 1) == BPS_SUCCESS && fgDisplay.pDisplay.event != NULL);
 
     /* Reset event to reduce chances of triggering something */
@@ -747,11 +794,14 @@ void fgPlatformMainLoopPreliminaryWork ( void )
     LOGI("fgPlatformMainLoopPreliminaryWork");
 
     /* Request navigator events */
-    navigator_request_events(0);
+    navigator_request_events(NAVIGATOR_EXTENDED_DATA);
 
     /* Allow rotation */
     navigator_rotation_lock(false);
 
+    /* Request keyboard events */
+    virtualkeyboard_request_events(0);
+
     /* Request window events */
     screen_request_events(fgDisplay.pDisplay.screenContext);
 }
@@ -763,7 +813,9 @@ void fgPlatformMainLoopPostWork ( void )
     /* Stop all events */
     screen_stop_events(fgDisplay.pDisplay.screenContext);
 
+#ifndef __PLAYBOOK__
     navigator_stop_events(0);
+#endif
 }
 
 /* deal with work list items */