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 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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. #ifdef WIN32
  34. #include <X11/Xwinsock.h>
  35. #endif
  36. #include <stdio.h>
  37. #include <X11/X.h>
  38. #define NEED_EVENTS
  39. #include <X11/Xproto.h>
  40. #include <X11/Xos.h>
  41. #include "scrnintstr.h"
  42. #include "glx_extinit.h"
  43. #include "servermd.h"
  44. #include "fb.h"
  45. #include "mi.h"
  46. #include "colormapst.h"
  47. #include "gcstruct.h"
  48. #include "input.h"
  49. #include "mipointer.h"
  50. #include "micmap.h"
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <errno.h>
  54. #ifndef WIN32
  55. #include <sys/param.h>
  56. #endif
  57. #include <X11/XWDFile.h>
  58. #ifdef HAS_SHM
  59. #include <sys/ipc.h>
  60. #include <sys/shm.h>
  61. #endif /* HAS_SHM */
  62. #include "dix.h"
  63. #include "os.h"
  64. #include "miline.h"
  65. #include "inputstr.h"
  66. #include "randrstr.h"
  67. #ifdef DPMSExtension
  68. #include "dpmsproc.h"
  69. #endif
  70. #include <X11/keysym.h>
  71. extern char buildtime[];
  72. #undef VENDOR_RELEASE
  73. #undef VENDOR_STRING
  74. #include "version-config.h"
  75. #include "site.h"
  76. #define XVNCVERSION "TigerVNC 1.11.80"
  77. #define XVNCCOPYRIGHT ("Copyright (C) 1999-2020 TigerVNC Team and many others (see README.rst)\n" \
  78. "See https://www.tigervnc.org for information on TigerVNC.\n")
  79. #define VFB_DEFAULT_WIDTH 1024
  80. #define VFB_DEFAULT_HEIGHT 768
  81. #define VFB_DEFAULT_DEPTH 24
  82. #define VFB_DEFAULT_WHITEPIXEL 0xffffffff
  83. #define VFB_DEFAULT_BLACKPIXEL 0
  84. #define VFB_DEFAULT_LINEBIAS 0
  85. #define XWD_WINDOW_NAME_LEN 60
  86. typedef struct
  87. {
  88. int width;
  89. int height;
  90. int depth;
  91. /* Computed when allocated */
  92. int paddedBytesWidth;
  93. int paddedWidth;
  94. int bitsPerPixel;
  95. /* Private */
  96. int sizeInBytes;
  97. void *pfbMemory;
  98. #ifdef HAS_SHM
  99. int shmid;
  100. #endif
  101. } vfbFramebufferInfo, *vfbFramebufferInfoPtr;
  102. typedef struct
  103. {
  104. int scrnum;
  105. Pixel blackPixel;
  106. Pixel whitePixel;
  107. unsigned int lineBias;
  108. CloseScreenProcPtr closeScreen;
  109. vfbFramebufferInfo fb;
  110. Bool pixelFormatDefined;
  111. Bool rgbNotBgr;
  112. int redBits, greenBits, blueBits;
  113. } vfbScreenInfo, *vfbScreenInfoPtr;
  114. static int vfbNumScreens;
  115. static vfbScreenInfo vfbScreens[MAXSCREENS];
  116. static Bool vfbPixmapDepths[33];
  117. typedef enum { NORMAL_MEMORY_FB, SHARED_MEMORY_FB } fbMemType;
  118. static fbMemType fbmemtype = NORMAL_MEMORY_FB;
  119. static int lastScreen = -1;
  120. static Bool Render = TRUE;
  121. static Bool displaySpecified = FALSE;
  122. static char displayNumStr[16];
  123. static int vncVerbose = DEFAULT_LOG_VERBOSITY;
  124. static void
  125. vncPrintBanner(void)
  126. {
  127. ErrorF("\nXvnc %s - built %s\n%s", XVNCVERSION, buildtime, XVNCCOPYRIGHT);
  128. ErrorF("Underlying X server release %d, %s\n\n", VENDOR_RELEASE,
  129. VENDOR_STRING);
  130. }
  131. static void
  132. vfbInitializePixmapDepths(void)
  133. {
  134. int i;
  135. vfbPixmapDepths[1] = TRUE; /* always need bitmaps */
  136. for (i = 2; i <= 32; i++)
  137. vfbPixmapDepths[i] = FALSE;
  138. }
  139. static void
  140. vfbInitializeDefaultScreens(void)
  141. {
  142. int i;
  143. for (i = 0; i < MAXSCREENS; i++)
  144. {
  145. vfbScreens[i].scrnum = i;
  146. vfbScreens[i].blackPixel = VFB_DEFAULT_BLACKPIXEL;
  147. vfbScreens[i].whitePixel = VFB_DEFAULT_WHITEPIXEL;
  148. vfbScreens[i].lineBias = VFB_DEFAULT_LINEBIAS;
  149. vfbScreens[i].fb.width = VFB_DEFAULT_WIDTH;
  150. vfbScreens[i].fb.height = VFB_DEFAULT_HEIGHT;
  151. vfbScreens[i].fb.pfbMemory = NULL;
  152. vfbScreens[i].fb.depth = VFB_DEFAULT_DEPTH;
  153. vfbScreens[i].pixelFormatDefined = FALSE;
  154. }
  155. vfbNumScreens = 1;
  156. }
  157. static int
  158. vfbBitsPerPixel(int depth)
  159. {
  160. if (depth == 1) return 1;
  161. else if (depth <= 8) return 8;
  162. else if (depth <= 16) return 16;
  163. else return 32;
  164. }
  165. static void vfbFreeFramebufferMemory(vfbFramebufferInfoPtr pfb);
  166. #ifdef DPMSExtension
  167. #if XORG < 120
  168. /* Why support DPMS? Because stupid modern desktop environments
  169. such as Unity 2D on Ubuntu 11.10 crashes if DPMS is not
  170. available. (DPMSSet is called by dpms.c, but the return value
  171. is ignored.) */
  172. int DPMSSet(ClientPtr client, int level)
  173. {
  174. return Success;
  175. }
  176. Bool DPMSSupported(void)
  177. {
  178. /* Causes DPMSCapable to return false, meaning no devices are DPMS
  179. capable */
  180. return FALSE;
  181. }
  182. #endif
  183. #endif
  184. void ddxGiveUp(enum ExitCode error)
  185. {
  186. int i;
  187. /* clean up the framebuffers */
  188. for (i = 0; i < vfbNumScreens; i++)
  189. vfbFreeFramebufferMemory(&vfbScreens[i].fb);
  190. }
  191. void
  192. AbortDDX(enum ExitCode error)
  193. {
  194. ddxGiveUp(error);
  195. }
  196. #ifdef __DARWIN__
  197. void
  198. DarwinHandleGUI(int argc, char *argv[])
  199. {
  200. }
  201. void GlxExtensionInit();
  202. void GlxWrapInitVisuals(void *procPtr);
  203. void
  204. DarwinGlxExtensionInit()
  205. {
  206. GlxExtensionInit();
  207. }
  208. void
  209. DarwinGlxWrapInitVisuals(
  210. void *procPtr)
  211. {
  212. GlxWrapInitVisuals(procPtr);
  213. }
  214. #endif
  215. void
  216. OsVendorInit(void)
  217. {
  218. }
  219. void
  220. OsVendorFatalError(const char *f, va_list args)
  221. {
  222. }
  223. #ifdef DDXBEFORERESET
  224. void ddxBeforeReset(void)
  225. {
  226. return;
  227. }
  228. #endif
  229. void ddxUseMsg(void)
  230. {
  231. vncPrintBanner();
  232. ErrorF("-screen scrn WxHxD set screen's width, height, depth\n");
  233. ErrorF("-pixdepths list-of-int support given pixmap depths\n");
  234. #ifdef RENDER
  235. ErrorF("+/-render turn on/off RENDER extension support"
  236. "(default on)\n");
  237. #endif
  238. ErrorF("-linebias n adjust thin line pixelization\n");
  239. ErrorF("-blackpixel n pixel value for black\n");
  240. ErrorF("-whitepixel n pixel value for white\n");
  241. #ifdef HAS_SHM
  242. ErrorF("-shmem put framebuffers in shared memory\n");
  243. #endif
  244. ErrorF("-geometry WxH set screen 0's width, height\n");
  245. ErrorF("-depth D set screen 0's depth\n");
  246. ErrorF("-pixelformat fmt set pixel format (rgbNNN or bgrNNN)\n");
  247. ErrorF("-inetd has been launched from inetd\n");
  248. ErrorF("-noclipboard disable clipboard settings modification via vncconfig utility\n");
  249. ErrorF("-verbose [n] verbose startup messages\n");
  250. ErrorF("-quiet minimal startup messages\n");
  251. ErrorF("-version show the server version\n");
  252. ErrorF("\nVNC parameters:\n");
  253. fprintf(stderr,"\n"
  254. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  255. "Parameters which take a value can be specified as "
  256. "-<param> <value>\n"
  257. "Other valid forms are <param>=<value> -<param>=<value> "
  258. "--<param>=<value>\n"
  259. "Parameter names are case-insensitive. The parameters are:\n\n");
  260. vncListParams(79, 14);
  261. }
  262. static
  263. Bool displayNumFree(int num)
  264. {
  265. char file[256];
  266. if (vncIsTCPPortUsed(6000+num))
  267. return FALSE;
  268. sprintf(file, "/tmp/.X%d-lock", num);
  269. if (access(file, F_OK) == 0)
  270. return FALSE;
  271. sprintf(file, "/tmp/.X11-unix/X%d", num);
  272. if (access(file, F_OK) == 0)
  273. return FALSE;
  274. sprintf(file, "/usr/spool/sockets/X11/%d", num);
  275. if (access(file, F_OK) == 0)
  276. return FALSE;
  277. return TRUE;
  278. }
  279. #define fail_unless_args(_argc,_i,_n) \
  280. if (_i + _n >= _argc) \
  281. { \
  282. UseMsg(); \
  283. return 0; \
  284. }
  285. int
  286. ddxProcessArgument(int argc, char *argv[], int i)
  287. {
  288. static Bool firstTime = TRUE;
  289. if (firstTime)
  290. {
  291. /* Force -noreset as default until we properly handle resets */
  292. dispatchExceptionAtReset = 0;
  293. vfbInitializeDefaultScreens();
  294. vfbInitializePixmapDepths();
  295. firstTime = FALSE;
  296. vncInitRFB();
  297. }
  298. if (argv[i][0] == ':')
  299. displaySpecified = TRUE;
  300. if (strcmp (argv[i], "-screen") == 0) /* -screen n WxHxD */
  301. {
  302. int screenNum;
  303. fail_unless_args(argc, i, 2);
  304. screenNum = atoi(argv[i+1]);
  305. if (screenNum < 0 || screenNum >= MAXSCREENS)
  306. {
  307. ErrorF("Invalid screen number %d\n", screenNum);
  308. UseMsg();
  309. return 0;
  310. }
  311. if (3 != sscanf(argv[i+2], "%dx%dx%d",
  312. &vfbScreens[screenNum].fb.width,
  313. &vfbScreens[screenNum].fb.height,
  314. &vfbScreens[screenNum].fb.depth))
  315. {
  316. ErrorF("Invalid screen configuration %s\n", argv[i+2]);
  317. UseMsg();
  318. return 0;
  319. }
  320. if (screenNum >= vfbNumScreens)
  321. vfbNumScreens = screenNum + 1;
  322. lastScreen = screenNum;
  323. return 3;
  324. }
  325. if (strcmp (argv[i], "-pixdepths") == 0) /* -pixdepths list-of-depth */
  326. {
  327. int depth, ret = 1;
  328. fail_unless_args(argc, i, 1);
  329. ++i;
  330. while ((i < argc) && (depth = atoi(argv[i++])) != 0)
  331. {
  332. if (depth < 0 || depth > 32)
  333. {
  334. ErrorF("Invalid pixmap depth %d\n", depth);
  335. UseMsg();
  336. return 0;
  337. }
  338. vfbPixmapDepths[depth] = TRUE;
  339. ret++;
  340. }
  341. return ret;
  342. }
  343. if (strcmp (argv[i], "+render") == 0) /* +render */
  344. {
  345. Render = TRUE;
  346. return 1;
  347. }
  348. if (strcmp (argv[i], "-render") == 0) /* -render */
  349. {
  350. Render = FALSE;
  351. return 1;
  352. }
  353. if (strcmp (argv[i], "-blackpixel") == 0) /* -blackpixel n */
  354. {
  355. Pixel pix;
  356. fail_unless_args(argc, i, 1);
  357. ++i;
  358. pix = atoi(argv[i]);
  359. if (-1 == lastScreen)
  360. {
  361. int j;
  362. for (j = 0; j < MAXSCREENS; j++)
  363. {
  364. vfbScreens[j].blackPixel = pix;
  365. }
  366. }
  367. else
  368. {
  369. vfbScreens[lastScreen].blackPixel = pix;
  370. }
  371. return 2;
  372. }
  373. if (strcmp (argv[i], "-whitepixel") == 0) /* -whitepixel n */
  374. {
  375. Pixel pix;
  376. fail_unless_args(argc, i, 1);
  377. ++i;
  378. pix = atoi(argv[i]);
  379. if (-1 == lastScreen)
  380. {
  381. int j;
  382. for (j = 0; j < MAXSCREENS; j++)
  383. {
  384. vfbScreens[j].whitePixel = pix;
  385. }
  386. }
  387. else
  388. {
  389. vfbScreens[lastScreen].whitePixel = pix;
  390. }
  391. return 2;
  392. }
  393. if (strcmp (argv[i], "-linebias") == 0) /* -linebias n */
  394. {
  395. unsigned int linebias;
  396. fail_unless_args(argc, i, 1);
  397. ++i;
  398. linebias = atoi(argv[i]);
  399. if (-1 == lastScreen)
  400. {
  401. int j;
  402. for (j = 0; j < MAXSCREENS; j++)
  403. {
  404. vfbScreens[j].lineBias = linebias;
  405. }
  406. }
  407. else
  408. {
  409. vfbScreens[lastScreen].lineBias = linebias;
  410. }
  411. return 2;
  412. }
  413. #ifdef HAS_SHM
  414. if (strcmp (argv[i], "-shmem") == 0) /* -shmem */
  415. {
  416. fbmemtype = SHARED_MEMORY_FB;
  417. return 1;
  418. }
  419. #endif
  420. if (strcmp(argv[i], "-geometry") == 0)
  421. {
  422. fail_unless_args(argc, i, 1);
  423. ++i;
  424. if (sscanf(argv[i],"%dx%d",&vfbScreens[0].fb.width,
  425. &vfbScreens[0].fb.height) != 2) {
  426. ErrorF("Invalid geometry %s\n", argv[i]);
  427. UseMsg();
  428. return 0;
  429. }
  430. return 2;
  431. }
  432. if (strcmp(argv[i], "-depth") == 0)
  433. {
  434. fail_unless_args(argc, i, 1);
  435. ++i;
  436. vfbScreens[0].fb.depth = atoi(argv[i]);
  437. return 2;
  438. }
  439. if (strcmp(argv[i], "-pixelformat") == 0)
  440. {
  441. char rgbbgr[4];
  442. int bits1, bits2, bits3;
  443. fail_unless_args(argc, i, 1);
  444. ++i;
  445. if (sscanf(argv[i], "%3s%1d%1d%1d", rgbbgr,&bits1,&bits2,&bits3) < 4) {
  446. ErrorF("Invalid pixel format %s\n", argv[i]);
  447. UseMsg();
  448. return 0;
  449. }
  450. #define SET_PIXEL_FORMAT(vfbScreen) \
  451. (vfbScreen).pixelFormatDefined = TRUE; \
  452. (vfbScreen).fb.depth = bits1 + bits2 + bits3; \
  453. (vfbScreen).greenBits = bits2; \
  454. if (strcasecmp(rgbbgr, "bgr") == 0) { \
  455. (vfbScreen).rgbNotBgr = FALSE; \
  456. (vfbScreen).redBits = bits3; \
  457. (vfbScreen).blueBits = bits1; \
  458. } else if (strcasecmp(rgbbgr, "rgb") == 0) { \
  459. (vfbScreen).rgbNotBgr = TRUE; \
  460. (vfbScreen).redBits = bits1; \
  461. (vfbScreen).blueBits = bits3; \
  462. } else { \
  463. ErrorF("Invalid pixel format %s\n", argv[i]); \
  464. UseMsg(); \
  465. return 0; \
  466. }
  467. if (-1 == lastScreen)
  468. {
  469. int j;
  470. for (j = 0; j < MAXSCREENS; j++)
  471. {
  472. SET_PIXEL_FORMAT(vfbScreens[j]);
  473. }
  474. }
  475. else
  476. {
  477. SET_PIXEL_FORMAT(vfbScreens[lastScreen]);
  478. }
  479. return 2;
  480. }
  481. if (strcmp(argv[i], "-inetd") == 0)
  482. {
  483. int nullfd;
  484. dup2(0,3);
  485. vncInetdSock = 3;
  486. /* Avoid xserver >= 1.19's epoll-fd becoming fd 2 / stderr only to be
  487. replaced by /dev/null by OsInit() because the pollfd is not
  488. writable, breaking ospoll_wait(). */
  489. nullfd = open("/dev/null", O_WRONLY);
  490. dup2(nullfd, 2);
  491. close(nullfd);
  492. if (!displaySpecified) {
  493. int port = vncGetSocketPort(vncInetdSock);
  494. int displayNum = port - 5900;
  495. if (displayNum < 0 || displayNum > 99 || !displayNumFree(displayNum)) {
  496. for (displayNum = 1; displayNum < 100; displayNum++)
  497. if (displayNumFree(displayNum)) break;
  498. if (displayNum == 100)
  499. FatalError("Xvnc error: no free display number for -inetd\n");
  500. }
  501. display = displayNumStr;
  502. sprintf(displayNumStr, "%d", displayNum);
  503. }
  504. return 1;
  505. }
  506. if (strcmp(argv[i], "-noclipboard") == 0) {
  507. vncNoClipboard = 1;
  508. return 1;
  509. }
  510. if (!strcmp(argv[i], "-verbose")) {
  511. if (++i < argc && argv[i]) {
  512. char *end;
  513. long val;
  514. val = strtol(argv[i], &end, 0);
  515. if (*end == '\0') {
  516. vncVerbose = val;
  517. LogSetParameter(XLOG_VERBOSITY, vncVerbose);
  518. return 2;
  519. }
  520. }
  521. vncVerbose++;
  522. LogSetParameter(XLOG_VERBOSITY, vncVerbose);
  523. return 1;
  524. }
  525. if (!strcmp(argv[i], "-quiet")) {
  526. vncVerbose = -1;
  527. LogSetParameter(XLOG_VERBOSITY, vncVerbose);
  528. return 1;
  529. }
  530. if (!strcmp(argv[i], "-showconfig") || !strcmp(argv[i], "-version")) {
  531. vncPrintBanner();
  532. exit(0);
  533. }
  534. if (vncSetParamSimple(argv[i]))
  535. return 1;
  536. if (argv[i][0] == '-' && i+1 < argc) {
  537. if (vncSetParam(&argv[i][1], argv[i+1]))
  538. return 2;
  539. }
  540. return 0;
  541. }
  542. #ifdef DDXTIME /* from ServerOSDefines */
  543. CARD32
  544. GetTimeInMillis()
  545. {
  546. struct timeval tp;
  547. X_GETTIMEOFDAY(&tp);
  548. return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
  549. }
  550. #endif
  551. static DevPrivateKeyRec cmapScrPrivateKeyRec;
  552. #define cmapScrPrivateKey (&cmapScrPrivateKeyRec)
  553. #define GetInstalledColormap(s) ((ColormapPtr) dixLookupPrivate(&(s)->devPrivates, cmapScrPrivateKey))
  554. #define SetInstalledColormap(s,c) (dixSetPrivate(&(s)->devPrivates, cmapScrPrivateKey, c))
  555. static int
  556. vfbListInstalledColormaps(ScreenPtr pScreen, Colormap *pmaps)
  557. {
  558. /* By the time we are processing requests, we can guarantee that there
  559. * is always a colormap installed */
  560. *pmaps = GetInstalledColormap(pScreen)->mid;
  561. return (1);
  562. }
  563. static void
  564. vfbInstallColormap(ColormapPtr pmap)
  565. {
  566. ColormapPtr oldpmap;
  567. oldpmap = GetInstalledColormap(pmap->pScreen);
  568. if (pmap != oldpmap)
  569. {
  570. int entries;
  571. VisualPtr pVisual;
  572. Pixel * ppix;
  573. xrgb * prgb;
  574. xColorItem *defs;
  575. int i;
  576. if(oldpmap != (ColormapPtr)None)
  577. WalkTree(pmap->pScreen, TellLostMap, (char *)&oldpmap->mid);
  578. /* Install pmap */
  579. SetInstalledColormap(pmap->pScreen, pmap);
  580. WalkTree(pmap->pScreen, TellGainedMap, (char *)&pmap->mid);
  581. entries = pmap->pVisual->ColormapEntries;
  582. pVisual = pmap->pVisual;
  583. ppix = (Pixel *)calloc(entries, sizeof(Pixel));
  584. prgb = (xrgb *)calloc(entries, sizeof(xrgb));
  585. defs = (xColorItem *)calloc(entries, sizeof(xColorItem));
  586. if (!ppix || !prgb || !defs)
  587. FatalError ("Not enough memory for color map\n");
  588. for (i = 0; i < entries; i++) ppix[i] = i;
  589. /* XXX truecolor */
  590. QueryColors(pmap, entries, ppix, prgb, serverClient);
  591. for (i = 0; i < entries; i++) { /* convert xrgbs to xColorItems */
  592. defs[i].pixel = ppix[i] & 0xff; /* change pixel to index */
  593. defs[i].red = prgb[i].red;
  594. defs[i].green = prgb[i].green;
  595. defs[i].blue = prgb[i].blue;
  596. defs[i].flags = DoRed|DoGreen|DoBlue;
  597. }
  598. (*pmap->pScreen->StoreColors)(pmap, entries, defs);
  599. free(ppix);
  600. free(prgb);
  601. free(defs);
  602. }
  603. }
  604. static void
  605. vfbUninstallColormap(ColormapPtr pmap)
  606. {
  607. ColormapPtr curpmap = GetInstalledColormap(pmap->pScreen);
  608. if(pmap == curpmap)
  609. {
  610. if (pmap->mid != pmap->pScreen->defColormap)
  611. {
  612. int rc = dixLookupResourceByType((void * *) &curpmap, pmap->pScreen->defColormap,
  613. RT_COLORMAP, serverClient, DixUnknownAccess);
  614. if (rc != Success)
  615. ErrorF("Failed to uninstall color map\n");
  616. else
  617. (*pmap->pScreen->InstallColormap)(curpmap);
  618. }
  619. }
  620. }
  621. static Bool
  622. vfbSaveScreen(ScreenPtr pScreen, int on)
  623. {
  624. return TRUE;
  625. }
  626. #ifdef HAS_SHM
  627. static void
  628. vfbAllocateSharedMemoryFramebuffer(vfbFramebufferInfoPtr pfb)
  629. {
  630. /* create the shared memory segment */
  631. pfb->shmid = shmget(IPC_PRIVATE, pfb->sizeInBytes, IPC_CREAT|0777);
  632. if (pfb->shmid < 0) {
  633. perror("shmget");
  634. ErrorF("shmget %d bytes failed, errno %d", pfb->sizeInBytes, errno);
  635. return;
  636. }
  637. /* try to attach it */
  638. pfb->pfbMemory = shmat(pfb->shmid, 0, 0);
  639. if (-1 == (long)pfb->pfbMemory) {
  640. perror("shmat");
  641. ErrorF("shmat failed, errno %d", errno);
  642. pfb->pfbMemory = NULL;
  643. return;
  644. }
  645. }
  646. #endif /* HAS_SHM */
  647. static void *
  648. vfbAllocateFramebufferMemory(vfbFramebufferInfoPtr pfb)
  649. {
  650. if (pfb->pfbMemory != NULL)
  651. return pfb->pfbMemory; /* already done */
  652. /* Compute memory layout */
  653. pfb->paddedBytesWidth = PixmapBytePad(pfb->width, pfb->depth);
  654. pfb->bitsPerPixel = vfbBitsPerPixel(pfb->depth);
  655. pfb->paddedWidth = pfb->paddedBytesWidth * 8 / pfb->bitsPerPixel;
  656. pfb->sizeInBytes = pfb->paddedBytesWidth * pfb->height;
  657. /* And allocate buffer */
  658. switch (fbmemtype) {
  659. #ifdef HAS_SHM
  660. case SHARED_MEMORY_FB:
  661. vfbAllocateSharedMemoryFramebuffer(pfb);
  662. break;
  663. #else
  664. case SHARED_MEMORY_FB:
  665. break;
  666. #endif
  667. case NORMAL_MEMORY_FB:
  668. pfb->pfbMemory = malloc(pfb->sizeInBytes);
  669. break;
  670. }
  671. /* This will be NULL if any of the above failed */
  672. return pfb->pfbMemory;
  673. }
  674. static void
  675. vfbFreeFramebufferMemory(vfbFramebufferInfoPtr pfb)
  676. {
  677. if ((pfb == NULL) || (pfb->pfbMemory == NULL))
  678. return;
  679. switch (fbmemtype) {
  680. #ifdef HAS_SHM
  681. case SHARED_MEMORY_FB:
  682. if (-1 == shmdt(pfb->pfbMemory)) {
  683. perror("shmdt");
  684. ErrorF("shmdt failed, errno %d", errno);
  685. }
  686. break;
  687. #else /* HAS_SHM */
  688. case SHARED_MEMORY_FB:
  689. break;
  690. #endif /* HAS_SHM */
  691. case NORMAL_MEMORY_FB:
  692. free(pfb->pfbMemory);
  693. break;
  694. }
  695. pfb->pfbMemory = NULL;
  696. }
  697. static Bool
  698. vfbCursorOffScreen (ScreenPtr *ppScreen, int *x, int *y)
  699. {
  700. int absX, absY;
  701. int i;
  702. if (screenInfo.numScreens == 1)
  703. return FALSE;
  704. if ((*x >= 0) && (*x < (*ppScreen)->width) &&
  705. (*y >= 0) && (*y < (*ppScreen)->height))
  706. return FALSE;
  707. absX = *x + (*ppScreen)->x;
  708. absY = *y + (*ppScreen)->y;
  709. for (i = 0;i < screenInfo.numScreens;i++) {
  710. ScreenPtr newScreen;
  711. newScreen = screenInfo.screens[i];
  712. if (absX < newScreen->x)
  713. continue;
  714. if (absY < newScreen->y)
  715. continue;
  716. if (absX >= (newScreen->x + newScreen->width))
  717. continue;
  718. if (absY >= (newScreen->y + newScreen->height))
  719. continue;
  720. *ppScreen = newScreen;
  721. *x = absX - newScreen->x;
  722. *y = absY - newScreen->y;
  723. return TRUE;
  724. }
  725. return FALSE;
  726. }
  727. static void
  728. vfbCrossScreen (ScreenPtr pScreen, Bool entering)
  729. {
  730. }
  731. static Bool vfbRealizeCursor(DeviceIntPtr pDev,
  732. ScreenPtr pScreen, CursorPtr pCursor)
  733. {
  734. return TRUE;
  735. }
  736. static Bool vfbUnrealizeCursor(DeviceIntPtr pDev,
  737. ScreenPtr pScreen, CursorPtr pCursor)
  738. {
  739. return TRUE;
  740. }
  741. static void vfbSetCursor(DeviceIntPtr pDev,
  742. ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
  743. {
  744. }
  745. static void vfbMoveCursor(DeviceIntPtr pDev,
  746. ScreenPtr pScreen, int x, int y)
  747. {
  748. }
  749. static Bool
  750. vfbDeviceCursorInitialize(DeviceIntPtr pDev, ScreenPtr pScreen)
  751. {
  752. return TRUE;
  753. }
  754. static void
  755. vfbDeviceCursorCleanup(DeviceIntPtr pDev, ScreenPtr pScreen)
  756. {
  757. }
  758. static miPointerSpriteFuncRec vfbPointerSpriteFuncs = {
  759. vfbRealizeCursor,
  760. vfbUnrealizeCursor,
  761. vfbSetCursor,
  762. vfbMoveCursor,
  763. vfbDeviceCursorInitialize,
  764. vfbDeviceCursorCleanup
  765. };
  766. static miPointerScreenFuncRec vfbPointerCursorFuncs = {
  767. vfbCursorOffScreen,
  768. vfbCrossScreen,
  769. miPointerWarpCursor
  770. };
  771. static Bool vncRandRGetInfo (ScreenPtr pScreen, Rotation *rotations)
  772. {
  773. // We update all information right away, so there is nothing to
  774. // do here.
  775. return TRUE;
  776. }
  777. /* from hw/xfree86/common/xf86Helper.c */
  778. #include "mivalidate.h"
  779. static void
  780. xf86SetRootClip (ScreenPtr pScreen, Bool enable)
  781. {
  782. WindowPtr pWin = pScreen->root;
  783. WindowPtr pChild;
  784. Bool WasViewable = (Bool)(pWin->viewable);
  785. Bool anyMarked = FALSE;
  786. WindowPtr pLayerWin;
  787. BoxRec box;
  788. if (WasViewable)
  789. {
  790. for (pChild = pWin->firstChild; pChild; pChild = pChild->nextSib)
  791. {
  792. (void) (*pScreen->MarkOverlappedWindows)(pChild,
  793. pChild,
  794. &pLayerWin);
  795. }
  796. (*pScreen->MarkWindow) (pWin);
  797. anyMarked = TRUE;
  798. if (pWin->valdata)
  799. {
  800. if (HasBorder (pWin))
  801. {
  802. RegionPtr borderVisible;
  803. borderVisible = RegionCreate(NullBox, 1);
  804. RegionSubtract(borderVisible,
  805. &pWin->borderClip, &pWin->winSize);
  806. pWin->valdata->before.borderVisible = borderVisible;
  807. }
  808. pWin->valdata->before.resized = TRUE;
  809. }
  810. }
  811. /*
  812. * Use RegionBreak to avoid optimizations in ValidateTree
  813. * that assume the root borderClip can't change well, normally
  814. * it doesn't...)
  815. */
  816. if (enable)
  817. {
  818. box.x1 = 0;
  819. box.y1 = 0;
  820. box.x2 = pScreen->width;
  821. box.y2 = pScreen->height;
  822. RegionInit(&pWin->winSize, &box, 1);
  823. RegionInit(&pWin->borderSize, &box, 1);
  824. if (WasViewable)
  825. RegionReset(&pWin->borderClip, &box);
  826. pWin->drawable.width = pScreen->width;
  827. pWin->drawable.height = pScreen->height;
  828. RegionBreak(&pWin->clipList);
  829. }
  830. else
  831. {
  832. RegionEmpty(&pWin->borderClip);
  833. RegionBreak(&pWin->clipList);
  834. }
  835. ResizeChildrenWinSize (pWin, 0, 0, 0, 0);
  836. if (WasViewable)
  837. {
  838. if (pWin->firstChild)
  839. {
  840. anyMarked |= (*pScreen->MarkOverlappedWindows)(pWin->firstChild,
  841. pWin->firstChild,
  842. (WindowPtr *)NULL);
  843. }
  844. else
  845. {
  846. (*pScreen->MarkWindow) (pWin);
  847. anyMarked = TRUE;
  848. }
  849. if (anyMarked)
  850. (*pScreen->ValidateTree)(pWin, NullWindow, VTOther);
  851. }
  852. if (WasViewable)
  853. {
  854. if (anyMarked)
  855. (*pScreen->HandleExposures)(pWin);
  856. if (anyMarked && pScreen->PostValidateTree)
  857. (*pScreen->PostValidateTree)(pWin, NullWindow, VTOther);
  858. }
  859. if (pWin->realized)
  860. WindowsRestructured ();
  861. FlushAllOutput ();
  862. }
  863. static Bool vncRandRCrtcSet(ScreenPtr pScreen, RRCrtcPtr crtc, RRModePtr mode,
  864. int x, int y, Rotation rotation, int num_outputs,
  865. RROutputPtr *outputs);
  866. static RRModePtr vncRandRModeGet(int width, int height);
  867. static Bool vncRandRScreenSetSize(ScreenPtr pScreen,
  868. CARD16 width, CARD16 height,
  869. CARD32 mmWidth, CARD32 mmHeight)
  870. {
  871. vfbScreenInfoPtr pvfb = &vfbScreens[pScreen->myNum];
  872. vfbFramebufferInfo fb;
  873. rrScrPrivPtr rp = rrGetScrPriv(pScreen);
  874. PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
  875. void *pbits;
  876. Bool ret;
  877. int oldwidth, oldheight, oldmmWidth, oldmmHeight;
  878. /* Prevent updates while we fiddle */
  879. xf86SetRootClip(pScreen, FALSE);
  880. /* Store current state in case we fail */
  881. oldwidth = pScreen->width;
  882. oldheight = pScreen->height;
  883. oldmmWidth = pScreen->mmWidth;
  884. oldmmHeight = pScreen->mmHeight;
  885. /* Then set the new dimensions */
  886. pScreen->width = width;
  887. pScreen->height = height;
  888. pScreen->mmWidth = mmWidth;
  889. pScreen->mmHeight = mmHeight;
  890. /* Allocate a new framebuffer */
  891. memset(&fb, 0, sizeof(vfbFramebufferInfo));
  892. fb.width = pScreen->width;
  893. fb.height = pScreen->height;
  894. fb.depth = pvfb->fb.depth;
  895. pbits = vfbAllocateFramebufferMemory(&fb);
  896. if (!pbits) {
  897. /* Allocation failed. Restore old state */
  898. pScreen->width = oldwidth;
  899. pScreen->height = oldheight;
  900. pScreen->mmWidth = oldmmWidth;
  901. pScreen->mmHeight = oldmmHeight;
  902. xf86SetRootClip(pScreen, TRUE);
  903. return FALSE;
  904. }
  905. /* Update root pixmap with the new dimensions and buffer */
  906. ret = pScreen->ModifyPixmapHeader(rootPixmap, fb.width, fb.height,
  907. -1, -1, fb.paddedBytesWidth, pbits);
  908. if (!ret) {
  909. /* Update failed. Free the new framebuffer and restore old state */
  910. vfbFreeFramebufferMemory(&fb);
  911. pScreen->width = oldwidth;
  912. pScreen->height = oldheight;
  913. pScreen->mmWidth = oldmmWidth;
  914. pScreen->mmHeight = oldmmHeight;
  915. xf86SetRootClip(pScreen, TRUE);
  916. return FALSE;
  917. }
  918. /* Free the old framebuffer and keep the info about the new one */
  919. vfbFreeFramebufferMemory(&pvfb->fb);
  920. memcpy(&pvfb->fb, &fb, sizeof(vfbFramebufferInfo));
  921. /* Let VNC get the new framebuffer (actual update is in vncHooks.cc) */
  922. vncFbptr[pScreen->myNum] = pbits;
  923. vncFbstride[pScreen->myNum] = fb.paddedWidth;
  924. /* Restore ability to update screen, now with new dimensions */
  925. xf86SetRootClip(pScreen, TRUE);
  926. /*
  927. * Let RandR know we changed something (it doesn't assume that
  928. * TRUE means something changed for some reason...).
  929. */
  930. RRScreenSizeNotify(pScreen);
  931. /* Crop all CRTCs to the new screen */
  932. for (int i = 0;i < rp->numCrtcs;i++) {
  933. RRCrtcPtr crtc;
  934. RRModePtr mode;
  935. crtc = rp->crtcs[i];
  936. /* Disabled? */
  937. if (crtc->mode == NULL)
  938. continue;
  939. /* Fully inside? */
  940. if ((crtc->x + crtc->mode->mode.width <= width) &&
  941. (crtc->y + crtc->mode->mode.height <= height))
  942. continue;
  943. /* Fully outside? */
  944. if ((crtc->x >= width) || (crtc->y >= height)) {
  945. /* Disable it */
  946. ret = vncRandRCrtcSet(pScreen, crtc, NULL,
  947. crtc->x, crtc->y, crtc->rotation, 0, NULL);
  948. if (!ret)
  949. ErrorF("Warning: Unable to disable CRTC that is outside of new screen dimensions");
  950. continue;
  951. }
  952. /* Just needs to be resized to a temporary mode */
  953. mode = vncRandRModeGet(width - crtc->x, height - crtc->y);
  954. if (mode == NULL) {
  955. ErrorF("Warning: Unable to create custom mode for %dx%d",
  956. width - crtc->x, height - crtc->y);
  957. continue;
  958. }
  959. ret = vncRandRCrtcSet(pScreen, crtc, mode,
  960. crtc->x, crtc->y, crtc->rotation,
  961. crtc->numOutputs, crtc->outputs);
  962. RRModeDestroy(mode);
  963. if (!ret)
  964. ErrorF("Warning: Unable to crop CRTC to new screen dimensions");
  965. }
  966. return TRUE;
  967. }
  968. static Bool vncRandRCrtcSet(ScreenPtr pScreen, RRCrtcPtr crtc, RRModePtr mode,
  969. int x, int y, Rotation rotation, int num_outputs,
  970. RROutputPtr *outputs)
  971. {
  972. Bool ret;
  973. int i;
  974. /*
  975. * Some applications get confused by a connected output without a
  976. * mode or CRTC, so we need to fiddle with the connection state as well.
  977. */
  978. for (i = 0;i < crtc->numOutputs;i++)
  979. RROutputSetConnection(crtc->outputs[i], RR_Disconnected);
  980. for (i = 0;i < num_outputs;i++) {
  981. if (mode != NULL)
  982. RROutputSetConnection(outputs[i], RR_Connected);
  983. else
  984. RROutputSetConnection(outputs[i], RR_Disconnected);
  985. }
  986. /* Let RandR know we approve, and let it update its internal state */
  987. ret = RRCrtcNotify(crtc, mode, x, y, rotation, NULL, num_outputs, outputs);
  988. if (!ret)
  989. return FALSE;
  990. return TRUE;
  991. }
  992. static Bool vncRandROutputValidateMode(ScreenPtr pScreen,
  993. RROutputPtr output, RRModePtr mode)
  994. {
  995. /* We have no hardware so any mode works */
  996. return TRUE;
  997. }
  998. static void vncRandRModeDestroy(ScreenPtr pScreen, RRModePtr mode)
  999. {
  1000. /* We haven't allocated anything so nothing to destroy */
  1001. }
  1002. static const int vncRandRWidths[] = { 1920, 1920, 1600, 1680, 1400, 1360, 1280, 1280, 1280, 1280, 1024, 800, 640 };
  1003. static const int vncRandRHeights[] = { 1200, 1080, 1200, 1050, 1050, 768, 1024, 960, 800, 720, 768, 600, 480 };
  1004. static int vncRandRIndex = 0;
  1005. static RRModePtr vncRandRModeGet(int width, int height)
  1006. {
  1007. xRRModeInfo modeInfo;
  1008. char name[100];
  1009. RRModePtr mode;
  1010. memset(&modeInfo, 0, sizeof(modeInfo));
  1011. sprintf(name, "%dx%d", width, height);
  1012. modeInfo.width = width;
  1013. modeInfo.height = height;
  1014. modeInfo.hTotal = width;
  1015. modeInfo.vTotal = height;
  1016. modeInfo.dotClock = ((CARD32)width * (CARD32)height * 60);
  1017. modeInfo.nameLength = strlen(name);
  1018. mode = RRModeGet(&modeInfo, name);
  1019. if (mode == NULL)
  1020. return NULL;
  1021. return mode;
  1022. }
  1023. static void vncRandRSetModes(RROutputPtr output, int pref_width, int pref_height)
  1024. {
  1025. RRModePtr mode;
  1026. RRModePtr *modes;
  1027. int i, num_modes, num_pref;
  1028. num_modes = sizeof(vncRandRWidths)/sizeof(*vncRandRWidths) + 1;
  1029. modes = malloc(sizeof(RRModePtr)*num_modes);
  1030. if (modes == NULL)
  1031. return;
  1032. num_modes = 0;
  1033. num_pref = 0;
  1034. if ((pref_width > 0) && (pref_height > 0)) {
  1035. mode = vncRandRModeGet(pref_width, pref_height);
  1036. if (mode != NULL) {
  1037. modes[num_modes] = mode;
  1038. num_modes++;
  1039. num_pref++;
  1040. }
  1041. }
  1042. for (i = 0;i < sizeof(vncRandRWidths)/sizeof(*vncRandRWidths);i++) {
  1043. if ((vncRandRWidths[i] == pref_width) &&
  1044. (vncRandRHeights[i] == pref_height))
  1045. continue;
  1046. mode = vncRandRModeGet(vncRandRWidths[i], vncRandRHeights[i]);
  1047. if (mode != NULL) {
  1048. modes[num_modes] = mode;
  1049. num_modes++;
  1050. }
  1051. }
  1052. RROutputSetModes(output, modes, num_modes, num_pref);
  1053. free(modes);
  1054. }
  1055. static RRCrtcPtr vncRandRCrtcCreate(ScreenPtr pScreen)
  1056. {
  1057. RRCrtcPtr crtc;
  1058. RROutputPtr output;
  1059. char name[100];
  1060. /* First we create the CRTC... */
  1061. crtc = RRCrtcCreate(pScreen, NULL);
  1062. /* We don't actually support gamma, but xrandr complains when it is missing */
  1063. RRCrtcGammaSetSize (crtc, 256);
  1064. /* Then we create a dummy output for it... */
  1065. sprintf(name, "VNC-%d", vncRandRIndex);
  1066. vncRandRIndex++;
  1067. output = RROutputCreate(pScreen, name, strlen(name), NULL);
  1068. RROutputSetCrtcs(output, &crtc, 1);
  1069. RROutputSetConnection(output, RR_Disconnected);
  1070. /* Make sure the CRTC has this output set */
  1071. vncRandRCrtcSet(pScreen, crtc, NULL, 0, 0, RR_Rotate_0, 1, &output);
  1072. /* Populate a list of default modes */
  1073. vncRandRSetModes(output, -1, -1);
  1074. return crtc;
  1075. }
  1076. /* Used from XserverDesktop when it needs more outputs... */
  1077. int vncRandRCanCreateScreenOutputs(int scrIdx, int extraOutputs)
  1078. {
  1079. return 1;
  1080. }
  1081. int vncRandRCreateScreenOutputs(int scrIdx, int extraOutputs)
  1082. {
  1083. RRCrtcPtr crtc;
  1084. while (extraOutputs > 0) {
  1085. crtc = vncRandRCrtcCreate(screenInfo.screens[scrIdx]);
  1086. if (crtc == NULL)
  1087. return 0;
  1088. extraOutputs--;
  1089. }
  1090. return 1;
  1091. }
  1092. /* Creating and modifying modes, used by XserverDesktop and init here */
  1093. int vncRandRCanCreateModes()
  1094. {
  1095. return 1;
  1096. }
  1097. void* vncRandRCreateMode(void* out, int width, int height)
  1098. {
  1099. RROutputPtr output;
  1100. output = out;
  1101. /* Do we already have the mode? */
  1102. for (int i = 0; i < output->numModes; i++) {
  1103. if ((output->modes[i]->mode.width == width) &&
  1104. (output->modes[i]->mode.height == height))
  1105. return output->modes[i];
  1106. }
  1107. /* Just recreate the entire list */
  1108. vncRandRSetModes(output, width, height);
  1109. /* Find the new mode */
  1110. for (int i = 0; i < output->numModes; i++) {
  1111. if ((output->modes[i]->mode.width == width) &&
  1112. (output->modes[i]->mode.height == height))
  1113. return output->modes[i];
  1114. }
  1115. /* Something went horribly wrong */
  1116. return NULL;
  1117. }
  1118. void* vncRandRSetPreferredMode(void* out, void* m)
  1119. {
  1120. RRModePtr mode;
  1121. RROutputPtr output;
  1122. int width, height;
  1123. mode = m;
  1124. output = out;
  1125. width = mode->mode.width;
  1126. height = mode->mode.height;
  1127. /* Already the preferred mode? */
  1128. if ((output->numModes >= 1) && (output->numPreferred == 1) &&
  1129. (output->modes[0] == mode))
  1130. return mode;
  1131. /* Recreate the list, with the mode we want as preferred */
  1132. vncRandRSetModes(output, width, height);
  1133. /* Sanity check */
  1134. if ((output->numModes >= 1) && (output->numPreferred == 1) &&
  1135. (output->modes[0]->mode.width == width) &&
  1136. (output->modes[0]->mode.height == height))
  1137. return output->modes[0];
  1138. /* Something went horribly wrong */
  1139. return NULL;
  1140. }
  1141. static Bool vncRandRInit(ScreenPtr pScreen)
  1142. {
  1143. RRCrtcPtr crtc;
  1144. RRModePtr mode;
  1145. Bool ret;
  1146. if (!RRInit())
  1147. return FALSE;
  1148. /* These are completely arbitrary */
  1149. RRScreenSetSizeRange(pScreen, 32, 32, 32768, 32768);
  1150. /*
  1151. * Start with a single CRTC with a single output. More will be
  1152. * allocated as needed...
  1153. */
  1154. crtc = vncRandRCrtcCreate(pScreen);
  1155. /* Make sure the current screen size is the active mode */
  1156. mode = vncRandRCreateMode(crtc->outputs[0],
  1157. pScreen->width, pScreen->height);
  1158. if (mode == NULL)
  1159. return FALSE;
  1160. mode = vncRandRSetPreferredMode(crtc->outputs[0], mode);
  1161. if (mode == NULL)
  1162. return FALSE;
  1163. ret = vncRandRCrtcSet(pScreen, crtc, mode, 0, 0, RR_Rotate_0,
  1164. crtc->numOutputs, crtc->outputs);
  1165. if (!ret)
  1166. return FALSE;
  1167. return TRUE;
  1168. }
  1169. static Bool
  1170. vfbCloseScreen(ScreenPtr pScreen)
  1171. {
  1172. vfbScreenInfoPtr pvfb = &vfbScreens[pScreen->myNum];
  1173. int i;
  1174. pScreen->CloseScreen = pvfb->closeScreen;
  1175. /*
  1176. * XXX probably lots of stuff to clean. For now,
  1177. * clear installed colormaps so that server reset works correctly.
  1178. */
  1179. for (i = 0; i < screenInfo.numScreens; i++)
  1180. SetInstalledColormap(screenInfo.screens[i], NULL);
  1181. /*
  1182. * fb overwrites miCloseScreen, so do this here
  1183. */
  1184. if (pScreen->devPrivate)
  1185. (*pScreen->DestroyPixmap) ((PixmapPtr) pScreen->devPrivate);
  1186. pScreen->devPrivate = NULL;
  1187. return pScreen->CloseScreen(pScreen);
  1188. }
  1189. static Bool
  1190. vfbScreenInit(ScreenPtr pScreen, int argc, char **argv)
  1191. {
  1192. int index = pScreen->myNum;
  1193. vfbScreenInfoPtr pvfb = &vfbScreens[index];
  1194. int dpi;
  1195. int ret;
  1196. void *pbits;
  1197. rrScrPrivPtr rp;
  1198. if (!dixRegisterPrivateKey(&cmapScrPrivateKeyRec, PRIVATE_SCREEN, 0))
  1199. return FALSE;
  1200. /* 96 is the default used by most other systems */
  1201. dpi = 96;
  1202. if (monitorResolution)
  1203. dpi = monitorResolution;
  1204. pbits = vfbAllocateFramebufferMemory(&pvfb->fb);
  1205. if (!pbits) return FALSE;
  1206. vncFbptr[index] = pbits;
  1207. vncFbstride[index] = pvfb->fb.paddedWidth;
  1208. miSetPixmapDepths();
  1209. switch (pvfb->fb.depth) {
  1210. case 16:
  1211. miSetVisualTypesAndMasks (16,
  1212. ((1 << TrueColor) |
  1213. (1 << DirectColor)),
  1214. 8, TrueColor, 0xf800, 0x07e0, 0x001f);
  1215. break;
  1216. case 24:
  1217. miSetVisualTypesAndMasks (24,
  1218. ((1 << TrueColor) |
  1219. (1 << DirectColor)),
  1220. 8, TrueColor, 0xff0000, 0x00ff00, 0x0000ff);
  1221. break;
  1222. case 32:
  1223. miSetVisualTypesAndMasks (32,
  1224. ((1 << TrueColor) |
  1225. (1 << DirectColor)),
  1226. 8, TrueColor, 0xff000000, 0x00ff0000, 0x0000ff00);
  1227. break;
  1228. default:
  1229. return FALSE;
  1230. }
  1231. if (index > 0) {
  1232. ScreenPtr prevScreen = screenInfo.screens[index-1];
  1233. pScreen->x = prevScreen->x + prevScreen->width;
  1234. pScreen->y = 0;
  1235. }
  1236. ret = fbScreenInit(pScreen, pbits, pvfb->fb.width, pvfb->fb.height,
  1237. dpi, dpi, pvfb->fb.paddedWidth, pvfb->fb.bitsPerPixel);
  1238. #ifdef RENDER
  1239. if (ret && Render)
  1240. ret = fbPictureInit (pScreen, 0, 0);
  1241. #endif
  1242. if (!ret) return FALSE;
  1243. /*
  1244. * Circumvent the backing store that was just initialised. This amounts
  1245. * to a truely bizarre way of initialising SaveDoomedAreas and friends.
  1246. */
  1247. pScreen->InstallColormap = vfbInstallColormap;
  1248. pScreen->UninstallColormap = vfbUninstallColormap;
  1249. pScreen->ListInstalledColormaps = vfbListInstalledColormaps;
  1250. pScreen->SaveScreen = vfbSaveScreen;
  1251. miPointerInitialize(pScreen, &vfbPointerSpriteFuncs, &vfbPointerCursorFuncs,
  1252. FALSE);
  1253. pScreen->blackPixel = pvfb->blackPixel;
  1254. pScreen->whitePixel = pvfb->whitePixel;
  1255. if (!pvfb->pixelFormatDefined) {
  1256. switch (pvfb->fb.depth) {
  1257. case 16:
  1258. pvfb->pixelFormatDefined = TRUE;
  1259. pvfb->rgbNotBgr = TRUE;
  1260. pvfb->blueBits = pvfb->redBits = 5;
  1261. pvfb->greenBits = 6;
  1262. break;
  1263. case 24:
  1264. case 32:
  1265. pvfb->pixelFormatDefined = TRUE;
  1266. pvfb->rgbNotBgr = TRUE;
  1267. pvfb->blueBits = pvfb->redBits = pvfb->greenBits = 8;
  1268. break;
  1269. }
  1270. }
  1271. if (pvfb->pixelFormatDefined) {
  1272. VisualPtr vis = pScreen->visuals;
  1273. for (int i = 0; i < pScreen->numVisuals; i++) {
  1274. if (pvfb->rgbNotBgr) {
  1275. vis->offsetBlue = 0;
  1276. vis->blueMask = (1 << pvfb->blueBits) - 1;
  1277. vis->offsetGreen = pvfb->blueBits;
  1278. vis->greenMask = ((1 << pvfb->greenBits) - 1) << vis->offsetGreen;
  1279. vis->offsetRed = vis->offsetGreen + pvfb->greenBits;
  1280. vis->redMask = ((1 << pvfb->redBits) - 1) << vis->offsetRed;
  1281. } else {
  1282. vis->offsetRed = 0;
  1283. vis->redMask = (1 << pvfb->redBits) - 1;
  1284. vis->offsetGreen = pvfb->redBits;
  1285. vis->greenMask = ((1 << pvfb->greenBits) - 1) << vis->offsetGreen;
  1286. vis->offsetBlue = vis->offsetGreen + pvfb->greenBits;
  1287. vis->blueMask = ((1 << pvfb->blueBits) - 1) << vis->offsetBlue;
  1288. }
  1289. vis++;
  1290. }
  1291. }
  1292. ret = fbCreateDefColormap(pScreen);
  1293. if (!ret) return FALSE;
  1294. miSetZeroLineBias(pScreen, pvfb->lineBias);
  1295. pvfb->closeScreen = pScreen->CloseScreen;
  1296. pScreen->CloseScreen = vfbCloseScreen;
  1297. ret = RRScreenInit(pScreen);
  1298. if (!ret) return FALSE;
  1299. rp = rrGetScrPriv(pScreen);
  1300. rp->rrGetInfo = vncRandRGetInfo;
  1301. rp->rrSetConfig = NULL;
  1302. rp->rrScreenSetSize = vncRandRScreenSetSize;
  1303. rp->rrCrtcSet = vncRandRCrtcSet;
  1304. rp->rrOutputValidateMode = vncRandROutputValidateMode;
  1305. rp->rrModeDestroy = vncRandRModeDestroy;
  1306. ret = vncRandRInit(pScreen);
  1307. if (!ret) return FALSE;
  1308. return TRUE;
  1309. } /* end vfbScreenInit */
  1310. static void vfbClientStateChange(CallbackListPtr *a, void *b, void *c) {
  1311. if (dispatchException & DE_RESET) {
  1312. ErrorF("Warning: VNC extension does not support -reset, terminating instead. Use -noreset to prevent termination.\n");
  1313. dispatchException |= DE_TERMINATE;
  1314. dispatchException &= ~DE_RESET;
  1315. }
  1316. }
  1317. #ifdef GLXEXT
  1318. extern void GlxExtensionInit(void);
  1319. static ExtensionModule glxExt = {
  1320. GlxExtensionInit,
  1321. "GLX",
  1322. &noGlxExtension
  1323. };
  1324. #endif
  1325. void
  1326. InitOutput(ScreenInfo *scrInfo, int argc, char **argv)
  1327. {
  1328. int i;
  1329. int NumFormats = 0;
  1330. vncPrintBanner();
  1331. #if XORG >= 120
  1332. xorgGlxCreateVendor();
  1333. #else
  1334. #ifdef GLXEXT
  1335. if (serverGeneration == 1)
  1336. LoadExtensionList(&glxExt, 1, TRUE);
  1337. #endif
  1338. #endif
  1339. /* initialize pixmap formats */
  1340. /* must have a pixmap depth to match every screen depth */
  1341. for (i = 0; i < vfbNumScreens; i++)
  1342. {
  1343. vfbPixmapDepths[vfbScreens[i].fb.depth] = TRUE;
  1344. }
  1345. /* RENDER needs a good set of pixmaps. */
  1346. if (Render) {
  1347. vfbPixmapDepths[1] = TRUE;
  1348. vfbPixmapDepths[4] = TRUE;
  1349. vfbPixmapDepths[8] = TRUE;
  1350. /* vfbPixmapDepths[15] = TRUE; */
  1351. vfbPixmapDepths[16] = TRUE;
  1352. vfbPixmapDepths[24] = TRUE;
  1353. vfbPixmapDepths[32] = TRUE;
  1354. }
  1355. for (i = 1; i <= 32; i++)
  1356. {
  1357. if (vfbPixmapDepths[i])
  1358. {
  1359. if (NumFormats >= MAXFORMATS)
  1360. FatalError ("MAXFORMATS is too small for this server\n");
  1361. scrInfo->formats[NumFormats].depth = i;
  1362. scrInfo->formats[NumFormats].bitsPerPixel = vfbBitsPerPixel(i);
  1363. scrInfo->formats[NumFormats].scanlinePad = BITMAP_SCANLINE_PAD;
  1364. NumFormats++;
  1365. }
  1366. }
  1367. scrInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  1368. scrInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  1369. scrInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  1370. scrInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  1371. scrInfo->numPixmapFormats = NumFormats;
  1372. /* initialize screens */
  1373. for (i = 0; i < vfbNumScreens; i++)
  1374. {
  1375. if (-1 == AddScreen(vfbScreenInit, argc, argv))
  1376. {
  1377. FatalError("Couldn't add screen %d\n", i);
  1378. }
  1379. }
  1380. if (!AddCallback(&ClientStateCallback, vfbClientStateChange, 0)) {
  1381. FatalError("AddCallback failed\n");
  1382. }
  1383. } /* end InitOutput */
  1384. /* this is just to get the server to link on AIX */
  1385. #ifdef AIXV3
  1386. int SelectWaitTime = 10000; /* usec */
  1387. #endif
  1388. void DDXRingBell(int percent, int pitch, int duration)
  1389. {
  1390. if (percent > 0)
  1391. vncBell();
  1392. }
  1393. Bool LegalModifier(unsigned int key, DeviceIntPtr pDev)
  1394. {
  1395. return TRUE;
  1396. }
  1397. void ProcessInputEvents(void)
  1398. {
  1399. mieqProcessInputEvents();
  1400. }
  1401. void InitInput(int argc, char *argv[])
  1402. {
  1403. mieqInit ();
  1404. }
  1405. void CloseInput(void)
  1406. {
  1407. }
  1408. void vncClientGone(int fd)
  1409. {
  1410. if (fd == vncInetdSock) {
  1411. ErrorF("inetdSock client gone\n");
  1412. GiveUp(0);
  1413. }
  1414. }