summaryrefslogtreecommitdiff
path: root/src/Xinerama.c
blob: 93ea32c5c10327927dc94eac240e615589529502 (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
296
297
298
299
300
301
302
303
304
305
/* $XdotOrg: xc/lib/Xinerama/Xinerama.c,v 1.1.4.1 2003/12/18 19:29:12 kaleb Exp $ */
/*************************************************************************
*
* Copyright (c) 1999,2002 Hewlett-Packard Company
* Copyright (c) 1999,2002 IBM Corporation
* Copyright (c) 1999,2002 Hummingbird Communications Ltd.
* Copyright (c) 1999,2002 Silicon Graphics, Inc.
* Copyright (c) 1999,2002 Sun Microsystems, Inc.
* Copyright (c) 1999,2002 The Open Group
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a 
* copy of this software and associated documentation files (the "Software"), 
* to deal in the Software without restriction, including without limitation 
* the rights to use, copy, modify, merge, publish, distribute, and/or sell 
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, provided that the above copyright notice(s) and this 
* permission notice appear in all copies of the Software and that both the 
* above copyright notice(s) and this permission notice appear in supporting 
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR
* ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
* Except as contained in this notice, the name of a copyright holder shall 
* not be used in advertising or otherwise to promote the sale, use or other 
* dealings in this Software without prior written authorization of the 
* copyright holder.
*
*************************************************************************/

#define NEED_EVENTS
#define NEED_REPLIES
#include <stdio.h>
#include <X11/Xlibint.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xext.h>
#include <X11/extensions/extutil.h>
#include <X11/extensions/xineramaProto.h>
#include <X11/extensions/Xinerama.h>


static XExtensionInfo _xinerama_ext_info_data;
static XExtensionInfo *xinerama_ext_info = &_xinerama_ext_info_data;
static char *xinerama_extension_name = XINERAMA_PROTOCOL_NAME;

#define XineramaCheckExtension(dpy,i,val) \
  XextCheckExtension (dpy, i, xinerama_extension_name, val)

static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xinerama_ext_info)

static XExtensionHooks xinerama_extension_hooks = {
    NULL,                               /* create_gc */
    NULL,                               /* copy_gc */
    NULL,                               /* flush_gc */
    NULL,                               /* free_gc */
    NULL,                               /* create_font */
    NULL,                               /* free_font */
    close_display,                      /* close_display */
    NULL,                               /* wire_to_event */
    NULL,                               /* event_to_wire */
    NULL,                               /* error */
    NULL,                               /* error_string */
};

static XEXT_GENERATE_FIND_DISPLAY (find_display, xinerama_ext_info,
                                   xinerama_extension_name, 
                                   &xinerama_extension_hooks,
                                   0, NULL)

Bool XineramaQueryExtension (
    Display *dpy,
    int *event_basep,
    int *error_basep
)
{
    XExtDisplayInfo *info = find_display (dpy);

    if (XextHasExtension(info)) {
        *event_basep = info->codes->first_event;
        *error_basep = info->codes->first_error;
        return True;
    } else {
        return False;
    }
}

Status XineramaQueryVersion(
    Display *dpy,
    int *major_version, 
    int *minor_version
)
{
    XExtDisplayInfo *info = find_display (dpy);
    xXineramaQueryVersionReq *req;
    xXineramaQueryVersionReply rep;

    XineramaCheckExtension (dpy, info, 0);

    LockDisplay (dpy);
    GetReq (XineramaQueryVersion, req);
    req->reqType = info->codes->major_opcode;
    req->XineramaReqType = X_XineramaQueryVersion;
    req->client_major_version = XINERAMA_MAJOR_VERSION;
    req->client_minor_version = XINERAMA_MINOR_VERSION;
    if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
        UnlockDisplay (dpy);
        SyncHandle ();
        return 0;
    }
    *major_version = rep.server_major_version;
    *minor_version = rep.server_minor_version;
    UnlockDisplay (dpy);
    SyncHandle ();
    return 1;
}

Bool XineramaActive (
    Display *dpy,
    Window window 
)
{
    XExtDisplayInfo *info = find_display (dpy);
    xXineramaActiveReply rep;
    xXineramaActiveReq *req;
    Bool active;

    XineramaCheckExtension (dpy, info, False);

    LockDisplay (dpy);
    GetReq (XineramaActive, req);
    req->reqType = info->codes->major_opcode;
    req->XineramaReqType = X_XineramaActive;
    req->window = window;
    if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
        UnlockDisplay (dpy);
        SyncHandle ();
        return False;
    }
    active = rep.active;
    UnlockDisplay (dpy);
    SyncHandle ();
    return active;
}

#ifndef XNOXINERAMABC
Bool XineramaIsActive(Display *dpy)
{
    return XineramaActive(dpy, DefaultRootWindow(dpy));
}
#endif


Status XineramaGetData(
    Display *dpy,
    Window window,
    XRectangle **framebuffer_rects,
    int *number_framebuffers
)
{
    XExtDisplayInfo *info = find_display (dpy);
    xXineramaGetDataReq *req;
    xXineramaGetDataReply rep;
    XRectangle *rects;
    int result = 0;

    *number_framebuffers = 0;
    *framebuffer_rects = NULL;

    XineramaCheckExtension (dpy, info, 0);

    LockDisplay (dpy);
    GetReq (XineramaGetData, req);
    req->reqType = info->codes->major_opcode;
    req->XineramaReqType = X_XineramaGetData;
    req->window = window;
    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) {
        UnlockDisplay (dpy);
        SyncHandle ();
        return 0;
    }

    if(rep.num_rects) {
        if((rects = Xmalloc(sizeof(XRectangle) * rep.num_rects))) {
            xXineramaRectangle scratch;
            int i;

            for(i = 0; i < rep.num_rects; i++) {
                _XRead(dpy, (char*)&scratch, sz_xXineramaRectangle);
                rects[i].x = scratch.x;
                rects[i].y = scratch.y;
                rects[i].width = scratch.width;
                rects[i].height = scratch.height;
            }
            *framebuffer_rects = rects;
            *number_framebuffers = rep.num_rects;
            result = 1;
        } else {
            _XEatData(dpy, rep.length << 2);
        }
    }

    UnlockDisplay (dpy);
    SyncHandle ();
    return result;
}

#ifndef XNOXINERAMABC
XineramaScreenInfo *XineramaQueryScreens(Display *dpy, int *number)
{
    XRectangle *screens = NULL;
    if (XineramaGetData(dpy, DefaultRootWindow(dpy), &screens, number)) {
	if (*number) {
	    XineramaScreenInfo *scrnInfo = NULL;
	    if ((scrnInfo = Xmalloc(sizeof(XineramaScreenInfo) * *number))) {
		int i;
		for (i = 0; i < *number; i++) {
		    scrnInfo[i].screen_number = i;
		    scrnInfo[i].x_org = screens[i].x;
		    scrnInfo[i].y_org = screens[i].y;
		    scrnInfo[i].width = screens[i].width;
		    scrnInfo[i].height = screens[i].height;
		}
		XFree(screens);
		return scrnInfo;
	    }
	}
    }
    return NULL;
}
#endif

#define HINT_NAME "XINERAMA_CENTER_HINT"
#define HINT_TYPE "INTEGER"

Status XineramaGetCenterHint (
    Display *dpy,
    Window root,
    int *x,
    int *y
)
{
    XExtDisplayInfo *info = find_display (dpy);
    Atom atom, type, actual_type;
    unsigned long nitems, bytes_after;
    int actual_format, status;
    short *prop;

    XineramaCheckExtension (dpy, info, 0);

    type = XInternAtom(dpy, HINT_TYPE, False);
    atom = XInternAtom(dpy, HINT_NAME, True);

    if(atom == None) return 0;  /* no such atom */

    if( (status = XGetWindowProperty(dpy, root, atom, 0, 1, False, type, 
				     &actual_type, &actual_format, &nitems, 
				     &bytes_after, (unsigned char**)(&prop))) 
	!= Success)
	return 0;

    if(actual_type == None) return 0;  /* no such property */

    *x = prop[0];
    *y = prop[1];

    if (prop) XFree(prop);

    return 1;
}


Status XineramaSetCenterHint (
    Display *dpy,
    Window root,
    int x,
    int y
)
{
    XExtDisplayInfo *info = find_display (dpy);
    Atom atom, type;
    short data[2];

    XineramaCheckExtension (dpy, info, 0);

    atom = XInternAtom(dpy, HINT_NAME, False);
    type = XInternAtom(dpy, HINT_TYPE, False);

    data[0] = x;
    data[1] = y;

    return XChangeProperty(dpy, root, atom, type, 16, PropModeReplace, 
                           (unsigned char*)data, 2);
}