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.

xvnc.c 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /* Copyright (c) 1993 X Consortium
  2. Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  3. Copyright 2009-2015 Pierre Ossman for Cendio AB
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  17. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  18. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  19. OTHER DEALINGS IN THE SOFTWARE.
  20. Except as contained in this notice, the name of the X Consortium shall
  21. not be used in advertising or otherwise to promote the sale, use or
  22. other dealings in this Software without prior written authorization
  23. from the X Consortium.
  24. */
  25. #ifdef HAVE_DIX_CONFIG_H
  26. #include <dix-config.h>
  27. #endif
  28. #include "vncExtInit.h"
  29. #include "RFBGlue.h"
  30. #include "XorgGlue.h"
  31. #include "RandrGlue.h"
  32. #include "xorg-version.h"
  33. #include <stdio.h>
  34. #include <X11/X.h>
  35. #include <X11/Xproto.h>
  36. #include <X11/Xos.h>
  37. #include "scrnintstr.h"
  38. #include "servermd.h"
  39. #include "fb.h"
  40. #include "mi.h"
  41. #include "gcstruct.h"
  42. #include "input.h"
  43. #include "mipointer.h"
  44. #include "micmap.h"
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <errno.h>
  48. #include <sys/param.h>
  49. #include "dix.h"
  50. #include "os.h"
  51. #include "miline.h"
  52. #include "glx_extinit.h"
  53. #include "inputstr.h"
  54. #include "randrstr.h"
  55. #ifdef DPMSExtension
  56. #include "dpmsproc.h"
  57. #endif
  58. #include <X11/keysym.h>
  59. extern char buildtime[];
  60. #undef VENDOR_RELEASE
  61. #undef VENDOR_STRING
  62. #include "version-config.h"
  63. #define XVNCVERSION "TigerVNC 1.12.80"
  64. #define XVNCCOPYRIGHT ("Copyright (C) 1999-2022 TigerVNC Team and many others (see README.rst)\n" \
  65. "See https://www.tigervnc.org for information on TigerVNC.\n")
  66. #define VNC_DEFAULT_WIDTH 1024
  67. #define VNC_DEFAULT_HEIGHT 768
  68. #define VNC_DEFAULT_DEPTH 24
  69. typedef struct {
  70. int width;
  71. int paddedBytesWidth;
  72. int paddedWidth;
  73. int height;
  74. int depth;
  75. int bitsPerPixel;
  76. void *pfbMemory;
  77. } VncFramebufferInfo, *VncFramebufferInfoPtr;
  78. typedef struct {
  79. VncFramebufferInfo fb;
  80. Bool pixelFormatDefined;
  81. Bool rgbNotBgr;
  82. int redBits, greenBits, blueBits;
  83. } VncScreenInfo, *VncScreenInfoPtr;
  84. static VncScreenInfo vncScreenInfo = {
  85. .fb.width = VNC_DEFAULT_WIDTH,
  86. .fb.height = VNC_DEFAULT_HEIGHT,
  87. .fb.depth = VNC_DEFAULT_DEPTH,
  88. .fb.pfbMemory = NULL,
  89. .pixelFormatDefined = FALSE,
  90. };
  91. static Bool vncPixmapDepths[33];
  92. static Bool Render = TRUE;
  93. static Bool displaySpecified = FALSE;
  94. static char displayNumStr[16];
  95. static int vncVerbose = 0;
  96. static void
  97. vncPrintBanner(void)
  98. {
  99. ErrorF("\nXvnc %s - built %s\n%s", XVNCVERSION, buildtime, XVNCCOPYRIGHT);
  100. ErrorF("Underlying X server release %d\n\n", VENDOR_RELEASE);
  101. }
  102. static void
  103. vncInitializePixmapDepths(void)
  104. {
  105. int i;
  106. vncPixmapDepths[1] = TRUE; /* always need bitmaps */
  107. for (i = 2; i <= 32; i++)
  108. vncPixmapDepths[i] = FALSE;
  109. }
  110. static int
  111. vncBitsPerPixel(int depth)
  112. {
  113. if (depth == 1)
  114. return 1;
  115. else if (depth <= 8)
  116. return 8;
  117. else if (depth <= 16)
  118. return 16;
  119. else
  120. return 32;
  121. }
  122. static void vncFreeFramebufferMemory(VncFramebufferInfoPtr pfb);
  123. #ifdef DPMSExtension
  124. #if XORG_OLDER_THAN(1, 20, 0)
  125. /* Why support DPMS? Because stupid modern desktop environments
  126. such as Unity 2D on Ubuntu 11.10 crashes if DPMS is not
  127. available. (DPMSSet is called by dpms.c, but the return value
  128. is ignored.) */
  129. int
  130. DPMSSet(ClientPtr client, int level)
  131. {
  132. return Success;
  133. }
  134. Bool
  135. DPMSSupported(void)
  136. {
  137. /* Causes DPMSCapable to return false, meaning no devices are DPMS
  138. capable */
  139. return FALSE;
  140. }
  141. #endif
  142. #endif
  143. void
  144. ddxGiveUp(enum ExitCode error)
  145. {
  146. /* clean up the framebuffers */
  147. vncFreeFramebufferMemory(&vncScreenInfo.fb);
  148. }
  149. void
  150. AbortDDX(enum ExitCode error)
  151. {
  152. ddxGiveUp(error);
  153. }
  154. void
  155. OsVendorInit(void)
  156. {
  157. }
  158. void
  159. OsVendorFatalError(const char *f, va_list args)
  160. {
  161. }
  162. #ifdef DDXBEFORERESET
  163. void
  164. ddxBeforeReset(void)
  165. {
  166. return;
  167. }
  168. #endif
  169. #if INPUTTHREAD
  170. #if XORG_AT_LEAST(1, 20, 7)
  171. /** This function is called in Xserver/os/inputthread.c when starting
  172. the input thread. */
  173. void
  174. ddxInputThreadInit(void)
  175. {
  176. }
  177. #endif
  178. #endif
  179. void
  180. ddxUseMsg(void)
  181. {
  182. vncPrintBanner();
  183. ErrorF("-pixdepths list-of-int support given pixmap depths\n");
  184. ErrorF("+/-render turn on/off RENDER extension support"
  185. "(default on)\n");
  186. ErrorF("-geometry WxH set screen 0's width, height\n");
  187. ErrorF("-depth D set screen 0's depth\n");
  188. ErrorF("-pixelformat fmt set pixel format (rgbNNN or bgrNNN)\n");
  189. ErrorF("-inetd has been launched from inetd\n");
  190. ErrorF
  191. ("-noclipboard disable clipboard settings modification via vncconfig utility\n");
  192. ErrorF("-verbose [n] verbose startup messages\n");
  193. ErrorF("-quiet minimal startup messages\n");
  194. ErrorF("-version show the server version\n");
  195. ErrorF("\nVNC parameters:\n");
  196. fprintf(stderr, "\n"
  197. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  198. "Parameters which take a value can be specified as "
  199. "-<param> <value>\n"
  200. "Other valid forms are <param>=<value> -<param>=<value> "
  201. "--<param>=<value>\n"
  202. "Parameter names are case-insensitive. The parameters are:\n\n");
  203. vncListParams(79, 14);
  204. }
  205. static Bool
  206. displayNumFree(int num)
  207. {
  208. char file[256];
  209. if (vncIsTCPPortUsed(6000 + num))
  210. return FALSE;
  211. sprintf(file, "/tmp/.X%d-lock", num);
  212. if (access(file, F_OK) == 0)
  213. return FALSE;
  214. sprintf(file, "/tmp/.X11-unix/X%d", num);
  215. if (access(file, F_OK) == 0)
  216. return FALSE;
  217. sprintf(file, "/usr/spool/sockets/X11/%d", num);
  218. if (access(file, F_OK) == 0)
  219. return FALSE;
  220. return TRUE;
  221. }
  222. int
  223. ddxProcessArgument(int argc, char *argv[], int i)
  224. {
  225. static Bool firstTime = TRUE;
  226. if (firstTime) {
  227. /* Force -noreset as default until we properly handle resets */
  228. dispatchExceptionAtReset = 0;
  229. vncInitializePixmapDepths();
  230. firstTime = FALSE;
  231. vncInitRFB();
  232. }
  233. if (argv[i][0] == ':')
  234. displaySpecified = TRUE;
  235. #define CHECK_FOR_REQUIRED_ARGUMENTS(num) \
  236. if (((i + num) >= argc) || (!argv[i + num])) { \
  237. ErrorF("Required argument to %s not specified\n", argv[i]); \
  238. UseMsg(); \
  239. FatalError("Required argument to %s not specified\n", argv[i]); \
  240. }
  241. if (strcmp(argv[i], "-pixdepths") == 0) { /* -pixdepths list-of-depth */
  242. int depth, ret = 1;
  243. CHECK_FOR_REQUIRED_ARGUMENTS(1);
  244. ++i;
  245. while ((i < argc) && (depth = atoi(argv[i++])) != 0) {
  246. if (depth < 0 || depth > 32) {
  247. ErrorF("Invalid pixmap depth %d\n", depth);
  248. UseMsg();
  249. FatalError("Invalid pixmap depth %d passed to -pixdepths\n",
  250. depth);
  251. }
  252. vncPixmapDepths[depth] = TRUE;
  253. ret++;
  254. }
  255. return ret;
  256. }
  257. if (strcmp(argv[i], "+render") == 0) { /* +render */
  258. Render = TRUE;
  259. return 1;
  260. }
  261. if (strcmp(argv[i], "-render") == 0) { /* -render */
  262. Render = FALSE;
  263. return 1;
  264. }
  265. if (strcmp(argv[i], "-geometry") == 0) {
  266. CHECK_FOR_REQUIRED_ARGUMENTS(1);
  267. ++i;
  268. if (sscanf(argv[i], "%dx%d", &vncScreenInfo.fb.width,
  269. &vncScreenInfo.fb.height) != 2) {
  270. ErrorF("Invalid geometry %s\n", argv[i]);
  271. UseMsg();
  272. FatalError("Invalid geometry %s passed to -geometry\n",
  273. argv[i]);
  274. }
  275. return 2;
  276. }
  277. if (strcmp(argv[i], "-depth") == 0) {
  278. CHECK_FOR_REQUIRED_ARGUMENTS(1);
  279. ++i;
  280. vncScreenInfo.fb.depth = atoi(argv[i]);
  281. return 2;
  282. }
  283. if (strcmp(argv[i], "-pixelformat") == 0) {
  284. char rgbbgr[4];
  285. int bits1, bits2, bits3;
  286. CHECK_FOR_REQUIRED_ARGUMENTS(1);
  287. ++i;
  288. if (sscanf(argv[i], "%3s%1d%1d%1d", rgbbgr, &bits1, &bits2, &bits3) < 4) {
  289. ErrorF("Invalid pixel format %s\n", argv[i]);
  290. UseMsg();
  291. FatalError("Invalid pixel format %s passed to -pixelformat\n",
  292. argv[i]);
  293. }
  294. vncScreenInfo.pixelFormatDefined = TRUE;
  295. vncScreenInfo.fb.depth = bits1 + bits2 + bits3;
  296. vncScreenInfo.greenBits = bits2;
  297. if (strcasecmp(rgbbgr, "bgr") == 0) {
  298. vncScreenInfo.rgbNotBgr = FALSE;
  299. vncScreenInfo.redBits = bits3;
  300. vncScreenInfo.blueBits = bits1;
  301. } else if (strcasecmp(rgbbgr, "rgb") == 0) {
  302. vncScreenInfo.rgbNotBgr = TRUE;
  303. vncScreenInfo.redBits = bits1;
  304. vncScreenInfo.blueBits = bits3;
  305. } else {
  306. ErrorF("Invalid pixel format %s\n", argv[i]);
  307. UseMsg();
  308. FatalError("Invalid pixel format %s passed to -pixelformat\n",
  309. argv[i]);
  310. }
  311. return 2;
  312. }
  313. if (strcmp(argv[i], "-inetd") == 0) {
  314. int nullfd;
  315. dup2(0, 3);
  316. vncInetdSock = 3;
  317. /* Avoid xserver >= 1.19's epoll-fd becoming fd 2 / stderr only to be
  318. replaced by /dev/null by OsInit() because the pollfd is not
  319. writable, breaking ospoll_wait(). */
  320. nullfd = open("/dev/null", O_WRONLY);
  321. dup2(nullfd, 2);
  322. close(nullfd);
  323. if (!displaySpecified) {
  324. int port = vncGetSocketPort(vncInetdSock);
  325. int displayNum = port - 5900;
  326. if (displayNum < 0 || displayNum > 99 ||
  327. !displayNumFree(displayNum)) {
  328. for (displayNum = 1; displayNum < 100; displayNum++)
  329. if (displayNumFree(displayNum))
  330. break;
  331. if (displayNum == 100)
  332. FatalError
  333. ("Xvnc error: no free display number for -inetd\n");
  334. }
  335. display = displayNumStr;
  336. sprintf(displayNumStr, "%d", displayNum);
  337. }
  338. return 1;
  339. }
  340. if (strcmp(argv[i], "-noclipboard") == 0) {
  341. vncNoClipboard = 1;
  342. return 1;
  343. }
  344. if (!strcmp(argv[i], "-verbose")) {
  345. if (++i < argc && argv[i]) {
  346. char *end;
  347. long val;
  348. val = strtol(argv[i], &end, 0);
  349. if (*end == '\0') {
  350. vncVerbose = val;
  351. LogSetParameter(XLOG_VERBOSITY, vncVerbose);
  352. return 2;
  353. }
  354. }
  355. vncVerbose++;
  356. LogSetParameter(XLOG_VERBOSITY, vncVerbose);
  357. return 1;
  358. }
  359. if (!strcmp(argv[i], "-quiet")) {
  360. vncVerbose = -1;
  361. LogSetParameter(XLOG_VERBOSITY, vncVerbose);
  362. return 1;
  363. }
  364. if (!strcmp(argv[i], "-showconfig") || !strcmp(argv[i], "-version")) {
  365. vncPrintBanner();
  366. exit(0);
  367. }
  368. /* We need to resolve an ambiguity for booleans */
  369. if (argv[i][0] == '-' && i + 1 < argc && vncIsParamBool(&argv[i][1])) {
  370. if ((strcasecmp(argv[i + 1], "0") == 0) ||
  371. (strcasecmp(argv[i + 1], "1") == 0) ||
  372. (strcasecmp(argv[i + 1], "true") == 0) ||
  373. (strcasecmp(argv[i + 1], "false") == 0) ||
  374. (strcasecmp(argv[i + 1], "yes") == 0) ||
  375. (strcasecmp(argv[i + 1], "no") == 0)) {
  376. vncSetParam(&argv[i][1], argv[i + 1]);
  377. return 2;
  378. }
  379. }
  380. if (vncSetParamSimple(argv[i]))
  381. return 1;
  382. if (argv[i][0] == '-' && i + 1 < argc) {
  383. if (vncSetParam(&argv[i][1], argv[i + 1]))
  384. return 2;
  385. }
  386. return 0;
  387. }
  388. static Bool
  389. vncSaveScreen(ScreenPtr pScreen, int on)
  390. {
  391. return TRUE;
  392. }
  393. static void *
  394. vncAllocateFramebufferMemory(VncFramebufferInfoPtr pfb)
  395. {
  396. size_t sizeInBytes;
  397. if (pfb->pfbMemory != NULL)
  398. return pfb->pfbMemory; /* already done */
  399. /* Compute memory layout */
  400. pfb->paddedBytesWidth = PixmapBytePad(pfb->width, pfb->depth);
  401. pfb->bitsPerPixel = vncBitsPerPixel(pfb->depth);
  402. pfb->paddedWidth = pfb->paddedBytesWidth * 8 / pfb->bitsPerPixel;
  403. /* And allocate buffer */
  404. sizeInBytes = pfb->paddedBytesWidth * pfb->height;
  405. pfb->pfbMemory = malloc(sizeInBytes);
  406. /* This will be NULL if the above failed */
  407. return pfb->pfbMemory;
  408. }
  409. static void
  410. vncFreeFramebufferMemory(VncFramebufferInfoPtr pfb)
  411. {
  412. if ((pfb == NULL) || (pfb->pfbMemory == NULL))
  413. return;
  414. free(pfb->pfbMemory);
  415. pfb->pfbMemory = NULL;
  416. }
  417. static Bool
  418. vncCursorOffScreen(ScreenPtr *ppScreen, int *x, int *y)
  419. {
  420. return FALSE;
  421. }
  422. static void
  423. vncCrossScreen(ScreenPtr pScreen, Bool entering)
  424. {
  425. }
  426. static Bool
  427. vncRealizeCursor(DeviceIntPtr pDev, ScreenPtr pScreen, CursorPtr pCursor)
  428. {
  429. return TRUE;
  430. }
  431. static Bool
  432. vncUnrealizeCursor(DeviceIntPtr pDev, ScreenPtr pScreen, CursorPtr pCursor)
  433. {
  434. return TRUE;
  435. }
  436. static void
  437. vncSetCursor(DeviceIntPtr pDev,
  438. ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
  439. {
  440. }
  441. static void
  442. vncMoveCursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
  443. {
  444. }
  445. static Bool
  446. vncDeviceCursorInitialize(DeviceIntPtr pDev, ScreenPtr pScreen)
  447. {
  448. return TRUE;
  449. }
  450. static void
  451. vncDeviceCursorCleanup(DeviceIntPtr pDev, ScreenPtr pScreen)
  452. {
  453. }
  454. static miPointerSpriteFuncRec vncPointerSpriteFuncs = {
  455. vncRealizeCursor,
  456. vncUnrealizeCursor,
  457. vncSetCursor,
  458. vncMoveCursor,
  459. vncDeviceCursorInitialize,
  460. vncDeviceCursorCleanup
  461. };
  462. static miPointerScreenFuncRec vncPointerCursorFuncs = {
  463. vncCursorOffScreen,
  464. vncCrossScreen,
  465. miPointerWarpCursor
  466. };
  467. static Bool
  468. vncRandRGetInfo(ScreenPtr pScreen, Rotation * rotations)
  469. {
  470. // We update all information right away, so there is nothing to
  471. // do here.
  472. return TRUE;
  473. }
  474. static Bool vncRandRCrtcSet(ScreenPtr pScreen, RRCrtcPtr crtc, RRModePtr mode,
  475. int x, int y, Rotation rotation, int num_outputs,
  476. RROutputPtr * outputs);
  477. static RRModePtr vncRandRModeGet(int width, int height);
  478. static Bool
  479. vncRandRScreenSetSize(ScreenPtr pScreen,
  480. CARD16 width, CARD16 height,
  481. CARD32 mmWidth, CARD32 mmHeight)
  482. {
  483. VncFramebufferInfo fb;
  484. rrScrPrivPtr rp = rrGetScrPriv(pScreen);
  485. PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
  486. void *pbits;
  487. Bool ret;
  488. int oldwidth, oldheight, oldmmWidth, oldmmHeight;
  489. /* Prevent updates while we fiddle */
  490. SetRootClip(pScreen, ROOT_CLIP_NONE);
  491. /* Store current state in case we fail */
  492. oldwidth = pScreen->width;
  493. oldheight = pScreen->height;
  494. oldmmWidth = pScreen->mmWidth;
  495. oldmmHeight = pScreen->mmHeight;
  496. /* Then set the new dimensions */
  497. pScreen->width = width;
  498. pScreen->height = height;
  499. pScreen->mmWidth = mmWidth;
  500. pScreen->mmHeight = mmHeight;
  501. /* Allocate a new framebuffer */
  502. memset(&fb, 0, sizeof(VncFramebufferInfo));
  503. fb.width = pScreen->width;
  504. fb.height = pScreen->height;
  505. fb.depth = vncScreenInfo.fb.depth;
  506. pbits = vncAllocateFramebufferMemory(&fb);
  507. if (!pbits) {
  508. /* Allocation failed. Restore old state */
  509. pScreen->width = oldwidth;
  510. pScreen->height = oldheight;
  511. pScreen->mmWidth = oldmmWidth;
  512. pScreen->mmHeight = oldmmHeight;
  513. SetRootClip(pScreen, ROOT_CLIP_FULL);
  514. return FALSE;
  515. }
  516. /* Update root pixmap with the new dimensions and buffer */
  517. ret = pScreen->ModifyPixmapHeader(rootPixmap, fb.width, fb.height,
  518. -1, -1, fb.paddedBytesWidth, pbits);
  519. if (!ret) {
  520. /* Update failed. Free the new framebuffer and restore old state */
  521. vncFreeFramebufferMemory(&fb);
  522. pScreen->width = oldwidth;
  523. pScreen->height = oldheight;
  524. pScreen->mmWidth = oldmmWidth;
  525. pScreen->mmHeight = oldmmHeight;
  526. SetRootClip(pScreen, ROOT_CLIP_FULL);
  527. return FALSE;
  528. }
  529. /* Free the old framebuffer and keep the info about the new one */
  530. vncFreeFramebufferMemory(&vncScreenInfo.fb);
  531. vncScreenInfo.fb = fb;
  532. /* Let VNC get the new framebuffer (actual update is in vncHooks.cc) */
  533. vncFbptr[pScreen->myNum] = pbits;
  534. vncFbstride[pScreen->myNum] = fb.paddedWidth;
  535. /* Restore ability to update screen, now with new dimensions */
  536. SetRootClip(pScreen, ROOT_CLIP_FULL);
  537. /*
  538. * Let RandR know we changed something (it doesn't assume that
  539. * TRUE means something changed for some reason...).
  540. */
  541. RRScreenSizeNotify(pScreen);
  542. /* Crop all CRTCs to the new screen */
  543. for (int i = 0; i < rp->numCrtcs; i++) {
  544. RRCrtcPtr crtc;
  545. RRModePtr mode;
  546. crtc = rp->crtcs[i];
  547. /* Disabled? */
  548. if (crtc->mode == NULL)
  549. continue;
  550. /* Fully inside? */
  551. if ((crtc->x + crtc->mode->mode.width <= width) &&
  552. (crtc->y + crtc->mode->mode.height <= height))
  553. continue;
  554. /* Fully outside? */
  555. if ((crtc->x >= width) || (crtc->y >= height)) {
  556. /* Disable it */
  557. ret = vncRandRCrtcSet(pScreen, crtc, NULL,
  558. crtc->x, crtc->y, crtc->rotation, 0, NULL);
  559. if (!ret)
  560. ErrorF("Warning: Unable to disable CRTC that is outside of new screen dimensions");
  561. continue;
  562. }
  563. /* Just needs to be resized to a temporary mode */
  564. mode = vncRandRModeGet(width - crtc->x, height - crtc->y);
  565. if (mode == NULL) {
  566. ErrorF("Warning: Unable to create custom mode for %dx%d",
  567. width - crtc->x, height - crtc->y);
  568. continue;
  569. }
  570. ret = vncRandRCrtcSet(pScreen, crtc, mode,
  571. crtc->x, crtc->y, crtc->rotation,
  572. crtc->numOutputs, crtc->outputs);
  573. RRModeDestroy(mode);
  574. if (!ret)
  575. ErrorF("Warning: Unable to crop CRTC to new screen dimensions");
  576. }
  577. return TRUE;
  578. }
  579. static Bool
  580. vncRandRCrtcSet(ScreenPtr pScreen, RRCrtcPtr crtc, RRModePtr mode,
  581. int x, int y, Rotation rotation, int num_outputs,
  582. RROutputPtr * outputs)
  583. {
  584. Bool ret;
  585. int i;
  586. /*
  587. * Some applications get confused by a connected output without a
  588. * mode or CRTC, so we need to fiddle with the connection state as well.
  589. */
  590. for (i = 0; i < crtc->numOutputs; i++)
  591. RROutputSetConnection(crtc->outputs[i], RR_Disconnected);
  592. for (i = 0; i < num_outputs; i++) {
  593. if (mode != NULL)
  594. RROutputSetConnection(outputs[i], RR_Connected);
  595. else
  596. RROutputSetConnection(outputs[i], RR_Disconnected);
  597. }
  598. /* Let RandR know we approve, and let it update its internal state */
  599. ret = RRCrtcNotify(crtc, mode, x, y, rotation, NULL, num_outputs, outputs);
  600. if (!ret)
  601. return FALSE;
  602. return TRUE;
  603. }
  604. static Bool
  605. vncRandROutputValidateMode(ScreenPtr pScreen,
  606. RROutputPtr output, RRModePtr mode)
  607. {
  608. /* We have no hardware so any mode works */
  609. return TRUE;
  610. }
  611. static void
  612. vncRandRModeDestroy(ScreenPtr pScreen, RRModePtr mode)
  613. {
  614. /* We haven't allocated anything so nothing to destroy */
  615. }
  616. static const int vncRandRWidths[] =
  617. { 1920, 1920, 1600, 1680, 1400, 1360, 1280, 1280, 1280, 1280, 1024, 800, 640 };
  618. static const int vncRandRHeights[] =
  619. { 1200, 1080, 1200, 1050, 1050, 768, 1024, 960, 800, 720, 768, 600, 480 };
  620. static int vncRandRIndex = 0;
  621. static RRModePtr
  622. vncRandRModeGet(int width, int height)
  623. {
  624. xRRModeInfo modeInfo;
  625. char name[100];
  626. RRModePtr mode;
  627. memset(&modeInfo, 0, sizeof(modeInfo));
  628. sprintf(name, "%dx%d", width, height);
  629. modeInfo.width = width;
  630. modeInfo.height = height;
  631. modeInfo.hTotal = width;
  632. modeInfo.vTotal = height;
  633. modeInfo.dotClock = ((CARD32) width * (CARD32) height * 60);
  634. modeInfo.nameLength = strlen(name);
  635. mode = RRModeGet(&modeInfo, name);
  636. if (mode == NULL)
  637. return NULL;
  638. return mode;
  639. }
  640. static void
  641. vncRandRSetModes(RROutputPtr output, int pref_width, int pref_height)
  642. {
  643. RRModePtr mode;
  644. RRModePtr *modes;
  645. int i, num_modes, num_pref;
  646. num_modes = sizeof(vncRandRWidths) / sizeof(*vncRandRWidths) + 1;
  647. modes = malloc(sizeof(RRModePtr) * num_modes);
  648. if (modes == NULL)
  649. return;
  650. num_modes = 0;
  651. num_pref = 0;
  652. if ((pref_width > 0) && (pref_height > 0)) {
  653. mode = vncRandRModeGet(pref_width, pref_height);
  654. if (mode != NULL) {
  655. modes[num_modes] = mode;
  656. num_modes++;
  657. num_pref++;
  658. }
  659. }
  660. for (i = 0; i < sizeof(vncRandRWidths) / sizeof(*vncRandRWidths); i++) {
  661. if ((vncRandRWidths[i] == pref_width) &&
  662. (vncRandRHeights[i] == pref_height))
  663. continue;
  664. mode = vncRandRModeGet(vncRandRWidths[i], vncRandRHeights[i]);
  665. if (mode != NULL) {
  666. modes[num_modes] = mode;
  667. num_modes++;
  668. }
  669. }
  670. RROutputSetModes(output, modes, num_modes, num_pref);
  671. free(modes);
  672. }
  673. static RRCrtcPtr
  674. vncRandRCrtcCreate(ScreenPtr pScreen)
  675. {
  676. RRCrtcPtr crtc;
  677. RROutputPtr output;
  678. char name[100];
  679. /* First we create the CRTC... */
  680. crtc = RRCrtcCreate(pScreen, NULL);
  681. /* We don't actually support gamma, but xrandr complains when it is missing */
  682. RRCrtcGammaSetSize(crtc, 256);
  683. /* Then we create a dummy output for it... */
  684. sprintf(name, "VNC-%d", vncRandRIndex);
  685. vncRandRIndex++;
  686. output = RROutputCreate(pScreen, name, strlen(name), NULL);
  687. RROutputSetCrtcs(output, &crtc, 1);
  688. RROutputSetConnection(output, RR_Disconnected);
  689. /* Make sure the CRTC has this output set */
  690. vncRandRCrtcSet(pScreen, crtc, NULL, 0, 0, RR_Rotate_0, 1, &output);
  691. /* Populate a list of default modes */
  692. vncRandRSetModes(output, -1, -1);
  693. return crtc;
  694. }
  695. /* Used from XserverDesktop when it needs more outputs... */
  696. int
  697. vncRandRCanCreateScreenOutputs(int scrIdx, int extraOutputs)
  698. {
  699. return 1;
  700. }
  701. int
  702. vncRandRCreateScreenOutputs(int scrIdx, int extraOutputs)
  703. {
  704. RRCrtcPtr crtc;
  705. while (extraOutputs > 0) {
  706. crtc = vncRandRCrtcCreate(screenInfo.screens[scrIdx]);
  707. if (crtc == NULL)
  708. return 0;
  709. extraOutputs--;
  710. }
  711. return 1;
  712. }
  713. /* Creating and modifying modes, used by XserverDesktop and init here */
  714. int
  715. vncRandRCanCreateModes()
  716. {
  717. return 1;
  718. }
  719. void *
  720. vncRandRCreateMode(void *out, int width, int height)
  721. {
  722. RROutputPtr output;
  723. output = out;
  724. /* Do we already have the mode? */
  725. for (int i = 0; i < output->numModes; i++) {
  726. if ((output->modes[i]->mode.width == width) &&
  727. (output->modes[i]->mode.height == height))
  728. return output->modes[i];
  729. }
  730. /* Just recreate the entire list */
  731. vncRandRSetModes(output, width, height);
  732. /* Find the new mode */
  733. for (int i = 0; i < output->numModes; i++) {
  734. if ((output->modes[i]->mode.width == width) &&
  735. (output->modes[i]->mode.height == height))
  736. return output->modes[i];
  737. }
  738. /* Something went horribly wrong */
  739. return NULL;
  740. }
  741. void *
  742. vncRandRSetPreferredMode(void *out, void *m)
  743. {
  744. RRModePtr mode;
  745. RROutputPtr output;
  746. int width, height;
  747. mode = m;
  748. output = out;
  749. width = mode->mode.width;
  750. height = mode->mode.height;
  751. /* Already the preferred mode? */
  752. if ((output->numModes >= 1) && (output->numPreferred == 1) &&
  753. (output->modes[0] == mode))
  754. return mode;
  755. /* Recreate the list, with the mode we want as preferred */
  756. vncRandRSetModes(output, width, height);
  757. /* Sanity check */
  758. if ((output->numModes >= 1) && (output->numPreferred == 1) &&
  759. (output->modes[0]->mode.width == width) &&
  760. (output->modes[0]->mode.height == height))
  761. return output->modes[0];
  762. /* Something went horribly wrong */
  763. return NULL;
  764. }
  765. static Bool
  766. vncRandRInit(ScreenPtr pScreen)
  767. {
  768. rrScrPrivPtr pScrPriv;
  769. RRCrtcPtr crtc;
  770. RRModePtr mode;
  771. Bool ret;
  772. if (!RRScreenInit(pScreen))
  773. return FALSE;
  774. pScrPriv = rrGetScrPriv(pScreen);
  775. pScrPriv->rrGetInfo = vncRandRGetInfo;
  776. pScrPriv->rrSetConfig = NULL;
  777. pScrPriv->rrScreenSetSize = vncRandRScreenSetSize;
  778. pScrPriv->rrCrtcSet = vncRandRCrtcSet;
  779. pScrPriv->rrOutputValidateMode = vncRandROutputValidateMode;
  780. pScrPriv->rrModeDestroy = vncRandRModeDestroy;
  781. /* These are completely arbitrary */
  782. RRScreenSetSizeRange(pScreen, 32, 32, 32768, 32768);
  783. /*
  784. * Start with a single CRTC with a single output. More will be
  785. * allocated as needed...
  786. */
  787. crtc = vncRandRCrtcCreate(pScreen);
  788. /* Make sure the current screen size is the active mode */
  789. mode = vncRandRCreateMode(crtc->outputs[0],
  790. pScreen->width, pScreen->height);
  791. if (mode == NULL)
  792. return FALSE;
  793. mode = vncRandRSetPreferredMode(crtc->outputs[0], mode);
  794. if (mode == NULL)
  795. return FALSE;
  796. ret = vncRandRCrtcSet(pScreen, crtc, mode, 0, 0, RR_Rotate_0,
  797. crtc->numOutputs, crtc->outputs);
  798. if (!ret)
  799. return FALSE;
  800. return TRUE;
  801. }
  802. static Bool
  803. vncScreenInit(ScreenPtr pScreen, int argc, char **argv)
  804. {
  805. int dpi;
  806. int ret;
  807. void *pbits;
  808. /* 96 is the default used by most other systems */
  809. dpi = 96;
  810. if (monitorResolution)
  811. dpi = monitorResolution;
  812. pbits = vncAllocateFramebufferMemory(&vncScreenInfo.fb);
  813. if (!pbits)
  814. return FALSE;
  815. vncFbptr[0] = pbits;
  816. vncFbstride[0] = vncScreenInfo.fb.paddedWidth;
  817. miSetPixmapDepths();
  818. switch (vncScreenInfo.fb.depth) {
  819. case 16:
  820. miSetVisualTypesAndMasks(16,
  821. ((1 << TrueColor) |
  822. (1 << DirectColor)),
  823. 8, TrueColor, 0xf800, 0x07e0, 0x001f);
  824. break;
  825. case 24:
  826. miSetVisualTypesAndMasks(24,
  827. ((1 << TrueColor) |
  828. (1 << DirectColor)),
  829. 8, TrueColor, 0xff0000, 0x00ff00, 0x0000ff);
  830. break;
  831. case 32:
  832. miSetVisualTypesAndMasks(32,
  833. ((1 << TrueColor) |
  834. (1 << DirectColor)),
  835. 8, TrueColor, 0xff000000, 0x00ff0000,
  836. 0x0000ff00);
  837. break;
  838. default:
  839. return FALSE;
  840. }
  841. ret = fbScreenInit(pScreen, pbits,
  842. vncScreenInfo.fb.width, vncScreenInfo.fb.height,
  843. dpi, dpi, vncScreenInfo.fb.paddedWidth,
  844. vncScreenInfo.fb.bitsPerPixel);
  845. if (ret && Render)
  846. ret = fbPictureInit(pScreen, 0, 0);
  847. if (!ret)
  848. return FALSE;
  849. ret = vncRandRInit(pScreen);
  850. if (!ret)
  851. return FALSE;
  852. pScreen->SaveScreen = vncSaveScreen;
  853. miPointerInitialize(pScreen, &vncPointerSpriteFuncs, &vncPointerCursorFuncs,
  854. FALSE);
  855. if (!vncScreenInfo.pixelFormatDefined) {
  856. switch (vncScreenInfo.fb.depth) {
  857. case 16:
  858. vncScreenInfo.pixelFormatDefined = TRUE;
  859. vncScreenInfo.rgbNotBgr = TRUE;
  860. vncScreenInfo.blueBits = vncScreenInfo.redBits = 5;
  861. vncScreenInfo.greenBits = 6;
  862. break;
  863. case 24:
  864. case 32:
  865. vncScreenInfo.pixelFormatDefined = TRUE;
  866. vncScreenInfo.rgbNotBgr = TRUE;
  867. vncScreenInfo.blueBits = vncScreenInfo.redBits = vncScreenInfo.greenBits = 8;
  868. break;
  869. }
  870. }
  871. if (vncScreenInfo.pixelFormatDefined) {
  872. VisualPtr vis = pScreen->visuals;
  873. for (int i = 0; i < pScreen->numVisuals; i++) {
  874. if (vncScreenInfo.rgbNotBgr) {
  875. vis->offsetBlue = 0;
  876. vis->blueMask = (1 << vncScreenInfo.blueBits) - 1;
  877. vis->offsetGreen = vncScreenInfo.blueBits;
  878. vis->greenMask =
  879. ((1 << vncScreenInfo.greenBits) - 1) << vis->offsetGreen;
  880. vis->offsetRed = vis->offsetGreen + vncScreenInfo.greenBits;
  881. vis->redMask = ((1 << vncScreenInfo.redBits) - 1) << vis->offsetRed;
  882. }
  883. else {
  884. vis->offsetRed = 0;
  885. vis->redMask = (1 << vncScreenInfo.redBits) - 1;
  886. vis->offsetGreen = vncScreenInfo.redBits;
  887. vis->greenMask =
  888. ((1 << vncScreenInfo.greenBits) - 1) << vis->offsetGreen;
  889. vis->offsetBlue = vis->offsetGreen + vncScreenInfo.greenBits;
  890. vis->blueMask = ((1 << vncScreenInfo.blueBits) - 1) << vis->offsetBlue;
  891. }
  892. vis++;
  893. }
  894. }
  895. ret = fbCreateDefColormap(pScreen);
  896. if (!ret)
  897. return FALSE;
  898. return TRUE;
  899. } /* end vncScreenInit */
  900. static void
  901. vncClientStateChange(CallbackListPtr *a, void *b, void *c)
  902. {
  903. if (dispatchException & DE_RESET) {
  904. ErrorF("Warning: VNC extension does not support -reset, terminating instead. Use -noreset to prevent termination.\n");
  905. dispatchException |= DE_TERMINATE;
  906. dispatchException &= ~DE_RESET;
  907. }
  908. }
  909. #ifdef GLXEXT
  910. #if XORG_OLDER_THAN(1, 20, 0)
  911. extern void GlxExtensionInit(void);
  912. static ExtensionModule glxExt = {
  913. GlxExtensionInit,
  914. "GLX",
  915. &noGlxExtension
  916. };
  917. #endif
  918. #endif
  919. void
  920. InitOutput(ScreenInfo * scrInfo, int argc, char **argv)
  921. {
  922. int i;
  923. int NumFormats = 0;
  924. vncPrintBanner();
  925. #if XORG_AT_LEAST(1, 20, 0)
  926. xorgGlxCreateVendor();
  927. #else
  928. #ifdef GLXEXT
  929. if (serverGeneration == 1)
  930. LoadExtensionList(&glxExt, 1, TRUE);
  931. #endif
  932. #endif
  933. /* initialize pixmap formats */
  934. /* must have a pixmap depth to match every screen depth */
  935. vncPixmapDepths[vncScreenInfo.fb.depth] = TRUE;
  936. /* RENDER needs a good set of pixmaps. */
  937. if (Render) {
  938. vncPixmapDepths[1] = TRUE;
  939. vncPixmapDepths[4] = TRUE;
  940. vncPixmapDepths[8] = TRUE;
  941. /* vncPixmapDepths[15] = TRUE; */
  942. vncPixmapDepths[16] = TRUE;
  943. vncPixmapDepths[24] = TRUE;
  944. vncPixmapDepths[32] = TRUE;
  945. }
  946. for (i = 1; i <= 32; i++) {
  947. if (vncPixmapDepths[i]) {
  948. if (NumFormats >= MAXFORMATS)
  949. FatalError("MAXFORMATS is too small for this server\n");
  950. scrInfo->formats[NumFormats].depth = i;
  951. scrInfo->formats[NumFormats].bitsPerPixel = vncBitsPerPixel(i);
  952. scrInfo->formats[NumFormats].scanlinePad = BITMAP_SCANLINE_PAD;
  953. NumFormats++;
  954. }
  955. }
  956. scrInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  957. scrInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  958. scrInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  959. scrInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  960. scrInfo->numPixmapFormats = NumFormats;
  961. /* initialize screen */
  962. if (AddScreen(vncScreenInit, argc, argv) == -1) {
  963. FatalError("Couldn't add screen\n");
  964. }
  965. if (!AddCallback(&ClientStateCallback, vncClientStateChange, 0)) {
  966. FatalError("AddCallback failed\n");
  967. }
  968. } /* end InitOutput */
  969. void
  970. DDXRingBell(int percent, int pitch, int duration)
  971. {
  972. if (percent > 0)
  973. vncBell();
  974. }
  975. Bool
  976. LegalModifier(unsigned int key, DeviceIntPtr pDev)
  977. {
  978. return TRUE;
  979. }
  980. void
  981. ProcessInputEvents(void)
  982. {
  983. mieqProcessInputEvents();
  984. }
  985. void
  986. InitInput(int argc, char *argv[])
  987. {
  988. mieqInit();
  989. }
  990. void
  991. CloseInput(void)
  992. {
  993. }
  994. void
  995. vncClientGone(int fd)
  996. {
  997. if (fd == vncInetdSock) {
  998. ErrorF("inetdSock client gone\n");
  999. GiveUp(0);
  1000. }
  1001. }