- //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);
+ LOGI("fgPlatformProcessSingleEvent: SCREEN_EVENT_MTOUCH_*: Type: 0x%X, X: %d, Y: %d, Contact Id: %d", SLOG2_FA_SIGNED(eventType), SLOG2_FA_SIGNED(touchEvent.x), SLOG2_FA_SIGNED(touchEvent.y), SLOG2_FA_SIGNED(touchEvent.contact_id));
+ if(touchEvent.contact_id == 0) {
+ 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);
+ }
+
+ //Now handle mutlitouch (adapted from fg_main_windows)
+ if (eventType == SCREEN_EVENT_MTOUCH_TOUCH) {
+ INVOKE_WCB( *window, MultiEntry, ( touchEvent.contact_id, GLUT_ENTERED ) );
+ INVOKE_WCB( *window, MultiButton, ( touchEvent.contact_id, touchEvent.x, touchEvent.y, 0, GLUT_DOWN ) );
+ } else if (eventType == SCREEN_EVENT_MTOUCH_MOVE) {
+ INVOKE_WCB( *window, MultiMotion, ( touchEvent.contact_id, touchEvent.x, touchEvent.y ) );
+ //XXX No motion is performed without contact, thus MultiPassive is never used
+ } else if (eventType == SCREEN_EVENT_MTOUCH_RELEASE) {
+ INVOKE_WCB( *window, MultiButton, ( touchEvent.contact_id, touchEvent.x, touchEvent.y, 0, GLUT_UP ) );
+ INVOKE_WCB( *window, MultiEntry, ( touchEvent.contact_id, GLUT_LEFT ) );
+ }
+ break;
+ }
+
+ case SCREEN_EVENT_POINTER:
+ {
+ //Based off/part taken from GamePlay3d PlatformBlackBerry
+ static int mouse_pressed = 0;
+ int buttons;
+ int position[2];
+ int wheel;
+ // 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);
+ screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_MOUSE_WHEEL, &wheel);
+ int size[2];
+ screen_get_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_BUFFER_SIZE, size);
+
+ LOGI("fgPlatformProcessSingleEvent: SCREEN_EVENT_POINTER: Buttons: 0x%X, X: %d, Y: %d, Wheel: %d", SLOG2_FA_SIGNED(buttons), SLOG2_FA_SIGNED(position[0]), SLOG2_FA_SIGNED(position[1]), SLOG2_FA_SIGNED(wheel));
+
+ //XXX Should multitouch be handled?
+
+ // 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);
+ }
+
+ if (wheel) {
+ fgState.MouseWheelTicks -= wheel;
+ //TODO: Implement wheel support (based on fg_main_mswin... though it seems excessive)
+ }
+ 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));
+ // Suppress key repeats if desired
+ if ((flags & KEY_REPEAT) == 0 || (fgState.KeyRepeat == GLUT_KEY_REPEAT_ON && !fgStructure.CurrentWindow->State.IgnoreKeyRepeat)) {
+ 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;
+ }
+
+ case SCREEN_EVENT_PROPERTY:
+ case SCREEN_EVENT_IDLE:
+ break;
+
+ default:
+ LOGW("fgPlatformProcessSingleEvent: unknown screen event: 0x%X", SLOG2_FA_SIGNED(eventType));
+ break;
+ }
+ } else if (domain == navigator_get_domain()) {
+ int eventType = bps_event_get_code(event);
+ switch (eventType) {
+
+ case NAVIGATOR_WINDOW_STATE:
+ {
+ LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE");
+ 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");
+ 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");
+ INVOKE_WCB(*window, AppStatus, (GLUT_APPSTATUS_PAUSE));
+ break;
+ default:
+ LOGW("fgPlatformProcessSingleEvent: NAVIGATOR_WINDOW_STATE unknown: 0x%X", SLOG2_FA_SIGNED(state));
+ break;
+ }
+ break;
+ }
+
+ case NAVIGATOR_EXIT:
+ LOGI("fgPlatformProcessSingleEvent: NAVIGATOR_EXIT");
+ /* User closed the application for good, let's kill the window */
+ {
+ SFG_Window* window = fgStructure.CurrentWindow;
+ if (window != NULL) {
+ fgDestroyWindow(window);
+ } else {
+ LOGW("NAVIGATOR_EXIT: No current window");
+ }
+ }
+ break;
+
+ case NAVIGATOR_SWIPE_DOWN:
+ case NAVIGATOR_BACK:
+ case NAVIGATOR_WINDOW_ACTIVE:
+ case NAVIGATOR_DEVICE_LOCK_STATE:
+ case NAVIGATOR_WINDOW_COVER:
+ case NAVIGATOR_WINDOW_COVER_ENTER:
+ case NAVIGATOR_WINDOW_COVER_EXIT:
+ //XXX Should probably do something with these
+ break;
+
+ default:
+ LOGW("fgPlatformProcessSingleEvent: unknown navigator event: 0x%X", SLOG2_FA_SIGNED(eventType));
+ break;
+ }
+ }
+ }
+ } while(bps_get_event(eventPtr, 1) == BPS_SUCCESS && *eventPtr != NULL);