aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/systray/e_mod_main.c
blob: 035344cd970161240f920ab7dc0383d5f5026062 (plain)
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
/**
 * @addtogroup Optional_Gadgets
 * @{
 *
 * @defgroup Module_Systray Systray (System Icons Tray)
 *
 * Shows system icons in a box.
 *
 * The icons come from the FreeDesktop.Org systray specification.
 *
 * @see http://standards.freedesktop.org/systemtray-spec/systemtray-spec-latest.html
 * @}
 */
/**
 * systray implementation following freedesktop.org specification.
 *
 * @see: http://standards.freedesktop.org/systemtray-spec/latest/
 *
 * @todo: implement xembed, mostly done, at least relevant parts are done.
 *        http://standards.freedesktop.org/xembed-spec/latest/
 *
 * @todo: implement messages/popup part of the spec (anyone using this at all?)
 */

#include "e.h"

#define RETRY_TIMEOUT                     2.0

#define SYSTEM_TRAY_REQUEST_DOCK          0
#define SYSTEM_TRAY_BEGIN_MESSAGE         1
#define SYSTEM_TRAY_CANCEL_MESSAGE        2
#define _NET_SYSTEM_TRAY_ORIENTATION_HORZ 0
#define _NET_SYSTEM_TRAY_ORIENTATION_VERT 1

/* XEMBED messages */
#define XEMBED_EMBEDDED_NOTIFY            0
#define XEMBED_WINDOW_ACTIVATE            1
#define XEMBED_WINDOW_DEACTIVATE          2
#define XEMBED_REQUEST_FOCUS              3
#define XEMBED_FOCUS_IN                   4
#define XEMBED_FOCUS_OUT                  5
#define XEMBED_FOCUS_NEXT                 6
#define XEMBED_FOCUS_PREV                 7
/* 8-9 were used for XEMBED_GRAB_KEY/XEMBED_UNGRAB_KEY */
#define XEMBED_MODALITY_ON                10
#define XEMBED_MODALITY_OFF               11
#define XEMBED_REGISTER_ACCELERATOR       12
#define XEMBED_UNREGISTER_ACCELERATOR     13
#define XEMBED_ACTIVATE_ACCELERATOR       14

/* Details for  XEMBED_FOCUS_IN: */
#define XEMBED_FOCUS_CURRENT              0
#define XEMBED_FOCUS_FIRST                1
#define XEMBED_FOCUS_LAST                 2

typedef struct _Instance Instance;
typedef struct _Icon     Icon;

struct _Icon
{
   Ecore_X_Window win;
   Evas_Object   *o;
   Instance      *inst;
};

struct _Instance
{
   E_Gadcon_Client *gcc;
   E_Container     *con;
   Evas            *evas;
   struct
   {
      Ecore_X_Window parent;
      Ecore_X_Window base;
      Ecore_X_Window selection;
   } win;
   struct
   {
      Evas_Object *gadget;
   } ui;
   struct
   {
      Ecore_Event_Handler *message;
      Ecore_Event_Handler *destroy;
      Ecore_Event_Handler *show;
      Ecore_Event_Handler *reparent;
      Ecore_Event_Handler *sel_clear;
      Ecore_Event_Handler *configure;
   } handler;
   struct
   {
      Ecore_Timer *retry;
   } timer;
   struct
   {
      Ecore_Job *size_apply;
   } job;
   Eina_List *icons;
};

static const char _Name[] = "Systray";
static const char _name[] = "systray";
static const char _group_gadget[] = "e/modules/systray/main";
static const char _part_box[] = "e.box";
static const char _part_size[] = "e.size";
static const char _sig_source[] = "e";
static const char _sig_enable[] = "e,action,enable";
static const char _sig_disable[] = "e,action,disable";

static Ecore_X_Atom _atom_manager = 0;
static Ecore_X_Atom _atom_st_orient = 0;
static Ecore_X_Atom _atom_st_visual = 0;
static Ecore_X_Atom _atom_st_op_code = 0;
static Ecore_X_Atom _atom_st_msg_data = 0;
static Ecore_X_Atom _atom_xembed = 0;
static Ecore_X_Atom _atom_xembed_info = 0;
static Ecore_X_Atom _atom_st_num = 0;
static int _last_st_num = -1;

static E_Module *systray_mod = NULL;
static Instance *instance = NULL; /* only one systray ever possible */
static char tmpbuf[4096]; /* general purpose buffer, just use immediately */

static Eina_Bool
_systray_site_is_safe(E_Gadcon_Site site)
{
   /* NB: filter out sites we know are not safe for a systray to sit.
    * This was done so that systray could be put into illume indicator
    * (or anywhere else really) that is 'safe' for systray to be.
    * Pretty much, this is anywhere but Desktop and toolbars at the moment */
   if (e_gadcon_site_is_desktop(site))
     return EINA_FALSE;
   else if (e_gadcon_site_is_any_toolbar(site))
     return EINA_FALSE;
   return EINA_TRUE;
}

static const char *
_systray_theme_path(void)
{
#define TF "/e-module-systray.edj"
   unsigned int dirlen;
   const char *moddir = e_module_dir_get(systray_mod);

   dirlen = strlen(moddir);
   if (dirlen >= sizeof(tmpbuf) - sizeof(TF))
     return NULL;

   memcpy(tmpbuf, moddir, dirlen);
   memcpy(tmpbuf + dirlen, TF, sizeof(TF));

   return tmpbuf;
#undef TF
}

static void
_systray_menu_new(Instance *inst, Evas_Event_Mouse_Down *ev)
{
   E_Zone *zone;
   E_Menu *m;
   int x, y;

   zone = e_util_zone_current_get(e_manager_current_get());

   m = e_menu_new();
   m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
   e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon, &x, &y, NULL, NULL);
   e_menu_activate_mouse(m, zone, x + ev->output.x, y + ev->output.y,
                         1, 1, E_MENU_POP_DIRECTION_AUTO, ev->timestamp);
   evas_event_feed_mouse_up(inst->gcc->gadcon->evas, ev->button,
                            EVAS_BUTTON_NONE, ev->timestamp, NULL);
}

static void
_systray_cb_mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
{
   Instance *inst = data;
   Evas_Event_Mouse_Down *ev = event;

   if (ev->button == 3)
     _systray_menu_new(inst, ev);
}

static void
_systray_size_apply_do(Instance *inst)
{
   const Evas_Object *o;
   Evas_Coord x, y, w, h, mw = 1, mh = 1;

   edje_object_message_signal_process(inst->ui.gadget);
   o = edje_object_part_object_get(inst->ui.gadget, _part_box);
   if (!o) return;
   evas_object_size_hint_min_get(o, &w, &h);

   if (w < 1) w = 1;
   if (h < 1) h = 1;

   if (eina_list_count(inst->icons) == 0)
     ecore_x_window_hide(inst->win.base);
   else
     ecore_x_window_show(inst->win.base);

   edje_object_size_min_calc(inst->ui.gadget, &mw, &mh);
   e_gadcon_client_min_size_set(inst->gcc, mw, mh);

   evas_object_geometry_get(o, &x, &y, &w, &h);
   ecore_x_window_move_resize(inst->win.base, x, y, w, h);
}

static void
_systray_size_apply_delayed(void *data)
{
   Instance *inst = data;
   _systray_size_apply_do(inst);
   inst->job.size_apply = NULL;
}

static void
_systray_size_apply(Instance *inst)
{
   if (inst->job.size_apply) return;
   inst->job.size_apply = ecore_job_add(_systray_size_apply_delayed, inst);
}

static void
_systray_cb_move(void *data, Evas *evas __UNUSED__, Evas_Object *o __UNUSED__, void *event __UNUSED__)
{
   Instance *inst = data;
   _systray_size_apply(inst);
}

static void
_systray_cb_resize(void *data, Evas *evas __UNUSED__, Evas_Object *o __UNUSED__, void *event __UNUSED__)
{
   Instance *inst = data;
   _systray_size_apply(inst);
}

static void
_systray_icon_geometry_apply(Icon *icon)
{
   const Evas_Object *o;
   Evas_Coord x, y, w, h, wx, wy;

   o = edje_object_part_object_get(icon->inst->ui.gadget, _part_size);
   if (!o) return;

   evas_object_geometry_get(icon->o, &x, &y, &w, &h);
   evas_object_geometry_get(o, &wx, &wy, NULL, NULL);
   ecore_x_window_move_resize(icon->win, x - wx, y - wy, w, h);
}

static void
_systray_icon_cb_move(void *data, Evas *evas __UNUSED__, Evas_Object *o __UNUSED__, void *event __UNUSED__)
{
   Icon *icon = data;
   _systray_icon_geometry_apply(icon);
}

static void
_systray_icon_cb_resize(void *data, Evas *evas __UNUSED__, Evas_Object *o __UNUSED__, void *event __UNUSED__)
{
   Icon *icon = data;
   _systray_icon_geometry_apply(icon);
}

static Ecore_X_Gravity
_systray_gravity(const Instance *inst)
{
   switch (inst->gcc->gadcon->orient)
     {
      case E_GADCON_ORIENT_FLOAT:
        return ECORE_X_GRAVITY_STATIC;

      case E_GADCON_ORIENT_HORIZ:
        return ECORE_X_GRAVITY_CENTER;

      case E_GADCON_ORIENT_VERT:
        return ECORE_X_GRAVITY_CENTER;

      case E_GADCON_ORIENT_LEFT:
        return ECORE_X_GRAVITY_CENTER;

      case E_GADCON_ORIENT_RIGHT:
        return ECORE_X_GRAVITY_CENTER;

      case E_GADCON_ORIENT_TOP:
        return ECORE_X_GRAVITY_CENTER;

      case E_GADCON_ORIENT_BOTTOM:
        return ECORE_X_GRAVITY_CENTER;

      case E_GADCON_ORIENT_CORNER_TL:
        return ECORE_X_GRAVITY_S;

      case E_GADCON_ORIENT_CORNER_TR:
        return ECORE_X_GRAVITY_S;

      case E_GADCON_ORIENT_CORNER_BL:
        return ECORE_X_GRAVITY_N;

      case E_GADCON_ORIENT_CORNER_BR:
        return ECORE_X_GRAVITY_N;

      case E_GADCON_ORIENT_CORNER_LT:
        return ECORE_X_GRAVITY_E;

      case E_GADCON_ORIENT_CORNER_RT:
        return ECORE_X_GRAVITY_W;

      case E_GADCON_ORIENT_CORNER_LB:
        return ECORE_X_GRAVITY_E;

      case E_GADCON_ORIENT_CORNER_RB:
        return ECORE_X_GRAVITY_W;

      default:
        return ECORE_X_GRAVITY_STATIC;
     }
}

static Icon *
_systray_icon_add(Instance *inst, const Ecore_X_Window win)
{
   Ecore_X_Gravity gravity;
   Evas_Object *o;
   int w, h, sz;
   Icon *icon;

   edje_object_part_geometry_get(inst->ui.gadget, _part_size,
                                 NULL, NULL, &w, &h);
   if (w > h)
     w = h;
   else
     h = w;

   /* assuming systray must be on a shelf here */
   switch (inst->gcc->gadcon->orient)
     {
      case E_GADCON_ORIENT_HORIZ:
      case E_GADCON_ORIENT_TOP:
      case E_GADCON_ORIENT_BOTTOM:
      case E_GADCON_ORIENT_CORNER_TL:
      case E_GADCON_ORIENT_CORNER_TR:
      case E_GADCON_ORIENT_CORNER_BL:
      case E_GADCON_ORIENT_CORNER_BR:
        sz = inst->gcc->gadcon->shelf->h;
        break;

      case E_GADCON_ORIENT_VERT:
      case E_GADCON_ORIENT_LEFT:
      case E_GADCON_ORIENT_RIGHT:
      case E_GADCON_ORIENT_CORNER_LT:
      case E_GADCON_ORIENT_CORNER_RT:
      case E_GADCON_ORIENT_CORNER_LB:
      case E_GADCON_ORIENT_CORNER_RB:
      default:
        sz = inst->gcc->gadcon->shelf->w;
     }
   if ((w < 16) && (sz > 16))
     w = h = sz - 5;

   w = h = e_util_icon_size_normalize(w);
   if (w > sz - 5)
     w = h = e_util_icon_size_normalize(sz - 5);

   o = evas_object_rectangle_add(inst->evas);
   if (!o)
     return NULL;
   evas_object_color_set(o, 0, 0, 0, 0);
   evas_object_resize(o, w, h);
   evas_object_show(o);

   icon = malloc(sizeof(*icon));
   if (!icon)
     {
        evas_object_del(o);
        return NULL;
     }
   icon->win = win;
   icon->inst = inst;
   icon->o = o;

   gravity = _systray_gravity(inst);
   ecore_x_icccm_size_pos_hints_set(win, 1, gravity,
                                    w, h, w, h, w, h, 0, 0,
                                    1.0, (double)w / (double)h);

   ecore_x_window_reparent(win, inst->win.base, 0, 0);
   ecore_x_window_resize(win, w, h);
   ecore_x_window_raise(win);
   ecore_x_window_client_manage(win);
   ecore_x_window_save_set_add(win);
   ecore_x_window_shape_events_select(win, 1);

   //ecore_x_window_geometry_get(win, NULL, NULL, &w, &h);

   evas_object_event_callback_add
     (o, EVAS_CALLBACK_MOVE, _systray_icon_cb_move, icon);
   evas_object_event_callback_add
     (o, EVAS_CALLBACK_RESIZE, _systray_icon_cb_resize, icon);

   inst->icons = eina_list_append(inst->icons, icon);
   edje_object_part_box_append(inst->ui.gadget, _part_box, o);
   _systray_size_apply_do(inst);
   _systray_icon_geometry_apply(icon);

   ecore_x_window_show(win);

   return icon;
}

static void
_systray_icon_del_list(Instance *inst, Eina_List *l, Icon *icon)
{
   inst->icons = eina_list_remove_list(inst->icons, l);

   ecore_x_window_save_set_del(icon->win);
   ecore_x_window_reparent(icon->win, 0, 0, 0);
   evas_object_del(icon->o);
   free(icon);

   _systray_size_apply(inst);
}

static Ecore_X_Atom
_systray_atom_st_get(int screen_num)
{
   if ((_last_st_num == -1) || (_last_st_num != screen_num))
     {
        char buf[32];
        snprintf(buf, sizeof(buf), "_NET_SYSTEM_TRAY_S%d", screen_num);
        _atom_st_num = ecore_x_atom_get(buf);
        _last_st_num = screen_num;
     }

   return _atom_st_num;
}

static Eina_Bool
_systray_selection_owner_set(int screen_num, Ecore_X_Window win)
{
   Ecore_X_Atom atom;
   Ecore_X_Window cur_selection;
   Eina_Bool ret;

   atom = _systray_atom_st_get(screen_num);
   ecore_x_selection_owner_set(win, atom, ecore_x_current_time_get());
   ecore_x_sync();
   cur_selection = ecore_x_selection_owner_get(atom);

   ret = (cur_selection == win);
   if (!ret)
     fprintf(stderr, "SYSTRAY: tried to set selection to %#x, but got %#x\n",
             win, cur_selection);

   return ret;
}

static Eina_Bool
_systray_selection_owner_set_current(Instance *inst)
{
   return _systray_selection_owner_set
            (inst->con->manager->num, inst->win.selection);
}

static void
_systray_deactivate(Instance *inst)
{
   Ecore_X_Window old;

   if (inst->win.selection == 0) return;

   edje_object_signal_emit(inst->ui.gadget, _sig_disable, _sig_source);

   while (inst->icons)
     _systray_icon_del_list(inst, inst->icons, inst->icons->data);

   old = inst->win.selection;
   inst->win.selection = 0;
   _systray_selection_owner_set_current(inst);
   ecore_x_sync();
   ecore_x_window_free(old);
   ecore_x_window_free(inst->win.base);
   inst->win.base = 0;
}

static Eina_Bool
_systray_base_create(Instance *inst)
{
   const Evas_Object *o;
   Evas_Coord x, y, w, h;
   unsigned short r, g, b;
   const char *color;

   if (inst->gcc->gadcon->shelf && (!e_util_strcmp(inst->gcc->gadcon->shelf->style, "invisible")))
     e_util_dialog_internal (_("Systray Error"),
       _("Systray cannot set its background invisible to match its shelf."));
   color = edje_object_data_get(inst->ui.gadget, inst->gcc->style);
   if (!color)
     color = edje_object_data_get(inst->ui.gadget, "default");

   if (color && (sscanf(color, "%hu %hu %hu", &r, &g, &b) == 3))
     {
        r = (65535 * (unsigned int)r) / 255;
        g = (65535 * (unsigned int)g) / 255;
        b = (65535 * (unsigned int)b) / 255;
     }
   else
     r = g = b = (unsigned short)65535;

   o = edje_object_part_object_get(inst->ui.gadget, _part_size);
   if (!o)
     return 0;

   evas_object_geometry_get(o, &x, &y, &w, &h);
   if (w < 1) w = 1;
   if (h < 1) h = 1;
   inst->win.base = ecore_x_window_new(0, 0, 0, w, h);
   ecore_x_window_reparent(inst->win.base, inst->win.parent, x, y);
   ecore_x_window_background_color_set(inst->win.base, r, g, b);
   ecore_x_window_show(inst->win.base);
   return 1;
}

static Eina_Bool
_systray_activate(Instance *inst)
{
   unsigned int visual;
   Ecore_X_Atom atom;
   Ecore_X_Window old_win;
   Ecore_X_Window_Attributes attr;

   if (inst->win.selection != 0) return 1;

   atom = _systray_atom_st_get(inst->con->manager->num);
   old_win = ecore_x_selection_owner_get(atom);
   if (old_win != 0) return 0;

   if (inst->win.base == 0)
     {
        if (!_systray_base_create(inst))
          return 0;
     }

   inst->win.selection = ecore_x_window_input_new(inst->win.base, 0, 0, 1, 1);
   if (inst->win.selection == 0)
     {
        ecore_x_window_free(inst->win.base);
        inst->win.base = 0;
        return 0;
     }

   if (!_systray_selection_owner_set_current(inst))
     {
        ecore_x_window_free(inst->win.selection);
        inst->win.selection = 0;
        ecore_x_window_free(inst->win.base);
        inst->win.base = 0;
        return 0;
     }

   ecore_x_window_attributes_get(inst->win.base, &attr);

   visual = ecore_x_visual_id_get(attr.visual);
   ecore_x_window_prop_card32_set(inst->win.selection, _atom_st_visual,
                                  (void *)&visual, 1);

   ecore_x_client_message32_send(inst->con->manager->root, _atom_manager,
                                 ECORE_X_EVENT_MASK_WINDOW_CONFIGURE,
                                 ecore_x_current_time_get(), atom,
                                 inst->win.selection, 0, 0);

   edje_object_signal_emit(inst->ui.gadget, _sig_enable, _sig_source);

   return 1;
}

static Eina_Bool
_systray_activate_retry(void *data)
{
   Instance *inst = data;
   Eina_Bool ret;

   fputs("SYSTRAY: reactivate...\n", stderr);
   ret = _systray_activate(inst);
   if (ret)
     fputs("SYSTRAY: activate success!\n", stderr);
   else
     fprintf(stderr, "SYSTRAY: activate failure! retrying in %0.1f seconds\n",
             RETRY_TIMEOUT);

   if (!ret)
     return ECORE_CALLBACK_RENEW;

   inst->timer.retry = NULL;
   return ECORE_CALLBACK_CANCEL;
}

static void
_systray_retry(Instance *inst)
{
   if (inst->timer.retry) return;
   inst->timer.retry = ecore_timer_add
       (RETRY_TIMEOUT, _systray_activate_retry, inst);
}

static Eina_Bool
_systray_activate_retry_first(void *data)
{
   Instance *inst = data;
   Eina_Bool ret;

   fputs("SYSTRAY: reactivate (first time)...\n", stderr);
   ret = _systray_activate(inst);
   if (ret)
     {
        fputs("SYSTRAY: activate success!\n", stderr);
        inst->timer.retry = NULL;
        return ECORE_CALLBACK_CANCEL;
     }

   edje_object_signal_emit(inst->ui.gadget, _sig_disable, _sig_source);

   fprintf(stderr, "SYSTRAY: activate failure! retrying in %0.1f seconds\n",
           RETRY_TIMEOUT);

   inst->timer.retry = NULL;
   _systray_retry(inst);
   return ECORE_CALLBACK_CANCEL;
}

static void
_systray_handle_request_dock(Instance *inst, Ecore_X_Event_Client_Message *ev)
{
   Ecore_X_Window win = (Ecore_X_Window)ev->data.l[2];
   Ecore_X_Time t;
   Ecore_X_Window_Attributes attr;
   const Eina_List *l;
   Icon *icon;
   unsigned int val[2];
   int r;

   EINA_LIST_FOREACH(inst->icons, l, icon)
     if (icon->win == win)
       return;

   if (!ecore_x_window_attributes_get(win, &attr))
     {
        fprintf(stderr, "SYSTRAY: could not get attributes of win %#x\n", win);
        return;
     }

   icon = _systray_icon_add(inst, win);
   if (!icon)
     return;

   r = ecore_x_window_prop_card32_get(win, _atom_xembed_info, val, 2);
   if (r < 2)
     {
        /*
           fprintf(stderr, "SYSTRAY: win %#x does not support _XEMBED_INFO (%d)\n",
                win, r);
         */
        return;
     }

   t = ecore_x_current_time_get();
   ecore_x_client_message32_send(win, _atom_xembed,
                                 ECORE_X_EVENT_MASK_NONE,
                                 t, XEMBED_EMBEDDED_NOTIFY, 0,
                                 inst->win.selection, 0);
}

static void
_systray_handle_op_code(Instance *inst, Ecore_X_Event_Client_Message *ev)
{
   unsigned long message = ev->data.l[1];

   switch (message)
     {
      case SYSTEM_TRAY_REQUEST_DOCK:
        _systray_handle_request_dock(inst, ev);
        break;

      case SYSTEM_TRAY_BEGIN_MESSAGE:
      case SYSTEM_TRAY_CANCEL_MESSAGE:
        fputs("SYSTRAY TODO: handle messages (anyone uses this?)\n", stderr);
        break;

      default:
        fprintf(stderr,
                "SYSTRAY: error, unknown message op code: %ld, win: %#lx\n",
                message, ev->data.l[2]);
     }
}

static void
_systray_handle_message(Instance *inst __UNUSED__, Ecore_X_Event_Client_Message *ev)
{
   fprintf(stderr, "SYSTRAY TODO: message op: %ld, data: %ld, %ld, %ld\n",
           ev->data.l[1], ev->data.l[2], ev->data.l[3], ev->data.l[4]);
}

static void
_systray_handle_xembed(Instance *inst __UNUSED__, Ecore_X_Event_Client_Message *ev __UNUSED__)
{
   unsigned long message = ev->data.l[1];

   switch (message)
     {
      case XEMBED_EMBEDDED_NOTIFY:
      case XEMBED_WINDOW_ACTIVATE:
      case XEMBED_WINDOW_DEACTIVATE:
      case XEMBED_REQUEST_FOCUS:
      case XEMBED_FOCUS_IN:
      case XEMBED_FOCUS_OUT:
      case XEMBED_FOCUS_NEXT:
      case XEMBED_FOCUS_PREV:
      case XEMBED_MODALITY_ON:
      case XEMBED_MODALITY_OFF:
      case XEMBED_REGISTER_ACCELERATOR:
      case XEMBED_UNREGISTER_ACCELERATOR:
      case XEMBED_ACTIVATE_ACCELERATOR:
      default:
        fprintf(stderr,
                "SYSTRAY: unsupported xembed: %#lx, %#lx, %#lx, %#lx\n",
                ev->data.l[1], ev->data.l[2], ev->data.l[3], ev->data.l[4]);
     }
}

static Eina_Bool
_systray_cb_client_message(void *data, int type __UNUSED__, void *event)
{
   Ecore_X_Event_Client_Message *ev = event;
   Instance *inst = data;

   if (ev->message_type == _atom_st_op_code)
     _systray_handle_op_code(inst, ev);
   else if (ev->message_type == _atom_st_msg_data)
     _systray_handle_message(inst, ev);
   else if (ev->message_type == _atom_xembed)
     _systray_handle_xembed(inst, ev);

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_systray_cb_window_destroy(void *data, int type __UNUSED__, void *event)
{
   Ecore_X_Event_Window_Destroy *ev = event;
   Instance *inst = data;
   Icon *icon;
   Eina_List *l;
   Eina_Bool found = EINA_FALSE;

   EINA_LIST_FOREACH(inst->icons, l, icon)
     if (icon->win == ev->win)
       {
          _systray_icon_del_list(inst, l, icon);
          found = EINA_TRUE;
          break;
       }
   if (found)
     {
        _systray_deactivate(inst);
        if (!_systray_activate(inst))
          {
             if (!inst->timer.retry)
               inst->timer.retry = ecore_timer_add
                   (0.1, _systray_activate_retry_first, inst);
             else
               edje_object_signal_emit(inst->ui.gadget, _sig_disable, _sig_source);
          }
     }

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_systray_cb_window_show(void *data, int type __UNUSED__, void *event)
{
   Ecore_X_Event_Window_Show *ev = event;
   Instance *inst = data;
   Icon *icon;
   Eina_List *l;

   EINA_LIST_FOREACH(inst->icons, l, icon)
     if (icon->win == ev->win)
       {
          _systray_icon_geometry_apply(icon);
          break;
       }

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_systray_cb_window_configure(void *data, int type __UNUSED__, void *event)
{
   Ecore_X_Event_Window_Configure *ev = event;
   Instance *inst = data;
   Icon *icon;
   const Eina_List *l;

   EINA_LIST_FOREACH(inst->icons, l, icon)
     if (icon->win == ev->win)
       {
          _systray_icon_geometry_apply(icon);
          break;
       }

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_systray_cb_reparent_notify(void *data, int type __UNUSED__, void *event)
{
   Ecore_X_Event_Window_Reparent *ev = event;
   Instance *inst = data;
   Icon *icon;
   Eina_List *l;

   EINA_LIST_FOREACH(inst->icons, l, icon)
     if ((icon->win == ev->win) && (ev->parent != inst->win.base))
       {
          _systray_icon_del_list(inst, l, icon);
          break;
       }

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_systray_cb_selection_clear(void *data, int type __UNUSED__, void *event)
{
   Ecore_X_Event_Selection_Clear *ev = event;
   Instance *inst = data;

   if ((ev->win == inst->win.selection) && (inst->win.selection != 0) &&
       (ev->atom == _systray_atom_st_get(inst->con->manager->num)))
     {
        edje_object_signal_emit(inst->ui.gadget, _sig_disable, _sig_source);

        while (inst->icons)
          _systray_icon_del_list(inst, inst->icons, inst->icons->data);

        ecore_x_window_free(inst->win.selection);
        inst->win.selection = 0;
        ecore_x_window_free(inst->win.base);
        inst->win.base = 0;
        _systray_retry(inst);
     }
   return ECORE_CALLBACK_PASS_ON;
}

static void
_systray_theme(Evas_Object *o, const char *shelf_style, const char *gc_style)
{
   const char base_theme[] = "base/theme/modules/systray";
   const char *path = _systray_theme_path();
   char buf[128], *p;
   size_t len, avail;

   len = eina_strlcpy(buf, _group_gadget, sizeof(buf));
   if (len >= sizeof(buf))
     goto fallback;
   p = buf + len;
   *p = '/';
   p++;
   avail = sizeof(buf) - len - 1;

   if (shelf_style && gc_style)
     {
        size_t r;
        r = snprintf(p, avail, "%s/%s", shelf_style, gc_style);
        if (r < avail && e_theme_edje_object_set(o, base_theme, buf))
          return;
     }

   if (shelf_style)
     {
        size_t r;
        r = eina_strlcpy(p, shelf_style, avail);
        if (r < avail && e_theme_edje_object_set(o, base_theme, buf))
          return;
     }

   if (gc_style)
     {
        size_t r;
        r = eina_strlcpy(p, gc_style, avail);
        if (r < avail && e_theme_edje_object_set(o, base_theme, buf))
          return;
     }

   if (e_theme_edje_object_set(o, base_theme, _group_gadget))
     return;

   if (shelf_style && gc_style)
     {
        size_t r;
        r = snprintf(p, avail, "%s/%s", shelf_style, gc_style);
        if (r < avail && edje_object_file_set(o, path, buf))
          return;
     }

   if (shelf_style)
     {
        size_t r;
        r = eina_strlcpy(p, shelf_style, avail);
        if (r < avail && edje_object_file_set(o, path, buf))
          return;
     }

   if (gc_style)
     {
        size_t r;
        r = eina_strlcpy(p, gc_style, avail);
        if (r < avail && edje_object_file_set(o, path, buf))
          return;
     }

fallback:
   edje_object_file_set(o, path, _group_gadget);
}

static E_Gadcon_Client *
_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
{
   Instance *inst;

   // fprintf(stderr, "SYSTRAY: init name=%s, id=%s, style=%s\n", name, id, style);

   if (!systray_mod)
     return NULL;
   if ((!id) || (instance))
     {
        e_util_dialog_internal
          (_("Another systray exists"),
          _("There can be only one systray gadget and "
            "another one already exists."));
        return NULL;
     }

   if ((gc->shelf) && (!gc->shelf->popup))
     {
        e_util_dialog_internal
          (_("Systray Error"),
          _("Systray cannot work in a shelf that is set to below everything."));
        return NULL;
     }

   inst = E_NEW(Instance, 1);
   if (!inst)
     return NULL;
   inst->evas = gc->evas;
   inst->con = e_container_current_get(e_manager_current_get());
   if (!inst->con)
     {
        E_FREE(inst);
        return NULL;
     }

   if ((gc->shelf) && (gc->shelf->popup))
     inst->win.parent = gc->shelf->popup->evas_win;
   else
     inst->win.parent = (Ecore_X_Window)ecore_evas_window_get(gc->ecore_evas);

   inst->win.base = 0;
   inst->win.selection = 0;

   inst->ui.gadget = edje_object_add(inst->evas);

   _systray_theme(inst->ui.gadget, gc->shelf ? gc->shelf->style : NULL, style);

   inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->ui.gadget);
   if (!inst->gcc)
     {
        evas_object_del(inst->ui.gadget);
        E_FREE(inst);
        return NULL;
     }

   inst->gcc->data = inst;

   if (!_systray_activate(inst))
     {
        if (!inst->timer.retry)
          inst->timer.retry = ecore_timer_add
              (0.1, _systray_activate_retry_first, inst);
        else
          edje_object_signal_emit(inst->ui.gadget, _sig_disable, _sig_source);
     }

   evas_object_event_callback_add(inst->ui.gadget, EVAS_CALLBACK_MOUSE_DOWN,
                                  _systray_cb_mouse_down, inst);
   evas_object_event_callback_add(inst->ui.gadget, EVAS_CALLBACK_MOVE,
                                  _systray_cb_move, inst);
   evas_object_event_callback_add(inst->ui.gadget, EVAS_CALLBACK_RESIZE,
                                  _systray_cb_resize, inst);

   inst->handler.message = ecore_event_handler_add
       (ECORE_X_EVENT_CLIENT_MESSAGE, _systray_cb_client_message, inst);
   inst->handler.destroy = ecore_event_handler_add
       (ECORE_X_EVENT_WINDOW_DESTROY, _systray_cb_window_destroy, inst);
   inst->handler.show = ecore_event_handler_add
       (ECORE_X_EVENT_WINDOW_SHOW, _systray_cb_window_show, inst);
   inst->handler.reparent = ecore_event_handler_add
       (ECORE_X_EVENT_WINDOW_REPARENT, _systray_cb_reparent_notify, inst);
   inst->handler.sel_clear = ecore_event_handler_add
       (ECORE_X_EVENT_SELECTION_CLEAR, _systray_cb_selection_clear, inst);
   inst->handler.configure = ecore_event_handler_add
       (ECORE_X_EVENT_WINDOW_CONFIGURE, _systray_cb_window_configure, inst);

   instance = inst;
   return inst->gcc;
}

/* Called when Gadget_Container says stop */
static void
_gc_shutdown(E_Gadcon_Client *gcc)
{
   Instance *inst = gcc->data;

   // fprintf(stderr, "SYSTRAY: shutdown %p, inst=%p\n", gcc, inst);

   if (!inst)
     return;

   _systray_deactivate(inst);
   evas_object_del(inst->ui.gadget);

   if (inst->handler.message)
     ecore_event_handler_del(inst->handler.message);
   if (inst->handler.destroy)
     ecore_event_handler_del(inst->handler.destroy);
   if (inst->handler.show)
     ecore_event_handler_del(inst->handler.show);
   if (inst->handler.reparent)
     ecore_event_handler_del(inst->handler.reparent);
   if (inst->handler.sel_clear)
     ecore_event_handler_del(inst->handler.sel_clear);
   if (inst->handler.configure)
     ecore_event_handler_del(inst->handler.configure);
   if (inst->timer.retry)
     ecore_timer_del(inst->timer.retry);
   if (inst->job.size_apply)
     ecore_job_del(inst->job.size_apply);

   if (instance == inst)
     instance = NULL;

   E_FREE(inst);
   gcc->data = NULL;
}

static void
_gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient)
{
   Instance *inst = gcc->data;
   const char *sig;
   unsigned int systray_orient;

   if (!inst)
     return;

   switch (orient)
     {
      case E_GADCON_ORIENT_FLOAT:
        sig = "e,action,orient,float";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_HORIZ:
        sig = "e,action,orient,horiz";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_VERT:
        sig = "e,action,orient,vert";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      case E_GADCON_ORIENT_LEFT:
        sig = "e,action,orient,left";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      case E_GADCON_ORIENT_RIGHT:
        sig = "e,action,orient,right";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      case E_GADCON_ORIENT_TOP:
        sig = "e,action,orient,top";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_BOTTOM:
        sig = "e,action,orient,bottom";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_CORNER_TL:
        sig = "e,action,orient,corner_tl";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_CORNER_TR:
        sig = "e,action,orient,corner_tr";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_CORNER_BL:
        sig = "e,action,orient,corner_bl";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_CORNER_BR:
        sig = "e,action,orient,corner_br";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
        break;

      case E_GADCON_ORIENT_CORNER_LT:
        sig = "e,action,orient,corner_lt";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      case E_GADCON_ORIENT_CORNER_RT:
        sig = "e,action,orient,corner_rt";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      case E_GADCON_ORIENT_CORNER_LB:
        sig = "e,action,orient,corner_lb";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      case E_GADCON_ORIENT_CORNER_RB:
        sig = "e,action,orient,corner_rb";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_VERT;
        break;

      default:
        sig = "e,action,orient,horiz";
        systray_orient = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
     }

   ecore_x_window_prop_card32_set
     (inst->win.selection, _atom_st_orient, &systray_orient, 1);

   edje_object_signal_emit(inst->ui.gadget, sig, _sig_source);
   edje_object_message_signal_process(inst->ui.gadget);
   _systray_size_apply(inst);
}

static const char *
_gc_label(const E_Gadcon_Client_Class *client_class __UNUSED__)
{
   return _("Systray");
}

static Evas_Object *
_gc_icon(const E_Gadcon_Client_Class *client_class __UNUSED__, Evas *evas)
{
   Evas_Object *o;

   o = edje_object_add(evas);
   edje_object_file_set(o, _systray_theme_path(), "icon");
   return o;
}

static const char *
_gc_id_new(const E_Gadcon_Client_Class *client_class __UNUSED__)
{
   return _name;
}

static const E_Gadcon_Client_Class _gc_class =
{
   GADCON_CLIENT_CLASS_VERSION, _name,
   {
      _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL,
      _systray_site_is_safe
   },
   E_GADCON_CLIENT_STYLE_PLAIN
};

EAPI E_Module_Api e_modapi = {E_MODULE_API_VERSION, _Name};

EAPI void *
e_modapi_init(E_Module *m)
{
   systray_mod = m;

   e_gadcon_provider_register(&_gc_class);

   if (!_atom_manager)
     _atom_manager = ecore_x_atom_get("MANAGER");
   if (!_atom_st_orient)
     _atom_st_orient = ecore_x_atom_get("_NET_SYSTEM_TRAY_ORIENTATION");
   if (!_atom_st_visual)
     _atom_st_visual = ecore_x_atom_get("_NET_SYSTEM_TRAY_VISUAL");
   if (!_atom_st_op_code)
     _atom_st_op_code = ecore_x_atom_get("_NET_SYSTEM_TRAY_OPCODE");
   if (!_atom_st_msg_data)
     _atom_st_msg_data = ecore_x_atom_get("_NET_SYSTEM_TRAY_MESSAGE_DATA");
   if (!_atom_xembed)
     _atom_xembed = ecore_x_atom_get("_XEMBED");
   if (!_atom_xembed_info)
     _atom_xembed_info = ecore_x_atom_get("_XEMBED_INFO");

   return m;
}

EAPI int
e_modapi_shutdown(E_Module *m __UNUSED__)
{
   e_gadcon_provider_unregister(&_gc_class);
   systray_mod = NULL;
   return 1;
}

EAPI int
e_modapi_save(E_Module *m __UNUSED__)
{
   return 1;
}