XrrScreen.c revision 0597fb56
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 358c4a8e55Smrg/* 368c4a8e55Smrg * this is cheating on the knowledge that the two requests are identical 378c4a8e55Smrg * but for the request number. 388c4a8e55Smrg */ 398c4a8e55Smrgstatic XRRScreenResources * 408c4a8e55SmrgdoGetScreenResources (Display *dpy, Window window, int poll) 41b042e37fSmrg{ 42b042e37fSmrg XExtDisplayInfo *info = XRRFindDisplay(dpy); 43b042e37fSmrg xRRGetScreenResourcesReply rep; 44b042e37fSmrg xRRGetScreenResourcesReq *req; 45b042e37fSmrg _XAsyncHandler async; 46b042e37fSmrg _XRRVersionState async_state; 47b042e37fSmrg int nbytes, nbytesRead, rbytes; 48b042e37fSmrg int i; 49b042e37fSmrg xRRQueryVersionReq *vreq; 50b042e37fSmrg XRRScreenResources *xrsr; 51b042e37fSmrg char *names; 52b042e37fSmrg char *wire_names, *wire_name; 53b042e37fSmrg Bool getting_version = False; 54b042e37fSmrg XRandRInfo *xrri; 55b042e37fSmrg 568c4a8e55Smrg RRCheckExtension (dpy, info, NULL); 57b042e37fSmrg 58b042e37fSmrg LockDisplay (dpy); 59b042e37fSmrg xrri = (XRandRInfo *) info->data; 60b042e37fSmrg 61b042e37fSmrg if (xrri->major_version == -1) 62b042e37fSmrg { 63b042e37fSmrg /* hide a version query in the request */ 64b042e37fSmrg GetReq (RRQueryVersion, vreq); 65b042e37fSmrg vreq->reqType = info->codes->major_opcode; 66b042e37fSmrg vreq->randrReqType = X_RRQueryVersion; 67b042e37fSmrg vreq->majorVersion = RANDR_MAJOR; 68b042e37fSmrg vreq->minorVersion = RANDR_MINOR; 690597fb56Smrg 70b042e37fSmrg async_state.version_seq = dpy->request; 71b042e37fSmrg async_state.error = False; 72b042e37fSmrg async.next = dpy->async_handlers; 73b042e37fSmrg async.handler = _XRRVersionHandler; 74b042e37fSmrg async.data = (XPointer) &async_state; 75b042e37fSmrg dpy->async_handlers = &async; 76b042e37fSmrg 77b042e37fSmrg getting_version = True; 78b042e37fSmrg } 79b042e37fSmrg 80b042e37fSmrg GetReq (RRGetScreenResources, req); 81b042e37fSmrg req->reqType = info->codes->major_opcode; 828c4a8e55Smrg req->randrReqType = poll ? X_RRGetScreenResources 838c4a8e55Smrg : X_RRGetScreenResourcesCurrent; 84b042e37fSmrg req->window = window; 85b042e37fSmrg 86b042e37fSmrg if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) 87b042e37fSmrg { 88b042e37fSmrg if (getting_version) 89b042e37fSmrg DeqAsyncHandler (dpy, &async); 90b042e37fSmrg UnlockDisplay (dpy); 91b042e37fSmrg SyncHandle (); 92b042e37fSmrg return NULL; 93b042e37fSmrg } 94b042e37fSmrg if (getting_version) 95b042e37fSmrg { 96b042e37fSmrg DeqAsyncHandler (dpy, &async); 97b042e37fSmrg if (async_state.error) 98b042e37fSmrg { 99b042e37fSmrg UnlockDisplay (dpy); 100b042e37fSmrg SyncHandle(); 101b042e37fSmrg LockDisplay (dpy); 102b042e37fSmrg } 103b042e37fSmrg xrri->major_version = async_state.major_version; 104b042e37fSmrg xrri->minor_version = async_state.minor_version; 105b042e37fSmrg xrri->has_rates = _XRRHasRates (xrri->minor_version, xrri->major_version); 106b042e37fSmrg } 107b042e37fSmrg 108b042e37fSmrg nbytes = (long) rep.length << 2; 109b042e37fSmrg 110b042e37fSmrg nbytesRead = (long) (rep.nCrtcs * 4 + 111b042e37fSmrg rep.nOutputs * 4 + 112b042e37fSmrg rep.nModes * SIZEOF (xRRModeInfo) + 113b042e37fSmrg ((rep.nbytesNames + 3) & ~3)); 114b042e37fSmrg 1150597fb56Smrg /* 1160597fb56Smrg * first we must compute how much space to allocate for 117b042e37fSmrg * randr library's use; we'll allocate the structures in a single 118b042e37fSmrg * allocation, on cleanlyness grounds. 119b042e37fSmrg */ 120b042e37fSmrg 121b042e37fSmrg rbytes = (sizeof (XRRScreenResources) + 122b042e37fSmrg rep.nCrtcs * sizeof (RRCrtc) + 123b042e37fSmrg rep.nOutputs * sizeof (RROutput) + 124b042e37fSmrg rep.nModes * sizeof (XRRModeInfo) + 125b042e37fSmrg rep.nbytesNames + rep.nModes); /* '\0' terminate names */ 126b042e37fSmrg 127b042e37fSmrg xrsr = (XRRScreenResources *) Xmalloc(rbytes); 128b042e37fSmrg wire_names = (char *) Xmalloc (rep.nbytesNames); 129b042e37fSmrg if (xrsr == NULL || wire_names == NULL) { 130b042e37fSmrg if (xrsr) Xfree (xrsr); 131b042e37fSmrg if (wire_names) Xfree (wire_names); 132b042e37fSmrg _XEatData (dpy, (unsigned long) nbytes); 133b042e37fSmrg UnlockDisplay (dpy); 134b042e37fSmrg SyncHandle (); 135b042e37fSmrg return NULL; 136b042e37fSmrg } 137b042e37fSmrg 138b042e37fSmrg xrsr->timestamp = rep.timestamp; 139b042e37fSmrg xrsr->configTimestamp = rep.configTimestamp; 140b042e37fSmrg xrsr->ncrtc = rep.nCrtcs; 141b042e37fSmrg xrsr->crtcs = (RRCrtc *) (xrsr + 1); 142b042e37fSmrg xrsr->noutput = rep.nOutputs; 143b042e37fSmrg xrsr->outputs = (RROutput *) (xrsr->crtcs + rep.nCrtcs); 144b042e37fSmrg xrsr->nmode = rep.nModes; 145b042e37fSmrg xrsr->modes = (XRRModeInfo *) (xrsr->outputs + rep.nOutputs); 146b042e37fSmrg names = (char *) (xrsr->modes + rep.nModes); 147b042e37fSmrg 148b042e37fSmrg _XRead32 (dpy, xrsr->crtcs, rep.nCrtcs << 2); 149b042e37fSmrg _XRead32 (dpy, xrsr->outputs, rep.nOutputs << 2); 1500597fb56Smrg 151b042e37fSmrg for (i = 0; i < rep.nModes; i++) { 152b042e37fSmrg xRRModeInfo modeInfo; 1530597fb56Smrg 154b042e37fSmrg _XReadPad (dpy, (char *) &modeInfo, SIZEOF (xRRModeInfo)); 155b042e37fSmrg xrsr->modes[i].id = modeInfo.id; 156b042e37fSmrg xrsr->modes[i].width = modeInfo.width; 157b042e37fSmrg xrsr->modes[i].height = modeInfo.height; 158b042e37fSmrg xrsr->modes[i].dotClock = modeInfo.dotClock; 159b042e37fSmrg xrsr->modes[i].hSyncStart = modeInfo.hSyncStart; 160b042e37fSmrg xrsr->modes[i].hSyncEnd = modeInfo.hSyncEnd; 161b042e37fSmrg xrsr->modes[i].hTotal = modeInfo.hTotal; 162b042e37fSmrg xrsr->modes[i].hSkew = modeInfo.hSkew; 163b042e37fSmrg xrsr->modes[i].vSyncStart = modeInfo.vSyncStart; 164b042e37fSmrg xrsr->modes[i].vSyncEnd = modeInfo.vSyncEnd; 165b042e37fSmrg xrsr->modes[i].vTotal = modeInfo.vTotal; 166b042e37fSmrg xrsr->modes[i].nameLength = modeInfo.nameLength; 167b042e37fSmrg xrsr->modes[i].modeFlags = modeInfo.modeFlags; 168b042e37fSmrg } 169b042e37fSmrg 170b042e37fSmrg /* 171b042e37fSmrg * Read names and '\0' pad each one 172b042e37fSmrg */ 173b042e37fSmrg _XReadPad (dpy, wire_names, rep.nbytesNames); 174b042e37fSmrg wire_name = wire_names; 175b042e37fSmrg for (i = 0; i < rep.nModes; i++) { 176b042e37fSmrg xrsr->modes[i].name = names; 177b042e37fSmrg memcpy (names, wire_name, xrsr->modes[i].nameLength); 178b042e37fSmrg names[xrsr->modes[i].nameLength] = '\0'; 179b042e37fSmrg names += xrsr->modes[i].nameLength + 1; 180b042e37fSmrg wire_name += xrsr->modes[i].nameLength; 181b042e37fSmrg } 182b042e37fSmrg Xfree (wire_names); 1830597fb56Smrg 184b042e37fSmrg /* 185b042e37fSmrg * Skip any extra data 186b042e37fSmrg */ 187b042e37fSmrg if (nbytes > nbytesRead) 188b042e37fSmrg _XEatData (dpy, (unsigned long) (nbytes - nbytesRead)); 1890597fb56Smrg 190b042e37fSmrg UnlockDisplay (dpy); 191b042e37fSmrg SyncHandle(); 192b042e37fSmrg return (XRRScreenResources *) xrsr; 193b042e37fSmrg} 194b042e37fSmrg 1958c4a8e55SmrgXRRScreenResources * 1968c4a8e55SmrgXRRGetScreenResources(Display *dpy, Window window) 1978c4a8e55Smrg{ 1988c4a8e55Smrg return doGetScreenResources(dpy, window, 1); 1998c4a8e55Smrg} 2008c4a8e55Smrg 2018c4a8e55SmrgXRRScreenResources * 2028c4a8e55SmrgXRRGetScreenResourcesCurrent(Display *dpy, Window window) 2038c4a8e55Smrg{ 2048c4a8e55Smrg return doGetScreenResources(dpy, window, 0); 2058c4a8e55Smrg} 2068c4a8e55Smrg 207b042e37fSmrgvoid 208b042e37fSmrgXRRFreeScreenResources (XRRScreenResources *resources) 209b042e37fSmrg{ 210b042e37fSmrg Xfree (resources); 211b042e37fSmrg} 212b042e37fSmrg 213b042e37fSmrgStatus 214b042e37fSmrgXRRGetScreenSizeRange (Display *dpy, Window window, 215b042e37fSmrg int *minWidth, int *minHeight, 216b042e37fSmrg int *maxWidth, int *maxHeight) 217b042e37fSmrg{ 218b042e37fSmrg XExtDisplayInfo *info = XRRFindDisplay(dpy); 219b042e37fSmrg xRRGetScreenSizeRangeReq *req; 220b042e37fSmrg xRRGetScreenSizeRangeReply rep; 221b042e37fSmrg 222b042e37fSmrg RRCheckExtension (dpy, info, 0); 223b042e37fSmrg LockDisplay (dpy); 224b042e37fSmrg GetReq (RRGetScreenSizeRange, req); 225b042e37fSmrg req->reqType = info->codes->major_opcode; 226b042e37fSmrg req->randrReqType = X_RRGetScreenSizeRange; 227b042e37fSmrg req->window = window; 228b042e37fSmrg if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) 229b042e37fSmrg { 230b042e37fSmrg UnlockDisplay (dpy); 231b042e37fSmrg SyncHandle (); 232b042e37fSmrg return False; 233b042e37fSmrg } 234b042e37fSmrg UnlockDisplay (dpy); 235b042e37fSmrg SyncHandle (); 236b042e37fSmrg *minWidth = rep.minWidth; 237b042e37fSmrg *minHeight = rep.minHeight; 238b042e37fSmrg *maxWidth = rep.maxWidth; 239b042e37fSmrg *maxHeight = rep.maxHeight; 240b042e37fSmrg return True; 241b042e37fSmrg} 242b042e37fSmrg 243b042e37fSmrgvoid 244b042e37fSmrgXRRSetScreenSize (Display *dpy, Window window, 245b042e37fSmrg int width, int height, 246b042e37fSmrg int mmWidth, int mmHeight) 247b042e37fSmrg{ 248b042e37fSmrg XExtDisplayInfo *info = XRRFindDisplay(dpy); 249b042e37fSmrg xRRSetScreenSizeReq *req; 250b042e37fSmrg 251b042e37fSmrg RRSimpleCheckExtension (dpy, info); 252b042e37fSmrg LockDisplay (dpy); 253b042e37fSmrg GetReq (RRSetScreenSize, req); 254b042e37fSmrg req->reqType = info->codes->major_opcode; 255b042e37fSmrg req->randrReqType = X_RRSetScreenSize; 256b042e37fSmrg req->window = window; 257b042e37fSmrg req->width = width; 258b042e37fSmrg req->height = height; 259b042e37fSmrg req->widthInMillimeters = mmWidth; 260b042e37fSmrg req->heightInMillimeters = mmHeight; 261b042e37fSmrg UnlockDisplay (dpy); 262b042e37fSmrg SyncHandle (); 263b042e37fSmrg} 264