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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <string.h>
#include "config.h"
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xmu/CurUtil.h>
#include <X11/Xcursor/Xcursor.h>
#define debug(x) printf("%s\n", x)
#define IBUF_LEN (10 * (sizeof(struct inotify_event) + strlen(BAT0_DIR) + 1))
volatile int need_exit = 0;
struct gc_data {
int *openfd;
char *bat0bufptr;
off_t file_sz;
};
struct xorg_data {
Display *dpy;
int screen;
Window root;
};
static void x_start(struct xorg_data *xorg_data)
{
xorg_data->dpy = XOpenDisplay(NULL);
xorg_data->screen = DefaultScreen(xorg_data->dpy);
xorg_data->root = RootWindow(xorg_data->dpy, xorg_data->screen);
}
static void xsetroot_summon(struct xorg_data *xorg_data, char* text)
{
XStoreName(xorg_data->dpy, xorg_data->root, text);
}
static void x_end(struct xorg_data *xorg_data)
{
XCloseDisplay(xorg_data->dpy);
}
void signal_handler(int signal)
{
need_exit = 1;
}
static inline void run_garbage_collector_clear(struct gc_data *gc)
{
/* close(*gc->epfd); */
close(*gc->openfd);
free(gc->bat0bufptr);
}
static __off_t getfile_size(int fd)
{
struct stat st;
int ret;
ret = fstat(fd, &st);
if (ret == -1) {
perror("fstat");
return -1;
}
return st.st_size;
}
static inline int setup_fd(struct gc_data *gc)
{
int ret;
ret = open(BAT0_DIR, O_RDONLY);
if (ret == -1) {
perror("open");
return -1;
}
gc->openfd = &ret;
off_t size = getfile_size(*gc->openfd);
gc->bat0bufptr = (char*)malloc(size * sizeof(char));
gc->file_sz = size;
}
static char* get_time()
{
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime(&rawtime);
return asctime(timeinfo);
}
char* newline_cut(char *str, struct gc_data *gc)
{
char* data = (char*)malloc(gc->file_sz);
memset(data, '\0', gc->file_sz);
for(int i = 0 ; i < gc->file_sz; i++) {
if (*(str + i) != '\n') {
*(data + i) = *(str + i);
}
}
return data;
}
static void get_bat_num(struct gc_data *gc)
{
read(*gc->openfd, gc->bat0bufptr, gc->file_sz);
}
static void exec_notify_send(struct gc_data *gc, char** envp, char* text)
{
char* args[] = {
NOTIFY_SEND_PATH, text, NULL
};
execve(NOTIFY_SEND_PATH, args, envp);
}
static void exec_xsetroot(struct gc_data *gc, char** envp, char* text)
{
char* args[] = {
XSETROOT_PATH, "-name", text, NULL
};
execve(XSETROOT_PATH, args, envp);
}
int main(int argc, char **argv, char *envp[])
{
signal(SIGINT, signal_handler);
struct gc_data gc;
struct xorg_data xorg_data;
char randombuf[100];
setup_fd(&gc);
while (!need_exit) {
memset(randombuf, '\0', 100);
get_bat_num(&gc);
char* battery = newline_cut(gc.bat0bufptr, &gc);
char* time = newline_cut(get_time(), &gc);
snprintf(randombuf, 100, "%s %s %s", time, battery, DESKTOP_NAME);
// exec_xsetroot(&gc, envp, randombuf);
x_start(&xorg_data);
xsetroot_summon(&xorg_data, randombuf);
x_end(&xorg_data);
free(battery);
free(time);
sleep(1);
}
run_garbage_collector_clear(&gc);
return 0;
}
|