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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2006 Christian Hammond <chipx86@chipx86.com>
* Copyright (C) 2005 John (J5) Palmieri <johnp@redhat.com>
* Copyright (C) 2010 Red Hat, Inc.
* Copyright (C) 2011 Perberos <perberos@gmail.com>
*
* 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, 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"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <glib/gi18n.h>
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <X11/Xproto.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <gdk/gdkx.h>
#include <libmatewnck/libmatewnck.h>
#include "daemon.h"
#include "engines.h"
#include "stack.h"
#include "sound.h"
#include "notificationdaemon-dbus-glue.h"
#define MAX_NOTIFICATIONS 20
#define IMAGE_SIZE 48
#define IDLE_SECONDS 30
#define NOTIFICATION_BUS_NAME "org.freedesktop.Notifications"
#define NOTIFICATION_BUS_PATH "/org/freedesktop/Notifications"
#define NW_GET_NOTIFY_ID(nw) \
(GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(nw), "_notify_id")))
#define NW_GET_NOTIFY_SENDER(nw) \
(g_object_get_data(G_OBJECT(nw), "_notify_sender"))
#define NW_GET_DAEMON(nw) \
(g_object_get_data(G_OBJECT(nw), "_notify_daemon"))
typedef struct {
NotifyStackLocation type;
const char* identifier;
} PopupNotifyStackLocation;
const PopupNotifyStackLocation popup_stack_locations[] = {
{NOTIFY_STACK_LOCATION_TOP_LEFT, "top_left"},
{NOTIFY_STACK_LOCATION_TOP_RIGHT, "top_right"},
{NOTIFY_STACK_LOCATION_BOTTOM_LEFT, "bottom_left"},
{NOTIFY_STACK_LOCATION_BOTTOM_RIGHT, "bottom_right"},
{NOTIFY_STACK_LOCATION_UNKNOWN, NULL}
};
#define POPUP_STACK_DEFAULT_INDEX 3 /* XXX Hack! */
typedef struct {
NotifyDaemon* daemon;
GTimeVal expiration;
GTimeVal paused_diff;
guint id;
GtkWindow* nw;
Window src_window_xid;
guint has_timeout: 1;
guint paused: 1;
} NotifyTimeout;
typedef struct {
NotifyStack** stacks;
int n_stacks;
Atom workarea_atom;
} NotifyScreen;
struct _NotifyDaemonPrivate {
guint next_id;
guint timeout_source;
guint exit_timeout_source;
GHashTable* idle_reposition_notify_ids;
GHashTable* monitored_window_hash;
GHashTable* notification_hash;
gboolean url_clicked_lock;
NotifyStackLocation stack_location;
NotifyScreen** screens;
int n_screens;
};
typedef struct {
guint id;
NotifyDaemon* daemon;
} _NotifyPendingClose;
static DBusConnection* dbus_conn = NULL;
static void notify_daemon_finalize(GObject* object);
static void _notification_destroyed_cb(GtkWindow* nw, NotifyDaemon* daemon);
static void _close_notification(NotifyDaemon* daemon, guint id, gboolean hide_notification, NotifydClosedReason reason);
static GdkFilterReturn _notify_x11_filter(GdkXEvent* xevent, GdkEvent* event, NotifyDaemon* daemon);
static void _emit_closed_signal(GtkWindow* nw, NotifydClosedReason reason);
static void _action_invoked_cb(GtkWindow* nw, const char* key);
static NotifyStackLocation get_stack_location_from_string(const char* slocation);
static void sync_notification_position(NotifyDaemon* daemon, GtkWindow* nw, Window source);
static void monitor_notification_source_windows(NotifyDaemon* daemon, NotifyTimeout* nt, Window source);
G_DEFINE_TYPE(NotifyDaemon, notify_daemon, G_TYPE_OBJECT);
static void notify_daemon_class_init(NotifyDaemonClass* daemon_class)
{
GObjectClass* object_class = G_OBJECT_CLASS(daemon_class);
object_class->finalize = notify_daemon_finalize;
g_type_class_add_private(daemon_class, sizeof(NotifyDaemonPrivate));
}
static void _notify_timeout_destroy(NotifyTimeout* nt)
{
/*
* Disconnect the destroy handler to avoid a loop since the id
* won't be removed from the hash table before the widget is
* destroyed.
*/
g_signal_handlers_disconnect_by_func(nt->nw, _notification_destroyed_cb, nt->daemon);
gtk_widget_destroy(GTK_WIDGET(nt->nw));
g_free(nt);
}
static gboolean do_exit(gpointer user_data)
{
//g_debug("Exiting due to inactivity");
exit(1);
return FALSE;
}
static void add_exit_timeout(NotifyDaemon* daemon)
{
if (daemon->priv->exit_timeout_source > 0)
{
return;
}
daemon->priv->exit_timeout_source = g_timeout_add_seconds(IDLE_SECONDS, do_exit, NULL);
}
static void remove_exit_timeout(NotifyDaemon* daemon)
{
if (daemon->priv->exit_timeout_source == 0)
{
return;
}
g_source_remove(daemon->priv->exit_timeout_source);
daemon->priv->exit_timeout_source = 0;
}
static void create_stack_for_monitor(NotifyDaemon* daemon, GdkScreen* screen, int monitor_num)
{
int screen_num = gdk_screen_get_number(screen);
NotifyScreen* nscreen = daemon->priv->screens[screen_num];
nscreen->stacks[monitor_num] = notify_stack_new(daemon, screen, monitor_num, daemon->priv->stack_location);
}
static void on_screen_monitors_changed(GdkScreen* screen, NotifyDaemon* daemon)
{
NotifyScreen* nscreen;
int screen_num;
int n_monitors;
int i;
screen_num = gdk_screen_get_number(screen);
nscreen = daemon->priv->screens[screen_num];
n_monitors = gdk_screen_get_n_monitors(screen);
if (n_monitors > nscreen->n_stacks)
{
/* grow */
nscreen->stacks = g_renew(NotifyStack *, nscreen->stacks, n_monitors);
/* add more stacks */
for (i = nscreen->n_stacks; i < n_monitors; i++)
{
create_stack_for_monitor(daemon, screen, i);
}
nscreen->n_stacks = n_monitors;
}
else if (n_monitors < nscreen->n_stacks)
{
NotifyStack* last_stack;
last_stack = nscreen->stacks[n_monitors - 1];
/* transfer items before removing stacks */
for (i = n_monitors; i < nscreen->n_stacks; i++)
{
NotifyStack* stack = nscreen->stacks[i];
GList* windows = g_list_copy(notify_stack_get_windows(stack));
GList* l;
for (l = windows; l != NULL; l = l->next)
{
/* skip removing the window from the old stack since it will try
* to unrealize the window.
* And the stack is going away anyhow. */
notify_stack_add_window(last_stack, l->data, TRUE);
}
g_list_free(windows);
notify_stack_destroy(stack);
nscreen->stacks[i] = NULL;
}
/* remove the extra stacks */
nscreen->stacks = g_renew(NotifyStack*, nscreen->stacks, n_monitors);
nscreen->n_stacks = n_monitors;
}
}
static void create_stacks_for_screen(NotifyDaemon* daemon, GdkScreen *screen)
{
NotifyScreen* nscreen;
int screen_num;
int i;
screen_num = gdk_screen_get_number(screen);
nscreen = daemon->priv->screens[screen_num];
nscreen->n_stacks = gdk_screen_get_n_monitors(screen);
nscreen->stacks = g_renew(NotifyStack*, nscreen->stacks, nscreen->n_stacks);
for (i = 0; i < nscreen->n_stacks; i++)
{
create_stack_for_monitor(daemon, screen, i);
}
}
static GdkFilterReturn screen_xevent_filter(GdkXEvent* xevent, GdkEvent* event, NotifyScreen* nscreen)
{
XEvent* xev = (XEvent*) xevent;
if (xev->type == PropertyNotify && xev->xproperty.atom == nscreen->workarea_atom)
{
int i;
for (i = 0; i < nscreen->n_stacks; i++)
{
notify_stack_queue_update_position(nscreen->stacks[i]);
}
}
return GDK_FILTER_CONTINUE;
}
static void create_screens(NotifyDaemon* daemon)
{
GdkDisplay* display;
int i;
g_assert(daemon->priv->screens == NULL);
display = gdk_display_get_default();
daemon->priv->n_screens = gdk_display_get_n_screens(display);
daemon->priv->screens = g_new0(NotifyScreen *, daemon->priv->n_screens);
for (i = 0; i < daemon->priv->n_screens; i++)
{
GdkScreen* screen;
GdkWindow* gdkwindow;
screen = gdk_display_get_screen(display, i);
g_signal_connect(screen, "monitors-changed", G_CALLBACK(on_screen_monitors_changed), daemon);
daemon->priv->screens[i] = g_new0(NotifyScreen, 1);
#if GTK_CHECK_VERSION(3, 0, 0)
daemon->priv->screens[i]->workarea_atom = XInternAtom(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), "_NET_WORKAREA", True);
#else
daemon->priv->screens[i]->workarea_atom = XInternAtom(GDK_DISPLAY(), "_NET_WORKAREA", True);
#endif
gdkwindow = gdk_screen_get_root_window(screen);
gdk_window_add_filter(gdkwindow, (GdkFilterFunc) screen_xevent_filter, daemon->priv->screens[i]);
gdk_window_set_events(gdkwindow, gdk_window_get_events(gdkwindow) | GDK_PROPERTY_CHANGE_MASK);
create_stacks_for_screen(daemon, gdk_display_get_screen(display, i));
}
}
static void on_popup_location_changed(GSettings *settings, gchar *key, NotifyDaemon* daemon)
{
NotifyStackLocation stack_location;
const char* slocation;
int i;
slocation = g_settings_get_string(daemon->gsettings, key);
if (slocation != NULL && *slocation != '\0')
{
stack_location = get_stack_location_from_string(slocation);
}
else
{
g_settings_set_string (daemon->gsettings, GSETTINGS_KEY_POPUP_LOCATION, popup_stack_locations[POPUP_STACK_DEFAULT_INDEX].identifier);
stack_location = NOTIFY_STACK_LOCATION_DEFAULT;
}
daemon->priv->stack_location = stack_location;
for (i = 0; i < daemon->priv->n_screens; i++)
{
int j;
for (j = 0; j < daemon->priv->n_screens; j++)
{
NotifyStack* stack;
stack = daemon->priv->screens[i]->stacks[j];
notify_stack_set_location(stack, stack_location);
}
}
}
static void notify_daemon_init(NotifyDaemon* daemon)
{
char* location;
daemon->priv = G_TYPE_INSTANCE_GET_PRIVATE(daemon, NOTIFY_TYPE_DAEMON, NotifyDaemonPrivate);
daemon->priv->next_id = 1;
daemon->priv->timeout_source = 0;
add_exit_timeout(daemon);
daemon->gsettings = g_settings_new (GSETTINGS_SCHEMA);
location = g_settings_get_string (daemon->gsettings, GSETTINGS_KEY_POPUP_LOCATION);
daemon->priv->stack_location = get_stack_location_from_string(location);
g_free(location);
g_signal_connect (daemon->gsettings, "changed::" GSETTINGS_KEY_POPUP_LOCATION, G_CALLBACK (on_popup_location_changed), daemon);
daemon->priv->n_screens = 0;
daemon->priv->screens = NULL;
create_screens(daemon);
daemon->priv->idle_reposition_notify_ids = g_hash_table_new(NULL, NULL);
daemon->priv->monitored_window_hash = g_hash_table_new(NULL, NULL);
daemon->priv->notification_hash = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, (GDestroyNotify) _notify_timeout_destroy);
}
static void destroy_screens(NotifyDaemon* daemon)
{
GdkDisplay* display;
int i;
int j;
display = gdk_display_get_default();
for (i = 0; i < daemon->priv->n_screens; i++)
{
GdkScreen* screen;
GdkWindow* gdkwindow;
screen = gdk_display_get_screen(display, i);
g_signal_handlers_disconnect_by_func(screen, G_CALLBACK(on_screen_monitors_changed), daemon);
gdkwindow = gdk_screen_get_root_window(screen);
gdk_window_remove_filter(gdkwindow, (GdkFilterFunc) screen_xevent_filter, daemon->priv->screens[i]);
for (j = 0; i < daemon->priv->screens[i]->n_stacks; j++)
{
notify_stack_destroy(daemon->priv->screens[i]->stacks[j]);
daemon->priv->screens[i]->stacks[j] = NULL;
}
g_free(daemon->priv->screens[i]->stacks);
}
g_free(daemon->priv->screens);
daemon->priv->screens = NULL;
}
static void notify_daemon_finalize(GObject* object)
{
NotifyDaemon* daemon;
daemon = NOTIFY_DAEMON(object);
if (g_hash_table_size(daemon->priv->monitored_window_hash) > 0)
{
gdk_window_remove_filter(NULL, (GdkFilterFunc) _notify_x11_filter, daemon);
}
remove_exit_timeout(daemon);
g_hash_table_destroy(daemon->priv->monitored_window_hash);
g_hash_table_destroy(daemon->priv->idle_reposition_notify_ids);
g_hash_table_destroy(daemon->priv->notification_hash);
destroy_screens(daemon);
g_free(daemon->priv);
G_OBJECT_CLASS(notify_daemon_parent_class)->finalize(object);
}
static NotifyStackLocation get_stack_location_from_string(const char* slocation)
{
NotifyStackLocation stack_location = NOTIFY_STACK_LOCATION_DEFAULT;
if (slocation == NULL || *slocation == '\0')
{
return NOTIFY_STACK_LOCATION_DEFAULT;
}
else
{
const PopupNotifyStackLocation* l;
for (l = popup_stack_locations; l->type != NOTIFY_STACK_LOCATION_UNKNOWN; l++)
{
if (!strcmp(slocation, l->identifier))
{
stack_location = l->type;
}
}
}
return stack_location;
}
static DBusMessage* create_signal(GtkWindow* nw, const char* signal_name)
{
guint id;
char* dest;
DBusMessage* message;
id = NW_GET_NOTIFY_ID(nw);
dest = NW_GET_NOTIFY_SENDER(nw);
g_assert(dest != NULL);
message = dbus_message_new_signal(NOTIFICATION_BUS_PATH, NOTIFICATION_BUS_NAME, signal_name);
dbus_message_set_destination(message, dest);
dbus_message_append_args(message, DBUS_TYPE_UINT32, &id, DBUS_TYPE_INVALID);
return message;
}
static void _action_invoked_cb(GtkWindow* nw, const char *key)
{
NotifyDaemon* daemon;
guint id;
DBusMessage* message;
daemon = NW_GET_DAEMON(nw);
id = NW_GET_NOTIFY_ID(nw);
message = create_signal(nw, "ActionInvoked");
dbus_message_append_args(message, DBUS_TYPE_STRING, &key, DBUS_TYPE_INVALID);
dbus_connection_send(dbus_conn, message, NULL);
dbus_message_unref(message);
_close_notification(daemon, id, TRUE, NOTIFYD_CLOSED_USER);
}
static void _emit_closed_signal(GtkWindow* nw, NotifydClosedReason reason)
{
DBusMessage* message = create_signal(nw, "NotificationClosed");
dbus_message_append_args(message, DBUS_TYPE_UINT32, &reason, DBUS_TYPE_INVALID);
dbus_connection_send(dbus_conn, message, NULL);
dbus_message_unref(message);
}
static void _close_notification(NotifyDaemon* daemon, guint id, gboolean hide_notification, NotifydClosedReason reason)
{
NotifyDaemonPrivate* priv = daemon->priv;
NotifyTimeout* nt;
nt = (NotifyTimeout*) g_hash_table_lookup(priv->notification_hash, &id);
if (nt != NULL)
{
_emit_closed_signal(nt->nw, reason);
if (hide_notification)
{
theme_hide_notification(nt->nw);
}
g_hash_table_remove(priv->notification_hash, &id);
if (g_hash_table_size(daemon->priv->notification_hash) == 0)
{
add_exit_timeout(daemon);
}
}
}
static gboolean _close_notification_not_shown(_NotifyPendingClose* data)
{
_close_notification(data->daemon, data->id, TRUE, NOTIFYD_CLOSED_RESERVED);
g_object_unref(data->daemon);
g_free(data);
return FALSE;
}
static void _notification_destroyed_cb(GtkWindow* nw, NotifyDaemon* daemon)
{
/*
* This usually won't happen, but can if notification-daemon dies before
* all notifications are closed. Mark them as expired.
*/
_close_notification(daemon, NW_GET_NOTIFY_ID(nw), FALSE, NOTIFYD_CLOSED_EXPIRED);
}
typedef struct {
NotifyDaemon* daemon;
gint id;
} IdleRepositionData;
static gboolean idle_reposition_notification(IdleRepositionData* data)
{
NotifyDaemon* daemon;
NotifyTimeout* nt;
gint notify_id;
daemon = data->daemon;
notify_id = data->id;
/* Look up the timeout, if it's completed we don't need to do anything */
nt = (NotifyTimeout*) g_hash_table_lookup(daemon->priv->notification_hash, ¬ify_id);
if (nt != NULL)
{
sync_notification_position(daemon, nt->nw, nt->src_window_xid);
}
g_hash_table_remove(daemon->priv->idle_reposition_notify_ids, GINT_TO_POINTER(notify_id));
g_object_unref(daemon);
g_free(data);
return FALSE;
}
static void _queue_idle_reposition_notification(NotifyDaemon* daemon, gint notify_id)
{
IdleRepositionData* data;
gpointer orig_key;
gpointer value;
guint idle_id;
/* Do we already have an idle update pending? */
if (g_hash_table_lookup_extended(daemon->priv->idle_reposition_notify_ids, GINT_TO_POINTER(notify_id), &orig_key, &value))
{
return;
}
data = g_new0(IdleRepositionData, 1);
data->daemon = g_object_ref(daemon);
data->id = notify_id;
/* We do this as a short timeout to avoid repositioning spam */
idle_id = g_timeout_add_full(G_PRIORITY_LOW, 50, (GSourceFunc) idle_reposition_notification, data, NULL);
g_hash_table_insert(daemon->priv->idle_reposition_notify_ids, GINT_TO_POINTER(notify_id), GUINT_TO_POINTER(idle_id));
}
static GdkFilterReturn _notify_x11_filter(GdkXEvent* xevent, GdkEvent* event, NotifyDaemon* daemon)
{
XEvent* xev;
gpointer orig_key;
gpointer value;
gint notify_id;
NotifyTimeout* nt;
xev = (XEvent*) xevent;
if (xev->xany.type == DestroyNotify)
{
g_hash_table_remove(daemon->priv->monitored_window_hash, GUINT_TO_POINTER(xev->xany.window));
if (g_hash_table_size(daemon->priv->monitored_window_hash) == 0)
{
gdk_window_remove_filter(NULL, (GdkFilterFunc) _notify_x11_filter, daemon);
}
return GDK_FILTER_CONTINUE;
}
if (!g_hash_table_lookup_extended(daemon->priv->monitored_window_hash, GUINT_TO_POINTER(xev->xany.window), &orig_key, &value))
{
return GDK_FILTER_CONTINUE;
}
notify_id = GPOINTER_TO_INT(value);
if (xev->xany.type == ConfigureNotify || xev->xany.type == MapNotify)
{
_queue_idle_reposition_notification(daemon, notify_id);
}
else if (xev->xany.type == ReparentNotify)
{
nt = (NotifyTimeout *) g_hash_table_lookup(daemon->priv->notification_hash, ¬ify_id);
if (nt == NULL)
{
return GDK_FILTER_CONTINUE;
}
/*
* If the window got reparented, we need to start monitoring the
* new parents.
*/
monitor_notification_source_windows(daemon, nt, nt->src_window_xid);
sync_notification_position(daemon, nt->nw, nt->src_window_xid);
}
return GDK_FILTER_CONTINUE;
}
static void _mouse_entered_cb(GtkWindow* nw, GdkEventCrossing* event, NotifyDaemon* daemon)
{
NotifyTimeout* nt;
guint id;
GTimeVal now;
if (event->detail == GDK_NOTIFY_INFERIOR)
{
return;
}
id = NW_GET_NOTIFY_ID(nw);
nt = (NotifyTimeout*) g_hash_table_lookup(daemon->priv->notification_hash, &id);
nt->paused = TRUE;
g_get_current_time(&now);
nt->paused_diff.tv_usec = nt->expiration.tv_usec - now.tv_usec;
nt->paused_diff.tv_sec = nt->expiration.tv_sec - now.tv_sec;
if (nt->paused_diff.tv_usec < 0)
{
nt->paused_diff.tv_usec += G_USEC_PER_SEC;
nt->paused_diff.tv_sec--;
}
}
static void _mouse_exitted_cb(GtkWindow* nw, GdkEventCrossing* event, NotifyDaemon* daemon)
{
if (event->detail == GDK_NOTIFY_INFERIOR)
{
return;
}
guint id = NW_GET_NOTIFY_ID(nw);
NotifyTimeout* nt = (NotifyTimeout*) g_hash_table_lookup(daemon->priv->notification_hash, &id);
nt->paused = FALSE;
}
static gboolean _is_expired(gpointer key, NotifyTimeout* nt, gboolean* phas_more_timeouts)
{
time_t now_time;
time_t expiration_time;
GTimeVal now;
if (!nt->has_timeout)
{
return FALSE;
}
g_get_current_time(&now);
expiration_time = (nt->expiration.tv_sec * 1000) + (nt->expiration.tv_usec / 1000);
now_time = (now.tv_sec * 1000) + (now.tv_usec / 1000);
if (now_time > expiration_time)
{
theme_notification_tick(nt->nw, 0);
_emit_closed_signal(nt->nw, NOTIFYD_CLOSED_EXPIRED);
return TRUE;
}
else if (nt->paused)
{
nt->expiration.tv_usec = nt->paused_diff.tv_usec + now.tv_usec;
nt->expiration.tv_sec = nt->paused_diff.tv_sec + now.tv_sec;
if (nt->expiration.tv_usec >= G_USEC_PER_SEC)
{
nt->expiration.tv_usec -= G_USEC_PER_SEC;
nt->expiration.tv_sec++;
}
}
else
{
theme_notification_tick(nt->nw, expiration_time - now_time);
}
*phas_more_timeouts = TRUE;
return FALSE;
}
static gboolean _check_expiration(NotifyDaemon* daemon)
{
gboolean has_more_timeouts = FALSE;
g_hash_table_foreach_remove(daemon->priv->notification_hash, (GHRFunc) _is_expired, (gpointer) &has_more_timeouts);
if (!has_more_timeouts)
{
daemon->priv->timeout_source = 0;
if (g_hash_table_size (daemon->priv->notification_hash) == 0)
{
add_exit_timeout(daemon);
}
}
return has_more_timeouts;
}
static void _calculate_timeout(NotifyDaemon* daemon, NotifyTimeout* nt, int timeout)
{
if (timeout == 0)
{
nt->has_timeout = FALSE;
}
else
{
nt->has_timeout = TRUE;
if (timeout == -1)
{
timeout = NOTIFY_DAEMON_DEFAULT_TIMEOUT;
}
theme_set_notification_timeout(nt->nw, timeout);
glong usec = timeout * 1000; /* convert from msec to usec */
/*
* If it's less than 0, wrap around back to MAXLONG.
* g_time_val_add() requires a glong, and anything larger than
* MAXLONG will be treated as a negative value.
*/
if (usec < 0)
{
usec = G_MAXLONG;
}
g_get_current_time(&nt->expiration);
g_time_val_add(&nt->expiration, usec);
if (daemon->priv->timeout_source == 0)
{
daemon->priv->timeout_source = g_timeout_add(100, (GSourceFunc) _check_expiration, daemon);
}
}
}
static NotifyTimeout* _store_notification(NotifyDaemon* daemon, GtkWindow* nw, int timeout)
{
NotifyDaemonPrivate* priv = daemon->priv;
NotifyTimeout* nt;
guint id = 0;
do {
id = priv->next_id;
if (id != UINT_MAX)
{
priv->next_id++;
}
else
{
priv->next_id = 1;
}
if (g_hash_table_lookup (priv->notification_hash, &id) != NULL)
{
id = 0;
}
} while (id == 0);
nt = g_new0(NotifyTimeout, 1);
nt->id = id;
nt->nw = nw;
nt->daemon = daemon;
_calculate_timeout(daemon, nt, timeout);
g_hash_table_insert(priv->notification_hash, g_memdup(&id, sizeof(guint)), nt);
remove_exit_timeout(daemon);
return nt;
}
static GdkPixbuf* _notify_daemon_pixbuf_from_data_hint(GValue* icon_data)
{
const guchar* data = NULL;
gboolean has_alpha;
int bits_per_sample;
int width;
int height;
int rowstride;
int n_channels;
gsize expected_len;
GdkPixbuf* pixbuf;
GValueArray* image_struct;
GValue* value;
GArray* tmp_array;
GType struct_type;
struct_type = dbus_g_type_get_struct("GValueArray", G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_INT, dbus_g_type_get_collection ("GArray", G_TYPE_UCHAR), G_TYPE_INVALID);
if (!G_VALUE_HOLDS (icon_data, struct_type))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected a GValue of type GValueArray");
return NULL;
}
image_struct = (GValueArray *) g_value_get_boxed (icon_data);
value = g_value_array_get_nth (image_struct, 0);
if (value == NULL)
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 0 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, G_TYPE_INT))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 0 of the GValueArray to be of type int");
return NULL;
}
width = g_value_get_int (value);
value = g_value_array_get_nth (image_struct, 1);
if (value == NULL)
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 1 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, G_TYPE_INT))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 1 of the GValueArray to be of type int");
return NULL;
}
height = g_value_get_int (value);
value = g_value_array_get_nth (image_struct, 2);
if (value == NULL)
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 2 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, G_TYPE_INT))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 2 of the GValueArray to be of type int");
return NULL;
}
rowstride = g_value_get_int (value);
value = g_value_array_get_nth (image_struct, 3);
if (value == NULL)
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 3 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, G_TYPE_BOOLEAN))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 3 of the GValueArray to be of type gboolean");
return NULL;
}
has_alpha = g_value_get_boolean (value);
value = g_value_array_get_nth (image_struct, 4);
if (value == NULL) {
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 4 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, G_TYPE_INT))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 4 of the GValueArray to be of type int");
return NULL;
}
bits_per_sample = g_value_get_int (value);
value = g_value_array_get_nth (image_struct, 5);
if (value == NULL)
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 5 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, G_TYPE_INT))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 5 of the GValueArray to be of type int");
return NULL;
}
n_channels = g_value_get_int (value);
value = g_value_array_get_nth (image_struct, 6);
if (value == NULL)
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 6 of the GValueArray to exist");
return NULL;
}
if (!G_VALUE_HOLDS (value, dbus_g_type_get_collection ("GArray", G_TYPE_UCHAR)))
{
g_warning ("_notify_daemon_pixbuf_from_data_hint expected position 6 of the GValueArray to be of type GArray");
return NULL;
}
tmp_array = (GArray*) g_value_get_boxed(value);
expected_len = (height - 1) * rowstride + width * ((n_channels * bits_per_sample + 7) / 8);
if (expected_len != tmp_array->len)
{
g_warning("_notify_daemon_pixbuf_from_data_hint expected image data to be of length %" G_GSIZE_FORMAT " but got a " "length of %u", expected_len, tmp_array->len);
return NULL;
}
data = (guchar *) g_memdup(tmp_array->data, tmp_array->len);
pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, has_alpha, bits_per_sample, width, height, rowstride, (GdkPixbufDestroyNotify) g_free, NULL);
return pixbuf;
}
static GdkPixbuf* _notify_daemon_pixbuf_from_path(const char* path)
{
GdkPixbuf* pixbuf = NULL;
if (!strncmp (path, "file://", 7) || *path == '/')
{
if (!strncmp (path, "file://", 7))
{
path += 7;
}
/* Load file */
pixbuf = gdk_pixbuf_new_from_file (path, NULL);
}
else
{
/* Load icon theme icon */
GtkIconTheme *theme;
GtkIconInfo *icon_info;
theme = gtk_icon_theme_get_default ();
icon_info = gtk_icon_theme_lookup_icon (theme, path, IMAGE_SIZE, GTK_ICON_LOOKUP_USE_BUILTIN);
if (icon_info != NULL)
{
gint icon_size;
icon_size = MIN (IMAGE_SIZE, gtk_icon_info_get_base_size (icon_info));
if (icon_size == 0)
{
icon_size = IMAGE_SIZE;
}
pixbuf = gtk_icon_theme_load_icon (theme, path, icon_size, GTK_ICON_LOOKUP_USE_BUILTIN, NULL);
gtk_icon_info_free (icon_info);
}
if (pixbuf == NULL)
{
/* Well... maybe this is a file afterall. */
pixbuf = gdk_pixbuf_new_from_file (path, NULL);
}
}
return pixbuf;
}
static GdkPixbuf* _notify_daemon_scale_pixbuf(GdkPixbuf *pixbuf, gboolean no_stretch_hint)
{
int pw;
int ph;
float scale_factor_x = 1.0;
float scale_factor_y = 1.0;
float scale_factor = 1.0;
pw = gdk_pixbuf_get_width (pixbuf);
ph = gdk_pixbuf_get_height (pixbuf);
/* Determine which dimension requires the smallest scale. */
scale_factor_x = (float) IMAGE_SIZE / (float) pw;
scale_factor_y = (float) IMAGE_SIZE / (float) ph;
if (scale_factor_x > scale_factor_y)
{
scale_factor = scale_factor_y;
}
else
{
scale_factor = scale_factor_x;
}
/* always scale down, allow to disable scaling up */
if (scale_factor < 1.0 || !no_stretch_hint)
{
int scale_x;
int scale_y;
scale_x = (int) (pw * scale_factor);
scale_y = (int) (ph * scale_factor);
return gdk_pixbuf_scale_simple (pixbuf,
scale_x,
scale_y,
GDK_INTERP_BILINEAR);
}
else
{
return g_object_ref (pixbuf);
}
}
static void window_clicked_cb(GtkWindow* nw, GdkEventButton* button, NotifyDaemon* daemon)
{
if (daemon->priv->url_clicked_lock)
{
daemon->priv->url_clicked_lock = FALSE;
return;
}
_action_invoked_cb (nw, "default");
_close_notification (daemon, NW_GET_NOTIFY_ID (nw), TRUE, NOTIFYD_CLOSED_USER);
}
static void url_clicked_cb(GtkWindow* nw, const char *url)
{
NotifyDaemon* daemon;
char* escaped_url;
char* cmd = NULL;
daemon = NW_GET_DAEMON(nw);
/* Somewhat of a hack.. */
daemon->priv->url_clicked_lock = TRUE;
escaped_url = g_shell_quote (url);
if (g_find_program_in_path ("gvfs-open") != NULL)
{
cmd = g_strdup_printf ("gvfs-open %s", escaped_url);
}
else if (g_find_program_in_path ("xdg-open") != NULL)
{
cmd = g_strdup_printf ("xdg-open %s", escaped_url);
}
else if (g_find_program_in_path ("firefox") != NULL)
{
cmd = g_strdup_printf ("firefox %s", escaped_url);
}
else
{
g_warning ("Unable to find a browser.");
}
g_free (escaped_url);
if (cmd != NULL)
{
g_spawn_command_line_async (cmd, NULL);
g_free (cmd);
}
}
static gboolean screensaver_active(GtkWidget* nw)
{
GError* error = NULL;
gboolean active = FALSE;
DBusGConnection* connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
if (connection == NULL)
{
//g_warning("Failed to get dbus connection: %s", error->message);
g_error_free(error);
goto out;
}
DBusGProxy* gs_proxy = dbus_g_proxy_new_for_name(connection, "org.mate.ScreenSaver", "/", "org.mate.ScreenSaver");
if (!dbus_g_proxy_call(gs_proxy, "GetActive", &error, G_TYPE_INVALID, G_TYPE_BOOLEAN, &active, G_TYPE_INVALID))
{
//g_warning("Failed to call mate-screensaver: %s", error->message);
g_error_free(error);
}
g_object_unref(gs_proxy);
out:
return active;
}
static gboolean fullscreen_window_exists(GtkWidget* nw)
{
MatewnckScreen* matewnck_screen;
MatewnckWorkspace* matewnck_workspace;
GList* l;
#if (GDK_PIXBUF_MAJOR == 2) && (GDK_PIXBUF_MINOR < 22)
matewnck_screen = matewnck_screen_get(GDK_SCREEN_XNUMBER(gdk_window_get_screen(gtk_widget_get_window(nw))));
#else
matewnck_screen = matewnck_screen_get(GDK_SCREEN_XNUMBER(gdk_drawable_get_screen(GDK_DRAWABLE(GTK_WIDGET(nw)->window))));
#endif
matewnck_screen_force_update (matewnck_screen);
matewnck_workspace = matewnck_screen_get_active_workspace (matewnck_screen);
for (l = matewnck_screen_get_windows_stacked (matewnck_screen); l != NULL; l = l->next)
{
MatewnckWindow *matewnck_win = (MatewnckWindow *) l->data;
if (matewnck_window_is_on_workspace (matewnck_win, matewnck_workspace) && matewnck_window_is_fullscreen (matewnck_win) && matewnck_window_is_active (matewnck_win))
{
/*
* Sanity check if the window is _really_ fullscreen to
* work around a bug in libmatewnck that doesn't get all
* unfullscreen events.
*/
int sw = matewnck_screen_get_width (matewnck_screen);
int sh = matewnck_screen_get_height (matewnck_screen);
int x, y, w, h;
matewnck_window_get_geometry (matewnck_win, &x, &y, &w, &h);
if (sw == w && sh == h)
{
return TRUE;
}
}
}
return FALSE;
}
static Window get_window_parent(Display* display, Window window, Window* root)
{
Window parent;
Window* children = NULL;
guint nchildren;
gboolean result;
gdk_error_trap_push();
result = XQueryTree(display, window, root, &parent, &children, &nchildren);
if (gdk_error_trap_pop() || !result)
{
return None;
}
if (children)
{
XFree(children);
}
return parent;
}
/*
* Recurse over X Window and parents, up to root, and start watching them
* for position changes.
*/
static void monitor_notification_source_windows(NotifyDaemon *daemon, NotifyTimeout *nt, Window source)
{
Display* display;
Window root = None;
Window parent;
display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
/* Start monitoring events if necessary. We don't want to
filter events unless we absolutely have to. */
if (g_hash_table_size (daemon->priv->monitored_window_hash) == 0)
{
gdk_window_add_filter (NULL, (GdkFilterFunc) _notify_x11_filter, daemon);
}
/* Store the window in the timeout */
g_assert (nt != NULL);
nt->src_window_xid = source;
for (parent = get_window_parent (display, source, &root); parent != None && root != parent; parent = get_window_parent (display, parent, &root))
{
XSelectInput (display, parent, StructureNotifyMask);
g_hash_table_insert(daemon->priv->monitored_window_hash, GUINT_TO_POINTER (parent), GINT_TO_POINTER (nt->id));
}
}
/* Use a source X Window ID to reposition a notification. */
static void sync_notification_position(NotifyDaemon* daemon, GtkWindow* nw, Window source)
{
Display* display;
Status result;
Window root;
Window child;
int x, y;
unsigned int width, height;
unsigned int border_width, depth;
display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default ());
gdk_error_trap_push();
/* Get the root for this window */
result = XGetGeometry(display, source, &root, &x, &y, &width, &height, &border_width, &depth);
if (gdk_error_trap_pop () || !result)
{
return;
}
/*
* Now calculate the offset coordinates for the source window from
* the root.
*/
gdk_error_trap_push ();
result = XTranslateCoordinates (display, source, root, 0, 0, &x, &y, &child);
if (gdk_error_trap_pop () || !result)
{
return;
}
x += width / 2;
y += height / 2;
theme_set_notification_arrow (nw, TRUE, x, y);
theme_move_notification (nw, x, y);
theme_show_notification (nw);
/*
* We need to manually queue a draw here as the default theme recalculates
* its position in the draw handler and moves the window (which seems
* fairly broken), so just calling move/show above isn't enough to cause
* its position to be calculated.
*/
gtk_widget_queue_draw (GTK_WIDGET (nw));
}
GQuark notify_daemon_error_quark(void)
{
static GQuark q = 0;
if (q == 0)
{
q = g_quark_from_static_string ("notification-daemon-error-quark");
}
return q;
}
gboolean notify_daemon_notify_handler(NotifyDaemon* daemon, const char* app_name, guint id, const char* icon, const char* summary, const char* body, char** actions, GHashTable* hints, int timeout, DBusGMethodInvocation* context)
{
NotifyDaemonPrivate *priv = daemon->priv;
NotifyTimeout* nt = NULL;
GtkWindow* nw = NULL;
GValue* data;
gboolean use_pos_data = FALSE;
gboolean new_notification = FALSE;
gint x = 0;
gint y = 0;
Window window_xid = None;
guint return_id;
char* sender;
char* sound_file = NULL;
gboolean sound_enabled;
gint i;
GdkPixbuf* pixbuf;
GSettings* gsettings;
if (g_hash_table_size (priv->notification_hash) > MAX_NOTIFICATIONS)
{
GError *error;
error = g_error_new (notify_daemon_error_quark (), 1, _("Exceeded maximum number of notifications"));
dbus_g_method_return_error (context, error);
g_error_free (error);
return TRUE;
}
if (id > 0)
{
nt = (NotifyTimeout *) g_hash_table_lookup (priv->notification_hash, &id);
if (nt != NULL)
{
nw = nt->nw;
}
else
{
id = 0;
}
}
if (nw == NULL)
{
nw = theme_create_notification (url_clicked_cb);
g_object_set_data (G_OBJECT (nw), "_notify_daemon", daemon);
gtk_widget_realize (GTK_WIDGET (nw));
new_notification = TRUE;
g_signal_connect (G_OBJECT (nw), "button-release-event", G_CALLBACK (window_clicked_cb), daemon);
g_signal_connect (G_OBJECT (nw), "destroy", G_CALLBACK (_notification_destroyed_cb), daemon);
g_signal_connect (G_OBJECT (nw), "enter-notify-event", G_CALLBACK (_mouse_entered_cb), daemon);
g_signal_connect (G_OBJECT (nw), "leave-notify-event", G_CALLBACK (_mouse_exitted_cb), daemon);
}
else
{
theme_clear_notification_actions (nw);
}
theme_set_notification_text (nw, summary, body);
theme_set_notification_hints (nw, hints);
/*
*XXX This needs to handle file URIs and all that.
*/
if ((data = (GValue *) g_hash_table_lookup (hints, "window-xid")) != NULL)
{
window_xid = (Window) g_value_get_uint (data);
}
/* deal with x, and y hints */
else if ((data = (GValue *) g_hash_table_lookup (hints, "x")) != NULL)
{
x = g_value_get_int (data);
if ((data = (GValue *) g_hash_table_lookup (hints, "y")) != NULL)
{
y = g_value_get_int (data);
use_pos_data = TRUE;
}
}
/* Deal with sound hints */
gsettings = g_settings_new (GSETTINGS_SCHEMA);
sound_enabled = g_settings_get_boolean (gsettings, GSETTINGS_KEY_SOUND_ENABLED);
g_object_unref (gsettings);
data = (GValue *) g_hash_table_lookup (hints, "suppress-sound");
if (data != NULL)
{
if (G_VALUE_HOLDS_BOOLEAN (data))
{
sound_enabled = !g_value_get_boolean (data);
}
else if (G_VALUE_HOLDS_INT (data))
{
sound_enabled = (g_value_get_int (data) != 0);
}
else
{
g_warning ("suppress-sound is of type %s (expected bool or int)\n", g_type_name (G_VALUE_TYPE (data)));
}
}
if (sound_enabled)
{
data = (GValue *) g_hash_table_lookup (hints, "sound-file");
if (data != NULL)
{
sound_file = g_value_dup_string (data);
if (*sound_file == '\0' || !g_file_test (sound_file, G_FILE_TEST_EXISTS))
{
g_free (sound_file);
sound_file = NULL;
}
}
}
/* set up action buttons */
for (i = 0; actions[i] != NULL; i += 2)
{
char* l = actions[i + 1];
if (l == NULL)
{
g_warning ("Label not found for action %s. The protocol specifies that a label must follow an action in the actions array", actions[i]);
break;
}
if (strcasecmp (actions[i], "default"))
{
theme_add_notification_action (nw, l, actions[i], G_CALLBACK (_action_invoked_cb));
}
}
pixbuf = NULL;
if ((data = (GValue *) g_hash_table_lookup (hints, "image_data")))
{
pixbuf = _notify_daemon_pixbuf_from_data_hint (data);
}
else if ((data = (GValue *) g_hash_table_lookup (hints, "image_path")))
{
if (G_VALUE_HOLDS_STRING (data))
{
const char *path = g_value_get_string (data);
pixbuf = _notify_daemon_pixbuf_from_path (path);
}
else
{
g_warning ("notify_daemon_notify_handler expected image_path hint to be of type string");
}
}
else if (*icon != '\0')
{
pixbuf = _notify_daemon_pixbuf_from_path (icon);
}
else if ((data = (GValue *) g_hash_table_lookup (hints, "icon_data")))
{
g_warning("\"icon_data\" hint is deprecated, please use \"image_data\" instead");
pixbuf = _notify_daemon_pixbuf_from_data_hint (data);
}
if (pixbuf != NULL)
{
GdkPixbuf *scaled;
scaled = NULL;
scaled = _notify_daemon_scale_pixbuf (pixbuf, TRUE);
theme_set_notification_icon (nw, scaled);
g_object_unref (G_OBJECT (pixbuf));
if (scaled != NULL)
g_object_unref (scaled);
}
if (window_xid != None && !theme_get_always_stack (nw))
{
/*
* Do nothing here if we were passed an XID; we'll call
* sync_notification_position later.
*/
}
else if (use_pos_data && !theme_get_always_stack (nw))
{
/*
* Typically, the theme engine will set its own position based on
* the arrow X, Y hints. However, in case, move the notification to
* that position.
*/
theme_set_notification_arrow (nw, TRUE, x, y);
theme_move_notification (nw, x, y);
}
else
{
int monitor_num;
int screen_num;
GdkScreen* screen;
gint x, y;
theme_set_notification_arrow (nw, FALSE, 0, 0);
/* If the "use-active-monitor" gsettings key is set to TRUE, then
* get the monitor the pointer is at. Otherwise, get the monitor
* number the user has set in gsettings. */
if (g_settings_get_boolean(daemon->gsettings, GSETTINGS_KEY_USE_ACTIVE))
{
gdk_display_get_pointer (gdk_display_get_default (), &screen, &x, &y, NULL);
screen_num = gdk_screen_get_number (screen);
monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
}
else
{
screen = gdk_display_get_default_screen(gdk_display_get_default());
screen_num = gdk_screen_get_number(screen);
monitor_num = g_settings_get_int(daemon->gsettings, GSETTINGS_KEY_MONITOR_NUMBER);
}
if (monitor_num >= priv->screens[screen_num]->n_stacks)
{
/* screw it - dump it on the last one we'll get
a monitors-changed signal soon enough*/
monitor_num = priv->screens[screen_num]->n_stacks - 1;
}
notify_stack_add_window (priv->screens[screen_num]->stacks[monitor_num], nw, new_notification);
}
if (id == 0)
{
nt = _store_notification (daemon, nw, timeout);
return_id = nt->id;
}
else
{
return_id = id;
}
/*
* If we have a source Window XID, start monitoring the tree
* for changes, and reposition the window based on the source
* window. We need to do this after return_id is calculated.
*/
if (window_xid != None && !theme_get_always_stack (nw))
{
monitor_notification_source_windows (daemon, nt, window_xid);
sync_notification_position (daemon, nw, window_xid);
}
/* If there is no timeout, show the notification also if screensaver
* is active or there are fullscreen windows
*/
if (!nt->has_timeout || (!screensaver_active (GTK_WIDGET (nw)) && !fullscreen_window_exists (GTK_WIDGET (nw))))
{
theme_show_notification (nw);
if (sound_file != NULL)
{
sound_play_file (GTK_WIDGET (nw), sound_file);
}
}
else
{
_NotifyPendingClose* data;
/* The notification was not shown, so queue up a close
* for it */
data = g_new0 (_NotifyPendingClose, 1);
data->id = id;
data->daemon = g_object_ref (daemon);
g_idle_add ((GSourceFunc) _close_notification_not_shown, data);
}
g_free (sound_file);
sender = dbus_g_method_get_sender (context);
g_object_set_data (G_OBJECT (nw), "_notify_id", GUINT_TO_POINTER (return_id));
g_object_set_data_full (G_OBJECT (nw), "_notify_sender", sender, (GDestroyNotify) g_free);
if (nt)
{
_calculate_timeout (daemon, nt, timeout);
}
dbus_g_method_return (context, return_id);
return TRUE;
}
gboolean notify_daemon_close_notification_handler(NotifyDaemon* daemon, guint id, GError** error)
{
if (id == 0)
{
g_set_error (error, notify_daemon_error_quark (), 100, _("%u is not a valid notification ID"), id);
return FALSE;
}
else
{
_close_notification (daemon, id, TRUE, NOTIFYD_CLOSED_API);
return TRUE;
}
}
gboolean notify_daemon_get_capabilities(NotifyDaemon* daemon, char*** caps)
{
GPtrArray* a = g_ptr_array_new ();
g_ptr_array_add (a, g_strdup ("actions"));
g_ptr_array_add (a, g_strdup ("body"));
g_ptr_array_add (a, g_strdup ("body-hyperlinks"));
g_ptr_array_add (a, g_strdup ("body-markup"));
g_ptr_array_add (a, g_strdup ("icon-static"));
g_ptr_array_add (a, g_strdup ("sound"));
g_ptr_array_add (a, NULL);
char** _caps = (char**) g_ptr_array_free (a, FALSE);
*caps = _caps;
return TRUE;
}
gboolean notify_daemon_get_server_information(NotifyDaemon *daemon, char** out_name, char** out_vendor, char** out_version, char** out_spec_ver)
{
*out_name = g_strdup("Notification Daemon");
*out_vendor = g_strdup("MATE");
*out_version = g_strdup(PACKAGE_VERSION);
*out_spec_ver = g_strdup("1.1");
return TRUE;
}
int main(int argc, char** argv)
{
NotifyDaemon* daemon;
DBusGConnection* connection;
DBusGProxy* bus_proxy;
GError* error;
gboolean res;
guint request_name_result;
g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
gtk_init(&argc, &argv);
error = NULL;
connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
if (connection == NULL)
{
g_printerr("Failed to open connection to bus: %s\n", error->message);
g_error_free(error);
exit(1);
}
dbus_conn = dbus_g_connection_get_connection(connection);
dbus_g_object_type_install_info(NOTIFY_TYPE_DAEMON, &dbus_glib_notification_daemon_object_info);
bus_proxy = dbus_g_proxy_new_for_name(connection, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");
res = dbus_g_proxy_call(bus_proxy, "RequestName", &error, G_TYPE_STRING, NOTIFICATION_BUS_NAME, G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT, &request_name_result, G_TYPE_INVALID);
if (! res || request_name_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
{
if (error != NULL)
{
g_warning("Failed to acquire name %s: %s", NOTIFICATION_BUS_NAME, error->message);
g_error_free(error);
}
else
{
g_warning("Failed to acquire name %s", NOTIFICATION_BUS_NAME);
}
goto out;
}
daemon = g_object_new(NOTIFY_TYPE_DAEMON, NULL);
dbus_g_connection_register_g_object(connection, "/org/freedesktop/Notifications", G_OBJECT(daemon));
gtk_main();
g_object_unref(daemon);
out:
return 0;
}
|