PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/nx-3.5.0/nx-X11/programs/Xserver/hw/nxagent/X/NXdispatch.c

#
C | 4679 lines | 3756 code | 525 blank | 398 comment | 720 complexity | ed6231dd082c7ab6b203d5d589fe6790 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /**************************************************************************/
  2. /* */
  3. /* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/. */
  4. /* */
  5. /* NXAGENT, NX protocol compression and NX extensions to this software */
  6. /* are copyright of NoMachine. Redistribution and use of the present */
  7. /* software is allowed according to terms specified in the file LICENSE */
  8. /* which comes in the source distribution. */
  9. /* */
  10. /* Check http://www.nomachine.com/licensing.html for applicability. */
  11. /* */
  12. /* NX and NoMachine are trademarks of Medialogic S.p.A. */
  13. /* */
  14. /* All rights reserved. */
  15. /* */
  16. /**************************************************************************/
  17. /* $XdotOrg: xc/programs/Xserver/dix/dispatch.c,v 1.13 2005/09/13 01:33:19 daniels Exp $ */
  18. /* $Xorg: dispatch.c,v 1.5 2001/02/09 02:04:40 xorgcvs Exp $ */
  19. /************************************************************
  20. Copyright 1987, 1989, 1998 The Open Group
  21. Permission to use, copy, modify, distribute, and sell this software and its
  22. documentation for any purpose is hereby granted without fee, provided that
  23. the above copyright notice appear in all copies and that both that
  24. copyright notice and this permission notice appear in supporting
  25. documentation.
  26. The above copyright notice and this permission notice shall be included in
  27. all copies or substantial portions of the Software.
  28. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  32. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. Except as contained in this notice, the name of The Open Group shall not be
  35. used in advertising or otherwise to promote the sale, use or other dealings
  36. in this Software without prior written authorization from The Open Group.
  37. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  38. All Rights Reserved
  39. Permission to use, copy, modify, and distribute this software and its
  40. documentation for any purpose and without fee is hereby granted,
  41. provided that the above copyright notice appear in all copies and that
  42. both that copyright notice and this permission notice appear in
  43. supporting documentation, and that the name of Digital not be
  44. used in advertising or publicity pertaining to distribution of the
  45. software without specific, written prior permission.
  46. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  47. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  48. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  49. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  50. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  51. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  52. SOFTWARE.
  53. ********************************************************/
  54. /* The panoramix components contained the following notice */
  55. /*****************************************************************
  56. Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
  57. Permission is hereby granted, free of charge, to any person obtaining a copy
  58. of this software and associated documentation files (the "Software"), to deal
  59. in the Software without restriction, including without limitation the rights
  60. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  61. copies of the Software.
  62. The above copyright notice and this permission notice shall be included in
  63. all copies or substantial portions of the Software.
  64. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  65. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  66. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  67. DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
  68. BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
  69. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  70. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  71. Except as contained in this notice, the name of Digital Equipment Corporation
  72. shall not be used in advertising or otherwise to promote the sale, use or other
  73. dealings in this Software without prior written authorization from Digital
  74. Equipment Corporation.
  75. ******************************************************************/
  76. /* $XFree86: xc/programs/Xserver/dix/dispatch.c,v 3.32 2003/11/10 18:21:45 tsi Exp $ */
  77. #ifdef HAVE_DIX_CONFIG_H
  78. #include <dix-config.h>
  79. #endif
  80. #ifdef PANORAMIX_DEBUG
  81. #include <stdio.h>
  82. int ProcInitialConnection();
  83. #endif
  84. #ifdef __sun
  85. #define False 0
  86. #define True 1
  87. #endif
  88. #define GC XlibGC
  89. #include <X11/Xlib.h>
  90. #undef GC
  91. #include "windowstr.h"
  92. #include <X11/fonts/fontstruct.h>
  93. #include "dixfontstr.h"
  94. #include "gcstruct.h"
  95. #include "selection.h"
  96. #include "colormapst.h"
  97. #include "cursorstr.h"
  98. #include "scrnintstr.h"
  99. #include "opaque.h"
  100. #include "input.h"
  101. #include "servermd.h"
  102. #include "extnsionst.h"
  103. #include "dixfont.h"
  104. #include "../../dix/dispatch.h"
  105. #include "swaprep.h"
  106. #include "swapreq.h"
  107. #ifdef PANORAMIX
  108. #include "panoramiX.h"
  109. #include "panoramiXsrv.h"
  110. #endif
  111. #ifdef XCSECURITY
  112. #define _SECURITY_SERVER
  113. #include <X11/extensions/security.h>
  114. #endif
  115. #ifdef XAPPGROUP
  116. #include <X11/extensions/Xagsrv.h>
  117. #endif
  118. #ifdef XKB
  119. #define XKB_IN_SERVER
  120. #include "inputstr.h"
  121. #include <X11/extensions/XKBsrv.h>
  122. #endif
  123. #include "Atoms.h"
  124. #include "Splash.h"
  125. #include "Client.h"
  126. #include "Clipboard.h"
  127. #include "Reconnect.h"
  128. #include "Millis.h"
  129. #include "Font.h"
  130. #include "Shadow.h"
  131. #include "Handlers.h"
  132. #include "Keyboard.h"
  133. const int nxagentMaxFontNames = 10000;
  134. char dispatchExceptionAtReset = DE_RESET;
  135. /*
  136. * This allows the agent to exit if no
  137. * client is connected within a timeout.
  138. */
  139. int nxagentClients = 0;
  140. void nxagentWaitDisplay(void);
  141. void nxagentListRemoteFonts(const char *, int);
  142. unsigned int nxagentWMtimeout = 0;
  143. Bool nxagentWMPassed = 0;
  144. /*
  145. * Timeouts based on screen saver time.
  146. */
  147. int nxagentAutoDisconnectTimeout = 0;
  148. #ifdef LBX
  149. #include "../../lbx/lbxserve.h"
  150. #endif
  151. #include "Xatom.h"
  152. /*
  153. * Set here the required log level.
  154. */
  155. #define PANIC
  156. #define WARNING
  157. #undef TEST
  158. #undef WATCH
  159. /*
  160. * Log begin and end of the important handlers.
  161. */
  162. #undef BLOCKS
  163. #ifdef WATCH
  164. #include "unistd.h"
  165. #endif
  166. #ifdef TEST
  167. #include "Literals.h"
  168. #endif
  169. #define mskcnt ((MAXCLIENTS + 31) / 32)
  170. #define BITMASK(i) (1U << ((i) & 31))
  171. #define MASKIDX(i) ((i) >> 5)
  172. #define MASKWORD(buf, i) buf[MASKIDX(i)]
  173. #define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
  174. #define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
  175. #define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
  176. extern xConnSetupPrefix connSetupPrefix;
  177. extern char *ConnectionInfo;
  178. Selection *CurrentSelections;
  179. int NumCurrentSelections;
  180. CallbackListPtr SelectionCallback = NULL;
  181. #ifdef VIEWPORT_FRAME
  182. extern WindowPtr nxagentViewportFrameLeft;
  183. extern WindowPtr nxagentViewportFrameRight;
  184. extern WindowPtr nxagentViewportFrameAbove;
  185. extern WindowPtr nxagentViewportFrameBelow;
  186. #define IsViewportFrame(pWin) ((pWin) == nxagentViewportFrameLeft || \
  187. (pWin) == nxagentViewportFrameRight || \
  188. (pWin) == nxagentViewportFrameAbove || \
  189. (pWin) == nxagentViewportFrameBelow)
  190. #else
  191. #define IsViewportFrame(pWin) (0)
  192. #endif /* #ifdef VIEWPORT_FRAME */
  193. extern int nxagentMaxAllowedResets;
  194. extern int nxagentFindClientResource(int, RESTYPE, pointer);
  195. static ClientPtr grabClient;
  196. #define GrabNone 0
  197. #define GrabActive 1
  198. #define GrabKickout 2
  199. static int grabState = GrabNone;
  200. static long grabWaiters[mskcnt];
  201. CallbackListPtr ServerGrabCallback = NULL;
  202. HWEventQueuePtr checkForInput[2];
  203. extern int connBlockScreenStart;
  204. static void KillAllClients(void);
  205. static void DeleteClientFromAnySelections(ClientPtr client);
  206. static int nextFreeClientID; /* always MIN free client ID */
  207. static int nClients; /* number of authorized clients */
  208. CallbackListPtr ClientStateCallback;
  209. /* dispatchException & isItTimeToYield must be declared volatile since they
  210. * are modified by signal handlers - otherwise optimizer may assume it doesn't
  211. * need to actually check value in memory when used and may miss changes from
  212. * signal handlers.
  213. */
  214. volatile char dispatchException = 0;
  215. volatile char isItTimeToYield;
  216. /* Various of the DIX function interfaces were not designed to allow
  217. * the client->errorValue to be set on BadValue and other errors.
  218. * Rather than changing interfaces and breaking untold code we introduce
  219. * a new global that dispatch can use.
  220. */
  221. XID clientErrorValue; /* XXX this is a kludge */
  222. #define SAME_SCREENS(a, b) (\
  223. (a.pScreen == b.pScreen))
  224. void
  225. SetInputCheck(HWEventQueuePtr c0, HWEventQueuePtr c1)
  226. {
  227. checkForInput[0] = c0;
  228. checkForInput[1] = c1;
  229. }
  230. void
  231. UpdateCurrentTime()
  232. {
  233. TimeStamp systime;
  234. /* To avoid time running backwards, we must call GetTimeInMillis before
  235. * calling ProcessInputEvents.
  236. */
  237. systime.months = currentTime.months;
  238. systime.milliseconds = GetTimeInMillis();
  239. if (systime.milliseconds < currentTime.milliseconds)
  240. systime.months++;
  241. if (*checkForInput[0] != *checkForInput[1])
  242. ProcessInputEvents();
  243. if (CompareTimeStamps(systime, currentTime) == LATER)
  244. currentTime = systime;
  245. }
  246. /* Like UpdateCurrentTime, but can't call ProcessInputEvents */
  247. void
  248. UpdateCurrentTimeIf()
  249. {
  250. TimeStamp systime;
  251. systime.months = currentTime.months;
  252. systime.milliseconds = GetTimeInMillis();
  253. if (systime.milliseconds < currentTime.milliseconds)
  254. systime.months++;
  255. if (*checkForInput[0] == *checkForInput[1])
  256. currentTime = systime;
  257. }
  258. void
  259. InitSelections()
  260. {
  261. if (CurrentSelections)
  262. xfree(CurrentSelections);
  263. CurrentSelections = (Selection *)NULL;
  264. NumCurrentSelections = 0;
  265. #ifdef NXAGENT_CLIPBOARD
  266. {
  267. Selection *newsels;
  268. newsels = (Selection *)xalloc(2 * sizeof(Selection));
  269. if (!newsels)
  270. return;
  271. NumCurrentSelections += 2;
  272. CurrentSelections = newsels;
  273. CurrentSelections[0].selection = XA_PRIMARY;
  274. CurrentSelections[0].lastTimeChanged = ClientTimeToServerTime(0);
  275. CurrentSelections[0].window = WindowTable[0]->drawable.id;
  276. CurrentSelections[0].pWin = NULL;
  277. CurrentSelections[0].client = NullClient;
  278. CurrentSelections[1].selection = MakeAtom("CLIPBOARD", 9, 1);
  279. CurrentSelections[1].lastTimeChanged = ClientTimeToServerTime(0);
  280. CurrentSelections[1].window = WindowTable[0]->drawable.id;
  281. CurrentSelections[1].pWin = NULL;
  282. CurrentSelections[1].client = NullClient;
  283. }
  284. #endif
  285. }
  286. void
  287. FlushClientCaches(XID id)
  288. {
  289. int i;
  290. register ClientPtr client;
  291. client = clients[CLIENT_ID(id)];
  292. if (client == NullClient)
  293. return ;
  294. for (i=0; i<currentMaxClients; i++)
  295. {
  296. client = clients[i];
  297. if (client != NullClient)
  298. {
  299. if (client->lastDrawableID == id)
  300. {
  301. client->lastDrawableID = WindowTable[0]->drawable.id;
  302. client->lastDrawable = (DrawablePtr)WindowTable[0];
  303. }
  304. else if (client->lastGCID == id)
  305. {
  306. client->lastGCID = INVALID;
  307. client->lastGC = (GCPtr)NULL;
  308. }
  309. }
  310. }
  311. }
  312. #ifdef SMART_SCHEDULE
  313. #undef SMART_DEBUG
  314. #define SMART_SCHEDULE_DEFAULT_INTERVAL 20 /* ms */
  315. #define SMART_SCHEDULE_MAX_SLICE 200 /* ms */
  316. Bool SmartScheduleDisable = FALSE;
  317. long SmartScheduleSlice = SMART_SCHEDULE_DEFAULT_INTERVAL;
  318. long SmartScheduleInterval = SMART_SCHEDULE_DEFAULT_INTERVAL;
  319. long SmartScheduleMaxSlice = SMART_SCHEDULE_MAX_SLICE;
  320. long SmartScheduleTime;
  321. ClientPtr SmartLastClient;
  322. int SmartLastIndex[SMART_MAX_PRIORITY-SMART_MIN_PRIORITY+1];
  323. int SmartScheduleClient(int *clientReady, int nready);
  324. #ifdef SMART_DEBUG
  325. long SmartLastPrint;
  326. #endif
  327. void Dispatch(void);
  328. void InitProcVectors(void);
  329. int
  330. SmartScheduleClient (int *clientReady, int nready)
  331. {
  332. ClientPtr pClient;
  333. int i;
  334. int client;
  335. int bestPrio, best = 0;
  336. int bestRobin, robin;
  337. long now = SmartScheduleTime;
  338. long idle;
  339. bestPrio = -0x7fffffff;
  340. bestRobin = 0;
  341. idle = 2 * SmartScheduleSlice;
  342. for (i = 0; i < nready; i++)
  343. {
  344. client = clientReady[i];
  345. pClient = clients[client];
  346. /* Praise clients which are idle */
  347. if ((now - pClient->smart_check_tick) >= idle)
  348. {
  349. if (pClient->smart_priority < 0)
  350. pClient->smart_priority++;
  351. }
  352. pClient->smart_check_tick = now;
  353. /* check priority to select best client */
  354. robin = (pClient->index - SmartLastIndex[pClient->smart_priority-SMART_MIN_PRIORITY]) & 0xff;
  355. if (pClient->smart_priority > bestPrio ||
  356. (pClient->smart_priority == bestPrio && robin > bestRobin))
  357. {
  358. bestPrio = pClient->smart_priority;
  359. bestRobin = robin;
  360. best = client;
  361. }
  362. #ifdef SMART_DEBUG
  363. if ((now - SmartLastPrint) >= 5000)
  364. fprintf (stderr, " %2d: %3d", client, pClient->smart_priority);
  365. #endif
  366. }
  367. #ifdef SMART_DEBUG
  368. if ((now - SmartLastPrint) >= 5000)
  369. {
  370. fprintf (stderr, " use %2d\n", best);
  371. SmartLastPrint = now;
  372. }
  373. #endif
  374. pClient = clients[best];
  375. SmartLastIndex[bestPrio-SMART_MIN_PRIORITY] = pClient->index;
  376. /*
  377. * Set current client pointer
  378. */
  379. if (SmartLastClient != pClient)
  380. {
  381. pClient->smart_start_tick = now;
  382. SmartLastClient = pClient;
  383. }
  384. /*
  385. * Adjust slice
  386. */
  387. if (nready == 1)
  388. {
  389. /*
  390. * If it's been a long time since another client
  391. * has run, bump the slice up to get maximal
  392. * performance from a single client
  393. */
  394. if ((now - pClient->smart_start_tick) > 1000 &&
  395. SmartScheduleSlice < SmartScheduleMaxSlice)
  396. {
  397. SmartScheduleSlice += SmartScheduleInterval;
  398. }
  399. }
  400. else
  401. {
  402. SmartScheduleSlice = SmartScheduleInterval;
  403. }
  404. return best;
  405. }
  406. #endif
  407. #define MAJOROP ((xReq *)client->requestBuffer)->reqType
  408. void
  409. Dispatch(void)
  410. {
  411. register int *clientReady; /* array of request ready clients */
  412. register int result;
  413. register ClientPtr client;
  414. register int nready;
  415. register HWEventQueuePtr* icheck = checkForInput;
  416. #ifdef SMART_SCHEDULE
  417. long start_tick;
  418. #endif
  419. unsigned long currentDispatch = 0;
  420. nextFreeClientID = 1;
  421. InitSelections();
  422. nClients = 0;
  423. /*
  424. * The agent initialization was successfully
  425. * completed. We can now handle our clients.
  426. */
  427. #ifdef XKB
  428. nxagentInitXkbWrapper();
  429. nxagentTuneXkbWrapper();
  430. #endif
  431. #ifdef NXAGENT_ONSTART
  432. /*
  433. * Set NX_WM property (used by NX client to identify
  434. * the agent's window) three seconds since the first
  435. * client connects.
  436. */
  437. nxagentWMtimeout = GetTimeInMillis() + 3000;
  438. #endif
  439. clientReady = (int *) ALLOCATE_LOCAL(sizeof(int) * MaxClients);
  440. if (!clientReady)
  441. return;
  442. #ifdef WATCH
  443. fprintf(stderr, "Dispatch: Watchpoint 12.\n");
  444. /*
  445. Reply Total Cached Bits In Bits Out Bits/Reply Ratio
  446. ------- ----- ------ ------- -------- ---------- -----
  447. #3 1 352 bits (0 KB) -> 236 bits (0 KB) -> 352/1 -> 236/1 = 1.492:1
  448. #14 1 256 bits (0 KB) -> 101 bits (0 KB) -> 256/1 -> 101/1 = 2.535:1
  449. #16 1 256 bits (0 KB) -> 26 bits (0 KB) -> 256/1 -> 26/1 = 9.846:1
  450. #20 2 2 12256 bits (1 KB) -> 56 bits (0 KB) -> 6128/1 -> 28/1 = 218.857:1
  451. #43 1 256 bits (0 KB) -> 45 bits (0 KB) -> 256/1 -> 45/1 = 5.689:1
  452. #47 2 2 42304 bits (5 KB) -> 49 bits (0 KB) -> 21152/1 -> 24/1 = 863.347:1
  453. #98 1 256 bits (0 KB) -> 34 bits (0 KB) -> 256/1 -> 34/1 = 7.529:1
  454. */
  455. sleep(30);
  456. #endif
  457. #ifdef TEST
  458. fprintf(stderr, "Dispatch: Value of dispatchException is [%x].\n",
  459. dispatchException);
  460. fprintf(stderr, "Dispatch: Value of dispatchExceptionAtReset is [%x].\n",
  461. dispatchExceptionAtReset);
  462. #endif
  463. if (!(dispatchException & DE_TERMINATE))
  464. dispatchException = 0;
  465. while (!dispatchException)
  466. {
  467. if (*icheck[0] != *icheck[1])
  468. {
  469. ProcessInputEvents();
  470. FlushIfCriticalOutputPending();
  471. }
  472. /*
  473. * Ensure we remove the splash after the timeout.
  474. * Initializing clientReady[0] to -1 will tell
  475. * WaitForSomething() to yield control after the
  476. * timeout set in clientReady[1].
  477. */
  478. clientReady[0] = 0;
  479. if (nxagentSplashWindow != None || (nxagentOption(Xdmcp) == 1 && nxagentXdmcpUp == 0))
  480. {
  481. #ifdef TEST
  482. fprintf(stderr, "******Dispatch: Requesting a timeout of [%d] Ms.\n",
  483. NXAGENT_WAKEUP);
  484. #endif
  485. clientReady[0] = -1;
  486. clientReady[1] = NXAGENT_WAKEUP;
  487. }
  488. if (serverGeneration > nxagentMaxAllowedResets &&
  489. nxagentSessionState == SESSION_STARTING &&
  490. (nxagentOption(Xdmcp) == 0 || nxagentXdmcpUp == 1))
  491. {
  492. #ifdef NX_DEBUG_INPUT
  493. fprintf(stderr, "Session: Session started at '%s' timestamp [%lu].\n",
  494. GetTimeAsString(), GetTimeInMillis());
  495. #else
  496. fprintf(stderr, "Session: Session started at '%s'.\n",
  497. GetTimeAsString());
  498. #endif
  499. nxagentSessionState = SESSION_UP;
  500. }
  501. #ifdef BLOCKS
  502. fprintf(stderr, "[End dispatch]\n");
  503. #endif
  504. nready = WaitForSomething(clientReady);
  505. #ifdef BLOCKS
  506. fprintf(stderr, "[Begin dispatch]\n");
  507. #endif
  508. #ifdef TEST
  509. fprintf(stderr, "******Dispatch: Running with [%d] clients ready.\n",
  510. nready);
  511. #endif
  512. #ifdef NXAGENT_ONSTART
  513. currentDispatch = GetTimeInMillis();
  514. /*
  515. * If the timeout is expired set the
  516. * selection informing the NX client
  517. * that the agent is ready.
  518. */
  519. if (!nxagentWMPassed && (nxagentWMtimeout < currentDispatch))
  520. {
  521. nxagentRemoveSplashWindow(NULL);
  522. }
  523. nxagentClients = nClients;
  524. #endif
  525. #ifdef SMART_SCHEDULE
  526. if (nready && !SmartScheduleDisable)
  527. {
  528. clientReady[0] = SmartScheduleClient (clientReady, nready);
  529. nready = 1;
  530. }
  531. #endif
  532. /*****************
  533. * Handle events in round robin fashion, doing input between
  534. * each round
  535. *****************/
  536. while (!dispatchException && (--nready >= 0))
  537. {
  538. client = clients[clientReady[nready]];
  539. if (! client)
  540. {
  541. /* KillClient can cause this to happen */
  542. continue;
  543. }
  544. /* GrabServer activation can cause this to be true */
  545. if (grabState == GrabKickout)
  546. {
  547. grabState = GrabActive;
  548. break;
  549. }
  550. isItTimeToYield = FALSE;
  551. requestingClient = client;
  552. #ifdef SMART_SCHEDULE
  553. start_tick = SmartScheduleTime;
  554. #endif
  555. while (!isItTimeToYield)
  556. {
  557. if (*icheck[0] != *icheck[1])
  558. {
  559. ProcessInputEvents();
  560. FlushIfCriticalOutputPending();
  561. }
  562. #ifdef SMART_SCHEDULE
  563. if (!SmartScheduleDisable &&
  564. (SmartScheduleTime - start_tick) >= SmartScheduleSlice)
  565. {
  566. /* Penalize clients which consume ticks */
  567. if (client->smart_priority > SMART_MIN_PRIORITY)
  568. client->smart_priority--;
  569. break;
  570. }
  571. #endif
  572. /* now, finally, deal with client requests */
  573. #ifdef TEST
  574. fprintf(stderr, "******Dispatch: Reading request from client [%d].\n",
  575. client->index);
  576. #endif
  577. result = ReadRequestFromClient(client);
  578. if (result <= 0)
  579. {
  580. if (result < 0)
  581. CloseDownClient(client);
  582. break;
  583. }
  584. #ifdef NXAGENT_SERVER
  585. #ifdef TEST
  586. else
  587. {
  588. if (MAJOROP > 127)
  589. {
  590. fprintf(stderr, "******Dispatch: Read [Extension] request OPCODE#%d MINOR#%d "
  591. "size [%d] client [%d].\n", MAJOROP, *((char *) client->requestBuffer + 1),
  592. client->req_len << 2, client->index);
  593. }
  594. else
  595. {
  596. fprintf(stderr, "******Dispatch: Read [%s] request OPCODE#%d size [%d] client [%d].\n",
  597. nxagentRequestLiteral[MAJOROP], MAJOROP, client->req_len << 2,
  598. client->index);
  599. }
  600. }
  601. #endif
  602. #endif
  603. client->sequence++;
  604. #ifdef DEBUG
  605. if (client->requestLogIndex == MAX_REQUEST_LOG)
  606. client->requestLogIndex = 0;
  607. client->requestLog[client->requestLogIndex] = MAJOROP;
  608. client->requestLogIndex++;
  609. #endif
  610. if (result > (maxBigRequestSize << 2))
  611. result = BadLength;
  612. else
  613. #ifdef NXAGENT_SERVER
  614. {
  615. result = (* client->requestVector[MAJOROP])(client);
  616. #ifdef TEST
  617. if (MAJOROP > 127)
  618. {
  619. fprintf(stderr, "******Dispatch: Handled [Extension] request OPCODE#%d MINOR#%d "
  620. "size [%d] client [%d] result [%d].\n", MAJOROP,
  621. *((char *) client->requestBuffer + 1), client->req_len << 2,
  622. client->index, result);
  623. }
  624. else
  625. {
  626. fprintf(stderr, "******Dispatch: Handled [%s] request OPCODE#%d size [%d] client [%d] "
  627. "result [%d].\n", nxagentRequestLiteral[MAJOROP], MAJOROP,
  628. client->req_len << 2, client->index, result);
  629. }
  630. #endif
  631. /*
  632. * Can set isItTimeToYield to force
  633. * the dispatcher to pay attention
  634. * to another client.
  635. */
  636. nxagentDispatchHandler(client, client->req_len << 2, 0);
  637. }
  638. #else
  639. result = (* client->requestVector[MAJOROP])(client);
  640. #endif
  641. if (result != Success)
  642. {
  643. if (client->noClientException != Success)
  644. CloseDownClient(client);
  645. else
  646. SendErrorToClient(client, MAJOROP,
  647. MinorOpcodeOfRequest(client),
  648. client->errorValue, result);
  649. break;
  650. }
  651. #ifdef DAMAGEEXT
  652. FlushIfCriticalOutputPending ();
  653. #endif
  654. }
  655. FlushAllOutput();
  656. #ifdef SMART_SCHEDULE
  657. client = clients[clientReady[nready]];
  658. if (client)
  659. client->smart_stop_tick = SmartScheduleTime;
  660. #endif
  661. requestingClient = NULL;
  662. }
  663. dispatchException &= ~DE_PRIORITYCHANGE;
  664. }
  665. #if defined(DDXBEFORERESET)
  666. ddxBeforeReset ();
  667. #endif
  668. if ((dispatchException & DE_RESET) &&
  669. (serverGeneration > nxagentMaxAllowedResets))
  670. {
  671. dispatchException &= ~DE_RESET;
  672. dispatchException |= DE_TERMINATE;
  673. fprintf(stderr, "Info: Reached threshold of maximum allowed resets.\n");
  674. }
  675. nxagentResetAtomMap();
  676. if (serverGeneration > nxagentMaxAllowedResets)
  677. {
  678. /*
  679. * The session is terminating. Force an I/O
  680. * error on the display and wait until the
  681. * NX transport is gone.
  682. */
  683. fprintf(stderr, "Session: Terminating session at '%s'.\n", GetTimeAsString());
  684. nxagentWaitDisplay();
  685. fprintf(stderr, "Session: Session terminated at '%s'.\n", GetTimeAsString());
  686. }
  687. if (nxagentOption(Shadow) == 1)
  688. {
  689. NXShadowDestroy();
  690. }
  691. KillAllClients();
  692. DEALLOCATE_LOCAL(clientReady);
  693. dispatchException &= ~DE_RESET;
  694. }
  695. #undef MAJOROP
  696. int
  697. ProcBadRequest(ClientPtr client)
  698. {
  699. return (BadRequest);
  700. }
  701. int
  702. ProcCreateWindow(ClientPtr client)
  703. {
  704. register WindowPtr pParent, pWin;
  705. REQUEST(xCreateWindowReq);
  706. int result;
  707. int len;
  708. REQUEST_AT_LEAST_SIZE(xCreateWindowReq);
  709. LEGAL_NEW_RESOURCE(stuff->wid, client);
  710. if (!(pParent = (WindowPtr)SecurityLookupWindow(stuff->parent, client,
  711. SecurityWriteAccess)))
  712. return BadWindow;
  713. len = client->req_len - (sizeof(xCreateWindowReq) >> 2);
  714. if (Ones(stuff->mask) != len)
  715. return BadLength;
  716. if (!stuff->width || !stuff->height)
  717. {
  718. client->errorValue = 0;
  719. return BadValue;
  720. }
  721. pWin = CreateWindow(stuff->wid, pParent, stuff->x,
  722. stuff->y, stuff->width, stuff->height,
  723. stuff->borderWidth, stuff->class,
  724. stuff->mask, (XID *) &stuff[1],
  725. (int)stuff->depth,
  726. client, stuff->visual, &result);
  727. if (pWin)
  728. {
  729. Mask mask = pWin->eventMask;
  730. pWin->eventMask = 0; /* subterfuge in case AddResource fails */
  731. if (!AddResource(stuff->wid, RT_WINDOW, (pointer)pWin))
  732. return BadAlloc;
  733. pWin->eventMask = mask;
  734. }
  735. if (client->noClientException != Success)
  736. return(client->noClientException);
  737. else
  738. return(result);
  739. }
  740. int
  741. ProcChangeWindowAttributes(register ClientPtr client)
  742. {
  743. register WindowPtr pWin;
  744. REQUEST(xChangeWindowAttributesReq);
  745. register int result;
  746. int len;
  747. REQUEST_AT_LEAST_SIZE(xChangeWindowAttributesReq);
  748. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  749. SecurityWriteAccess);
  750. if (!pWin)
  751. return(BadWindow);
  752. len = client->req_len - (sizeof(xChangeWindowAttributesReq) >> 2);
  753. if (len != Ones(stuff->valueMask))
  754. return BadLength;
  755. result = ChangeWindowAttributes(pWin,
  756. stuff->valueMask,
  757. (XID *) &stuff[1],
  758. client);
  759. if (client->noClientException != Success)
  760. return(client->noClientException);
  761. else
  762. return(result);
  763. }
  764. int
  765. ProcGetWindowAttributes(register ClientPtr client)
  766. {
  767. register WindowPtr pWin;
  768. REQUEST(xResourceReq);
  769. xGetWindowAttributesReply wa;
  770. REQUEST_SIZE_MATCH(xResourceReq);
  771. pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
  772. SecurityReadAccess);
  773. if (!pWin)
  774. return(BadWindow);
  775. GetWindowAttributes(pWin, client, &wa);
  776. WriteReplyToClient(client, sizeof(xGetWindowAttributesReply), &wa);
  777. return(client->noClientException);
  778. }
  779. int
  780. ProcDestroyWindow(register ClientPtr client)
  781. {
  782. register WindowPtr pWin;
  783. REQUEST(xResourceReq);
  784. REQUEST_SIZE_MATCH(xResourceReq);
  785. pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
  786. SecurityDestroyAccess);
  787. if (!pWin)
  788. return(BadWindow);
  789. if (pWin->parent)
  790. FreeResource(stuff->id, RT_NONE);
  791. return(client->noClientException);
  792. }
  793. int
  794. ProcDestroySubwindows(register ClientPtr client)
  795. {
  796. register WindowPtr pWin;
  797. REQUEST(xResourceReq);
  798. REQUEST_SIZE_MATCH(xResourceReq);
  799. pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
  800. SecurityDestroyAccess);
  801. if (!pWin)
  802. return(BadWindow);
  803. DestroySubwindows(pWin, client);
  804. return(client->noClientException);
  805. }
  806. int
  807. ProcChangeSaveSet(register ClientPtr client)
  808. {
  809. register WindowPtr pWin;
  810. REQUEST(xChangeSaveSetReq);
  811. register int result;
  812. REQUEST_SIZE_MATCH(xChangeSaveSetReq);
  813. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  814. SecurityReadAccess);
  815. if (!pWin)
  816. return(BadWindow);
  817. if (client->clientAsMask == (CLIENT_BITS(pWin->drawable.id)))
  818. return BadMatch;
  819. if ((stuff->mode == SetModeInsert) || (stuff->mode == SetModeDelete))
  820. {
  821. result = AlterSaveSetForClient(client, pWin, stuff->mode, FALSE, TRUE);
  822. if (client->noClientException != Success)
  823. return(client->noClientException);
  824. else
  825. return(result);
  826. }
  827. else
  828. {
  829. client->errorValue = stuff->mode;
  830. return( BadValue );
  831. }
  832. }
  833. int
  834. ProcReparentWindow(register ClientPtr client)
  835. {
  836. register WindowPtr pWin, pParent;
  837. REQUEST(xReparentWindowReq);
  838. register int result;
  839. REQUEST_SIZE_MATCH(xReparentWindowReq);
  840. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  841. SecurityWriteAccess);
  842. if (!pWin)
  843. return(BadWindow);
  844. if (!nxagentWMPassed)
  845. {
  846. nxagentRemoveSplashWindow(pWin);
  847. }
  848. pParent = (WindowPtr)SecurityLookupWindow(stuff->parent, client,
  849. SecurityWriteAccess);
  850. if (!pParent)
  851. return(BadWindow);
  852. if (SAME_SCREENS(pWin->drawable, pParent->drawable))
  853. {
  854. if ((pWin->backgroundState == ParentRelative) &&
  855. (pParent->drawable.depth != pWin->drawable.depth))
  856. return BadMatch;
  857. if ((pWin->drawable.class != InputOnly) &&
  858. (pParent->drawable.class == InputOnly))
  859. return BadMatch;
  860. result = ReparentWindow(pWin, pParent,
  861. (short)stuff->x, (short)stuff->y, client);
  862. if (client->noClientException != Success)
  863. return(client->noClientException);
  864. else
  865. return(result);
  866. }
  867. else
  868. return (BadMatch);
  869. }
  870. int
  871. ProcMapWindow(register ClientPtr client)
  872. {
  873. register WindowPtr pWin;
  874. REQUEST(xResourceReq);
  875. REQUEST_SIZE_MATCH(xResourceReq);
  876. pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
  877. SecurityReadAccess);
  878. if (!pWin)
  879. return(BadWindow);
  880. MapWindow(pWin, client);
  881. /* update cache to say it is mapped */
  882. return(client->noClientException);
  883. }
  884. int
  885. ProcMapSubwindows(register ClientPtr client)
  886. {
  887. register WindowPtr pWin;
  888. REQUEST(xResourceReq);
  889. REQUEST_SIZE_MATCH(xResourceReq);
  890. pWin = (WindowPtr)SecurityLookupWindow( stuff->id, client,
  891. SecurityReadAccess);
  892. if (!pWin)
  893. return(BadWindow);
  894. MapSubwindows(pWin, client);
  895. /* update cache to say it is mapped */
  896. return(client->noClientException);
  897. }
  898. int
  899. ProcUnmapWindow(register ClientPtr client)
  900. {
  901. register WindowPtr pWin;
  902. REQUEST(xResourceReq);
  903. REQUEST_SIZE_MATCH(xResourceReq);
  904. pWin = (WindowPtr)SecurityLookupWindow( stuff->id, client,
  905. SecurityReadAccess);
  906. if (!pWin)
  907. return(BadWindow);
  908. UnmapWindow(pWin, FALSE);
  909. /* update cache to say it is mapped */
  910. return(client->noClientException);
  911. }
  912. int
  913. ProcUnmapSubwindows(register ClientPtr client)
  914. {
  915. register WindowPtr pWin;
  916. REQUEST(xResourceReq);
  917. REQUEST_SIZE_MATCH(xResourceReq);
  918. pWin = (WindowPtr)SecurityLookupWindow( stuff->id, client,
  919. SecurityReadAccess);
  920. if (!pWin)
  921. return(BadWindow);
  922. UnmapSubwindows(pWin);
  923. return(client->noClientException);
  924. }
  925. int
  926. ProcConfigureWindow(register ClientPtr client)
  927. {
  928. register WindowPtr pWin;
  929. REQUEST(xConfigureWindowReq);
  930. register int result;
  931. int len;
  932. REQUEST_AT_LEAST_SIZE(xConfigureWindowReq);
  933. pWin = (WindowPtr)SecurityLookupWindow( stuff->window, client,
  934. SecurityWriteAccess);
  935. if (!pWin)
  936. return(BadWindow);
  937. len = client->req_len - (sizeof(xConfigureWindowReq) >> 2);
  938. if (Ones((Mask)stuff->mask) != len)
  939. return BadLength;
  940. result = ConfigureWindow(pWin, (Mask)stuff->mask, (XID *) &stuff[1],
  941. client);
  942. if (client->noClientException != Success)
  943. return(client->noClientException);
  944. else
  945. return(result);
  946. }
  947. int
  948. ProcCirculateWindow(register ClientPtr client)
  949. {
  950. register WindowPtr pWin;
  951. REQUEST(xCirculateWindowReq);
  952. REQUEST_SIZE_MATCH(xCirculateWindowReq);
  953. if ((stuff->direction != RaiseLowest) &&
  954. (stuff->direction != LowerHighest))
  955. {
  956. client->errorValue = stuff->direction;
  957. return BadValue;
  958. }
  959. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  960. SecurityWriteAccess);
  961. if (!pWin)
  962. return(BadWindow);
  963. CirculateWindow(pWin, (int)stuff->direction, client);
  964. return(client->noClientException);
  965. }
  966. int
  967. GetGeometry(register ClientPtr client, xGetGeometryReply *rep)
  968. {
  969. register DrawablePtr pDraw;
  970. REQUEST(xResourceReq);
  971. REQUEST_SIZE_MATCH(xResourceReq);
  972. SECURITY_VERIFY_GEOMETRABLE (pDraw, stuff->id, client, SecurityReadAccess);
  973. rep->type = X_Reply;
  974. rep->length = 0;
  975. rep->sequenceNumber = client->sequence;
  976. rep->root = WindowTable[pDraw->pScreen->myNum]->drawable.id;
  977. rep->depth = pDraw->depth;
  978. rep->width = pDraw->width;
  979. rep->height = pDraw->height;
  980. /* XXX - Because the pixmap-implementation of the multibuffer extension
  981. * may have the buffer-id's drawable resource value be a pointer
  982. * to the buffer's window instead of the buffer itself
  983. * (this happens if the buffer is the displayed buffer),
  984. * we also have to check that the id matches before we can
  985. * truly say that it is a DRAWABLE_WINDOW.
  986. */
  987. if ((pDraw->type == UNDRAWABLE_WINDOW) ||
  988. ((pDraw->type == DRAWABLE_WINDOW) && (stuff->id == pDraw->id)))
  989. {
  990. register WindowPtr pWin = (WindowPtr)pDraw;
  991. rep->x = pWin->origin.x - wBorderWidth (pWin);
  992. rep->y = pWin->origin.y - wBorderWidth (pWin);
  993. rep->borderWidth = pWin->borderWidth;
  994. }
  995. else /* DRAWABLE_PIXMAP or DRAWABLE_BUFFER */
  996. {
  997. rep->x = rep->y = rep->borderWidth = 0;
  998. }
  999. return Success;
  1000. }
  1001. int
  1002. ProcGetGeometry(register ClientPtr client)
  1003. {
  1004. xGetGeometryReply rep;
  1005. int status;
  1006. if ((status = GetGeometry(client, &rep)) != Success)
  1007. return status;
  1008. WriteReplyToClient(client, sizeof(xGetGeometryReply), &rep);
  1009. return(client->noClientException);
  1010. }
  1011. int
  1012. ProcQueryTree(register ClientPtr client)
  1013. {
  1014. xQueryTreeReply reply;
  1015. int numChildren = 0;
  1016. register WindowPtr pChild, pWin, pHead;
  1017. Window *childIDs = (Window *)NULL;
  1018. REQUEST(xResourceReq);
  1019. REQUEST_SIZE_MATCH(xResourceReq);
  1020. pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
  1021. SecurityReadAccess);
  1022. if (!pWin)
  1023. return(BadWindow);
  1024. reply.type = X_Reply;
  1025. reply.root = WindowTable[pWin->drawable.pScreen->myNum]->drawable.id;
  1026. reply.sequenceNumber = client->sequence;
  1027. if (pWin->parent)
  1028. reply.parent = pWin->parent->drawable.id;
  1029. else
  1030. reply.parent = (Window)None;
  1031. pHead = RealChildHead(pWin);
  1032. for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  1033. {
  1034. if (!IsViewportFrame(pChild))
  1035. {
  1036. numChildren++;
  1037. }
  1038. }
  1039. if (numChildren)
  1040. {
  1041. int curChild = 0;
  1042. childIDs = (Window *) ALLOCATE_LOCAL(numChildren * sizeof(Window));
  1043. if (!childIDs)
  1044. return BadAlloc;
  1045. for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  1046. {
  1047. if (!IsViewportFrame(pChild))
  1048. {
  1049. childIDs[curChild++] = pChild->drawable.id;
  1050. }
  1051. }
  1052. }
  1053. reply.nChildren = numChildren;
  1054. reply.length = (numChildren * sizeof(Window)) >> 2;
  1055. WriteReplyToClient(client, sizeof(xQueryTreeReply), &reply);
  1056. if (numChildren)
  1057. {
  1058. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  1059. WriteSwappedDataToClient(client, numChildren * sizeof(Window), childIDs);
  1060. DEALLOCATE_LOCAL(childIDs);
  1061. }
  1062. return(client->noClientException);
  1063. }
  1064. int
  1065. ProcInternAtom(register ClientPtr client)
  1066. {
  1067. Atom atom;
  1068. char *tchar;
  1069. REQUEST(xInternAtomReq);
  1070. REQUEST_FIXED_SIZE(xInternAtomReq, stuff->nbytes);
  1071. if ((stuff->onlyIfExists != xTrue) && (stuff->onlyIfExists != xFalse))
  1072. {
  1073. client->errorValue = stuff->onlyIfExists;
  1074. return(BadValue);
  1075. }
  1076. tchar = (char *) &stuff[1];
  1077. atom = MakeAtom(tchar, stuff->nbytes, !stuff->onlyIfExists);
  1078. if (atom != BAD_RESOURCE)
  1079. {
  1080. xInternAtomReply reply;
  1081. reply.type = X_Reply;
  1082. reply.length = 0;
  1083. reply.sequenceNumber = client->sequence;
  1084. reply.atom = atom;
  1085. WriteReplyToClient(client, sizeof(xInternAtomReply), &reply);
  1086. return(client->noClientException);
  1087. }
  1088. else
  1089. return (BadAlloc);
  1090. }
  1091. int
  1092. ProcGetAtomName(register ClientPtr client)
  1093. {
  1094. char *str;
  1095. xGetAtomNameReply reply;
  1096. int len;
  1097. REQUEST(xResourceReq);
  1098. REQUEST_SIZE_MATCH(xResourceReq);
  1099. if ( (str = NameForAtom(stuff->id)) )
  1100. {
  1101. len = strlen(str);
  1102. reply.type = X_Reply;
  1103. reply.length = (len + 3) >> 2;
  1104. reply.sequenceNumber = client->sequence;
  1105. reply.nameLength = len;
  1106. WriteReplyToClient(client, sizeof(xGetAtomNameReply), &reply);
  1107. (void)WriteToClient(client, len, str);
  1108. return(client->noClientException);
  1109. }
  1110. else
  1111. {
  1112. client->errorValue = stuff->id;
  1113. return (BadAtom);
  1114. }
  1115. }
  1116. #ifdef K5AUTH
  1117. extern int k5_bad();
  1118. #endif
  1119. int
  1120. ProcSetSelectionOwner(register ClientPtr client)
  1121. {
  1122. WindowPtr pWin;
  1123. TimeStamp time;
  1124. REQUEST(xSetSelectionOwnerReq);
  1125. REQUEST_SIZE_MATCH(xSetSelectionOwnerReq);
  1126. UpdateCurrentTime();
  1127. time = ClientTimeToServerTime(stuff->time);
  1128. /* If the client's time stamp is in the future relative to the server's
  1129. time stamp, do not set the selection, just return success. */
  1130. if (CompareTimeStamps(time, currentTime) == LATER)
  1131. return Success;
  1132. if (stuff->window != None)
  1133. {
  1134. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  1135. SecurityReadAccess);
  1136. if (!pWin)
  1137. return(BadWindow);
  1138. }
  1139. else
  1140. pWin = (WindowPtr)None;
  1141. if (ValidAtom(stuff->selection))
  1142. {
  1143. int i = 0;
  1144. /*
  1145. * First, see if the selection is already set...
  1146. */
  1147. while ((i < NumCurrentSelections) &&
  1148. CurrentSelections[i].selection != stuff->selection)
  1149. i++;
  1150. if (i < NumCurrentSelections)
  1151. {
  1152. xEvent event;
  1153. /* If the timestamp in client's request is in the past relative
  1154. to the time stamp indicating the last time the owner of the
  1155. selection was set, do not set the selection, just return
  1156. success. */
  1157. if (CompareTimeStamps(time, CurrentSelections[i].lastTimeChanged)
  1158. == EARLIER)
  1159. return Success;
  1160. if (CurrentSelections[i].client &&
  1161. (!pWin || (CurrentSelections[i].client != client)))
  1162. {
  1163. event.u.u.type = SelectionClear;
  1164. event.u.selectionClear.time = time.milliseconds;
  1165. event.u.selectionClear.window = CurrentSelections[i].window;
  1166. event.u.selectionClear.atom = CurrentSelections[i].selection;
  1167. (void) TryClientEvents (CurrentSelections[i].client, &event, 1,
  1168. NoEventMask, NoEventMask /* CantBeFiltered */,
  1169. NullGrab);
  1170. }
  1171. }
  1172. else
  1173. {
  1174. /*
  1175. * It doesn't exist, so add it...
  1176. */
  1177. Selection *newsels;
  1178. if (i == 0)
  1179. newsels = (Selection *)xalloc(sizeof(Selection));
  1180. else
  1181. newsels = (Selection *)xrealloc(CurrentSelections,
  1182. (NumCurrentSelections + 1) * sizeof(Selection));
  1183. if (!newsels)
  1184. return BadAlloc;
  1185. NumCurrentSelections++;
  1186. CurrentSelections = newsels;
  1187. CurrentSelections[i].selection = stuff->selection;
  1188. }
  1189. CurrentSelections[i].lastTimeChanged = time;
  1190. CurrentSelections[i].window = stuff->window;
  1191. CurrentSelections[i].pWin = pWin;
  1192. CurrentSelections[i].client = (pWin ? client : NullClient);
  1193. if (SelectionCallback)
  1194. {
  1195. SelectionInfoRec info;
  1196. info.selection = &CurrentSelections[i];
  1197. info.kind= SelectionSetOwner;
  1198. CallCallbacks(&SelectionCallback, &info);
  1199. }
  1200. #ifdef NXAGENT_CLIPBOARD
  1201. if ((CurrentSelections[i].pWin != NULL) &&
  1202. (nxagentOption(Clipboard) != ClipboardNone) &&
  1203. ((CurrentSelections[i].selection == XA_PRIMARY) ||
  1204. (CurrentSelections[i].selection == MakeAtom("CLIPBOARD", 9, 0))))
  1205. {
  1206. nxagentSetSelectionOwner(&CurrentSelections[i]);
  1207. }
  1208. #endif
  1209. return (client->noClientException);
  1210. }
  1211. else
  1212. {
  1213. client->errorValue = stuff->selection;
  1214. return (BadAtom);
  1215. }
  1216. }
  1217. int
  1218. ProcGetSelectionOwner(register ClientPtr client)
  1219. {
  1220. REQUEST(xResourceReq);
  1221. REQUEST_SIZE_MATCH(xResourceReq);
  1222. if (ValidAtom(stuff->id))
  1223. {
  1224. int i;
  1225. xGetSelectionOwnerReply reply;
  1226. i = 0;
  1227. while ((i < NumCurrentSelections) &&
  1228. CurrentSelections[i].selection != stuff->id) i++;
  1229. reply.type = X_Reply;
  1230. reply.length = 0;
  1231. reply.sequenceNumber = client->sequence;
  1232. if (i < NumCurrentSelections)
  1233. reply.owner = CurrentSelections[i].window;
  1234. else
  1235. reply.owner = None;
  1236. WriteReplyToClient(client, sizeof(xGetSelectionOwnerReply), &reply);
  1237. return(client->noClientException);
  1238. }
  1239. else
  1240. {
  1241. client->errorValue = stuff->id;
  1242. return (BadAtom);
  1243. }
  1244. }
  1245. int
  1246. ProcConvertSelection(register ClientPtr client)
  1247. {
  1248. Bool paramsOkay;
  1249. xEvent event;
  1250. WindowPtr pWin;
  1251. REQUEST(xConvertSelectionReq);
  1252. REQUEST_SIZE_MATCH(xConvertSelectionReq);
  1253. pWin = (WindowPtr)SecurityLookupWindow(stuff->requestor, client,
  1254. SecurityReadAccess);
  1255. if (!pWin)
  1256. return(BadWindow);
  1257. #ifdef NXAGENT_CLIPBOARD
  1258. if (((stuff->selection == XA_PRIMARY) ||
  1259. (stuff->selection == MakeAtom("CLIPBOARD", 9, 0))) &&
  1260. nxagentOption(Clipboard) != ClipboardNone)
  1261. {
  1262. int i = 0;
  1263. while ((i < NumCurrentSelections) &&
  1264. CurrentSelections[i].selection != stuff->selection) i++;
  1265. if ((i < NumCurrentSelections) && (CurrentSelections[i].window != None))
  1266. {
  1267. if (nxagentConvertSelection(client, pWin, stuff->selection, stuff->requestor,
  1268. stuff->property, stuff->target, stuff->time))
  1269. {
  1270. return (client->noClientException);
  1271. }
  1272. }
  1273. }
  1274. #endif
  1275. paramsOkay = (ValidAtom(stuff->selection) && ValidAtom(stuff->target));
  1276. if (stuff->property != None)
  1277. paramsOkay &= ValidAtom(stuff->property);
  1278. if (paramsOkay)
  1279. {
  1280. int i;
  1281. i = 0;
  1282. while ((i < NumCurrentSelections) &&
  1283. CurrentSelections[i].selection != stuff->selection) i++;
  1284. if ((i < NumCurrentSelections) &&
  1285. (CurrentSelections[i].window != None) && (CurrentSelections[i].client != NullClient)
  1286. #ifdef XCSECURITY
  1287. && (!client->CheckAccess ||
  1288. (* client->CheckAccess)(client, CurrentSelections[i].window,
  1289. RT_WINDOW, SecurityReadAccess,
  1290. CurrentSelections[i].pWin))
  1291. #endif
  1292. )
  1293. {
  1294. event.u.u.type = SelectionRequest;
  1295. event.u.selectionRequest.time = stuff->time;
  1296. event.u.selectionRequest.owner =
  1297. CurrentSelections[i].window;
  1298. event.u.selectionRequest.requestor = stuff->requestor;
  1299. event.u.selectionRequest.selection = stuff->selection;
  1300. event.u.selectionRequest.target = stuff->target;
  1301. event.u.selectionRequest.property = stuff->property;
  1302. if (TryClientEvents(
  1303. CurrentSelections[i].client, &event, 1, NoEventMask,
  1304. NoEventMask /* CantBeFiltered */, NullGrab))
  1305. return (client->noClientException);
  1306. }
  1307. event.u.u.type = SelectionNotify;
  1308. event.u.selectionNotify.time = stuff->time;
  1309. event.u.selectionNotify.requestor = stuff->requestor;
  1310. event.u.selectionNotify.selection = stuff->selection;
  1311. event.u.selectionNotify.target = stuff->target;
  1312. event.u.selectionNotify.property = None;
  1313. (void) TryClientEvents(client, &event, 1, NoEventMask,
  1314. NoEventMask /* CantBeFiltered */, NullGrab);
  1315. return (client->noClientException);
  1316. }
  1317. else
  1318. {
  1319. client->errorValue = stuff->property;
  1320. return (BadAtom);
  1321. }
  1322. }
  1323. int
  1324. ProcGrabServer(register ClientPtr client)
  1325. {
  1326. REQUEST_SIZE_MATCH(xReq);
  1327. if (grabState != GrabNone && client != grabClient)
  1328. {
  1329. ResetCurrentRequest(client);
  1330. client->sequence--;
  1331. BITSET(grabWaiters, client->index);
  1332. IgnoreClient(client);
  1333. return(client->noClientException);
  1334. }
  1335. OnlyListenToOneClient(client);
  1336. grabState = GrabKickout;
  1337. grabClient = client;
  1338. if (ServerGrabCallback)
  1339. {
  1340. ServerGrabInfoRec grabinfo;
  1341. grabinfo.client = client;
  1342. grabinfo.grabstate = SERVER_GRABBED;
  1343. CallCallbacks(&ServerGrabCallback, (pointer)&grabinfo);
  1344. }
  1345. return(client->noClientException);
  1346. }
  1347. static void
  1348. UngrabServer(ClientPtr client)
  1349. {
  1350. int i;
  1351. grabState = GrabNone;
  1352. ListenToAllClients();
  1353. for (i = mskcnt; --i >= 0 && !grabWaiters[i]; )
  1354. ;
  1355. if (i >= 0)
  1356. {
  1357. i <<= 5;
  1358. while (!GETBIT(grabWaiters, i))
  1359. i++;
  1360. BITCLEAR(grabWaiters, i);
  1361. AttendClient(clients[i]);
  1362. }
  1363. if (ServerGrabCallback)
  1364. {
  1365. ServerGrabInfoRec grabinfo;
  1366. grabinfo.client = client;
  1367. grabinfo.grabstate = SERVER_UNGRABBED;
  1368. CallCallbacks(&ServerGrabCallback, (pointer)&grabinfo);
  1369. }
  1370. }
  1371. int
  1372. ProcUngrabServer(register ClientPtr client)
  1373. {
  1374. REQUEST_SIZE_MATCH(xReq);
  1375. UngrabServer(client);
  1376. return(client->noClientException);
  1377. }
  1378. int
  1379. ProcTranslateCoords(register ClientPtr client)
  1380. {
  1381. REQUEST(xTranslateCoordsReq);
  1382. register WindowPtr pWin, pDst;
  1383. xTranslateCoordsReply rep;
  1384. REQUEST_SIZE_MATCH(xTranslateCoordsReq);
  1385. pWin = (WindowPtr)SecurityLookupWindow(stuff->srcWid, client,
  1386. SecurityReadAccess);
  1387. if (!pWin)
  1388. return(BadWindow);
  1389. pDst = (WindowPtr)SecurityLookupWindow(stuff->dstWid, client,
  1390. SecurityReadAccess);
  1391. if (!pDst)
  1392. return(BadWindow);
  1393. rep.type = X_Reply;
  1394. rep.length = 0;
  1395. rep.sequenceNumber = client->sequence;
  1396. if (!SAME_SCREENS(pWin->drawable, pDst->drawable))
  1397. {
  1398. rep.sameScreen = xFalse;
  1399. rep.child = None;
  1400. rep.dstX = rep.dstY = 0;
  1401. }
  1402. else
  1403. {
  1404. INT16 x, y;
  1405. rep.sameScreen = xTrue;
  1406. rep.child = None;
  1407. /* computing absolute coordinates -- adjust to destination later */
  1408. x = pWin->drawable.x + stuff->srcX;
  1409. y = pWin->drawable.y + stuff->srcY;
  1410. pWin = pDst->firstChild;
  1411. while (pWin)
  1412. {
  1413. #ifdef SHAPE
  1414. BoxRec box;
  1415. #endif
  1416. if ((pWin->mapped) &&
  1417. (x >= pWin->drawable.x - wBorderWidth (pWin)) &&
  1418. (x < pWin->drawable.x + (int)pWin->drawable.width +
  1419. wBorderWidth (pWin)) &&
  1420. (y >= pWin->drawable.y - wBorderWidth (pWin)) &&
  1421. (y < pWin->drawable.y + (int)pWin->drawable.height +
  1422. wBorderWidth (pWin))
  1423. #ifdef SHAPE
  1424. /* When a window is shaped, a further check
  1425. * is made to see if the point is inside
  1426. * borderSize
  1427. */
  1428. && (!wBoundingShape(pWin) ||
  1429. POINT_IN_REGION(pWin->drawable.pScreen,
  1430. &pWin->borderSize, x, y, &box))
  1431. && (!wInputShape(pWin) ||
  1432. POINT_IN_REGION(pWin->drawable.pScreen,
  1433. wInputShape(pWin),
  1434. x - pWin->drawable.x,
  1435. y - pWin->drawable.y, &box))
  1436. #endif
  1437. )
  1438. {
  1439. rep.child = pWin->drawable.id;
  1440. pWin = (WindowPtr) NULL;
  1441. }
  1442. else
  1443. pWin = pWin->nextSib;
  1444. }
  1445. /* adjust to destination coordinates */
  1446. rep.dstX = x - pDst->drawable.x;
  1447. rep.dstY = y - pDst->drawable.y;
  1448. }
  1449. WriteReplyToClient(client, sizeof(xTranslateCoordsReply), &rep);
  1450. return(client->noClientException);
  1451. }
  1452. int
  1453. ProcOpenFont(register ClientPtr client)
  1454. {
  1455. int err;
  1456. char fontReq[256];
  1457. REQUEST(xOpenFontReq);
  1458. REQUEST_FIXED_SIZE(xOpenFontReq, stuff->nbytes);
  1459. client->errorValue = stuff->fid;
  1460. LEGAL_NEW_RESOURCE(stuff->fid, client);
  1461. memcpy(fontReq,(char *)&stuff[1],(stuff->nbytes<256)?stuff->nbytes:255);
  1462. fontReq[stuff->nbytes]=0;
  1463. if (strchr(fontReq,'*') || strchr(fontReq,'?'))
  1464. {
  1465. extern int nxOpenFont(ClientPtr, XID, Mask, unsigned, char*);
  1466. #ifdef NXAGENT_FONTMATCH_DEBUG
  1467. fprintf(stderr, "Dispatch: ProcOpenFont try to find a common font with font pattern=%s\n",fontReq);
  1468. #endif
  1469. nxagentListRemoteFonts(fontReq, nxagentMaxFontNames);
  1470. err = nxOpenFont(client, stuff->fid, (Mask) 0,
  1471. stuff->nbytes, (char *)&stuff[1]);
  1472. }
  1473. else
  1474. err = OpenFont(client, stuff->fid, (Mask) 0,
  1475. stuff->nbytes, (char *)&stuff[1]);
  1476. if (err == Success)
  1477. {
  1478. return(client->noClientException);
  1479. }
  1480. else
  1481. return err;
  1482. }
  1483. int
  1484. ProcCloseFont(register ClientPtr client)
  1485. {
  1486. FontPtr pFont;
  1487. REQUEST(xResourceReq);
  1488. REQUEST_SIZE_MATCH(xResourceReq);
  1489. pFont = (FontPtr)SecurityLookupIDByType(client, stuff->id, RT_FONT,
  1490. SecurityDestroyAccess);
  1491. if (pFont != (FontPtr)NULL)
  1492. {
  1493. #ifdef NXAGENT_SERVER
  1494. /*
  1495. * When a client closes a font the resource
  1496. * should not be lost if the reference counter
  1497. * is not 0, otherwise the server will not be
  1498. * able to find this font looping through the
  1499. * resources.
  1500. */
  1501. if (pFont -> refcnt > 0)
  1502. {
  1503. if (nxagentFindClientResource(serverClient -> index, RT_NX_FONT, pFont) == 0)
  1504. {
  1505. #ifdef TEST
  1506. fprintf(stderr, "ProcCloseFont: Switching resource for font at [%p].\n",
  1507. (void *) pFont);
  1508. #endif
  1509. nxagentFontPriv(pFont) -> mirrorID = FakeClientID(serverClient -> index);
  1510. AddResource(nxagentFontPriv(pFont) -> mirrorID, RT_NX_FONT, pFont);
  1511. }
  1512. #ifdef TEST
  1513. else
  1514. {
  1515. fprintf(stderr, "ProcCloseFont: Found duplicated font at [%p], "
  1516. "resource switching skipped.\n", (void *) pFont);
  1517. }
  1518. #endif
  1519. }
  1520. #endif
  1521. FreeResource(stuff->id, RT_NONE);
  1522. return(client->noClientException);
  1523. }
  1524. else
  1525. {
  1526. client->errorValue = stuff->id;
  1527. return (BadFont);
  1528. }
  1529. }
  1530. int
  1531. ProcQueryFont(register ClientPtr client)
  1532. {
  1533. xQueryFontReply *reply;
  1534. FontPtr pFont;
  1535. register GC *pGC;
  1536. REQUEST(xResourceReq);
  1537. REQUEST_SIZE_MATCH(xResourceReq);
  1538. client->errorValue = stuff->id; /* EITHER font or gc */
  1539. pFont = NULL;
  1540. pFont = (FontPtr)SecurityLookupIDByType(client, stuff->id, RT_FONT,
  1541. SecurityReadAccess);
  1542. if (!pFont)
  1543. {
  1544. /* can't use VERIFY_GC because it might return BadGC */
  1545. pGC = (GC *) SecurityLookupIDByType(client, stuff->id, RT_GC,
  1546. SecurityReadAccess);
  1547. if (!pGC)
  1548. {
  1549. client->errorValue = stuff->id;
  1550. return(BadFont); /* procotol spec says only error is BadFont */
  1551. }
  1552. pFont = pGC->font;
  1553. }
  1554. /* test
  1555. {
  1556. Atom name_atom, value_atom;
  1557. int nprops;
  1558. FontPropPtr props;
  1559. int i;
  1560. char *name;
  1561. name_atom = MakeAtom("FONT", 4, True);
  1562. value_atom = 0L;
  1563. nprops = pFont->info.nprops;
  1564. props = pFont->info.props;
  1565. for (i = 0; i < nprops; i++)
  1566. if (props[i].name == name_atom) {
  1567. value_atom = props[i].value;
  1568. break;
  1569. }
  1570. if (!value_atom) return (BadFont);
  1571. name = (char *)NameForAtom(value_atom);
  1572. fprintf(stderr, "QueryFont: font name [%s]\n",name);
  1573. }
  1574. end test */
  1575. {
  1576. xCharInfo *pmax = FONTINKMAX(pFont);
  1577. xCharInfo *pmin = FONTINKMIN(pFont);
  1578. int nprotoxcistructs;
  1579. int rlength;
  1580. nprotoxcistructs = (
  1581. pmax->rightSideBearing == pmin->rightSideBearing &&
  1582. pmax->leftSideBearing == pmin->leftSideBearing &&
  1583. pmax->descent == pmin->descent &&
  1584. pmax->ascent == pmin->ascent &&
  1585. pmax->characterWidth == pmin->characterWidth) ?
  1586. 0 : N2dChars(pFont);
  1587. rlength = sizeof(xQueryFontReply) +
  1588. FONTINFONPROPS(FONTCHARSET(pFont)) * sizeof(xFontProp) +
  1589. nprotoxcistructs * sizeof(xCharInfo);
  1590. reply = NULL;
  1591. reply = (xQueryFontReply *)ALLOCATE_LOCAL(rlength);
  1592. if(!reply)
  1593. {
  1594. return(BadAlloc);
  1595. }
  1596. reply->type = X_Reply;
  1597. reply->length = (rlength - sizeof(xGenericReply)) >> 2;
  1598. reply->sequenceNumber = client->sequence;
  1599. QueryFont( pFont, reply, nprotoxcistructs);
  1600. WriteReplyToClient(client, rlength, reply);
  1601. DEALLOCATE_LOCAL(reply);
  1602. return(client->noClientException);
  1603. }
  1604. }
  1605. int
  1606. ProcQueryTextExtents(register ClientPtr client)
  1607. {
  1608. REQUEST(xQueryTextExtentsReq);
  1609. xQueryTextExtentsReply reply;
  1610. FontPtr pFont;
  1611. GC *pGC;
  1612. ExtentInfoRec info;
  1613. unsigned long length;
  1614. REQUEST_AT_LEAST_SIZE(xQueryTextExtentsReq);
  1615. pFont = (FontPtr)SecurityLookupIDByType(client, stuff->fid, RT_FONT,
  1616. SecurityReadAccess);
  1617. if (!pFont)
  1618. {
  1619. pGC = (GC *)SecurityLookupIDByType(client, stuff->fid, RT_GC,
  1620. SecurityReadAccess);
  1621. if (!pGC)
  1622. {
  1623. client->errorValue = stuff->fid;
  1624. return(BadFont);
  1625. }
  1626. pFont = pGC->font;
  1627. }
  1628. length = client->req_len - (sizeof(xQueryTextExtentsReq) >> 2);
  1629. length = length << 1;
  1630. if (stuff->oddLength)
  1631. {
  1632. if (length == 0)
  1633. return(BadLength);
  1634. length--;
  1635. }
  1636. if (!QueryTextExtents(pFont, length, (unsigned char *)&stuff[1], &info))
  1637. return(BadAlloc);
  1638. reply.type = X_Reply;
  1639. reply.length = 0;
  1640. reply.sequenceNumber = client->sequence;
  1641. reply.drawDirection = info.drawDirection;
  1642. reply.fontAscent = info.fontAscent;
  1643. reply.fontDescent = info.fontDescent;
  1644. reply.overallAscent = info.overallAscent;
  1645. reply.overallDescent = info.overallDescent;
  1646. reply.overallWidth = info.overallWidth;
  1647. reply.overallLeft = info.overallLeft;
  1648. reply.overallRight = info.overallRight;
  1649. WriteReplyToClient(client, sizeof(xQueryTextExtentsReply), &reply);
  1650. return(client->noClientException);
  1651. }
  1652. int
  1653. ProcListFonts(register ClientPtr client)
  1654. {
  1655. char tmp[256];
  1656. REQUEST(xListFontsReq);
  1657. REQUEST_FIXED_SIZE(xListFontsReq, stuff->nbytes);
  1658. memcpy(tmp,(unsigned char *) &stuff[1],(stuff->nbytes<256)?stuff->nbytes:255);
  1659. tmp[stuff->nbytes]=0;
  1660. #ifdef NXAGENT_FONTMATCH_DEBUG
  1661. fprintf(stderr, "Dispatch: ListFont request with pattern %s max_names=%d\n",tmp,stuff->maxNames);
  1662. #endif
  1663. nxagentListRemoteFonts(tmp, stuff -> maxNames < nxagentMaxFontNames ? nxagentMaxFontNames : stuff->maxNames);
  1664. return ListFonts(client, (unsigned char *) &stuff[1], stuff->nbytes,
  1665. stuff->maxNames);
  1666. }
  1667. int
  1668. ProcListFontsWithInfo(register ClientPtr client)
  1669. {
  1670. char tmp[256];
  1671. REQUEST(xListFontsWithInfoReq);
  1672. REQUEST_FIXED_SIZE(xListFontsWithInfoReq, stuff->nbytes);
  1673. memcpy(tmp,(unsigned char *) &stuff[1],(stuff->nbytes<256)?stuff->nbytes:255);
  1674. tmp[stuff->nbytes]=0;
  1675. #ifdef NXAGENT_FONTMATCH_DEBUG
  1676. fprintf(stderr, "Dispatch: ListFont with info request with pattern %s max_names=%d\n",tmp,stuff->maxNames);
  1677. #endif
  1678. nxagentListRemoteFonts(tmp, stuff -> maxNames < nxagentMaxFontNames ? nxagentMaxFontNames :stuff->maxNames);
  1679. return StartListFontsWithInfo(client, stuff->nbytes,
  1680. (unsigned char *) &stuff[1], stuff->maxNames);
  1681. }
  1682. /**
  1683. *
  1684. * \param value must conform to DeleteType
  1685. */
  1686. int
  1687. dixDestroyPixmap(pointer value, XID pid)
  1688. {
  1689. PixmapPtr pPixmap = (PixmapPtr)value;
  1690. return (*pPixmap->drawable.pScreen->DestroyPixmap)(pPixmap);
  1691. }
  1692. int
  1693. ProcCreatePixmap(register ClientPtr client)
  1694. {
  1695. PixmapPtr pMap;
  1696. register DrawablePtr pDraw;
  1697. REQUEST(xCreatePixmapReq);
  1698. DepthPtr pDepth;
  1699. register int i;
  1700. REQUEST_SIZE_MATCH(xCreatePixmapReq);
  1701. client->errorValue = stuff->pid;
  1702. LEGAL_NEW_RESOURCE(stuff->pid, client);
  1703. SECURITY_VERIFY_GEOMETRABLE (pDraw, stuff->drawable, client,
  1704. SecurityReadAccess);
  1705. if (!stuff->width || !stuff->height)
  1706. {
  1707. client->errorValue = 0;
  1708. return BadValue;
  1709. }
  1710. if (stuff->width > 32767 || stuff->height > 32767)
  1711. {
  1712. /* It is allowed to try and allocate a pixmap which is larger than
  1713. * 32767 in either dimension. However, all of the framebuffer code
  1714. * is buggy and does not reliably draw to such big pixmaps, basically
  1715. * because the Region data structure operates with signed shorts
  1716. * for the rectangles in it.
  1717. *
  1718. * Furthermore, several places in the X server computes the
  1719. * size in bytes of the pixmap and tries to store it in an
  1720. * integer. This integer can overflow and cause the allocated size
  1721. * to be much smaller.
  1722. *
  1723. * So, such big pixmaps are rejected here with a BadAlloc
  1724. */
  1725. return BadAlloc;
  1726. }
  1727. if (stuff->depth != 1)
  1728. {
  1729. pDepth = pDraw->pScreen->allowedDepths;
  1730. for (i=0; i<pDraw->pScreen->numDepths; i++, pDepth++)
  1731. if (pDepth->depth == stuff->depth)
  1732. goto CreatePmap;
  1733. client->errorValue = stuff->depth;
  1734. return BadValue;
  1735. }
  1736. CreatePmap:
  1737. pMap = (PixmapPtr)(*pDraw->pScreen->CreatePixmap)
  1738. (pDraw->pScreen, stuff->width,
  1739. stuff->height, stuff->depth);
  1740. if (pMap)
  1741. {
  1742. pMap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  1743. pMap->drawable.id = stuff->pid;
  1744. if (AddResource(stuff->pid, RT_PIXMAP, (pointer)pMap))
  1745. return(client->noClientException);
  1746. }
  1747. return (BadAlloc);
  1748. }
  1749. int
  1750. ProcFreePixmap(register ClientPtr client)
  1751. {
  1752. PixmapPtr pMap;
  1753. REQUEST(xResourceReq);
  1754. REQUEST_SIZE_MATCH(xResourceReq);
  1755. pMap = (PixmapPtr)SecurityLookupIDByType(client, stuff->id, RT_PIXMAP,
  1756. SecurityDestroyAccess);
  1757. if (pMap)
  1758. {
  1759. #ifdef NXAGENT_SERVER
  1760. /*
  1761. * When a client releases a pixmap the resource
  1762. * should not be lost if the reference counter
  1763. * is not 0, otherwise the server will not be
  1764. * able to find this pixmap looping through the
  1765. * resources.
  1766. */
  1767. if (pMap -> refcnt > 0)
  1768. {
  1769. if (nxagentFindClientResource(serverClient -> index, RT_NX_PIXMAP, pMap) == 0)
  1770. {
  1771. #ifdef TEST
  1772. fprintf(stderr, "ProcFreePixmap: Switching resource for pixmap at [%p].\n",
  1773. (void *) pMap);
  1774. #endif
  1775. nxagentPixmapPriv(pMap) -> mid = FakeClientID(serverClient -> index);
  1776. AddResource(nxagentPixmapPriv(pMap) -> mid, RT_NX_PIXMAP, pMap);
  1777. }
  1778. #ifdef TEST
  1779. else
  1780. {
  1781. fprintf(stderr, "ProcFreePixmap: Found duplicated pixmap at [%p], "
  1782. "resource switching skipped.\n", (void *) pMap);
  1783. }
  1784. #endif
  1785. }
  1786. #endif
  1787. FreeResource(stuff->id, RT_NONE);
  1788. return(client->noClientException);
  1789. }
  1790. else
  1791. {
  1792. client->errorValue = stuff->id;
  1793. return (BadPixmap);
  1794. }
  1795. }
  1796. int
  1797. ProcCreateGC(register ClientPtr client)
  1798. {
  1799. int error;
  1800. GC *pGC;
  1801. register DrawablePtr pDraw;
  1802. unsigned len;
  1803. REQUEST(xCreateGCReq);
  1804. REQUEST_AT_LEAST_SIZE(xCreateGCReq);
  1805. client->errorValue = stuff->gc;
  1806. LEGAL_NEW_RESOURCE(stuff->gc, client);
  1807. SECURITY_VERIFY_DRAWABLE (pDraw, stuff->drawable, client,
  1808. SecurityReadAccess);
  1809. len = client->req_len - (sizeof(xCreateGCReq) >> 2);
  1810. if (len != Ones(stuff->mask))
  1811. return BadLength;
  1812. pGC = (GC *)CreateGC(pDraw, stuff->mask,
  1813. (XID *) &stuff[1], &error);
  1814. if (error != Success)
  1815. return error;
  1816. if (!AddResource(stuff->gc, RT_GC, (pointer)pGC))
  1817. return (BadAlloc);
  1818. return(client->noClientException);
  1819. }
  1820. int
  1821. ProcChangeGC(register ClientPtr client)
  1822. {
  1823. GC *pGC;
  1824. REQUEST(xChangeGCReq);
  1825. int result;
  1826. unsigned len;
  1827. REQUEST_AT_LEAST_SIZE(xChangeGCReq);
  1828. SECURITY_VERIFY_GC(pGC, stuff->gc, client, SecurityWriteAccess);
  1829. len = client->req_len - (sizeof(xChangeGCReq) >> 2);
  1830. if (len != Ones(stuff->mask))
  1831. return BadLength;
  1832. result = dixChangeGC(client, pGC, stuff->mask, (CARD32 *) &stuff[1], 0);
  1833. if (client->noClientException != Success)
  1834. return(client->noClientException);
  1835. else
  1836. {
  1837. client->errorValue = clientErrorValue;
  1838. return(result);
  1839. }
  1840. }
  1841. int
  1842. ProcCopyGC(register ClientPtr client)
  1843. {
  1844. register GC *dstGC;
  1845. register GC *pGC;
  1846. int result;
  1847. REQUEST(xCopyGCReq);
  1848. REQUEST_SIZE_MATCH(xCopyGCReq);
  1849. SECURITY_VERIFY_GC( pGC, stuff->srcGC, client, SecurityReadAccess);
  1850. SECURITY_VERIFY_GC( dstGC, stuff->dstGC, client, SecurityWriteAccess);
  1851. if ((dstGC->pScreen != pGC->pScreen) || (dstGC->depth != pGC->depth))
  1852. return (BadMatch);
  1853. result = CopyGC(pGC, dstGC, stuff->mask);
  1854. if (client->noClientException != Success)
  1855. return(client->noClientException);
  1856. else
  1857. {
  1858. client->errorValue = clientErrorValue;
  1859. return(result);
  1860. }
  1861. }
  1862. int
  1863. ProcSetDashes(register ClientPtr client)
  1864. {
  1865. register GC *pGC;
  1866. int result;
  1867. REQUEST(xSetDashesReq);
  1868. REQUEST_FIXED_SIZE(xSetDashesReq, stuff->nDashes);
  1869. if (stuff->nDashes == 0)
  1870. {
  1871. client->errorValue = 0;
  1872. return BadValue;
  1873. }
  1874. SECURITY_VERIFY_GC(pGC,stuff->gc, client, SecurityWriteAccess);
  1875. result = SetDashes(pGC, stuff->dashOffset, stuff->nDashes,
  1876. (unsigned char *)&stuff[1]);
  1877. if (client->noClientException != Success)
  1878. return(client->noClientException);
  1879. else
  1880. {
  1881. client->errorValue = clientErrorValue;
  1882. return(result);
  1883. }
  1884. }
  1885. int
  1886. ProcSetClipRectangles(register ClientPtr client)
  1887. {
  1888. int nr;
  1889. int result;
  1890. register GC *pGC;
  1891. REQUEST(xSetClipRectanglesReq);
  1892. REQUEST_AT_LEAST_SIZE(xSetClipRectanglesReq);
  1893. if ((stuff->ordering != Unsorted) && (stuff->ordering != YSorted) &&
  1894. (stuff->ordering != YXSorted) && (stuff->ordering != YXBanded))
  1895. {
  1896. client->errorValue = stuff->ordering;
  1897. return BadValue;
  1898. }
  1899. SECURITY_VERIFY_GC(pGC,stuff->gc, client, SecurityWriteAccess);
  1900. nr = (client->req_len << 2) - sizeof(xSetClipRectanglesReq);
  1901. if (nr & 4)
  1902. return(BadLength);
  1903. nr >>= 3;
  1904. result = SetClipRects(pGC, stuff->xOrigin, stuff->yOrigin,
  1905. nr, (xRectangle *)&stuff[1], (int)stuff->ordering);
  1906. if (client->noClientException != Success)
  1907. return(client->noClientException);
  1908. else
  1909. return(result);
  1910. }
  1911. int
  1912. ProcFreeGC(register ClientPtr client)
  1913. {
  1914. register GC *pGC;
  1915. REQUEST(xResourceReq);
  1916. REQUEST_SIZE_MATCH(xResourceReq);
  1917. SECURITY_VERIFY_GC(pGC, stuff->id, client, SecurityDestroyAccess);
  1918. FreeResource(stuff->id, RT_NONE);
  1919. return(client->noClientException);
  1920. }
  1921. int
  1922. ProcClearToBackground(register ClientPtr client)
  1923. {
  1924. REQUEST(xClearAreaReq);
  1925. register WindowPtr pWin;
  1926. REQUEST_SIZE_MATCH(xClearAreaReq);
  1927. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  1928. SecurityWriteAccess);
  1929. if (!pWin)
  1930. return(BadWindow);
  1931. if (pWin->drawable.class == InputOnly)
  1932. {
  1933. client->errorValue = stuff->window;
  1934. return (BadMatch);
  1935. }
  1936. if ((stuff->exposures != xTrue) && (stuff->exposures != xFalse))
  1937. {
  1938. client->errorValue = stuff->exposures;
  1939. return(BadValue);
  1940. }
  1941. (*pWin->drawable.pScreen->ClearToBackground)(pWin, stuff->x, stuff->y,
  1942. stuff->width, stuff->height,
  1943. (Bool)stuff->exposures);
  1944. return(client->noClientException);
  1945. }
  1946. int
  1947. ProcCopyArea(register ClientPtr client)
  1948. {
  1949. register DrawablePtr pDst;
  1950. register DrawablePtr pSrc;
  1951. register GC *pGC;
  1952. REQUEST(xCopyAreaReq);
  1953. RegionPtr pRgn;
  1954. REQUEST_SIZE_MATCH(xCopyAreaReq);
  1955. VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pDst, pGC, client);
  1956. if (stuff->dstDrawable != stuff->srcDrawable)
  1957. {
  1958. SECURITY_VERIFY_DRAWABLE(pSrc, stuff->srcDrawable, client,
  1959. SecurityReadAccess);
  1960. if ((pDst->pScreen != pSrc->pScreen) || (pDst->depth != pSrc->depth))
  1961. {
  1962. client->errorValue = stuff->dstDrawable;
  1963. return (BadMatch);
  1964. }
  1965. }
  1966. else
  1967. pSrc = pDst;
  1968. SET_DBE_SRCBUF(pSrc, stuff->srcDrawable);
  1969. pRgn = (*pGC->ops->CopyArea)(pSrc, pDst, pGC, stuff->srcX, stuff->srcY,
  1970. stuff->width, stuff->height,
  1971. stuff->dstX, stuff->dstY);
  1972. if (pGC->graphicsExposures)
  1973. {
  1974. (*pDst->pScreen->SendGraphicsExpose)
  1975. (client, pRgn, stuff->dstDrawable, X_CopyArea, 0);
  1976. if (pRgn)
  1977. REGION_DESTROY(pDst->pScreen, pRgn);
  1978. }
  1979. return(client->noClientException);
  1980. }
  1981. int
  1982. ProcCopyPlane(register ClientPtr client)
  1983. {
  1984. register DrawablePtr psrcDraw, pdstDraw;
  1985. register GC *pGC;
  1986. REQUEST(xCopyPlaneReq);
  1987. RegionPtr pRgn;
  1988. REQUEST_SIZE_MATCH(xCopyPlaneReq);
  1989. VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pdstDraw, pGC, client);
  1990. if (stuff->dstDrawable != stuff->srcDrawable)
  1991. {
  1992. SECURITY_VERIFY_DRAWABLE(psrcDraw, stuff->srcDrawable, client,
  1993. SecurityReadAccess);
  1994. if (pdstDraw->pScreen != psrcDraw->pScreen)
  1995. {
  1996. client->errorValue = stuff->dstDrawable;
  1997. return (BadMatch);
  1998. }
  1999. }
  2000. else
  2001. psrcDraw = pdstDraw;
  2002. SET_DBE_SRCBUF(psrcDraw, stuff->srcDrawable);
  2003. /* Check to see if stuff->bitPlane has exactly ONE good bit set */
  2004. if(stuff->bitPlane == 0 || (stuff->bitPlane & (stuff->bitPlane - 1)) ||
  2005. (stuff->bitPlane > (1L << (psrcDraw->depth - 1))))
  2006. {
  2007. client->errorValue = stuff->bitPlane;
  2008. return(BadValue);
  2009. }
  2010. pRgn = (*pGC->ops->CopyPlane)(psrcDraw, pdstDraw, pGC, stuff->srcX, stuff->srcY,
  2011. stuff->width, stuff->height,
  2012. stuff->dstX, stuff->dstY, stuff->bitPlane);
  2013. if (pGC->graphicsExposures)
  2014. {
  2015. (*pdstDraw->pScreen->SendGraphicsExpose)
  2016. (client, pRgn, stuff->dstDrawable, X_CopyPlane, 0);
  2017. if (pRgn)
  2018. REGION_DESTROY(pdstDraw->pScreen, pRgn);
  2019. }
  2020. return(client->noClientException);
  2021. }
  2022. int
  2023. ProcPolyPoint(register ClientPtr client)
  2024. {
  2025. int npoint;
  2026. register GC *pGC;
  2027. register DrawablePtr pDraw;
  2028. REQUEST(xPolyPointReq);
  2029. REQUEST_AT_LEAST_SIZE(xPolyPointReq);
  2030. if ((stuff->coordMode != CoordModeOrigin) &&
  2031. (stuff->coordMode != CoordModePrevious))
  2032. {
  2033. client->errorValue = stuff->coordMode;
  2034. return BadValue;
  2035. }
  2036. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2037. npoint = ((client->req_len << 2) - sizeof(xPolyPointReq)) >> 2;
  2038. if (npoint)
  2039. {
  2040. (*pGC->ops->PolyPoint)(pDraw, pGC, stuff->coordMode, npoint,
  2041. (xPoint *) &stuff[1]);
  2042. }
  2043. return (client->noClientException);
  2044. }
  2045. int
  2046. ProcPolyLine(register ClientPtr client)
  2047. {
  2048. int npoint;
  2049. register GC *pGC;
  2050. register DrawablePtr pDraw;
  2051. REQUEST(xPolyLineReq);
  2052. REQUEST_AT_LEAST_SIZE(xPolyLineReq);
  2053. if ((stuff->coordMode != CoordModeOrigin) &&
  2054. (stuff->coordMode != CoordModePrevious))
  2055. {
  2056. client->errorValue = stuff->coordMode;
  2057. return BadValue;
  2058. }
  2059. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2060. npoint = ((client->req_len << 2) - sizeof(xPolyLineReq)) >> 2;
  2061. if (npoint > 1)
  2062. {
  2063. (*pGC->ops->Polylines)(pDraw, pGC, stuff->coordMode, npoint,
  2064. (DDXPointPtr) &stuff[1]);
  2065. }
  2066. return(client->noClientException);
  2067. }
  2068. int
  2069. ProcPolySegment(register ClientPtr client)
  2070. {
  2071. int nsegs;
  2072. register GC *pGC;
  2073. register DrawablePtr pDraw;
  2074. REQUEST(xPolySegmentReq);
  2075. REQUEST_AT_LEAST_SIZE(xPolySegmentReq);
  2076. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2077. nsegs = (client->req_len << 2) - sizeof(xPolySegmentReq);
  2078. if (nsegs & 4)
  2079. return(BadLength);
  2080. nsegs >>= 3;
  2081. if (nsegs)
  2082. {
  2083. (*pGC->ops->PolySegment)(pDraw, pGC, nsegs, (xSegment *) &stuff[1]);
  2084. }
  2085. return (client->noClientException);
  2086. }
  2087. int
  2088. ProcPolyRectangle (register ClientPtr client)
  2089. {
  2090. int nrects;
  2091. register GC *pGC;
  2092. register DrawablePtr pDraw;
  2093. REQUEST(xPolyRectangleReq);
  2094. REQUEST_AT_LEAST_SIZE(xPolyRectangleReq);
  2095. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2096. nrects = (client->req_len << 2) - sizeof(xPolyRectangleReq);
  2097. if (nrects & 4)
  2098. return(BadLength);
  2099. nrects >>= 3;
  2100. if (nrects)
  2101. {
  2102. (*pGC->ops->PolyRectangle)(pDraw, pGC,
  2103. nrects, (xRectangle *) &stuff[1]);
  2104. }
  2105. return(client->noClientException);
  2106. }
  2107. int
  2108. ProcPolyArc(register ClientPtr client)
  2109. {
  2110. int narcs;
  2111. register GC *pGC;
  2112. register DrawablePtr pDraw;
  2113. REQUEST(xPolyArcReq);
  2114. REQUEST_AT_LEAST_SIZE(xPolyArcReq);
  2115. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2116. narcs = (client->req_len << 2) - sizeof(xPolyArcReq);
  2117. if (narcs % sizeof(xArc))
  2118. return(BadLength);
  2119. narcs /= sizeof(xArc);
  2120. if (narcs)
  2121. {
  2122. (*pGC->ops->PolyArc)(pDraw, pGC, narcs, (xArc *) &stuff[1]);
  2123. }
  2124. return (client->noClientException);
  2125. }
  2126. int
  2127. ProcFillPoly(register ClientPtr client)
  2128. {
  2129. int things;
  2130. register GC *pGC;
  2131. register DrawablePtr pDraw;
  2132. REQUEST(xFillPolyReq);
  2133. REQUEST_AT_LEAST_SIZE(xFillPolyReq);
  2134. if ((stuff->shape != Complex) && (stuff->shape != Nonconvex) &&
  2135. (stuff->shape != Convex))
  2136. {
  2137. client->errorValue = stuff->shape;
  2138. return BadValue;
  2139. }
  2140. if ((stuff->coordMode != CoordModeOrigin) &&
  2141. (stuff->coordMode != CoordModePrevious))
  2142. {
  2143. client->errorValue = stuff->coordMode;
  2144. return BadValue;
  2145. }
  2146. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2147. things = ((client->req_len << 2) - sizeof(xFillPolyReq)) >> 2;
  2148. if (things)
  2149. {
  2150. (*pGC->ops->FillPolygon) (pDraw, pGC, stuff->shape,
  2151. stuff->coordMode, things,
  2152. (DDXPointPtr) &stuff[1]);
  2153. }
  2154. return(client->noClientException);
  2155. }
  2156. int
  2157. ProcPolyFillRectangle(register ClientPtr client)
  2158. {
  2159. int things;
  2160. register GC *pGC;
  2161. register DrawablePtr pDraw;
  2162. REQUEST(xPolyFillRectangleReq);
  2163. REQUEST_AT_LEAST_SIZE(xPolyFillRectangleReq);
  2164. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2165. things = (client->req_len << 2) - sizeof(xPolyFillRectangleReq);
  2166. if (things & 4)
  2167. return(BadLength);
  2168. things >>= 3;
  2169. if (things)
  2170. {
  2171. (*pGC->ops->PolyFillRect) (pDraw, pGC, things,
  2172. (xRectangle *) &stuff[1]);
  2173. }
  2174. return (client->noClientException);
  2175. }
  2176. int
  2177. ProcPolyFillArc(register ClientPtr client)
  2178. {
  2179. int narcs;
  2180. register GC *pGC;
  2181. register DrawablePtr pDraw;
  2182. REQUEST(xPolyFillArcReq);
  2183. REQUEST_AT_LEAST_SIZE(xPolyFillArcReq);
  2184. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2185. narcs = (client->req_len << 2) - sizeof(xPolyFillArcReq);
  2186. if (narcs % sizeof(xArc))
  2187. return(BadLength);
  2188. narcs /= sizeof(xArc);
  2189. if (narcs)
  2190. {
  2191. (*pGC->ops->PolyFillArc) (pDraw, pGC, narcs, (xArc *) &stuff[1]);
  2192. }
  2193. return (client->noClientException);
  2194. }
  2195. #ifdef MATCH_CLIENT_ENDIAN
  2196. int
  2197. ServerOrder (void)
  2198. {
  2199. int whichbyte = 1;
  2200. if (*((char *) &whichbyte))
  2201. return LSBFirst;
  2202. return MSBFirst;
  2203. }
  2204. #define ClientOrder(client) ((client)->swapped ? !ServerOrder() : ServerOrder())
  2205. void
  2206. ReformatImage (char *base, int nbytes, int bpp, int order)
  2207. {
  2208. switch (bpp) {
  2209. case 1: /* yuck */
  2210. if (BITMAP_BIT_ORDER != order)
  2211. BitOrderInvert ((unsigned char *) base, nbytes);
  2212. #if IMAGE_BYTE_ORDER != BITMAP_BIT_ORDER && BITMAP_SCANLINE_UNIT != 8
  2213. ReformatImage (base, nbytes, BITMAP_SCANLINE_UNIT, order);
  2214. #endif
  2215. break;
  2216. case 4:
  2217. break; /* yuck */
  2218. case 8:
  2219. break;
  2220. case 16:
  2221. if (IMAGE_BYTE_ORDER != order)
  2222. TwoByteSwap ((unsigned char *) base, nbytes);
  2223. break;
  2224. case 32:
  2225. if (IMAGE_BYTE_ORDER != order)
  2226. FourByteSwap ((unsigned char *) base, nbytes);
  2227. break;
  2228. }
  2229. }
  2230. #else
  2231. #define ReformatImage(b,n,bpp,o)
  2232. #endif
  2233. /* 64-bit server notes: the protocol restricts padding of images to
  2234. * 8-, 16-, or 32-bits. We would like to have 64-bits for the server
  2235. * to use internally. Removes need for internal alignment checking.
  2236. * All of the PutImage functions could be changed individually, but
  2237. * as currently written, they call other routines which require things
  2238. * to be 64-bit padded on scanlines, so we changed things here.
  2239. * If an image would be padded differently for 64- versus 32-, then
  2240. * copy each scanline to a 64-bit padded scanline.
  2241. * Also, we need to make sure that the image is aligned on a 64-bit
  2242. * boundary, even if the scanlines are padded to our satisfaction.
  2243. */
  2244. int
  2245. ProcPutImage(register ClientPtr client)
  2246. {
  2247. register GC *pGC;
  2248. register DrawablePtr pDraw;
  2249. long length; /* length of scanline server padded */
  2250. long lengthProto; /* length of scanline protocol padded */
  2251. char *tmpImage;
  2252. REQUEST(xPutImageReq);
  2253. REQUEST_AT_LEAST_SIZE(xPutImageReq);
  2254. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2255. if (stuff->format == XYBitmap)
  2256. {
  2257. if ((stuff->depth != 1) ||
  2258. (stuff->leftPad >= (unsigned int)screenInfo.bitmapScanlinePad))
  2259. return BadMatch;
  2260. length = BitmapBytePad(stuff->width + stuff->leftPad);
  2261. }
  2262. else if (stuff->format == XYPixmap)
  2263. {
  2264. if ((pDraw->depth != stuff->depth) ||
  2265. (stuff->leftPad >= (unsigned int)screenInfo.bitmapScanlinePad))
  2266. return BadMatch;
  2267. length = BitmapBytePad(stuff->width + stuff->leftPad);
  2268. length *= stuff->depth;
  2269. }
  2270. else if (stuff->format == ZPixmap)
  2271. {
  2272. if ((pDraw->depth != stuff->depth) || (stuff->leftPad != 0))
  2273. return BadMatch;
  2274. length = PixmapBytePad(stuff->width, stuff->depth);
  2275. }
  2276. else
  2277. {
  2278. client->errorValue = stuff->format;
  2279. return BadValue;
  2280. }
  2281. tmpImage = (char *)&stuff[1];
  2282. lengthProto = length;
  2283. if (((((lengthProto * stuff->height) + (unsigned)3) >> 2) +
  2284. (sizeof(xPutImageReq) >> 2)) != client->req_len)
  2285. return BadLength;
  2286. ReformatImage (tmpImage, lengthProto * stuff->height,
  2287. stuff->format == ZPixmap ? BitsPerPixel (stuff->depth) : 1,
  2288. ClientOrder(client));
  2289. (*pGC->ops->PutImage) (pDraw, pGC, stuff->depth, stuff->dstX, stuff->dstY,
  2290. stuff->width, stuff->height,
  2291. stuff->leftPad, stuff->format, tmpImage);
  2292. return (client->noClientException);
  2293. }
  2294. int
  2295. DoGetImage(register ClientPtr client, int format, Drawable drawable,
  2296. int x, int y, int width, int height,
  2297. Mask planemask, xGetImageReply **im_return)
  2298. {
  2299. register DrawablePtr pDraw;
  2300. int nlines, linesPerBuf;
  2301. register int linesDone;
  2302. long widthBytesLine, length;
  2303. Mask plane = 0;
  2304. char *pBuf;
  2305. xGetImageReply xgi;
  2306. #ifdef XCSECURITY
  2307. RegionPtr pVisibleRegion = NULL;
  2308. #endif
  2309. if ((format != XYPixmap) && (format != ZPixmap))
  2310. {
  2311. client->errorValue = format;
  2312. return(BadValue);
  2313. }
  2314. SECURITY_VERIFY_DRAWABLE(pDraw, drawable, client, SecurityReadAccess);
  2315. if(pDraw->type == DRAWABLE_WINDOW)
  2316. {
  2317. if( /* check for being viewable */
  2318. !((WindowPtr) pDraw)->realized ||
  2319. /* check for being on screen */
  2320. pDraw->x + x < 0 ||
  2321. pDraw->x + x + width > pDraw->pScreen->width ||
  2322. pDraw->y + y < 0 ||
  2323. pDraw->y + y + height > pDraw->pScreen->height ||
  2324. /* check for being inside of border */
  2325. x < - wBorderWidth((WindowPtr)pDraw) ||
  2326. x + width > wBorderWidth((WindowPtr)pDraw) + (int)pDraw->width ||
  2327. y < -wBorderWidth((WindowPtr)pDraw) ||
  2328. y + height > wBorderWidth ((WindowPtr)pDraw) + (int)pDraw->height
  2329. )
  2330. return(BadMatch);
  2331. xgi.visual = wVisual (((WindowPtr) pDraw));
  2332. }
  2333. else
  2334. {
  2335. if(x < 0 ||
  2336. x+width > (int)pDraw->width ||
  2337. y < 0 ||
  2338. y+height > (int)pDraw->height
  2339. )
  2340. return(BadMatch);
  2341. xgi.visual = None;
  2342. }
  2343. SET_DBE_SRCBUF(pDraw, drawable);
  2344. xgi.type = X_Reply;
  2345. xgi.sequenceNumber = client->sequence;
  2346. xgi.depth = pDraw->depth;
  2347. if(format == ZPixmap)
  2348. {
  2349. widthBytesLine = PixmapBytePad(width, pDraw->depth);
  2350. length = widthBytesLine * height;
  2351. }
  2352. else
  2353. {
  2354. widthBytesLine = BitmapBytePad(width);
  2355. plane = ((Mask)1) << (pDraw->depth - 1);
  2356. /* only planes asked for */
  2357. length = widthBytesLine * height *
  2358. Ones(planemask & (plane | (plane - 1)));
  2359. }
  2360. xgi.length = length;
  2361. if (im_return) {
  2362. pBuf = (char *)xalloc(sz_xGetImageReply + length);
  2363. if (!pBuf)
  2364. return (BadAlloc);
  2365. if (widthBytesLine == 0)
  2366. linesPerBuf = 0;
  2367. else
  2368. linesPerBuf = height;
  2369. *im_return = (xGetImageReply *)pBuf;
  2370. *(xGetImageReply *)pBuf = xgi;
  2371. pBuf += sz_xGetImageReply;
  2372. } else {
  2373. xgi.length = (xgi.length + 3) >> 2;
  2374. if (widthBytesLine == 0 || height == 0)
  2375. linesPerBuf = 0;
  2376. else if (widthBytesLine >= IMAGE_BUFSIZE)
  2377. linesPerBuf = 1;
  2378. else
  2379. {
  2380. linesPerBuf = IMAGE_BUFSIZE / widthBytesLine;
  2381. if (linesPerBuf > height)
  2382. linesPerBuf = height;
  2383. }
  2384. length = linesPerBuf * widthBytesLine;
  2385. if (linesPerBuf < height)
  2386. {
  2387. /* we have to make sure intermediate buffers don't need padding */
  2388. while ((linesPerBuf > 1) &&
  2389. (length & ((1L << LOG2_BYTES_PER_SCANLINE_PAD)-1)))
  2390. {
  2391. linesPerBuf--;
  2392. length -= widthBytesLine;
  2393. }
  2394. while (length & ((1L << LOG2_BYTES_PER_SCANLINE_PAD)-1))
  2395. {
  2396. linesPerBuf++;
  2397. length += widthBytesLine;
  2398. }
  2399. }
  2400. if(!(pBuf = (char *) ALLOCATE_LOCAL(length)))
  2401. return (BadAlloc);
  2402. WriteReplyToClient(client, sizeof (xGetImageReply), &xgi);
  2403. }
  2404. #ifdef XCSECURITY
  2405. if (client->trustLevel != XSecurityClientTrusted &&
  2406. pDraw->type == DRAWABLE_WINDOW)
  2407. {
  2408. pVisibleRegion = NotClippedByChildren((WindowPtr)pDraw);
  2409. if (pVisibleRegion)
  2410. {
  2411. REGION_TRANSLATE(pDraw->pScreen, pVisibleRegion, -pDraw->x, -pDraw->y);
  2412. }
  2413. }
  2414. #endif
  2415. if (linesPerBuf == 0)
  2416. {
  2417. /* nothing to do */
  2418. }
  2419. else if (format == ZPixmap)
  2420. {
  2421. linesDone = 0;
  2422. while (height - linesDone > 0)
  2423. {
  2424. nlines = min(linesPerBuf, height - linesDone);
  2425. (*pDraw->pScreen->GetImage) (pDraw,
  2426. x,
  2427. y + linesDone,
  2428. width,
  2429. nlines,
  2430. format,
  2431. planemask,
  2432. (pointer) pBuf);
  2433. #ifdef XCSECURITY
  2434. if (pVisibleRegion)
  2435. SecurityCensorImage(client, pVisibleRegion, widthBytesLine,
  2436. pDraw, x, y + linesDone, width,
  2437. nlines, format, pBuf);
  2438. #endif
  2439. /* Note that this is NOT a call to WriteSwappedDataToClient,
  2440. as we do NOT byte swap */
  2441. if (!im_return)
  2442. {
  2443. ReformatImage (pBuf, (int)(nlines * widthBytesLine),
  2444. BitsPerPixel (pDraw->depth),
  2445. ClientOrder(client));
  2446. /* Don't split me, gcc pukes when you do */
  2447. (void)WriteToClient(client,
  2448. (int)(nlines * widthBytesLine),
  2449. pBuf);
  2450. }
  2451. linesDone += nlines;
  2452. }
  2453. }
  2454. else /* XYPixmap */
  2455. {
  2456. for (; plane; plane >>= 1)
  2457. {
  2458. if (planemask & plane)
  2459. {
  2460. linesDone = 0;
  2461. while (height - linesDone > 0)
  2462. {
  2463. nlines = min(linesPerBuf, height - linesDone);
  2464. (*pDraw->pScreen->GetImage) (pDraw,
  2465. x,
  2466. y + linesDone,
  2467. width,
  2468. nlines,
  2469. format,
  2470. plane,
  2471. (pointer)pBuf);
  2472. #ifdef XCSECURITY
  2473. if (pVisibleRegion)
  2474. SecurityCensorImage(client, pVisibleRegion,
  2475. widthBytesLine,
  2476. pDraw, x, y + linesDone, width,
  2477. nlines, format, pBuf);
  2478. #endif
  2479. /* Note: NOT a call to WriteSwappedDataToClient,
  2480. as we do NOT byte swap */
  2481. if (im_return) {
  2482. pBuf += nlines * widthBytesLine;
  2483. } else {
  2484. ReformatImage (pBuf,
  2485. (int)(nlines * widthBytesLine),
  2486. 1,
  2487. ClientOrder (client));
  2488. /* Don't split me, gcc pukes when you do */
  2489. (void)WriteToClient(client,
  2490. (int)(nlines * widthBytesLine),
  2491. pBuf);
  2492. }
  2493. linesDone += nlines;
  2494. }
  2495. }
  2496. }
  2497. }
  2498. #ifdef XCSECURITY
  2499. if (pVisibleRegion)
  2500. REGION_DESTROY(pDraw->pScreen, pVisibleRegion);
  2501. #endif
  2502. if (!im_return)
  2503. DEALLOCATE_LOCAL(pBuf);
  2504. return (client->noClientException);
  2505. }
  2506. int
  2507. ProcGetImage(register ClientPtr client)
  2508. {
  2509. REQUEST(xGetImageReq);
  2510. REQUEST_SIZE_MATCH(xGetImageReq);
  2511. return DoGetImage(client, stuff->format, stuff->drawable,
  2512. stuff->x, stuff->y,
  2513. (int)stuff->width, (int)stuff->height,
  2514. stuff->planeMask, (xGetImageReply **)NULL);
  2515. }
  2516. int
  2517. ProcPolyText(register ClientPtr client)
  2518. {
  2519. int err;
  2520. REQUEST(xPolyTextReq);
  2521. DrawablePtr pDraw;
  2522. GC *pGC;
  2523. REQUEST_AT_LEAST_SIZE(xPolyTextReq);
  2524. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2525. err = PolyText(client,
  2526. pDraw,
  2527. pGC,
  2528. (unsigned char *)&stuff[1],
  2529. ((unsigned char *) stuff) + (client->req_len << 2),
  2530. stuff->x,
  2531. stuff->y,
  2532. stuff->reqType,
  2533. stuff->drawable);
  2534. if (err == Success)
  2535. {
  2536. return(client->noClientException);
  2537. }
  2538. else
  2539. return err;
  2540. }
  2541. int
  2542. ProcImageText8(register ClientPtr client)
  2543. {
  2544. int err;
  2545. register DrawablePtr pDraw;
  2546. register GC *pGC;
  2547. REQUEST(xImageTextReq);
  2548. REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars);
  2549. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2550. err = ImageText(client,
  2551. pDraw,
  2552. pGC,
  2553. stuff->nChars,
  2554. (unsigned char *)&stuff[1],
  2555. stuff->x,
  2556. stuff->y,
  2557. stuff->reqType,
  2558. stuff->drawable);
  2559. if (err == Success)
  2560. {
  2561. return(client->noClientException);
  2562. }
  2563. else
  2564. return err;
  2565. }
  2566. int
  2567. ProcImageText16(register ClientPtr client)
  2568. {
  2569. int err;
  2570. register DrawablePtr pDraw;
  2571. register GC *pGC;
  2572. REQUEST(xImageTextReq);
  2573. REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars << 1);
  2574. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2575. err = ImageText(client,
  2576. pDraw,
  2577. pGC,
  2578. stuff->nChars,
  2579. (unsigned char *)&stuff[1],
  2580. stuff->x,
  2581. stuff->y,
  2582. stuff->reqType,
  2583. stuff->drawable);
  2584. if (err == Success)
  2585. {
  2586. return(client->noClientException);
  2587. }
  2588. else
  2589. return err;
  2590. }
  2591. int
  2592. ProcCreateColormap(register ClientPtr client)
  2593. {
  2594. VisualPtr pVisual;
  2595. ColormapPtr pmap;
  2596. Colormap mid;
  2597. register WindowPtr pWin;
  2598. ScreenPtr pScreen;
  2599. REQUEST(xCreateColormapReq);
  2600. int i, result;
  2601. REQUEST_SIZE_MATCH(xCreateColormapReq);
  2602. if ((stuff->alloc != AllocNone) && (stuff->alloc != AllocAll))
  2603. {
  2604. client->errorValue = stuff->alloc;
  2605. return(BadValue);
  2606. }
  2607. mid = stuff->mid;
  2608. LEGAL_NEW_RESOURCE(mid, client);
  2609. pWin = (WindowPtr)SecurityLookupWindow(stuff->window, client,
  2610. SecurityReadAccess);
  2611. if (!pWin)
  2612. return(BadWindow);
  2613. pScreen = pWin->drawable.pScreen;
  2614. for (i = 0, pVisual = pScreen->visuals;
  2615. i < pScreen->numVisuals;
  2616. i++, pVisual++)
  2617. {
  2618. if (pVisual->vid != stuff->visual)
  2619. continue;
  2620. result = CreateColormap(mid, pScreen, pVisual, &pmap,
  2621. (int)stuff->alloc, client->index);
  2622. if (client->noClientException != Success)
  2623. return(client->noClientException);
  2624. else
  2625. return(result);
  2626. }
  2627. client->errorValue = stuff->visual;
  2628. return(BadValue);
  2629. }
  2630. int
  2631. ProcFreeColormap(register ClientPtr client)
  2632. {
  2633. ColormapPtr pmap;
  2634. REQUEST(xResourceReq);
  2635. REQUEST_SIZE_MATCH(xResourceReq);
  2636. pmap = (ColormapPtr )SecurityLookupIDByType(client, stuff->id, RT_COLORMAP,
  2637. SecurityDestroyAccess);
  2638. if (pmap)
  2639. {
  2640. /* Freeing a default colormap is a no-op */
  2641. if (!(pmap->flags & IsDefault))
  2642. FreeResource(stuff->id, RT_NONE);
  2643. return (client->noClientException);
  2644. }
  2645. else
  2646. {
  2647. client->errorValue = stuff->id;
  2648. return (BadColor);
  2649. }
  2650. }
  2651. int
  2652. ProcCopyColormapAndFree(register ClientPtr client)
  2653. {
  2654. Colormap mid;
  2655. ColormapPtr pSrcMap;
  2656. REQUEST(xCopyColormapAndFreeReq);
  2657. int result;
  2658. REQUEST_SIZE_MATCH(xCopyColormapAndFreeReq);
  2659. mid = stuff->mid;
  2660. LEGAL_NEW_RESOURCE(mid, client);
  2661. if( (pSrcMap = (ColormapPtr )SecurityLookupIDByType(client, stuff->srcCmap,
  2662. RT_COLORMAP, SecurityReadAccess|SecurityWriteAccess)) )
  2663. {
  2664. result = CopyColormapAndFree(mid, pSrcMap, client->index);
  2665. if (client->noClientException != Success)
  2666. return(client->noClientException);
  2667. else
  2668. return(result);
  2669. }
  2670. else
  2671. {
  2672. client->errorValue = stuff->srcCmap;
  2673. return(BadColor);
  2674. }
  2675. }
  2676. int
  2677. ProcInstallColormap(register ClientPtr client)
  2678. {
  2679. ColormapPtr pcmp;
  2680. REQUEST(xResourceReq);
  2681. REQUEST_SIZE_MATCH(xResourceReq);
  2682. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->id,
  2683. RT_COLORMAP, SecurityReadAccess);
  2684. if (pcmp)
  2685. {
  2686. (*(pcmp->pScreen->InstallColormap)) (pcmp);
  2687. return (client->noClientException);
  2688. }
  2689. else
  2690. {
  2691. client->errorValue = stuff->id;
  2692. return (BadColor);
  2693. }
  2694. }
  2695. int
  2696. ProcUninstallColormap(register ClientPtr client)
  2697. {
  2698. ColormapPtr pcmp;
  2699. REQUEST(xResourceReq);
  2700. REQUEST_SIZE_MATCH(xResourceReq);
  2701. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->id,
  2702. RT_COLORMAP, SecurityReadAccess);
  2703. if (pcmp)
  2704. {
  2705. if(pcmp->mid != pcmp->pScreen->defColormap)
  2706. (*(pcmp->pScreen->UninstallColormap)) (pcmp);
  2707. return (client->noClientException);
  2708. }
  2709. else
  2710. {
  2711. client->errorValue = stuff->id;
  2712. return (BadColor);
  2713. }
  2714. }
  2715. int
  2716. ProcListInstalledColormaps(register ClientPtr client)
  2717. {
  2718. xListInstalledColormapsReply *preply;
  2719. int nummaps;
  2720. WindowPtr pWin;
  2721. REQUEST(xResourceReq);
  2722. REQUEST_SIZE_MATCH(xResourceReq);
  2723. pWin = (WindowPtr)SecurityLookupWindow(stuff->id, client,
  2724. SecurityReadAccess);
  2725. if (!pWin)
  2726. return(BadWindow);
  2727. preply = (xListInstalledColormapsReply *)
  2728. ALLOCATE_LOCAL(sizeof(xListInstalledColormapsReply) +
  2729. pWin->drawable.pScreen->maxInstalledCmaps *
  2730. sizeof(Colormap));
  2731. if(!preply)
  2732. return(BadAlloc);
  2733. preply->type = X_Reply;
  2734. preply->sequenceNumber = client->sequence;
  2735. nummaps = (*pWin->drawable.pScreen->ListInstalledColormaps)
  2736. (pWin->drawable.pScreen, (Colormap *)&preply[1]);
  2737. preply->nColormaps = nummaps;
  2738. preply->length = nummaps;
  2739. WriteReplyToClient(client, sizeof (xListInstalledColormapsReply), preply);
  2740. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  2741. WriteSwappedDataToClient(client, nummaps * sizeof(Colormap), &preply[1]);
  2742. DEALLOCATE_LOCAL(preply);
  2743. return(client->noClientException);
  2744. }
  2745. int
  2746. ProcAllocColor (register ClientPtr client)
  2747. {
  2748. ColormapPtr pmap;
  2749. int retval;
  2750. xAllocColorReply acr;
  2751. REQUEST(xAllocColorReq);
  2752. REQUEST_SIZE_MATCH(xAllocColorReq);
  2753. pmap = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  2754. RT_COLORMAP, SecurityWriteAccess);
  2755. if (pmap)
  2756. {
  2757. #ifdef LBX
  2758. /*
  2759. * If the colormap is grabbed by a proxy, the server will have
  2760. * to regain control over the colormap. This AllocColor request
  2761. * will be handled after the server gets back the colormap control.
  2762. */
  2763. if (LbxCheckColorRequest (client, pmap, (xReq *) stuff))
  2764. return Success;
  2765. #endif
  2766. acr.type = X_Reply;
  2767. acr.length = 0;
  2768. acr.sequenceNumber = client->sequence;
  2769. acr.red = stuff->red;
  2770. acr.green = stuff->green;
  2771. acr.blue = stuff->blue;
  2772. acr.pixel = 0;
  2773. if( (retval = AllocColor(pmap, &acr.red, &acr.green, &acr.blue,
  2774. &acr.pixel, client->index)) )
  2775. {
  2776. if (client->noClientException != Success)
  2777. return(client->noClientException);
  2778. else
  2779. return (retval);
  2780. }
  2781. #ifdef PANORAMIX
  2782. if (noPanoramiXExtension || !pmap->pScreen->myNum)
  2783. #endif
  2784. WriteReplyToClient(client, sizeof(xAllocColorReply), &acr);
  2785. return (client->noClientException);
  2786. }
  2787. else
  2788. {
  2789. client->errorValue = stuff->cmap;
  2790. return (BadColor);
  2791. }
  2792. }
  2793. int
  2794. ProcAllocNamedColor (register ClientPtr client)
  2795. {
  2796. ColormapPtr pcmp;
  2797. REQUEST(xAllocNamedColorReq);
  2798. REQUEST_FIXED_SIZE(xAllocNamedColorReq, stuff->nbytes);
  2799. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  2800. RT_COLORMAP, SecurityWriteAccess);
  2801. if (pcmp)
  2802. {
  2803. int retval;
  2804. xAllocNamedColorReply ancr;
  2805. #ifdef LBX
  2806. /*
  2807. * If the colormap is grabbed by a proxy, the server will have
  2808. * to regain control over the colormap. This AllocNamedColor request
  2809. * will be handled after the server gets back the colormap control.
  2810. */
  2811. if (LbxCheckColorRequest (client, pcmp, (xReq *) stuff))
  2812. return Success;
  2813. #endif
  2814. ancr.type = X_Reply;
  2815. ancr.length = 0;
  2816. ancr.sequenceNumber = client->sequence;
  2817. if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2818. &ancr.exactRed, &ancr.exactGreen, &ancr.exactBlue))
  2819. {
  2820. ancr.screenRed = ancr.exactRed;
  2821. ancr.screenGreen = ancr.exactGreen;
  2822. ancr.screenBlue = ancr.exactBlue;
  2823. ancr.pixel = 0;
  2824. if( (retval = AllocColor(pcmp,
  2825. &ancr.screenRed, &ancr.screenGreen, &ancr.screenBlue,
  2826. &ancr.pixel, client->index)) )
  2827. {
  2828. if (client->noClientException != Success)
  2829. return(client->noClientException);
  2830. else
  2831. return(retval);
  2832. }
  2833. #ifdef PANORAMIX
  2834. if (noPanoramiXExtension || !pcmp->pScreen->myNum)
  2835. #endif
  2836. WriteReplyToClient(client, sizeof (xAllocNamedColorReply), &ancr);
  2837. return (client->noClientException);
  2838. }
  2839. else
  2840. return(BadName);
  2841. }
  2842. else
  2843. {
  2844. client->errorValue = stuff->cmap;
  2845. return (BadColor);
  2846. }
  2847. }
  2848. int
  2849. ProcAllocColorCells (register ClientPtr client)
  2850. {
  2851. ColormapPtr pcmp;
  2852. REQUEST(xAllocColorCellsReq);
  2853. REQUEST_SIZE_MATCH(xAllocColorCellsReq);
  2854. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  2855. RT_COLORMAP, SecurityWriteAccess);
  2856. if (pcmp)
  2857. {
  2858. xAllocColorCellsReply accr;
  2859. int npixels, nmasks, retval;
  2860. long length;
  2861. Pixel *ppixels, *pmasks;
  2862. #ifdef LBX
  2863. /*
  2864. * If the colormap is grabbed by a proxy, the server will have
  2865. * to regain control over the colormap. This AllocColorCells request
  2866. * will be handled after the server gets back the colormap control.
  2867. */
  2868. if (LbxCheckColorRequest (client, pcmp, (xReq *) stuff))
  2869. return Success;
  2870. #endif
  2871. npixels = stuff->colors;
  2872. if (!npixels)
  2873. {
  2874. client->errorValue = npixels;
  2875. return (BadValue);
  2876. }
  2877. if (stuff->contiguous != xTrue && stuff->contiguous != xFalse)
  2878. {
  2879. client->errorValue = stuff->contiguous;
  2880. return (BadValue);
  2881. }
  2882. nmasks = stuff->planes;
  2883. length = ((long)npixels + (long)nmasks) * sizeof(Pixel);
  2884. ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2885. if(!ppixels)
  2886. return(BadAlloc);
  2887. pmasks = ppixels + npixels;
  2888. if( (retval = AllocColorCells(client->index, pcmp, npixels, nmasks,
  2889. (Bool)stuff->contiguous, ppixels, pmasks)) )
  2890. {
  2891. DEALLOCATE_LOCAL(ppixels);
  2892. if (client->noClientException != Success)
  2893. return(client->noClientException);
  2894. else
  2895. return(retval);
  2896. }
  2897. #ifdef PANORAMIX
  2898. if (noPanoramiXExtension || !pcmp->pScreen->myNum)
  2899. #endif
  2900. {
  2901. accr.type = X_Reply;
  2902. accr.length = length >> 2;
  2903. accr.sequenceNumber = client->sequence;
  2904. accr.nPixels = npixels;
  2905. accr.nMasks = nmasks;
  2906. WriteReplyToClient(client, sizeof (xAllocColorCellsReply), &accr);
  2907. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  2908. WriteSwappedDataToClient(client, length, ppixels);
  2909. }
  2910. DEALLOCATE_LOCAL(ppixels);
  2911. return (client->noClientException);
  2912. }
  2913. else
  2914. {
  2915. client->errorValue = stuff->cmap;
  2916. return (BadColor);
  2917. }
  2918. }
  2919. int
  2920. ProcAllocColorPlanes(register ClientPtr client)
  2921. {
  2922. ColormapPtr pcmp;
  2923. REQUEST(xAllocColorPlanesReq);
  2924. REQUEST_SIZE_MATCH(xAllocColorPlanesReq);
  2925. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  2926. RT_COLORMAP, SecurityWriteAccess);
  2927. if (pcmp)
  2928. {
  2929. xAllocColorPlanesReply acpr;
  2930. int npixels, retval;
  2931. long length;
  2932. Pixel *ppixels;
  2933. #ifdef LBX
  2934. /*
  2935. * If the colormap is grabbed by a proxy, the server will have
  2936. * to regain control over the colormap. This AllocColorPlanes request
  2937. * will be handled after the server gets back the colormap control.
  2938. */
  2939. if (LbxCheckColorRequest (client, pcmp, (xReq *) stuff))
  2940. return Success;
  2941. #endif
  2942. npixels = stuff->colors;
  2943. if (!npixels)
  2944. {
  2945. client->errorValue = npixels;
  2946. return (BadValue);
  2947. }
  2948. if (stuff->contiguous != xTrue && stuff->contiguous != xFalse)
  2949. {
  2950. client->errorValue = stuff->contiguous;
  2951. return (BadValue);
  2952. }
  2953. acpr.type = X_Reply;
  2954. acpr.sequenceNumber = client->sequence;
  2955. acpr.nPixels = npixels;
  2956. length = (long)npixels * sizeof(Pixel);
  2957. ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2958. if(!ppixels)
  2959. return(BadAlloc);
  2960. if( (retval = AllocColorPlanes(client->index, pcmp, npixels,
  2961. (int)stuff->red, (int)stuff->green, (int)stuff->blue,
  2962. (Bool)stuff->contiguous, ppixels,
  2963. &acpr.redMask, &acpr.greenMask, &acpr.blueMask)) )
  2964. {
  2965. DEALLOCATE_LOCAL(ppixels);
  2966. if (client->noClientException != Success)
  2967. return(client->noClientException);
  2968. else
  2969. return(retval);
  2970. }
  2971. acpr.length = length >> 2;
  2972. #ifdef PANORAMIX
  2973. if (noPanoramiXExtension || !pcmp->pScreen->myNum)
  2974. #endif
  2975. {
  2976. WriteReplyToClient(client, sizeof(xAllocColorPlanesReply), &acpr);
  2977. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  2978. WriteSwappedDataToClient(client, length, ppixels);
  2979. }
  2980. DEALLOCATE_LOCAL(ppixels);
  2981. return (client->noClientException);
  2982. }
  2983. else
  2984. {
  2985. client->errorValue = stuff->cmap;
  2986. return (BadColor);
  2987. }
  2988. }
  2989. int
  2990. ProcFreeColors(register ClientPtr client)
  2991. {
  2992. ColormapPtr pcmp;
  2993. REQUEST(xFreeColorsReq);
  2994. REQUEST_AT_LEAST_SIZE(xFreeColorsReq);
  2995. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  2996. RT_COLORMAP, SecurityWriteAccess);
  2997. if (pcmp)
  2998. {
  2999. int count;
  3000. int retval;
  3001. if(pcmp->flags & AllAllocated)
  3002. return(BadAccess);
  3003. count = ((client->req_len << 2)- sizeof(xFreeColorsReq)) >> 2;
  3004. retval = FreeColors(pcmp, client->index, count,
  3005. (Pixel *)&stuff[1], (Pixel)stuff->planeMask);
  3006. if (client->noClientException != Success)
  3007. return(client->noClientException);
  3008. else
  3009. {
  3010. client->errorValue = clientErrorValue;
  3011. return(retval);
  3012. }
  3013. }
  3014. else
  3015. {
  3016. client->errorValue = stuff->cmap;
  3017. return (BadColor);
  3018. }
  3019. }
  3020. int
  3021. ProcStoreColors (ClientPtr client)
  3022. {
  3023. ColormapPtr pcmp;
  3024. REQUEST(xStoreColorsReq);
  3025. REQUEST_AT_LEAST_SIZE(xStoreColorsReq);
  3026. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  3027. RT_COLORMAP, SecurityWriteAccess);
  3028. if (pcmp)
  3029. {
  3030. int count;
  3031. int retval;
  3032. count = (client->req_len << 2) - sizeof(xStoreColorsReq);
  3033. if (count % sizeof(xColorItem))
  3034. return(BadLength);
  3035. count /= sizeof(xColorItem);
  3036. retval = StoreColors(pcmp, count, (xColorItem *)&stuff[1]);
  3037. if (client->noClientException != Success)
  3038. return(client->noClientException);
  3039. else
  3040. {
  3041. client->errorValue = clientErrorValue;
  3042. return(retval);
  3043. }
  3044. }
  3045. else
  3046. {
  3047. client->errorValue = stuff->cmap;
  3048. return (BadColor);
  3049. }
  3050. }
  3051. int
  3052. ProcStoreNamedColor (register ClientPtr client)
  3053. {
  3054. ColormapPtr pcmp;
  3055. REQUEST(xStoreNamedColorReq);
  3056. REQUEST_FIXED_SIZE(xStoreNamedColorReq, stuff->nbytes);
  3057. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  3058. RT_COLORMAP, SecurityWriteAccess);
  3059. if (pcmp)
  3060. {
  3061. xColorItem def;
  3062. int retval;
  3063. if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1],
  3064. stuff->nbytes, &def.red, &def.green, &def.blue))
  3065. {
  3066. def.flags = stuff->flags;
  3067. def.pixel = stuff->pixel;
  3068. retval = StoreColors(pcmp, 1, &def);
  3069. if (client->noClientException != Success)
  3070. return(client->noClientException);
  3071. else
  3072. return(retval);
  3073. }
  3074. return (BadName);
  3075. }
  3076. else
  3077. {
  3078. client->errorValue = stuff->cmap;
  3079. return (BadColor);
  3080. }
  3081. }
  3082. int
  3083. ProcQueryColors(register ClientPtr client)
  3084. {
  3085. ColormapPtr pcmp;
  3086. REQUEST(xQueryColorsReq);
  3087. REQUEST_AT_LEAST_SIZE(xQueryColorsReq);
  3088. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  3089. RT_COLORMAP, SecurityReadAccess);
  3090. if (pcmp)
  3091. {
  3092. int count, retval;
  3093. xrgb *prgbs;
  3094. xQueryColorsReply qcr;
  3095. count = ((client->req_len << 2) - sizeof(xQueryColorsReq)) >> 2;
  3096. prgbs = (xrgb *)ALLOCATE_LOCAL(count * sizeof(xrgb));
  3097. if(!prgbs && count)
  3098. return(BadAlloc);
  3099. if( (retval = QueryColors(pcmp, count, (Pixel *)&stuff[1], prgbs)) )
  3100. {
  3101. if (prgbs) DEALLOCATE_LOCAL(prgbs);
  3102. if (client->noClientException != Success)
  3103. return(client->noClientException);
  3104. else
  3105. {
  3106. client->errorValue = clientErrorValue;
  3107. return (retval);
  3108. }
  3109. }
  3110. qcr.type = X_Reply;
  3111. qcr.length = (count * sizeof(xrgb)) >> 2;
  3112. qcr.sequenceNumber = client->sequence;
  3113. qcr.nColors = count;
  3114. WriteReplyToClient(client, sizeof(xQueryColorsReply), &qcr);
  3115. if (count)
  3116. {
  3117. client->pSwapReplyFunc = (ReplySwapPtr) SQColorsExtend;
  3118. WriteSwappedDataToClient(client, count * sizeof(xrgb), prgbs);
  3119. }
  3120. if (prgbs) DEALLOCATE_LOCAL(prgbs);
  3121. return(client->noClientException);
  3122. }
  3123. else
  3124. {
  3125. client->errorValue = stuff->cmap;
  3126. return (BadColor);
  3127. }
  3128. }
  3129. int
  3130. ProcLookupColor(register ClientPtr client)
  3131. {
  3132. ColormapPtr pcmp;
  3133. REQUEST(xLookupColorReq);
  3134. REQUEST_FIXED_SIZE(xLookupColorReq, stuff->nbytes);
  3135. pcmp = (ColormapPtr)SecurityLookupIDByType(client, stuff->cmap,
  3136. RT_COLORMAP, SecurityReadAccess);
  3137. if (pcmp)
  3138. {
  3139. xLookupColorReply lcr;
  3140. if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  3141. &lcr.exactRed, &lcr.exactGreen, &lcr.exactBlue))
  3142. {
  3143. lcr.type = X_Reply;
  3144. lcr.length = 0;
  3145. lcr.sequenceNumber = client->sequence;
  3146. lcr.screenRed = lcr.exactRed;
  3147. lcr.screenGreen = lcr.exactGreen;
  3148. lcr.screenBlue = lcr.exactBlue;
  3149. (*pcmp->pScreen->ResolveColor)(&lcr.screenRed,
  3150. &lcr.screenGreen,
  3151. &lcr.screenBlue,
  3152. pcmp->pVisual);
  3153. WriteReplyToClient(client, sizeof(xLookupColorReply), &lcr);
  3154. return(client->noClientException);
  3155. }
  3156. return (BadName);
  3157. }
  3158. else
  3159. {
  3160. client->errorValue = stuff->cmap;
  3161. return (BadColor);
  3162. }
  3163. }
  3164. int
  3165. ProcCreateCursor (register ClientPtr client)
  3166. {
  3167. CursorPtr pCursor;
  3168. register PixmapPtr src;
  3169. register PixmapPtr msk;
  3170. unsigned char * srcbits;
  3171. unsigned char * mskbits;
  3172. unsigned short width, height;
  3173. long n;
  3174. CursorMetricRec cm;
  3175. REQUEST(xCreateCursorReq);
  3176. REQUEST_SIZE_MATCH(xCreateCursorReq);
  3177. LEGAL_NEW_RESOURCE(stuff->cid, client);
  3178. src = (PixmapPtr)SecurityLookupIDByType(client, stuff->source,
  3179. RT_PIXMAP, SecurityReadAccess);
  3180. msk = (PixmapPtr)SecurityLookupIDByType(client, stuff->mask,
  3181. RT_PIXMAP, SecurityReadAccess);
  3182. if ( src == (PixmapPtr)NULL)
  3183. {
  3184. client->errorValue = stuff->source;
  3185. return (BadPixmap);
  3186. }
  3187. if ( msk == (PixmapPtr)NULL)
  3188. {
  3189. if (stuff->mask != None)
  3190. {
  3191. client->errorValue = stuff->mask;
  3192. return (BadPixmap);
  3193. }
  3194. }
  3195. else if ( src->drawable.width != msk->drawable.width
  3196. || src->drawable.height != msk->drawable.height
  3197. || src->drawable.depth != 1
  3198. || msk->drawable.depth != 1)
  3199. return (BadMatch);
  3200. width = src->drawable.width;
  3201. height = src->drawable.height;
  3202. if ( stuff->x > width
  3203. || stuff->y > height )
  3204. return (BadMatch);
  3205. n = BitmapBytePad(width)*height;
  3206. srcbits = (unsigned char *)xalloc(n);
  3207. if (!srcbits)
  3208. return (BadAlloc);
  3209. mskbits = (unsigned char *)xalloc(n);
  3210. if (!mskbits)
  3211. {
  3212. xfree(srcbits);
  3213. return (BadAlloc);
  3214. }
  3215. /* zeroing the (pad) bits helps some ddx cursor handling */
  3216. bzero((char *)srcbits, n);
  3217. (* src->drawable.pScreen->GetImage)( (DrawablePtr)src, 0, 0, width, height,
  3218. XYPixmap, 1, (pointer)srcbits);
  3219. if ( msk == (PixmapPtr)NULL)
  3220. {
  3221. register unsigned char *bits = mskbits;
  3222. while (--n >= 0)
  3223. *bits++ = ~0;
  3224. }
  3225. else
  3226. {
  3227. /* zeroing the (pad) bits helps some ddx cursor handling */
  3228. bzero((char *)mskbits, n);
  3229. (* msk->drawable.pScreen->GetImage)( (DrawablePtr)msk, 0, 0, width,
  3230. height, XYPixmap, 1, (pointer)mskbits);
  3231. }
  3232. cm.width = width;
  3233. cm.height = height;
  3234. cm.xhot = stuff->x;
  3235. cm.yhot = stuff->y;
  3236. pCursor = AllocCursor( srcbits, mskbits, &cm,
  3237. stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  3238. stuff->backRed, stuff->backGreen, stuff->backBlue);
  3239. if (pCursor && AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  3240. {
  3241. #ifdef TEST
  3242. fprintf(stderr, "ProcCreateCursor: Created cursor at [%p].\n", (void *) pCursor);
  3243. #endif
  3244. return (client->noClientException);
  3245. }
  3246. return BadAlloc;
  3247. }
  3248. int
  3249. ProcCreateGlyphCursor (register ClientPtr client)
  3250. {
  3251. CursorPtr pCursor;
  3252. int res;
  3253. REQUEST(xCreateGlyphCursorReq);
  3254. REQUEST_SIZE_MATCH(xCreateGlyphCursorReq);
  3255. LEGAL_NEW_RESOURCE(stuff->cid, client);
  3256. res = AllocGlyphCursor(stuff->source, stuff->sourceChar,
  3257. stuff->mask, stuff->maskChar,
  3258. stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  3259. stuff->backRed, stuff->backGreen, stuff->backBlue,
  3260. &pCursor, client);
  3261. if (res != Success)
  3262. return res;
  3263. if (AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  3264. return client->noClientException;
  3265. return BadAlloc;
  3266. }
  3267. int
  3268. ProcFreeCursor (register ClientPtr client)
  3269. {
  3270. CursorPtr pCursor;
  3271. REQUEST(xResourceReq);
  3272. REQUEST_SIZE_MATCH(xResourceReq);
  3273. pCursor = (CursorPtr)SecurityLookupIDByType(client, stuff->id,
  3274. RT_CURSOR, SecurityDestroyAccess);
  3275. if (pCursor)
  3276. {
  3277. FreeResource(stuff->id, RT_NONE);
  3278. return (client->noClientException);
  3279. }
  3280. else
  3281. {
  3282. client->errorValue = stuff->id;
  3283. return (BadCursor);
  3284. }
  3285. }
  3286. int
  3287. ProcQueryBestSize (register ClientPtr client)
  3288. {
  3289. xQueryBestSizeReply reply;
  3290. register DrawablePtr pDraw;
  3291. ScreenPtr pScreen;
  3292. REQUEST(xQueryBestSizeReq);
  3293. REQUEST_SIZE_MATCH(xQueryBestSizeReq);
  3294. if ((stuff->class != CursorShape) &&
  3295. (stuff->class != TileShape) &&
  3296. (stuff->class != StippleShape))
  3297. {
  3298. client->errorValue = stuff->class;
  3299. return(BadValue);
  3300. }
  3301. SECURITY_VERIFY_GEOMETRABLE (pDraw, stuff->drawable, client,
  3302. SecurityReadAccess);
  3303. if (stuff->class != CursorShape && pDraw->type == UNDRAWABLE_WINDOW)
  3304. return (BadMatch);
  3305. pScreen = pDraw->pScreen;
  3306. (* pScreen->QueryBestSize)(stuff->class, &stuff->width,
  3307. &stuff->height, pScreen);
  3308. reply.type = X_Reply;
  3309. reply.length = 0;
  3310. reply.sequenceNumber = client->sequence;
  3311. reply.width = stuff->width;
  3312. reply.height = stuff->height;
  3313. WriteReplyToClient(client, sizeof(xQueryBestSizeReply), &reply);
  3314. return (client->noClientException);
  3315. }
  3316. int
  3317. ProcSetScreenSaver (register ClientPtr client)
  3318. {
  3319. int blankingOption, exposureOption;
  3320. REQUEST(xSetScreenSaverReq);
  3321. REQUEST_SIZE_MATCH(xSetScreenSaverReq);
  3322. blankingOption = stuff->preferBlank;
  3323. if ((blankingOption != DontPreferBlanking) &&
  3324. (blankingOption != PreferBlanking) &&
  3325. (blankingOption != DefaultBlanking))
  3326. {
  3327. client->errorValue = blankingOption;
  3328. return BadValue;
  3329. }
  3330. exposureOption = stuff->allowExpose;
  3331. if ((exposureOption != DontAllowExposures) &&
  3332. (exposureOption != AllowExposures) &&
  3333. (exposureOption != DefaultExposures))
  3334. {
  3335. client->errorValue = exposureOption;
  3336. return BadValue;
  3337. }
  3338. if (stuff->timeout < -1)
  3339. {
  3340. client->errorValue = stuff->timeout;
  3341. return BadValue;
  3342. }
  3343. if (stuff->interval < -1)
  3344. {
  3345. client->errorValue = stuff->interval;
  3346. return BadValue;
  3347. }
  3348. /*
  3349. * The NX agent uses the screen saver procedure
  3350. * to monitor the user activities and launch its
  3351. * handlers (like timeout feature), so we can't
  3352. * always allow the clients to change our values.
  3353. */
  3354. #ifdef TEST
  3355. fprintf(stderr, "ProcSetScreenSaver: Called with timeout [%d] interval [%d] Blanking [%d] Exposure [%d].\n",
  3356. stuff -> timeout, stuff -> interval, blankingOption, exposureOption);
  3357. #endif
  3358. if (nxagentOption(Timeout) == 0)
  3359. {
  3360. if (blankingOption == DefaultBlanking)
  3361. {
  3362. ScreenSaverBlanking = defaultScreenSaverBlanking;
  3363. }
  3364. else
  3365. {
  3366. ScreenSaverBlanking = blankingOption;
  3367. }
  3368. if (exposureOption == DefaultExposures)
  3369. {
  3370. ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
  3371. }
  3372. else
  3373. {
  3374. ScreenSaverAllowExposures = exposureOption;
  3375. }
  3376. if (stuff->timeout >= 0)
  3377. {
  3378. ScreenSaverTime = stuff->timeout * MILLI_PER_SECOND;
  3379. }
  3380. else
  3381. {
  3382. ScreenSaverTime = defaultScreenSaverTime;
  3383. }
  3384. if (stuff->interval >= 0)
  3385. {
  3386. ScreenSaverInterval = stuff->interval * MILLI_PER_SECOND;
  3387. }
  3388. else
  3389. {
  3390. ScreenSaverInterval = defaultScreenSaverInterval;
  3391. }
  3392. SetScreenSaverTimer();
  3393. }
  3394. #ifdef TEST
  3395. else
  3396. {
  3397. fprintf(stderr, "ProcSetScreenSaver: Keeping auto-disconnect timeout set to [%d] seconds.\n",
  3398. nxagentOption(Timeout));
  3399. }
  3400. #endif
  3401. return (client->noClientException);
  3402. }
  3403. int
  3404. ProcGetScreenSaver(register ClientPtr client)
  3405. {
  3406. xGetScreenSaverReply rep;
  3407. REQUEST_SIZE_MATCH(xReq);
  3408. rep.type = X_Reply;
  3409. rep.length = 0;
  3410. rep.sequenceNumber = client->sequence;
  3411. rep.timeout = ScreenSaverTime / MILLI_PER_SECOND;
  3412. rep.interval = ScreenSaverInterval / MILLI_PER_SECOND;
  3413. rep.preferBlanking = ScreenSaverBlanking;
  3414. rep.allowExposures = ScreenSaverAllowExposures;
  3415. WriteReplyToClient(client, sizeof(xGetScreenSaverReply), &rep);
  3416. return (client->noClientException);
  3417. }
  3418. int
  3419. ProcChangeHosts(register ClientPtr client)
  3420. {
  3421. REQUEST(xChangeHostsReq);
  3422. int result;
  3423. REQUEST_FIXED_SIZE(xChangeHostsReq, stuff->hostLength);
  3424. if(stuff->mode == HostInsert)
  3425. result = AddHost(client, (int)stuff->hostFamily,
  3426. stuff->hostLength, (pointer)&stuff[1]);
  3427. else if (stuff->mode == HostDelete)
  3428. result = RemoveHost(client, (int)stuff->hostFamily,
  3429. stuff->hostLength, (pointer)&stuff[1]);
  3430. else
  3431. {
  3432. client->errorValue = stuff->mode;
  3433. return BadValue;
  3434. }
  3435. if (!result)
  3436. result = client->noClientException;
  3437. return (result);
  3438. }
  3439. int
  3440. ProcListHosts(register ClientPtr client)
  3441. {
  3442. xListHostsReply reply;
  3443. int len, nHosts, result;
  3444. pointer pdata;
  3445. /* REQUEST(xListHostsReq); */
  3446. REQUEST_SIZE_MATCH(xListHostsReq);
  3447. #ifdef XCSECURITY
  3448. /* untrusted clients can't list hosts */
  3449. if (client->trustLevel != XSecurityClientTrusted)
  3450. {
  3451. SecurityAudit("client %d attempted to list hosts\n", client->index);
  3452. return BadAccess;
  3453. }
  3454. #endif
  3455. result = GetHosts(&pdata, &nHosts, &len, &reply.enabled);
  3456. if (result != Success)
  3457. return(result);
  3458. reply.type = X_Reply;
  3459. reply.sequenceNumber = client->sequence;
  3460. reply.nHosts = nHosts;
  3461. reply.length = len >> 2;
  3462. WriteReplyToClient(client, sizeof(xListHostsReply), &reply);
  3463. if (nHosts)
  3464. {
  3465. client->pSwapReplyFunc = (ReplySwapPtr) SLHostsExtend;
  3466. WriteSwappedDataToClient(client, len, pdata);
  3467. }
  3468. xfree(pdata);
  3469. return (client->noClientException);
  3470. }
  3471. int
  3472. ProcChangeAccessControl(register ClientPtr client)
  3473. {
  3474. int result;
  3475. REQUEST(xSetAccessControlReq);
  3476. REQUEST_SIZE_MATCH(xSetAccessControlReq);
  3477. if ((stuff->mode != EnableAccess) && (stuff->mode != DisableAccess))
  3478. {
  3479. client->errorValue = stuff->mode;
  3480. return BadValue;
  3481. }
  3482. result = ChangeAccessControl(client, stuff->mode == EnableAccess);
  3483. if (!result)
  3484. result = client->noClientException;
  3485. return (result);
  3486. }
  3487. int
  3488. ProcKillClient(register ClientPtr client)
  3489. {
  3490. REQUEST(xResourceReq);
  3491. ClientPtr killclient;
  3492. REQUEST_SIZE_MATCH(xResourceReq);
  3493. if (stuff->id == AllTemporary)
  3494. {
  3495. CloseDownRetainedResources();
  3496. return (client->noClientException);
  3497. }
  3498. if ((killclient = LookupClient(stuff->id, client)))
  3499. {
  3500. CloseDownClient(killclient);
  3501. /* if an LBX proxy gets killed, isItTimeToYield will be set */
  3502. if (isItTimeToYield || (client == killclient))
  3503. {
  3504. /* force yield and return Success, so that Dispatch()
  3505. * doesn't try to touch client
  3506. */
  3507. isItTimeToYield = TRUE;
  3508. return (Success);
  3509. }
  3510. return (client->noClientException);
  3511. }
  3512. else
  3513. {
  3514. client->errorValue = stuff->id;
  3515. return (BadValue);
  3516. }
  3517. }
  3518. int
  3519. ProcSetFontPath(register ClientPtr client)
  3520. {
  3521. unsigned char *ptr;
  3522. unsigned long nbytes, total;
  3523. long nfonts;
  3524. int n, result;
  3525. int error;
  3526. REQUEST(xSetFontPathReq);
  3527. REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
  3528. nbytes = (client->req_len << 2) - sizeof(xSetFontPathReq);
  3529. total = nbytes;
  3530. ptr = (unsigned char *)&stuff[1];
  3531. nfonts = stuff->nFonts;
  3532. while (--nfonts >= 0)
  3533. {
  3534. if ((total == 0) || (total < (n = (*ptr + 1))))
  3535. return(BadLength);
  3536. total -= n;
  3537. ptr += n;
  3538. }
  3539. if (total >= 4)
  3540. return(BadLength);
  3541. result = SetFontPath(client, stuff->nFonts, (unsigned char *)&stuff[1],
  3542. &error);
  3543. if (!result)
  3544. {
  3545. result = client->noClientException;
  3546. client->errorValue = error;
  3547. }
  3548. return (result);
  3549. }
  3550. int
  3551. ProcGetFontPath(register ClientPtr client)
  3552. {
  3553. xGetFontPathReply reply;
  3554. int stringLens, numpaths;
  3555. unsigned char *bufferStart;
  3556. /* REQUEST (xReq); */
  3557. REQUEST_SIZE_MATCH(xReq);
  3558. bufferStart = GetFontPath(&numpaths, &stringLens);
  3559. reply.type = X_Reply;
  3560. reply.sequenceNumber = client->sequence;
  3561. reply.length = (stringLens + numpaths + 3) >> 2;
  3562. reply.nPaths = numpaths;
  3563. WriteReplyToClient(client, sizeof(xGetFontPathReply), &reply);
  3564. if (stringLens || numpaths)
  3565. (void)WriteToClient(client, stringLens + numpaths, (char *)bufferStart);
  3566. return(client->noClientException);
  3567. }
  3568. int
  3569. ProcChangeCloseDownMode(register ClientPtr client)
  3570. {
  3571. REQUEST(xSetCloseDownModeReq);
  3572. REQUEST_SIZE_MATCH(xSetCloseDownModeReq);
  3573. if ((stuff->mode == AllTemporary) ||
  3574. (stuff->mode == RetainPermanent) ||
  3575. (stuff->mode == RetainTemporary))
  3576. {
  3577. client->closeDownMode = stuff->mode;
  3578. return (client->noClientException);
  3579. }
  3580. else
  3581. {
  3582. client->errorValue = stuff->mode;
  3583. return (BadValue);
  3584. }
  3585. }
  3586. int ProcForceScreenSaver(register ClientPtr client)
  3587. {
  3588. REQUEST(xForceScreenSaverReq);
  3589. REQUEST_SIZE_MATCH(xForceScreenSaverReq);
  3590. if ((stuff->mode != ScreenSaverReset) &&
  3591. (stuff->mode != ScreenSaverActive))
  3592. {
  3593. client->errorValue = stuff->mode;
  3594. return BadValue;
  3595. }
  3596. /*
  3597. * The NX agent uses the screen saver procedure
  3598. * to monitor the user activities and launch its
  3599. * handlers (like timeout feature), so we can't
  3600. * always allow the clients to force the screen
  3601. * saver handler execution.
  3602. */
  3603. if (nxagentOption(Timeout) == 0)
  3604. {
  3605. SaveScreens(SCREEN_SAVER_FORCER, (int)stuff->mode);
  3606. }
  3607. #ifdef TEST
  3608. else
  3609. {
  3610. fprintf(stderr, "ProcForceScreenSaver: Ignoring the client request with mode [%d].\n",
  3611. stuff -> mode);
  3612. }
  3613. #endif
  3614. return client->noClientException;
  3615. }
  3616. int ProcNoOperation(register ClientPtr client)
  3617. {
  3618. REQUEST_AT_LEAST_SIZE(xReq);
  3619. /* noop -- don't do anything */
  3620. return(client->noClientException);
  3621. }
  3622. void
  3623. InitProcVectors(void)
  3624. {
  3625. int i;
  3626. for (i = 0; i<256; i++)
  3627. {
  3628. if(!ProcVector[i])
  3629. {
  3630. ProcVector[i] = SwappedProcVector[i] = ProcBadRequest;
  3631. ReplySwapVector[i] = ReplyNotSwappd;
  3632. }
  3633. #ifdef K5AUTH
  3634. if (!k5_Vector[i])
  3635. {
  3636. k5_Vector[i] = k5_bad;
  3637. }
  3638. #endif
  3639. }
  3640. for(i = LASTEvent; i < 128; i++)
  3641. {
  3642. EventSwapVector[i] = NotImplemented;
  3643. }
  3644. }
  3645. /**********************
  3646. * CloseDownClient
  3647. *
  3648. * Client can either mark his resources destroy or retain. If retained and
  3649. * then killed again, the client is really destroyed.
  3650. *********************/
  3651. void
  3652. CloseDownClient(register ClientPtr client)
  3653. {
  3654. Bool really_close_down = client->clientGone ||
  3655. client->closeDownMode == DestroyAll;
  3656. /*
  3657. * There must be a better way to hook a
  3658. * call-back function to be called any
  3659. * time a client is going to be closed.
  3660. */
  3661. nxagentClearClipboard(client, NULL);
  3662. /*
  3663. * Need to reset the karma counter and
  3664. * get rid of the pending sync replies.
  3665. */
  3666. nxagentWakeupByReset(client);
  3667. /*
  3668. * Check if the client
  3669. * is a shadow nxagent.
  3670. */
  3671. nxagentCheckIfShadowAgent(client);
  3672. if (!client->clientGone)
  3673. {
  3674. /* ungrab server if grabbing client dies */
  3675. if (grabState != GrabNone && grabClient == client)
  3676. {
  3677. UngrabServer(client);
  3678. }
  3679. BITCLEAR(grabWaiters, client->index);
  3680. DeleteClientFromAnySelections(client);
  3681. ReleaseActiveGrabs(client);
  3682. DeleteClientFontStuff(client);
  3683. if (!really_close_down)
  3684. {
  3685. /* This frees resources that should never be retained
  3686. * no matter what the close down mode is. Actually we
  3687. * could do this unconditionally, but it's probably
  3688. * better not to traverse all the client's resources
  3689. * twice (once here, once a few lines down in
  3690. * FreeClientResources) in the common case of
  3691. * really_close_down == TRUE.
  3692. */
  3693. FreeClientNeverRetainResources(client);
  3694. client->clientState = ClientStateRetained;
  3695. if (ClientStateCallback)
  3696. {
  3697. NewClientInfoRec clientinfo;
  3698. clientinfo.client = client;
  3699. clientinfo.prefix = (xConnSetupPrefix *)NULL;
  3700. clientinfo.setup = (xConnSetup *) NULL;
  3701. CallCallbacks((&ClientStateCallback), (pointer)&clientinfo);
  3702. }
  3703. }
  3704. client->clientGone = TRUE; /* so events aren't sent to client */
  3705. if (ClientIsAsleep(client))
  3706. ClientSignal (client);
  3707. ProcessWorkQueueZombies();
  3708. #ifdef LBX
  3709. ProcessQTagZombies();
  3710. #endif
  3711. CloseDownConnection(client);
  3712. /* If the client made it to the Running stage, nClients has
  3713. * been incremented on its behalf, so we need to decrement it
  3714. * now. If it hasn't gotten to Running, nClients has *not*
  3715. * been incremented, so *don't* decrement it.
  3716. */
  3717. if (client->clientState != ClientStateInitial &&
  3718. client->clientState != ClientStateAuthenticating )
  3719. {
  3720. --nClients;
  3721. }
  3722. }
  3723. if (really_close_down)
  3724. {
  3725. if (client->clientState == ClientStateRunning && nClients == 0)
  3726. dispatchException |= dispatchExceptionAtReset;
  3727. client->clientState = ClientStateGone;
  3728. if (ClientStateCallback)
  3729. {
  3730. NewClientInfoRec clientinfo;
  3731. clientinfo.client = client;
  3732. clientinfo.prefix = (xConnSetupPrefix *)NULL;
  3733. clientinfo.setup = (xConnSetup *) NULL;
  3734. CallCallbacks((&ClientStateCallback), (pointer)&clientinfo);
  3735. }
  3736. FreeClientResources(client);
  3737. if (client->index < nextFreeClientID)
  3738. nextFreeClientID = client->index;
  3739. clients[client->index] = NullClient;
  3740. #ifdef SMART_SCHEDULE
  3741. SmartLastClient = NullClient;
  3742. #endif
  3743. xfree(client);
  3744. while (!clients[currentMaxClients-1])
  3745. currentMaxClients--;
  3746. }
  3747. }
  3748. static void
  3749. KillAllClients()
  3750. {
  3751. int i;
  3752. for (i=1; i<currentMaxClients; i++)
  3753. if (clients[i]) {
  3754. /* Make sure Retained clients are released. */
  3755. clients[i]->closeDownMode = DestroyAll;
  3756. CloseDownClient(clients[i]);
  3757. }
  3758. }
  3759. /*********************
  3760. * CloseDownRetainedResources
  3761. *
  3762. * Find all clients that are gone and have terminated in RetainTemporary
  3763. * and destroy their resources.
  3764. *********************/
  3765. void
  3766. CloseDownRetainedResources()
  3767. {
  3768. register int i;
  3769. register ClientPtr client;
  3770. for (i=1; i<currentMaxClients; i++)
  3771. {
  3772. client = clients[i];
  3773. if (client && (client->closeDownMode == RetainTemporary)
  3774. && (client->clientGone))
  3775. CloseDownClient(client);
  3776. }
  3777. }
  3778. void InitClient(ClientPtr client, int i, pointer ospriv)
  3779. {
  3780. client->index = i;
  3781. client->sequence = 0;
  3782. client->clientAsMask = ((Mask)i) << CLIENTOFFSET;
  3783. client->clientGone = FALSE;
  3784. if (i)
  3785. {
  3786. client->closeDownMode = DestroyAll;
  3787. client->lastDrawable = (DrawablePtr)WindowTable[0];
  3788. client->lastDrawableID = WindowTable[0]->drawable.id;
  3789. }
  3790. else
  3791. {
  3792. client->closeDownMode = RetainPermanent;
  3793. client->lastDrawable = (DrawablePtr)NULL;
  3794. client->lastDrawableID = INVALID;
  3795. }
  3796. client->lastGC = (GCPtr) NULL;
  3797. client->lastGCID = INVALID;
  3798. client->numSaved = 0;
  3799. client->saveSet = (SaveSetElt *)NULL;
  3800. client->noClientException = Success;
  3801. #ifdef LOG_DEBUG
  3802. client->requestLogIndex = 0;
  3803. #endif
  3804. client->requestVector = InitialVector;
  3805. client->osPrivate = ospriv;
  3806. client->swapped = FALSE;
  3807. client->big_requests = FALSE;
  3808. client->priority = 0;
  3809. client->clientState = ClientStateInitial;
  3810. #ifdef XKB
  3811. if (!noXkbExtension) {
  3812. client->xkbClientFlags = 0;
  3813. client->mapNotifyMask = 0;
  3814. QueryMinMaxKeyCodes(&client->minKC,&client->maxKC);
  3815. }
  3816. #endif
  3817. client->replyBytesRemaining = 0;
  3818. #ifdef LBX
  3819. client->readRequest = StandardReadRequestFromClient;
  3820. #endif
  3821. #ifdef XCSECURITY
  3822. client->trustLevel = XSecurityClientTrusted;
  3823. client->CheckAccess = NULL;
  3824. client->authId = 0;
  3825. #endif
  3826. #ifdef XAPPGROUP
  3827. client->appgroup = NULL;
  3828. #endif
  3829. client->fontResFunc = NULL;
  3830. #ifdef SMART_SCHEDULE
  3831. client->smart_priority = 0;
  3832. client->smart_start_tick = SmartScheduleTime;
  3833. client->smart_stop_tick = SmartScheduleTime;
  3834. client->smart_check_tick = SmartScheduleTime;
  3835. #endif
  3836. }
  3837. extern int clientPrivateLen;
  3838. extern unsigned *clientPrivateSizes;
  3839. extern unsigned totalClientSize;
  3840. int
  3841. InitClientPrivates(ClientPtr client)
  3842. {
  3843. register char *ptr;
  3844. DevUnion *ppriv;
  3845. register unsigned *sizes;
  3846. register unsigned size;
  3847. register int i;
  3848. if (totalClientSize == sizeof(ClientRec))
  3849. ppriv = (DevUnion *)NULL;
  3850. else if (client->index)
  3851. ppriv = (DevUnion *)(client + 1);
  3852. else
  3853. {
  3854. ppriv = (DevUnion *)xalloc(totalClientSize - sizeof(ClientRec));
  3855. if (!ppriv)
  3856. return 0;
  3857. }
  3858. client->devPrivates = ppriv;
  3859. sizes = clientPrivateSizes;
  3860. ptr = (char *)(ppriv + clientPrivateLen);
  3861. for (i = clientPrivateLen; --i >= 0; ppriv++, sizes++)
  3862. {
  3863. if ( (size = *sizes) )
  3864. {
  3865. ppriv->ptr = (pointer)ptr;
  3866. ptr += size;
  3867. }
  3868. else
  3869. ppriv->ptr = (pointer)NULL;
  3870. }
  3871. /*
  3872. * Initialize the private members.
  3873. */
  3874. nxagentInitClientPrivates(client);
  3875. return 1;
  3876. }
  3877. /************************
  3878. * int NextAvailableClient(ospriv)
  3879. *
  3880. * OS dependent portion can't assign client id's because of CloseDownModes.
  3881. * Returns NULL if there are no free clients.
  3882. *************************/
  3883. ClientPtr NextAvailableClient(pointer ospriv)
  3884. {
  3885. register int i;
  3886. register ClientPtr client;
  3887. xReq data;
  3888. i = nextFreeClientID;
  3889. if (i == MAXCLIENTS)
  3890. return (ClientPtr)NULL;
  3891. clients[i] = client = (ClientPtr)xalloc(totalClientSize);
  3892. if (!client)
  3893. return (ClientPtr)NULL;
  3894. InitClient(client, i, ospriv);
  3895. InitClientPrivates(client);
  3896. if (!InitClientResources(client))
  3897. {
  3898. xfree(client);
  3899. return (ClientPtr)NULL;
  3900. }
  3901. data.reqType = 1;
  3902. data.length = (sz_xReq + sz_xConnClientPrefix) >> 2;
  3903. if (!InsertFakeRequest(client, (char *)&data, sz_xReq))
  3904. {
  3905. FreeClientResources(client);
  3906. xfree(client);
  3907. return (ClientPtr)NULL;
  3908. }
  3909. if (i == currentMaxClients)
  3910. currentMaxClients++;
  3911. while ((nextFreeClientID < MAXCLIENTS) && clients[nextFreeClientID])
  3912. nextFreeClientID++;
  3913. if (ClientStateCallback)
  3914. {
  3915. NewClientInfoRec clientinfo;
  3916. clientinfo.client = client;
  3917. clientinfo.prefix = (xConnSetupPrefix *)NULL;
  3918. clientinfo.setup = (xConnSetup *) NULL;
  3919. CallCallbacks((&ClientStateCallback), (pointer)&clientinfo);
  3920. }
  3921. return(client);
  3922. }
  3923. int
  3924. ProcInitialConnection(register ClientPtr client)
  3925. {
  3926. REQUEST(xReq);
  3927. register xConnClientPrefix *prefix;
  3928. int whichbyte = 1;
  3929. prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3930. if ((prefix->byteOrder != 'l') && (prefix->byteOrder != 'B'))
  3931. return (client->noClientException = -1);
  3932. if (((*(char *) &whichbyte) && (prefix->byteOrder == 'B')) ||
  3933. (!(*(char *) &whichbyte) && (prefix->byteOrder == 'l')))
  3934. {
  3935. client->swapped = TRUE;
  3936. SwapConnClientPrefix(prefix);
  3937. }
  3938. stuff->reqType = 2;
  3939. stuff->length += ((prefix->nbytesAuthProto + (unsigned)3) >> 2) +
  3940. ((prefix->nbytesAuthString + (unsigned)3) >> 2);
  3941. if (client->swapped)
  3942. {
  3943. swaps(&stuff->length, whichbyte);
  3944. }
  3945. ResetCurrentRequest(client);
  3946. return (client->noClientException);
  3947. }
  3948. #ifdef LBX
  3949. void
  3950. IncrementClientCount()
  3951. {
  3952. nClients++;
  3953. }
  3954. #endif
  3955. int
  3956. SendConnSetup(register ClientPtr client, char *reason)
  3957. {
  3958. register xWindowRoot *root;
  3959. register int i;
  3960. int numScreens;
  3961. char* lConnectionInfo;
  3962. xConnSetupPrefix* lconnSetupPrefix;
  3963. if (reason)
  3964. {
  3965. xConnSetupPrefix csp;
  3966. csp.success = xFalse;
  3967. csp.lengthReason = strlen(reason);
  3968. csp.length = (csp.lengthReason + (unsigned)3) >> 2;
  3969. csp.majorVersion = X_PROTOCOL;
  3970. csp.minorVersion = X_PROTOCOL_REVISION;
  3971. if (client->swapped)
  3972. WriteSConnSetupPrefix(client, &csp);
  3973. else
  3974. (void)WriteToClient(client, sz_xConnSetupPrefix, (char *) &csp);
  3975. (void)WriteToClient(client, (int)csp.lengthReason, reason);
  3976. return (client->noClientException = -1);
  3977. }
  3978. numScreens = screenInfo.numScreens;
  3979. lConnectionInfo = ConnectionInfo;
  3980. lconnSetupPrefix = &connSetupPrefix;
  3981. /* We're about to start speaking X protocol back to the client by
  3982. * sending the connection setup info. This means the authorization
  3983. * step is complete, and we can count the client as an
  3984. * authorized one.
  3985. */
  3986. nClients++;
  3987. client->requestVector = client->swapped ? SwappedProcVector : ProcVector;
  3988. client->sequence = 0;
  3989. #ifdef XAPPGROUP
  3990. XagConnectionInfo (client, &lconnSetupPrefix, &lConnectionInfo, &numScreens);
  3991. #endif
  3992. ((xConnSetup *)lConnectionInfo)->ridBase = client->clientAsMask;
  3993. ((xConnSetup *)lConnectionInfo)->ridMask = RESOURCE_ID_MASK;
  3994. #ifdef MATCH_CLIENT_ENDIAN
  3995. ((xConnSetup *)lConnectionInfo)->imageByteOrder = ClientOrder (client);
  3996. ((xConnSetup *)lConnectionInfo)->bitmapBitOrder = ClientOrder (client);
  3997. #endif
  3998. /* fill in the "currentInputMask" */
  3999. root = (xWindowRoot *)(lConnectionInfo + connBlockScreenStart);
  4000. #ifdef PANORAMIX
  4001. if (noPanoramiXExtension)
  4002. numScreens = screenInfo.numScreens;
  4003. else
  4004. numScreens = ((xConnSetup *)ConnectionInfo)->numRoots;
  4005. #endif
  4006. for (i=0; i<numScreens; i++)
  4007. {
  4008. register unsigned int j;
  4009. register xDepth *pDepth;
  4010. root->currentInputMask = WindowTable[i]->eventMask |
  4011. wOtherEventMasks (WindowTable[i]);
  4012. pDepth = (xDepth *)(root + 1);
  4013. for (j = 0; j < root->nDepths; j++)
  4014. {
  4015. pDepth = (xDepth *)(((char *)(pDepth + 1)) +
  4016. pDepth->nVisuals * sizeof(xVisualType));
  4017. }
  4018. root = (xWindowRoot *)pDepth;
  4019. }
  4020. if (client->swapped)
  4021. {
  4022. WriteSConnSetupPrefix(client, lconnSetupPrefix);
  4023. WriteSConnectionInfo(client,
  4024. (unsigned long)(lconnSetupPrefix->length << 2),
  4025. lConnectionInfo);
  4026. }
  4027. else
  4028. {
  4029. (void)WriteToClient(client, sizeof(xConnSetupPrefix),
  4030. (char *) lconnSetupPrefix);
  4031. (void)WriteToClient(client, (int)(lconnSetupPrefix->length << 2),
  4032. lConnectionInfo);
  4033. }
  4034. client->clientState = ClientStateRunning;
  4035. if (ClientStateCallback)
  4036. {
  4037. NewClientInfoRec clientinfo;
  4038. clientinfo.client = client;
  4039. clientinfo.prefix = lconnSetupPrefix;
  4040. clientinfo.setup = (xConnSetup *)lConnectionInfo;
  4041. CallCallbacks((&ClientStateCallback), (pointer)&clientinfo);
  4042. }
  4043. return (client->noClientException);
  4044. }
  4045. int
  4046. ProcEstablishConnection(register ClientPtr client)
  4047. {
  4048. char *reason, *auth_proto, *auth_string;
  4049. register xConnClientPrefix *prefix;
  4050. REQUEST(xReq);
  4051. prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  4052. auth_proto = (char *)prefix + sz_xConnClientPrefix;
  4053. auth_string = auth_proto + ((prefix->nbytesAuthProto + 3) & ~3);
  4054. if ((prefix->majorVersion != X_PROTOCOL) ||
  4055. (prefix->minorVersion != X_PROTOCOL_REVISION))
  4056. reason = "Protocol version mismatch";
  4057. else
  4058. reason = ClientAuthorized(client,
  4059. (unsigned short)prefix->nbytesAuthProto,
  4060. auth_proto,
  4061. (unsigned short)prefix->nbytesAuthString,
  4062. auth_string);
  4063. /*
  4064. * If Kerberos is being used for this client, the clientState
  4065. * will be set to ClientStateAuthenticating at this point.
  4066. * More messages need to be exchanged among the X server, Kerberos
  4067. * server, and client to figure out if everyone is authorized.
  4068. * So we don't want to send the connection setup info yet, since
  4069. * the auth step isn't really done.
  4070. */
  4071. if (client->clientState == ClientStateCheckingSecurity)
  4072. client->clientState = ClientStateCheckedSecurity;
  4073. else if (client->clientState != ClientStateAuthenticating)
  4074. return(SendConnSetup(client, reason));
  4075. return(client->noClientException);
  4076. }
  4077. void
  4078. SendErrorToClient(ClientPtr client, unsigned majorCode, unsigned minorCode,
  4079. XID resId, int errorCode)
  4080. {
  4081. xError rep;
  4082. rep.type = X_Error;
  4083. rep.sequenceNumber = client->sequence;
  4084. rep.errorCode = errorCode;
  4085. rep.majorCode = majorCode;
  4086. rep.minorCode = minorCode;
  4087. rep.resourceID = resId;
  4088. WriteEventsToClient (client, 1, (xEvent *)&rep);
  4089. }
  4090. void
  4091. DeleteWindowFromAnySelections(WindowPtr pWin)
  4092. {
  4093. register int i;
  4094. for (i = 0; i< NumCurrentSelections; i++)
  4095. if (CurrentSelections[i].pWin == pWin)
  4096. {
  4097. if (SelectionCallback)
  4098. {
  4099. SelectionInfoRec info;
  4100. info.selection = &CurrentSelections[i];
  4101. info.kind = SelectionWindowDestroy;
  4102. CallCallbacks(&SelectionCallback, &info);
  4103. }
  4104. CurrentSelections[i].pWin = (WindowPtr)NULL;
  4105. CurrentSelections[i].window = None;
  4106. CurrentSelections[i].client = NullClient;
  4107. }
  4108. }
  4109. static void
  4110. DeleteClientFromAnySelections(ClientPtr client)
  4111. {
  4112. register int i;
  4113. for (i = 0; i< NumCurrentSelections; i++)
  4114. if (CurrentSelections[i].client == client)
  4115. {
  4116. if (SelectionCallback)
  4117. {
  4118. SelectionInfoRec info;
  4119. info.selection = &CurrentSelections[i];
  4120. info.kind = SelectionWindowDestroy;
  4121. CallCallbacks(&SelectionCallback, &info);
  4122. }
  4123. CurrentSelections[i].pWin = (WindowPtr)NULL;
  4124. CurrentSelections[i].window = None;
  4125. CurrentSelections[i].client = NullClient;
  4126. }
  4127. }
  4128. void
  4129. MarkClientException(ClientPtr client)
  4130. {
  4131. client->noClientException = -1;
  4132. }