summaryrefslogtreecommitdiff
path: root/stickynotes/util.c
blob: d13f9f41d12e9211d501a8fd76557667ea97b770 (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
/* Sticky Notes
 * Copyright (C) 2002-2003 Loban A Rahman
 *
 * 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 "util.h"

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

static Atom
xstuff_atom_get (const char *atom_name)
{
    static GHashTable *atom_hash;
    Display           *xdisplay;
    Atom               retval;

    g_return_val_if_fail (atom_name != NULL, None);

    xdisplay = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());

    if (!atom_hash)
        atom_hash = g_hash_table_new_full (g_str_hash,
                                           g_str_equal,
                                           g_free, NULL);

    retval = GPOINTER_TO_UINT (g_hash_table_lookup (atom_hash, atom_name));
    if (!retval) {
        retval = XInternAtom (xdisplay, atom_name, FALSE);

        if (retval != None)
            g_hash_table_insert (atom_hash, g_strdup (atom_name),
                                 GUINT_TO_POINTER (retval));
    }

    return retval;
}

int
xstuff_get_current_workspace (GtkWindow *window)
{
#ifdef GDK_WINDOWING_X11
    Window      root_window;
    Atom        type = None;
    gulong      nitems;
    gulong      bytes_after;
    gulong     *num;
    int         format;
    int         result;
    int         retval;
    GdkDisplay *gdk_display;
    Display    *xdisplay;

    gdk_display = gdk_display_get_default ();
    if  (!GDK_IS_X11_DISPLAY (gdk_display))
        return -1;

    root_window = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
    xdisplay = GDK_DISPLAY_XDISPLAY (gdk_display);

    gdk_x11_display_error_trap_push (gdk_display);
    result = XGetWindowProperty (xdisplay,
                                 root_window,
                                 xstuff_atom_get ("_NET_CURRENT_DESKTOP"),
                                 0, G_MAXLONG,
                                 False, XA_CARDINAL,
                                 &type, &format, &nitems,
                                 &bytes_after, (gpointer) &num);
    if (gdk_x11_display_error_trap_pop (gdk_display) || result != Success)
        return -1;

    if (type != XA_CARDINAL) {
        XFree (num);
        return -1;
    }

    retval = *num;

    XFree (num);

    return retval;
#else
    return -1;
#endif
}
void
xstuff_change_workspace (GtkWindow *window,
                         int        new_space)
{
    XEvent xev;
    Window xwindow;
    Display *gdk_display;
    Screen *screen;

    gdk_display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
    xwindow = GDK_WINDOW_XID (GDK_WINDOW (gtk_widget_get_window (GTK_WIDGET (window))));
    screen = GDK_SCREEN_XSCREEN (gtk_widget_get_screen (GTK_WIDGET (window)));

    xev.xclient.type = ClientMessage;
    xev.xclient.serial = 0;
    xev.xclient.send_event = True;
    xev.xclient.display = gdk_display;
    xev.xclient.window = xwindow;
    xev.xclient.message_type = xstuff_atom_get ("_NET_WM_DESKTOP");
    xev.xclient.format = 32;
    xev.xclient.data.l[0] = new_space;
    xev.xclient.data.l[1] = 0;
    xev.xclient.data.l[2] = 0;

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