From 667bf7127e0da7160c3eea03151e37590caa1332 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 17 Apr 2019 12:00:29 +0930 Subject: first commit --- xwake.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 xwake.c (limited to 'xwake.c') diff --git a/xwake.c b/xwake.c new file mode 100644 index 0000000..0701db6 --- /dev/null +++ b/xwake.c @@ -0,0 +1,101 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + *(at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +/* + * start - the time to start preventing the screen from sleeping + * end - the time to stop preventing the screen from sleeping + * must be in "xx:xx" format + */ +char start[] = "08:00"; +char end[] = "21:00"; + +void +gettime(int *store) +{ + time_t t = time(NULL); + struct tm *tm = localtime(&t); + store[0] = tm->tm_hour; + store[1] = tm->tm_min; +} + +void +gettriggertimes(int *startarr, int *endarr) +{ + startarr[0] = atoi(strtok(start, ":")); + startarr[1] = atoi(strtok(NULL, ":")); + + endarr[0] = atoi(strtok(end, ":")); + endarr[1] = atoi(strtok(NULL, ":")); + +} + +int +main(int argc, char *argv[]) +{ + int time[2]; + int startarr[2]; + int endarr[2]; + int setwontsleep = 0; + gettriggertimes(startarr, endarr); + Display *dsp = XOpenDisplay(NULL); + if(!dsp) + { + fprintf(stderr, "cannot open X display\n"); + exit(EXIT_FAILURE); + } + if (argc > 1) + { + if (strcmp(argv[1], "reset") == 0) + { + puts("enabling screensaver and exiting"); + XScreenSaverSuspend(dsp, False); + XFlush(dsp); + XCloseDisplay(dsp); + return 0; + } + else + { + printf("usage: %s [reset]\nreset: re-enable screensaver\n", argv[0]); + return 0; + } + } + while (1) + { + gettime(time); + if (setwontsleep == 0 && (startarr[0] == time[0]) && (startarr[1] == time[1])) + { + XScreenSaverSuspend(dsp, True); + XFlush(dsp); + setwontsleep = 1; + } + else if (setwontsleep == 1 && endarr[0] == time[0] && endarr[1] == time[1]) + { + XScreenSaverSuspend(dsp, False); + XFlush(dsp); + setwontsleep = 0; + } + sleep(5); + } + XCloseDisplay(dsp); + return 0; +} -- cgit v1.2.3