You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RandrGlue.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* Copyright 2018 Peter Astrand <astrand@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_XRANDR
  19. #include <X11/Xlib.h>
  20. #include <X11/extensions/Xrandr.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "RandrGlue.h"
  24. typedef struct _vncGlueContext {
  25. Display *dpy;
  26. XRRScreenResources *res;
  27. } vncGlueContext;
  28. static vncGlueContext randrGlueContext;
  29. void vncSetGlueContext(Display *dpy, void *res)
  30. {
  31. randrGlueContext.dpy = dpy;
  32. randrGlueContext.res = (XRRScreenResources *)res;
  33. }
  34. static RRMode vncRandRGetMatchingMode(XRROutputInfo *output,
  35. unsigned int width, unsigned int height)
  36. {
  37. vncGlueContext *ctx = &randrGlueContext;
  38. /*
  39. * We're not going to change which modes are preferred, but let's
  40. * see if we can at least find a mode with matching dimensions.
  41. */
  42. if (output->crtc) {
  43. XRRCrtcInfo *crtc;
  44. unsigned int swap;
  45. crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtc);
  46. if (!crtc)
  47. return None;
  48. switch (crtc->rotation) {
  49. case RR_Rotate_90:
  50. case RR_Rotate_270:
  51. swap = width;
  52. width = height;
  53. height = swap;
  54. break;
  55. }
  56. XRRFreeCrtcInfo(crtc);
  57. }
  58. for (int i = 0; i < ctx->res->nmode; i++) {
  59. for (int j = 0; j < output->nmode; j++) {
  60. if ((output->modes[j] == ctx->res->modes[i].id) &&
  61. (ctx->res->modes[i].width == width) &&
  62. (ctx->res->modes[i].height == height)) {
  63. return ctx->res->modes[i].id;
  64. }
  65. }
  66. }
  67. return None;
  68. }
  69. int vncGetScreenWidth(void)
  70. {
  71. vncGlueContext *ctx = &randrGlueContext;
  72. return DisplayWidth(ctx->dpy, DefaultScreen(ctx->dpy));
  73. }
  74. int vncGetScreenHeight(void)
  75. {
  76. vncGlueContext *ctx = &randrGlueContext;
  77. return DisplayHeight(ctx->dpy, DefaultScreen(ctx->dpy));
  78. }
  79. int vncRandRIsValidScreenSize(int width, int height)
  80. {
  81. vncGlueContext *ctx = &randrGlueContext;
  82. /* Assert size ranges */
  83. int minwidth, minheight, maxwidth, maxheight;
  84. int ret = XRRGetScreenSizeRange(ctx->dpy, DefaultRootWindow(ctx->dpy),
  85. &minwidth, &minheight,
  86. &maxwidth, &maxheight);
  87. if (!ret) {
  88. return 0;
  89. }
  90. if (width < minwidth || maxwidth < width) {
  91. return 0;
  92. }
  93. if (height < minheight || maxheight < height) {
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. int vncRandRResizeScreen(int width, int height)
  99. {
  100. vncGlueContext *ctx = &randrGlueContext;
  101. int xwidth = DisplayWidth(ctx->dpy, DefaultScreen(ctx->dpy));
  102. int xheight = DisplayHeight(ctx->dpy, DefaultScreen(ctx->dpy));
  103. int xwidthmm = DisplayWidthMM(ctx->dpy, DefaultScreen(ctx->dpy));
  104. int xheightmm = DisplayHeightMM(ctx->dpy, DefaultScreen(ctx->dpy));
  105. /* Try to retain DPI when we resize */
  106. XRRSetScreenSize(ctx->dpy, DefaultRootWindow(ctx->dpy), width, height,
  107. xwidthmm * width / xwidth,
  108. xheightmm * height / xheight);
  109. return 1;
  110. }
  111. void vncRandRUpdateSetTime(void)
  112. {
  113. }
  114. int vncRandRHasOutputClones(void)
  115. {
  116. vncGlueContext *ctx = &randrGlueContext;
  117. for (int i = 0; i < ctx->res->ncrtc; i++) {
  118. XRRCrtcInfo *crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, ctx->res->crtcs[i]);
  119. if (!crtc) {
  120. return 0;
  121. }
  122. if (crtc->noutput > 1) {
  123. XRRFreeCrtcInfo (crtc);
  124. return 1;
  125. }
  126. XRRFreeCrtcInfo (crtc);
  127. }
  128. return 0;
  129. }
  130. int vncRandRGetOutputCount(void)
  131. {
  132. vncGlueContext *ctx = &randrGlueContext;
  133. return ctx->res->noutput;
  134. }
  135. int vncRandRGetAvailableOutputs(void)
  136. {
  137. vncGlueContext *ctx = &randrGlueContext;
  138. int availableOutputs;
  139. RRCrtc *usedCrtcs;
  140. int numUsed;
  141. int i, j, k;
  142. usedCrtcs = (RRCrtc*)malloc(sizeof(RRCrtc) * ctx->res->ncrtc);
  143. if (usedCrtcs == NULL)
  144. return 0;
  145. /*
  146. * This gets slightly complicated because we might need to hook a CRTC
  147. * up to the output, but also check that we don't try to use the same
  148. * CRTC for multiple outputs.
  149. */
  150. availableOutputs = 0;
  151. numUsed = 0;
  152. for (i = 0;i < ctx->res->noutput; i++) {
  153. XRROutputInfo *output;
  154. output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[i]);
  155. if (!output) {
  156. continue;
  157. }
  158. if (output->crtc != None)
  159. availableOutputs++;
  160. else {
  161. for (j = 0;j < output->ncrtc;j++) {
  162. XRRCrtcInfo *crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtcs[j]);
  163. if (!crtc) {
  164. continue;
  165. }
  166. if (crtc->noutput != 0) {
  167. XRRFreeCrtcInfo(crtc);
  168. continue;
  169. }
  170. XRRFreeCrtcInfo(crtc);
  171. for (k = 0;k < numUsed;k++) {
  172. if (usedCrtcs[k] == output->crtcs[j])
  173. break;
  174. }
  175. if (k != numUsed)
  176. continue;
  177. availableOutputs++;
  178. usedCrtcs[numUsed] = output->crtcs[j];
  179. numUsed++;
  180. break;
  181. }
  182. }
  183. XRRFreeOutputInfo(output);
  184. }
  185. free(usedCrtcs);
  186. return availableOutputs;
  187. }
  188. char *vncRandRGetOutputName(int outputIdx)
  189. {
  190. vncGlueContext *ctx = &randrGlueContext;
  191. XRROutputInfo *output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  192. if (!output) {
  193. return strdup("");
  194. }
  195. char *ret = strdup(output->name);
  196. XRRFreeOutputInfo(output);
  197. return ret;
  198. }
  199. int vncRandRIsOutputEnabled(int outputIdx)
  200. {
  201. vncGlueContext *ctx = &randrGlueContext;
  202. XRROutputInfo *output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  203. if (!output) {
  204. return 0;
  205. }
  206. if (output->crtc == None) {
  207. XRRFreeOutputInfo(output);
  208. return 0;
  209. }
  210. XRRCrtcInfo *crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtc);
  211. XRRFreeOutputInfo(output);
  212. if (!crtc) {
  213. return 0;
  214. }
  215. if (crtc->mode == None) {
  216. XRRFreeCrtcInfo(crtc);
  217. return 0;
  218. }
  219. XRRFreeCrtcInfo(crtc);
  220. return 1;
  221. }
  222. int vncRandRIsOutputUsable(int outputIdx)
  223. {
  224. vncGlueContext *ctx = &randrGlueContext;
  225. XRROutputInfo *output;
  226. int i;
  227. output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  228. if (!output) {
  229. return 0;
  230. }
  231. if (output->crtc != None) {
  232. XRRFreeOutputInfo(output);
  233. return 1;
  234. }
  235. /* Any unused CRTCs? */
  236. for (i = 0;i < output->ncrtc;i++) {
  237. XRRCrtcInfo *crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtcs[i]);
  238. if (crtc->noutput == 0) {
  239. XRRFreeOutputInfo(output);
  240. XRRFreeCrtcInfo(crtc);
  241. return 1;
  242. }
  243. XRRFreeCrtcInfo(crtc);
  244. }
  245. XRRFreeOutputInfo(output);
  246. return 0;
  247. }
  248. int vncRandRIsOutputConnected(int outputIdx)
  249. {
  250. vncGlueContext *ctx = &randrGlueContext;
  251. XRROutputInfo *output;
  252. output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  253. if (!output) {
  254. return 0;
  255. }
  256. int ret = (output->connection == RR_Connected);
  257. XRRFreeOutputInfo(output);
  258. return ret;
  259. }
  260. int vncRandRCheckOutputMode(int outputIdx, int width, int height)
  261. {
  262. vncGlueContext *ctx = &randrGlueContext;
  263. XRROutputInfo *output;
  264. RRMode mode;
  265. output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  266. if (!output)
  267. return 0;
  268. /* Make sure we have the mode we want */
  269. mode = vncRandRGetMatchingMode(output, width, height);
  270. XRRFreeOutputInfo(output);
  271. if (mode == None)
  272. return 0;
  273. return 1;
  274. }
  275. int vncRandRDisableOutput(int outputIdx)
  276. {
  277. vncGlueContext *ctx = &randrGlueContext;
  278. RRCrtc crtcid;
  279. int i;
  280. int move = 0;
  281. XRROutputInfo *output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  282. if (!output) {
  283. return 0;
  284. }
  285. crtcid = output->crtc;
  286. if (crtcid == 0) {
  287. XRRFreeOutputInfo(output);
  288. return 1;
  289. }
  290. XRRCrtcInfo *crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtc);
  291. XRRFreeOutputInfo(output);
  292. if (!crtc) {
  293. return 0;
  294. }
  295. /* Remove this output from the CRTC configuration */
  296. for (i = 0; i < crtc->noutput; i++) {
  297. if (ctx->res->outputs[outputIdx] == crtc->outputs[i]) {
  298. crtc->noutput -= 1;
  299. move = 1;
  300. }
  301. if (move && i < crtc->noutput) {
  302. crtc->outputs[i] = crtc->outputs[i+1];
  303. }
  304. }
  305. if (crtc->noutput == 0) {
  306. crtc->mode = None;
  307. crtc->outputs = NULL;
  308. }
  309. int ret = XRRSetCrtcConfig(ctx->dpy,
  310. ctx->res,
  311. crtcid,
  312. CurrentTime,
  313. crtc->x, crtc->y,
  314. crtc->mode, crtc->rotation,
  315. crtc->outputs, crtc->noutput);
  316. XRRFreeCrtcInfo(crtc);
  317. return (ret == RRSetConfigSuccess);
  318. }
  319. unsigned int vncRandRGetOutputId(int outputIdx)
  320. {
  321. vncGlueContext *ctx = &randrGlueContext;
  322. return ctx->res->outputs[outputIdx];
  323. }
  324. int vncRandRGetOutputDimensions(int outputIdx,
  325. int *x, int *y, int *width, int *height)
  326. {
  327. vncGlueContext *ctx = &randrGlueContext;
  328. int swap;
  329. *x = *y = *width = *height = 0;
  330. XRROutputInfo *output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  331. if (!output) {
  332. return 1;
  333. }
  334. if (!output->crtc) {
  335. XRRFreeOutputInfo(output);
  336. return 1;
  337. }
  338. XRRCrtcInfo *crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtc);
  339. XRRFreeOutputInfo(output);
  340. if (!crtc) {
  341. return 1;
  342. }
  343. if (crtc->mode == None) {
  344. XRRFreeCrtcInfo(crtc);
  345. return 1;
  346. }
  347. *x = crtc->x;
  348. *y = crtc->y;
  349. for (int m = 0; m < ctx->res->nmode; m++) {
  350. if (crtc->mode == ctx->res->modes[m].id) {
  351. *width = ctx->res->modes[m].width;
  352. *height = ctx->res->modes[m].height;
  353. }
  354. }
  355. switch (crtc->rotation) {
  356. case RR_Rotate_90:
  357. case RR_Rotate_270:
  358. swap = *width;
  359. *width = *height;
  360. *height = swap;
  361. break;
  362. }
  363. XRRFreeCrtcInfo(crtc);
  364. return 0;
  365. }
  366. int vncRandRReconfigureOutput(int outputIdx, int x, int y,
  367. int width, int height)
  368. {
  369. vncGlueContext *ctx = &randrGlueContext;
  370. XRROutputInfo *output;
  371. RRCrtc crtcid;
  372. RRMode mode;
  373. XRRCrtcInfo *crtc = NULL;
  374. int i, ret;
  375. output = XRRGetOutputInfo(ctx->dpy, ctx->res, ctx->res->outputs[outputIdx]);
  376. if (!output) {
  377. return 0;
  378. }
  379. crtcid = output->crtc;
  380. /* Need a CRTC? */
  381. if (crtcid == None) {
  382. for (i = 0;i < output->ncrtc;i++) {
  383. crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, output->crtcs[i]);
  384. if (!crtc) {
  385. continue;
  386. }
  387. if (crtc->noutput != 0) {
  388. XRRFreeCrtcInfo(crtc);
  389. continue;
  390. }
  391. crtcid = output->crtcs[i];
  392. crtc->rotation = RR_Rotate_0;
  393. break;
  394. }
  395. } else {
  396. crtc = XRRGetCrtcInfo(ctx->dpy, ctx->res, crtcid);
  397. }
  398. /* Couldn't find one... */
  399. if (crtc == NULL) {
  400. XRRFreeOutputInfo(output);
  401. return 0;
  402. }
  403. /* Make sure we have the mode we want */
  404. mode = vncRandRGetMatchingMode(output, width, height);
  405. if (mode == None) {
  406. XRRFreeCrtcInfo(crtc);
  407. XRRFreeOutputInfo(output);
  408. return 0;
  409. }
  410. /* Reconfigure new mode and position */
  411. ret = XRRSetCrtcConfig (ctx->dpy, ctx->res, crtcid, CurrentTime, x, y,
  412. mode, crtc->rotation, ctx->res->outputs+outputIdx, 1);
  413. XRRFreeCrtcInfo(crtc);
  414. XRRFreeOutputInfo(output);
  415. return (ret == RRSetConfigSuccess);
  416. }
  417. int vncRandRCanCreateOutputs(int extraOutputs)
  418. {
  419. return 0;
  420. }
  421. int vncRandRCreateOutputs(int extraOutputs)
  422. {
  423. return 0;
  424. }
  425. #endif /* HAVE_XRANDR */