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

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