forked from baskerville/xtitle
/
xtitle.c
179 lines (165 loc) · 5.52 KB
/
xtitle.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <sys/select.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_ewmh.h>
#include "helpers.h"
#include "xtitle.h"
int main(int argc, char *argv[])
{
bool snoop = false;
char *format = NULL;
char opt;
while ((opt = getopt(argc, argv, "hvsf:")) != -1) {
switch (opt) {
case 'h':
printf("xtitle [-h|-v|-s|-f FORMAT] [WID ...]\n");
return EXIT_SUCCESS;
break;
case 'v':
printf("%s\n", VERSION);
return EXIT_SUCCESS;
break;
case 's':
snoop = true;
break;
case 'f':
format = optarg;
break;
}
}
int num = argc - optind;
char **args = argv + optind;
setup();
char title[MAXLEN] = {0};
if (num > 0) {
char *end;
for (int i = 0; i < num; i++) {
errno = 0;
long int wid = strtol(args[i], &end, 0);
if (errno != 0 || *end != '\0')
warn("Invalid window ID: '%s'.\n", args[i]);
else
output_title(wid, format, title, sizeof(title));
}
} else {
xcb_window_t win = XCB_NONE;
if (get_active_window(&win))
output_title(win, format, title, sizeof(title));
if (snoop) {
signal(SIGINT, hold);
signal(SIGHUP, hold);
signal(SIGTERM, hold);
watch(root, true);
watch(win, true);
xcb_window_t last_win = XCB_NONE;
fd_set descriptors;
int fd = xcb_get_file_descriptor(dpy);
running = true;
xcb_flush(dpy);
while (running) {
FD_ZERO(&descriptors);
FD_SET(fd, &descriptors);
if (select(fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
xcb_generic_event_t *evt;
while ((evt = xcb_poll_for_event(dpy)) != NULL) {
if (title_changed(evt, &win, &last_win))
output_title(win, format, title, sizeof(title));
free(evt);
}
}
if (xcb_connection_has_error(dpy)) {
warn("The server closed the connection.\n");
running = false;
}
}
}
}
xcb_ewmh_connection_wipe(ewmh);
free(ewmh);
xcb_disconnect(dpy);
return EXIT_SUCCESS;
}
void setup(void)
{
dpy = xcb_connect(NULL, &default_screen);
if (xcb_connection_has_error(dpy))
err("Can't open display.\n");
xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
if (screen == NULL)
err("Can't acquire screen.\n");
root = screen->root;
ewmh = malloc(sizeof(xcb_ewmh_connection_t));
if (xcb_ewmh_init_atoms_replies(ewmh, xcb_ewmh_init_atoms(dpy, ewmh), NULL) == 0)
err("Can't initialize EWMH atoms.\n");
}
void output_title(xcb_window_t win, char *format, char *title, size_t len)
{
get_window_title(win, title, len);
printf(format == NULL ? FORMAT : format, title);
printf("\n");
fflush(stdout);
}
bool title_changed(xcb_generic_event_t *evt, xcb_window_t *win, xcb_window_t *last_win)
{
if (XCB_EVENT_RESPONSE_TYPE(evt) == XCB_PROPERTY_NOTIFY) {
xcb_property_notify_event_t *pne = (xcb_property_notify_event_t *) evt;
if (pne->atom == ewmh->_NET_ACTIVE_WINDOW) {
watch(*last_win, false);
if (get_active_window(win)) {
watch(*win, true);
*last_win = *win;
} else {
*win = *last_win = XCB_NONE;
}
return true;
} else if (*win != XCB_NONE && pne->window == *win && (pne->atom == ewmh->_NET_WM_NAME || pne->atom == XCB_ATOM_WM_NAME)) {
return true;
}
}
return false;
}
void watch(xcb_window_t win, bool state)
{
if (win == XCB_NONE)
return;
uint32_t value = (state ? XCB_EVENT_MASK_PROPERTY_CHANGE : XCB_EVENT_MASK_NO_EVENT);
xcb_change_window_attributes(dpy, win, XCB_CW_EVENT_MASK, &value);
}
bool get_active_window(xcb_window_t *win)
{
return (xcb_ewmh_get_active_window_reply(ewmh, xcb_ewmh_get_active_window(ewmh, default_screen), win, NULL) == 1);
}
void get_window_title(xcb_window_t win, char *title, size_t len) {
xcb_ewmh_get_utf8_strings_reply_t ewmh_txt_prop;
xcb_icccm_get_text_property_reply_t icccm_txt_prop;
ewmh_txt_prop.strings = icccm_txt_prop.name = NULL;
title[0] = '\0';
if (win != XCB_NONE && (xcb_ewmh_get_wm_name_reply(ewmh, xcb_ewmh_get_wm_name(ewmh, win), &ewmh_txt_prop, NULL) == 1 || xcb_icccm_get_wm_name_reply(dpy, xcb_icccm_get_wm_name(dpy, win), &icccm_txt_prop, NULL) == 1)) {
char *src = NULL;
size_t title_len = 0;
if (ewmh_txt_prop.strings != NULL) {
src = ewmh_txt_prop.strings;
title_len = MIN(len, ewmh_txt_prop.strings_len);
} else if (icccm_txt_prop.name != NULL) {
src = icccm_txt_prop.name;
title_len = MIN(len, icccm_txt_prop.name_len);
}
if (src != NULL) {
strncpy(title, src, title_len);
title[title_len] = '\0';
}
}
}
void hold(int sig)
{
if (sig == SIGHUP || sig == SIGINT || sig == SIGTERM)
running = false;
}