Make it possible to use SDL events separately from the video subsystem.
authorSam Lantinga <slouken@libsdl.org>
Sat, 06 Jul 2013 12:28:57 -0700
changeset 7360 b86947ea2749
parent 7359 96ac247b672e
child 7361 9b4430c49235
Make it possible to use SDL events separately from the video subsystem.
include/SDL.h
src/SDL.c
src/joystick/SDL_joystick.c
src/video/SDL_video.c
--- a/include/SDL.h	Sat Jul 06 11:56:04 2013 -0700
+++ b/include/SDL.h	Sat Jul 06 12:28:57 2013 -0700
@@ -106,13 +106,14 @@
 /*@{*/
 #define SDL_INIT_TIMER          0x00000001
 #define SDL_INIT_AUDIO          0x00000010
-#define SDL_INIT_VIDEO          0x00000020
-#define SDL_INIT_JOYSTICK       0x00000200
+#define SDL_INIT_VIDEO          0x00000020  /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */
+#define SDL_INIT_JOYSTICK       0x00000200  /**< SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */
 #define SDL_INIT_HAPTIC         0x00001000
-#define SDL_INIT_GAMECONTROLLER 0x00002000      /**< turn on game controller also implicitly does JOYSTICK */
-#define SDL_INIT_NOPARACHUTE    0x00100000      /**< Don't catch fatal signals */
+#define SDL_INIT_GAMECONTROLLER 0x00002000  /**< SDL_INIT_GAMECONTROLLE implies SDL_INIT_JOYSTICK */
+#define SDL_INIT_EVENTS         0x00004000
+#define SDL_INIT_NOPARACHUTE    0x00100000  /**< Don't catch fatal signals */
 #define SDL_INIT_EVERYTHING ( \
-                SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | \
+                SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS | \
                 SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER \
             )
 /*@}*/
--- a/src/SDL.c	Sat Jul 06 11:56:04 2013 -0700
+++ b/src/SDL.c	Sat Jul 06 12:28:57 2013 -0700
@@ -27,6 +27,7 @@
 #include "SDL_revision.h"
 #include "SDL_fatal.h"
 #include "SDL_assert_c.h"
+#include "events/SDL_events_c.h"
 #include "haptic/SDL_haptic_c.h"
 #include "joystick/SDL_joystick_c.h"
 
@@ -111,8 +112,33 @@
     SDL_InitTicks();
 #endif
 
+    if ((flags & SDL_INIT_GAMECONTROLLER)) {
+        /* game controller implies joystick */
+        flags |= SDL_INIT_JOYSTICK;
+    }
+
+    if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {
+        /* video or joystick implies events */
+        flags |= SDL_INIT_EVENTS;
+    }
+
+    /* Initialize the event subsystem */
+    if ((flags & SDL_INIT_EVENTS)) {
+#if !SDL_EVENTS_DISABLED
+        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
+            if (SDL_StartEventLoop() < 0) {
+                return (-1);
+            }
+            SDL_QuitInit();
+        }
+        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
+#else
+        return SDL_SetError("SDL not built with events support");
+#endif
+    }
+
     /* Initialize the timer subsystem */
-    if ((flags & SDL_INIT_TIMER) ){
+    if ((flags & SDL_INIT_TIMER)){
 #if !SDL_TIMERS_DISABLED
         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
             if (SDL_TimerInit() < 0) {
@@ -125,8 +151,8 @@
 #endif
     }
 
-    /* Initialize the video/event subsystem */
-    if ((flags & SDL_INIT_VIDEO) ){
+    /* Initialize the video subsystem */
+    if ((flags & SDL_INIT_VIDEO)){
 #if !SDL_VIDEO_DISABLED
         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
             if (SDL_VideoInit(NULL) < 0) {
@@ -140,7 +166,7 @@
     }
 
     /* Initialize the audio subsystem */
-    if ((flags & SDL_INIT_AUDIO) ){
+    if ((flags & SDL_INIT_AUDIO)){
 #if !SDL_AUDIO_DISABLED
         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
             if (SDL_AudioInit(NULL) < 0) {
@@ -153,13 +179,8 @@
 #endif
     }
 
-    if ((flags & SDL_INIT_GAMECONTROLLER)) {
-        /* Game controller implies Joystick. */
-        flags |= SDL_INIT_JOYSTICK;
-    }
-
     /* Initialize the joystick subsystem */
-    if ((flags & SDL_INIT_JOYSTICK) ){
+    if ((flags & SDL_INIT_JOYSTICK)){
 #if !SDL_JOYSTICK_DISABLED
         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
            if (SDL_JoystickInit() < 0) {
@@ -172,7 +193,7 @@
 #endif
     }
 
-    if ((flags & SDL_INIT_GAMECONTROLLER) ){
+    if ((flags & SDL_INIT_GAMECONTROLLER)){
 #if !SDL_JOYSTICK_DISABLED
         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
             if (SDL_GameControllerInit() < 0) {
@@ -186,7 +207,7 @@
     }
 
     /* Initialize the haptic subsystem */
-    if ((flags & SDL_INIT_HAPTIC) ){
+    if ((flags & SDL_INIT_HAPTIC)){
 #if !SDL_HAPTIC_DISABLED
         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
             if (SDL_HapticInit() < 0) {
@@ -237,7 +258,7 @@
     /* Shut down requested initialized subsystems */
 #if !SDL_JOYSTICK_DISABLED
     if ((flags & SDL_INIT_GAMECONTROLLER)) {
-        /* Game controller implies Joystick. */
+        /* game controller implies joystick */
         flags |= SDL_INIT_JOYSTICK;
 
         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
@@ -247,6 +268,9 @@
     }
 
     if ((flags & SDL_INIT_JOYSTICK)) {
+        /* joystick implies events */
+        flags |= SDL_INIT_EVENTS;
+
         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
             SDL_JoystickQuit();
         }
@@ -274,6 +298,9 @@
 
 #if !SDL_VIDEO_DISABLED
     if ((flags & SDL_INIT_VIDEO)) {
+        /* video implies events */
+        flags |= SDL_INIT_EVENTS;
+
         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
             SDL_VideoQuit();
         }
@@ -289,6 +316,16 @@
         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
     }
 #endif
+
+#if !SDL_EVENTS_DISABLED
+    if ((flags & SDL_INIT_EVENTS)) {
+        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
+            SDL_QuitQuit();
+            SDL_StopEventLoop();
+        }
+        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
+    }
+#endif
 }
 
 Uint32
--- a/src/joystick/SDL_joystick.c	Sat Jul 06 11:56:04 2013 -0700
+++ b/src/joystick/SDL_joystick.c	Sat Jul 06 12:28:57 2013 -0700
@@ -48,6 +48,12 @@
         SDL_joystick_allows_background_events = SDL_TRUE;
     }
 
+#if !SDL_EVENTS_DISABLED
+    if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
+        return -1;
+    }
+#endif /* !SDL_EVENTS_DISABLED */
+
     status = SDL_SYS_JoystickInit();
     if (status >= 0) {
         status = 0;
@@ -458,6 +464,10 @@
 
     /* Quit the joystick setup */
     SDL_SYS_JoystickQuit();
+
+#if !SDL_EVENTS_DISABLED
+    SDL_QuitSubSystem(SDL_INIT_EVENTS);
+#endif
 }
 
 
--- a/src/video/SDL_video.c	Sat Jul 06 11:56:04 2013 -0700
+++ b/src/video/SDL_video.c	Sat Jul 06 12:28:57 2013 -0700
@@ -422,11 +422,10 @@
 #endif
 
     /* Start the event loop */
-    if (SDL_StartEventLoop() < 0 ||
+    if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0 ||
         SDL_KeyboardInit() < 0 ||
         SDL_MouseInit() < 0 ||
-        SDL_TouchInit() < 0 ||
-        SDL_QuitInit() < 0) {
+        SDL_TouchInit() < 0) {
         return -1;
     }
 
@@ -2233,10 +2232,10 @@
     }
 
     /* Halt event processing before doing anything else */
-    SDL_QuitQuit();
+    SDL_TouchQuit();
     SDL_MouseQuit();
     SDL_KeyboardQuit();
-    SDL_StopEventLoop();
+    SDL_QuitSubSystem(SDL_INIT_EVENTS);
 
     SDL_EnableScreenSaver();