XrrOutput.c revision 8bd17e5f
1b042e37fSmrg/*
2b042e37fSmrg * Copyright © 2006 Keith Packard
38c4a8e55Smrg * Copyright © 2008 Red Hat, Inc.
4b042e37fSmrg *
5b042e37fSmrg * Permission to use, copy, modify, distribute, and sell this software and its
6b042e37fSmrg * documentation for any purpose is hereby granted without fee, provided that
7b042e37fSmrg * the above copyright notice appear in all copies and that both that copyright
8b042e37fSmrg * notice and this permission notice appear in supporting documentation, and
9b042e37fSmrg * that the name of the copyright holders not be used in advertising or
10b042e37fSmrg * publicity pertaining to distribution of the software without specific,
11b042e37fSmrg * written prior permission.  The copyright holders make no representations
12b042e37fSmrg * about the suitability of this software for any purpose.  It is provided "as
13b042e37fSmrg * is" without express or implied warranty.
14b042e37fSmrg *
15b042e37fSmrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16b042e37fSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17b042e37fSmrg * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18b042e37fSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19b042e37fSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20b042e37fSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21b042e37fSmrg * OF THIS SOFTWARE.
22b042e37fSmrg */
23b042e37fSmrg
24b042e37fSmrg#ifdef HAVE_CONFIG_H
25b042e37fSmrg#include <config.h>
26b042e37fSmrg#endif
27b042e37fSmrg
28b042e37fSmrg#include <stdio.h>
29b042e37fSmrg#include <X11/Xlib.h>
30b042e37fSmrg/* we need to be able to manipulate the Display structure on events */
31b042e37fSmrg#include <X11/Xlibint.h>
32b042e37fSmrg#include <X11/extensions/render.h>
33b042e37fSmrg#include <X11/extensions/Xrender.h>
34b042e37fSmrg#include "Xrandrint.h"
35b042e37fSmrg
36b042e37fSmrg#define OutputInfoExtra	(SIZEOF(xRRGetOutputInfoReply) - 32)
370597fb56Smrg
38b042e37fSmrgXRROutputInfo *
39b042e37fSmrgXRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output)
40b042e37fSmrg{
41b042e37fSmrg    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
42b042e37fSmrg    xRRGetOutputInfoReply	rep;
43b042e37fSmrg    xRRGetOutputInfoReq		*req;
44b042e37fSmrg    int				nbytes, nbytesRead, rbytes;
45b042e37fSmrg    XRROutputInfo		*xoi;
46b042e37fSmrg
478c4a8e55Smrg    RRCheckExtension (dpy, info, NULL);
48b042e37fSmrg
49b042e37fSmrg    LockDisplay (dpy);
50b042e37fSmrg    GetReq (RRGetOutputInfo, req);
51b042e37fSmrg    req->reqType = info->codes->major_opcode;
52b042e37fSmrg    req->randrReqType = X_RRGetOutputInfo;
53b042e37fSmrg    req->output = output;
54b042e37fSmrg    req->configTimestamp = resources->configTimestamp;
55b042e37fSmrg
56b042e37fSmrg    if (!_XReply (dpy, (xReply *) &rep, OutputInfoExtra >> 2, xFalse))
57b042e37fSmrg    {
58b042e37fSmrg	UnlockDisplay (dpy);
59b042e37fSmrg	SyncHandle ();
60b042e37fSmrg	return NULL;
61b042e37fSmrg    }
62b042e37fSmrg
63b042e37fSmrg    nbytes = ((long) (rep.length) << 2) - OutputInfoExtra;
64b042e37fSmrg
65b042e37fSmrg    nbytesRead = (long) (rep.nCrtcs * 4 +
66b042e37fSmrg			 rep.nModes * 4 +
67b042e37fSmrg			 rep.nClones * 4 +
68b042e37fSmrg			 ((rep.nameLength + 3) & ~3));
69b042e37fSmrg
700597fb56Smrg    /*
710597fb56Smrg     * first we must compute how much space to allocate for
72b042e37fSmrg     * randr library's use; we'll allocate the structures in a single
73b042e37fSmrg     * allocation, on cleanlyness grounds.
74b042e37fSmrg     */
75b042e37fSmrg
76b042e37fSmrg    rbytes = (sizeof (XRROutputInfo) +
77b042e37fSmrg	      rep.nCrtcs * sizeof (RRCrtc) +
78b042e37fSmrg	      rep.nModes * sizeof (RRMode) +
79b042e37fSmrg	      rep.nClones * sizeof (RROutput) +
80b042e37fSmrg	      rep.nameLength + 1);	    /* '\0' terminate name */
81b042e37fSmrg
82b042e37fSmrg    xoi = (XRROutputInfo *) Xmalloc(rbytes);
83b042e37fSmrg    if (xoi == NULL) {
848bd17e5fSmrg	_XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2));
85b042e37fSmrg	UnlockDisplay (dpy);
86b042e37fSmrg	SyncHandle ();
87b042e37fSmrg	return NULL;
88b042e37fSmrg    }
89b042e37fSmrg
90b042e37fSmrg    xoi->timestamp = rep.timestamp;
91b042e37fSmrg    xoi->crtc = rep.crtc;
92b042e37fSmrg    xoi->mm_width = rep.mmWidth;
93b042e37fSmrg    xoi->mm_height = rep.mmHeight;
94b042e37fSmrg    xoi->connection = rep.connection;
95b042e37fSmrg    xoi->subpixel_order = rep.subpixelOrder;
96b042e37fSmrg    xoi->ncrtc = rep.nCrtcs;
97b042e37fSmrg    xoi->crtcs = (RRCrtc *) (xoi + 1);
98b042e37fSmrg    xoi->nmode = rep.nModes;
99b042e37fSmrg    xoi->npreferred = rep.nPreferred;
100b042e37fSmrg    xoi->modes = (RRMode *) (xoi->crtcs + rep.nCrtcs);
101b042e37fSmrg    xoi->nclone = rep.nClones;
102b042e37fSmrg    xoi->clones = (RROutput *) (xoi->modes + rep.nModes);
103b042e37fSmrg    xoi->name = (char *) (xoi->clones + rep.nClones);
104b042e37fSmrg
105b042e37fSmrg    _XRead32 (dpy, xoi->crtcs, rep.nCrtcs << 2);
106b042e37fSmrg    _XRead32 (dpy, xoi->modes, rep.nModes << 2);
107b042e37fSmrg    _XRead32 (dpy, xoi->clones, rep.nClones << 2);
1080597fb56Smrg
109b042e37fSmrg    /*
110b042e37fSmrg     * Read name and '\0' terminate
111b042e37fSmrg     */
112b042e37fSmrg    _XReadPad (dpy, xoi->name, rep.nameLength);
113b042e37fSmrg    xoi->name[rep.nameLength] = '\0';
1140597fb56Smrg    xoi->nameLen = rep.nameLength;
1150597fb56Smrg
116b042e37fSmrg    /*
117b042e37fSmrg     * Skip any extra data
118b042e37fSmrg     */
119b042e37fSmrg    if (nbytes > nbytesRead)
120b042e37fSmrg	_XEatData (dpy, (unsigned long) (nbytes - nbytesRead));
1210597fb56Smrg
122b042e37fSmrg    UnlockDisplay (dpy);
123b042e37fSmrg    SyncHandle ();
124b042e37fSmrg    return (XRROutputInfo *) xoi;
125b042e37fSmrg}
126b042e37fSmrg
127b042e37fSmrgvoid
128b042e37fSmrgXRRFreeOutputInfo (XRROutputInfo *outputInfo)
129b042e37fSmrg{
130b042e37fSmrg    Xfree (outputInfo);
131b042e37fSmrg}
1328c4a8e55Smrg
1338c4a8e55Smrgstatic Bool
1348c4a8e55Smrg_XRRHasOutputPrimary (int major, int minor)
1358c4a8e55Smrg{
1368c4a8e55Smrg    return major > 1 || (major == 1 && minor >= 3);
1378c4a8e55Smrg}
1388c4a8e55Smrg
1398c4a8e55Smrgvoid
1408c4a8e55SmrgXRRSetOutputPrimary(Display *dpy, Window window, RROutput output)
1418c4a8e55Smrg{
1428c4a8e55Smrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
1438c4a8e55Smrg    xRRSetOutputPrimaryReq  *req;
1448c4a8e55Smrg    int			    major_version, minor_version;
1458c4a8e55Smrg
1468c4a8e55Smrg    RRSimpleCheckExtension (dpy, info);
1478c4a8e55Smrg
1480597fb56Smrg    if (!XRRQueryVersion (dpy, &major_version, &minor_version) ||
1498c4a8e55Smrg	!_XRRHasOutputPrimary (major_version, minor_version))
1508c4a8e55Smrg	return;
1518c4a8e55Smrg
1528c4a8e55Smrg    LockDisplay(dpy);
1538c4a8e55Smrg    GetReq (RRSetOutputPrimary, req);
1548c4a8e55Smrg    req->reqType       = info->codes->major_opcode;
1558c4a8e55Smrg    req->randrReqType  = X_RRSetOutputPrimary;
1568c4a8e55Smrg    req->window        = window;
1578c4a8e55Smrg    req->output	       = output;
1588c4a8e55Smrg
1598c4a8e55Smrg    UnlockDisplay (dpy);
1608c4a8e55Smrg    SyncHandle ();
1618c4a8e55Smrg}
1628c4a8e55Smrg
1638c4a8e55SmrgRROutput
1648c4a8e55SmrgXRRGetOutputPrimary(Display *dpy, Window window)
1658c4a8e55Smrg{
1668c4a8e55Smrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
1678c4a8e55Smrg    xRRGetOutputPrimaryReq  *req;
1688c4a8e55Smrg    xRRGetOutputPrimaryReply rep;
1698c4a8e55Smrg    int			    major_version, minor_version;
1708c4a8e55Smrg
1718c4a8e55Smrg    RRCheckExtension (dpy, info, 0);
1728c4a8e55Smrg
1730597fb56Smrg    if (!XRRQueryVersion (dpy, &major_version, &minor_version) ||
1748c4a8e55Smrg	!_XRRHasOutputPrimary (major_version, minor_version))
1758c4a8e55Smrg	return None;
1768c4a8e55Smrg
1778c4a8e55Smrg    LockDisplay(dpy);
1788c4a8e55Smrg    GetReq (RRGetOutputPrimary, req);
1798c4a8e55Smrg    req->reqType	= info->codes->major_opcode;
1808c4a8e55Smrg    req->randrReqType	= X_RRGetOutputPrimary;
1818c4a8e55Smrg    req->window		= window;
1828c4a8e55Smrg
1838c4a8e55Smrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
1848c4a8e55Smrg	rep.output = None;
1850597fb56Smrg
1868c4a8e55Smrg    UnlockDisplay(dpy);
1878c4a8e55Smrg    SyncHandle();
1888c4a8e55Smrg
1898c4a8e55Smrg    return rep.output;
1908c4a8e55Smrg}
191