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
|
/* Wncklet applet Wayland backend */
/*
* Copyright (C) 2019 William Wold
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <config.h>
#ifndef HAVE_WAYLAND
#error file should only be compiled when HAVE_WAYLAND is enabled
#endif
#include <gdk/gdkwayland.h>
#include "wayland-backend.h"
#include "wayland-protocol/wlr-foreign-toplevel-management-unstable-v1-client.h"
static const int window_button_width = 140;
typedef struct
{
GtkWidget *menu;
GtkWidget *maximize;
GtkWidget *minimize;
GtkWidget *on_top;
GtkWidget *close;
} ContextMenu;
typedef struct
{
GtkWidget *list;
GtkWidget *outer_box;
ContextMenu *context_menu;
struct zwlr_foreign_toplevel_manager_v1 *manager;
} TasklistManager;
typedef struct
{
GtkWidget *button;
GtkWidget *label;
struct zwlr_foreign_toplevel_handle_v1 *toplevel;
gboolean active;
gboolean maximized;
gboolean minimized;
gboolean fullscreen;
} ToplevelTask;
static const char *tasklist_manager_key = "tasklist_manager";
static const char *toplevel_task_key = "toplevel_task";
static gboolean has_initialized = FALSE;
static struct wl_registry *wl_registry_global = NULL;
static uint32_t foreign_toplevel_manager_global_id = 0;
static uint32_t foreign_toplevel_manager_global_version = 0;
static ToplevelTask *toplevel_task_new (TasklistManager *tasklist, struct zwlr_foreign_toplevel_handle_v1 *handle);
static void
wl_registry_handle_global (void *_data,
struct wl_registry *registry,
uint32_t id,
const char *interface,
uint32_t version)
{
/* pull out needed globals */
if (strcmp (interface, zwlr_foreign_toplevel_manager_v1_interface.name) == 0)
{
g_warn_if_fail (zwlr_foreign_toplevel_manager_v1_interface.version == 2);
foreign_toplevel_manager_global_id = id;
foreign_toplevel_manager_global_version =
MIN((uint32_t)zwlr_foreign_toplevel_manager_v1_interface.version, version);
}
}
static void
wl_registry_handle_global_remove (void *_data,
struct wl_registry *_registry,
uint32_t id)
{
if (id == foreign_toplevel_manager_global_id)
{
foreign_toplevel_manager_global_id = 0;
}
}
static const struct wl_registry_listener wl_registry_listener = {
.global = wl_registry_handle_global,
.global_remove = wl_registry_handle_global_remove,
};
static void
wayland_tasklist_init_if_needed (void)
{
if (has_initialized)
return;
GdkDisplay *gdk_display = gdk_display_get_default ();
g_return_if_fail (gdk_display);
g_return_if_fail (GDK_IS_WAYLAND_DISPLAY (gdk_display));
struct wl_display *wl_display = gdk_wayland_display_get_wl_display (gdk_display);
wl_registry_global = wl_display_get_registry (wl_display);
wl_registry_add_listener (wl_registry_global, &wl_registry_listener, NULL);
wl_display_roundtrip (wl_display);
if (!foreign_toplevel_manager_global_id)
g_warning ("%s not supported by Wayland compositor",
zwlr_foreign_toplevel_manager_v1_interface.name);
has_initialized = TRUE;
}
static void
foreign_toplevel_manager_handle_toplevel (void *data,
struct zwlr_foreign_toplevel_manager_v1 *manager,
struct zwlr_foreign_toplevel_handle_v1 *toplevel)
{
TasklistManager *tasklist = data;
ToplevelTask *task = toplevel_task_new (tasklist, toplevel);
gtk_box_pack_start (GTK_BOX (tasklist->list), task->button, TRUE, TRUE, 2);
}
static void
foreign_toplevel_manager_handle_finished (void *data,
struct zwlr_foreign_toplevel_manager_v1 *manager)
{
TasklistManager *tasklist = data;
tasklist->manager = NULL;
zwlr_foreign_toplevel_manager_v1_destroy (manager);
if (tasklist->outer_box)
g_object_set_data (G_OBJECT (tasklist->outer_box),
tasklist_manager_key,
NULL);
g_free (tasklist);
}
static const struct zwlr_foreign_toplevel_manager_v1_listener foreign_toplevel_manager_listener = {
.toplevel = foreign_toplevel_manager_handle_toplevel,
.finished = foreign_toplevel_manager_handle_finished,
};
static void
tasklist_manager_disconnected_from_widget (TasklistManager *tasklist)
{
if (tasklist->list)
{
GList *children = gtk_container_get_children (GTK_CONTAINER (tasklist->list));
for (GList *iter = children; iter != NULL; iter = g_list_next (iter))
gtk_widget_destroy (GTK_WIDGET (iter->data));
g_list_free(children);
tasklist->list = NULL;
}
if (tasklist->outer_box)
tasklist->outer_box = NULL;
if (tasklist->manager)
zwlr_foreign_toplevel_manager_v1_stop (tasklist->manager);
if (tasklist->context_menu)
{
gtk_widget_destroy (tasklist->context_menu->menu);
g_free (tasklist->context_menu);
tasklist->context_menu = NULL;
}
}
static void
menu_on_maximize (GtkMenuItem *item, gpointer user_data)
{
ToplevelTask *task = g_object_get_data (G_OBJECT (item), toplevel_task_key);
if (task->toplevel) {
if (task->maximized) {
zwlr_foreign_toplevel_handle_v1_unset_maximized (task->toplevel);
} else {
zwlr_foreign_toplevel_handle_v1_set_maximized (task->toplevel);
}
}
}
static void
menu_on_minimize (GtkMenuItem *item, gpointer user_data)
{
ToplevelTask *task = g_object_get_data (G_OBJECT (item), toplevel_task_key);
if (task->toplevel) {
if (task->minimized) {
zwlr_foreign_toplevel_handle_v1_unset_minimized (task->toplevel);
} else {
zwlr_foreign_toplevel_handle_v1_set_minimized (task->toplevel);
}
}
}
static void
menu_on_close (GtkMenuItem *item, gpointer user_data)
{
ToplevelTask *task = g_object_get_data (G_OBJECT (item), toplevel_task_key);
if (task->toplevel) {
zwlr_foreign_toplevel_handle_v1_close (task->toplevel);
}
}
static ContextMenu *
context_menu_new ()
{
ContextMenu *menu = g_new0 (ContextMenu, 1);
menu->menu = gtk_menu_new ();
menu->maximize = gtk_menu_item_new ();
menu->minimize = gtk_menu_item_new ();
menu->on_top = gtk_check_menu_item_new_with_label ("Always On Top");
menu->close = gtk_menu_item_new_with_label ("Close");
gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), menu->maximize);
gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), menu->minimize);
gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), gtk_separator_menu_item_new ());
gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), menu->on_top);
gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), gtk_separator_menu_item_new ());
gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), menu->close);
gtk_widget_show_all (menu->menu);
g_signal_connect (menu->maximize, "activate", G_CALLBACK (menu_on_maximize), NULL);
g_signal_connect (menu->minimize, "activate", G_CALLBACK (menu_on_minimize), NULL);
g_signal_connect (menu->close, "activate", G_CALLBACK (menu_on_close), NULL);
gtk_widget_set_sensitive (menu->on_top, FALSE);
return menu;
}
static TasklistManager *
tasklist_manager_new (void)
{
if (!foreign_toplevel_manager_global_id)
return NULL;
TasklistManager *tasklist = g_new0 (TasklistManager, 1);
tasklist->list = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
gtk_box_set_homogeneous (GTK_BOX (tasklist->list), TRUE);
tasklist->outer_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start (GTK_BOX (tasklist->outer_box), tasklist->list, FALSE, FALSE, 0);
gtk_widget_show (tasklist->list);
tasklist->manager = wl_registry_bind (wl_registry_global,
foreign_toplevel_manager_global_id,
&zwlr_foreign_toplevel_manager_v1_interface,
foreign_toplevel_manager_global_version);
zwlr_foreign_toplevel_manager_v1_add_listener (tasklist->manager,
&foreign_toplevel_manager_listener,
tasklist);
g_object_set_data_full (G_OBJECT (tasklist->outer_box),
tasklist_manager_key,
tasklist,
(GDestroyNotify)tasklist_manager_disconnected_from_widget);
tasklist->context_menu = context_menu_new ();
return tasklist;
}
static void
foreign_toplevel_handle_title (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel,
const char *title)
{
ToplevelTask *task = data;
if (task->label)
{
gtk_label_set_label (GTK_LABEL (task->label), title);
}
}
static void
foreign_toplevel_handle_app_id (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel,
const char *app_id)
{
/* ignore */
}
static void
foreign_toplevel_handle_output_enter (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel,
struct wl_output *output)
{
/* ignore */
}
static void
foreign_toplevel_handle_output_leave (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel,
struct wl_output *output)
{
/* ignore */
}
static void
foreign_toplevel_handle_state (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel,
struct wl_array *state)
{
ToplevelTask *task = data;
task->active = FALSE;
task->maximized = FALSE;
task->minimized = FALSE;
task->fullscreen = FALSE;
enum zwlr_foreign_toplevel_handle_v1_state *i;
wl_array_for_each (i, state)
{
switch (*i)
{
case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED:
task->active = TRUE;
break;
case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED:
task->maximized = TRUE;
break;
case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED:
task->minimized = TRUE;
break;
case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN:
task->fullscreen = TRUE;
break;
default:
break;
}
}
gtk_button_set_relief (GTK_BUTTON (task->button), task->active ? GTK_RELIEF_NORMAL : GTK_RELIEF_NONE);
}
static void
foreign_toplevel_handle_done (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel)
{
/* ignore */
}
static void
foreign_toplevel_handle_closed (void *data,
struct zwlr_foreign_toplevel_handle_v1 *toplevel)
{
ToplevelTask *task = data;
if (task->button)
gtk_widget_destroy (task->button);
}
static const struct zwlr_foreign_toplevel_handle_v1_listener foreign_toplevel_handle_listener = {
.title = foreign_toplevel_handle_title,
.app_id = foreign_toplevel_handle_app_id,
.output_enter = foreign_toplevel_handle_output_enter,
.output_leave = foreign_toplevel_handle_output_leave,
.state = foreign_toplevel_handle_state,
.done = foreign_toplevel_handle_done,
.closed = foreign_toplevel_handle_closed,
};
static void
toplevel_task_disconnected_from_widget (ToplevelTask *task)
{
struct zwlr_foreign_toplevel_handle_v1 *toplevel = task->toplevel;
task->button = NULL;
task->label = NULL;
task->toplevel = NULL;
if (toplevel)
zwlr_foreign_toplevel_handle_v1_destroy (toplevel);
g_free (task);
}
static void
toplevel_task_handle_clicked (GtkButton *button, ToplevelTask *task)
{
if (task->toplevel)
{
if (task->active)
{
zwlr_foreign_toplevel_handle_v1_set_minimized (task->toplevel);
}
else
{
GdkDisplay *gdk_display = gtk_widget_get_display (GTK_WIDGET (button));
GdkSeat *gdk_seat = gdk_display_get_default_seat (gdk_display);
struct wl_seat *wl_seat = gdk_wayland_seat_get_wl_seat (gdk_seat);
zwlr_foreign_toplevel_handle_v1_activate (task->toplevel, wl_seat);
}
}
}
static gboolean on_toplevel_button_press (GtkWidget *button, GdkEvent *event, TasklistManager *tasklist)
{
/* Assume event is a button press */
if (((GdkEventButton*)event)->button == GDK_BUTTON_SECONDARY)
{
ContextMenu *menu = tasklist->context_menu;
ToplevelTask *task = g_object_get_data (G_OBJECT (button), toplevel_task_key);
g_object_set_data (G_OBJECT (menu->maximize), toplevel_task_key, task);
g_object_set_data (G_OBJECT (menu->minimize), toplevel_task_key, task);
g_object_set_data (G_OBJECT (menu->close), toplevel_task_key, task);
gtk_menu_item_set_label (GTK_MENU_ITEM (menu->minimize),
task->minimized ? "Unminimize" : "Minimize");
gtk_menu_item_set_label (GTK_MENU_ITEM (menu->maximize),
task->maximized ? "Unmaximize" : "Maximize");
gtk_menu_popup_at_widget (GTK_MENU (menu->menu), button,
GDK_GRAVITY_NORTH_WEST, GDK_GRAVITY_SOUTH_WEST, event);
return TRUE;
}
else
{
return FALSE;
}
}
static ToplevelTask *
toplevel_task_new (TasklistManager *tasklist, struct zwlr_foreign_toplevel_handle_v1 *toplevel)
{
ToplevelTask *task = g_new0 (ToplevelTask, 1);
task->button = gtk_button_new ();
g_signal_connect (task->button, "clicked", G_CALLBACK (toplevel_task_handle_clicked), task);
task->label = gtk_label_new ("");
gtk_label_set_max_width_chars (GTK_LABEL (task->label), 1);
gtk_widget_set_size_request (task->label, window_button_width, -1);
gtk_label_set_ellipsize (GTK_LABEL (task->label), PANGO_ELLIPSIZE_END);
gtk_container_add (GTK_CONTAINER(task->button), task->label);
gtk_widget_show_all (task->button);
task->toplevel = toplevel;
zwlr_foreign_toplevel_handle_v1_add_listener (toplevel,
&foreign_toplevel_handle_listener,
task);
g_object_set_data_full (G_OBJECT (task->button),
toplevel_task_key,
task,
(GDestroyNotify)toplevel_task_disconnected_from_widget);
g_signal_connect (task->button, "button-press-event",
G_CALLBACK (on_toplevel_button_press),
tasklist);
return task;
}
GtkWidget*
wayland_tasklist_new ()
{
wayland_tasklist_init_if_needed ();
TasklistManager *tasklist = tasklist_manager_new ();
if (!tasklist)
return gtk_label_new ("Shell does not support WLR Foreign Toplevel Control");
return tasklist->outer_box;
}
static TasklistManager *
tasklist_widget_get_tasklist (GtkWidget* tasklist_widget)
{
return g_object_get_data (G_OBJECT (tasklist_widget), tasklist_manager_key);
}
void
wayland_tasklist_set_orientation (GtkWidget* tasklist_widget, GtkOrientation orient)
{
TasklistManager *tasklist = tasklist_widget_get_tasklist (tasklist_widget);
g_return_if_fail(tasklist);
gtk_orientable_set_orientation (GTK_ORIENTABLE (tasklist->list), orient);
gtk_orientable_set_orientation (GTK_ORIENTABLE (tasklist->outer_box), orient);
}
|