+ XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
+ if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
+ stop_net();
+ fprintf(stderr, "tint2 : can't get systray manager\n");
+ return;
+ }
+
+ //fprintf(stderr, "tint2 : systray started\n");
+ XClientMessageEvent ev;
+ ev.type = ClientMessage;
+ ev.window = server.root_win;
+ ev.message_type = server.atom.MANAGER;
+ ev.format = 32;
+ ev.data.l[0] = CurrentTime;
+ ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
+ ev.data.l[2] = net_sel_win;
+ ev.data.l[3] = 0;
+ ev.data.l[4] = 0;
+ XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
+}
+
+
+void stop_net()
+{
+ //fprintf(stderr, "tint2 : systray stopped\n");
+ if (systray.list_icons) {
+ // remove_icon change systray.list_icons
+ while(systray.list_icons)
+ remove_icon((TrayWindow*)systray.list_icons->data);
+
+ g_slist_free(systray.list_icons);
+ systray.list_icons = 0;
+ }
+
+ if (net_sel_win != None) {
+ XDestroyWindow(server.dsp, net_sel_win);
+ net_sel_win = None;
+ }
+}
+
+
+gboolean error;
+int window_error_handler(Display *d, XErrorEvent *e)
+{
+ d=d;e=e;
+ error = TRUE;
+ if (e->error_code != BadWindow) {
+ printf("error_handler %d\n", e->error_code);
+ }
+ return 0;
+}
+
+
+static gint compare_traywindows(gconstpointer a, gconstpointer b)
+{
+ const TrayWindow * traywin_a = (TrayWindow*)a;
+ const TrayWindow * traywin_b = (TrayWindow*)b;
+ XTextProperty name_a, name_b;
+
+ if(XGetWMName(server.dsp, traywin_a->tray_id, &name_a) == 0) {
+ return -1;
+ }
+ else if(XGetWMName(server.dsp, traywin_b->tray_id, &name_b) == 0) {
+ XFree(name_a.value);
+ return 1;
+ }
+ else {
+ gint retval = g_ascii_strncasecmp((char*)name_a.value, (char*)name_b.value, -1) * systray.sort;
+ XFree(name_a.value);
+ XFree(name_b.value);
+ return retval;
+ }
+}
+
+
+gboolean add_icon(Window id)
+{
+ TrayWindow *traywin;
+ XErrorHandler old;
+ Panel *panel = systray.area.panel;
+ int hide = 0;
+
+ error = FALSE;
+ XWindowAttributes attr;
+ if ( XGetWindowAttributes(server.dsp, id, &attr) == False ) return FALSE;
+ unsigned long mask = 0;
+ XSetWindowAttributes set_attr;
+ Visual* visual = server.visual;
+ //printf("icon with depth: %d, width %d, height %d\n", attr.depth, attr.width, attr.height);
+ //printf("icon with depth: %d\n", attr.depth);
+ if (attr.depth != server.depth || systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0) {
+ visual = attr.visual;
+ set_attr.colormap = attr.colormap;
+ set_attr.background_pixel = 0;
+ set_attr.border_pixel = 0;
+ mask = CWColormap|CWBackPixel|CWBorderPixel;
+ }
+ else {
+ set_attr.background_pixmap = ParentRelative;
+ mask = CWBackPixmap;
+ }
+ Window parent_window;
+ parent_window = XCreateWindow(server.dsp, panel->main_win, 0, 0, 30, 30, 0, attr.depth, InputOutput, visual, mask, &set_attr);
+ old = XSetErrorHandler(window_error_handler);
+ XReparentWindow(server.dsp, id, parent_window, 0, 0);
+ XSync(server.dsp, False);
+ XSetErrorHandler(old);
+ if (error != FALSE) {
+ fprintf(stderr, "tint2 : not icon_swallow\n");
+ XDestroyWindow(server.dsp, parent_window);
+ return FALSE;
+ }
+
+ {
+ Atom acttype;
+ int actfmt;
+ unsigned long nbitem, bytes;
+ unsigned char *data = 0;
+ int ret;
+
+ ret = XGetWindowProperty(server.dsp, id, server.atom._XEMBED_INFO, 0, 2, False, server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data);
+ if (ret == Success) {
+ if (data) {
+ if (nbitem == 2) {
+ //hide = ((data[1] & XEMBED_MAPPED) == 0);
+ //printf("hide %d\n", hide);
+ }
+ XFree(data);
+ }
+ }
+ else {
+ fprintf(stderr, "tint2 : xembed error\n");
+ XDestroyWindow(server.dsp, parent_window);
+ return FALSE;
+ }
+ }
+ {
+ XEvent e;
+ e.xclient.type = ClientMessage;
+ e.xclient.serial = 0;
+ e.xclient.send_event = True;
+ e.xclient.message_type = server.atom._XEMBED;
+ e.xclient.window = id;
+ e.xclient.format = 32;
+ e.xclient.data.l[0] = CurrentTime;
+ e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
+ e.xclient.data.l[2] = 0;
+ e.xclient.data.l[3] = parent_window;
+ e.xclient.data.l[4] = 0;
+ XSendEvent(server.dsp, id, False, 0xFFFFFF, &e);
+ }
+
+ traywin = g_new0(TrayWindow, 1);
+ traywin->id = parent_window;
+ traywin->tray_id = id;
+ traywin->hide = hide;
+ traywin->depth = attr.depth;
+ traywin->damage = 0;
+
+ if (systray.area.on_screen == 0)
+ show(&systray.area);
+
+ if (systray.sort == 3)
+ systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
+ else if (systray.sort == 2)
+ systray.list_icons = g_slist_append(systray.list_icons, traywin);
+ else
+ systray.list_icons = g_slist_insert_sorted(systray.list_icons, traywin, compare_traywindows);
+ //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
+
+ // watch for the icon trying to resize itself!
+ XSelectInput(server.dsp, traywin->tray_id, StructureNotifyMask);
+ if (server.real_transparency || systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0) {
+ traywin->damage = XDamageCreate(server.dsp, traywin->id, XDamageReportRawRectangles);
+ XCompositeRedirectWindow(server.dsp, traywin->id, CompositeRedirectManual);
+ }
+
+ // show the window
+ if (!traywin->hide)
+ XMapWindow(server.dsp, traywin->tray_id);
+ if (!traywin->hide && !panel->is_hidden)
+ XMapRaised(server.dsp, traywin->id);
+
+ // changed in systray
+ systray.area.resize = 1;
+ panel_refresh = 1;
+ return TRUE;
+}
+
+
+void remove_icon(TrayWindow *traywin)
+{
+ XErrorHandler old;
+
+ // remove from our list
+ systray.list_icons = g_slist_remove(systray.list_icons, traywin);
+ //printf("remove_icon id %lx, %d\n", traywin->id);
+
+ XSelectInput(server.dsp, traywin->tray_id, NoEventMask);
+ if (traywin->damage)
+ XDamageDestroy(server.dsp, traywin->damage);
+
+ // reparent to root
+ error = FALSE;
+ old = XSetErrorHandler(window_error_handler);
+ if (!traywin->hide)
+ XUnmapWindow(server.dsp, traywin->tray_id);
+ XReparentWindow(server.dsp, traywin->tray_id, server.root_win, 0, 0);
+ XDestroyWindow(server.dsp, traywin->id);
+ XSync(server.dsp, False);
+ XSetErrorHandler(old);
+ if (traywin->render_timeout)
+ stop_timeout(traywin->render_timeout);
+ g_free(traywin);
+
+ // check empty systray
+ int count = 0;
+ GSList *l;
+ for (l = systray.list_icons; l ; l = l->next) {
+ if (!((TrayWindow*)l->data)->hide)
+ count++;
+ }
+ if (count == 0)
+ hide(&systray.area);
+
+ // changed in systray
+ systray.area.resize = 1;
+ panel_refresh = 1;
+}
+
+
+void net_message(XClientMessageEvent *e)