-
Notifications
You must be signed in to change notification settings - Fork 0
/
winmgmt.c
106 lines (95 loc) · 3.19 KB
/
winmgmt.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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <xdo.h>
#include "config.h"
#include "uManage.h"
#include "winmgmt.h"
struct program_options *options = NULL;
xdo_t *xdo;
void init_winmgmt(struct program_options *progopts) {
xdo = xdo_new(NULL);
options = progopts;
}
int is_window_updated (struct window_state *state, int *poll_continue, int quote_args) {
/*
* Grab the focused window's ID and title
* If it's a new title, better to keep track of file saves than not,
* likewise with browser tabs, which is why we don't test
* the window ID
* Generate the CSV
* Reset the window state
* Notify upstream whether anything useful is going on
*
* Note that the XDO documentation appears to be out of date. It
* doesn't list xdo_get_focused_window_sane(), but does have a
* xdo_window_get_focus(), which doesn't actually exist.
*/
Window win;
unsigned char *name;
int n_len,
n_type;
time_t now;
xdo_get_focused_window_sane(xdo, &win);
xdo_get_window_name(xdo, win, &name, &n_len, &n_type);
if((name && strcmp((char *)name, (char *)state->window_title)) || state->force) {
now = window_state_report(state, quote_args);
state->window_start = now;
state->window_id = win;
strcpy((char *)state->window_title, (char *)name);
state->idle_accumulated = 0;
state->idle_start = 0;
state->last_idle = (unsigned long)(-1);
if (state->force) {
*poll_continue = 0;
}
return 1;
}
return 0;
}
time_t window_state_report (struct window_state *state, int quote_args) {
/*
* Get the duration the most recent window has been used and
* retrieve the relevant CSV.
*/
time_t window_end,
window_dur;
time(&window_end);
window_dur = window_end - state->window_start;
window_state_format(state, NULL, &window_dur, quote_args);
return window_end;
}
char * window_state_format (struct window_state *state, time_t *instead, time_t *duration, int quote_args) {
/*
* Create the CSV line inside the window state, ready for printing.
*
* If the caller set the instead parameter, use it instead of the
* state's duration.
*/
char tstr[64],
format_string[64];
time_t *time = instead,
used;
struct tm *start = NULL;
if (time == NULL) {
time = &state->window_start;
}
start = localtime(time);
strftime(tstr, sizeof(tstr), options->time_format, start);
used = *duration - state->idle_accumulated;
if ((long)used < 0) {
used = 0;
}
if (quote_args) {
sprintf(format_string, "\"%%s\",\"%%08X\",\"%%s\",%%u,%%u");
} else {
sprintf(format_string, "%%s,%%08X,\"%%s\",%%u,%%u");
}
/* Time,Window ID,Window Title,Time Used,Time Idle */
sprintf(state->csv, format_string,
tstr,
(unsigned)state->window_id, state->window_title,
(unsigned)used,
(unsigned)state->idle_accumulated);
return state->csv;
}