| 1 |
/* |
| 2 |
* Copyright © 2000 Compaq Computer Corporation, Inc. |
| 3 |
* Copyright © 2002 Hewlett Packard Company, Inc. |
| 4 |
* Copyright © 2006 Keith Packard |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 7 |
* documentation for any purpose is hereby granted without fee, provided that |
| 8 |
* the above copyright notice appear in all copies and that both that copyright |
| 9 |
* notice and this permission notice appear in supporting documentation, and |
| 10 |
* that the name of the copyright holders not be used in advertising or |
| 11 |
* publicity pertaining to distribution of the software without specific, |
| 12 |
* written prior permission. The copyright holders make no representations |
| 13 |
* about the suitability of this software for any purpose. It is provided "as |
| 14 |
* is" without express or implied warranty. |
| 15 |
* |
| 16 |
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 18 |
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
* |
| 24 |
* Author: Jim Gettys, HP Labs, HP. |
| 25 |
* Author: Keith Packard, Intel Corporation |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include <config.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <limits.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <X11/Xlib.h> |
| 35 |
/* we need to be able to manipulate the Display structure on events */ |
| 36 |
#include <X11/Xlibint.h> |
| 37 |
#include <X11/extensions/render.h> |
| 38 |
#include <X11/extensions/Xrender.h> |
| 39 |
#include "Xrandrint.h" |
| 40 |
|
| 41 |
static XRRScreenConfiguration *_XRRGetScreenInfo (Display *dpy, |
| 42 |
XExtDisplayInfo *info, |
| 43 |
Window window); |
| 44 |
|
| 45 |
Rotation XRRConfigRotations(XRRScreenConfiguration *config, Rotation *current_rotation) |
| 46 |
{ |
| 47 |
*current_rotation = config->current_rotation; |
| 48 |
return config->rotations; |
| 49 |
} |
| 50 |
|
| 51 |
XRRScreenSize *XRRConfigSizes(XRRScreenConfiguration *config, int *nsizes) |
| 52 |
{ |
| 53 |
*nsizes = config->nsizes; |
| 54 |
return config->sizes; |
| 55 |
} |
| 56 |
|
| 57 |
short *XRRConfigRates (XRRScreenConfiguration *config, int sizeID, int *nrates) |
| 58 |
{ |
| 59 |
short *r = config->rates; |
| 60 |
int nents = config->nrates; |
| 61 |
|
| 62 |
/* Skip over the intervening rate lists */ |
| 63 |
while (sizeID > 0 && nents > 0) |
| 64 |
{ |
| 65 |
int i = (*r + 1); |
| 66 |
r += i; |
| 67 |
nents -= i; |
| 68 |
sizeID--; |
| 69 |
} |
| 70 |
if (!nents) |
| 71 |
{ |
| 72 |
*nrates = 0; |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
*nrates = (int) *r; |
| 76 |
return r + 1; |
| 77 |
} |
| 78 |
|
| 79 |
Time XRRConfigTimes (XRRScreenConfiguration *config, Time *config_timestamp) |
| 80 |
{ |
| 81 |
*config_timestamp = config->config_timestamp; |
| 82 |
return config->timestamp; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
SizeID XRRConfigCurrentConfiguration (XRRScreenConfiguration *config, |
| 87 |
Rotation *rotation) |
| 88 |
{ |
| 89 |
*rotation = (Rotation) config->current_rotation; |
| 90 |
return (SizeID) config->current_size; |
| 91 |
} |
| 92 |
|
| 93 |
short XRRConfigCurrentRate (XRRScreenConfiguration *config) |
| 94 |
{ |
| 95 |
return config->current_rate; |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Go get the screen configuration data and salt it away for future use; |
| 100 |
* returns NULL if extension not supported |
| 101 |
*/ |
| 102 |
static XRRScreenConfiguration *_XRRValidateCache (Display *dpy, |
| 103 |
XExtDisplayInfo *info, |
| 104 |
int screen) |
| 105 |
{ |
| 106 |
if ((screen >= 0) && (screen < ScreenCount(dpy)) && XextHasExtension(info)) { |
| 107 |
XRandRInfo *xrri = (XRandRInfo *) info->data; |
| 108 |
XRRScreenConfiguration **configs = xrri->config; |
| 109 |
|
| 110 |
if (configs[screen] == NULL) |
| 111 |
configs[screen] = _XRRGetScreenInfo (dpy, info, RootWindow(dpy, screen)); |
| 112 |
return configs[screen]; |
| 113 |
} else { |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/* given a screen, return the information from the (possibly) cached data */ |
| 119 |
Rotation XRRRotations(Display *dpy, int screen, Rotation *current_rotation) |
| 120 |
{ |
| 121 |
XRRScreenConfiguration *config; |
| 122 |
XExtDisplayInfo *info = XRRFindDisplay(dpy); |
| 123 |
|
| 124 |
LockDisplay(dpy); |
| 125 |
if ((config = _XRRValidateCache(dpy, info, screen))) { |
| 126 |
Rotation cr = config->rotations; |
| 127 |
*current_rotation = config->current_rotation; |
| 128 |
UnlockDisplay(dpy); |
| 129 |
return cr; |
| 130 |
} |
| 131 |
else { |
| 132 |
UnlockDisplay(dpy); |
| 133 |
*current_rotation = RR_Rotate_0; |
| 134 |
return 0; /* no rotations supported */ |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/* given a screen, return the information from the (possibly) cached data */ |
| 139 |
XRRScreenSize *XRRSizes(Display *dpy, int screen, int *nsizes) |
| 140 |
{ |
| 141 |
XRRScreenConfiguration *config; |
| 142 |
XExtDisplayInfo *info = XRRFindDisplay(dpy); |
| 143 |
|
| 144 |
LockDisplay(dpy); |
| 145 |
if ((config = _XRRValidateCache(dpy, info, screen))) { |
| 146 |
XRRScreenSize *sizes = config->sizes; |
| 147 |
*nsizes = config->nsizes; |
| 148 |
UnlockDisplay(dpy); |
| 149 |
return sizes; |
| 150 |
} |
| 151 |
else { |
| 152 |
UnlockDisplay(dpy); |
| 153 |
*nsizes = 0; |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
short *XRRRates (Display *dpy, int screen, int sizeID, int *nrates) |
| 159 |
{ |
| 160 |
XRRScreenConfiguration *config; |
| 161 |
XExtDisplayInfo *info = XRRFindDisplay(dpy); |
| 162 |
|
| 163 |
LockDisplay(dpy); |
| 164 |
if ((config = _XRRValidateCache(dpy, info, screen))) { |
| 165 |
short *rates = XRRConfigRates (config, sizeID, nrates); |
| 166 |
UnlockDisplay(dpy); |
| 167 |
return rates; |
| 168 |
} |
| 169 |
else { |
| 170 |
UnlockDisplay(dpy); |
| 171 |
*nrates = 0; |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* given a screen, return the information from the (possibly) cached data */ |
| 177 |
Time XRRTimes (Display *dpy, int screen, Time *config_timestamp) |
| 178 |
{ |
| 179 |
XRRScreenConfiguration *config; |
| 180 |
XExtDisplayInfo *info = XRRFindDisplay(dpy); |
| 181 |
|
| 182 |
LockDisplay(dpy); |
| 183 |
if ((config = _XRRValidateCache(dpy, info, screen))) { |
| 184 |
Time ts = config->timestamp; |
| 185 |
*config_timestamp = config->config_timestamp; |
| 186 |
UnlockDisplay(dpy); |
| 187 |
return ts; |
| 188 |
} else { |
| 189 |
UnlockDisplay(dpy); |
| 190 |
return CurrentTime; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/* need a version that does not hold the display lock */ |
| 195 |
static XRRScreenConfiguration *_XRRGetScreenInfo (Display *dpy, |
| 196 |
XExtDisplayInfo *info, |
| 197 |
Window window) |
| 198 |
{ |
| 199 |
xRRGetScreenInfoReply rep; |
| 200 |
xRRGetScreenInfoReq *req; |
| 201 |
_XAsyncHandler async; |
| 202 |
_XRRVersionState async_state; |
| 203 |
int nbytes, nbytesRead, rbytes; |
| 204 |
xScreenSizes size; |
| 205 |
struct _XRRScreenConfiguration *scp; |
| 206 |
XRRScreenSize *ssp; |
| 207 |
short *rates; |
| 208 |
XRandRInfo *xrri; |
| 209 |
Bool getting_version = False; |
| 210 |
|
| 211 |
xrri = (XRandRInfo *) info->data; |
| 212 |
if (!xrri) |
| 213 |
return NULL; |
| 214 |
|
| 215 |
if (xrri->major_version == -1) |
| 216 |
{ |
| 217 |
xRRQueryVersionReq *vreq; |
| 218 |
|
| 219 |
/* hide a version query in the request */ |
| 220 |
GetReq (RRQueryVersion, vreq); |
| 221 |
vreq->reqType = info->codes->major_opcode; |
| 222 |
vreq->randrReqType = X_RRQueryVersion; |
| 223 |
vreq->majorVersion = RANDR_MAJOR; |
| 224 |
vreq->minorVersion = RANDR_MINOR; |
| 225 |
|
| 226 |
async_state.version_seq = dpy->request; |
| 227 |
async_state.error = False; |
| 228 |
async.next = dpy->async_handlers; |
| 229 |
async.handler = _XRRVersionHandler; |
| 230 |
async.data = (XPointer) &async_state; |
| 231 |
dpy->async_handlers = &async; |
| 232 |
|
| 233 |
getting_version = True; |
| 234 |
} |
| 235 |
|
| 236 |
GetReq (RRGetScreenInfo, req); |
| 237 |
req->reqType = info->codes->major_opcode; |
| 238 |
req->randrReqType = X_RRGetScreenInfo; |
| 239 |
req->window = window; |
| 240 |
|
| 241 |
if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) |
| 242 |
{ |
| 243 |
if (getting_version) |
| 244 |
DeqAsyncHandler (dpy, &async); |
| 245 |
SyncHandle (); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
if (getting_version) |
| 249 |
{ |
| 250 |
DeqAsyncHandler (dpy, &async); |
| 251 |
if (async_state.error) |
| 252 |
{ |
| 253 |
UnlockDisplay (dpy); |
| 254 |
SyncHandle(); |
| 255 |
LockDisplay (dpy); |
| 256 |
} |
| 257 |
xrri->major_version = async_state.major_version; |
| 258 |
xrri->minor_version = async_state.minor_version; |
| 259 |
xrri->has_rates = _XRRHasRates (xrri->minor_version, xrri->major_version); |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* Make the reply compatible with v1.1 |
| 264 |
*/ |
| 265 |
if (!xrri->has_rates) |
| 266 |
{ |
| 267 |
rep.rate = 0; |
| 268 |
rep.nrateEnts = 0; |
| 269 |
} |
| 270 |
if (rep.length < INT_MAX >> 2) { |
| 271 |
nbytes = (long) rep.length << 2; |
| 272 |
|
| 273 |
nbytesRead = (long) (rep.nSizes * SIZEOF (xScreenSizes) + |
| 274 |
((rep.nrateEnts + 1)& ~1) * 2 /* SIZEOF(CARD16) */); |
| 275 |
|
| 276 |
/* |
| 277 |
* first we must compute how much space to allocate for |
| 278 |
* randr library's use; we'll allocate the structures in a single |
| 279 |
* allocation, on cleanlyness grounds. |
| 280 |
*/ |
| 281 |
|
| 282 |
rbytes = sizeof (XRRScreenConfiguration) + |
| 283 |
(rep.nSizes * sizeof (XRRScreenSize) + |
| 284 |
rep.nrateEnts * sizeof (int)); |
| 285 |
|
| 286 |
scp = Xmalloc(rbytes); |
| 287 |
} else { |
| 288 |
nbytes = 0; |
| 289 |
nbytesRead = 0; |
| 290 |
rbytes = 0; |
| 291 |
scp = NULL; |
| 292 |
} |
| 293 |
|
| 294 |
if (scp == NULL) { |
| 295 |
_XEatData (dpy, (unsigned long) nbytes); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
ssp = (XRRScreenSize *)(scp + 1); |
| 301 |
rates = (short *) (ssp + rep.nSizes); |
| 302 |
|
| 303 |
/* set up the screen configuration structure */ |
| 304 |
scp->screen = |
| 305 |
ScreenOfDisplay (dpy, XRRRootToScreen(dpy, rep.root)); |
| 306 |
|
| 307 |
scp->sizes = ssp; |
| 308 |
scp->rates = rates; |
| 309 |
scp->rotations = rep.setOfRotations; |
| 310 |
scp->current_size = rep.sizeID; |
| 311 |
scp->current_rate = rep.rate; |
| 312 |
scp->current_rotation = rep.rotation; |
| 313 |
scp->timestamp = rep.timestamp; |
| 314 |
scp->config_timestamp = rep.configTimestamp; |
| 315 |
scp->nsizes = rep.nSizes; |
| 316 |
scp->nrates = rep.nrateEnts; |
| 317 |
|
| 318 |
/* |
| 319 |
* Time to unpack the data from the server. |
| 320 |
*/ |
| 321 |
|
| 322 |
/* |
| 323 |
* First the size information |
| 324 |
*/ |
| 325 |
for (unsigned int i = 0; i < rep.nSizes; i++) { |
| 326 |
_XReadPad (dpy, (char *) &size, SIZEOF (xScreenSizes)); |
| 327 |
|
| 328 |
ssp[i].width = size.widthInPixels; |
| 329 |
ssp[i].height = size.heightInPixels; |
| 330 |
ssp[i].mwidth = size.widthInMillimeters; |
| 331 |
ssp[i].mheight = size.heightInMillimeters; |
| 332 |
} |
| 333 |
/* |
| 334 |
* And the rates |
| 335 |
*/ |
| 336 |
_XRead16Pad (dpy, rates, 2 /* SIZEOF (CARD16) */ * rep.nrateEnts); |
| 337 |
|
| 338 |
/* |
| 339 |
* Skip any extra data |
| 340 |
*/ |
| 341 |
if (nbytes > nbytesRead) |
| 342 |
_XEatData (dpy, (unsigned long) (nbytes - nbytesRead)); |
| 343 |
|
| 344 |
return (XRRScreenConfiguration *)(scp); |
| 345 |
} |
| 346 |
|
| 347 |
XRRScreenConfiguration *XRRGetScreenInfo (Display *dpy, Window window) |
| 348 |
{ |
| 349 |
XRRScreenConfiguration *config; |
| 350 |
XExtDisplayInfo *info = XRRFindDisplay(dpy); |
| 351 |
XRRFindDisplay(dpy); |
| 352 |
LockDisplay (dpy); |
| 353 |
config = _XRRGetScreenInfo(dpy, info, window); |
| 354 |
UnlockDisplay (dpy); |
| 355 |
SyncHandle (); |
| 356 |
return config; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
void XRRFreeScreenConfigInfo (XRRScreenConfiguration *config) |
| 361 |
{ |
| 362 |
Xfree (config); |
| 363 |
} |
| 364 |
|
| 365 |
Status XRRSetScreenConfigAndRate (Display *dpy, |
| 366 |
XRRScreenConfiguration *config, |
| 367 |
Drawable draw, |
| 368 |
int size_index, |
| 369 |
Rotation rotation, |
| 370 |
short rate, |
| 371 |
Time timestamp) |
| 372 |
{ |
| 373 |
XExtDisplayInfo *info = XRRFindDisplay (dpy); |
| 374 |
xRRSetScreenConfigReply rep; |
| 375 |
XRandRInfo *xrri; |
| 376 |
int major, minor; |
| 377 |
|
| 378 |
RRCheckExtension (dpy, info, 0); |
| 379 |
|
| 380 |
/* Make sure has_rates is set */ |
| 381 |
if (!XRRQueryVersion (dpy, &major, &minor)) |
| 382 |
return 0; |
| 383 |
|
| 384 |
LockDisplay (dpy); |
| 385 |
xrri = (XRandRInfo *) info->data; |
| 386 |
if (xrri->has_rates) |
| 387 |
{ |
| 388 |
xRRSetScreenConfigReq *req; |
| 389 |
GetReq (RRSetScreenConfig, req); |
| 390 |
req->reqType = info->codes->major_opcode; |
| 391 |
req->randrReqType = X_RRSetScreenConfig; |
| 392 |
req->drawable = draw; |
| 393 |
req->sizeID = size_index; |
| 394 |
req->rotation = rotation; |
| 395 |
req->timestamp = timestamp; |
| 396 |
req->configTimestamp = config->config_timestamp; |
| 397 |
req->rate = rate; |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
xRR1_0SetScreenConfigReq *req; |
| 402 |
GetReq (RR1_0SetScreenConfig, req); |
| 403 |
req->reqType = info->codes->major_opcode; |
| 404 |
req->randrReqType = X_RRSetScreenConfig; |
| 405 |
req->drawable = draw; |
| 406 |
req->sizeID = size_index; |
| 407 |
req->rotation = rotation; |
| 408 |
req->timestamp = timestamp; |
| 409 |
req->configTimestamp = config->config_timestamp; |
| 410 |
} |
| 411 |
|
| 412 |
(void) _XReply (dpy, (xReply *) &rep, 0, xTrue); |
| 413 |
|
| 414 |
/* actually .errorCode in struct xError */ |
| 415 |
if (rep.status == RRSetConfigSuccess) { |
| 416 |
/* if we succeed, set our view of reality to what we set it to */ |
| 417 |
config->config_timestamp = rep.newConfigTimestamp; |
| 418 |
config->timestamp = rep.newTimestamp; |
| 419 |
config->screen = ScreenOfDisplay (dpy, XRRRootToScreen(dpy, rep.root)); |
| 420 |
config->current_size = size_index; |
| 421 |
config->current_rotation = rotation; |
| 422 |
} |
| 423 |
UnlockDisplay (dpy); |
| 424 |
SyncHandle (); |
| 425 |
return(rep.status); |
| 426 |
} |
| 427 |
|
| 428 |
Status XRRSetScreenConfig (Display *dpy, |
| 429 |
XRRScreenConfiguration *config, |
| 430 |
Drawable draw, |
| 431 |
int size_index, |
| 432 |
Rotation rotation, Time timestamp) |
| 433 |
{ |
| 434 |
return XRRSetScreenConfigAndRate (dpy, config, draw, size_index, |
| 435 |
rotation, 0, timestamp); |
| 436 |
} |