summaryrefslogtreecommitdiff
path: root/src/misc.c
blob: 67b3d3a9839eb963525a7c3fba41c3792166d560 (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
/*      $Id$

        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., 675 Mass Ave, Cambridge, MA 02139, USA.

        oroborus - (c) 2001 Ken Lynch
        xfwm4    - (c) 2002-2006 Olivier Fourdan

 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmd.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <libxfce4util/libxfce4util.h>

#include "display.h"
#include "screen.h"
#include "mywindow.h"
#include "client.h"
#include "misc.h"

unsigned int
getMouseXY (ScreenInfo *screen_info, Window w, int *x2, int *y2)
{
    Window w1, w2;
    unsigned int mask;
    int x1, y1;

    TRACE ("entering getMouseXY");

    XQueryPointer (myScreenGetXDisplay (screen_info), w, &w1, &w2, &x1, &y1, x2, y2, &mask);
    return mask;
}

Window
getMouseWindow (ScreenInfo *screen_info, Window w)
{
    Window w1, w2;
    unsigned int mask;
    int x1, y1, x2, y2;

    TRACE ("entering getMouseWindow");

    XQueryPointer (myScreenGetXDisplay (screen_info), w, &w1, &w2, &x1, &y1, &x2, &y2, &mask);
    return w2;
}

GC
createGC (ScreenInfo *screen_info, char *col, int func, XFontStruct * font,
          int line_width, gboolean inc_sw)
{
    XGCValues gv;
    XColor xc1, xc2;
    GC gc;
    int mask;

    TRACE ("entering createGC");
    TRACE ("color=%s", col);

    mask = GCForeground | GCFunction;
    XAllocNamedColor (myScreenGetXDisplay (screen_info), screen_info->cmap, col, &xc1, &xc2);
    gv.foreground = xc2.pixel;
    gv.function = func;
    if (font)
    {
        gv.font = font->fid;
        mask = mask | GCFont;
    }
    if (inc_sw)
    {
        gv.subwindow_mode = IncludeInferiors;
        mask = mask | GCSubwindowMode;
    }
    if (line_width > -1)
    {
        gv.line_width = line_width;
        mask = mask | GCLineWidth;
    }
    gc = XCreateGC (myScreenGetXDisplay (screen_info), screen_info->xroot, mask, &gv);
    return gc;
}

void
sendClientMessage (ScreenInfo *screen_info, Window w, int atom_id, Time timestamp)
{
    DisplayInfo *display_info;
    XClientMessageEvent ev;

    g_return_if_fail ((atom_id > 0) && (atom_id < ATOM_COUNT));
    TRACE ("entering sendClientMessage");

    display_info = screen_info->display_info;
    ev.type = ClientMessage;
    ev.window = w;
    ev.message_type = display_info->atoms[WM_PROTOCOLS];
    ev.format = 32;
    ev.send_event = TRUE;
    ev.data.l[0] = display_info->atoms[atom_id];
    ev.data.l[1] = timestamp;
    XSendEvent (myScreenGetXDisplay (screen_info), w, FALSE, 0L, (XEvent *)&ev);
}

void
sendRootMessage (ScreenInfo *screen_info, int atom_id, long value, Time timestamp)
{
    DisplayInfo *display_info;
    XClientMessageEvent ev;

    g_return_if_fail ((atom_id > 0) && (atom_id < ATOM_COUNT));
    TRACE ("entering sendClientMessage");

    display_info = screen_info->display_info;
    ev.type = ClientMessage;
    ev.window = screen_info->xroot;
    ev.message_type = display_info->atoms[atom_id];
    ev.format = 32;
    ev.data.l[0] = value;
    ev.data.l[1] = timestamp;
    XSendEvent (myScreenGetXDisplay (screen_info), screen_info->xroot, FALSE,
                SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&ev);
}

/*
 * it's safer to grab the display before calling this routine
 * Returns true if the given window is present and mapped on root
 */
gboolean
checkWindowOnRoot(ScreenInfo *screen_info, Window w)
{
    DisplayInfo *display_info;
    Window dummy_root, parent;
    Window *wins;
    Status test;
    unsigned int count;

    g_return_val_if_fail (screen_info != NULL, FALSE);
    g_return_val_if_fail (w != None, FALSE);

    display_info = screen_info->display_info;
    wins = NULL;

    gdk_error_trap_push ();
    test = XQueryTree(display_info->dpy, w, &dummy_root, &parent, &wins, &count);
    if (wins)
    {
        XFree (wins);
    }
    return (!gdk_error_trap_pop () && (test != 0) && (dummy_root == parent));
}

void
placeSidewalks(ScreenInfo *screen_info, gboolean activate)
{
    NetWmDesktopLayout l;

    g_return_if_fail (MYWINDOW_XWINDOW (screen_info->sidewalk[0]) != None);
    g_return_if_fail (MYWINDOW_XWINDOW (screen_info->sidewalk[1]) != None);
    g_return_if_fail (MYWINDOW_XWINDOW (screen_info->sidewalk[2]) != None);
    g_return_if_fail (MYWINDOW_XWINDOW (screen_info->sidewalk[3]) != None);

    l = screen_info->desktop_layout;
    if ((activate) && (l.cols > 1))
    {
        /*left*/
        xfwmWindowShow (&screen_info->sidewalk[0],
                        0, 0,
                        1, screen_info->height, FALSE);

        /*right*/
         xfwmWindowShow (&screen_info->sidewalk[1],
                        screen_info->width - 1, 0,
                        1, screen_info->height, FALSE);
    }
    else /* Place the windows off screen */
    {
        /*left*/
        xfwmWindowShow (&screen_info->sidewalk[0],
                        -1, 0,
                        1, screen_info->height, FALSE);

        /*right*/
        xfwmWindowShow (&screen_info->sidewalk[1],
                        screen_info->width, 0,
                        1, screen_info->height, FALSE);
    }

    if ((activate) && (l.rows > 1))
    {
        /*top*/
        xfwmWindowShow (&screen_info->sidewalk[2],
                        0, 0,
                        screen_info->width, 1, FALSE);

        /*bottom*/
        xfwmWindowShow (&screen_info->sidewalk[3],
                        0, screen_info->height - 1,
                        screen_info->width, 1, FALSE);
    }
    else /* Place the windows off screen */
    {
        /*top*/
        xfwmWindowShow (&screen_info->sidewalk[2],
                        0, -1,
                        screen_info->width, 1, FALSE);

        /*bottom*/
        xfwmWindowShow (&screen_info->sidewalk[3],
                        0, screen_info->height,
                        screen_info->width, 1, FALSE);
    }
}

/*
   gdk_screen_get_monitor_at_point () doesn't give accurate results
   when the point is off screen, use my own implementation from xfce 3
 */
gint
find_monitor_at_point (GdkScreen *screen, gint x, gint y)
{
    static gint cache_monitor = -1;
    static gint cache_x, cache_y;
    gint dx, dy, center_x, center_y;
    guint32 distsquare, min_distsquare;
    gint num_monitors, nearest_monitor, i;
    GdkRectangle monitor;

    g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);

    /* Cache system */
    if ((cache_monitor >= 0) && (x == cache_x) && (y == cache_y))
    {
        return (cache_monitor);
    }

    cache_x = x;
    cache_y = y;

    /* No monitor has been eligible, use the closest one */

    min_distsquare = ((guint32) 0xffffffff);
    nearest_monitor = 0;

    num_monitors = gdk_screen_get_n_monitors (screen);
    for (i = 0; i < num_monitors; i++)
    {
        gdk_screen_get_monitor_geometry (screen, i, &monitor);

        if ((x >= monitor.x) && (x < monitor.x + monitor.width) &&
            (y >= monitor.y) && (y < (monitor.y + monitor.height)))
        {
            cache_monitor = i;
            return i;
        }

        center_x = monitor.x + (monitor.width / 2);
        center_y = monitor.y + (monitor.height / 2);

        dx = x - center_x;
        dy = y - center_y;

        distsquare = (dx * dx) + (dy * dy);

        if (distsquare < min_distsquare)
        {
            min_distsquare = distsquare;
            nearest_monitor = i;
        }
    }

    cache_monitor = nearest_monitor;
    return (nearest_monitor);
}