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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2006-2007 William Jon McCann <mccann@jhu.edu>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2, 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser 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"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "msd-media-keys-window.h"
#define MSD_MEDIA_KEYS_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MSD_TYPE_MEDIA_KEYS_WINDOW, MsdMediaKeysWindowPrivate))
#define ICON_SCALE 0.55 /* size of the icon compared to the whole OSD */
struct MsdMediaKeysWindowPrivate
{
MsdMediaKeysWindowAction action;
char *icon_name;
char *description;
guint volume_muted : 1;
int volume_level;
GtkImage *image;
GtkWidget *progress;
GtkWidget *label;
};
G_DEFINE_TYPE (MsdMediaKeysWindow, msd_media_keys_window, MSD_TYPE_OSD_WINDOW)
static void
volume_controls_set_visible (MsdMediaKeysWindow *window,
gboolean visible)
{
if (window->priv->progress == NULL)
return;
if (visible) {
gtk_widget_show (window->priv->progress);
} else {
gtk_widget_hide (window->priv->progress);
}
}
static void
description_label_set_visible (MsdMediaKeysWindow *window,
gboolean visible)
{
if (visible) {
gtk_label_set_text (GTK_LABEL (window->priv->label), window->priv->description);
gtk_widget_show (window->priv->label);
} else {
gtk_widget_hide (window->priv->label);
}
}
static void
window_set_icon_name (MsdMediaKeysWindow *window,
const char *name)
{
if (window->priv->image == NULL)
return;
gtk_image_set_from_icon_name (window->priv->image,
name, GTK_ICON_SIZE_DIALOG);
}
static void
action_changed (MsdMediaKeysWindow *window)
{
if (!msd_osd_window_is_composited (MSD_OSD_WINDOW (window))) {
switch (window->priv->action) {
case MSD_MEDIA_KEYS_WINDOW_ACTION_VOLUME:
volume_controls_set_visible (window, TRUE);
description_label_set_visible (window, FALSE);
if (window->priv->volume_muted) {
window_set_icon_name (window, "audio-volume-muted");
} else {
window_set_icon_name (window, "audio-volume-high");
}
break;
case MSD_MEDIA_KEYS_WINDOW_ACTION_CUSTOM:
volume_controls_set_visible (window, FALSE);
description_label_set_visible (window, TRUE);
window_set_icon_name (window, window->priv->icon_name);
break;
default:
g_assert_not_reached ();
break;
}
}
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
}
static void
volume_level_changed (MsdMediaKeysWindow *window)
{
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
if (!msd_osd_window_is_composited (MSD_OSD_WINDOW (window)) && window->priv->progress != NULL) {
double fraction;
fraction = (double) window->priv->volume_level / 100.0;
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (window->priv->progress),
fraction);
}
}
static void
volume_muted_changed (MsdMediaKeysWindow *window)
{
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
if (!msd_osd_window_is_composited (MSD_OSD_WINDOW (window))) {
if (window->priv->volume_muted) {
window_set_icon_name (window, "audio-volume-muted");
} else {
window_set_icon_name (window, "audio-volume-high");
}
}
}
void
msd_media_keys_window_set_action (MsdMediaKeysWindow *window,
MsdMediaKeysWindowAction action)
{
g_return_if_fail (MSD_IS_MEDIA_KEYS_WINDOW (window));
g_return_if_fail (action == MSD_MEDIA_KEYS_WINDOW_ACTION_VOLUME);
if (window->priv->action != action) {
window->priv->action = action;
action_changed (window);
} else {
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
}
}
void
msd_media_keys_window_set_action_custom (MsdMediaKeysWindow *window,
const char *icon_name,
const char *description)
{
g_return_if_fail (MSD_IS_MEDIA_KEYS_WINDOW (window));
g_return_if_fail (icon_name != NULL);
if (window->priv->action != MSD_MEDIA_KEYS_WINDOW_ACTION_CUSTOM ||
g_strcmp0 (window->priv->icon_name, icon_name) != 0 ||
g_strcmp0 (window->priv->description, description) != 0) {
window->priv->action = MSD_MEDIA_KEYS_WINDOW_ACTION_CUSTOM;
g_free (window->priv->icon_name);
window->priv->icon_name = g_strdup (icon_name);
g_free (window->priv->description);
window->priv->description = g_strdup (description);
action_changed (window);
} else {
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
}
}
void
msd_media_keys_window_set_volume_muted (MsdMediaKeysWindow *window,
gboolean muted)
{
g_return_if_fail (MSD_IS_MEDIA_KEYS_WINDOW (window));
if (window->priv->volume_muted != muted) {
window->priv->volume_muted = muted;
volume_muted_changed (window);
}
}
void
msd_media_keys_window_set_volume_level (MsdMediaKeysWindow *window,
int level)
{
g_return_if_fail (MSD_IS_MEDIA_KEYS_WINDOW (window));
if (window->priv->volume_level != level) {
window->priv->volume_level = level;
volume_level_changed (window);
}
}
static GdkPixbuf *
load_pixbuf (MsdMediaKeysWindow *window,
const char *name,
int icon_size)
{
GtkIconTheme *theme;
GdkPixbuf *pixbuf;
if (window != NULL && gtk_widget_has_screen (GTK_WIDGET (window))) {
theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (window)));
} else {
theme = gtk_icon_theme_get_default ();
}
pixbuf = gtk_icon_theme_load_icon (theme,
name,
icon_size,
GTK_ICON_LOOKUP_FORCE_SIZE,
NULL);
return pixbuf;
}
static void
draw_eject (cairo_t *cr,
double _x0,
double _y0,
double width,
double height)
{
int box_height;
int tri_height;
int separation;
box_height = height * 0.2;
separation = box_height / 3;
tri_height = height - box_height - separation;
cairo_rectangle (cr, _x0, _y0 + height - box_height, width, box_height);
cairo_move_to (cr, _x0, _y0 + tri_height);
cairo_rel_line_to (cr, width, 0);
cairo_rel_line_to (cr, -width / 2, -tri_height);
cairo_rel_line_to (cr, -width / 2, tri_height);
cairo_close_path (cr);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, MSD_OSD_WINDOW_FG_ALPHA);
cairo_fill_preserve (cr);
cairo_set_source_rgba (cr, 0.6, 0.6, 0.6, MSD_OSD_WINDOW_FG_ALPHA / 2);
cairo_set_line_width (cr, 2);
cairo_stroke (cr);
}
static void
draw_waves (cairo_t *cr,
double cx,
double cy,
double max_radius,
int volume_level)
{
const int n_waves = 3;
int last_wave;
int i;
last_wave = n_waves * volume_level / 100;
for (i = 0; i < n_waves; i++) {
double angle1;
double angle2;
double radius;
double alpha;
angle1 = -M_PI / 4;
angle2 = M_PI / 4;
if (i < last_wave)
alpha = 1.0;
else if (i > last_wave)
alpha = 0.1;
else alpha = 0.1 + 0.9 * (n_waves * volume_level % 100) / 100.0;
radius = (i + 1) * (max_radius / n_waves);
cairo_arc (cr, cx, cy, radius, angle1, angle2);
cairo_set_source_rgba (cr, 0.6, 0.6, 0.6, alpha / 2);
cairo_set_line_width (cr, 14);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
cairo_stroke_preserve (cr);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, alpha);
cairo_set_line_width (cr, 10);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
cairo_stroke (cr);
}
}
static void
draw_cross (cairo_t *cr,
double cx,
double cy,
double size)
{
cairo_move_to (cr, cx, cy - size/2.0);
cairo_rel_line_to (cr, size, size);
cairo_move_to (cr, cx, cy + size/2.0);
cairo_rel_line_to (cr, size, -size);
cairo_set_source_rgba (cr, 0.6, 0.6, 0.6, MSD_OSD_WINDOW_FG_ALPHA / 2);
cairo_set_line_width (cr, 14);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
cairo_stroke_preserve (cr);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, MSD_OSD_WINDOW_FG_ALPHA);
cairo_set_line_width (cr, 10);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
cairo_stroke (cr);
}
static void
draw_speaker (cairo_t *cr,
double cx,
double cy,
double width,
double height)
{
double box_width;
double box_height;
double _x0;
double _y0;
box_width = width / 3;
box_height = height / 3;
_x0 = cx - (width / 2) + box_width;
_y0 = cy - box_height / 2;
cairo_move_to (cr, _x0, _y0);
cairo_rel_line_to (cr, - box_width, 0);
cairo_rel_line_to (cr, 0, box_height);
cairo_rel_line_to (cr, box_width, 0);
cairo_line_to (cr, cx + box_width, cy + height / 2);
cairo_rel_line_to (cr, 0, -height);
cairo_line_to (cr, _x0, _y0);
cairo_close_path (cr);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, MSD_OSD_WINDOW_FG_ALPHA);
cairo_fill_preserve (cr);
cairo_set_source_rgba (cr, 0.6, 0.6, 0.6, MSD_OSD_WINDOW_FG_ALPHA / 2);
cairo_set_line_width (cr, 2);
cairo_stroke (cr);
}
static gboolean
render_speaker (MsdMediaKeysWindow *window,
cairo_t *cr,
double _x0,
double _y0,
double width,
double height)
{
GdkPixbuf *pixbuf;
int icon_size;
int n;
static const char *icon_names[] = {
"audio-volume-muted",
"audio-volume-low",
"audio-volume-medium",
"audio-volume-high",
NULL
};
if (window->priv->volume_muted) {
n = 0;
} else {
/* select image */
n = 3 * window->priv->volume_level / 100 + 1;
if (n < 1) {
n = 1;
} else if (n > 3) {
n = 3;
}
}
icon_size = (int)width;
pixbuf = load_pixbuf (window, icon_names[n], icon_size);
if (pixbuf == NULL) {
return FALSE;
}
gdk_cairo_set_source_pixbuf (cr, pixbuf, _x0, _y0);
cairo_paint_with_alpha (cr, MSD_OSD_WINDOW_FG_ALPHA);
g_object_unref (pixbuf);
return TRUE;
}
static void
draw_description_label (MsdMediaKeysWindow *window,
cairo_t *cr,
double _y0,
double width)
{
cairo_text_extents_t extents;
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_set_font_size (cr, 14);
/* centered text */
cairo_text_extents (cr, window->priv->description, &extents);
cairo_move_to (cr, width / 2 - extents.width / 2, _y0);
cairo_show_text (cr, window->priv->description);
}
static void
draw_volume_boxes (MsdMediaKeysWindow *window,
cairo_t *cr,
double percentage,
double _x0,
double _y0,
double width,
double height)
{
gdouble x1;
GtkStyleContext *context;
height = round (height) - 1;
width = round (width) - 1;
x1 = round ((width - 1) * percentage);
context = gtk_widget_get_style_context (GTK_WIDGET (window));
/* bar background */
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
gtk_render_background (context, cr, _x0, _y0, width, height);
gtk_render_frame (context, cr, _x0, _y0, width, height);
gtk_style_context_restore (context);
/* bar progress */
if (percentage < 0.01)
return;
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
gtk_render_background (context, cr, _x0 + 0.5, _y0 + 0.5, x1, height -1 );
gtk_render_frame (context, cr, _x0 + 0.5, _y0 + 0.5, x1, height -1 );
gtk_style_context_restore (context);
}
static void
draw_action_volume (MsdMediaKeysWindow *window,
cairo_t *cr)
{
int window_width;
int window_height;
double icon_box_width;
double icon_box_height;
double icon_box_x0;
double icon_box_y0;
double volume_box_x0;
double volume_box_y0;
double volume_box_width;
double volume_box_height;
gboolean res;
gtk_window_get_size (GTK_WINDOW (window), &window_width, &window_height);
icon_box_width = round (window_width * ICON_SCALE);
icon_box_height = round (window_height * ICON_SCALE);
volume_box_width = icon_box_width;
volume_box_height = round (window_height * 0.05);
icon_box_x0 = round ((window_width - icon_box_width) / 2);
icon_box_y0 = round ((window_height - icon_box_height) / 2);
volume_box_x0 = round (icon_box_x0);
volume_box_y0 = round (window_height - icon_box_y0 / 2 - volume_box_height);
#if 0
g_message ("icon box: w=%f h=%f _x0=%f _y0=%f",
icon_box_width,
icon_box_height,
icon_box_x0,
icon_box_y0);
g_message ("volume box: w=%f h=%f _x0=%f _y0=%f",
volume_box_width,
volume_box_height,
volume_box_x0,
volume_box_y0);
#endif
res = render_speaker (window,
cr,
icon_box_x0, icon_box_y0,
icon_box_width, icon_box_height);
if (! res) {
double speaker_width;
double speaker_height;
double speaker_cx;
double speaker_cy;
speaker_width = icon_box_width * 0.5;
speaker_height = icon_box_height * 0.75;
speaker_cx = icon_box_x0 + speaker_width / 2;
speaker_cy = icon_box_y0 + speaker_height / 2;
#if 0
g_message ("speaker box: w=%f h=%f cx=%f cy=%f",
speaker_width,
speaker_height,
speaker_cx,
speaker_cy);
#endif
/* draw speaker symbol */
draw_speaker (cr, speaker_cx, speaker_cy, speaker_width, speaker_height);
if (! window->priv->volume_muted) {
/* draw sound waves */
double wave_x0;
double wave_y0;
double wave_radius;
wave_x0 = window_width / 2;
wave_y0 = speaker_cy;
wave_radius = icon_box_width / 2;
draw_waves (cr, wave_x0, wave_y0, wave_radius, window->priv->volume_level);
} else {
/* draw 'mute' cross */
double cross_x0;
double cross_y0;
double cross_size;
cross_size = speaker_width * 3 / 4;
cross_x0 = icon_box_x0 + icon_box_width - cross_size;
cross_y0 = speaker_cy;
draw_cross (cr, cross_x0, cross_y0, cross_size);
}
}
/* draw volume meter */
draw_volume_boxes (window,
cr,
(double)window->priv->volume_level / 100.0,
volume_box_x0,
volume_box_y0,
volume_box_width,
volume_box_height);
}
static gboolean
render_custom (MsdMediaKeysWindow *window,
cairo_t *cr,
double _x0,
double _y0,
double width,
double height)
{
GdkPixbuf *pixbuf;
int icon_size;
icon_size = (int)width;
pixbuf = load_pixbuf (window, window->priv->icon_name, icon_size);
if (pixbuf == NULL) {
char *name;
if (gtk_widget_get_direction (GTK_WIDGET (window)) == GTK_TEXT_DIR_RTL)
name = g_strdup_printf ("%s-rtl", window->priv->icon_name);
else
name = g_strdup_printf ("%s-ltr", window->priv->icon_name);
pixbuf = load_pixbuf (window, name, icon_size);
g_free (name);
if (pixbuf == NULL)
return FALSE;
}
gdk_cairo_set_source_pixbuf (cr, pixbuf, _x0, _y0);
cairo_paint_with_alpha (cr, MSD_OSD_WINDOW_FG_ALPHA);
g_object_unref (pixbuf);
return TRUE;
}
static void
draw_action_custom (MsdMediaKeysWindow *window,
cairo_t *cr)
{
int window_width;
int window_height;
double icon_box_width;
double icon_box_height;
double icon_box_x0;
double icon_box_y0;
double label_box_y0;
double label_box_width;
double label_box_height;
gboolean res;
gtk_window_get_size (GTK_WINDOW (window), &window_width, &window_height);
icon_box_width = round (window_width * ICON_SCALE);
icon_box_height = round (window_height * ICON_SCALE);
label_box_width = round (window_width);
label_box_height = round (window_height * 0.175);
icon_box_x0 = round ((window_width - icon_box_width) / 2);
icon_box_y0 = round ((window_height - icon_box_height) / 2);
label_box_y0 = round (window_height - label_box_height / 2);
#if 0
g_message ("icon box: w=%f h=%f _x0=%f _y0=%f",
icon_box_width,
icon_box_height,
icon_box_x0,
icon_box_y0);
g_message ("label box: w=%f h=%f _x0=0.0 _y0=%f",
label_box_width,
label_box_height,
label_box_y0);
#endif
res = render_custom (window,
cr,
icon_box_x0, icon_box_y0,
icon_box_width, icon_box_height);
if (! res && g_strcmp0 (window->priv->icon_name, "media-eject") == 0) {
/* draw eject symbol */
draw_eject (cr,
icon_box_x0, icon_box_y0,
icon_box_width, icon_box_height);
}
if (window->priv->description != NULL) {
/* draw description label meter */
draw_description_label (window,
cr,
label_box_y0,
label_box_width);
}
}
static void
msd_media_keys_window_draw_when_composited (MsdOsdWindow *osd_window,
cairo_t *cr)
{
MsdMediaKeysWindow *window = MSD_MEDIA_KEYS_WINDOW (osd_window);
switch (window->priv->action) {
case MSD_MEDIA_KEYS_WINDOW_ACTION_VOLUME:
draw_action_volume (window, cr);
break;
case MSD_MEDIA_KEYS_WINDOW_ACTION_CUSTOM:
draw_action_custom (window, cr);
break;
default:
break;
}
}
static void
msd_media_keys_window_class_init (MsdMediaKeysWindowClass *klass)
{
MsdOsdWindowClass *osd_window_class = MSD_OSD_WINDOW_CLASS (klass);
osd_window_class->draw_when_composited = msd_media_keys_window_draw_when_composited;
g_type_class_add_private (klass, sizeof (MsdMediaKeysWindowPrivate));
}
static void
msd_media_keys_window_init (MsdMediaKeysWindow *window)
{
window->priv = MSD_MEDIA_KEYS_WINDOW_GET_PRIVATE (window);
if (!msd_osd_window_is_composited (MSD_OSD_WINDOW (window))) {
GtkBuilder *builder;
const gchar *objects[] = {"acme_box", NULL};
GtkWidget *box;
builder = gtk_builder_new ();
gtk_builder_add_objects_from_file (builder,
GTKBUILDERDIR "/acme.ui",
(char **) objects,
NULL);
window->priv->image = GTK_IMAGE (gtk_builder_get_object (builder, "acme_image"));
window->priv->progress = GTK_WIDGET (gtk_builder_get_object (builder, "acme_volume_progressbar"));
window->priv->label = GTK_WIDGET (gtk_builder_get_object (builder, "acme_label"));
box = GTK_WIDGET (gtk_builder_get_object (builder, "acme_box"));
if (box != NULL) {
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show_all (box);
}
/* The builder needs to stay alive until the window
takes ownership of the box (and its children) */
g_object_unref (builder);
}
}
GtkWidget *
msd_media_keys_window_new (void)
{
return g_object_new (MSD_TYPE_MEDIA_KEYS_WINDOW, NULL);
}
|