-
Notifications
You must be signed in to change notification settings - Fork 0
/
old.c
493 lines (391 loc) · 13.9 KB
/
old.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Author: Michael Proctor-Smith <[email protected]>
*
* This program is distributed under the terms of the GNU General Public License.
* For more info see http://www.gnu.org/licenses/gpl.txt.
*/
/*
* COMPILE
*
* gcc -O2 -march=native -pipe follow-sound.c \
* -o follow-sound $(pkg-config --libs xcb xcb-icccm)
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xproto.h>
typedef struct xcb_config_ignore_list_t {
unsigned int length;
char* name;
struct xcb_config_ignore_list_t* next;
} xcb_config_ignore_list_t;
typedef struct xcb_atom_list_t {
xcb_intern_atom_cookie_t cookie;
xcb_atom_t atom;
struct xcb_atom_list_t* next;
} xcb_atom_list_t;
typedef struct xcb_config_t {
int error;
int screen_no;
float opacity;
xcb_connection_t* connection;
xcb_screen_t* screen;
xcb_window_t window;
xcb_atom_list_t* list;
xcb_config_ignore_list_t* ignore;
} xcb_config_t;
/* signal handler data */
static xcb_config_t* global = NULL;
int xcb_config_run(int argc, char* argv[]);
void xcb_config_parse(xcb_config_t* config, int argc, char* argv[]);
int xcb_config_error(xcb_config_t* config);
void xcb_config_command(char const* command, char retval[]);
int xcb_config_init(xcb_config_t* config);
void xcb_config_uninit(xcb_config_t* config);
int xcb_config_valid_window(xcb_config_t* config, xcb_window_t window);
void xcb_config_find_parent(xcb_config_t* config, xcb_window_t* child);
void xcb_config_set_atom(xcb_config_t* config, char const* atom);
void xcb_config_set_error(xcb_config_t* config, int error);
void xcb_config_set_event_mask(xcb_config_t* config,
uint16_t mask, uint32_t* values);
void xcb_config_event_loop(xcb_config_t* config);
int xcb_config_event_property_notify(xcb_config_t* config,
xcb_generic_event_t* event);
void xcb_config_event_property_update(xcb_config_t* config);
void register_signal_handlers();
static void signal_handler(int signal);
int main(int argc, char* argv[])
{
return xcb_config_run(argc, argv);
}
int xcb_config_run(int argc, char* argv[])
{
const unsigned int size = 1;
uint16_t mask;
uint32_t values[size];
xcb_config_t config;
register_signal_handlers();
int error = xcb_config_init(&config);
if (! error)
{
xcb_config_parse(&config, argc, argv);
mask = XCB_CW_EVENT_MASK;
values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_config_set_event_mask(&config, mask, values);
xcb_config_set_atom(&config, "_NET_CURRENT_DESKTOP");
xcb_config_set_atom(&config, "_NET_ACTIVE_WINDOW");
xcb_config_event_loop(&config);
}
xcb_config_uninit(&config);
return error;
}
void xcb_config_parse(xcb_config_t* config, int argc, char* argv[])
{
int i;
float opacity;
xcb_config_ignore_list_t* ignore;
if (argc > 1)
{
sscanf(argv[1], "%5f", &opacity);
if ((0.0f > opacity) || (opacity > .999f))
opacity = .999f;
}
else
opacity = 0.75f;
config->opacity = opacity;
if (argc > 2)
{
for(i = 2; i < argc; i++)
{
ignore = (xcb_config_ignore_list_t*)
malloc(sizeof(xcb_config_ignore_list_t));
/* evil? feel free to improve! :) */
ignore->length = sizeof(argv[i]);
ignore->name = argv[i];
ignore->next = config->ignore;
config->ignore = ignore;
}
}
}
int xcb_config_error(xcb_config_t* config)
{
return config->error;
}
fprintf (stream, "%d\n", i);
if (ferror (stream))
{
fprintf (stderr, "Output to stream failed.\n");
exit (EXIT_FAILURE);
}
}
void unpatch()
{
char unpatchcmd[150];
xcb_config_command("port=$(jack_lsp Meters:meter_12 -c | tail -1); jack_disconnect $port Meters:meter_12; jack_disconnect $port Meters:meter_13", unpatchcmd);
// xcb_config_command("jack_disconnect $(jack_lsp Meters:meter_13 -c | tail -1) Meters:meter_13", unpatchcmd);
}
void patch(char* pid)
{
char patchcmd[250], retval[100];
int len = strlen(pid);
pid[len-1] = '\0';
snprintf(patchcmd, 250, "port=$(jack_lsp -c %s | tail -1 | sed \"s/_/ /g\" | awk '{ print $NF }') ; jack_connect Meters:monitor_$port Meters:meter_12 ; jack_connect Meters:monitor_$port Meters:meter_13", pid);
printf("cmd=[%s]\n", patchcmd);
xcb_config_command(patchcmd, retval);
}
void xcb_config_command(char const* command, char retval[])
{
FILE *output;
int tmp = 0;
output = popen (command, "r");
if (!output)
{
fprintf (stderr,
"incorrect parameters or too many files.\n");
return;
}
fgets(retval, 100, output);
if (pclose (output) != 0)
{
fprintf (stderr,
"Could not run more or other error.\n");
}
return;
}
int xcb_config_init(xcb_config_t* config)
{
int i;
config->connection = xcb_connect(NULL, &config->screen_no);
xcb_config_set_error(config, xcb_connection_has_error(config->connection));
config->opacity = .999f;
config->screen = NULL;
config->window = XCB_WINDOW_NONE;
config->list = NULL;
config->ignore = NULL;
if (! xcb_config_error(config))
{
/* register as global for signal handler */
global = config;
xcb_setup_t const* setup = xcb_get_setup(config->connection);
xcb_screen_iterator_t iterator = xcb_setup_roots_iterator(setup);
for(i = 0; i < config->screen_no; i++)
xcb_screen_next(&iterator);
config->screen = iterator.data;
if (! config->screen)
xcb_config_set_error(config, 1);
}
return xcb_config_error(config);
}
void xcb_config_uninit(xcb_config_t* config)
{
xcb_atom_list_t* list;
xcb_config_ignore_list_t* ignore;
while(config->list)
{
list = config->list;
config->list = list->next;
free(list);
}
while(config->ignore)
{
ignore = config->ignore;
config->ignore = ignore->next;
free(ignore);
}
if (! xcb_connection_has_error(config->connection))
{
xcb_disconnect(config->connection);
}
}
int xcb_config_valid_window(xcb_config_t* config, xcb_window_t window)
{
xcb_get_geometry_cookie_t cookie =
xcb_get_geometry_unchecked(config->connection, window);
xcb_get_geometry_reply_t* reply =
xcb_get_geometry_reply(config->connection, cookie, NULL);
if (reply)
free(reply);
else
return 0;
return 1;
}
void xcb_config_find_parent(xcb_config_t* config, xcb_window_t* child)
{
const uint32_t class = 0xffff0000;
uint32_t class_child, class_parent;
xcb_window_t parent;
xcb_query_tree_cookie_t cookie =
xcb_query_tree_unchecked(config->connection, *child);
xcb_query_tree_reply_t* reply =
xcb_query_tree_reply(config->connection, cookie, NULL);
while(reply)
{
parent = reply->parent;
free(reply);
class_child = *child & class;
class_parent = parent & class;
/* not proper but simple :) */
if (class_child == class_parent)
*child = parent;
else
break;
cookie = xcb_query_tree_unchecked(config->connection, *child);
reply = xcb_query_tree_reply(config->connection, cookie, NULL);
}
}
void xcb_config_set_atom(xcb_config_t* config, char const* atom)
{
xcb_atom_list_t* list;
xcb_atom_list_t* iterator;
xcb_intern_atom_reply_t* reply;
list = (xcb_atom_list_t*)malloc(sizeof(xcb_atom_list_t));
list->cookie =
xcb_intern_atom_unchecked(config->connection, 1, strlen(atom), atom);
reply = xcb_intern_atom_reply(config->connection, list->cookie, NULL);
if (reply)
{
list->atom = reply->atom;
list->next = config->list;
config->list = list;
xcb_get_property_cookie_t cookie_property =
xcb_get_property_unchecked(config->connection, 0,
config->screen->root, list->atom,
XCB_ATOM_WINDOW, 0, UINT32_MAX);
xcb_get_property_reply_t* reply_property =
xcb_get_property_reply(config->connection, cookie_property, NULL);
if (reply_property)
{
xcb_window_t* window = xcb_get_property_value(reply_property);
config->window = *window;
free(reply_property);
}
free(reply);
}
else
free(list);
}
void xcb_config_set_error(xcb_config_t* config, int error)
{
config->error = error;
}
void xcb_config_set_event_mask(xcb_config_t* config,
uint16_t mask, uint32_t* values)
{
xcb_change_window_attributes(config->connection,
config->screen->root, mask, values);
}
void xcb_config_event_loop(xcb_config_t* config)
{
int done = 0;
xcb_generic_event_t* event;
while(event = xcb_wait_for_event(config->connection))
{
if (xcb_connection_has_error(config->connection))
break;
switch(event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK)
{
case XCB_PROPERTY_NOTIFY:
done = xcb_config_event_property_notify(config, event);
break;
default:
break;
}
free(event);
event = NULL;
if (done)
break;
}
if (event)
free(event);
}
int xcb_config_event_property_notify(xcb_config_t* config,
xcb_generic_event_t* event)
{
xcb_atom_list_t* list;
xcb_property_notify_event_t const* property =
(xcb_property_notify_event_t const*)event;
if (property->window == XCB_WINDOW_NONE)
return 1;
for(list = config->list; list; list = list->next)
{
if (property->atom == list->atom)
{
xcb_config_event_property_update(config);
break;
}
}
return 0;
}
void xcb_config_event_property_update(xcb_config_t* config)
{
float opacity;
xcb_config_ignore_list_t* ignore;
xcb_get_input_focus_cookie_t cookie =
xcb_get_input_focus_unchecked(config->connection);
xcb_get_input_focus_reply_t* reply =
xcb_get_input_focus_reply(config->connection, cookie, NULL);
if (reply)
{
xcb_get_property_cookie_t cookie_wm_class =
xcb_get_wm_class_unchecked(config->connection, reply->focus);
xcb_get_wm_class_reply_t reply_wm_class;
xcb_get_property_cookie_t cookie_wm_name;
xcb_get_text_property_reply_t wm_name_reply;
if (xcb_get_wm_class_reply(config->connection,
cookie_wm_class, &reply_wm_class, NULL))
xcb_get_wm_class_reply_wipe(&reply_wm_class);
else
/* focus is set to a child window */
xcb_config_find_parent(config, &reply->focus);
cookie_wm_class =
xcb_get_wm_class_unchecked(config->connection,
reply->focus);
if (xcb_get_wm_class_reply(config->connection,
cookie_wm_class, &reply_wm_class, NULL))
{
if (strlen(reply_wm_class.class_name) == 0)
config->window = reply->focus;
else
{
printf("Active Window class Name?[%s]\n", reply_wm_class.class_name);
if( strcmp(reply_wm_class.class_name, "Vlc") == 0 ){
char pid[100];
xcb_config_command("xdotool getactivewindow getwindowpid", pid);
printf("pid=[%s]\n", pid);
unpatch();
patch(pid);
}
}
xcb_get_wm_class_reply_wipe(&reply_wm_class);
}
config->window = reply->focus;
free(reply);
}
}
void register_signal_handlers()
{
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
}
void signal_handler(int signal)
{
xcb_connection_t* connection;
xcb_window_t window;
if (global)
{
connection = global->connection;
window = global->screen->root;
global = NULL;
xcb_property_notify_event_t* event = (xcb_property_notify_event_t*)
malloc(sizeof(xcb_property_notify_event_t));
memset(event, 0, sizeof(xcb_property_notify_event_t));
event->response_type = XCB_PROPERTY_NOTIFY;
xcb_send_event(connection, 0, window,
XCB_EVENT_MASK_PROPERTY_CHANGE, (char const*)event);
xcb_flush(connection);
}
}