mirror of
https://github.com/encounter/SDL.git
synced 2025-12-09 05:27:48 +00:00
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.
This commit is contained in:
@@ -689,7 +689,7 @@ SDL_PumpEvents(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_SendPendingQuit(); /* in case we had a signal handler fire, etc. */
|
||||
SDL_SendPendingSignalEvents(); /* in case we had a signal handler fire, etc. */
|
||||
}
|
||||
|
||||
/* Public functions */
|
||||
|
||||
@@ -52,7 +52,7 @@ extern int SDL_SendQuit(void);
|
||||
extern int SDL_EventsInit(void);
|
||||
extern void SDL_EventsQuit(void);
|
||||
|
||||
extern void SDL_SendPendingQuit(void);
|
||||
extern void SDL_SendPendingSignalEvents(void);
|
||||
|
||||
#endif /* SDL_events_c_h_ */
|
||||
|
||||
|
||||
@@ -34,6 +34,14 @@
|
||||
static SDL_bool disable_signals = SDL_FALSE;
|
||||
static SDL_bool send_quit_pending = SDL_FALSE;
|
||||
|
||||
#ifdef SDL_BACKGROUNDING_SIGNAL
|
||||
static SDL_bool send_backgrounding_pending = SDL_FALSE;
|
||||
#endif
|
||||
|
||||
#ifdef SDL_FOREGROUNDING_SIGNAL
|
||||
static SDL_bool send_foregrounding_pending = SDL_FALSE;
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SIGNAL_H
|
||||
static void
|
||||
SDL_HandleSIG(int sig)
|
||||
@@ -43,46 +51,81 @@ SDL_HandleSIG(int sig)
|
||||
|
||||
/* Send a quit event next time the event loop pumps. */
|
||||
/* We can't send it in signal handler; malloc() might be interrupted! */
|
||||
send_quit_pending = SDL_TRUE;
|
||||
if ((sig == SIGINT) || (sig == SIGTERM)) {
|
||||
send_quit_pending = SDL_TRUE;
|
||||
}
|
||||
|
||||
#ifdef SDL_BACKGROUNDING_SIGNAL
|
||||
else if (sig == SDL_BACKGROUNDING_SIGNAL) {
|
||||
send_backgrounding_pending = SDL_TRUE;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SDL_FOREGROUNDING_SIGNAL
|
||||
else if (sig == SDL_FOREGROUNDING_SIGNAL) {
|
||||
send_foregrounding_pending = SDL_TRUE;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif /* HAVE_SIGNAL_H */
|
||||
|
||||
static void
|
||||
SDL_EventSignal_Init(const int sig)
|
||||
{
|
||||
#ifdef HAVE_SIGACTION
|
||||
struct sigaction action;
|
||||
|
||||
sigaction(sig, NULL, &action);
|
||||
#ifdef HAVE_SA_SIGACTION
|
||||
if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
|
||||
#else
|
||||
if ( action.sa_handler == SIG_DFL ) {
|
||||
#endif
|
||||
action.sa_handler = SDL_HandleSIG;
|
||||
sigaction(sig, &action, NULL);
|
||||
}
|
||||
#elif HAVE_SIGNAL_H
|
||||
void (*ohandler) (int) = signal(sig, SDL_HandleSIG);
|
||||
if (ohandler != SIG_DFL) {
|
||||
signal(sig, ohandler);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
SDL_EventSignal_Quit(const int sig)
|
||||
{
|
||||
#ifdef HAVE_SIGACTION
|
||||
struct sigaction action;
|
||||
sigaction(sig, NULL, &action);
|
||||
if ( action.sa_handler == SDL_HandleSIG ) {
|
||||
action.sa_handler = SIG_DFL;
|
||||
sigaction(sig, &action, NULL);
|
||||
}
|
||||
#elif HAVE_SIGNAL_H
|
||||
void (*ohandler) (int) = signal(sig, SIG_DFL);
|
||||
if (ohandler != SDL_HandleSIG) {
|
||||
signal(sig, ohandler);
|
||||
}
|
||||
#endif /* HAVE_SIGNAL_H */
|
||||
}
|
||||
|
||||
/* Public functions */
|
||||
static int
|
||||
SDL_QuitInit_Internal(void)
|
||||
{
|
||||
#ifdef HAVE_SIGACTION
|
||||
struct sigaction action;
|
||||
sigaction(SIGINT, NULL, &action);
|
||||
#ifdef HAVE_SA_SIGACTION
|
||||
if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
|
||||
#else
|
||||
if ( action.sa_handler == SIG_DFL ) {
|
||||
#endif
|
||||
action.sa_handler = SDL_HandleSIG;
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
}
|
||||
sigaction(SIGTERM, NULL, &action);
|
||||
|
||||
#ifdef HAVE_SA_SIGACTION
|
||||
if ( action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL ) {
|
||||
#else
|
||||
if ( action.sa_handler == SIG_DFL ) {
|
||||
#endif
|
||||
action.sa_handler = SDL_HandleSIG;
|
||||
sigaction(SIGTERM, &action, NULL);
|
||||
}
|
||||
#elif HAVE_SIGNAL_H
|
||||
void (*ohandler) (int);
|
||||
|
||||
/* Both SIGINT and SIGTERM are translated into quit interrupts */
|
||||
ohandler = signal(SIGINT, SDL_HandleSIG);
|
||||
if (ohandler != SIG_DFL)
|
||||
signal(SIGINT, ohandler);
|
||||
ohandler = signal(SIGTERM, SDL_HandleSIG);
|
||||
if (ohandler != SIG_DFL)
|
||||
signal(SIGTERM, ohandler);
|
||||
#endif /* HAVE_SIGNAL_H */
|
||||
/* and SDL can be built to simulate iOS/Android semantics with arbitrary signals. */
|
||||
SDL_EventSignal_Init(SIGINT);
|
||||
SDL_EventSignal_Init(SIGTERM);
|
||||
|
||||
#ifdef SDL_BACKGROUNDING_SIGNAL
|
||||
SDL_EventSignal_Init(SDL_BACKGROUNDING_SIGNAL);
|
||||
#endif
|
||||
|
||||
#ifdef SDL_FOREGROUNDING_SIGNAL
|
||||
SDL_EventSignal_Init(SDL_FOREGROUNDING_SIGNAL);
|
||||
#endif
|
||||
|
||||
/* That's it! */
|
||||
return 0;
|
||||
@@ -100,28 +143,16 @@ SDL_QuitInit(void)
|
||||
static void
|
||||
SDL_QuitQuit_Internal(void)
|
||||
{
|
||||
#ifdef HAVE_SIGACTION
|
||||
struct sigaction action;
|
||||
sigaction(SIGINT, NULL, &action);
|
||||
if ( action.sa_handler == SDL_HandleSIG ) {
|
||||
action.sa_handler = SIG_DFL;
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
}
|
||||
sigaction(SIGTERM, NULL, &action);
|
||||
if ( action.sa_handler == SDL_HandleSIG ) {
|
||||
action.sa_handler = SIG_DFL;
|
||||
sigaction(SIGTERM, &action, NULL);
|
||||
}
|
||||
#elif HAVE_SIGNAL_H
|
||||
void (*ohandler) (int);
|
||||
SDL_EventSignal_Quit(SIGINT);
|
||||
SDL_EventSignal_Quit(SIGTERM);
|
||||
|
||||
ohandler = signal(SIGINT, SIG_DFL);
|
||||
if (ohandler != SDL_HandleSIG)
|
||||
signal(SIGINT, ohandler);
|
||||
ohandler = signal(SIGTERM, SIG_DFL);
|
||||
if (ohandler != SDL_HandleSIG)
|
||||
signal(SIGTERM, ohandler);
|
||||
#endif /* HAVE_SIGNAL_H */
|
||||
#ifdef SDL_BACKGROUNDING_SIGNAL
|
||||
SDL_EventSignal_Quit(SDL_BACKGROUNDING_SIGNAL);
|
||||
#endif
|
||||
|
||||
#ifdef SDL_FOREGROUNDING_SIGNAL
|
||||
SDL_EventSignal_Quit(SDL_FOREGROUNDING_SIGNAL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@@ -141,12 +172,26 @@ SDL_SendQuit(void)
|
||||
}
|
||||
|
||||
void
|
||||
SDL_SendPendingQuit(void)
|
||||
SDL_SendPendingSignalEvents(void)
|
||||
{
|
||||
if (send_quit_pending) {
|
||||
SDL_SendQuit();
|
||||
SDL_assert(!send_quit_pending);
|
||||
}
|
||||
|
||||
#ifdef SDL_BACKGROUNDING_SIGNAL
|
||||
if (send_backgrounding_pending) {
|
||||
send_backgrounding_pending = SDL_FALSE;
|
||||
SDL_OnApplicationWillResignActive();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SDL_FOREGROUNDING_SIGNAL
|
||||
if (send_foregrounding_pending) {
|
||||
send_foregrounding_pending = SDL_FALSE;
|
||||
SDL_OnApplicationDidBecomeActive();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
Reference in New Issue
Block a user