summaryrefslogtreecommitdiff
path: root/xwake.c
blob: 5ecef8fcaff5d5e34b31ab54e2eb89d692ea9520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * 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)
{
	char smodify[6];
	char emodify[6];
	strncpy(smodify, start, 6);
	strncpy(emodify, end, 6);
	startarr[0] = atoi(strtok(smodify, ":"));
	startarr[1] = atoi(strtok(NULL, ":"));

	endarr[0] = atoi(strtok(emodify, ":"));
	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;
}