The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Unlink eventloop start from video init
nescivi


Joined: 20 Feb 2013
Posts: 7
My first post here, but it seems this topic has come up in the past, as I found some past discussions about this.

In some cases, it is desirable to start the eventloop separate from the video; the diff below implements this succesully for joysticks.

You can then use:
SDL_Init(SDL_INIT_EVENT | SDL_INIT_JOYSTICK)
rather than:
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)


To illustrate the usecase in my field... there are quite a few audio programming languages like SuperCollider (http://supercollider.sourceforge.net), Chuck (http://chuck.cs.princeton.edu/), and others, that are used to create custom music instruments/compositions, and using HID devices in this is very popular. A lot of these programming languages operate cross platform, and thus benefit from a common interface to access them.
(In return some developers of these environments might contribute back to libSDL. Around 2006/7 one of Chuck's developers adapted a lot of libSDL code for quite the same reason I now submit this patch).

LibSDL is the only library thus far I have come across that provides such cross platform HID access. However, we don't need the video interface with it, and from what I've read, it could cause some undesired side effects even (though I've only tried on Linux so far).
Also, since I believe it is possible to compile libSDL without video support, it would make sense to still provide access to the eventloop in such a case.

Anyway, I hope the patch (I can mail it too, as I don't seem to be able to upload files on the forum) can find its way into the code base somehow, as that would mean I don't need to fork libSDL, but can rather contribute to it :)

sincerely,
Marije

Code:

diff -r 865811c9f372 include/SDL.h
--- a/include/SDL.h   Mon Feb 18 07:59:05 2013 -0800
+++ b/include/SDL.h   Wed Feb 20 13:32:02 2013 +0100
@@ -111,6 +111,7 @@
 #define SDL_INIT_JOYSTICK       0x00000200
 #define SDL_INIT_HAPTIC         0x00001000
 #define SDL_INIT_GAMECONTROLLER 0x00002000      /**< turn on game controller also implicitly does JOYSTICK */
+#define SDL_INIT_EVENT      0x00010000
 #define SDL_INIT_NOPARACHUTE    0x00100000      /**< Don't catch fatal signals */
 #define SDL_INIT_EVERYTHING     0x0000FFFF
 /*@}*/
diff -r 865811c9f372 include/SDL_events.h
--- a/include/SDL_events.h   Mon Feb 18 07:59:05 2013 -0800
+++ b/include/SDL_events.h   Wed Feb 20 13:32:02 2013 +0100
@@ -680,6 +680,25 @@
 /*@}*/
 #define SDL_GetEventState(type) SDL_EventState(type, SDL_QUERY)
 
+
+/**
+ *  \brief Initialize the event subsystem.
+ * 
+ *  \return 0 on success, -1 on error
+ * 
+ *  This function initializes the event subsystem.
+ * 
+ *  \sa SDL_EventInit()
+ */
+extern DECLSPEC int SDLCALL SDL_EventInit(void);
+
+/**
+ *  \brief Shuts down the event subsystem.
+ * 
+ *  \sa SDL_EventQuit()
+ */
+extern DECLSPEC void SDLCALL SDL_EventQuit(void);
+
 /**
  *  This function allocates a set of user-defined events, and returns
  *  the beginning event number for that set of events.
diff -r 865811c9f372 src/SDL.c
--- a/src/SDL.c   Mon Feb 18 07:59:05 2013 -0800
+++ b/src/SDL.c   Wed Feb 20 13:32:02 2013 +0100
@@ -118,6 +118,19 @@
     }
 
     /* Initialize the video/event subsystem */
+    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_EVENT)) {
+#if !SDL_EVENTS_DISABLED
+        if (SDL_EventInit() < 0) {
+            return (-1);
+        }
+        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENT);
+#else
+        SDL_SetError("SDL not built with event support");
+        return (-1);
+#endif
+    }
+
+    /* Initialize the video/event subsystem */
     if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_VIDEO)) {
 #if !SDL_VIDEO_DISABLED
         if (SDL_VideoInit(NULL) < 0) {
diff -r 865811c9f372 src/events/SDL_events.c
--- a/src/events/SDL_events.c   Mon Feb 18 07:59:05 2013 -0800
+++ b/src/events/SDL_events.c   Wed Feb 20 13:32:02 2013 +0100
@@ -535,4 +535,46 @@
     return (posted);
 }
 
+
+/*
+ * Initialize the event subsystems -- determine native pixel format
+ */
+int
+SDL_EventInit(void)
+{
+    /* Start the event loop */
+    if (SDL_StartEventLoop() < 0 ||
+        SDL_KeyboardInit() < 0 ||
+        SDL_MouseInit() < 0 ||
+        SDL_TouchInit() < 0 ||
+        SDL_QuitInit() < 0) {
+        return -1;
+    }
+
+    /* If we don't use a screen keyboard, turn on text input by default,
+       otherwise programs that expect to get text events without enabling
+       UNICODE input won't get any events.
+
+       Actually, come to think of it, you needed to call SDL_EnableUNICODE(1)
+       in SDL 1.2 before you got text input events.  Hmm...
+     */
+    if (!SDL_HasScreenKeyboardSupport()) {
+        SDL_StartTextInput();
+    }
+
+    /* We're ready to go! */
+    return 0;
+}
+
+void
+SDL_EventQuit(void)
+{
+    /* Halt event processing before doing anything else */
+    SDL_QuitQuit();
+    SDL_MouseQuit();
+    SDL_KeyboardQuit();
+    SDL_StopEventLoop();
+}
+
+
 /* vi: set ts=4 sw=4 expandtab: */
diff -r 865811c9f372 src/video/SDL_video.c
--- a/src/video/SDL_video.c   Mon Feb 18 07:59:05 2013 -0800
+++ b/src/video/SDL_video.c   Wed Feb 20 13:32:02 2013 +0100
@@ -420,12 +420,13 @@
         SDL_VideoQuit();
     }
 
+    if (SDL_EventInit() < 0 ){
     /* Start the event loop */
-    if (SDL_StartEventLoop() < 0 ||
-        SDL_KeyboardInit() < 0 ||
-        SDL_MouseInit() < 0 ||
-        SDL_TouchInit() < 0 ||
-        SDL_QuitInit() < 0) {
+//     if (SDL_StartEventLoop() < 0 ||
+//         SDL_KeyboardInit() < 0 ||
+//         SDL_MouseInit() < 0 ||
+//         SDL_TouchInit() < 0 ||
+//         SDL_QuitInit() < 0) {
         return -1;
     }
 
@@ -2216,10 +2217,11 @@
     }
 
     /* Halt event processing before doing anything else */
-    SDL_QuitQuit();
-    SDL_MouseQuit();
-    SDL_KeyboardQuit();
-    SDL_StopEventLoop();
+    SDL_EventQuit();
+//     SDL_QuitQuit();
+//     SDL_MouseQuit();
+//     SDL_KeyboardQuit();
+//     SDL_StopEventLoop();
 
     SDL_EnableScreenSaver();