~p12/awn/master

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
/* Xlib utils */

/*
 * Copyright (C) 2001 Havoc Pennington
 *               2009 Michal Hruby <michal.mhr@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>

#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/shape.h>

#include "xutils.h"

enum {
    STRUT_LEFT = 0,
    STRUT_RIGHT = 1,
    STRUT_TOP = 2,
    STRUT_BOTTOM = 3,
    STRUT_LEFT_START = 4,
    STRUT_LEFT_END = 5,
    STRUT_RIGHT_START = 6,
    STRUT_RIGHT_END = 7,
    STRUT_TOP_START = 8,
    STRUT_TOP_END = 9,
    STRUT_BOTTOM_START = 10,
    STRUT_BOTTOM_END = 11
};

//pulled out of xutils.c
static Atom net_wm_strut              = 0;
static Atom net_wm_strut_partial      = 0;

/* xutils_set_strut:
 * @gdk_window: window to set struts on
 * @position: position of the window, top=0, right=1, bottom=2, left=3
 * @strut: the height of the strut
 * @strut_start: the start position of the strut
 * @strut_end: the end position of the strut
 *
 * Sets struts for a given @gdk_window on the specified @position.
 * To let a maximised window not cover the given struts
 *
 * - partially pulled out of xutils.c -
 */
void
xutils_set_strut(GdkWindow*        gdk_window,
                 GtkPositionType   position,
                 guint32           strut,
                 guint32           strut_start,
                 guint32           strut_end)
{
    Display* display;
    Window   window;
    gulong   struts [12] = { 0, };

    g_return_if_fail(GDK_IS_WINDOW(gdk_window));

    display = GDK_WINDOW_XDISPLAY(gdk_window);
    window  = GDK_WINDOW_XWINDOW(gdk_window);

    if (net_wm_strut == None) {
        net_wm_strut = XInternAtom(display, "_NET_WM_STRUT", False);
    }
    if (net_wm_strut_partial == None) {
        net_wm_strut_partial = XInternAtom(display, "_NET_WM_STRUT_PARTIAL", False);
    }

    switch (position) {
    case GTK_POS_TOP:
        struts [STRUT_TOP] = strut;
        struts [STRUT_TOP_START] = strut_start;
        struts [STRUT_TOP_END] = strut_end;
        break;
    case GTK_POS_RIGHT:
        struts [STRUT_RIGHT] = strut;
        struts [STRUT_RIGHT_START] = strut_start;
        struts [STRUT_RIGHT_END] = strut_end;
        break;
    case GTK_POS_BOTTOM:
        struts [STRUT_BOTTOM] = strut;
        struts [STRUT_BOTTOM_START] = strut_start;
        struts [STRUT_BOTTOM_END] = strut_end;
        break;
    case GTK_POS_LEFT:
        struts [STRUT_LEFT] = strut;
        struts [STRUT_LEFT_START] = strut_start;
        struts [STRUT_LEFT_END] = strut_end;
        break;
    }

    gdk_error_trap_push();
    XChangeProperty(display, window, net_wm_strut,
                    XA_CARDINAL, 32, PropModeReplace,
                    (guchar*) &struts, 4);
    XChangeProperty(display, window, net_wm_strut_partial,
                    XA_CARDINAL, 32, PropModeReplace,
                    (guchar*) &struts, 12);
    gdk_error_trap_pop();
}

GdkRegion*
xutils_get_input_shape(GdkWindow* window)
{
    GdkRegion* region = gdk_region_new();

    GdkRectangle rect;
    XRectangle* rects;
    int count = 0;
    int ordering = 0;

    gdk_error_trap_push();
    rects = XShapeGetRectangles(GDK_WINDOW_XDISPLAY(window),
                                GDK_WINDOW_XID(window),
                                ShapeInput, &count, &ordering);
    gdk_error_trap_pop();

    for (int i = 0; i < count; ++i) {
        rect.x = rects[i].x;
        rect.y = rects[i].y;
        rect.width = rects[i].width;
        rect.height = rects[i].height;

        gdk_region_union_with_rect(region, &rect);
    }
    if (rects) {
        free(rects);
    }

    return region;
}

GdkWindow*
xutils_get_window_at_pointer(GdkDisplay* display)
{
    GdkWindow* result = NULL;
    Window root;
    Window child;
    int root_x;
    int root_y;
    int win_x;
    int win_y;
    unsigned int flags;

    Bool res = XQueryPointer(GDK_DISPLAY_XDISPLAY(display),
                             GDK_ROOT_WINDOW(), &root, &child,
                             &root_x, &root_y, &win_x, &win_y, &flags);
    if (!res) {
        return NULL;
    }

    result = gdk_window_lookup_for_display(display, child);
    if (!result && child != None) {
        result = gdk_window_foreign_new(child);
    }

    return result;
}

gboolean
xutils_is_window_minimized(GdkWindow* window)
{
    gboolean result = FALSE;

    g_return_val_if_fail(GDK_IS_WINDOW(window), FALSE);

    Atom type;
    int format;
    gulong nitems;
    gulong bytes_after;
    gulong* num;
    Atom wm_state = gdk_x11_atom_to_xatom(
                        gdk_atom_intern_static_string("WM_STATE"));

    gdk_error_trap_push();
    type = None;
    XGetWindowProperty(GDK_WINDOW_XDISPLAY(window),
                       GDK_WINDOW_XWINDOW(window),
                       wm_state,
                       0, G_MAXLONG,
                       False, wm_state, &type, &format, &nitems,
                       &bytes_after, (void*)&num);

    if (gdk_error_trap_pop()) {
        return FALSE;
    }

    if (type != wm_state) {
        XFree(num);
        return FALSE;
    }

    result = *num == IconicState;

    XFree(num);

    return result;
}