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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
* Copyright (c) 2013 Tiago Vignatti
* Copyright (c) 2013-2014 Collabora Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkwayland.h>
#include "desktop-shell-client-protocol.h"
#include "shell-helper-client-protocol.h"
#include "maynard-resources.h"
#include "app-icon.h"
#include "clock.h"
#include "favorites.h"
#include "launcher.h"
#include "panel.h"
#include "vertical-clock.h"
extern char **environ; /* defined by libc */
#define DEFAULT_BACKGROUND "/usr/share/wallpapers/Hanami/contents/images/3872x2592.jpg"
struct element {
GtkWidget *window;
GdkPixbuf *pixbuf;
struct wl_surface *surface;
};
struct desktop {
struct wl_display *display;
struct wl_registry *registry;
struct desktop_shell *shell;
struct wl_output *output;
struct shell_helper *helper;
GdkDisplay *gdk_display;
struct element *background;
struct element *panel;
struct element *launcher_grid;
struct element *clock;
guint initial_panel_timeout_id;
guint hide_panel_idle_id;
gboolean grid_visible;
gboolean system_visible;
gboolean volume_visible;
};
static gboolean panel_window_enter_cb (GtkWidget *widget,
GdkEventCrossing *event, struct desktop *desktop);
static gboolean panel_window_leave_cb (GtkWidget *widget,
GdkEventCrossing *event, struct desktop *desktop);
static gboolean
connect_enter_leave_signals (gpointer data)
{
struct desktop *desktop = data;
GList *l;
g_signal_connect (desktop->panel->window, "enter-notify-event",
G_CALLBACK (panel_window_enter_cb), desktop);
g_signal_connect (desktop->panel->window, "leave-notify-event",
G_CALLBACK (panel_window_leave_cb), desktop);
g_signal_connect (desktop->clock->window, "enter-notify-event",
G_CALLBACK (panel_window_enter_cb), desktop);
g_signal_connect (desktop->clock->window, "leave-notify-event",
G_CALLBACK (panel_window_leave_cb), desktop);
g_signal_connect (desktop->launcher_grid->window, "enter-notify-event",
G_CALLBACK (panel_window_enter_cb), desktop);
g_signal_connect (desktop->launcher_grid->window, "leave-notify-event",
G_CALLBACK (panel_window_leave_cb), desktop);
return G_SOURCE_REMOVE;
}
static void
desktop_shell_configure (void *data,
struct desktop_shell *desktop_shell,
uint32_t edges,
struct wl_surface *surface,
int32_t width, int32_t height)
{
struct desktop *desktop = data;
int window_height;
int grid_width, grid_height;
gtk_widget_set_size_request (desktop->background->window,
width, height);
/* TODO: make this height a little nicer */
window_height = height * MAYNARD_PANEL_HEIGHT_RATIO;
gtk_window_resize (GTK_WINDOW (desktop->panel->window),
MAYNARD_PANEL_WIDTH, window_height);
maynard_launcher_calculate (MAYNARD_LAUNCHER (desktop->launcher_grid->window),
&grid_width, &grid_height, NULL);
gtk_widget_set_size_request (desktop->launcher_grid->window,
grid_width, grid_height);
shell_helper_move_surface (desktop->helper, desktop->panel->surface,
0, (height - window_height) / 2);
gtk_window_resize (GTK_WINDOW (desktop->clock->window),
MAYNARD_CLOCK_WIDTH, MAYNARD_CLOCK_HEIGHT);
shell_helper_move_surface (desktop->helper, desktop->clock->surface,
MAYNARD_PANEL_WIDTH, (height - window_height) / 2);
shell_helper_move_surface (desktop->helper,
desktop->launcher_grid->surface,
- grid_width,
((height - window_height) / 2) + MAYNARD_CLOCK_HEIGHT);
desktop_shell_desktop_ready (desktop->shell);
/* TODO: why does the panel signal leave on drawing for
* startup? we don't want to have to have this silly
* timeout. */
g_timeout_add_seconds (1, connect_enter_leave_signals, desktop);
}
static void
desktop_shell_prepare_lock_surface (void *data,
struct desktop_shell *desktop_shell)
{
desktop_shell_unlock (desktop_shell);
}
static void
desktop_shell_grab_cursor (void *data,
struct desktop_shell *desktop_shell,
uint32_t cursor)
{
}
static const struct desktop_shell_listener listener = {
desktop_shell_configure,
desktop_shell_prepare_lock_surface,
desktop_shell_grab_cursor
};
static void
launcher_grid_toggle (GtkWidget *widget,
struct desktop *desktop)
{
if (desktop->grid_visible)
{
shell_helper_slide_surface_back (desktop->helper,
desktop->launcher_grid->surface);
}
else
{
int width;
gtk_widget_get_size_request (desktop->launcher_grid->window,
&width, NULL);
shell_helper_slide_surface (desktop->helper,
desktop->launcher_grid->surface,
width + MAYNARD_PANEL_WIDTH, 0);
}
desktop->grid_visible = !desktop->grid_visible;
}
static void
launcher_grid_create (struct desktop *desktop)
{
struct element *launcher_grid;
GdkWindow *gdk_window;
launcher_grid = malloc (sizeof *launcher_grid);
memset (launcher_grid, 0, sizeof *launcher_grid);
launcher_grid->window = maynard_launcher_new (desktop->background->window);
gdk_window = gtk_widget_get_window (launcher_grid->window);
launcher_grid->surface = gdk_wayland_window_get_wl_surface (gdk_window);
gdk_wayland_window_set_use_custom_surface (gdk_window);
shell_helper_add_surface_to_layer (desktop->helper,
launcher_grid->surface,
desktop->panel->surface);
g_signal_connect (launcher_grid->window, "app-launched",
G_CALLBACK (launcher_grid_toggle), desktop);
gtk_widget_show_all (launcher_grid->window);
desktop->launcher_grid = launcher_grid;
}
static void
volume_changed_cb (MaynardClock *clock,
gdouble value,
const gchar *icon_name,
struct desktop *desktop)
{
maynard_panel_set_volume_icon_name (
MAYNARD_PANEL (desktop->panel->window), icon_name);
}
static GtkWidget *
clock_create (struct desktop *desktop)
{
struct element *clock;
GdkWindow *gdk_window;
clock = malloc (sizeof *clock);
memset (clock, 0, sizeof *clock);
clock->window = maynard_clock_new ();
g_signal_connect (clock->window, "volume-changed",
G_CALLBACK (volume_changed_cb), desktop);
gdk_window = gtk_widget_get_window (clock->window);
clock->surface = gdk_wayland_window_get_wl_surface (gdk_window);
gdk_wayland_window_set_use_custom_surface (gdk_window);
shell_helper_add_surface_to_layer (desktop->helper, clock->surface,
desktop->panel->surface);
gtk_widget_show_all (clock->window);
desktop->clock = clock;
}
static void
button_toggled_cb (struct desktop *desktop,
gboolean *visible,
gboolean *not_visible)
{
*visible = !*visible;
*not_visible = FALSE;
if (desktop->system_visible)
{
maynard_clock_show_section (MAYNARD_CLOCK (desktop->clock->window),
MAYNARD_CLOCK_SECTION_SYSTEM);
maynard_panel_show_previous (MAYNARD_PANEL (desktop->panel->window),
MAYNARD_PANEL_BUTTON_SYSTEM);
}
else if (desktop->volume_visible)
{
maynard_clock_show_section (MAYNARD_CLOCK (desktop->clock->window),
MAYNARD_CLOCK_SECTION_VOLUME);
maynard_panel_show_previous (MAYNARD_PANEL (desktop->panel->window),
MAYNARD_PANEL_BUTTON_VOLUME);
}
else
{
maynard_clock_show_section (MAYNARD_CLOCK (desktop->clock->window),
MAYNARD_CLOCK_SECTION_CLOCK);
maynard_panel_show_previous (MAYNARD_PANEL (desktop->panel->window),
MAYNARD_PANEL_BUTTON_NONE);
}
}
static void
system_toggled_cb (GtkWidget *widget,
struct desktop *desktop)
{
button_toggled_cb (desktop,
&desktop->system_visible,
&desktop->volume_visible);
}
static void
volume_toggled_cb (GtkWidget *widget,
struct desktop *desktop)
{
button_toggled_cb (desktop,
&desktop->volume_visible,
&desktop->system_visible);
}
static gboolean
panel_window_enter_cb (GtkWidget *widget,
GdkEventCrossing *event,
struct desktop *desktop)
{
if (desktop->initial_panel_timeout_id > 0)
{
g_source_remove (desktop->initial_panel_timeout_id);
desktop->initial_panel_timeout_id = 0;
}
if (desktop->hide_panel_idle_id > 0)
{
g_source_remove (desktop->hide_panel_idle_id);
desktop->hide_panel_idle_id = 0;
return;
}
shell_helper_slide_surface_back (desktop->helper,
desktop->panel->surface);
shell_helper_slide_surface_back (desktop->helper,
desktop->clock->surface);
maynard_panel_set_expand (MAYNARD_PANEL (desktop->panel->window), TRUE);
return FALSE;
}
static gboolean
leave_panel_idle_cb (gpointer data)
{
struct desktop *desktop = data;
gint width;
desktop->hide_panel_idle_id = 0;
gtk_window_get_size (GTK_WINDOW (desktop->clock->window),
&width, NULL);
shell_helper_slide_surface (desktop->helper,
desktop->panel->surface,
MAYNARD_VERTICAL_CLOCK_WIDTH - MAYNARD_PANEL_WIDTH, 0);
shell_helper_slide_surface (desktop->helper,
desktop->clock->surface,
MAYNARD_VERTICAL_CLOCK_WIDTH - MAYNARD_PANEL_WIDTH - width, 0);
maynard_panel_set_expand (MAYNARD_PANEL (desktop->panel->window), FALSE);
maynard_clock_show_section (MAYNARD_CLOCK (desktop->clock->window),
MAYNARD_CLOCK_SECTION_CLOCK);
maynard_panel_show_previous (MAYNARD_PANEL (desktop->panel->window),
MAYNARD_PANEL_BUTTON_NONE);
desktop->system_visible = FALSE;
desktop->volume_visible = FALSE;
return G_SOURCE_REMOVE;
}
static gboolean
panel_window_leave_cb (GtkWidget *widget,
GdkEventCrossing *event,
struct desktop *desktop)
{
if (desktop->initial_panel_timeout_id > 0)
{
g_source_remove (desktop->initial_panel_timeout_id);
desktop->initial_panel_timeout_id = 0;
}
if (desktop->hide_panel_idle_id > 0)
return;
if (desktop->grid_visible)
return;
desktop->hide_panel_idle_id = g_idle_add (leave_panel_idle_cb, desktop);
return FALSE;
}
static gboolean
panel_hide_timeout_cb (gpointer data)
{
struct desktop *desktop = data;
panel_window_leave_cb (NULL, NULL, desktop);
return G_SOURCE_REMOVE;
}
static void
favorite_launched_cb (MaynardPanel *panel,
struct desktop *desktop)
{
if (desktop->grid_visible)
launcher_grid_toggle (desktop->launcher_grid->window, desktop);
panel_window_leave_cb (NULL, NULL, desktop);
}
static void
panel_create (struct desktop *desktop)
{
struct element *panel;
GdkWindow *gdk_window;
panel = malloc (sizeof *panel);
memset (panel, 0, sizeof *panel);
panel->window = maynard_panel_new ();
g_signal_connect (panel->window, "app-menu-toggled",
G_CALLBACK (launcher_grid_toggle), desktop);
g_signal_connect (panel->window, "system-toggled",
G_CALLBACK (system_toggled_cb), desktop);
g_signal_connect (panel->window, "volume-toggled",
G_CALLBACK (volume_toggled_cb), desktop);
g_signal_connect (panel->window, "favorite-launched",
G_CALLBACK (favorite_launched_cb), desktop);
desktop->initial_panel_timeout_id =
g_timeout_add_seconds (2, panel_hide_timeout_cb, desktop);
/* set it up as the panel */
gdk_window = gtk_widget_get_window (panel->window);
gdk_wayland_window_set_use_custom_surface (gdk_window);
panel->surface = gdk_wayland_window_get_wl_surface (gdk_window);
desktop_shell_set_user_data (desktop->shell, desktop);
desktop_shell_set_panel (desktop->shell, desktop->output, panel->surface);
desktop_shell_set_panel_position (desktop->shell,
DESKTOP_SHELL_PANEL_POSITION_LEFT);
shell_helper_set_panel (desktop->helper, panel->surface);
gtk_widget_show_all (panel->window);
desktop->panel = panel;
}
/* Expose callback for the drawing area */
static gboolean
draw_cb (GtkWidget *widget,
cairo_t *cr,
gpointer data)
{
struct desktop *desktop = data;
gdk_cairo_set_source_pixbuf (cr, desktop->background->pixbuf, 0, 0);
cairo_paint (cr);
return TRUE;
}
/* Destroy handler for the window */
static void
destroy_cb (GObject *object,
gpointer data)
{
gtk_main_quit ();
}
static GdkPixbuf *
scale_background (GdkPixbuf *original_pixbuf)
{
/* Scale original_pixbuf so it mostly fits on the screen.
* If the aspect ratio is different than a bit on the right or on the
* bottom could be cropped out. */
GdkScreen *screen = gdk_screen_get_default ();
gint screen_width, screen_height;
gint original_width, original_height;
gint final_width, final_height;
gdouble ratio_horizontal, ratio_vertical, ratio;
screen_width = gdk_screen_get_width (screen);
screen_height = gdk_screen_get_height (screen);
original_width = gdk_pixbuf_get_width (original_pixbuf);
original_height = gdk_pixbuf_get_height (original_pixbuf);
ratio_horizontal = (double) screen_width / original_width;
ratio_vertical = (double) screen_height / original_height;
ratio = MAX (ratio_horizontal, ratio_vertical);
final_width = ceil (ratio * original_width);
final_height = ceil (ratio * original_height);
return gdk_pixbuf_scale_simple (original_pixbuf,
final_width, final_height, GDK_INTERP_BILINEAR);
}
static void
background_create (struct desktop *desktop)
{
GdkWindow *gdk_window;
struct element *background;
const gchar *filename;
GdkPixbuf *unscaled_background;
background = malloc (sizeof *background);
memset (background, 0, sizeof *background);
filename = g_getenv ("BACKGROUND");
if (filename == NULL)
filename = DEFAULT_BACKGROUND;
unscaled_background = gdk_pixbuf_new_from_file (filename, NULL);
if (!unscaled_background)
{
g_message ("Could not load background. "
"Do you have kdewallpapers installed?");
exit (EXIT_FAILURE);
}
background->pixbuf = scale_background (unscaled_background);
g_object_unref (unscaled_background);
background->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (background->window, "destroy",
G_CALLBACK (destroy_cb), NULL);
g_signal_connect (background->window, "draw",
G_CALLBACK (draw_cb), desktop);
gtk_window_set_title (GTK_WINDOW (background->window), "maynard");
gtk_window_set_decorated (GTK_WINDOW (background->window), FALSE);
gtk_widget_realize (background->window);
gdk_window = gtk_widget_get_window (background->window);
gdk_wayland_window_set_use_custom_surface (gdk_window);
background->surface = gdk_wayland_window_get_wl_surface (gdk_window);
desktop_shell_set_user_data (desktop->shell, desktop);
desktop_shell_set_background (desktop->shell, desktop->output,
background->surface);
desktop->background = background;
gtk_widget_show_all (background->window);
}
static void
css_setup (struct desktop *desktop)
{
GtkCssProvider *provider;
GFile *file;
GError *error = NULL;
provider = gtk_css_provider_new ();
file = g_file_new_for_uri ("resource:///org/raspberry-pi/maynard/style.css");
if (!gtk_css_provider_load_from_file (provider, file, &error))
{
g_warning ("Failed to load CSS file: %s", error->message);
g_clear_error (&error);
g_object_unref (file);
return;
}
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider), 600);
g_object_unref (file);
}
static void
registry_handle_global (void *data,
struct wl_registry *registry,
uint32_t name,
const char *interface,
uint32_t version)
{
struct desktop *d = data;
if (!strcmp (interface, "desktop_shell"))
{
d->shell = wl_registry_bind (registry, name,
&desktop_shell_interface, 3);
desktop_shell_add_listener (d->shell, &listener, d);
}
else if (!strcmp (interface, "wl_output"))
{
/* TODO: create multiple outputs */
d->output = wl_registry_bind (registry, name,
&wl_output_interface, 1);
}
else if (!strcmp (interface, "shell_helper"))
{
d->helper = wl_registry_bind (registry, name,
&shell_helper_interface, 1);
}
}
static void
registry_handle_global_remove (void *data,
struct wl_registry *registry,
uint32_t name)
{
}
static const struct wl_registry_listener registry_listener = {
registry_handle_global,
registry_handle_global_remove
};
int
main (int argc,
char *argv[])
{
struct desktop *desktop;
gdk_set_allowed_backends ("wayland");
gtk_init (&argc, &argv);
g_resources_register (maynard_get_resource ());
desktop = malloc (sizeof *desktop);
desktop->output = NULL;
desktop->shell = NULL;
desktop->helper = NULL;
desktop->gdk_display = gdk_display_get_default ();
desktop->display =
gdk_wayland_display_get_wl_display (desktop->gdk_display);
if (desktop->display == NULL)
{
fprintf (stderr, "failed to get display: %m\n");
return -1;
}
desktop->registry = wl_display_get_registry (desktop->display);
wl_registry_add_listener (desktop->registry,
®istry_listener, desktop);
/* Wait until we have been notified about the compositor,
* shell, and shell helper objects */
if (!desktop->output || !desktop->shell || !desktop->helper)
wl_display_roundtrip (desktop->display);
if (!desktop->output || !desktop->shell || !desktop->helper)
{
fprintf (stderr, "could not find output, shell or helper modules\n");
return -1;
}
desktop->grid_visible = FALSE;
desktop->system_visible = FALSE;
desktop->volume_visible = FALSE;
css_setup (desktop);
background_create (desktop);
/* panel needs to be first so the clock and launcher grid can
* be added to its layer */
panel_create (desktop);
clock_create (desktop);
launcher_grid_create (desktop);
gtk_main ();
/* TODO cleanup */
return EXIT_SUCCESS;
}
|