-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuManage.c
309 lines (280 loc) · 8.25 KB
/
uManage.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <xdo.h>
#include "uManage.h"
#include "winmgmt.h"
#include "config.h"
#include "idle.h"
#include "indicate.h"
#include "sqlite.h"
#include "mouse.h"
#include "weather.h"
pthread_t thr_wx;
struct window_state current;
struct program_options opts;
FILE *report;
int poll_continue = 1,
thr_wx_running = 0;
void *get_weather_data(void *);
int main (int argc, char *argv[]) {
#ifdef GUI
void *status;
char path[256];
pthread_t thr_menu;
#endif
/*
* Program setup
* Handle user interrupt and alarm
* Get program options
* Open report file for logging if needed
* Initialize UI
*/
signal(SIGINT, handle_break);
signal(SIGALRM, handle_alarm);
alarm(1);
get_configuration(&opts);
if (parse_options(argc, argv, &opts)) {
return 1;
}
if (opts.save_options) {
save_configuration(&opts);
}
if (opts.text_out) {
report = fopen(opts.filename, "a");
if(report == NULL) {
report = stdout;
}
}
init_idle();
init_winmgmt(&opts);
window_state_init(¤t);
#ifdef GUI
get_executable_path(path, sizeof(path));
init_indicator(argc, argv, path, &opts);
if (opts.menu_items != NULL) {
add_menu_items(opts.menu_items);
}
fflush(NULL);
pthread_create(&thr_menu, NULL, run_indicator, ¤t.force);
#endif
if (opts.use_database) {
open_database(opts.dbname);
}
while(poll_continue || current.force) {
sleep(1); /* Just wait for program close */
}
/*
* Clean up
*/
alarm(1); /* Last hurrah */
sleep(1);
#ifdef GUI
pthread_join(thr_menu, &status);
#endif
if (report != stdout && opts.text_out) {
fclose(report);
}
if (opts.use_database) {
close_database();
}
return 0;
}
void handle_break (int signal) {
/*
* On our way out, we need to identify the final, buffered
* window, flush any buffers, and close the file.
*/
if (signal != SIGINT) {
return;
}
#ifdef GUI
stop_indicator();
#else
current.force = 1;
opts.pause = 0;
opts.jiggle = 0;
#endif
}
void handle_alarm (int sig) {
/*
* Check for new idling
* Accumulate total idle time per window use
* Check for changed window
* Log status if interesting
* Set for next iteration
*/
void *status;
unsigned long idle; /* Idle time in ms */
time_t idle_dur;
#ifdef GUI
int idx;
#endif
if (sig != SIGALRM) {
return;
}
signal(SIGALRM, SIG_IGN);
if (opts.pause) {
if (current.pause_since == 0) {
time(¤t.pause_since);
}
signal(SIGALRM, handle_alarm);
alarm(opts.poll_period);
return;
}
idle = idle_time();
if (thr_wx_running < 0) {
pthread_join(thr_wx, &status);
thr_wx_running = 0;
}
time(&idle_dur);
if(thr_wx_running == 0 &&
opts.use_database &&
idle_dur - current.weather_since > 60 * 60 * 10) {
time(¤t.weather_since);
pthread_create(&thr_wx, NULL, get_weather_data, 0);
}
if(idle < current.last_idle) {
/* New idle "session" */
if(idle > opts.idle_threshold * (unsigned long)1000) {
/* It has been long enough, so reset the idle timer */
/* ...but round up. */
time(¤t.idle_start);
current.idle_start -= (idle + 500) / 1000;
}
else if(current.idle_start != 0) {
/* If we're tracking idle time and it grows, add it */
time(&idle_dur);
idle_dur -= current.idle_start;
current.idle_accumulated += idle_dur;
current.idle_start = 0;
}
}
#ifdef GUI
for(idx = 0; idx < opts.menu_len; idx++) {
if (opts.userdef[idx] == 1) {
/* Newly active */
time((time_t *)&opts.userdef[idx]);
} else if (opts.userdef[idx] < 0) {
/* Deactivated Option */
time(&idle_dur);
opts.userdef[idx] = -opts.userdef[idx];
report_duration(current.csv, opts.time_format, (time_t *)&opts.userdef[idx], &idle_dur);
if (opts.use_database) {
write_duration_to_database(current.csv, opts.menu_items[idx], opts.cycle_db);
}
opts.userdef[idx] = 0;
}
}
if(opts.jiggle != 0) {
time(&idle_dur);
/* We need to worry about jiggling the mouse */
if(current.last_jiggle == 0) {
time(¤t.last_jiggle);
} else if ((idle_dur - current.last_jiggle) / opts.mouse_period > 0) {
move_mouse(opts.mouse_dist);
current.last_jiggle = 0;
}
}
if (current.jiggle_since == 0 && opts.jiggle != 0) {
/* Feature turned on, needs to be tracked */
time(¤t.jiggle_since);
} else if (current.jiggle_since != 0 && opts.jiggle == 0) {
/* Feature turned off, emit duration */
time(&idle_dur);
report_duration(current.csv, opts.time_format, ¤t.jiggle_since, &idle_dur);
current.jiggle_since = 0;
if (opts.use_database) {
write_keepalive_to_database(current.csv, opts.cycle_db);
}
}
if (current.pause_since != 0 && opts.pause == 0) {
/* Feature turned off, emit duration */
time(&idle_dur);
report_duration(current.csv, opts.time_format, ¤t.pause_since, &idle_dur);
current.pause_since = 0;
if (opts.use_database) {
write_duration_to_database(current.csv, "Pause", opts.cycle_db);
}
}
#endif
if(is_window_updated(¤t, &poll_continue, opts.use_database)) {
if (opts.use_database) {
write_activity_to_database(current.csv, opts.cycle_db);
}
if (opts.text_out) {
/* Flush in case someone monitors the output file */
fprintf(report, "%s\n", current.csv);
fflush(report);
}
}
/* Reset this, so that we don't have problems exiting */
current.force = 0;
if(opts.poll_period < 1) {
/* I might want to use this for one-off scripts */
return;
}
signal(SIGALRM, handle_alarm);
alarm(opts.poll_period);
}
void window_state_init (struct window_state *state) {
state->force = 0;
state->last_jiggle = 0;
state->jiggle_since = 0;
state->pause_since = 0;
state->last_idle = (unsigned long)(-1);
state->idle_start = 0;
state->idle_accumulated = 0;
time(&state->window_start);
strcpy((char *)state->window_title, "");
}
void *get_weather_data(void *arg) {
int year, month, rows;
char csv[128];
time_t now;
struct tm *ti;
if (arg != NULL) {
/* Just use the parameter for something... */
}
thr_wx_running = 1;
time(&now);
ti = localtime(&now);
year = ti->tm_year + 1900;
month = ti->tm_mon;
if (month == 0) {
month = 12;
}
rows = queryRowsForMonth("weather", year, month);
if (rows > 0) {
thr_wx_running = -2;
return NULL;
}
rows = getWxMonth(year, month, opts.airport);
while (wxLinesRemaining()) {
nextWxLine(csv);
write_weather_to_database(csv, opts.cycle_db);
}
thr_wx_running = -1;
return NULL;
}
void get_executable_path (char *path, size_t length) {
/*
* Please note that this routine is not entirely portable.
* /proc/self/exe is specific to Linux
* For FreeBSD, change to "/proc/curproc/file"
* For Solaris, change to "/proc/self/path/a.out"
* Others will be added as discovered, and argv[0] may be
* manipulated, in some cases.
*/
unsigned int index;
for(index=0;index<sizeof(path);index++) {
path[index] = '\000';
}
readlink("/proc/self/exe", path, length);
for(index = length - 1; index > 0 && path[index] != '/'; index--) {
path[index] = '\000';
}
}