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
Using SDL in a plugin dll - Init/Deinit & Message Pumpin
die_hoernse


Joined: 06 Jan 2015
Posts: 3
Hello,

this is about how to correctly use SDL in a scenario in which one has to provide a plugin library to be used by an application executable that is agnostic about the fact that SDL ist used inside the dll. Specifically, SDL is to be used to provide joystick input to said application.

Are there any well defined examples of how this can be achieved?

I do unerstand that SDL is redefining main(...) to inject it's own init/deinit boilerplate code. But there is no such mechanism for using SDL in a library where (for windows targets) dllmain(...) would have to be redefined. This may be because dllmain's restrictions regarding thread creation and joining and the general problems that arise from the unloading order, where SDL.dll would be unloaded before my dependent plugin sees DLL_PROCESS_DETACH where it would put it's deinitialization logic.

My approach was to have an Init() and Deinit() Function that would execute said boilerplate code that SDL would inject in an application's main(...) and to start/stop a message pumping thread.

Questions:

  1. Is it sufficient to simply call SDL_SetMainReady(), SDL_Init(...) and start pumping at Init and call SDL_Quit() and stop pumping at Deinit or is there more to watch out for?

  2. Is the following function a proper message pump or am I missing some fancy windows-message-pumping-esque stuff?
    Code:
    void work(void) {
        SDL_Event evt;

        while(DoWork()) { // <- Check non SDL quit signal
            if (SDL_WaitEventTimeout(&evt, 10) != 0) {
                switch(evt.type) {
                    // yadda ...
                }
            }
        }
    }


  3. I used SDL_Init(SDL_INIT_JOYSTICK); until now and I have received joystick events in the worker loop (shown above). I do not intend to use graphial capabilities of SDL. testjoystick.c has the following code:
    Code:
    /* Initialize SDL (Note: video is required to start event loop) */
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
    // ...

    Can I omit initializing SDL_INIT_VIDEO because I have my own message pumping thread?

  4. Can I (if yes: how?) safely restart SDL after SDL_Quit()?
    Background:
    Since I cannot SDL_Quit() on DLL_PROCESS_DETACH (bacause SDL.dll will be already unloaded at that time) and since there is no function in the application's plugin interface to signal impending unloading, I use el cheapo reference counting. The plugin interface main methods are CreateDevice() and void FreeDevice(dev*). Multiple device instances may be created and have to be freed before shutting down the application. So I can count the created devices. Upon creation of the first device i do init SDL. If the last remaining device is destroyed the application is free to terminate. So i have to be ready for that and I deinit SDL (including stopping the message pumping thread and calling SDL_Quit()). But the application can also continue running and create a new device. So I would have to reinit after SDL_Quit().

  5. SDL2-2.0.3's testjoystick.c does (as far as I can see) never call SDL_Quit(). Is it generally not necessary? Aka: Could I just never call it? Could I go and never deinit at all, assuming the library unload will take care of everything for me?

  6. I assume it is not possible to safely have two plugin dlls of the type outlined here that are agnostic about each other and both trying to use SDL the same way (both do init/deinit and message pumping) without having them use SDL via a wrapping layer dll. Am I correct?


I hope to have given a brief and yet complete and comprehensive outline on what I tried to do and what problems arised. Please note that I have already tested everything mentioned above except reinit after SDL_Quit() and it seems to work. My concern is that I am abusing glitches that might be removed or semantically changed in later SDL versions or trigger hard to diagnose bugs like data races or resource leaks.
die_hoernse


Joined: 06 Jan 2015
Posts: 3
Sorry for pushing, but did I ask a dumb question or is the topic just too specific? Maybe i could need help in asking the right question. If you think so, any suggestion would be very welcome. I am new to actively discussing this kind of topics in sophisticated communities.

TL;DR:
No answers or inquiries yet. Is the question too hard or too stupid?