rrcrtc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright © 2006 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include "randrstr.h"
  23. #include "swaprep.h"
  24. RESTYPE RRCrtcType;
  25. /*
  26. * Notify the CRTC of some change
  27. */
  28. void
  29. RRCrtcChanged(RRCrtcPtr crtc, Bool layoutChanged)
  30. {
  31. ScreenPtr pScreen = crtc->pScreen;
  32. crtc->changed = TRUE;
  33. if (pScreen) {
  34. rrScrPriv(pScreen);
  35. pScrPriv->changed = TRUE;
  36. /*
  37. * Send ConfigureNotify on any layout change
  38. */
  39. if (layoutChanged)
  40. pScrPriv->layoutChanged = TRUE;
  41. }
  42. }
  43. /*
  44. * Create a CRTC
  45. */
  46. RRCrtcPtr
  47. RRCrtcCreate(void *devPrivate)
  48. {
  49. RRCrtcPtr crtc;
  50. if (!RRInit())
  51. return NULL;
  52. crtc = malloc(sizeof(RRCrtcRec));
  53. if (!crtc)
  54. return NULL;
  55. crtc->id = FakeClientID(0);
  56. crtc->pScreen = NULL;
  57. crtc->mode = NULL;
  58. crtc->x = 0;
  59. crtc->y = 0;
  60. crtc->rotation = RR_Rotate_0;
  61. crtc->rotations = RR_Rotate_0;
  62. crtc->outputs = NULL;
  63. crtc->numOutputs = 0;
  64. crtc->gammaSize = 0;
  65. crtc->gammaRed = crtc->gammaBlue = crtc->gammaGreen = NULL;
  66. crtc->changed = FALSE;
  67. crtc->devPrivate = devPrivate;
  68. if (!AddResource(crtc->id, RRCrtcType, (pointer) crtc))
  69. return NULL;
  70. return crtc;
  71. }
  72. /*
  73. * Set the allowed rotations on a CRTC
  74. */
  75. void
  76. RRCrtcSetRotations(RRCrtcPtr crtc, Rotation rotations)
  77. {
  78. crtc->rotations = rotations;
  79. }
  80. /*
  81. * Attach a Crtc to a screen. This is done as a separate step
  82. * so that an xf86-based driver can create CRTCs in PreInit
  83. * before the Screen has been created
  84. */
  85. Bool
  86. RRCrtcAttachScreen(RRCrtcPtr crtc, ScreenPtr pScreen)
  87. {
  88. rrScrPriv(pScreen);
  89. RRCrtcPtr *crtcs;
  90. /* make space for the crtc pointer */
  91. if (pScrPriv->numCrtcs)
  92. crtcs = realloc(pScrPriv->crtcs,
  93. (pScrPriv->numCrtcs + 1) * sizeof(RRCrtcPtr));
  94. else
  95. crtcs = malloc(sizeof(RRCrtcPtr));
  96. if (!crtcs)
  97. return FALSE;
  98. /* attach the screen and crtc together */
  99. crtc->pScreen = pScreen;
  100. pScrPriv->crtcs = crtcs;
  101. pScrPriv->crtcs[pScrPriv->numCrtcs++] = crtc;
  102. RRCrtcChanged(crtc, TRUE);
  103. return TRUE;
  104. }
  105. /*
  106. * Notify the extension that the Crtc has been reconfigured,
  107. * the driver calls this whenever it has updated the mode
  108. */
  109. Bool
  110. RRCrtcNotify(RRCrtcPtr crtc,
  111. RRModePtr mode,
  112. int x,
  113. int y, Rotation rotation, int numOutputs, RROutputPtr * outputs)
  114. {
  115. int i, j;
  116. /*
  117. * Check to see if any of the new outputs were
  118. * not in the old list and mark them as changed
  119. */
  120. for (i = 0; i < numOutputs; i++) {
  121. for (j = 0; j < crtc->numOutputs; j++)
  122. if (outputs[i] == crtc->outputs[j])
  123. break;
  124. if (j == crtc->numOutputs) {
  125. RROutputChanged(outputs[i], FALSE);
  126. RRCrtcChanged(crtc, FALSE);
  127. }
  128. }
  129. /*
  130. * Check to see if any of the old outputs are
  131. * not in the new list and mark them as changed
  132. */
  133. for (j = 0; j < crtc->numOutputs; j++) {
  134. for (i = 0; i < numOutputs; i++)
  135. if (outputs[i] == crtc->outputs[j])
  136. break;
  137. if (i == numOutputs) {
  138. RROutputChanged(crtc->outputs[j], FALSE);
  139. RRCrtcChanged(crtc, FALSE);
  140. }
  141. }
  142. /*
  143. * Reallocate the crtc output array if necessary
  144. */
  145. if (numOutputs != crtc->numOutputs) {
  146. RROutputPtr *newoutputs;
  147. if (numOutputs) {
  148. if (crtc->numOutputs)
  149. newoutputs = realloc(crtc->outputs,
  150. numOutputs * sizeof(RROutputPtr));
  151. else
  152. newoutputs = malloc(numOutputs * sizeof(RROutputPtr));
  153. if (!newoutputs)
  154. return FALSE;
  155. }
  156. else {
  157. if (crtc->outputs)
  158. free(crtc->outputs);
  159. newoutputs = NULL;
  160. }
  161. crtc->outputs = newoutputs;
  162. crtc->numOutputs = numOutputs;
  163. }
  164. /*
  165. * Copy the new list of outputs into the crtc
  166. */
  167. memcpy(crtc->outputs, outputs, numOutputs * sizeof(RROutputPtr));
  168. /*
  169. * Update remaining crtc fields
  170. */
  171. if (mode != crtc->mode) {
  172. if (crtc->mode)
  173. RRModeDestroy(crtc->mode);
  174. crtc->mode = mode;
  175. if (mode != NULL)
  176. mode->refcnt++;
  177. RRCrtcChanged(crtc, TRUE);
  178. }
  179. if (x != crtc->x) {
  180. crtc->x = x;
  181. RRCrtcChanged(crtc, TRUE);
  182. }
  183. if (y != crtc->y) {
  184. crtc->y = y;
  185. RRCrtcChanged(crtc, TRUE);
  186. }
  187. if (rotation != crtc->rotation) {
  188. crtc->rotation = rotation;
  189. RRCrtcChanged(crtc, TRUE);
  190. }
  191. return TRUE;
  192. }
  193. void
  194. RRDeliverCrtcEvent(ClientPtr client, WindowPtr pWin, RRCrtcPtr crtc)
  195. {
  196. ScreenPtr pScreen = pWin->drawable.pScreen;
  197. rrScrPriv(pScreen);
  198. xRRCrtcChangeNotifyEvent ce;
  199. RRModePtr mode = crtc->mode;
  200. ce.type = RRNotify + RREventBase;
  201. ce.subCode = RRNotify_CrtcChange;
  202. ce.timestamp = pScrPriv->lastSetTime.milliseconds;
  203. ce.window = pWin->drawable.id;
  204. ce.crtc = crtc->id;
  205. ce.rotation = crtc->rotation;
  206. if (mode) {
  207. ce.mode = mode->mode.id;
  208. ce.x = crtc->x;
  209. ce.y = crtc->y;
  210. ce.width = mode->mode.width;
  211. ce.height = mode->mode.height;
  212. }
  213. else {
  214. ce.mode = None;
  215. ce.x = 0;
  216. ce.y = 0;
  217. ce.width = 0;
  218. ce.height = 0;
  219. }
  220. WriteEventsToClient(client, 1, (xEvent *) &ce);
  221. }
  222. /*
  223. * Request that the Crtc be reconfigured
  224. */
  225. Bool
  226. RRCrtcSet(RRCrtcPtr crtc,
  227. RRModePtr mode,
  228. int x,
  229. int y, Rotation rotation, int numOutputs, RROutputPtr * outputs)
  230. {
  231. ScreenPtr pScreen = crtc->pScreen;
  232. /* See if nothing changed */
  233. if (crtc->mode == mode &&
  234. crtc->x == x &&
  235. crtc->y == y &&
  236. crtc->rotation == rotation &&
  237. crtc->numOutputs == numOutputs &&
  238. !memcmp(crtc->outputs, outputs, numOutputs * sizeof(RROutputPtr))) {
  239. return TRUE;
  240. }
  241. if (pScreen) {
  242. #if RANDR_12_INTERFACE
  243. rrScrPriv(pScreen);
  244. if (pScrPriv->rrCrtcSet) {
  245. return (*pScrPriv->rrCrtcSet) (pScreen, crtc, mode, x, y,
  246. rotation, numOutputs, outputs);
  247. }
  248. #endif
  249. #if RANDR_10_INTERFACE
  250. if (pScrPriv->rrSetConfig) {
  251. RRScreenSize size;
  252. RRScreenRate rate;
  253. if (!mode) {
  254. RRCrtcNotify(crtc, NULL, x, y, rotation, 0, NULL);
  255. return TRUE;
  256. }
  257. Bool ret;
  258. size.width = mode->mode.width;
  259. size.height = mode->mode.height;
  260. if (outputs[0]->mmWidth && outputs[0]->mmHeight) {
  261. size.mmWidth = outputs[0]->mmWidth;
  262. size.mmHeight = outputs[0]->mmHeight;
  263. }
  264. else {
  265. size.mmWidth = pScreen->mmWidth;
  266. size.mmHeight = pScreen->mmHeight;
  267. }
  268. size.nRates = 1;
  269. rate.rate = RRVerticalRefresh(&mode->mode);
  270. size.pRates = &rate;
  271. ret =
  272. (*pScrPriv->rrSetConfig) (pScreen, rotation, rate.rate, &size);
  273. /*
  274. * Old 1.0 interface tied screen size to mode size
  275. */
  276. if (ret) {
  277. RRCrtcNotify(crtc, mode, x, y, rotation, 1, outputs);
  278. RRScreenSizeNotify(pScreen);
  279. }
  280. return ret;
  281. }
  282. #endif
  283. RRTellChanged(pScreen);
  284. }
  285. return FALSE;
  286. }
  287. /*
  288. * Destroy a Crtc at shutdown
  289. */
  290. void
  291. RRCrtcDestroy(RRCrtcPtr crtc)
  292. {
  293. FreeResource(crtc->id, 0);
  294. }
  295. static int
  296. RRCrtcDestroyResource(pointer value, XID pid)
  297. {
  298. RRCrtcPtr crtc = (RRCrtcPtr) value;
  299. ScreenPtr pScreen = crtc->pScreen;
  300. if (pScreen) {
  301. rrScrPriv(pScreen);
  302. int i;
  303. for (i = 0; i < pScrPriv->numCrtcs; i++) {
  304. if (pScrPriv->crtcs[i] == crtc) {
  305. memmove(pScrPriv->crtcs + i, pScrPriv->crtcs + i + 1,
  306. (pScrPriv->numCrtcs - (i + 1)) * sizeof(RRCrtcPtr));
  307. --pScrPriv->numCrtcs;
  308. break;
  309. }
  310. }
  311. }
  312. if (crtc->gammaRed)
  313. free(crtc->gammaRed);
  314. free(crtc);
  315. return 1;
  316. }
  317. /*
  318. * Request that the Crtc gamma be changed
  319. */
  320. Bool
  321. RRCrtcGammaSet(RRCrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue)
  322. {
  323. Bool ret = TRUE;
  324. #if RANDR_12_INTERFACE
  325. ScreenPtr pScreen = crtc->pScreen;
  326. #endif
  327. memcpy(crtc->gammaRed, red, crtc->gammaSize * sizeof(CARD16));
  328. memcpy(crtc->gammaGreen, green, crtc->gammaSize * sizeof(CARD16));
  329. memcpy(crtc->gammaBlue, blue, crtc->gammaSize * sizeof(CARD16));
  330. #if RANDR_12_INTERFACE
  331. if (pScreen) {
  332. rrScrPriv(pScreen);
  333. if (pScrPriv->rrCrtcSetGamma)
  334. ret = (*pScrPriv->rrCrtcSetGamma) (pScreen, crtc);
  335. }
  336. #endif
  337. return ret;
  338. }
  339. /*
  340. * Notify the extension that the Crtc gamma has been changed
  341. * The driver calls this whenever it has changed the gamma values
  342. * in the RRCrtcRec
  343. */
  344. Bool
  345. RRCrtcGammaNotify(RRCrtcPtr crtc)
  346. {
  347. return TRUE; /* not much going on here */
  348. }
  349. /**
  350. * Returns the width/height that the crtc scans out from the framebuffer
  351. */
  352. void
  353. RRCrtcGetScanoutSize(RRCrtcPtr crtc, int *width, int *height)
  354. {
  355. if (crtc->mode == NULL) {
  356. *width = 0;
  357. *height = 0;
  358. return;
  359. }
  360. switch (crtc->rotation & 0xf) {
  361. case RR_Rotate_0:
  362. case RR_Rotate_180:
  363. *width = crtc->mode->mode.width;
  364. *height = crtc->mode->mode.height;
  365. break;
  366. case RR_Rotate_90:
  367. case RR_Rotate_270:
  368. *width = crtc->mode->mode.height;
  369. *height = crtc->mode->mode.width;
  370. break;
  371. }
  372. }
  373. /*
  374. * Set the size of the gamma table at server startup time
  375. */
  376. Bool
  377. RRCrtcGammaSetSize(RRCrtcPtr crtc, int size)
  378. {
  379. CARD16 *gamma;
  380. if (size == crtc->gammaSize)
  381. return TRUE;
  382. if (size) {
  383. gamma = malloc(size * 3 * sizeof(CARD16));
  384. if (!gamma)
  385. return FALSE;
  386. }
  387. else
  388. gamma = NULL;
  389. if (crtc->gammaRed)
  390. free(crtc->gammaRed);
  391. crtc->gammaRed = gamma;
  392. crtc->gammaGreen = gamma + size;
  393. crtc->gammaBlue = gamma + size * 2;
  394. crtc->gammaSize = size;
  395. return TRUE;
  396. }
  397. /*
  398. * Initialize crtc type
  399. */
  400. Bool
  401. RRCrtcInit(void)
  402. {
  403. RRCrtcType = CreateNewResourceType(RRCrtcDestroyResource);
  404. if (!RRCrtcType)
  405. return FALSE;
  406. #ifdef XResExtension
  407. RegisterResourceName(RRCrtcType, "CRTC");
  408. #endif
  409. return TRUE;
  410. }
  411. int
  412. ProcRRGetCrtcInfo(ClientPtr client)
  413. {
  414. REQUEST(xRRGetCrtcInfoReq);;
  415. xRRGetCrtcInfoReply rep;
  416. RRCrtcPtr crtc;
  417. CARD8 *extra;
  418. unsigned long extraLen;
  419. ScreenPtr pScreen;
  420. rrScrPrivPtr pScrPriv;
  421. RRModePtr mode;
  422. RROutput *outputs;
  423. RROutput *possible;
  424. int i, j, k;
  425. REQUEST_SIZE_MATCH(xRRGetCrtcInfoReq);
  426. crtc = LookupCrtc(client, stuff->crtc, SecurityReadAccess);
  427. if (!crtc)
  428. return RRErrorBase + BadRRCrtc;
  429. /* All crtcs must be associated with screens before client
  430. * requests are processed
  431. */
  432. pScreen = crtc->pScreen;
  433. pScrPriv = rrGetScrPriv(pScreen);
  434. mode = crtc->mode;
  435. rep.type = X_Reply;
  436. rep.status = RRSetConfigSuccess;
  437. rep.sequenceNumber = client->sequence;
  438. rep.length = 0;
  439. rep.timestamp = pScrPriv->lastSetTime.milliseconds;
  440. rep.x = crtc->x;
  441. rep.y = crtc->y;
  442. rep.width = mode ? mode->mode.width : 0;
  443. rep.height = mode ? mode->mode.height : 0;
  444. rep.mode = mode ? mode->mode.id : 0;
  445. rep.rotation = crtc->rotation;
  446. rep.rotations = crtc->rotations;
  447. rep.nOutput = crtc->numOutputs;
  448. k = 0;
  449. for (i = 0; i < pScrPriv->numOutputs; i++)
  450. for (j = 0; j < pScrPriv->outputs[i]->numCrtcs; j++)
  451. if (pScrPriv->outputs[i]->crtcs[j] == crtc)
  452. k++;
  453. rep.nPossibleOutput = k;
  454. rep.length = rep.nOutput + rep.nPossibleOutput;
  455. extraLen = rep.length << 2;
  456. if (extraLen) {
  457. extra = malloc(extraLen);
  458. if (!extra)
  459. return BadAlloc;
  460. }
  461. else
  462. extra = NULL;
  463. outputs = (RROutput *) extra;
  464. possible = (RROutput *) (outputs + rep.nOutput);
  465. for (i = 0; i < crtc->numOutputs; i++) {
  466. outputs[i] = crtc->outputs[i]->id;
  467. if (client->swapped)
  468. swapl(&outputs[i]);
  469. }
  470. k = 0;
  471. for (i = 0; i < pScrPriv->numOutputs; i++)
  472. for (j = 0; j < pScrPriv->outputs[i]->numCrtcs; j++)
  473. if (pScrPriv->outputs[i]->crtcs[j] == crtc) {
  474. possible[k] = pScrPriv->outputs[i]->id;
  475. if (client->swapped)
  476. swapl(&possible[k]);
  477. k++;
  478. }
  479. if (client->swapped) {
  480. swaps(&rep.sequenceNumber);
  481. swapl(&rep.length);
  482. swapl(&rep.timestamp);
  483. swaps(&rep.x);
  484. swaps(&rep.y);
  485. swaps(&rep.width);
  486. swaps(&rep.height);
  487. swapl(&rep.mode);
  488. swaps(&rep.rotation);
  489. swaps(&rep.rotations);
  490. swaps(&rep.nOutput);
  491. swaps(&rep.nPossibleOutput);
  492. }
  493. WriteToClient(client, sizeof(xRRGetCrtcInfoReply), (char *) &rep);
  494. if (extraLen) {
  495. WriteToClient(client, extraLen, (char *) extra);
  496. free(extra);
  497. }
  498. return client->noClientException;
  499. }
  500. int
  501. ProcRRSetCrtcConfig(ClientPtr client)
  502. {
  503. REQUEST(xRRSetCrtcConfigReq);
  504. xRRSetCrtcConfigReply rep;
  505. ScreenPtr pScreen;
  506. rrScrPrivPtr pScrPriv;
  507. RRCrtcPtr crtc;
  508. RRModePtr mode;
  509. int numOutputs;
  510. RROutputPtr *outputs = NULL;
  511. RROutput *outputIds;
  512. TimeStamp configTime;
  513. TimeStamp time;
  514. Rotation rotation;
  515. int i, j;
  516. REQUEST_AT_LEAST_SIZE(xRRSetCrtcConfigReq);
  517. numOutputs = (stuff->length - (SIZEOF(xRRSetCrtcConfigReq) >> 2));
  518. crtc = LookupIDByType(stuff->crtc, RRCrtcType);
  519. if (!crtc) {
  520. client->errorValue = stuff->crtc;
  521. return RRErrorBase + BadRRCrtc;
  522. }
  523. if (stuff->mode == None) {
  524. mode = NULL;
  525. if (numOutputs > 0)
  526. return BadMatch;
  527. }
  528. else {
  529. mode = LookupIDByType(stuff->mode, RRModeType);
  530. if (!mode) {
  531. client->errorValue = stuff->mode;
  532. return RRErrorBase + BadRRMode;
  533. }
  534. if (numOutputs == 0)
  535. return BadMatch;
  536. }
  537. if (numOutputs) {
  538. outputs = malloc(numOutputs * sizeof(RROutputPtr));
  539. if (!outputs)
  540. return BadAlloc;
  541. }
  542. else
  543. outputs = NULL;
  544. outputIds = (RROutput *) (stuff + 1);
  545. for (i = 0; i < numOutputs; i++) {
  546. outputs[i] = (RROutputPtr) LookupIDByType(outputIds[i], RROutputType);
  547. if (!outputs[i]) {
  548. client->errorValue = outputIds[i];
  549. if (outputs)
  550. free(outputs);
  551. return RRErrorBase + BadRROutput;
  552. }
  553. /* validate crtc for this output */
  554. for (j = 0; j < outputs[i]->numCrtcs; j++)
  555. if (outputs[i]->crtcs[j] == crtc)
  556. break;
  557. if (j == outputs[i]->numCrtcs) {
  558. if (outputs)
  559. free(outputs);
  560. return BadMatch;
  561. }
  562. /* validate mode for this output */
  563. for (j = 0; j < outputs[i]->numModes; j++)
  564. if (outputs[i]->modes[j] == mode)
  565. break;
  566. if (j == outputs[i]->numModes) {
  567. if (outputs)
  568. free(outputs);
  569. return BadMatch;
  570. }
  571. }
  572. pScreen = crtc->pScreen;
  573. pScrPriv = rrGetScrPriv(pScreen);
  574. time = ClientTimeToServerTime(stuff->timestamp);
  575. configTime = ClientTimeToServerTime(stuff->configTimestamp);
  576. if (!pScrPriv) {
  577. rep.status = RRSetConfigFailed;
  578. goto sendReply;
  579. }
  580. /*
  581. * if the client's config timestamp is not the same as the last config
  582. * timestamp, then the config information isn't up-to-date and
  583. * can't even be validated
  584. */
  585. if (CompareTimeStamps(configTime, pScrPriv->lastConfigTime) != 0) {
  586. rep.status = RRSetConfigInvalidConfigTime;
  587. goto sendReply;
  588. }
  589. /*
  590. * Validate requested rotation
  591. */
  592. rotation = (Rotation) stuff->rotation;
  593. /* test the rotation bits only! */
  594. switch (rotation & 0xf) {
  595. case RR_Rotate_0:
  596. case RR_Rotate_90:
  597. case RR_Rotate_180:
  598. case RR_Rotate_270:
  599. break;
  600. default:
  601. /*
  602. * Invalid rotation
  603. */
  604. client->errorValue = stuff->rotation;
  605. if (outputs)
  606. free(outputs);
  607. return BadValue;
  608. }
  609. if (mode) {
  610. if ((~crtc->rotations) & rotation) {
  611. /*
  612. * requested rotation or reflection not supported by screen
  613. */
  614. client->errorValue = stuff->rotation;
  615. if (outputs)
  616. free(outputs);
  617. return BadMatch;
  618. }
  619. #ifdef RANDR_12_INTERFACE
  620. /*
  621. * Check screen size bounds if the DDX provides a 1.2 interface
  622. * for setting screen size. Else, assume the CrtcSet sets
  623. * the size along with the mode
  624. */
  625. if (pScrPriv->rrScreenSetSize) {
  626. int source_width = mode->mode.width;
  627. int source_height = mode->mode.height;
  628. if (rotation == RR_Rotate_90 || rotation == RR_Rotate_270) {
  629. source_width = mode->mode.height;
  630. source_height = mode->mode.width;
  631. }
  632. if (stuff->x + source_width > pScreen->width) {
  633. client->errorValue = stuff->x;
  634. if (outputs)
  635. free(outputs);
  636. return BadValue;
  637. }
  638. if (stuff->y + source_height > pScreen->height) {
  639. client->errorValue = stuff->y;
  640. if (outputs)
  641. free(outputs);
  642. return BadValue;
  643. }
  644. }
  645. #endif
  646. }
  647. /*
  648. * Make sure the requested set-time is not older than
  649. * the last set-time
  650. */
  651. if (CompareTimeStamps(time, pScrPriv->lastSetTime) < 0) {
  652. rep.status = RRSetConfigInvalidTime;
  653. goto sendReply;
  654. }
  655. if (!RRCrtcSet(crtc, mode, stuff->x, stuff->y,
  656. rotation, numOutputs, outputs)) {
  657. rep.status = RRSetConfigFailed;
  658. goto sendReply;
  659. }
  660. rep.status = RRSetConfigSuccess;
  661. sendReply:
  662. if (outputs)
  663. free(outputs);
  664. rep.type = X_Reply;
  665. /* rep.status has already been filled in */
  666. rep.length = 0;
  667. rep.sequenceNumber = client->sequence;
  668. rep.newTimestamp = pScrPriv->lastConfigTime.milliseconds;
  669. if (client->swapped) {
  670. swaps(&rep.sequenceNumber);
  671. swapl(&rep.length);
  672. swapl(&rep.newTimestamp);
  673. }
  674. WriteToClient(client, sizeof(xRRSetCrtcConfigReply), (char *) &rep);
  675. return client->noClientException;
  676. }
  677. int
  678. ProcRRGetCrtcGammaSize(ClientPtr client)
  679. {
  680. REQUEST(xRRGetCrtcGammaSizeReq);
  681. xRRGetCrtcGammaSizeReply reply;
  682. RRCrtcPtr crtc;
  683. REQUEST_SIZE_MATCH(xRRGetCrtcGammaSizeReq);
  684. crtc = LookupCrtc(client, stuff->crtc, SecurityReadAccess);
  685. if (!crtc)
  686. return RRErrorBase + BadRRCrtc;
  687. reply.type = X_Reply;
  688. reply.sequenceNumber = client->sequence;
  689. reply.length = 0;
  690. reply.size = crtc->gammaSize;
  691. if (client->swapped) {
  692. swaps(&reply.sequenceNumber);
  693. swapl(&reply.length);
  694. swaps(&reply.size);
  695. }
  696. WriteToClient(client, sizeof(xRRGetCrtcGammaSizeReply), (char *) &reply);
  697. return client->noClientException;
  698. }
  699. int
  700. ProcRRGetCrtcGamma(ClientPtr client)
  701. {
  702. REQUEST(xRRGetCrtcGammaReq);
  703. xRRGetCrtcGammaReply reply;
  704. RRCrtcPtr crtc;
  705. unsigned long len;
  706. REQUEST_SIZE_MATCH(xRRGetCrtcGammaReq);
  707. crtc = LookupCrtc(client, stuff->crtc, SecurityReadAccess);
  708. if (!crtc)
  709. return RRErrorBase + BadRRCrtc;
  710. len = crtc->gammaSize * 3 * 2;
  711. reply.type = X_Reply;
  712. reply.sequenceNumber = client->sequence;
  713. reply.length = (len + 3) >> 2;
  714. reply.size = crtc->gammaSize;
  715. if (client->swapped) {
  716. swaps(&reply.sequenceNumber);
  717. swapl(&reply.length);
  718. swaps(&reply.size);
  719. }
  720. WriteToClient(client, sizeof(xRRGetCrtcGammaReply), (char *) &reply);
  721. if (crtc->gammaSize) {
  722. client->pSwapReplyFunc = (ReplySwapPtr) CopySwap16Write;
  723. WriteSwappedDataToClient(client, len, (char *) crtc->gammaRed);
  724. }
  725. return client->noClientException;
  726. }
  727. int
  728. ProcRRSetCrtcGamma(ClientPtr client)
  729. {
  730. REQUEST(xRRSetCrtcGammaReq);
  731. RRCrtcPtr crtc;
  732. unsigned long len;
  733. CARD16 *red, *green, *blue;
  734. REQUEST_AT_LEAST_SIZE(xRRSetCrtcGammaReq);
  735. crtc = LookupCrtc(client, stuff->crtc, SecurityWriteAccess);
  736. if (!crtc)
  737. return RRErrorBase + BadRRCrtc;
  738. len = client->req_len - (sizeof(xRRSetCrtcGammaReq) >> 2);
  739. if (len < (stuff->size * 3 + 1) >> 1)
  740. return BadLength;
  741. if (stuff->size != crtc->gammaSize)
  742. return BadMatch;
  743. red = (CARD16 *) (stuff + 1);
  744. green = red + crtc->gammaSize;
  745. blue = green + crtc->gammaSize;
  746. RRCrtcGammaSet(crtc, red, green, blue);
  747. return Success;
  748. }