summaryrefslogtreecommitdiff
path: root/xwake.c
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2019-04-17 12:00:29 +0930
committerDaniel Jones <admin@danieljon.es>2019-04-17 12:00:29 +0930
commit667bf7127e0da7160c3eea03151e37590caa1332 (patch)
treedde95587a907fd54cda1862a6955c9ea68ea645f /xwake.c
downloadxwake-667bf7127e0da7160c3eea03151e37590caa1332.tar.gz
xwake-667bf7127e0da7160c3eea03151e37590caa1332.zip
first commit
Diffstat (limited to 'xwake.c')
-rw-r--r--xwake.c101
1 files changed, 101 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+#include <X11/Xlib.h>
+#include <X11/extensions/scrnsaver.h>
+
+/*
+ * 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;
+}