-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmonitor.c
165 lines (122 loc) · 3.7 KB
/
xmonitor.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <errno.h>
#include "xmonitor.h"
#include "events.h"
#include "log.h"
static XEvent event;
static Display *dis;
static Window root;
static int screen = -1;
static XRROutputInfo *output_info;
static XRRScreenResources *res;
void insert_event(const XRRNotifyEvent *e, const struct timeval *t){
int i;
char msg[255];
t_event ev;
ev.etype = e->type;
memcpy(&ev.timestamp, t, sizeof (struct timeval));
if (exec_count == 0){
exec_events[0] = ev;
exec_count++;
cache_pos++;
logMessage(INFO, "Starting executed event cache\n");
}else if (exec_count <= NUM_EVENTS){
exec_events[exec_count - 1] = ev;
exec_count++;
cache_pos++;
sprintf(msg, "Added %d into events cache, now size is: %d\n", ev.etype, exec_count);
logMessage(INFO, msg);
}else{
exec_events[(cache_pos % NUM_EVENTS) - 1] = ev;
cache_pos++;
logMessage(INFO, "Events cache full, replacing oldest one\n");
}
sprintf(msg, "cache count: %d =============\n", exec_count);
logMessage(INFO, msg);
for (i = 0; i < NUM_EVENTS; i++){
sprintf(msg, "\tCache[%d]} = Event: %d, timestamp: %ld %ld\n", i, exec_events[i].etype, exec_events[i].timestamp.tv_sec, exec_events[i].timestamp.tv_usec);
logMessage(INFO, msg);
}
logMessage(INFO, "Event cache dump end =========================\n");
}
int check_event(const XRRNotifyEvent *e, const struct timeval *t){
int i;
char *msg = (char*)malloc(255 * sizeof(char));
for (i = 0; i <= exec_count ; i++){
if(exec_events[i].etype == e->type){
time_t tsdiff = t->tv_sec - exec_events[i].timestamp.tv_sec;
time_t tusdiff = 0;
time_t tdiff;
if (tsdiff == 0){
tusdiff = t->tv_usec - exec_events[i].timestamp.tv_usec;
}
tdiff = tsdiff + (100 * tusdiff);
sprintf(msg, "Event already found, time diff %lu. (> THS? %d)\n", tdiff, tdiff >= TIME_THS);
logMessage(INFO, msg);
return tdiff >= TIME_THS ? 1 : 0;
}
}
return 1;
}
int init_monitor(){
int outi;
char msg[255];
dis = XOpenDisplay(NULL);
startLogger();
if (!dis){
logMessage(ERROR, "Cannot connect into XServer using default display...exiting");
return ENOMEM;
}
screen = DefaultScreen(dis);
logMessage(INFO, "Loading root window");
root = RootWindow(dis, screen);
/*Select X input to handle the events*/
logMessage(INFO, "Selecting input");
XSelectInput(dis, root, StructureNotifyMask);
/*Getting Screen Resources*/
logMessage(INFO, "Getting screen resources");
res = XRRGetScreenResources(dis, root);
sprintf(msg, "%d resources found", res->noutput);
logMessage(INFO, msg);
for (outi = 0; outi < res->noutput; outi++){
output_info = XRRGetOutputInfo(dis, res, res->outputs[outi]);
sprintf(msg, "Output name: %s", output_info->name);
logMessage(INFO, msg);
}
return 0;
}
void start_monitor(){
struct timeval *currenttime;
exec_count = 0;
currenttime = (struct timeval*)malloc(sizeof(struct timeval));
exec_events = (t_event*)calloc(NUM_EVENTS, sizeof(t_event));
/*Keep waiting for new X events for a given screen*/
while(1){
XNextEvent(dis, (XEvent*) &event);
XRRNotifyEvent *ev = (XRRNotifyEvent*)&event;
gettimeofday(currenttime, NULL);
if(!check_event(ev, currenttime)){
logMessage(WARNING, "Event already executed within threshold parameters..skiping this round\n");
continue;
}
insert_event(ev, currenttime);
execute_events(&event);
}
free(currenttime);
free(exec_events);
}
int main(int argc, char **argv){
int init_status;
if((init_status = init_monitor())){
fprintf(stderr, "Xevent-monitor failed to init, aborting...\n");
return init_status;
}
start_monitor();
endLogger();
return EXIT_SUCCESS;
}