forked from nsf/bmpanel2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget-desktops.c
412 lines (349 loc) · 10.9 KB
/
widget-desktops.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
#include "builtin-widgets.h"
static int create_widget_private(struct widget *w, struct config_format_entry *e,
struct config_format_tree *tree);
static void destroy_widget_private(struct widget *w);
static void draw(struct widget *w);
static void button_click(struct widget *w, XButtonEvent *e);
static void prop_change(struct widget *w, XPropertyEvent *e);
static void client_msg(struct widget *w, XClientMessageEvent *e);
static void dnd_drop(struct widget *w, struct drag_info *di);
static void mouse_motion(struct widget *w, XMotionEvent *e);
static void mouse_leave(struct widget *w);
struct widget_interface desktops_interface = {
.theme_name = "desktop_switcher",
.size_type = WIDGET_SIZE_CONSTANT,
.create_widget_private = create_widget_private,
.destroy_widget_private = destroy_widget_private,
.draw = draw,
.button_click = button_click,
.prop_change = prop_change,
.dnd_drop = dnd_drop,
.client_msg = client_msg,
.mouse_motion = mouse_motion,
.mouse_leave = mouse_leave
};
/**************************************************************************
Desktop switcher theme
**************************************************************************/
static int parse_desktops_state(struct desktops_state *ds, const char *name,
struct config_format_entry *e,
struct config_format_tree *tree,
int required)
{
struct config_format_entry *ee = find_config_format_entry(e, name);
if (!ee) {
if (required)
required_entry_not_found(e, name);
ds->exists = 0;
return -1;
}
if (parse_triple_image(&ds->background, ee, tree, 1))
return -1;
parse_text_info_named(&ds->font, "font", ee, 0);
ds->left_corner = parse_image_part_named("left_corner", ee, tree, 0);
ds->right_corner = parse_image_part_named("right_corner", ee, tree, 0);
ds->exists = 1;
return 0;
}
static void free_desktops_state(struct desktops_state *ds)
{
if (ds->exists) {
free_triple_image(&ds->background);
free_text_info(&ds->font);
if (ds->left_corner)
cairo_surface_destroy(ds->left_corner);
if (ds->right_corner)
cairo_surface_destroy(ds->right_corner);
}
}
static int parse_desktops_theme(struct desktops_theme *dt,
struct config_format_entry *e,
struct config_format_tree *tree)
{
if (parse_desktops_state(&dt->states[BUTTON_STATE_IDLE], "idle", e, tree, 1))
goto parse_desktops_state_error_idle;
if (parse_desktops_state(&dt->states[BUTTON_STATE_PRESSED], "pressed", e, tree, 1))
goto parse_desktops_state_error_pressed;
dt->separator = parse_image_part_named("separator", e, tree, 0);
parse_desktops_state(&dt->states[BUTTON_STATE_IDLE_HIGHLIGHT],
"idle_highlight", e, tree, 0);
parse_desktops_state(&dt->states[BUTTON_STATE_PRESSED_HIGHLIGHT],
"pressed_highlight", e, tree, 0);
return 0;
parse_desktops_state_error_pressed:
free_desktops_state(&dt->states[BUTTON_STATE_IDLE]);
parse_desktops_state_error_idle:
return -1;
}
static void free_desktops_theme(struct desktops_theme *dt)
{
unsigned int i;
for (i = 0; i < 4; ++i)
free_desktops_state(&dt->states[i]);
if (dt->separator)
cairo_surface_destroy(dt->separator);
}
/**************************************************************************
Desktops management
**************************************************************************/
static void free_desktops(struct desktops_widget *dw)
{
size_t i;
for (i = 0; i < dw->desktops_n; ++i)
xfree(dw->desktops[i].name);
CLEAR_ARRAY(dw->desktops);
}
static void update_active_desktop(struct desktops_widget *dw, struct x_connection *c)
{
dw->active = x_get_prop_int(c, c->root,
c->atoms[XATOM_NET_CURRENT_DESKTOP]);
}
static void switch_desktop(int desktop, struct x_connection *c)
{
int desktops = x_get_prop_int(c, c->root,
c->atoms[XATOM_NET_NUMBER_OF_DESKTOPS]);
if (desktop >= desktops)
return;
x_send_netwm_message(c, c->root, c->atoms[XATOM_NET_CURRENT_DESKTOP],
desktop, 0, 0, 0, 0);
}
static void update_desktops(struct desktops_widget *dw, struct x_connection *c)
{
free_desktops(dw);
update_active_desktop(dw, c);
int desktops_n = x_get_prop_int(c, c->root,
c->atoms[XATOM_NET_NUMBER_OF_DESKTOPS]);
char *name, *names;
size_t i;
names = name = x_get_prop_data(c, c->root,
c->atoms[XATOM_NET_DESKTOP_NAMES],
c->atoms[XATOM_UTF8_STRING], 0);
for (i = 0; i < desktops_n; ++i) {
struct desktops_desktop d = {0,0,0,0};
if (names) {
d.name = xstrdup(name);
name += strlen(name) + 1;
} else {
char buf[16];
snprintf(buf, sizeof(buf), "%zu", i+1);
d.name = xstrdup(buf);
}
ARRAY_APPEND(dw->desktops, d);
}
if (names)
XFree(names);
}
static void resize_desktops(struct widget *w)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
struct desktops_state *ds = &dw->theme.states[BUTTON_STATE_IDLE];
size_t i;
int x = 0;
int left_cornerw = image_width(ds->left_corner);
int leftw = image_width(ds->background.left);
int rightw = image_width(ds->background.right);
int right_cornerw = image_width(ds->right_corner);
int sepw = image_width(dw->theme.separator);
for (i = 0; i < dw->desktops_n; ++i) {
int basex = x;
if (i == 0)
x += left_cornerw;
else
x += leftw;
int maxwidth = 0;
if (ds->font.pfd) {
int j, width = 0;
for (j = 0; j < 4; ++j) {
struct desktops_state *lds = &dw->theme.states[j];
if (!lds->exists)
continue;
text_extents(w->panel->layout, lds->font.pfd,
dw->desktops[i].name, &width, 0);
if (width > maxwidth)
maxwidth = width;
}
}
dw->desktops[i].textw = maxwidth;
x += maxwidth;
if (i == dw->desktops_n - 1)
x += right_cornerw;
else
x += rightw + sepw;
dw->desktops[i].w = x - sepw - basex;
}
w->width = x;
}
static int get_desktop_at(struct widget *w, int x)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
size_t i;
for (i = 0; i < dw->desktops_n; ++i) {
struct desktops_desktop *d = &dw->desktops[i];
if (x < (d->x + d->w) && x > d->x)
return (int)i;
}
return -1;
}
/**************************************************************************
Desktop switcher interface
**************************************************************************/
static int create_widget_private(struct widget *w, struct config_format_entry *e,
struct config_format_tree *tree)
{
struct desktops_widget *dw = xmallocz(sizeof(struct desktops_widget));
if (parse_desktops_theme(&dw->theme, e, tree)) {
xfree(dw);
XWARNING("Failed to parse desktop switcher theme");
return -1;
}
INIT_ARRAY(dw->desktops, 16);
w->private = dw;
struct x_connection *c = &w->panel->connection;
update_desktops(dw, c);
resize_desktops(w);
dw->highlighted = -1;
return 0;
}
static void destroy_widget_private(struct widget *w)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
free_desktops_theme(&dw->theme);
free_desktops(dw);
FREE_ARRAY(dw->desktops);
xfree(dw);
}
static void draw(struct widget *w)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
struct desktops_state *idle = &dw->theme.states[BUTTON_STATE_IDLE];
cairo_t *cr = w->panel->cr;
size_t i;
int x = w->x;
int left_cornerw = image_width(idle->left_corner);
int leftw = image_width(idle->background.left);
int rightw = image_width(idle->background.right);
int right_cornerw = image_width(idle->right_corner);
int sepw = image_width(dw->theme.separator);
int h = w->panel->height;
for (i = 0; i < dw->desktops_n; ++i) {
int state = (i == dw->active) << 1;
int state_hl = ((i == dw->active) << 1) | (i == dw->highlighted);
struct desktops_state *cur;
if (dw->theme.states[state_hl].exists)
cur = &dw->theme.states[state_hl];
else
cur = &dw->theme.states[state];
dw->desktops[i].x = x;
/* TODO: There is a bug when left_corner and right_corner is abscent */
if (i == 0 && left_cornerw) {
blit_image(cur->left_corner, cr, x, 0);
x += left_cornerw;
} else if (leftw) {
blit_image(cur->background.left, cr, x, 0);
x += leftw;
}
int width = dw->desktops[i].textw;
pattern_image(cur->background.center, cr, x, 0, width, 1);
if (cur->font.pfd)
draw_text(cr, w->panel->layout, &cur->font,
dw->desktops[i].name, x, 0, width, h, 0);
x += width;
if (i == dw->desktops_n - 1 && right_cornerw) {
blit_image(cur->right_corner, cr, x, 0);
x += right_cornerw;
} else {
if (rightw) {
blit_image(cur->background.right, cr, x, 0);
x += rightw;
}
if (sepw) {
blit_image(dw->theme.separator, cr, x, 0);
x += sepw;
}
}
}
}
static void button_click(struct widget *w, XButtonEvent *e)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
int di = get_desktop_at(w, e->x);
if (di == -1)
return;
struct x_connection *c = &w->panel->connection;
int mbutton_use = check_mbutton_condition(w->panel, e->button, MBUTTON_USE);
if (mbutton_use && e->type == ButtonRelease && dw->active != di)
switch_desktop(di, c);
}
static void prop_change(struct widget *w, XPropertyEvent *e)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
struct x_connection *c = &w->panel->connection;
if (e->window == c->root) {
if (e->atom == c->atoms[XATOM_NET_NUMBER_OF_DESKTOPS] ||
e->atom == c->atoms[XATOM_NET_DESKTOP_NAMES])
{
update_desktops(dw, c);
resize_desktops(w);
recalculate_widgets_sizes(w->panel);
return;
}
if (e->atom == c->atoms[XATOM_NET_CURRENT_DESKTOP]) {
update_active_desktop(dw, c);
w->needs_expose = 1;
return;
}
}
}
static void client_msg(struct widget *w, XClientMessageEvent *e)
{
struct panel *p = w->panel;
struct x_connection *c = &p->connection;
struct desktops_widget *dw = (struct desktops_widget*)w->private;
if (e->message_type == c->atoms[XATOM_XDND_POSITION]) {
int x = (e->data.l[2] >> 16) & 0xFFFF;
/* if it's not ours, skip.. */
if ((x < (p->x + w->x)) || (x > (p->x + w->x + w->width)))
return;
int di = get_desktop_at(w, x - p->x);
if (di != -1 && di != dw->active)
switch_desktop(di, c);
x_send_dnd_message(c, e->data.l[0],
c->atoms[XATOM_XDND_STATUS],
p->win,
2, /* bits: 0 1 */
0, 0,
None);
}
}
static void dnd_drop(struct widget *w, struct drag_info *di)
{
if (di->taken_on->interface != &taskbar_interface)
return;
struct taskbar_widget *tw = di->taken_on->private;
if (tw->taken == None)
return;
int desktop = get_desktop_at(w, di->dropped_x);
if (desktop == -1)
return;
struct x_connection *c = &w->panel->connection;
x_send_netwm_message(c, tw->taken,
c->atoms[XATOM_NET_WM_DESKTOP],
(long)desktop, 2, 0, 0, 0);
}
static void mouse_motion(struct widget *w, XMotionEvent *e)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
int i = get_desktop_at(w, e->x);
if (i != dw->highlighted) {
dw->highlighted = i;
w->needs_expose = 1;
}
}
static void mouse_leave(struct widget *w)
{
struct desktops_widget *dw = (struct desktops_widget*)w->private;
if (dw->highlighted != -1) {
dw->highlighted = -1;
w->needs_expose = 1;
}
}