Allolib  1.0
C++ Components For Interactive Multimedia
al_Signal.hpp
1 #ifndef AL_SIGNAL_H
2 #define AL_SIGNAL_H
3 
4 /*
5 
6 Manages signals & interrupts
7 
8 need to invoke
9 
10 registerSigInt(this);
11 
12 in onCreate
13 
14 This will allow onExit to execute even when app is abruptly killed.
15 
16 Kon Hyong Kim, 2019
17 konhyong@gmail.com
18 
19 */
20 
21 #include <csignal>
22 #include "al/app/al_App.hpp"
23 
24 namespace al {
25 
26 static void* userApp;
27 void* getUser() { return userApp; }
28 
29 static void userHandler(int s) {
30  // printf("Caught signal %d\n", s);
31  ((App*)getUser())->onExit();
32 
33  exit(1);
34 }
35 
36 void registerSigInt(void* app) {
37  userApp = app;
38 
39 #ifdef _WIN32
40  signal(SIGINT, userHandler);
41  signal(SIGTERM, userHandler);
42  signal(SIGABRT, userHandler);
43 #else
44  struct sigaction sigIntHandler;
45  sigIntHandler.sa_handler = userHandler;
46  sigemptyset(&sigIntHandler.sa_mask);
47  sigIntHandler.sa_flags = 0;
48  sigaction(SIGINT, &sigIntHandler, NULL);
49 #endif
50 }
51 
52 } // namespace al
53 
54 #endif
Definition: al_App.hpp:23