XrrOutput.c revision b042e37f
1b042e37fSmrg/*
2b042e37fSmrg * Copyright © 2006 Keith Packard
3b042e37fSmrg *
4b042e37fSmrg * Permission to use, copy, modify, distribute, and sell this software and its
5b042e37fSmrg * documentation for any purpose is hereby granted without fee, provided that
6b042e37fSmrg * the above copyright notice appear in all copies and that both that copyright
7b042e37fSmrg * notice and this permission notice appear in supporting documentation, and
8b042e37fSmrg * that the name of the copyright holders not be used in advertising or
9b042e37fSmrg * publicity pertaining to distribution of the software without specific,
10b042e37fSmrg * written prior permission.  The copyright holders make no representations
11b042e37fSmrg * about the suitability of this software for any purpose.  It is provided "as
12b042e37fSmrg * is" without express or implied warranty.
13b042e37fSmrg *
14b042e37fSmrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15b042e37fSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16b042e37fSmrg * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17b042e37fSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18b042e37fSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19b042e37fSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20b042e37fSmrg * OF THIS SOFTWARE.
21b042e37fSmrg */
22b042e37fSmrg
23b042e37fSmrg#ifdef HAVE_CONFIG_H
24b042e37fSmrg#include <config.h>
25b042e37fSmrg#endif
26b042e37fSmrg
27b042e37fSmrg#include <stdio.h>
28b042e37fSmrg#include <X11/Xlib.h>
29b042e37fSmrg/* we need to be able to manipulate the Display structure on events */
30b042e37fSmrg#include <X11/Xlibint.h>
31b042e37fSmrg#include <X11/extensions/render.h>
32b042e37fSmrg#include <X11/extensions/Xrender.h>
33b042e37fSmrg#include "Xrandrint.h"
34b042e37fSmrg
35b042e37fSmrg#define OutputInfoExtra	(SIZEOF(xRRGetOutputInfoReply) - 32)
36b042e37fSmrg
37b042e37fSmrgXRROutputInfo *
38b042e37fSmrgXRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output)
39b042e37fSmrg{
40b042e37fSmrg    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
41b042e37fSmrg    xRRGetOutputInfoReply	rep;
42b042e37fSmrg    xRRGetOutputInfoReq		*req;
43b042e37fSmrg    int				nbytes, nbytesRead, rbytes;
44b042e37fSmrg    int				i;
45b042e37fSmrg    xRRQueryVersionReq		*vreq;
46b042e37fSmrg    XRROutputInfo		*xoi;
47b042e37fSmrg
48b042e37fSmrg    RRCheckExtension (dpy, info, 0);
49b042e37fSmrg
50b042e37fSmrg    LockDisplay (dpy);
51b042e37fSmrg    GetReq (RRGetOutputInfo, req);
52b042e37fSmrg    req->reqType = info->codes->major_opcode;
53b042e37fSmrg    req->randrReqType = X_RRGetOutputInfo;
54b042e37fSmrg    req->output = output;
55b042e37fSmrg    req->configTimestamp = resources->configTimestamp;
56b042e37fSmrg
57b042e37fSmrg    if (!_XReply (dpy, (xReply *) &rep, OutputInfoExtra >> 2, xFalse))
58b042e37fSmrg    {
59b042e37fSmrg	UnlockDisplay (dpy);
60b042e37fSmrg	SyncHandle ();
61b042e37fSmrg	return NULL;
62b042e37fSmrg    }
63b042e37fSmrg
64b042e37fSmrg    nbytes = ((long) (rep.length) << 2) - OutputInfoExtra;
65b042e37fSmrg
66b042e37fSmrg    nbytesRead = (long) (rep.nCrtcs * 4 +
67b042e37fSmrg			 rep.nModes * 4 +
68b042e37fSmrg			 rep.nClones * 4 +
69b042e37fSmrg			 ((rep.nameLength + 3) & ~3));
70b042e37fSmrg
71b042e37fSmrg    /*
72b042e37fSmrg     * first we must compute how much space to allocate for
73b042e37fSmrg     * randr library's use; we'll allocate the structures in a single
74b042e37fSmrg     * allocation, on cleanlyness grounds.
75b042e37fSmrg     */
76b042e37fSmrg
77b042e37fSmrg    rbytes = (sizeof (XRROutputInfo) +
78b042e37fSmrg	      rep.nCrtcs * sizeof (RRCrtc) +
79b042e37fSmrg	      rep.nModes * sizeof (RRMode) +
80b042e37fSmrg	      rep.nClones * sizeof (RROutput) +
81b042e37fSmrg	      rep.nameLength + 1);	    /* '\0' terminate name */
82b042e37fSmrg
83b042e37fSmrg    xoi = (XRROutputInfo *) Xmalloc(rbytes);
84b042e37fSmrg    if (xoi == NULL) {
85b042e37fSmrg	_XEatData (dpy, (unsigned long) nbytes);
86b042e37fSmrg	UnlockDisplay (dpy);
87b042e37fSmrg	SyncHandle ();
88b042e37fSmrg	return NULL;
89b042e37fSmrg    }
90b042e37fSmrg
91b042e37fSmrg    xoi->timestamp = rep.timestamp;
92b042e37fSmrg    xoi->crtc = rep.crtc;
93b042e37fSmrg    xoi->mm_width = rep.mmWidth;
94b042e37fSmrg    xoi->mm_height = rep.mmHeight;
95b042e37fSmrg    xoi->connection = rep.connection;
96b042e37fSmrg    xoi->subpixel_order = rep.subpixelOrder;
97b042e37fSmrg    xoi->ncrtc = rep.nCrtcs;
98b042e37fSmrg    xoi->crtcs = (RRCrtc *) (xoi + 1);
99b042e37fSmrg    xoi->nmode = rep.nModes;
100b042e37fSmrg    xoi->npreferred = rep.nPreferred;
101b042e37fSmrg    xoi->modes = (RRMode *) (xoi->crtcs + rep.nCrtcs);
102b042e37fSmrg    xoi->nclone = rep.nClones;
103b042e37fSmrg    xoi->clones = (RROutput *) (xoi->modes + rep.nModes);
104b042e37fSmrg    xoi->name = (char *) (xoi->clones + rep.nClones);
105b042e37fSmrg
106b042e37fSmrg    _XRead32 (dpy, xoi->crtcs, rep.nCrtcs << 2);
107b042e37fSmrg    _XRead32 (dpy, xoi->modes, rep.nModes << 2);
108b042e37fSmrg    _XRead32 (dpy, xoi->clones, rep.nClones << 2);
109b042e37fSmrg
110b042e37fSmrg    /*
111b042e37fSmrg     * Read name and '\0' terminate
112b042e37fSmrg     */
113b042e37fSmrg    _XReadPad (dpy, xoi->name, rep.nameLength);
114b042e37fSmrg    xoi->name[rep.nameLength] = '\0';
115b042e37fSmrg
116b042e37fSmrg    /*
117b042e37fSmrg     * Skip any extra data
118b042e37fSmrg     */
119b042e37fSmrg    if (nbytes > nbytesRead)
120b042e37fSmrg	_XEatData (dpy, (unsigned long) (nbytes - nbytesRead));
121b042e37fSmrg
122b042e37fSmrg    UnlockDisplay (dpy);
123b042e37fSmrg    SyncHandle ();
124b042e37fSmrg    return (XRROutputInfo *) xoi;
125b042e37fSmrg}
126b042e37fSmrg
127b042e37fSmrgvoid
128b042e37fSmrgXRRFreeOutputInfo (XRROutputInfo *outputInfo)
129b042e37fSmrg{
130b042e37fSmrg    Xfree (outputInfo);
131b042e37fSmrg}
132