WinRT: send window 'minimized' and 'restored' events for app-suspend and app-resume. Use SDL_AddEventWatch() to receive these.
authorDavid Ludwig <dludwig@pobox.com>
Tue, 08 Jan 2013 22:50:29 -0500
changeset 83831f415392ccf0
parent 8382 5433c96fd195
child 8384 bc7a52629e1e
WinRT: send window 'minimized' and 'restored' events for app-suspend and app-resume. Use SDL_AddEventWatch() to receive these.
src/video/windowsrt/SDL_WinRTApp.cpp
     1.1 --- a/src/video/windowsrt/SDL_WinRTApp.cpp	Mon Dec 31 10:30:38 2012 -0500
     1.2 +++ b/src/video/windowsrt/SDL_WinRTApp.cpp	Tue Jan 08 22:50:29 2013 -0500
     1.3 @@ -7,6 +7,7 @@
     1.4  #include "../SDL_sysvideo.h"
     1.5  #include "../../events/SDL_mouse_c.h"
     1.6  #include "../../events/SDL_keyboard_c.h"
     1.7 +#include "../../events/SDL_windowevents_c.h"
     1.8  #include "SDL_events.h"
     1.9  #include "SDL_log.h"
    1.10  }
    1.11 @@ -494,6 +495,26 @@
    1.12  	CoreWindow::GetForCurrentThread()->Activate();
    1.13  }
    1.14  
    1.15 +static int SDLCALL RemoveAppSuspendAndResumeEvents(void * userdata, SDL_Event * event)
    1.16 +{
    1.17 +    if (event->type == SDL_WINDOWEVENT)
    1.18 +    {
    1.19 +        switch (event->window.event)
    1.20 +        {
    1.21 +            case SDL_WINDOWEVENT_MINIMIZED:
    1.22 +            case SDL_WINDOWEVENT_RESTORED:
    1.23 +                // Return 0 to indicate that the event should be removed from the
    1.24 +                // event queue:
    1.25 +                return 0;
    1.26 +            default:
    1.27 +                break;
    1.28 +        }
    1.29 +    }
    1.30 +
    1.31 +    // Return 1 to indicate that the event should stay in the event queue:
    1.32 +    return 1;
    1.33 +}
    1.34 +
    1.35  void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
    1.36  {
    1.37  	// Save app state asynchronously after requesting a deferral. Holding a deferral
    1.38 @@ -501,20 +522,46 @@
    1.39  	// aware that a deferral may not be held indefinitely. After about five seconds,
    1.40  	// the app will be forced to exit.
    1.41  	SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
    1.42 -
    1.43  	create_task([this, deferral]()
    1.44  	{
    1.45 -		// Insert your code here.
    1.46 -
    1.47 +        // Send a window-minimized event immediately to observers.
    1.48 +        // CoreDispatcher::ProcessEvents, which is the backbone on which
    1.49 +        // SDL_WinRTApp::PumpEvents is built, will not return to its caller
    1.50 +        // once it sends out a suspend event.  Any events posted to SDL's
    1.51 +        // event queue won't get received until the WinRT app is resumed.
    1.52 +        // SDL_AddEventWatch() may be used to receive app-suspend events on
    1.53 +        // WinRT.
    1.54 +        //
    1.55 +        // In order to prevent app-suspend events from being received twice:
    1.56 +        // first via a callback passed to SDL_AddEventWatch, and second via
    1.57 +        // SDL's event queue, the event will be sent to SDL, then immediately
    1.58 +        // removed from the queue.
    1.59 +        if (m_sdlWindowData)
    1.60 +        {
    1.61 +            SDL_SendWindowEvent(m_sdlWindowData->sdlWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);   // TODO: see if SDL_WINDOWEVENT_SIZE_CHANGED should be getting triggered here (it is, currently)
    1.62 +            SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
    1.63 +        }
    1.64  		deferral->Complete();
    1.65  	});
    1.66  }
    1.67 - 
    1.68 +
    1.69  void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
    1.70  {
    1.71  	// Restore any data or state that was unloaded on suspend. By default, data
    1.72  	// and state are persisted when resuming from suspend. Note that this event
    1.73  	// does not occur if the app was previously terminated.
    1.74 +    if (m_sdlWindowData)
    1.75 +    {
    1.76 +        SDL_SendWindowEvent(m_sdlWindowData->sdlWindow, SDL_WINDOWEVENT_RESTORED, 0, 0);    // TODO: see if SDL_WINDOWEVENT_SIZE_CHANGED should be getting triggered here (it is, currently)
    1.77 +
    1.78 +        // Remove the app-resume event from the queue, as is done with the
    1.79 +        // app-suspend event.
    1.80 +        //
    1.81 +        // TODO, WinRT: consider posting this event to the queue even though
    1.82 +        // its counterpart, the app-suspend event, effectively has to be
    1.83 +        // processed immediately.
    1.84 +        SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
    1.85 +    }
    1.86  }
    1.87  
    1.88  SDL_DisplayMode SDL_WinRTApp::GetMainDisplayMode()