Use signals to allow us to exit the application and do proper cleanup
This commit is contained in:
parent
89b4253d54
commit
4b181e30e3
1 changed files with 25 additions and 7 deletions
|
@ -1,13 +1,16 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <telldus-core.h>
|
#include <telldus-core.h>
|
||||||
|
|
||||||
|
bool running;
|
||||||
|
|
||||||
class Events {
|
class Events {
|
||||||
public:
|
public:
|
||||||
Events();
|
Events();
|
||||||
~Events();
|
~Events();
|
||||||
void deviceEvent(int deviceId, int method, const char *data);
|
void deviceEvent(int deviceId, int method, const char *data);
|
||||||
|
|
||||||
static void deviceEventCallback(int deviceId, int method, const char *data, int callbackId, void *context);
|
static void deviceEventCallback(int deviceId, int method, const char *data, int callbackId, void *context);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -43,15 +46,30 @@ void Events::deviceEventCallback(int deviceId, int method, const char *data, int
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void signalHandler(int sig) {
|
||||||
|
if (sig == SIGINT) {
|
||||||
|
printf("Shutting down\n");
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
Events ev;
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
printf("Listening for events, press control-c to quit...\n");
|
||||||
|
//Our own simple eventloop
|
||||||
|
while(running) {
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
tdInit();
|
tdInit();
|
||||||
|
|
||||||
Events ev;
|
signal(SIGINT, signalHandler);
|
||||||
|
run();
|
||||||
//Our own simple eventloop
|
|
||||||
while(1) {
|
|
||||||
sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
tdClose();
|
tdClose();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue