-
Notifications
You must be signed in to change notification settings - Fork 0
/
fyfsp.c
275 lines (215 loc) · 8.27 KB
/
fyfsp.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
#include "config.h"
#include <X11/Xlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "global-types.h"
#include "window.h"
#include "xquery.h"
#include "cache.h"
char *filter = NULL;
char verbose = 0;
void process_options(int argc, char **argv) {
int c;
while( (c=getopt(argc, argv, "hvf:")) != -1 ) {
switch(c) {
case 'h':
dprintf(1, "fyfsp [-h] [-v] [-f filter]\n");
dprintf(1, "\nFiltering is simple string matching against the window title and class. "
"Fancier filters are a todo item. If the filter string is in either the title "
"or in the class: go ahead and fyfsp the focus change, otherwise, ignore it.\n"
);
dprintf(1, "\n-v, -vv, -vvv all make fyfsp print decision info to stdout.\n");
exit(0);
case 'f':
filter = optarg;
break;
case 'v':
verbose += 1;
break;
}
}
}
int (*defaulthandler)();
int errorhandler(Display *display, XErrorEvent *error) {
// I tend to print the errors locally with more information
// (well, at least a hint about which call caused the problem)
//
// fprintf(stderr, "XerrorEvent(%d): \n", error->error_code);
// The default exits. I probably never want that to happen.
//
// if (error->error_code != BadWindow)
// (*defaulthandler)(display, error);
return 0;
}
void exit_gracefully(int signo) {
if (signo == SIGINT || signo == SIGTERM) {
printf("normal exit\n");
exit(0);
}
printf("unexpected signal\n");
exit(1);
}
void do_mainloop_checks(Display *display, Window has_rat, Window has_focus) {
#ifdef DEBUG_RECURSE_MOUSE_WINDOW_PROPERTIES
get_wm_type_recursively(display, has_rat);
get_wm_state_recursively(display, has_rat);
#endif
#ifdef DEBUG_RECURSE_FOCUS_WINDOW_PROPERTIES
get_wm_type_recursively(display, has_focus);
get_wm_state_recursively(display, has_focus);
#endif
if (test_rat_window_has_focus(0, display, has_rat, has_focus))
printf("has-focus: 0x%lx ∈ has-rat: 0x%lx\n", has_focus, has_rat);
else {
if (is_special_window(display, has_rat))
printf("has-focus: 0x%lx ∅ has-rat: 0x%lx\n", has_focus, has_rat);
else {
if( is_modal(display, has_focus) && same_group(display, has_focus, has_rat) )
printf("has-focus: 0x%lx « has-rat: 0x%lx\n", has_focus, has_rat);
else {
char *wm_name;
char *wm_class;
XFetchName(display, has_rat, &wm_name);
XFetchClass(display, has_rat, &wm_class);
printf("has-focus: 0x%lx ∉ has-rat: 0x%lx -- wm_name=\"%s\" wm_class=\"%s\"\n",
has_focus, has_rat, wm_name, wm_class);
int skip = 0;
if( filter ) {
int name_match = 0;
int class_match = 0;
printf("filter=\"%s\"", filter);
if(wm_name)
name_match = strstr(wm_name, filter) != NULL;
if(wm_class)
class_match = strstr(wm_class, filter) != NULL;
skip = !(name_match || class_match);
printf("filter skip=%d\n", skip);
}
#ifdef DEBUG
if( !skip )
fuck_window_manager(display, has_rat);
#else
if( !skip )
if( fuck_window_manager(display, has_rat) && verbose > 0 )
dprintf(1, "0x%lx \"%s\" \"%s\"\n",
has_rat, wm_name, wm_class);
#endif
XFree(wm_name);
XFree(wm_class);
}
}
}
}
int main(int argc, char **argv) {
process_options(argc,argv);
Display *display = XOpenDisplay(NULL);
XEvent event;
int num_screen,i;
#ifdef DEBUG
char *_changed_property_atom_name;
#endif
if (signal(SIGINT, exit_gracefully) == SIG_ERR)
perror("error setting SIGINT handler");
// exit(??) :- why? just explain that we can't catch it and ignore it
#ifdef DEBUG_LOG_TO_FILE
if( fclose(stdout) ) {
perror("couldn't close stdout for DEBUG_LOG_TO_FILE");
exit(1);
}
if( (stdout = fopen(DEBUG_LOG_TO_FILE, "a")) == NULL ) {
perror("couldn't open DEBUG_LOG_TO_FILE for write");
exit(1);
}
fprintf(stderr, "debugging output is being directed to %s\n", DEBUG_LOG_TO_FILE);
#endif
setlinebuf(stdout);
if (!display) {
perror("could not open display");
exit(1);
}
defaulthandler = XSetErrorHandler(errorhandler);
window_query_setup(display);
defaulthandler = XSetErrorHandler(errorhandler);
num_screen = ScreenCount(display);
for (i = 0; i < num_screen; i++)
XSelectInput(display, RootWindow(display, i), SubstructureNotifyMask);
Window has_rat = find_rat_window(display);
Window has_focus = find_focus_window(display);
#define xsi(window) XSelectInput(display, window, EnterWindowMask|LeaveWindowMask|FocusChangeMask)
xsi(has_rat);
xsi(has_focus);
#define find_rat_and_focus_and_handle(X) do { \
xsi(has_rat = find_rat_window(display)); \
xsi(has_focus = find_focus_window(display)); \
xsi(X); \
} while(0)
while (1) {
XNextEvent(display, &event);
switch (event.type) {
// Main Loop Events:
case MapNotify:
printf("XEvent(MapNotify) 0x%lx\n", event.xmap.window);
find_rat_and_focus_and_handle( event.xmap.window );
do_mainloop_checks(display, has_rat, has_focus);
break;
case FocusIn:
case FocusOut:
printf("XEvent(FocusOut) 0x%lx\n", event.xfocus.window);
find_rat_and_focus_and_handle( event.xfocus.window );
do_mainloop_checks(display, has_rat, has_focus);
break;
#ifdef TRACK_MOUSE_MOVEMENTS
case EnterNotify:
case LeaveNotify:
printf("XEvent(LeaveNotify) 0x%lx\n", event.xcrossing.window);
find_rat_and_focus_and_handle( event.xcrossing.window );
do_mainloop_checks(display, has_rat, has_focus);
break;
case MotionNotify:
// we don't actually ask for or receive this, but we might some day
printf("XEvent(MotionNotify) 0x%lx\n", event.xmotion.window);
find_rat_and_focus_and_handle( event.xmotion.window );
do_mainloop_checks(display, has_rat, has_focus);
break;
#endif
// Cache Events:
case PropertyNotify: // ← must set PropertyChangeMask set in cache-add functions
#ifdef DEBUG
_changed_property_atom_name = XGetAtomName(display, event.xproperty.atom);
printf("XEvent(PropertyChangeNotify) 0x%lx %s\n",
event.xproperty.window, _changed_property_atom_name);
if( _changed_property_atom_name != NULL )
XFree(_changed_property_atom_name);
#endif
if( is_this_atom(XPSI_WM_STATE, event.xproperty.atom) )
cache_kill_window_state_related(display, event.xproperty.window);
break;
case ReparentNotify: // (SubstructureMask)
printf("XEvent(ReparentNotify) 0x%lx\n", event.xreparent.window);
cache_kill_window(display, event.xreparent.window);
break;
case UnmapNotify: // (SubstructureMask)
printf("XEvent(UnmapNotify) 0x%lx\n", event.xunmap.window);
cache_kill_window(display, event.xunmap.window);
break;
}
}
XCloseDisplay(display);
return 0;
}
#ifdef DEBUG_LOG_TO_FILE
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#define TIME_STRING_BUFFER 25
char buffer[TIME_STRING_BUFFER];
char *current_time_string() {
time_t curtime = time(NULL);
struct tm *loctime = localtime(&curtime);
snprintf(buffer, 7, "%-5d ", getpid());
strftime(buffer + 6, TIME_STRING_BUFFER, "%Y-%m-%d %H:%M:%S", loctime);
return buffer;
}
#endif