XrrOutput.c revision 8d0bc965
1/* 2 * Copyright © 2006 Keith Packard 3 * Copyright © 2008 Red Hat, Inc. 4 * 5 * Permission to use, copy, modify, distribute, and sell this software and its 6 * documentation for any purpose is hereby granted without fee, provided that 7 * the above copyright notice appear in all copies and that both that copyright 8 * notice and this permission notice appear in supporting documentation, and 9 * that the name of the copyright holders not be used in advertising or 10 * publicity pertaining to distribution of the software without specific, 11 * written prior permission. The copyright holders make no representations 12 * about the suitability of this software for any purpose. It is provided "as 13 * is" without express or implied warranty. 14 * 15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 * OF THIS SOFTWARE. 22 */ 23 24#ifdef HAVE_CONFIG_H 25#include <config.h> 26#endif 27 28#include <limits.h> 29#include <stdio.h> 30#include <X11/Xlib.h> 31/* we need to be able to manipulate the Display structure on events */ 32#include <X11/Xlibint.h> 33#include <X11/extensions/render.h> 34#include <X11/extensions/Xrender.h> 35#include "Xrandrint.h" 36 37#define OutputInfoExtra (SIZEOF(xRRGetOutputInfoReply) - 32) 38 39XRROutputInfo * 40XRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output) 41{ 42 XExtDisplayInfo *info = XRRFindDisplay(dpy); 43 xRRGetOutputInfoReply rep; 44 xRRGetOutputInfoReq *req; 45 int nbytes, nbytesRead, rbytes; 46 XRROutputInfo *xoi; 47 48 RRCheckExtension (dpy, info, NULL); 49 50 LockDisplay (dpy); 51 GetReq (RRGetOutputInfo, req); 52 req->reqType = info->codes->major_opcode; 53 req->randrReqType = X_RRGetOutputInfo; 54 req->output = output; 55 req->configTimestamp = resources->configTimestamp; 56 57 if (!_XReply (dpy, (xReply *) &rep, OutputInfoExtra >> 2, xFalse)) 58 { 59 UnlockDisplay (dpy); 60 SyncHandle (); 61 return NULL; 62 } 63 64 if (rep.length > INT_MAX >> 2 || rep.length < (OutputInfoExtra >> 2)) 65 { 66 if (rep.length > (OutputInfoExtra >> 2)) 67 _XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2)); 68 else 69 _XEatDataWords (dpy, rep.length); 70 UnlockDisplay (dpy); 71 SyncHandle (); 72 return NULL; 73 } 74 nbytes = ((long) (rep.length) << 2) - OutputInfoExtra; 75 76 nbytesRead = (long) (rep.nCrtcs * 4 + 77 rep.nModes * 4 + 78 rep.nClones * 4 + 79 ((rep.nameLength + 3) & ~3)); 80 81 /* 82 * first we must compute how much space to allocate for 83 * randr library's use; we'll allocate the structures in a single 84 * allocation, on cleanlyness grounds. 85 */ 86 87 rbytes = (sizeof (XRROutputInfo) + 88 rep.nCrtcs * sizeof (RRCrtc) + 89 rep.nModes * sizeof (RRMode) + 90 rep.nClones * sizeof (RROutput) + 91 rep.nameLength + 1); /* '\0' terminate name */ 92 93 xoi = Xmalloc(rbytes); 94 if (xoi == NULL) { 95 _XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2)); 96 UnlockDisplay (dpy); 97 SyncHandle (); 98 return NULL; 99 } 100 101 xoi->timestamp = rep.timestamp; 102 xoi->crtc = rep.crtc; 103 xoi->mm_width = rep.mmWidth; 104 xoi->mm_height = rep.mmHeight; 105 xoi->connection = rep.connection; 106 xoi->subpixel_order = rep.subpixelOrder; 107 xoi->ncrtc = rep.nCrtcs; 108 xoi->crtcs = (RRCrtc *) (xoi + 1); 109 xoi->nmode = rep.nModes; 110 xoi->npreferred = rep.nPreferred; 111 xoi->modes = (RRMode *) (xoi->crtcs + rep.nCrtcs); 112 xoi->nclone = rep.nClones; 113 xoi->clones = (RROutput *) (xoi->modes + rep.nModes); 114 xoi->name = (char *) (xoi->clones + rep.nClones); 115 116 _XRead32 (dpy, (long *) xoi->crtcs, rep.nCrtcs << 2); 117 _XRead32 (dpy, (long *) xoi->modes, rep.nModes << 2); 118 _XRead32 (dpy, (long *) xoi->clones, rep.nClones << 2); 119 120 /* 121 * Read name and '\0' terminate 122 */ 123 _XReadPad (dpy, xoi->name, rep.nameLength); 124 xoi->name[rep.nameLength] = '\0'; 125 xoi->nameLen = rep.nameLength; 126 127 /* 128 * Skip any extra data 129 */ 130 if (nbytes > nbytesRead) 131 _XEatData (dpy, (unsigned long) (nbytes - nbytesRead)); 132 133 UnlockDisplay (dpy); 134 SyncHandle (); 135 return (XRROutputInfo *) xoi; 136} 137 138void 139XRRFreeOutputInfo (XRROutputInfo *outputInfo) 140{ 141 Xfree (outputInfo); 142} 143 144static Bool 145_XRRHasOutputPrimary (int major, int minor) 146{ 147 return major > 1 || (major == 1 && minor >= 3); 148} 149 150void 151XRRSetOutputPrimary(Display *dpy, Window window, RROutput output) 152{ 153 XExtDisplayInfo *info = XRRFindDisplay(dpy); 154 xRRSetOutputPrimaryReq *req; 155 int major_version, minor_version; 156 157 RRSimpleCheckExtension (dpy, info); 158 159 if (!XRRQueryVersion (dpy, &major_version, &minor_version) || 160 !_XRRHasOutputPrimary (major_version, minor_version)) 161 return; 162 163 LockDisplay(dpy); 164 GetReq (RRSetOutputPrimary, req); 165 req->reqType = info->codes->major_opcode; 166 req->randrReqType = X_RRSetOutputPrimary; 167 req->window = window; 168 req->output = output; 169 170 UnlockDisplay (dpy); 171 SyncHandle (); 172} 173 174RROutput 175XRRGetOutputPrimary(Display *dpy, Window window) 176{ 177 XExtDisplayInfo *info = XRRFindDisplay(dpy); 178 xRRGetOutputPrimaryReq *req; 179 xRRGetOutputPrimaryReply rep; 180 int major_version, minor_version; 181 182 RRCheckExtension (dpy, info, 0); 183 184 if (!XRRQueryVersion (dpy, &major_version, &minor_version) || 185 !_XRRHasOutputPrimary (major_version, minor_version)) 186 return None; 187 188 LockDisplay(dpy); 189 GetReq (RRGetOutputPrimary, req); 190 req->reqType = info->codes->major_opcode; 191 req->randrReqType = X_RRGetOutputPrimary; 192 req->window = window; 193 194 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) 195 rep.output = None; 196 197 UnlockDisplay(dpy); 198 SyncHandle(); 199 200 return rep.output; 201} 202