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

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