events: Let arbitrary signals to simulate iOS/Android backgrounding events.
authorRyan C. Gordon
Fri, 15 Mar 2019 15:51:05 -0400
changeset 12631779b63fc4acb
parent 12630 a26e3c61e35f
child 12632 e8bea0d8c9f6
events: Let arbitrary signals to simulate iOS/Android backgrounding events.

This lets you build a custom embedded device that roughly offers the "this
process is going to the background NOW" semantics of SDL on a mobile device.
CMakeLists.txt
configure
configure.in
src/events/SDL_events.c
src/events/SDL_events_c.h
src/events/SDL_quit.c
     1.1 --- a/CMakeLists.txt	Fri Mar 15 14:08:30 2019 -0400
     1.2 +++ b/CMakeLists.txt	Fri Mar 15 15:51:05 2019 -0400
     1.3 @@ -341,6 +341,8 @@
     1.4  dep_option(VIDEO_VULKAN        "Enable Vulkan support" ON "ANDROID OR APPLE OR LINUX OR WINDOWS" OFF)
     1.5  set_option(VIDEO_KMSDRM        "Use KMS DRM video driver" ${UNIX_SYS})
     1.6  dep_option(KMSDRM_SHARED       "Dynamically load KMS DRM support" ON "VIDEO_KMSDRM" OFF)
     1.7 +option_string(BACKGROUNDING_SIGNAL "number to use for magic backgrounding signal or 'OFF'" "OFF")
     1.8 +option_string(FOREGROUNDING_SIGNAL "number to use for magic foregrounding signal or 'OFF'" "OFF")
     1.9  
    1.10  # TODO: We should (should we?) respect cmake's ${BUILD_SHARED_LIBS} flag here
    1.11  # The options below are for compatibility to configure's default behaviour.
    1.12 @@ -384,6 +386,14 @@
    1.13  endif()
    1.14  set(HAVE_ASSERTIONS ${ASSERTIONS})
    1.15  
    1.16 +if(NOT BACKGROUNDING_SIGNAL STREQUAL "OFF")
    1.17 +  add_definitions("-DSDL_BACKGROUNDING_SIGNAL=${BACKGROUNDING_SIGNAL}")
    1.18 +endif()
    1.19 +
    1.20 +if(NOT FOREGROUNDING_SIGNAL STREQUAL "OFF")
    1.21 +  add_definitions("-DSDL_FOREGROUNDING_SIGNAL=${FOREGROUNDING_SIGNAL}")
    1.22 +endif()
    1.23 +
    1.24  # Compiler option evaluation
    1.25  if(USE_GCC OR USE_CLANG)
    1.26    # Check for -Wall first, so later things can override pieces of it.
     2.1 --- a/configure	Fri Mar 15 14:08:30 2019 -0400
     2.2 +++ b/configure	Fri Mar 15 15:51:05 2019 -0400
     2.3 @@ -870,6 +870,8 @@
     2.4  enable_hidapi
     2.5  enable_clock_gettime
     2.6  enable_rpath
     2.7 +enable_backgrounding_signal
     2.8 +enable_foregrounding_signal
     2.9  enable_render_d3d
    2.10  '
    2.11        ac_precious_vars='build_alias
    2.12 @@ -1627,6 +1629,12 @@
    2.13    --enable-clock_gettime  use clock_gettime() instead of gettimeofday() on
    2.14                            UNIX [[default=yes]]
    2.15    --enable-rpath          use an rpath when linking SDL [[default=yes]]
    2.16 +  --enable-backgrounding-signal
    2.17 +                          number to use for magic backgrounding signal or 'no'
    2.18 +                          [[default=no]]
    2.19 +  --enable-foregrounding-signal
    2.20 +                          number to use for magic foregrounding signal or 'no'
    2.21 +                          [[default=no]]
    2.22    --enable-render-d3d     enable the Direct3D render driver [[default=yes]]
    2.23  
    2.24  Optional Packages:
    2.25 @@ -23947,8 +23955,37 @@
    2.26  
    2.27  }
    2.28  
    2.29 +CheckEventSignals()
    2.30 +{
    2.31 +    # Check whether --enable-backgrounding-signal was given.
    2.32 +if test "${enable_backgrounding_signal+set}" = set; then :
    2.33 +  enableval=$enable_backgrounding_signal;
    2.34 +else
    2.35 +  enable_backgrounding_signal=no
    2.36 +fi
    2.37 +
    2.38 +    if test x$enable_backgrounding_signal != xno; then
    2.39 +        EXTRA_CFLAGS="$EXTRA_CFLAGS -DSDL_BACKGROUNDING_SIGNAL=$enable_backgrounding_signal"
    2.40 +    fi
    2.41 +
    2.42 +    # Check whether --enable-foregrounding-signal was given.
    2.43 +if test "${enable_foregrounding_signal+set}" = set; then :
    2.44 +  enableval=$enable_foregrounding_signal;
    2.45 +else
    2.46 +  enable_foregrounding_signal=no
    2.47 +fi
    2.48 +
    2.49 +    if test x$enable_foregrounding_signal != xno; then
    2.50 +        EXTRA_CFLAGS="$EXTRA_CFLAGS -DSDL_FOREGROUNDING_SIGNAL=$enable_foregrounding_signal"
    2.51 +    fi
    2.52 +}
    2.53 +
    2.54 +
    2.55 +
    2.56  CheckWarnAll
    2.57  
    2.58 +CheckEventSignals
    2.59 +
    2.60  case "$host" in
    2.61      *-*-linux*|*-*-uclinux*|*-*-gnu*|*-*-k*bsd*-gnu|*-*-bsdi*|*-*-freebsd*|*-*-dragonfly*|*-*-netbsd*|*-*-openbsd*|*-*-sysv5*|*-*-solaris*|*-*-hpux*|*-*-aix*|*-*-minix*|*-*-nto*)
    2.62          case "$host" in
     3.1 --- a/configure.in	Fri Mar 15 14:08:30 2019 -0400
     3.2 +++ b/configure.in	Fri Mar 15 15:51:05 2019 -0400
     3.3 @@ -3344,9 +3344,34 @@
     3.4                    , enable_rpath=yes)
     3.5  }
     3.6  
     3.7 +dnl Check if we want to use custom signals to fake iOS/Android's backgrounding
     3.8 +dnl  events. These could be useful if you're building a custom embedded
     3.9 +dnl  environment, etc, but most people don't need this.
    3.10 +CheckEventSignals()
    3.11 +{
    3.12 +    AC_ARG_ENABLE(backgrounding-signal,
    3.13 +AC_HELP_STRING([--enable-backgrounding-signal], [number to use for magic backgrounding signal or 'no' [[default=no]]]),
    3.14 +                  , enable_backgrounding_signal=no)
    3.15 +    if test x$enable_backgrounding_signal != xno; then
    3.16 +        EXTRA_CFLAGS="$EXTRA_CFLAGS -DSDL_BACKGROUNDING_SIGNAL=$enable_backgrounding_signal"
    3.17 +    fi
    3.18 +
    3.19 +    AC_ARG_ENABLE(foregrounding-signal,
    3.20 +AC_HELP_STRING([--enable-foregrounding-signal], [number to use for magic foregrounding signal or 'no' [[default=no]]]),
    3.21 +                  , enable_foregrounding_signal=no)
    3.22 +    if test x$enable_foregrounding_signal != xno; then
    3.23 +        EXTRA_CFLAGS="$EXTRA_CFLAGS -DSDL_FOREGROUNDING_SIGNAL=$enable_foregrounding_signal"
    3.24 +    fi
    3.25 +}
    3.26 +
    3.27 +
    3.28 +
    3.29  dnl Do this on all platforms, before everything else (other things might want to override it).
    3.30  CheckWarnAll
    3.31  
    3.32 +dnl Do this for every platform, but for some it doesn't mean anything, but better to catch it here anyhow.
    3.33 +CheckEventSignals
    3.34 +
    3.35  dnl Set up the configuration based on the host platform!
    3.36  case "$host" in
    3.37      *-*-linux*|*-*-uclinux*|*-*-gnu*|*-*-k*bsd*-gnu|*-*-bsdi*|*-*-freebsd*|*-*-dragonfly*|*-*-netbsd*|*-*-openbsd*|*-*-sysv5*|*-*-solaris*|*-*-hpux*|*-*-aix*|*-*-minix*|*-*-nto*)
    3.38 @@ -3923,7 +3948,7 @@
    3.39          CheckDummyVideo
    3.40          CheckInputEvents
    3.41          CheckPTHREAD
    3.42 -        
    3.43 +
    3.44          # Set up files for the timer library
    3.45          if test x$enable_timers = xyes; then
    3.46              AC_DEFINE(SDL_TIMER_UNIX, 1, [ ])
     4.1 --- a/src/events/SDL_events.c	Fri Mar 15 14:08:30 2019 -0400
     4.2 +++ b/src/events/SDL_events.c	Fri Mar 15 15:51:05 2019 -0400
     4.3 @@ -689,7 +689,7 @@
     4.4      }
     4.5  #endif
     4.6  
     4.7 -    SDL_SendPendingQuit();  /* in case we had a signal handler fire, etc. */
     4.8 +    SDL_SendPendingSignalEvents();  /* in case we had a signal handler fire, etc. */
     4.9  }
    4.10  
    4.11  /* Public functions */
     5.1 --- a/src/events/SDL_events_c.h	Fri Mar 15 14:08:30 2019 -0400
     5.2 +++ b/src/events/SDL_events_c.h	Fri Mar 15 15:51:05 2019 -0400
     5.3 @@ -52,7 +52,7 @@
     5.4  extern int SDL_EventsInit(void);
     5.5  extern void SDL_EventsQuit(void);
     5.6  
     5.7 -extern void SDL_SendPendingQuit(void);
     5.8 +extern void SDL_SendPendingSignalEvents(void);
     5.9  
    5.10  #endif /* SDL_events_c_h_ */
    5.11  
     6.1 --- a/src/events/SDL_quit.c	Fri Mar 15 14:08:30 2019 -0400
     6.2 +++ b/src/events/SDL_quit.c	Fri Mar 15 15:51:05 2019 -0400
     6.3 @@ -34,6 +34,14 @@
     6.4  static SDL_bool disable_signals = SDL_FALSE;
     6.5  static SDL_bool send_quit_pending = SDL_FALSE;
     6.6  
     6.7 +#ifdef SDL_BACKGROUNDING_SIGNAL
     6.8 +static SDL_bool send_backgrounding_pending = SDL_FALSE;
     6.9 +#endif
    6.10 +
    6.11 +#ifdef SDL_FOREGROUNDING_SIGNAL
    6.12 +static SDL_bool send_foregrounding_pending = SDL_FALSE;
    6.13 +#endif
    6.14 +
    6.15  #ifdef HAVE_SIGNAL_H
    6.16  static void
    6.17  SDL_HandleSIG(int sig)
    6.18 @@ -43,46 +51,81 @@
    6.19  
    6.20      /* Send a quit event next time the event loop pumps. */
    6.21      /* We can't send it in signal handler; malloc() might be interrupted! */
    6.22 -    send_quit_pending = SDL_TRUE;
    6.23 +    if ((sig == SIGINT) || (sig == SIGTERM)) {
    6.24 +        send_quit_pending = SDL_TRUE;
    6.25 +    }
    6.26 +
    6.27 +    #ifdef SDL_BACKGROUNDING_SIGNAL
    6.28 +    else if (sig == SDL_BACKGROUNDING_SIGNAL) {
    6.29 +        send_backgrounding_pending = SDL_TRUE;
    6.30 +    }
    6.31 +    #endif
    6.32 +
    6.33 +    #ifdef SDL_FOREGROUNDING_SIGNAL
    6.34 +    else if (sig == SDL_FOREGROUNDING_SIGNAL) {
    6.35 +        send_foregrounding_pending = SDL_TRUE;
    6.36 +    }
    6.37 +    #endif
    6.38  }
    6.39  #endif /* HAVE_SIGNAL_H */
    6.40  
    6.41 -/* Public functions */
    6.42 -static int
    6.43 -SDL_QuitInit_Internal(void)
    6.44 +static void
    6.45 +SDL_EventSignal_Init(const int sig)
    6.46  {
    6.47  #ifdef HAVE_SIGACTION
    6.48      struct sigaction action;
    6.49 -    sigaction(SIGINT, NULL, &action);
    6.50 +
    6.51 +    sigaction(sig, NULL, &action);
    6.52  #ifdef HAVE_SA_SIGACTION
    6.53      if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
    6.54  #else
    6.55      if ( action.sa_handler == SIG_DFL ) {
    6.56  #endif
    6.57          action.sa_handler = SDL_HandleSIG;
    6.58 -        sigaction(SIGINT, &action, NULL);
    6.59 +        sigaction(sig, &action, NULL);
    6.60      }
    6.61 -    sigaction(SIGTERM, NULL, &action);
    6.62 +#elif HAVE_SIGNAL_H
    6.63 +    void (*ohandler) (int) = signal(sig, SDL_HandleSIG);
    6.64 +    if (ohandler != SIG_DFL) {
    6.65 +        signal(sig, ohandler);
    6.66 +    }
    6.67 +#endif
    6.68 +}
    6.69  
    6.70 -#ifdef HAVE_SA_SIGACTION
    6.71 -    if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
    6.72 -#else
    6.73 -    if ( action.sa_handler == SIG_DFL ) {
    6.74 -#endif
    6.75 -        action.sa_handler = SDL_HandleSIG;
    6.76 -        sigaction(SIGTERM, &action, NULL);
    6.77 +static void
    6.78 +SDL_EventSignal_Quit(const int sig)
    6.79 +{
    6.80 +#ifdef HAVE_SIGACTION
    6.81 +    struct sigaction action;
    6.82 +    sigaction(sig, NULL, &action);
    6.83 +    if ( action.sa_handler == SDL_HandleSIG ) {
    6.84 +        action.sa_handler = SIG_DFL;
    6.85 +        sigaction(sig, &action, NULL);
    6.86      }
    6.87  #elif HAVE_SIGNAL_H
    6.88 -    void (*ohandler) (int);
    6.89 +    void (*ohandler) (int) = signal(sig, SIG_DFL);
    6.90 +    if (ohandler != SDL_HandleSIG) {
    6.91 +        signal(sig, ohandler);
    6.92 +    }
    6.93 +#endif /* HAVE_SIGNAL_H */
    6.94 +}
    6.95  
    6.96 +/* Public functions */
    6.97 +static int
    6.98 +SDL_QuitInit_Internal(void)
    6.99 +{
   6.100      /* Both SIGINT and SIGTERM are translated into quit interrupts */
   6.101 -    ohandler = signal(SIGINT, SDL_HandleSIG);
   6.102 -    if (ohandler != SIG_DFL)
   6.103 -        signal(SIGINT, ohandler);
   6.104 -    ohandler = signal(SIGTERM, SDL_HandleSIG);
   6.105 -    if (ohandler != SIG_DFL)
   6.106 -        signal(SIGTERM, ohandler);
   6.107 -#endif /* HAVE_SIGNAL_H */
   6.108 +    /* and SDL can be built to simulate iOS/Android semantics with arbitrary signals. */
   6.109 +    SDL_EventSignal_Init(SIGINT);
   6.110 +    SDL_EventSignal_Init(SIGTERM);
   6.111 +
   6.112 +    #ifdef SDL_BACKGROUNDING_SIGNAL
   6.113 +    SDL_EventSignal_Init(SDL_BACKGROUNDING_SIGNAL);
   6.114 +    #endif
   6.115 +
   6.116 +    #ifdef SDL_FOREGROUNDING_SIGNAL
   6.117 +    SDL_EventSignal_Init(SDL_FOREGROUNDING_SIGNAL);
   6.118 +    #endif
   6.119  
   6.120      /* That's it! */
   6.121      return 0;
   6.122 @@ -100,28 +143,16 @@
   6.123  static void
   6.124  SDL_QuitQuit_Internal(void)
   6.125  {
   6.126 -#ifdef HAVE_SIGACTION
   6.127 -    struct sigaction action;
   6.128 -    sigaction(SIGINT, NULL, &action);
   6.129 -    if ( action.sa_handler == SDL_HandleSIG ) {
   6.130 -        action.sa_handler = SIG_DFL;
   6.131 -        sigaction(SIGINT, &action, NULL);
   6.132 -    }
   6.133 -    sigaction(SIGTERM, NULL, &action);
   6.134 -    if ( action.sa_handler == SDL_HandleSIG ) {
   6.135 -        action.sa_handler = SIG_DFL;
   6.136 -        sigaction(SIGTERM, &action, NULL);
   6.137 -    }
   6.138 -#elif HAVE_SIGNAL_H
   6.139 -    void (*ohandler) (int);
   6.140 +    SDL_EventSignal_Quit(SIGINT);
   6.141 +    SDL_EventSignal_Quit(SIGTERM);
   6.142  
   6.143 -    ohandler = signal(SIGINT, SIG_DFL);
   6.144 -    if (ohandler != SDL_HandleSIG)
   6.145 -        signal(SIGINT, ohandler);
   6.146 -    ohandler = signal(SIGTERM, SIG_DFL);
   6.147 -    if (ohandler != SDL_HandleSIG)
   6.148 -        signal(SIGTERM, ohandler);
   6.149 -#endif /* HAVE_SIGNAL_H */
   6.150 +    #ifdef SDL_BACKGROUNDING_SIGNAL
   6.151 +    SDL_EventSignal_Quit(SDL_BACKGROUNDING_SIGNAL);
   6.152 +    #endif
   6.153 +
   6.154 +    #ifdef SDL_FOREGROUNDING_SIGNAL
   6.155 +    SDL_EventSignal_Quit(SDL_FOREGROUNDING_SIGNAL);
   6.156 +    #endif
   6.157  }
   6.158  
   6.159  void
   6.160 @@ -141,12 +172,26 @@
   6.161  }
   6.162  
   6.163  void
   6.164 -SDL_SendPendingQuit(void)
   6.165 +SDL_SendPendingSignalEvents(void)
   6.166  {
   6.167      if (send_quit_pending) {
   6.168          SDL_SendQuit();
   6.169          SDL_assert(!send_quit_pending);
   6.170      }
   6.171 +
   6.172 +    #ifdef SDL_BACKGROUNDING_SIGNAL
   6.173 +    if (send_backgrounding_pending) {
   6.174 +        send_backgrounding_pending = SDL_FALSE;
   6.175 +        SDL_OnApplicationWillResignActive();
   6.176 +    }
   6.177 +    #endif
   6.178 +
   6.179 +    #ifdef SDL_FOREGROUNDING_SIGNAL
   6.180 +    if (send_foregrounding_pending) {
   6.181 +        send_foregrounding_pending = SDL_FALSE;
   6.182 +        SDL_OnApplicationDidBecomeActive();
   6.183 +    }
   6.184 +    #endif
   6.185  }
   6.186  
   6.187  /* vi: set ts=4 sw=4 expandtab: */