aboutsummaryrefslogtreecommitdiffstats
path: root/applets/showdesktop/showdesktop.c
blob: 33c13e179855e02d5c802490845c57c7c143cef1 (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
/* 
 * (C) 2006 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * Licensed under the GPL v2 or greater.
 */

#include <config.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include <matchbox-panel/mb-panel.h>
#include <matchbox-panel/mb-panel-scaling-image.h>

typedef struct {
        GtkButton *button;
        MBPanelScalingImage *image;

        gboolean active;

        Atom atom;
        
        GdkWindow *root_window;
} ShowDesktopApplet;

static GdkFilterReturn
filter_func (GdkXEvent         *xevent,
             GdkEvent          *event,
             ShowDesktopApplet *applet);

static void
show_desktop_applet_free (ShowDesktopApplet *applet)
{
        if (applet->root_window) {
                gdk_window_remove_filter (applet->root_window,
                                          (GdkFilterFunc) filter_func,
                                          applet);
        }

        g_slice_free (ShowDesktopApplet, applet);
}

/* Set @applets state to @active */
static void
set_active (ShowDesktopApplet *applet, gboolean active)
{
        const char *icon;

        applet->active = active;

        /* TODO: remove this function and instead use a toggle button? */
        
        icon = "panel-user-desktop";

        mb_panel_scaling_image_set_icon (applet->image, icon);
}

/* Sync @applet with the _NET_SHOWING_DESKTOP root window property */
static void
sync_applet (ShowDesktopApplet *applet)
{
        GdkDisplay *display;
        Atom type;
        int format, result;
        gulong nitems, bytes_after, *num;

        display = gtk_widget_get_display (GTK_WIDGET (applet->button));

        type = 0;

        gdk_error_trap_push ();
        result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
                                     GDK_WINDOW_XWINDOW
                                            (applet->root_window),
                                     applet->atom,
                                     0,
                                     G_MAXLONG,
                                     False,
                                     XA_CARDINAL,
                                     &type,
                                     &format,
                                     &nitems,
                                     &bytes_after,
                                     (gpointer) &num);
        if (!gdk_error_trap_pop () && result == Success) {
                if (type == XA_CARDINAL && nitems > 0)
                          set_active (applet, *num);

                XFree (num);
        }
}

/* Something happened on the root window */
static GdkFilterReturn
filter_func (GdkXEvent         *xevent,
             GdkEvent          *event,
             ShowDesktopApplet *applet)
{
        XEvent *xev;

        xev = (XEvent *) xevent;

        if (xev->type == PropertyNotify) {
                if (xev->xproperty.atom == applet->atom) {
                        /* _NET_SHOWING_DESKTOP changed */
                        sync_applet (applet);
                }
        }

        return GDK_FILTER_CONTINUE;
}

/* Screen changed */
static void
screen_changed_cb (GtkWidget         *button,
                   GdkScreen         *old_screen,
                   ShowDesktopApplet *applet)
{
        GdkScreen *screen;
        GdkDisplay *display;
        GdkEventMask events;

        if (applet->root_window) {
                gdk_window_remove_filter (applet->root_window,
                                          (GdkFilterFunc) filter_func,
                                          applet);
        }

        screen = gtk_widget_get_screen (button);
        display = gdk_screen_get_display (screen);

        applet->atom = gdk_x11_get_xatom_by_name_for_display
                                (display, "_NET_SHOWING_DESKTOP");
        applet->root_window = gdk_screen_get_root_window (screen);

        /* Watch _NET_SHOWING_DESKTOP */
        events = gdk_window_get_events (applet->root_window);
        if ((events & GDK_PROPERTY_CHANGE_MASK) == 0) {
                gdk_window_set_events (applet->root_window,
                                       events & GDK_PROPERTY_CHANGE_MASK);
        }

        gdk_window_add_filter (applet->root_window,
                               (GdkFilterFunc) filter_func,
                               applet);

        /* Sync */
        sync_applet (applet);
}

/* Button clicked */
static void
button_clicked_cb (GtkButton         *button,
                   ShowDesktopApplet *applet)
{
        GtkWidget *widget;
        Screen *screen;
        XEvent xev;

        widget = GTK_WIDGET (button);
        screen = GDK_SCREEN_XSCREEN (gtk_widget_get_screen (widget));
  
        xev.xclient.type = ClientMessage;
        xev.xclient.serial = 0;
        xev.xclient.send_event = True;
        xev.xclient.display = DisplayOfScreen (screen);
        xev.xclient.window = RootWindowOfScreen (screen);
        xev.xclient.message_type = applet->atom; 
        xev.xclient.format = 32;
        xev.xclient.data.l[0] = !applet->active;
        xev.xclient.data.l[1] = 0;
        xev.xclient.data.l[2] = 0;
        xev.xclient.data.l[3] = 0;
        xev.xclient.data.l[4] = 0;

        XSendEvent (DisplayOfScreen (screen),
	            RootWindowOfScreen (screen),
                    False,
	            SubstructureRedirectMask | SubstructureNotifyMask,
	            &xev);
}

static void
realize_cb (GtkWidget *button, ShowDesktopApplet *applet)
{
        sync_applet (applet);
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create (const char    *id,
                        GtkOrientation orientation)
{
        ShowDesktopApplet *applet;
        GtkWidget *button, *image;

        /* Create applet data structure */
        applet = g_slice_new0 (ShowDesktopApplet);

        applet->root_window = NULL;

        /* Create button */
        button = gtk_button_new ();
        applet->button = GTK_BUTTON (button);

        gtk_button_set_relief (applet->button, GTK_RELIEF_NONE);

        gtk_widget_set_can_focus (button, FALSE);

        gtk_widget_set_name (button, "MatchboxPanelShowDesktop");

        image = mb_panel_scaling_image_new (orientation, NULL);
        applet->image = MB_PANEL_SCALING_IMAGE (image);

        gtk_container_add (GTK_CONTAINER (button), image);

        g_signal_connect (button,
                          "screen-changed",
                          G_CALLBACK (screen_changed_cb),
                          applet);
        g_signal_connect (button,
                          "clicked",
                          G_CALLBACK (button_clicked_cb),
                          applet);
        g_signal_connect (button,
                          "realize",
                          G_CALLBACK (realize_cb),
                          applet);

        g_object_weak_ref (G_OBJECT (button),
                           (GWeakNotify) show_desktop_applet_free,
                           applet);

        /* Show! */
        gtk_widget_show_all (button);

        return button;
};