- //TODO
- //--------------------------------
- /* When the screen is resized, the window handle still points to the
- old window until the next SwapBuffer, while it's crucial to set
- the size (onShape) correctly before the next onDisplay callback.
- Plus we don't know if the next SwapBuffer already occurred at the
- time we process the event (e.g. during onDisplay). * /
- /* So we do the check each time rather than on event. * /
- /* Interestingly, on a Samsung Galaxy S/PowerVR SGX540 GPU/Android
- 2.3, that next SwapBuffer is fake (but still necessary to get the
- new size). * /
- SFG_Window* window = fgStructure.CurrentWindow;
- if (window != NULL && window->Window.Handle != NULL) {
- int32_t width = ANativeWindow_getWidth(window->Window.Handle);
- int32_t height = ANativeWindow_getHeight(window->Window.Handle);
- fghOnReshapeNotify(window,width,height,GL_FALSE);
- }
-
- /* Read pending event. * /
- int ident;
- int events;
- struct android_poll_source* source;
- /* This is called "ProcessSingleEvent" but this means we'd only
- process ~60 (screen Hz) mouse events per second, plus other ports
- are processing all events already. So let's process all pending
- events. */
- /* if ((ident=ALooper_pollOnce(0, NULL, &events, (void**)&source)) >= 0) { * /
- while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) {
- /* Process this event. * /
- if (source != NULL) {
- source->process(source->app, source);
- }
- }
-
- /* If we're not in RESUME state, Android paused us, so wait * /
- struct android_app* app = fgDisplay.pDisplay.app;
- if (app->destroyRequested != 1 && app->activityState != APP_CMD_RESUME) {
- INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_PAUSE));
-
- int FOREVER = -1;
- while (app->destroyRequested != 1 && (app->activityState != APP_CMD_RESUME)) {
- if ((ident=ALooper_pollOnce(FOREVER, NULL, &events, (void**)&source)) >= 0) {
- /* Process this event. * /
- if (source != NULL) {
- source->process(source->app, source);
- }
- }
- }
- /* Coming back from a pause: * /
- /* - Recreate window context and surface */
- /* - Call user-defined hook to restore resources (textures...) * /
- /* - Exit pause loop * /
- if (app->destroyRequested != 1) {
- /* Android is full-screen only, simplified call.. * /
- /* Ideally we'd have a fgPlatformReopenWindow() * /
- /* If we're hidden by a non-fullscreen or translucent activity,
- we'll be paused but not stopped, and keep the current
- surface; in which case fgPlatformOpenWindow will no-op. * /
- fgPlatformOpenWindow(window, "", GL_FALSE, 0, 0, GL_FALSE, 0, 0, GL_FALSE, GL_FALSE);
-
- /* TODO: should there be a whole GLUT_INIT_WORK forced? probably...
- * Could queue that up in APP_CMD_TERM_WINDOW handler, but it'll
- * be not possible to ensure InitContext CB gets called before
- * Resume CB like that.. so maybe just force calling initContext CB
- * here is best. Or we could force work on the window in question..
- * 1) save old work mask, 2) set mask to init only, 3) call fgProcessWork directly
- * 4) set work mask back to the one saved in step 1.
- /
- if (!FETCH_WCB(*window, InitContext))
- fgWarning("Resuming application, but no callback to reload context resources (glutInitContextFunc)");
- }
+ int domain;
+ bps_event_t** eventPtr = &fgStructure.CurrentWindow->Window.pContext.event; //XXX Is there a more direct way to access the context?
+ bps_event_t* event;
+ do
+ {
+ if(*eventPtr != NULL) {
+ SFG_Window* window = fgStructure.CurrentWindow;
+ if (window != NULL && window->Window.Handle != NULL) {
+ int size[2];
+ screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
+ fghOnReshapeNotify(window,size[0],size[1],GL_FALSE);
+ }
+
+ event = *eventPtr;
+ domain = bps_event_get_domain(event);
+ if (domain == screen_get_domain()) {
+ int eventType;
+ screen_event_t screenEvent = screen_event_get_event(event);
+ screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_TYPE, &eventType);
+ switch (eventType) {
+
+ //Mostly from fg_main_android
+ case SCREEN_EVENT_MTOUCH_TOUCH:
+ case SCREEN_EVENT_MTOUCH_RELEASE:
+ case SCREEN_EVENT_MTOUCH_MOVE:
+ {
+ mtouch_event_t touchEvent;
+ screen_get_mtouch_event(screenEvent, &touchEvent, 0);
+ if(touchEvent.contact_id == 0) { //XXX Only support one contact for now
+ int size[2];
+ screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
+ handle_left_mouse(touchEvent.x, touchEvent.y, size[1], eventType, window);
+ }
+ break;
+ }
+
+ case SCREEN_EVENT_POINTER:
+ {
+ //Based off/part taken from GamePlay3d PlatformBlackBerry
+ static int mouse_pressed = 0;
+ int buttons;
+ int position[2];
+ // A move event will be fired unless a button state changed.
+ bool move = true;
+ bool left_move = false;
+ // 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);
+ int size[2];
+ screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
+
+ // Handle left mouse. Interpret as touch if the left mouse event is not consumed.
+ if (buttons & SCREEN_LEFT_MOUSE_BUTTON) {
+ if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON) {
+ left_move = true;
+ } else {
+ move = false;
+ mouse_pressed |= SCREEN_LEFT_MOUSE_BUTTON;
+ handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_TOUCH, window);
+ }
+ } else if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON) {
+ move = false;
+ mouse_pressed &= ~SCREEN_LEFT_MOUSE_BUTTON;
+ handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_RELEASE, window);
+ }
+
+ // Handle right mouse.
+ if (buttons & SCREEN_RIGHT_MOUSE_BUTTON) {
+ if ((mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
+ move = false;
+ mouse_pressed |= SCREEN_RIGHT_MOUSE_BUTTON;
+ INVOKE_WCB(*window, Mouse, (GLUT_RIGHT_BUTTON, GLUT_DOWN, position[0], position[1]));
+ }
+ } else if (mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) {
+ move = false;
+ mouse_pressed &= ~SCREEN_RIGHT_MOUSE_BUTTON;
+ INVOKE_WCB(*window, Mouse, (GLUT_RIGHT_BUTTON, GLUT_UP, position[0], position[1]));
+ }
+
+ // Handle middle mouse.
+ if (buttons & SCREEN_MIDDLE_MOUSE_BUTTON) {
+ if ((mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
+ move = false;
+ mouse_pressed |= SCREEN_MIDDLE_MOUSE_BUTTON;
+ INVOKE_WCB(*window, Mouse, (GLUT_MIDDLE_BUTTON, GLUT_DOWN, position[0], position[1]));
+ }
+ } else if (mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) {
+ move = false;
+ mouse_pressed &= ~SCREEN_MIDDLE_MOUSE_BUTTON;
+ INVOKE_WCB(*window, Mouse, (GLUT_MIDDLE_BUTTON, GLUT_UP, position[0], position[1]));
+ }
+
+ // Fire a move event if none of the buttons changed.
+ if (left_move || move) {
+ handle_left_mouse(position[0], position[1], size[1], SCREEN_EVENT_MTOUCH_MOVE, window);
+ }
+ break;
+ }
+
+ //Based off fg_main_android
+ case SCREEN_EVENT_KEYBOARD:
+ {
+ int flags;
+ int value;
+ screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_FLAGS, &flags);
+ screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_KEY_SYM, &value);
+ LOGI("fgPlatformProcessSingleEvent: SCREEN_EVENT_KEYBOARD. Flags: 0x%X, Sym: 0x%X", SLOG2_FA_SIGNED(flags), SLOG2_FA_SIGNED(value), SLOG2_FA_END);
+ // Suppress key repeats if desired
+ if (!fgStructure.CurrentWindow->State.IgnoreKeyRepeat && (flags & KEY_REPEAT) == 0) {
+ unsigned int keypress = 0;
+ unsigned char ascii = 0;
+ if ((keypress = key_special(value))) {
+ if(flags & KEY_DOWN) {
+ INVOKE_WCB(*window, Special, (keypress, window->State.MouseX, window->State.MouseY));
+ } else {
+ INVOKE_WCB(*window, SpecialUp, (keypress, window->State.MouseX, window->State.MouseY));
+ }
+ } else if((flags & KEY_SYM_VALID) && (ascii = key_ascii(value))) {
+ if(flags & KEY_DOWN) {
+ INVOKE_WCB(*window, Keyboard, (ascii, window->State.MouseX, window->State.MouseY));
+ } else {
+ INVOKE_WCB(*window, KeyboardUp, (ascii, window->State.MouseX, window->State.MouseY));
+ }
+ }
+ }
+ break;
+ }
+ }
+ } else if (domain == navigator_get_domain()) {
+ switch (bps_event_get_code(event)) {
+
+ case NAVIGATOR_WINDOW_STATE:
+ {
+ LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE", SLOG2_FA_END);
+ navigator_window_state_t state = navigator_event_get_window_state(event);
+ switch (state)
+ {
+ case NAVIGATOR_WINDOW_FULLSCREEN:
+ LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_FULLSCREEN", SLOG2_FA_END);
+ INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_RESUME));
+ break;
+ case NAVIGATOR_WINDOW_THUMBNAIL:
+ case NAVIGATOR_WINDOW_INVISIBLE:
+ LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE-NAVIGATOR_WINDOW_THUMBNAIL/NAVIGATOR_WINDOW_INVISIBLE", SLOG2_FA_END);
+ INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_PAUSE));
+ break;
+ }
+ break;
+ }
+
+ case NAVIGATOR_EXIT:
+ LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_EXIT", SLOG2_FA_END);
+ /* User closed the application for good, let's kill the window */
+ {
+ SFG_Window* window = fgStructure.CurrentWindow;
+ if (window != NULL) {
+ fgDestroyWindow(window);
+ } else {
+ LOGI("NAVIGATOR_EXIT: No current window", SLOG2_FA_END);
+ }
+ }
+ break;
+ }
+ }
+ }
+ } while(bps_get_event(eventPtr, 1) == BPS_SUCCESS && *eventPtr != NULL);