dispatch.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872
  1. /************************************************************
  2. Copyright 1987, 1989, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ********************************************************/
  36. /* The panoramix components contained the following notice */
  37. /*****************************************************************
  38. Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
  39. Permission is hereby granted, free of charge, to any person obtaining a copy
  40. of this software and associated documentation files (the "Software"), to deal
  41. in the Software without restriction, including without limitation the rights
  42. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  43. copies of the Software.
  44. The above copyright notice and this permission notice shall be included in
  45. all copies or substantial portions of the Software.
  46. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  47. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  48. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  49. DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
  50. BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
  51. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  52. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  53. Except as contained in this notice, the name of Digital Equipment Corporation
  54. shall not be used in advertising or otherwise to promote the sale, use or other
  55. dealings in this Software without prior written authorization from Digital
  56. Equipment Corporation.
  57. ******************************************************************/
  58. #ifdef HAVE_DIX_CONFIG_H
  59. #include <dix-config.h>
  60. #endif
  61. #include "windowstr.h"
  62. #include <X11/fonts/fontstruct.h>
  63. #include "dixfontstr.h"
  64. #include "gcstruct.h"
  65. #include "selection.h"
  66. #include "colormapst.h"
  67. #include "cursorstr.h"
  68. #include "scrnintstr.h"
  69. #include "opaque.h"
  70. #include "input.h"
  71. #include "servermd.h"
  72. #include "extnsionst.h"
  73. #include "dixfont.h"
  74. #include "dispatch.h"
  75. #include "swaprep.h"
  76. #include "swapreq.h"
  77. #define mskcnt ((MAXCLIENTS + 31) / 32)
  78. #define BITMASK(i) (1U << ((i) & 31))
  79. #define MASKIDX(i) ((i) >> 5)
  80. #define MASKWORD(buf, i) buf[MASKIDX(i)]
  81. #define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
  82. #define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
  83. #define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
  84. extern xConnSetupPrefix connSetupPrefix;
  85. extern char *ConnectionInfo;
  86. _X_EXPORT Selection *CurrentSelections;
  87. _X_EXPORT int NumCurrentSelections;
  88. CallbackListPtr SelectionCallback = NULL;
  89. static ClientPtr grabClient;
  90. #define GrabNone 0
  91. #define GrabActive 1
  92. #define GrabKickout 2
  93. static int grabState = GrabNone;
  94. static long grabWaiters[mskcnt];
  95. _X_EXPORT CallbackListPtr ServerGrabCallback = NULL;
  96. HWEventQueuePtr checkForInput[2];
  97. extern int connBlockScreenStart;
  98. static void KillAllClients(void);
  99. static void DeleteClientFromAnySelections(ClientPtr client);
  100. static int nextFreeClientID; /* always MIN free client ID */
  101. static int nClients; /* number of authorized clients */
  102. _X_EXPORT CallbackListPtr ClientStateCallback;
  103. /* dispatchException & isItTimeToYield must be declared volatile since they
  104. * are modified by signal handlers - otherwise optimizer may assume it doesn't
  105. * need to actually check value in memory when used and may miss changes from
  106. * signal handlers.
  107. */
  108. _X_EXPORT volatile char dispatchException = 0;
  109. _X_EXPORT volatile char isItTimeToYield;
  110. /* Various of the DIX function interfaces were not designed to allow
  111. * the client->errorValue to be set on BadValue and other errors.
  112. * Rather than changing interfaces and breaking untold code we introduce
  113. * a new global that dispatch can use.
  114. */
  115. XID clientErrorValue; /* XXX this is a kludge */
  116. #define SAME_SCREENS(a, b) (\
  117. (a.pScreen == b.pScreen))
  118. _X_EXPORT void
  119. SetInputCheck(HWEventQueuePtr c0, HWEventQueuePtr c1)
  120. {
  121. checkForInput[0] = c0;
  122. checkForInput[1] = c1;
  123. }
  124. _X_EXPORT void
  125. UpdateCurrentTime()
  126. {
  127. TimeStamp systime;
  128. /* To avoid time running backwards, we must call GetTimeInMillis before
  129. * calling ProcessInputEvents.
  130. */
  131. systime.months = currentTime.months;
  132. systime.milliseconds = GetTimeInMillis();
  133. if (systime.milliseconds < currentTime.milliseconds)
  134. systime.months++;
  135. if (*checkForInput[0] != *checkForInput[1])
  136. ProcessInputEvents();
  137. if (CompareTimeStamps(systime, currentTime) == LATER)
  138. currentTime = systime;
  139. }
  140. /* Like UpdateCurrentTime, but can't call ProcessInputEvents */
  141. _X_EXPORT void
  142. UpdateCurrentTimeIf()
  143. {
  144. TimeStamp systime;
  145. systime.months = currentTime.months;
  146. systime.milliseconds = GetTimeInMillis();
  147. if (systime.milliseconds < currentTime.milliseconds)
  148. systime.months++;
  149. if (*checkForInput[0] == *checkForInput[1])
  150. currentTime = systime;
  151. }
  152. void
  153. InitSelections()
  154. {
  155. if (CurrentSelections)
  156. free(CurrentSelections);
  157. CurrentSelections = (Selection *) NULL;
  158. NumCurrentSelections = 0;
  159. }
  160. void
  161. FlushClientCaches(XID id)
  162. {
  163. int i;
  164. ClientPtr client;
  165. client = clients[CLIENT_ID(id)];
  166. if (client == NullClient)
  167. return;
  168. for (i = 0; i < currentMaxClients; i++) {
  169. client = clients[i];
  170. if (client != NullClient) {
  171. if (client->lastDrawableID == id) {
  172. client->lastDrawableID = WindowTable[0]->drawable.id;
  173. client->lastDrawable = (DrawablePtr) WindowTable[0];
  174. }
  175. else if (client->lastGCID == id) {
  176. client->lastGCID = INVALID;
  177. client->lastGC = (GCPtr) NULL;
  178. }
  179. }
  180. }
  181. }
  182. #ifdef SMART_SCHEDULE
  183. #undef SMART_DEBUG
  184. #define SMART_SCHEDULE_DEFAULT_INTERVAL 7 /* ms */
  185. #define SMART_SCHEDULE_MAX_SLICE 20 /* ms */
  186. Bool SmartScheduleDisable = FALSE;
  187. long SmartScheduleSlice = SMART_SCHEDULE_DEFAULT_INTERVAL;
  188. long SmartScheduleInterval = SMART_SCHEDULE_DEFAULT_INTERVAL;
  189. long SmartScheduleMaxSlice = SMART_SCHEDULE_MAX_SLICE;
  190. long SmartScheduleTime;
  191. ClientPtr SmartLastClient;
  192. int SmartLastIndex[SMART_MAX_PRIORITY - SMART_MIN_PRIORITY + 1];
  193. int SmartScheduleClient(int *clientReady, int nready);
  194. #ifdef SMART_DEBUG
  195. long SmartLastPrint;
  196. #endif
  197. void Dispatch(void);
  198. int
  199. SmartScheduleClient(int *clientReady, int nready)
  200. {
  201. ClientPtr pClient;
  202. int i;
  203. int client;
  204. int bestPrio, best = 0;
  205. int bestRobin, robin;
  206. long now = SmartScheduleTime;
  207. long idle;
  208. bestPrio = -0x7fffffff;
  209. bestRobin = 0;
  210. idle = 2 * SmartScheduleSlice;
  211. for (i = 0; i < nready; i++) {
  212. client = clientReady[i];
  213. pClient = clients[client];
  214. /* Praise clients which are idle */
  215. if ((now - pClient->smart_check_tick) >= idle) {
  216. if (pClient->smart_priority < 0)
  217. pClient->smart_priority++;
  218. }
  219. pClient->smart_check_tick = now;
  220. /* check priority to select best client */
  221. robin =
  222. (pClient->index -
  223. SmartLastIndex[pClient->smart_priority -
  224. SMART_MIN_PRIORITY]) & 0xff;
  225. if (pClient->smart_priority > bestPrio ||
  226. (pClient->smart_priority == bestPrio && robin > bestRobin)) {
  227. bestPrio = pClient->smart_priority;
  228. bestRobin = robin;
  229. best = client;
  230. }
  231. #ifdef SMART_DEBUG
  232. if ((now - SmartLastPrint) >= 5000)
  233. fprintf(stderr, " %2d: %3d", client, pClient->smart_priority);
  234. #endif
  235. }
  236. #ifdef SMART_DEBUG
  237. if ((now - SmartLastPrint) >= 5000) {
  238. fprintf(stderr, " use %2d\n", best);
  239. SmartLastPrint = now;
  240. }
  241. #endif
  242. pClient = clients[best];
  243. SmartLastIndex[bestPrio - SMART_MIN_PRIORITY] = pClient->index;
  244. /*
  245. * Set current client pointer
  246. */
  247. if (SmartLastClient != pClient) {
  248. pClient->smart_start_tick = now;
  249. SmartLastClient = pClient;
  250. }
  251. /*
  252. * Adjust slice
  253. */
  254. if (nready == 1) {
  255. /*
  256. * If it's been a long time since another client
  257. * has run, bump the slice up to get maximal
  258. * performance from a single client
  259. */
  260. if ((now - pClient->smart_start_tick) > 1000 &&
  261. SmartScheduleSlice < SmartScheduleMaxSlice) {
  262. SmartScheduleSlice += SmartScheduleInterval;
  263. }
  264. }
  265. else {
  266. SmartScheduleSlice = SmartScheduleInterval;
  267. }
  268. return best;
  269. }
  270. #endif
  271. #define MAJOROP ((xReq *)client->requestBuffer)->reqType
  272. void
  273. Dispatch(void)
  274. {
  275. int *clientReady; /* array of request ready clients */
  276. int result;
  277. ClientPtr client;
  278. int nready;
  279. HWEventQueuePtr *icheck = checkForInput;
  280. #ifdef SMART_SCHEDULE
  281. long start_tick;
  282. #endif
  283. nextFreeClientID = 1;
  284. InitSelections();
  285. nClients = 0;
  286. clientReady = (int *) ALLOCATE_LOCAL(sizeof(int) * MaxClients);
  287. if (!clientReady)
  288. return;
  289. while (!dispatchException) {
  290. if (*icheck[0] != *icheck[1]) {
  291. ProcessInputEvents();
  292. FlushIfCriticalOutputPending();
  293. }
  294. nready = WaitForSomething(clientReady);
  295. #ifdef SMART_SCHEDULE
  296. if (nready && !SmartScheduleDisable) {
  297. clientReady[0] = SmartScheduleClient(clientReady, nready);
  298. nready = 1;
  299. }
  300. #endif
  301. /*****************
  302. * Handle events in round robin fashion, doing input between
  303. * each round
  304. *****************/
  305. while (!dispatchException && (--nready >= 0)) {
  306. client = clients[clientReady[nready]];
  307. if (!client) {
  308. /* KillClient can cause this to happen */
  309. continue;
  310. }
  311. /* GrabServer activation can cause this to be true */
  312. if (grabState == GrabKickout) {
  313. grabState = GrabActive;
  314. break;
  315. }
  316. isItTimeToYield = FALSE;
  317. requestingClient = client;
  318. #ifdef SMART_SCHEDULE
  319. start_tick = SmartScheduleTime;
  320. #endif
  321. while (!isItTimeToYield) {
  322. if (*icheck[0] != *icheck[1]) {
  323. ProcessInputEvents();
  324. FlushIfCriticalOutputPending();
  325. }
  326. #ifdef SMART_SCHEDULE
  327. if (!SmartScheduleDisable &&
  328. (SmartScheduleTime - start_tick) >= SmartScheduleSlice) {
  329. /* Penalize clients which consume ticks */
  330. if (client->smart_priority > SMART_MIN_PRIORITY)
  331. client->smart_priority--;
  332. break;
  333. }
  334. #endif
  335. /* now, finally, deal with client requests */
  336. result = ReadRequestFromClient(client);
  337. if (result <= 0) {
  338. if (result < 0)
  339. CloseDownClient(client);
  340. break;
  341. }
  342. client->sequence++;
  343. #ifdef DEBUG
  344. if (client->requestLogIndex == MAX_REQUEST_LOG)
  345. client->requestLogIndex = 0;
  346. client->requestLog[client->requestLogIndex] = MAJOROP;
  347. client->requestLogIndex++;
  348. #endif
  349. if (result > (maxBigRequestSize << 2))
  350. result = BadLength;
  351. else
  352. result = (*client->requestVector[MAJOROP]) (client);
  353. if (result != Success) {
  354. if (client->noClientException != Success)
  355. CloseDownClient(client);
  356. else
  357. SendErrorToClient(client, MAJOROP,
  358. MinorOpcodeOfRequest(client),
  359. client->errorValue, result);
  360. break;
  361. }
  362. }
  363. FlushAllOutput();
  364. #ifdef SMART_SCHEDULE
  365. client = clients[clientReady[nready]];
  366. if (client)
  367. client->smart_stop_tick = SmartScheduleTime;
  368. #endif
  369. requestingClient = NULL;
  370. }
  371. dispatchException &= ~DE_PRIORITYCHANGE;
  372. }
  373. KillAllClients();
  374. DEALLOCATE_LOCAL(clientReady);
  375. dispatchException &= ~DE_RESET;
  376. ResetOsBuffers();
  377. }
  378. #undef MAJOROP
  379. _X_EXPORT int
  380. ProcBadRequest(ClientPtr client)
  381. {
  382. return (BadRequest);
  383. }
  384. int
  385. ProcCreateWindow(ClientPtr client)
  386. {
  387. WindowPtr pParent, pWin;
  388. REQUEST(xCreateWindowReq);
  389. int result;
  390. int len;
  391. REQUEST_AT_LEAST_SIZE(xCreateWindowReq);
  392. LEGAL_NEW_RESOURCE(stuff->wid, client);
  393. if (!(pParent = (WindowPtr) SecurityLookupWindow(stuff->parent, client,
  394. SecurityWriteAccess)))
  395. return BadWindow;
  396. len = client->req_len - (sizeof(xCreateWindowReq) >> 2);
  397. if (Ones(stuff->mask) != len)
  398. return BadLength;
  399. if (!stuff->width || !stuff->height) {
  400. client->errorValue = 0;
  401. return BadValue;
  402. }
  403. pWin = CreateWindow(stuff->wid, pParent, stuff->x,
  404. stuff->y, stuff->width, stuff->height,
  405. stuff->borderWidth, stuff->class,
  406. stuff->mask, (XID *) &stuff[1],
  407. (int) stuff->depth, client, stuff->visual, &result);
  408. if (pWin) {
  409. Mask mask = pWin->eventMask;
  410. pWin->eventMask = 0; /* subterfuge in case AddResource fails */
  411. if (!AddResource(stuff->wid, RT_WINDOW, (pointer) pWin))
  412. return BadAlloc;
  413. pWin->eventMask = mask;
  414. }
  415. if (client->noClientException != Success)
  416. return (client->noClientException);
  417. else
  418. return (result);
  419. }
  420. int
  421. ProcChangeWindowAttributes(register ClientPtr client)
  422. {
  423. WindowPtr pWin;
  424. REQUEST(xChangeWindowAttributesReq);
  425. int result;
  426. int len;
  427. REQUEST_AT_LEAST_SIZE(xChangeWindowAttributesReq);
  428. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  429. SecurityWriteAccess);
  430. if (!pWin)
  431. return (BadWindow);
  432. len = client->req_len - (sizeof(xChangeWindowAttributesReq) >> 2);
  433. if (len != Ones(stuff->valueMask))
  434. return BadLength;
  435. result = ChangeWindowAttributes(pWin,
  436. stuff->valueMask,
  437. (XID *) &stuff[1], client);
  438. if (client->noClientException != Success)
  439. return (client->noClientException);
  440. else
  441. return (result);
  442. }
  443. int
  444. ProcGetWindowAttributes(register ClientPtr client)
  445. {
  446. WindowPtr pWin;
  447. REQUEST(xResourceReq);
  448. xGetWindowAttributesReply wa = {0};
  449. REQUEST_SIZE_MATCH(xResourceReq);
  450. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  451. SecurityReadAccess);
  452. if (!pWin)
  453. return (BadWindow);
  454. memset(&wa, 0, sizeof(xGetWindowAttributesReply));
  455. GetWindowAttributes(pWin, client, &wa);
  456. WriteReplyToClient(client, sizeof(xGetWindowAttributesReply), &wa);
  457. return (client->noClientException);
  458. }
  459. int
  460. ProcDestroyWindow(register ClientPtr client)
  461. {
  462. WindowPtr pWin;
  463. REQUEST(xResourceReq);
  464. REQUEST_SIZE_MATCH(xResourceReq);
  465. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  466. SecurityDestroyAccess);
  467. if (!pWin)
  468. return (BadWindow);
  469. if (pWin->parent)
  470. FreeResource(stuff->id, RT_NONE);
  471. return (client->noClientException);
  472. }
  473. int
  474. ProcDestroySubwindows(register ClientPtr client)
  475. {
  476. WindowPtr pWin;
  477. REQUEST(xResourceReq);
  478. REQUEST_SIZE_MATCH(xResourceReq);
  479. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  480. SecurityDestroyAccess);
  481. if (!pWin)
  482. return (BadWindow);
  483. DestroySubwindows(pWin, client);
  484. return (client->noClientException);
  485. }
  486. int
  487. ProcChangeSaveSet(register ClientPtr client)
  488. {
  489. WindowPtr pWin;
  490. REQUEST(xChangeSaveSetReq);
  491. int result;
  492. REQUEST_SIZE_MATCH(xChangeSaveSetReq);
  493. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  494. SecurityReadAccess);
  495. if (!pWin)
  496. return (BadWindow);
  497. if (client->clientAsMask == (CLIENT_BITS(pWin->drawable.id)))
  498. return BadMatch;
  499. if ((stuff->mode == SetModeInsert) || (stuff->mode == SetModeDelete)) {
  500. result = AlterSaveSetForClient(client, pWin, stuff->mode, FALSE, TRUE);
  501. if (client->noClientException != Success)
  502. return (client->noClientException);
  503. else
  504. return (result);
  505. }
  506. else {
  507. client->errorValue = stuff->mode;
  508. return (BadValue);
  509. }
  510. }
  511. int
  512. ProcReparentWindow(register ClientPtr client)
  513. {
  514. WindowPtr pWin, pParent;
  515. REQUEST(xReparentWindowReq);
  516. int result;
  517. REQUEST_SIZE_MATCH(xReparentWindowReq);
  518. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  519. SecurityWriteAccess);
  520. if (!pWin)
  521. return (BadWindow);
  522. pParent = (WindowPtr) SecurityLookupWindow(stuff->parent, client,
  523. SecurityWriteAccess);
  524. if (!pParent)
  525. return (BadWindow);
  526. if (SAME_SCREENS(pWin->drawable, pParent->drawable)) {
  527. if ((pWin->backgroundState == ParentRelative) &&
  528. (pParent->drawable.depth != pWin->drawable.depth))
  529. return BadMatch;
  530. if ((pWin->drawable.class != InputOnly) &&
  531. (pParent->drawable.class == InputOnly))
  532. return BadMatch;
  533. result = ReparentWindow(pWin, pParent,
  534. (short) stuff->x, (short) stuff->y, client);
  535. if (client->noClientException != Success)
  536. return (client->noClientException);
  537. else
  538. return (result);
  539. }
  540. else
  541. return (BadMatch);
  542. }
  543. int
  544. ProcMapWindow(register ClientPtr client)
  545. {
  546. WindowPtr pWin;
  547. REQUEST(xResourceReq);
  548. REQUEST_SIZE_MATCH(xResourceReq);
  549. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  550. SecurityReadAccess);
  551. if (!pWin)
  552. return (BadWindow);
  553. MapWindow(pWin, client);
  554. /* update cache to say it is mapped */
  555. return (client->noClientException);
  556. }
  557. int
  558. ProcMapSubwindows(register ClientPtr client)
  559. {
  560. WindowPtr pWin;
  561. REQUEST(xResourceReq);
  562. REQUEST_SIZE_MATCH(xResourceReq);
  563. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  564. SecurityReadAccess);
  565. if (!pWin)
  566. return (BadWindow);
  567. MapSubwindows(pWin, client);
  568. /* update cache to say it is mapped */
  569. return (client->noClientException);
  570. }
  571. int
  572. ProcUnmapWindow(register ClientPtr client)
  573. {
  574. WindowPtr pWin;
  575. REQUEST(xResourceReq);
  576. REQUEST_SIZE_MATCH(xResourceReq);
  577. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  578. SecurityReadAccess);
  579. if (!pWin)
  580. return (BadWindow);
  581. UnmapWindow(pWin, FALSE);
  582. /* update cache to say it is mapped */
  583. return (client->noClientException);
  584. }
  585. int
  586. ProcUnmapSubwindows(register ClientPtr client)
  587. {
  588. WindowPtr pWin;
  589. REQUEST(xResourceReq);
  590. REQUEST_SIZE_MATCH(xResourceReq);
  591. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  592. SecurityReadAccess);
  593. if (!pWin)
  594. return (BadWindow);
  595. UnmapSubwindows(pWin);
  596. return (client->noClientException);
  597. }
  598. int
  599. ProcConfigureWindow(register ClientPtr client)
  600. {
  601. WindowPtr pWin;
  602. REQUEST(xConfigureWindowReq);
  603. int result;
  604. int len;
  605. REQUEST_AT_LEAST_SIZE(xConfigureWindowReq);
  606. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  607. SecurityWriteAccess);
  608. if (!pWin)
  609. return (BadWindow);
  610. len = client->req_len - (sizeof(xConfigureWindowReq) >> 2);
  611. if (Ones((Mask) stuff->mask) != len)
  612. return BadLength;
  613. result = ConfigureWindow(pWin, (Mask) stuff->mask, (XID *) &stuff[1],
  614. client);
  615. if (client->noClientException != Success)
  616. return (client->noClientException);
  617. else
  618. return (result);
  619. }
  620. int
  621. ProcCirculateWindow(register ClientPtr client)
  622. {
  623. WindowPtr pWin;
  624. REQUEST(xCirculateWindowReq);
  625. REQUEST_SIZE_MATCH(xCirculateWindowReq);
  626. if ((stuff->direction != RaiseLowest) && (stuff->direction != LowerHighest)) {
  627. client->errorValue = stuff->direction;
  628. return BadValue;
  629. }
  630. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  631. SecurityWriteAccess);
  632. if (!pWin)
  633. return (BadWindow);
  634. CirculateWindow(pWin, (int) stuff->direction, client);
  635. return (client->noClientException);
  636. }
  637. int
  638. GetGeometry(register ClientPtr client, xGetGeometryReply * rep)
  639. {
  640. DrawablePtr pDraw;
  641. REQUEST(xResourceReq);
  642. REQUEST_SIZE_MATCH(xResourceReq);
  643. SECURITY_VERIFY_GEOMETRABLE(pDraw, stuff->id, client, SecurityReadAccess);
  644. rep->type = X_Reply;
  645. rep->length = 0;
  646. rep->sequenceNumber = client->sequence;
  647. rep->root = WindowTable[pDraw->pScreen->myNum]->drawable.id;
  648. rep->depth = pDraw->depth;
  649. rep->width = pDraw->width;
  650. rep->height = pDraw->height;
  651. /* XXX - Because the pixmap-implementation of the multibuffer extension
  652. * may have the buffer-id's drawable resource value be a pointer
  653. * to the buffer's window instead of the buffer itself
  654. * (this happens if the buffer is the displayed buffer),
  655. * we also have to check that the id matches before we can
  656. * truly say that it is a DRAWABLE_WINDOW.
  657. */
  658. if ((pDraw->type == UNDRAWABLE_WINDOW) ||
  659. ((pDraw->type == DRAWABLE_WINDOW) && (stuff->id == pDraw->id))) {
  660. WindowPtr pWin = (WindowPtr) pDraw;
  661. rep->x = pWin->origin.x - wBorderWidth(pWin);
  662. rep->y = pWin->origin.y - wBorderWidth(pWin);
  663. rep->borderWidth = pWin->borderWidth;
  664. }
  665. else { /* DRAWABLE_PIXMAP or DRAWABLE_BUFFER */
  666. rep->x = rep->y = rep->borderWidth = 0;
  667. }
  668. return Success;
  669. }
  670. int
  671. ProcGetGeometry(register ClientPtr client)
  672. {
  673. xGetGeometryReply rep = {0};
  674. int status;
  675. memset(&rep, 0, sizeof(xGetGeometryReply));
  676. if ((status = GetGeometry(client, &rep)) != Success)
  677. return status;
  678. WriteReplyToClient(client, sizeof(xGetGeometryReply), &rep);
  679. return (client->noClientException);
  680. }
  681. int
  682. ProcQueryTree(register ClientPtr client)
  683. {
  684. xQueryTreeReply reply;
  685. int numChildren = 0;
  686. WindowPtr pChild, pWin, pHead;
  687. Window *childIDs = (Window *) NULL;
  688. REQUEST(xResourceReq);
  689. REQUEST_SIZE_MATCH(xResourceReq);
  690. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  691. SecurityReadAccess);
  692. if (!pWin)
  693. return (BadWindow);
  694. memset(&reply, 0, sizeof(xQueryTreeReply));
  695. reply.type = X_Reply;
  696. reply.root = WindowTable[pWin->drawable.pScreen->myNum]->drawable.id;
  697. reply.sequenceNumber = client->sequence;
  698. if (pWin->parent)
  699. reply.parent = pWin->parent->drawable.id;
  700. else
  701. reply.parent = (Window) None;
  702. pHead = RealChildHead(pWin);
  703. for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  704. numChildren++;
  705. if (numChildren) {
  706. int curChild = 0;
  707. childIDs = (Window *) ALLOCATE_LOCAL(numChildren * sizeof(Window));
  708. if (!childIDs)
  709. return BadAlloc;
  710. for (pChild = pWin->lastChild; pChild != pHead;
  711. pChild = pChild->prevSib)
  712. childIDs[curChild++] = pChild->drawable.id;
  713. }
  714. reply.nChildren = numChildren;
  715. reply.length = (numChildren * sizeof(Window)) >> 2;
  716. WriteReplyToClient(client, sizeof(xQueryTreeReply), &reply);
  717. if (numChildren) {
  718. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  719. WriteSwappedDataToClient(client, numChildren * sizeof(Window),
  720. childIDs);
  721. DEALLOCATE_LOCAL(childIDs);
  722. }
  723. return (client->noClientException);
  724. }
  725. int
  726. ProcInternAtom(register ClientPtr client)
  727. {
  728. Atom atom;
  729. char *tchar;
  730. REQUEST(xInternAtomReq);
  731. REQUEST_FIXED_SIZE(xInternAtomReq, stuff->nbytes);
  732. if ((stuff->onlyIfExists != xTrue) && (stuff->onlyIfExists != xFalse)) {
  733. client->errorValue = stuff->onlyIfExists;
  734. return (BadValue);
  735. }
  736. tchar = (char *) &stuff[1];
  737. atom = MakeAtom(tchar, stuff->nbytes, !stuff->onlyIfExists);
  738. if (atom != BAD_RESOURCE) {
  739. xInternAtomReply reply = {0};
  740. memset(&reply, 0, sizeof(xInternAtomReply));
  741. reply.type = X_Reply;
  742. reply.length = 0;
  743. reply.sequenceNumber = client->sequence;
  744. reply.atom = atom;
  745. WriteReplyToClient(client, sizeof(xInternAtomReply), &reply);
  746. return (client->noClientException);
  747. }
  748. else
  749. return (BadAlloc);
  750. }
  751. int
  752. ProcGetAtomName(register ClientPtr client)
  753. {
  754. char *str;
  755. xGetAtomNameReply reply;
  756. int len;
  757. REQUEST(xResourceReq);
  758. REQUEST_SIZE_MATCH(xResourceReq);
  759. if ((str = NameForAtom(stuff->id))) {
  760. len = strlen(str);
  761. memset(&reply, 0, sizeof(xGetAtomNameReply));
  762. reply.type = X_Reply;
  763. reply.length = (len + 3) >> 2;
  764. reply.sequenceNumber = client->sequence;
  765. reply.nameLength = len;
  766. WriteReplyToClient(client, sizeof(xGetAtomNameReply), &reply);
  767. (void) WriteToClient(client, len, str);
  768. return (client->noClientException);
  769. }
  770. else {
  771. client->errorValue = stuff->id;
  772. return (BadAtom);
  773. }
  774. }
  775. int
  776. ProcSetSelectionOwner(register ClientPtr client)
  777. {
  778. WindowPtr pWin;
  779. TimeStamp time;
  780. REQUEST(xSetSelectionOwnerReq);
  781. REQUEST_SIZE_MATCH(xSetSelectionOwnerReq);
  782. UpdateCurrentTime();
  783. time = ClientTimeToServerTime(stuff->time);
  784. /* If the client's time stamp is in the future relative to the server's
  785. time stamp, do not set the selection, just return success. */
  786. if (CompareTimeStamps(time, currentTime) == LATER)
  787. return Success;
  788. if (stuff->window != None) {
  789. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  790. SecurityReadAccess);
  791. if (!pWin)
  792. return (BadWindow);
  793. }
  794. else
  795. pWin = (WindowPtr) None;
  796. if (ValidAtom(stuff->selection)) {
  797. int i = 0;
  798. /*
  799. * First, see if the selection is already set...
  800. */
  801. while ((i < NumCurrentSelections) &&
  802. CurrentSelections[i].selection != stuff->selection)
  803. i++;
  804. if (i < NumCurrentSelections) {
  805. xEvent event;
  806. /* If the timestamp in client's request is in the past relative
  807. to the time stamp indicating the last time the owner of the
  808. selection was set, do not set the selection, just return
  809. success. */
  810. if (CompareTimeStamps(time, CurrentSelections[i].lastTimeChanged)
  811. == EARLIER)
  812. return Success;
  813. if (CurrentSelections[i].client &&
  814. (!pWin || (CurrentSelections[i].client != client))) {
  815. event.u.u.type = SelectionClear;
  816. event.u.selectionClear.time = time.milliseconds;
  817. event.u.selectionClear.window = CurrentSelections[i].window;
  818. event.u.selectionClear.atom = CurrentSelections[i].selection;
  819. (void) TryClientEvents(CurrentSelections[i].client, &event, 1,
  820. NoEventMask,
  821. NoEventMask /* CantBeFiltered */ ,
  822. NullGrab);
  823. }
  824. }
  825. else {
  826. /*
  827. * It doesn't exist, so add it...
  828. */
  829. Selection *newsels;
  830. if (i == 0)
  831. newsels = malloc(sizeof(Selection));
  832. else
  833. newsels = (Selection *) realloc(CurrentSelections,
  834. (NumCurrentSelections +
  835. 1) * sizeof(Selection));
  836. if (!newsels)
  837. return BadAlloc;
  838. NumCurrentSelections++;
  839. CurrentSelections = newsels;
  840. CurrentSelections[i].selection = stuff->selection;
  841. }
  842. CurrentSelections[i].lastTimeChanged = time;
  843. CurrentSelections[i].window = stuff->window;
  844. CurrentSelections[i].pWin = pWin;
  845. CurrentSelections[i].client = (pWin ? client : NullClient);
  846. if (SelectionCallback) {
  847. SelectionInfoRec info;
  848. info.selection = &CurrentSelections[i];
  849. info.kind = SelectionSetOwner;
  850. CallCallbacks(&SelectionCallback, &info);
  851. }
  852. return (client->noClientException);
  853. }
  854. else {
  855. client->errorValue = stuff->selection;
  856. return (BadAtom);
  857. }
  858. }
  859. int
  860. ProcGetSelectionOwner(register ClientPtr client)
  861. {
  862. REQUEST(xResourceReq);
  863. REQUEST_SIZE_MATCH(xResourceReq);
  864. if (ValidAtom(stuff->id)) {
  865. int i;
  866. xGetSelectionOwnerReply reply;
  867. i = 0;
  868. while ((i < NumCurrentSelections) &&
  869. CurrentSelections[i].selection != stuff->id)
  870. i++;
  871. reply.type = X_Reply;
  872. reply.length = 0;
  873. reply.sequenceNumber = client->sequence;
  874. if (i < NumCurrentSelections)
  875. reply.owner = CurrentSelections[i].window;
  876. else
  877. reply.owner = None;
  878. WriteReplyToClient(client, sizeof(xGetSelectionOwnerReply), &reply);
  879. return (client->noClientException);
  880. }
  881. else {
  882. client->errorValue = stuff->id;
  883. return (BadAtom);
  884. }
  885. }
  886. int
  887. ProcConvertSelection(register ClientPtr client)
  888. {
  889. Bool paramsOkay;
  890. xEvent event;
  891. WindowPtr pWin;
  892. REQUEST(xConvertSelectionReq);
  893. REQUEST_SIZE_MATCH(xConvertSelectionReq);
  894. pWin = (WindowPtr) SecurityLookupWindow(stuff->requestor, client,
  895. SecurityReadAccess);
  896. if (!pWin)
  897. return (BadWindow);
  898. paramsOkay = (ValidAtom(stuff->selection) && ValidAtom(stuff->target));
  899. if (stuff->property != None)
  900. paramsOkay &= ValidAtom(stuff->property);
  901. if (paramsOkay) {
  902. int i;
  903. i = 0;
  904. while ((i < NumCurrentSelections) &&
  905. CurrentSelections[i].selection != stuff->selection)
  906. i++;
  907. if ((i < NumCurrentSelections) && (CurrentSelections[i].window != None)
  908. ) {
  909. event.u.u.type = SelectionRequest;
  910. event.u.selectionRequest.time = stuff->time;
  911. event.u.selectionRequest.owner = CurrentSelections[i].window;
  912. event.u.selectionRequest.requestor = stuff->requestor;
  913. event.u.selectionRequest.selection = stuff->selection;
  914. event.u.selectionRequest.target = stuff->target;
  915. event.u.selectionRequest.property = stuff->property;
  916. if (TryClientEvents
  917. (CurrentSelections[i].client, &event, 1, NoEventMask,
  918. NoEventMask /* CantBeFiltered */ , NullGrab))
  919. return (client->noClientException);
  920. }
  921. event.u.u.type = SelectionNotify;
  922. event.u.selectionNotify.time = stuff->time;
  923. event.u.selectionNotify.requestor = stuff->requestor;
  924. event.u.selectionNotify.selection = stuff->selection;
  925. event.u.selectionNotify.target = stuff->target;
  926. event.u.selectionNotify.property = None;
  927. (void) TryClientEvents(client, &event, 1, NoEventMask,
  928. NoEventMask /* CantBeFiltered */ , NullGrab);
  929. return (client->noClientException);
  930. }
  931. else {
  932. client->errorValue = stuff->property;
  933. return (BadAtom);
  934. }
  935. }
  936. int
  937. ProcGrabServer(register ClientPtr client)
  938. {
  939. REQUEST_SIZE_MATCH(xReq);
  940. if (grabState != GrabNone && client != grabClient) {
  941. ResetCurrentRequest(client);
  942. client->sequence--;
  943. BITSET(grabWaiters, client->index);
  944. IgnoreClient(client);
  945. return (client->noClientException);
  946. }
  947. OnlyListenToOneClient(client);
  948. grabState = GrabKickout;
  949. grabClient = client;
  950. if (ServerGrabCallback) {
  951. ServerGrabInfoRec grabinfo;
  952. grabinfo.client = client;
  953. grabinfo.grabstate = SERVER_GRABBED;
  954. CallCallbacks(&ServerGrabCallback, (pointer) &grabinfo);
  955. }
  956. return (client->noClientException);
  957. }
  958. static void
  959. UngrabServer(ClientPtr client)
  960. {
  961. int i;
  962. grabState = GrabNone;
  963. ListenToAllClients();
  964. for (i = mskcnt; --i >= 0 && !grabWaiters[i];);
  965. if (i >= 0) {
  966. i <<= 5;
  967. while (!GETBIT(grabWaiters, i))
  968. i++;
  969. BITCLEAR(grabWaiters, i);
  970. AttendClient(clients[i]);
  971. }
  972. if (ServerGrabCallback) {
  973. ServerGrabInfoRec grabinfo;
  974. grabinfo.client = client;
  975. grabinfo.grabstate = SERVER_UNGRABBED;
  976. CallCallbacks(&ServerGrabCallback, (pointer) &grabinfo);
  977. }
  978. }
  979. int
  980. ProcUngrabServer(register ClientPtr client)
  981. {
  982. REQUEST_SIZE_MATCH(xReq);
  983. UngrabServer(client);
  984. return (client->noClientException);
  985. }
  986. int
  987. ProcTranslateCoords(register ClientPtr client)
  988. {
  989. REQUEST(xTranslateCoordsReq);
  990. WindowPtr pWin, pDst;
  991. xTranslateCoordsReply rep;
  992. REQUEST_SIZE_MATCH(xTranslateCoordsReq);
  993. pWin = (WindowPtr) SecurityLookupWindow(stuff->srcWid, client,
  994. SecurityReadAccess);
  995. if (!pWin)
  996. return (BadWindow);
  997. pDst = (WindowPtr) SecurityLookupWindow(stuff->dstWid, client,
  998. SecurityReadAccess);
  999. if (!pDst)
  1000. return (BadWindow);
  1001. memset(&rep, 0, sizeof(xTranslateCoordsReply));
  1002. rep.type = X_Reply;
  1003. rep.length = 0;
  1004. rep.sequenceNumber = client->sequence;
  1005. if (!SAME_SCREENS(pWin->drawable, pDst->drawable)) {
  1006. rep.sameScreen = xFalse;
  1007. rep.child = None;
  1008. rep.dstX = rep.dstY = 0;
  1009. }
  1010. else {
  1011. INT16 x, y;
  1012. rep.sameScreen = xTrue;
  1013. rep.child = None;
  1014. /* computing absolute coordinates -- adjust to destination later */
  1015. x = pWin->drawable.x + stuff->srcX;
  1016. y = pWin->drawable.y + stuff->srcY;
  1017. pWin = pDst->firstChild;
  1018. while (pWin) {
  1019. BoxRec box;
  1020. if ((pWin->mapped) &&
  1021. (x >= pWin->drawable.x - wBorderWidth(pWin)) &&
  1022. (x < pWin->drawable.x + (int) pWin->drawable.width +
  1023. wBorderWidth(pWin)) &&
  1024. (y >= pWin->drawable.y - wBorderWidth(pWin)) &&
  1025. (y < pWin->drawable.y + (int) pWin->drawable.height +
  1026. wBorderWidth(pWin))
  1027. /* When a window is shaped, a further check
  1028. * is made to see if the point is inside
  1029. * borderSize
  1030. */
  1031. && (!wBoundingShape(pWin) ||
  1032. POINT_IN_REGION(
  1033. &pWin->borderSize, x, y, &box))
  1034. && (!wInputShape(pWin) ||
  1035. POINT_IN_REGION(
  1036. wInputShape(pWin),
  1037. x - pWin->drawable.x,
  1038. y - pWin->drawable.y, &box))
  1039. ) {
  1040. rep.child = pWin->drawable.id;
  1041. pWin = (WindowPtr) NULL;
  1042. }
  1043. else
  1044. pWin = pWin->nextSib;
  1045. }
  1046. /* adjust to destination coordinates */
  1047. rep.dstX = x - pDst->drawable.x;
  1048. rep.dstY = y - pDst->drawable.y;
  1049. }
  1050. WriteReplyToClient(client, sizeof(xTranslateCoordsReply), &rep);
  1051. return (client->noClientException);
  1052. }
  1053. int
  1054. ProcOpenFont(register ClientPtr client)
  1055. {
  1056. int err;
  1057. REQUEST(xOpenFontReq);
  1058. REQUEST_FIXED_SIZE(xOpenFontReq, stuff->nbytes);
  1059. client->errorValue = stuff->fid;
  1060. LEGAL_NEW_RESOURCE(stuff->fid, client);
  1061. err = OpenFont(client, stuff->fid, (Mask) 0,
  1062. stuff->nbytes, (char *) &stuff[1]);
  1063. if (err == Success) {
  1064. return (client->noClientException);
  1065. }
  1066. else
  1067. return err;
  1068. }
  1069. int
  1070. ProcCloseFont(register ClientPtr client)
  1071. {
  1072. FontPtr pFont;
  1073. REQUEST(xResourceReq);
  1074. REQUEST_SIZE_MATCH(xResourceReq);
  1075. pFont = (FontPtr) SecurityLookupIDByType(client, stuff->id, RT_FONT,
  1076. SecurityDestroyAccess);
  1077. if (pFont != (FontPtr) NULL) { /* id was valid */
  1078. FreeResource(stuff->id, RT_NONE);
  1079. return (client->noClientException);
  1080. }
  1081. else {
  1082. client->errorValue = stuff->id;
  1083. return (BadFont);
  1084. }
  1085. }
  1086. int
  1087. ProcQueryFont(register ClientPtr client)
  1088. {
  1089. xQueryFontReply *reply;
  1090. FontPtr pFont;
  1091. GC *pGC;
  1092. REQUEST(xResourceReq);
  1093. REQUEST_SIZE_MATCH(xResourceReq);
  1094. client->errorValue = stuff->id; /* EITHER font or gc */
  1095. pFont = (FontPtr) SecurityLookupIDByType(client, stuff->id, RT_FONT,
  1096. SecurityReadAccess);
  1097. if (!pFont) {
  1098. /* can't use VERIFY_GC because it might return BadGC */
  1099. pGC = (GC *) SecurityLookupIDByType(client, stuff->id, RT_GC,
  1100. SecurityReadAccess);
  1101. if (!pGC) {
  1102. client->errorValue = stuff->id;
  1103. return (BadFont); /* procotol spec says only error is BadFont */
  1104. }
  1105. pFont = pGC->font;
  1106. }
  1107. {
  1108. xCharInfo *pmax = FONTINKMAX(pFont);
  1109. xCharInfo *pmin = FONTINKMIN(pFont);
  1110. int nprotoxcistructs;
  1111. int rlength;
  1112. nprotoxcistructs = (pmax->rightSideBearing == pmin->rightSideBearing &&
  1113. pmax->leftSideBearing == pmin->leftSideBearing &&
  1114. pmax->descent == pmin->descent &&
  1115. pmax->ascent == pmin->ascent &&
  1116. pmax->characterWidth == pmin->characterWidth) ?
  1117. 0 : N2dChars(pFont);
  1118. rlength = sizeof(xQueryFontReply) +
  1119. FONTINFONPROPS(FONTCHARSET(pFont)) * sizeof(xFontProp) +
  1120. nprotoxcistructs * sizeof(xCharInfo);
  1121. reply = (xQueryFontReply *) calloc(1, rlength);
  1122. if (!reply) {
  1123. return (BadAlloc);
  1124. }
  1125. reply->type = X_Reply;
  1126. reply->length = (rlength - sizeof(xGenericReply)) >> 2;
  1127. reply->sequenceNumber = client->sequence;
  1128. QueryFont(pFont, reply, nprotoxcistructs);
  1129. WriteReplyToClient(client, rlength, reply);
  1130. free(reply);
  1131. return (client->noClientException);
  1132. }
  1133. }
  1134. int
  1135. ProcQueryTextExtents(register ClientPtr client)
  1136. {
  1137. REQUEST(xQueryTextExtentsReq);
  1138. xQueryTextExtentsReply reply;
  1139. FontPtr pFont;
  1140. GC *pGC;
  1141. ExtentInfoRec info;
  1142. unsigned long length;
  1143. REQUEST_AT_LEAST_SIZE(xQueryTextExtentsReq);
  1144. pFont = (FontPtr) SecurityLookupIDByType(client, stuff->fid, RT_FONT,
  1145. SecurityReadAccess);
  1146. if (!pFont) {
  1147. pGC = (GC *) SecurityLookupIDByType(client, stuff->fid, RT_GC,
  1148. SecurityReadAccess);
  1149. if (!pGC) {
  1150. client->errorValue = stuff->fid;
  1151. return (BadFont);
  1152. }
  1153. pFont = pGC->font;
  1154. }
  1155. length = client->req_len - (sizeof(xQueryTextExtentsReq) >> 2);
  1156. length = length << 1;
  1157. if (stuff->oddLength) {
  1158. if (length == 0)
  1159. return (BadLength);
  1160. length--;
  1161. }
  1162. if (!QueryTextExtents(pFont, length, (unsigned char *) &stuff[1], &info))
  1163. return (BadAlloc);
  1164. reply.type = X_Reply;
  1165. reply.length = 0;
  1166. reply.sequenceNumber = client->sequence;
  1167. reply.drawDirection = info.drawDirection;
  1168. reply.fontAscent = info.fontAscent;
  1169. reply.fontDescent = info.fontDescent;
  1170. reply.overallAscent = info.overallAscent;
  1171. reply.overallDescent = info.overallDescent;
  1172. reply.overallWidth = info.overallWidth;
  1173. reply.overallLeft = info.overallLeft;
  1174. reply.overallRight = info.overallRight;
  1175. WriteReplyToClient(client, sizeof(xQueryTextExtentsReply), &reply);
  1176. return (client->noClientException);
  1177. }
  1178. int
  1179. ProcListFonts(register ClientPtr client)
  1180. {
  1181. REQUEST(xListFontsReq);
  1182. REQUEST_FIXED_SIZE(xListFontsReq, stuff->nbytes);
  1183. return ListFonts(client, (unsigned char *) &stuff[1], stuff->nbytes,
  1184. stuff->maxNames);
  1185. }
  1186. int
  1187. ProcListFontsWithInfo(register ClientPtr client)
  1188. {
  1189. REQUEST(xListFontsWithInfoReq);
  1190. REQUEST_FIXED_SIZE(xListFontsWithInfoReq, stuff->nbytes);
  1191. return StartListFontsWithInfo(client, stuff->nbytes,
  1192. (unsigned char *) &stuff[1], stuff->maxNames);
  1193. }
  1194. /**
  1195. *
  1196. * \param value must conform to DeleteType
  1197. */
  1198. int
  1199. dixDestroyPixmap(pointer value, XID pid)
  1200. {
  1201. PixmapPtr pPixmap = (PixmapPtr) value;
  1202. return (*pPixmap->drawable.pScreen->DestroyPixmap) (pPixmap);
  1203. }
  1204. int
  1205. ProcCreatePixmap(register ClientPtr client)
  1206. {
  1207. PixmapPtr pMap;
  1208. DrawablePtr pDraw;
  1209. REQUEST(xCreatePixmapReq);
  1210. DepthPtr pDepth;
  1211. int i;
  1212. REQUEST_SIZE_MATCH(xCreatePixmapReq);
  1213. client->errorValue = stuff->pid;
  1214. LEGAL_NEW_RESOURCE(stuff->pid, client);
  1215. SECURITY_VERIFY_GEOMETRABLE(pDraw, stuff->drawable, client,
  1216. SecurityReadAccess);
  1217. if (!stuff->width || !stuff->height) {
  1218. client->errorValue = 0;
  1219. return BadValue;
  1220. }
  1221. if (stuff->width > 32767 || stuff->height > 32767) {
  1222. /* It is allowed to try and allocate a pixmap which is larger than
  1223. * 32767 in either dimension. However, all of the framebuffer code
  1224. * is buggy and does not reliably draw to such big pixmaps, basically
  1225. * because the Region data structure operates with signed shorts
  1226. * for the rectangles in it.
  1227. *
  1228. * Furthermore, several places in the X server computes the
  1229. * size in bytes of the pixmap and tries to store it in an
  1230. * integer. This integer can overflow and cause the allocated size
  1231. * to be much smaller.
  1232. *
  1233. * So, such big pixmaps are rejected here with a BadAlloc
  1234. */
  1235. return BadAlloc;
  1236. }
  1237. if (stuff->depth != 1) {
  1238. pDepth = pDraw->pScreen->allowedDepths;
  1239. for (i = 0; i < pDraw->pScreen->numDepths; i++, pDepth++)
  1240. if (pDepth->depth == stuff->depth)
  1241. goto CreatePmap;
  1242. client->errorValue = stuff->depth;
  1243. return BadValue;
  1244. }
  1245. CreatePmap:
  1246. pMap = (PixmapPtr) (*pDraw->pScreen->CreatePixmap)
  1247. (pDraw->pScreen, stuff->width, stuff->height, stuff->depth);
  1248. if (pMap) {
  1249. pMap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  1250. pMap->drawable.id = stuff->pid;
  1251. if (AddResource(stuff->pid, RT_PIXMAP, (pointer) pMap))
  1252. return (client->noClientException);
  1253. }
  1254. return (BadAlloc);
  1255. }
  1256. int
  1257. ProcFreePixmap(register ClientPtr client)
  1258. {
  1259. PixmapPtr pMap;
  1260. REQUEST(xResourceReq);
  1261. REQUEST_SIZE_MATCH(xResourceReq);
  1262. pMap = (PixmapPtr) SecurityLookupIDByType(client, stuff->id, RT_PIXMAP,
  1263. SecurityDestroyAccess);
  1264. if (pMap) {
  1265. FreeResource(stuff->id, RT_NONE);
  1266. return (client->noClientException);
  1267. }
  1268. else {
  1269. client->errorValue = stuff->id;
  1270. return (BadPixmap);
  1271. }
  1272. }
  1273. int
  1274. ProcCreateGC(register ClientPtr client)
  1275. {
  1276. int error;
  1277. GC *pGC;
  1278. DrawablePtr pDraw;
  1279. unsigned len;
  1280. REQUEST(xCreateGCReq);
  1281. REQUEST_AT_LEAST_SIZE(xCreateGCReq);
  1282. client->errorValue = stuff->gc;
  1283. LEGAL_NEW_RESOURCE(stuff->gc, client);
  1284. SECURITY_VERIFY_DRAWABLE(pDraw, stuff->drawable, client,
  1285. SecurityReadAccess);
  1286. len = client->req_len - (sizeof(xCreateGCReq) >> 2);
  1287. if (len != Ones(stuff->mask))
  1288. return BadLength;
  1289. pGC = (GC *) CreateGC(pDraw, stuff->mask, (XID *) &stuff[1], &error);
  1290. if (error != Success)
  1291. return error;
  1292. if (!AddResource(stuff->gc, RT_GC, (pointer) pGC))
  1293. return (BadAlloc);
  1294. return (client->noClientException);
  1295. }
  1296. int
  1297. ProcChangeGC(register ClientPtr client)
  1298. {
  1299. GC *pGC;
  1300. REQUEST(xChangeGCReq);
  1301. int result;
  1302. unsigned len;
  1303. REQUEST_AT_LEAST_SIZE(xChangeGCReq);
  1304. SECURITY_VERIFY_GC(pGC, stuff->gc, client, SecurityWriteAccess);
  1305. len = client->req_len - (sizeof(xChangeGCReq) >> 2);
  1306. if (len != Ones(stuff->mask))
  1307. return BadLength;
  1308. result = dixChangeGC(client, pGC, stuff->mask, (CARD32 *) &stuff[1], 0);
  1309. if (client->noClientException != Success)
  1310. return (client->noClientException);
  1311. else {
  1312. client->errorValue = clientErrorValue;
  1313. return (result);
  1314. }
  1315. }
  1316. int
  1317. ProcCopyGC(register ClientPtr client)
  1318. {
  1319. GC *dstGC;
  1320. GC *pGC;
  1321. int result;
  1322. REQUEST(xCopyGCReq);
  1323. REQUEST_SIZE_MATCH(xCopyGCReq);
  1324. SECURITY_VERIFY_GC(pGC, stuff->srcGC, client, SecurityReadAccess);
  1325. SECURITY_VERIFY_GC(dstGC, stuff->dstGC, client, SecurityWriteAccess);
  1326. if ((dstGC->pScreen != pGC->pScreen) || (dstGC->depth != pGC->depth))
  1327. return (BadMatch);
  1328. result = CopyGC(pGC, dstGC, stuff->mask);
  1329. if (client->noClientException != Success)
  1330. return (client->noClientException);
  1331. else {
  1332. client->errorValue = clientErrorValue;
  1333. return (result);
  1334. }
  1335. }
  1336. int
  1337. ProcSetDashes(register ClientPtr client)
  1338. {
  1339. GC *pGC;
  1340. int result;
  1341. REQUEST(xSetDashesReq);
  1342. REQUEST_FIXED_SIZE(xSetDashesReq, stuff->nDashes);
  1343. if (stuff->nDashes == 0) {
  1344. client->errorValue = 0;
  1345. return BadValue;
  1346. }
  1347. SECURITY_VERIFY_GC(pGC, stuff->gc, client, SecurityWriteAccess);
  1348. result = SetDashes(pGC, stuff->dashOffset, stuff->nDashes,
  1349. (unsigned char *) &stuff[1]);
  1350. if (client->noClientException != Success)
  1351. return (client->noClientException);
  1352. else {
  1353. client->errorValue = clientErrorValue;
  1354. return (result);
  1355. }
  1356. }
  1357. int
  1358. ProcSetClipRectangles(register ClientPtr client)
  1359. {
  1360. int nr;
  1361. int result;
  1362. GC *pGC;
  1363. REQUEST(xSetClipRectanglesReq);
  1364. REQUEST_AT_LEAST_SIZE(xSetClipRectanglesReq);
  1365. if ((stuff->ordering != Unsorted) && (stuff->ordering != YSorted) &&
  1366. (stuff->ordering != YXSorted) && (stuff->ordering != YXBanded)) {
  1367. client->errorValue = stuff->ordering;
  1368. return BadValue;
  1369. }
  1370. SECURITY_VERIFY_GC(pGC, stuff->gc, client, SecurityWriteAccess);
  1371. nr = (client->req_len << 2) - sizeof(xSetClipRectanglesReq);
  1372. if (nr & 4)
  1373. return (BadLength);
  1374. nr >>= 3;
  1375. result = SetClipRects(pGC, stuff->xOrigin, stuff->yOrigin,
  1376. nr, (xRectangle *) &stuff[1], (int) stuff->ordering);
  1377. if (client->noClientException != Success)
  1378. return (client->noClientException);
  1379. else
  1380. return (result);
  1381. }
  1382. int
  1383. ProcFreeGC(register ClientPtr client)
  1384. {
  1385. GC *pGC;
  1386. REQUEST(xResourceReq);
  1387. REQUEST_SIZE_MATCH(xResourceReq);
  1388. SECURITY_VERIFY_GC(pGC, stuff->id, client, SecurityDestroyAccess);
  1389. FreeResource(stuff->id, RT_NONE);
  1390. return (client->noClientException);
  1391. }
  1392. int
  1393. ProcClearToBackground(register ClientPtr client)
  1394. {
  1395. REQUEST(xClearAreaReq);
  1396. WindowPtr pWin;
  1397. REQUEST_SIZE_MATCH(xClearAreaReq);
  1398. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  1399. SecurityWriteAccess);
  1400. if (!pWin)
  1401. return (BadWindow);
  1402. if (pWin->drawable.class == InputOnly) {
  1403. client->errorValue = stuff->window;
  1404. return (BadMatch);
  1405. }
  1406. if ((stuff->exposures != xTrue) && (stuff->exposures != xFalse)) {
  1407. client->errorValue = stuff->exposures;
  1408. return (BadValue);
  1409. }
  1410. (*pWin->drawable.pScreen->ClearToBackground) (pWin, stuff->x, stuff->y,
  1411. stuff->width, stuff->height,
  1412. (Bool) stuff->exposures);
  1413. return (client->noClientException);
  1414. }
  1415. int
  1416. ProcCopyArea(register ClientPtr client)
  1417. {
  1418. DrawablePtr pDst;
  1419. DrawablePtr pSrc;
  1420. GC *pGC;
  1421. REQUEST(xCopyAreaReq);
  1422. RegionPtr pRgn;
  1423. REQUEST_SIZE_MATCH(xCopyAreaReq);
  1424. VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pDst, pGC, client);
  1425. if (stuff->dstDrawable != stuff->srcDrawable) {
  1426. SECURITY_VERIFY_DRAWABLE(pSrc, stuff->srcDrawable, client,
  1427. SecurityReadAccess);
  1428. if ((pDst->pScreen != pSrc->pScreen) || (pDst->depth != pSrc->depth)) {
  1429. client->errorValue = stuff->dstDrawable;
  1430. return (BadMatch);
  1431. }
  1432. }
  1433. else
  1434. pSrc = pDst;
  1435. SET_DBE_SRCBUF(pSrc, stuff->srcDrawable);
  1436. pRgn = (*pGC->ops->CopyArea) (pSrc, pDst, pGC, stuff->srcX, stuff->srcY,
  1437. stuff->width, stuff->height,
  1438. stuff->dstX, stuff->dstY);
  1439. if (pGC->graphicsExposures) {
  1440. (*pDst->pScreen->SendGraphicsExpose)
  1441. (client, pRgn, stuff->dstDrawable, X_CopyArea, 0);
  1442. if (pRgn)
  1443. REGION_DESTROY(pRgn);
  1444. }
  1445. return (client->noClientException);
  1446. }
  1447. int
  1448. ProcCopyPlane(register ClientPtr client)
  1449. {
  1450. DrawablePtr psrcDraw, pdstDraw;
  1451. GC *pGC;
  1452. REQUEST(xCopyPlaneReq);
  1453. RegionPtr pRgn;
  1454. REQUEST_SIZE_MATCH(xCopyPlaneReq);
  1455. VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pdstDraw, pGC, client);
  1456. if (stuff->dstDrawable != stuff->srcDrawable) {
  1457. SECURITY_VERIFY_DRAWABLE(psrcDraw, stuff->srcDrawable, client,
  1458. SecurityReadAccess);
  1459. if (pdstDraw->pScreen != psrcDraw->pScreen) {
  1460. client->errorValue = stuff->dstDrawable;
  1461. return (BadMatch);
  1462. }
  1463. }
  1464. else
  1465. psrcDraw = pdstDraw;
  1466. SET_DBE_SRCBUF(psrcDraw, stuff->srcDrawable);
  1467. /* Check to see if stuff->bitPlane has exactly ONE good bit set */
  1468. if (stuff->bitPlane == 0 || (stuff->bitPlane & (stuff->bitPlane - 1)) ||
  1469. (stuff->bitPlane > (1L << (psrcDraw->depth - 1)))) {
  1470. client->errorValue = stuff->bitPlane;
  1471. return (BadValue);
  1472. }
  1473. pRgn =
  1474. (*pGC->ops->CopyPlane) (psrcDraw, pdstDraw, pGC, stuff->srcX,
  1475. stuff->srcY, stuff->width, stuff->height,
  1476. stuff->dstX, stuff->dstY, stuff->bitPlane);
  1477. if (pGC->graphicsExposures) {
  1478. (*pdstDraw->pScreen->SendGraphicsExpose)
  1479. (client, pRgn, stuff->dstDrawable, X_CopyPlane, 0);
  1480. if (pRgn)
  1481. REGION_DESTROY(pRgn);
  1482. }
  1483. return (client->noClientException);
  1484. }
  1485. int
  1486. ProcPolyPoint(register ClientPtr client)
  1487. {
  1488. int npoint;
  1489. GC *pGC;
  1490. DrawablePtr pDraw;
  1491. REQUEST(xPolyPointReq);
  1492. REQUEST_AT_LEAST_SIZE(xPolyPointReq);
  1493. if ((stuff->coordMode != CoordModeOrigin) &&
  1494. (stuff->coordMode != CoordModePrevious)) {
  1495. client->errorValue = stuff->coordMode;
  1496. return BadValue;
  1497. }
  1498. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1499. npoint = ((client->req_len << 2) - sizeof(xPolyPointReq)) >> 2;
  1500. if (npoint)
  1501. (*pGC->ops->PolyPoint) (pDraw, pGC, stuff->coordMode, npoint,
  1502. (xPoint *) & stuff[1]);
  1503. return (client->noClientException);
  1504. }
  1505. int
  1506. ProcPolyLine(register ClientPtr client)
  1507. {
  1508. int npoint;
  1509. GC *pGC;
  1510. DrawablePtr pDraw;
  1511. REQUEST(xPolyLineReq);
  1512. REQUEST_AT_LEAST_SIZE(xPolyLineReq);
  1513. if ((stuff->coordMode != CoordModeOrigin) &&
  1514. (stuff->coordMode != CoordModePrevious)) {
  1515. client->errorValue = stuff->coordMode;
  1516. return BadValue;
  1517. }
  1518. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1519. npoint = ((client->req_len << 2) - sizeof(xPolyLineReq)) >> 2;
  1520. if (npoint > 1)
  1521. (*pGC->ops->Polylines) (pDraw, pGC, stuff->coordMode, npoint,
  1522. (DDXPointPtr) & stuff[1]);
  1523. return (client->noClientException);
  1524. }
  1525. int
  1526. ProcPolySegment(register ClientPtr client)
  1527. {
  1528. int nsegs;
  1529. GC *pGC;
  1530. DrawablePtr pDraw;
  1531. REQUEST(xPolySegmentReq);
  1532. REQUEST_AT_LEAST_SIZE(xPolySegmentReq);
  1533. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1534. nsegs = (client->req_len << 2) - sizeof(xPolySegmentReq);
  1535. if (nsegs & 4)
  1536. return (BadLength);
  1537. nsegs >>= 3;
  1538. if (nsegs)
  1539. (*pGC->ops->PolySegment) (pDraw, pGC, nsegs, (xSegment *) & stuff[1]);
  1540. return (client->noClientException);
  1541. }
  1542. int
  1543. ProcPolyRectangle(register ClientPtr client)
  1544. {
  1545. int nrects;
  1546. GC *pGC;
  1547. DrawablePtr pDraw;
  1548. REQUEST(xPolyRectangleReq);
  1549. REQUEST_AT_LEAST_SIZE(xPolyRectangleReq);
  1550. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1551. nrects = (client->req_len << 2) - sizeof(xPolyRectangleReq);
  1552. if (nrects & 4)
  1553. return (BadLength);
  1554. nrects >>= 3;
  1555. if (nrects)
  1556. (*pGC->ops->PolyRectangle) (pDraw, pGC,
  1557. nrects, (xRectangle *) &stuff[1]);
  1558. return (client->noClientException);
  1559. }
  1560. int
  1561. ProcPolyArc(register ClientPtr client)
  1562. {
  1563. int narcs;
  1564. GC *pGC;
  1565. DrawablePtr pDraw;
  1566. REQUEST(xPolyArcReq);
  1567. REQUEST_AT_LEAST_SIZE(xPolyArcReq);
  1568. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1569. narcs = (client->req_len << 2) - sizeof(xPolyArcReq);
  1570. if (narcs % sizeof(xArc))
  1571. return (BadLength);
  1572. narcs /= sizeof(xArc);
  1573. if (narcs)
  1574. (*pGC->ops->PolyArc) (pDraw, pGC, narcs, (xArc *) & stuff[1]);
  1575. return (client->noClientException);
  1576. }
  1577. int
  1578. ProcFillPoly(register ClientPtr client)
  1579. {
  1580. int things;
  1581. GC *pGC;
  1582. DrawablePtr pDraw;
  1583. REQUEST(xFillPolyReq);
  1584. REQUEST_AT_LEAST_SIZE(xFillPolyReq);
  1585. if ((stuff->shape != Complex) && (stuff->shape != Nonconvex) &&
  1586. (stuff->shape != Convex)) {
  1587. client->errorValue = stuff->shape;
  1588. return BadValue;
  1589. }
  1590. if ((stuff->coordMode != CoordModeOrigin) &&
  1591. (stuff->coordMode != CoordModePrevious)) {
  1592. client->errorValue = stuff->coordMode;
  1593. return BadValue;
  1594. }
  1595. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1596. things = ((client->req_len << 2) - sizeof(xFillPolyReq)) >> 2;
  1597. if (things)
  1598. (*pGC->ops->FillPolygon) (pDraw, pGC, stuff->shape,
  1599. stuff->coordMode, things,
  1600. (DDXPointPtr) & stuff[1]);
  1601. return (client->noClientException);
  1602. }
  1603. int
  1604. ProcPolyFillRectangle(register ClientPtr client)
  1605. {
  1606. int things;
  1607. GC *pGC;
  1608. DrawablePtr pDraw;
  1609. REQUEST(xPolyFillRectangleReq);
  1610. REQUEST_AT_LEAST_SIZE(xPolyFillRectangleReq);
  1611. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1612. things = (client->req_len << 2) - sizeof(xPolyFillRectangleReq);
  1613. if (things & 4)
  1614. return (BadLength);
  1615. things >>= 3;
  1616. if (things)
  1617. (*pGC->ops->PolyFillRect) (pDraw, pGC, things,
  1618. (xRectangle *) &stuff[1]);
  1619. return (client->noClientException);
  1620. }
  1621. int
  1622. ProcPolyFillArc(register ClientPtr client)
  1623. {
  1624. int narcs;
  1625. GC *pGC;
  1626. DrawablePtr pDraw;
  1627. REQUEST(xPolyFillArcReq);
  1628. REQUEST_AT_LEAST_SIZE(xPolyFillArcReq);
  1629. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1630. narcs = (client->req_len << 2) - sizeof(xPolyFillArcReq);
  1631. if (narcs % sizeof(xArc))
  1632. return (BadLength);
  1633. narcs /= sizeof(xArc);
  1634. if (narcs)
  1635. (*pGC->ops->PolyFillArc) (pDraw, pGC, narcs, (xArc *) & stuff[1]);
  1636. return (client->noClientException);
  1637. }
  1638. #ifdef MATCH_CLIENT_ENDIAN
  1639. int
  1640. ServerOrder(void)
  1641. {
  1642. int whichbyte = 1;
  1643. if (*((char *) &whichbyte))
  1644. return LSBFirst;
  1645. return MSBFirst;
  1646. }
  1647. #define ClientOrder(client) ((client)->swapped ? !ServerOrder() : ServerOrder())
  1648. void
  1649. ReformatImage(char *base, int nbytes, int bpp, int order)
  1650. {
  1651. switch (bpp) {
  1652. case 1: /* yuck */
  1653. if (BITMAP_BIT_ORDER != order)
  1654. BitOrderInvert((unsigned char *) base, nbytes);
  1655. #if IMAGE_BYTE_ORDER != BITMAP_BIT_ORDER && BITMAP_SCANLINE_UNIT != 8
  1656. ReformatImage(base, nbytes, BITMAP_SCANLINE_UNIT, order);
  1657. #endif
  1658. break;
  1659. case 4:
  1660. break; /* yuck */
  1661. case 8:
  1662. break;
  1663. case 16:
  1664. if (IMAGE_BYTE_ORDER != order)
  1665. TwoByteSwap((unsigned char *) base, nbytes);
  1666. break;
  1667. case 32:
  1668. if (IMAGE_BYTE_ORDER != order)
  1669. FourByteSwap((unsigned char *) base, nbytes);
  1670. break;
  1671. }
  1672. }
  1673. #else
  1674. #define ReformatImage(b,n,bpp,o)
  1675. #endif
  1676. /* 64-bit server notes: the protocol restricts padding of images to
  1677. * 8-, 16-, or 32-bits. We would like to have 64-bits for the server
  1678. * to use internally. Removes need for internal alignment checking.
  1679. * All of the PutImage functions could be changed individually, but
  1680. * as currently written, they call other routines which require things
  1681. * to be 64-bit padded on scanlines, so we changed things here.
  1682. * If an image would be padded differently for 64- versus 32-, then
  1683. * copy each scanline to a 64-bit padded scanline.
  1684. * Also, we need to make sure that the image is aligned on a 64-bit
  1685. * boundary, even if the scanlines are padded to our satisfaction.
  1686. */
  1687. int
  1688. ProcPutImage(register ClientPtr client)
  1689. {
  1690. GC *pGC;
  1691. DrawablePtr pDraw;
  1692. long length; /* length of scanline server padded */
  1693. long lengthProto; /* length of scanline protocol padded */
  1694. char *tmpImage;
  1695. REQUEST(xPutImageReq);
  1696. REQUEST_AT_LEAST_SIZE(xPutImageReq);
  1697. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1698. if (stuff->format == XYBitmap) {
  1699. if ((stuff->depth != 1) ||
  1700. (stuff->leftPad >= (unsigned int) screenInfo.bitmapScanlinePad))
  1701. return BadMatch;
  1702. length = BitmapBytePad(stuff->width + stuff->leftPad);
  1703. }
  1704. else if (stuff->format == XYPixmap) {
  1705. if ((pDraw->depth != stuff->depth) ||
  1706. (stuff->leftPad >= (unsigned int) screenInfo.bitmapScanlinePad))
  1707. return BadMatch;
  1708. length = BitmapBytePad(stuff->width + stuff->leftPad);
  1709. length *= stuff->depth;
  1710. }
  1711. else if (stuff->format == ZPixmap) {
  1712. if ((pDraw->depth != stuff->depth) || (stuff->leftPad != 0))
  1713. return BadMatch;
  1714. length = PixmapBytePad(stuff->width, stuff->depth);
  1715. }
  1716. else {
  1717. client->errorValue = stuff->format;
  1718. return BadValue;
  1719. }
  1720. tmpImage = (char *) &stuff[1];
  1721. lengthProto = length;
  1722. if (((((lengthProto * stuff->height) + (unsigned) 3) >> 2) +
  1723. (sizeof(xPutImageReq) >> 2)) != client->req_len)
  1724. return BadLength;
  1725. ReformatImage(tmpImage, lengthProto * stuff->height,
  1726. stuff->format == ZPixmap ? BitsPerPixel(stuff->depth) : 1,
  1727. ClientOrder(client));
  1728. (*pGC->ops->PutImage) (pDraw, pGC, stuff->depth, stuff->dstX, stuff->dstY,
  1729. stuff->width, stuff->height,
  1730. stuff->leftPad, stuff->format, tmpImage);
  1731. return (client->noClientException);
  1732. }
  1733. int
  1734. DoGetImage(register ClientPtr client, int format, Drawable drawable,
  1735. int x, int y, int width, int height,
  1736. Mask planemask, xGetImageReply ** im_return)
  1737. {
  1738. DrawablePtr pDraw;
  1739. int nlines, linesPerBuf;
  1740. int linesDone;
  1741. long widthBytesLine, length;
  1742. Mask plane = 0;
  1743. char *pBuf;
  1744. xGetImageReply xgi;
  1745. if ((format != XYPixmap) && (format != ZPixmap)) {
  1746. client->errorValue = format;
  1747. return (BadValue);
  1748. }
  1749. SECURITY_VERIFY_DRAWABLE(pDraw, drawable, client, SecurityReadAccess);
  1750. memset(&xgi, 0, sizeof(xGetImageReply));
  1751. if (pDraw->type == DRAWABLE_WINDOW) {
  1752. if ( /* check for being viewable */
  1753. !((WindowPtr) pDraw)->realized ||
  1754. /* check for being on screen */
  1755. pDraw->x + x < 0 ||
  1756. pDraw->x + x + width > pDraw->pScreen->width ||
  1757. pDraw->y + y < 0 ||
  1758. pDraw->y + y + height > pDraw->pScreen->height ||
  1759. /* check for being inside of border */
  1760. x < -wBorderWidth((WindowPtr) pDraw) ||
  1761. x + width > wBorderWidth((WindowPtr) pDraw) + (int) pDraw->width
  1762. || y < -wBorderWidth((WindowPtr) pDraw) ||
  1763. y + height >
  1764. wBorderWidth((WindowPtr) pDraw) + (int) pDraw->height)
  1765. return (BadMatch);
  1766. xgi.visual = wVisual(((WindowPtr) pDraw));
  1767. }
  1768. else {
  1769. if (x < 0 ||
  1770. x + width > (int) pDraw->width ||
  1771. y < 0 || y + height > (int) pDraw->height)
  1772. return (BadMatch);
  1773. xgi.visual = None;
  1774. }
  1775. SET_DBE_SRCBUF(pDraw, drawable);
  1776. xgi.type = X_Reply;
  1777. xgi.sequenceNumber = client->sequence;
  1778. xgi.depth = pDraw->depth;
  1779. if (format == ZPixmap) {
  1780. widthBytesLine = PixmapBytePad(width, pDraw->depth);
  1781. length = widthBytesLine * height;
  1782. }
  1783. else {
  1784. widthBytesLine = BitmapBytePad(width);
  1785. plane = ((Mask) 1) << (pDraw->depth - 1);
  1786. /* only planes asked for */
  1787. length = widthBytesLine * height *
  1788. Ones(planemask & (plane | (plane - 1)));
  1789. }
  1790. xgi.length = length;
  1791. if (im_return) {
  1792. pBuf = malloc(sz_xGetImageReply + length);
  1793. if (!pBuf)
  1794. return (BadAlloc);
  1795. if (widthBytesLine == 0)
  1796. linesPerBuf = 0;
  1797. else
  1798. linesPerBuf = height;
  1799. *im_return = (xGetImageReply *) pBuf;
  1800. *(xGetImageReply *) pBuf = xgi;
  1801. pBuf += sz_xGetImageReply;
  1802. }
  1803. else {
  1804. xgi.length = (xgi.length + 3) >> 2;
  1805. if (widthBytesLine == 0 || height == 0)
  1806. linesPerBuf = 0;
  1807. else if (widthBytesLine >= IMAGE_BUFSIZE)
  1808. linesPerBuf = 1;
  1809. else {
  1810. linesPerBuf = IMAGE_BUFSIZE / widthBytesLine;
  1811. if (linesPerBuf > height)
  1812. linesPerBuf = height;
  1813. }
  1814. length = linesPerBuf * widthBytesLine;
  1815. if (linesPerBuf < height) {
  1816. /* we have to make sure intermediate buffers don't need padding */
  1817. while ((linesPerBuf > 1) &&
  1818. (length & ((1L << LOG2_BYTES_PER_SCANLINE_PAD) - 1))) {
  1819. linesPerBuf--;
  1820. length -= widthBytesLine;
  1821. }
  1822. while (length & ((1L << LOG2_BYTES_PER_SCANLINE_PAD) - 1)) {
  1823. linesPerBuf++;
  1824. length += widthBytesLine;
  1825. }
  1826. }
  1827. if (!(pBuf = (char *) ALLOCATE_LOCAL(length)))
  1828. return (BadAlloc);
  1829. WriteReplyToClient(client, sizeof(xGetImageReply), &xgi);
  1830. }
  1831. if (linesPerBuf == 0) {
  1832. /* nothing to do */
  1833. }
  1834. else if (format == ZPixmap) {
  1835. linesDone = 0;
  1836. while (height - linesDone > 0) {
  1837. nlines = min(linesPerBuf, height - linesDone);
  1838. (*pDraw->pScreen->GetImage) (pDraw,
  1839. x,
  1840. y + linesDone,
  1841. width,
  1842. nlines,
  1843. format, planemask, (pointer) pBuf);
  1844. /* Note that this is NOT a call to WriteSwappedDataToClient,
  1845. as we do NOT byte swap */
  1846. if (!im_return) {
  1847. ReformatImage(pBuf, (int) (nlines * widthBytesLine),
  1848. BitsPerPixel(pDraw->depth), ClientOrder(client));
  1849. /* Don't split me, gcc pukes when you do */
  1850. (void) WriteToClient(client,
  1851. (int) (nlines * widthBytesLine), pBuf);
  1852. }
  1853. linesDone += nlines;
  1854. }
  1855. }
  1856. else { /* XYPixmap */
  1857. for (; plane; plane >>= 1) {
  1858. if (planemask & plane) {
  1859. linesDone = 0;
  1860. while (height - linesDone > 0) {
  1861. nlines = min(linesPerBuf, height - linesDone);
  1862. (*pDraw->pScreen->GetImage) (pDraw,
  1863. x,
  1864. y + linesDone,
  1865. width,
  1866. nlines,
  1867. format, plane, (pointer) pBuf);
  1868. /* Note: NOT a call to WriteSwappedDataToClient,
  1869. as we do NOT byte swap */
  1870. if (im_return) {
  1871. pBuf += nlines * widthBytesLine;
  1872. }
  1873. else {
  1874. ReformatImage(pBuf,
  1875. (int) (nlines * widthBytesLine),
  1876. 1, ClientOrder(client));
  1877. /* Don't split me, gcc pukes when you do */
  1878. (void) WriteToClient(client,
  1879. (int) (nlines * widthBytesLine),
  1880. pBuf);
  1881. }
  1882. linesDone += nlines;
  1883. }
  1884. }
  1885. }
  1886. }
  1887. if (!im_return)
  1888. DEALLOCATE_LOCAL(pBuf);
  1889. return (client->noClientException);
  1890. }
  1891. int
  1892. ProcGetImage(register ClientPtr client)
  1893. {
  1894. REQUEST(xGetImageReq);
  1895. REQUEST_SIZE_MATCH(xGetImageReq);
  1896. return DoGetImage(client, stuff->format, stuff->drawable,
  1897. stuff->x, stuff->y,
  1898. (int) stuff->width, (int) stuff->height,
  1899. stuff->planeMask, (xGetImageReply **) NULL);
  1900. }
  1901. int
  1902. ProcPolyText(register ClientPtr client)
  1903. {
  1904. int err;
  1905. REQUEST(xPolyTextReq);
  1906. DrawablePtr pDraw;
  1907. GC *pGC;
  1908. REQUEST_AT_LEAST_SIZE(xPolyTextReq);
  1909. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1910. err = PolyText(client,
  1911. pDraw,
  1912. pGC,
  1913. (unsigned char *) &stuff[1],
  1914. ((unsigned char *) stuff) + (client->req_len << 2),
  1915. stuff->x, stuff->y, stuff->reqType, stuff->drawable);
  1916. if (err == Success) {
  1917. return (client->noClientException);
  1918. }
  1919. else
  1920. return err;
  1921. }
  1922. int
  1923. ProcImageText8(register ClientPtr client)
  1924. {
  1925. int err;
  1926. DrawablePtr pDraw;
  1927. GC *pGC;
  1928. REQUEST(xImageTextReq);
  1929. REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars);
  1930. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1931. err = ImageText(client,
  1932. pDraw,
  1933. pGC,
  1934. stuff->nChars,
  1935. (unsigned char *) &stuff[1],
  1936. stuff->x, stuff->y, stuff->reqType, stuff->drawable);
  1937. if (err == Success) {
  1938. return (client->noClientException);
  1939. }
  1940. else
  1941. return err;
  1942. }
  1943. int
  1944. ProcImageText16(register ClientPtr client)
  1945. {
  1946. int err;
  1947. DrawablePtr pDraw;
  1948. GC *pGC;
  1949. REQUEST(xImageTextReq);
  1950. REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars << 1);
  1951. VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1952. err = ImageText(client,
  1953. pDraw,
  1954. pGC,
  1955. stuff->nChars,
  1956. (unsigned char *) &stuff[1],
  1957. stuff->x, stuff->y, stuff->reqType, stuff->drawable);
  1958. if (err == Success) {
  1959. return (client->noClientException);
  1960. }
  1961. else
  1962. return err;
  1963. }
  1964. int
  1965. ProcCreateColormap(register ClientPtr client)
  1966. {
  1967. VisualPtr pVisual;
  1968. ColormapPtr pmap;
  1969. Colormap mid;
  1970. WindowPtr pWin;
  1971. ScreenPtr pScreen;
  1972. REQUEST(xCreateColormapReq);
  1973. int i, result;
  1974. REQUEST_SIZE_MATCH(xCreateColormapReq);
  1975. if ((stuff->alloc != AllocNone) && (stuff->alloc != AllocAll)) {
  1976. client->errorValue = stuff->alloc;
  1977. return (BadValue);
  1978. }
  1979. mid = stuff->mid;
  1980. LEGAL_NEW_RESOURCE(mid, client);
  1981. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  1982. SecurityReadAccess);
  1983. if (!pWin)
  1984. return (BadWindow);
  1985. pScreen = pWin->drawable.pScreen;
  1986. for (i = 0, pVisual = pScreen->visuals;
  1987. i < pScreen->numVisuals; i++, pVisual++) {
  1988. if (pVisual->vid != stuff->visual)
  1989. continue;
  1990. result = CreateColormap(mid, pScreen, pVisual, &pmap,
  1991. (int) stuff->alloc, client->index);
  1992. if (client->noClientException != Success)
  1993. return (client->noClientException);
  1994. else
  1995. return (result);
  1996. }
  1997. client->errorValue = stuff->visual;
  1998. return (BadMatch);
  1999. }
  2000. int
  2001. ProcFreeColormap(register ClientPtr client)
  2002. {
  2003. ColormapPtr pmap;
  2004. REQUEST(xResourceReq);
  2005. REQUEST_SIZE_MATCH(xResourceReq);
  2006. pmap = (ColormapPtr) SecurityLookupIDByType(client, stuff->id, RT_COLORMAP,
  2007. SecurityDestroyAccess);
  2008. if (pmap) {
  2009. /* Freeing a default colormap is a no-op */
  2010. if (!(pmap->flags & IsDefault))
  2011. FreeResource(stuff->id, RT_NONE);
  2012. return (client->noClientException);
  2013. }
  2014. else {
  2015. client->errorValue = stuff->id;
  2016. return (BadColor);
  2017. }
  2018. }
  2019. int
  2020. ProcCopyColormapAndFree(register ClientPtr client)
  2021. {
  2022. Colormap mid;
  2023. ColormapPtr pSrcMap;
  2024. REQUEST(xCopyColormapAndFreeReq);
  2025. int result;
  2026. REQUEST_SIZE_MATCH(xCopyColormapAndFreeReq);
  2027. mid = stuff->mid;
  2028. LEGAL_NEW_RESOURCE(mid, client);
  2029. if ((pSrcMap = (ColormapPtr) SecurityLookupIDByType(client, stuff->srcCmap,
  2030. RT_COLORMAP,
  2031. SecurityReadAccess |
  2032. SecurityWriteAccess))) {
  2033. result = CopyColormapAndFree(mid, pSrcMap, client->index);
  2034. if (client->noClientException != Success)
  2035. return (client->noClientException);
  2036. else
  2037. return (result);
  2038. }
  2039. else {
  2040. client->errorValue = stuff->srcCmap;
  2041. return (BadColor);
  2042. }
  2043. }
  2044. int
  2045. ProcInstallColormap(register ClientPtr client)
  2046. {
  2047. ColormapPtr pcmp;
  2048. REQUEST(xResourceReq);
  2049. REQUEST_SIZE_MATCH(xResourceReq);
  2050. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->id,
  2051. RT_COLORMAP,
  2052. SecurityReadAccess);
  2053. if (pcmp) {
  2054. (*(pcmp->pScreen->InstallColormap)) (pcmp);
  2055. return (client->noClientException);
  2056. }
  2057. else {
  2058. client->errorValue = stuff->id;
  2059. return (BadColor);
  2060. }
  2061. }
  2062. int
  2063. ProcUninstallColormap(register ClientPtr client)
  2064. {
  2065. ColormapPtr pcmp;
  2066. REQUEST(xResourceReq);
  2067. REQUEST_SIZE_MATCH(xResourceReq);
  2068. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->id,
  2069. RT_COLORMAP,
  2070. SecurityReadAccess);
  2071. if (pcmp) {
  2072. if (pcmp->mid != pcmp->pScreen->defColormap)
  2073. (*(pcmp->pScreen->UninstallColormap)) (pcmp);
  2074. return (client->noClientException);
  2075. }
  2076. else {
  2077. client->errorValue = stuff->id;
  2078. return (BadColor);
  2079. }
  2080. }
  2081. int
  2082. ProcListInstalledColormaps(register ClientPtr client)
  2083. {
  2084. xListInstalledColormapsReply *preply;
  2085. int nummaps;
  2086. WindowPtr pWin;
  2087. REQUEST(xResourceReq);
  2088. REQUEST_SIZE_MATCH(xResourceReq);
  2089. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  2090. SecurityReadAccess);
  2091. if (!pWin)
  2092. return (BadWindow);
  2093. preply = (xListInstalledColormapsReply *)
  2094. ALLOCATE_LOCAL(sizeof(xListInstalledColormapsReply) +
  2095. pWin->drawable.pScreen->maxInstalledCmaps *
  2096. sizeof(Colormap));
  2097. if (!preply)
  2098. return (BadAlloc);
  2099. preply->type = X_Reply;
  2100. preply->sequenceNumber = client->sequence;
  2101. nummaps = (*pWin->drawable.pScreen->ListInstalledColormaps)
  2102. (pWin->drawable.pScreen, (Colormap *) & preply[1]);
  2103. preply->nColormaps = nummaps;
  2104. preply->length = nummaps;
  2105. WriteReplyToClient(client, sizeof(xListInstalledColormapsReply), preply);
  2106. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  2107. WriteSwappedDataToClient(client, nummaps * sizeof(Colormap), &preply[1]);
  2108. DEALLOCATE_LOCAL(preply);
  2109. return (client->noClientException);
  2110. }
  2111. int
  2112. ProcAllocColor(register ClientPtr client)
  2113. {
  2114. ColormapPtr pmap;
  2115. int retval;
  2116. xAllocColorReply acr = {0};
  2117. REQUEST(xAllocColorReq);
  2118. REQUEST_SIZE_MATCH(xAllocColorReq);
  2119. pmap = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2120. RT_COLORMAP,
  2121. SecurityWriteAccess);
  2122. if (pmap) {
  2123. acr.type = X_Reply;
  2124. acr.length = 0;
  2125. acr.sequenceNumber = client->sequence;
  2126. acr.red = stuff->red;
  2127. acr.green = stuff->green;
  2128. acr.blue = stuff->blue;
  2129. acr.pixel = 0;
  2130. if ((retval = AllocColor(pmap, &acr.red, &acr.green, &acr.blue,
  2131. &acr.pixel, client->index))) {
  2132. if (client->noClientException != Success)
  2133. return (client->noClientException);
  2134. else
  2135. return (retval);
  2136. }
  2137. WriteReplyToClient(client, sizeof(xAllocColorReply), &acr);
  2138. return (client->noClientException);
  2139. }
  2140. else {
  2141. client->errorValue = stuff->cmap;
  2142. return (BadColor);
  2143. }
  2144. }
  2145. int
  2146. ProcAllocNamedColor(register ClientPtr client)
  2147. {
  2148. ColormapPtr pcmp;
  2149. REQUEST(xAllocNamedColorReq);
  2150. REQUEST_FIXED_SIZE(xAllocNamedColorReq, stuff->nbytes);
  2151. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2152. RT_COLORMAP,
  2153. SecurityWriteAccess);
  2154. if (pcmp) {
  2155. int retval;
  2156. xAllocNamedColorReply ancr;
  2157. ancr.type = X_Reply;
  2158. ancr.length = 0;
  2159. ancr.sequenceNumber = client->sequence;
  2160. if (OsLookupColor
  2161. (pcmp->pScreen->myNum, (char *) &stuff[1], stuff->nbytes,
  2162. &ancr.exactRed, &ancr.exactGreen, &ancr.exactBlue)) {
  2163. ancr.screenRed = ancr.exactRed;
  2164. ancr.screenGreen = ancr.exactGreen;
  2165. ancr.screenBlue = ancr.exactBlue;
  2166. ancr.pixel = 0;
  2167. if ((retval = AllocColor(pcmp,
  2168. &ancr.screenRed, &ancr.screenGreen,
  2169. &ancr.screenBlue, &ancr.pixel,
  2170. client->index))) {
  2171. if (client->noClientException != Success)
  2172. return (client->noClientException);
  2173. else
  2174. return (retval);
  2175. }
  2176. WriteReplyToClient(client, sizeof(xAllocNamedColorReply),
  2177. &ancr);
  2178. return (client->noClientException);
  2179. }
  2180. else
  2181. return (BadName);
  2182. }
  2183. else {
  2184. client->errorValue = stuff->cmap;
  2185. return (BadColor);
  2186. }
  2187. }
  2188. int
  2189. ProcAllocColorCells(register ClientPtr client)
  2190. {
  2191. ColormapPtr pcmp;
  2192. REQUEST(xAllocColorCellsReq);
  2193. REQUEST_SIZE_MATCH(xAllocColorCellsReq);
  2194. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2195. RT_COLORMAP,
  2196. SecurityWriteAccess);
  2197. if (pcmp) {
  2198. xAllocColorCellsReply accr;
  2199. int npixels, nmasks, retval;
  2200. long length;
  2201. Pixel *ppixels, *pmasks;
  2202. npixels = stuff->colors;
  2203. if (!npixels) {
  2204. client->errorValue = npixels;
  2205. return (BadValue);
  2206. }
  2207. if (stuff->contiguous != xTrue && stuff->contiguous != xFalse) {
  2208. client->errorValue = stuff->contiguous;
  2209. return (BadValue);
  2210. }
  2211. nmasks = stuff->planes;
  2212. length = ((long) npixels + (long) nmasks) * sizeof(Pixel);
  2213. ppixels = (Pixel *) ALLOCATE_LOCAL(length);
  2214. if (!ppixels)
  2215. return (BadAlloc);
  2216. pmasks = ppixels + npixels;
  2217. if ((retval = AllocColorCells(client->index, pcmp, npixels, nmasks,
  2218. (Bool) stuff->contiguous, ppixels,
  2219. pmasks))) {
  2220. DEALLOCATE_LOCAL(ppixels);
  2221. if (client->noClientException != Success)
  2222. return (client->noClientException);
  2223. else
  2224. return (retval);
  2225. }
  2226. {
  2227. accr.type = X_Reply;
  2228. accr.length = length >> 2;
  2229. accr.sequenceNumber = client->sequence;
  2230. accr.nPixels = npixels;
  2231. accr.nMasks = nmasks;
  2232. WriteReplyToClient(client, sizeof(xAllocColorCellsReply), &accr);
  2233. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  2234. WriteSwappedDataToClient(client, length, ppixels);
  2235. }
  2236. DEALLOCATE_LOCAL(ppixels);
  2237. return (client->noClientException);
  2238. }
  2239. else {
  2240. client->errorValue = stuff->cmap;
  2241. return (BadColor);
  2242. }
  2243. }
  2244. int
  2245. ProcAllocColorPlanes(register ClientPtr client)
  2246. {
  2247. ColormapPtr pcmp;
  2248. REQUEST(xAllocColorPlanesReq);
  2249. REQUEST_SIZE_MATCH(xAllocColorPlanesReq);
  2250. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2251. RT_COLORMAP,
  2252. SecurityWriteAccess);
  2253. if (pcmp) {
  2254. xAllocColorPlanesReply acpr;
  2255. int npixels, retval;
  2256. long length;
  2257. Pixel *ppixels;
  2258. npixels = stuff->colors;
  2259. if (!npixels) {
  2260. client->errorValue = npixels;
  2261. return (BadValue);
  2262. }
  2263. if (stuff->contiguous != xTrue && stuff->contiguous != xFalse) {
  2264. client->errorValue = stuff->contiguous;
  2265. return (BadValue);
  2266. }
  2267. acpr.type = X_Reply;
  2268. acpr.sequenceNumber = client->sequence;
  2269. acpr.nPixels = npixels;
  2270. length = (long) npixels *sizeof(Pixel);
  2271. ppixels = (Pixel *) ALLOCATE_LOCAL(length);
  2272. if (!ppixels)
  2273. return (BadAlloc);
  2274. if ((retval = AllocColorPlanes(client->index, pcmp, npixels,
  2275. (int) stuff->red, (int) stuff->green,
  2276. (int) stuff->blue,
  2277. (Bool) stuff->contiguous, ppixels,
  2278. &acpr.redMask, &acpr.greenMask,
  2279. &acpr.blueMask))) {
  2280. DEALLOCATE_LOCAL(ppixels);
  2281. if (client->noClientException != Success)
  2282. return (client->noClientException);
  2283. else
  2284. return (retval);
  2285. }
  2286. acpr.length = length >> 2;
  2287. {
  2288. WriteReplyToClient(client, sizeof(xAllocColorPlanesReply), &acpr);
  2289. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  2290. WriteSwappedDataToClient(client, length, ppixels);
  2291. }
  2292. DEALLOCATE_LOCAL(ppixels);
  2293. return (client->noClientException);
  2294. }
  2295. else {
  2296. client->errorValue = stuff->cmap;
  2297. return (BadColor);
  2298. }
  2299. }
  2300. int
  2301. ProcFreeColors(register ClientPtr client)
  2302. {
  2303. ColormapPtr pcmp;
  2304. REQUEST(xFreeColorsReq);
  2305. REQUEST_AT_LEAST_SIZE(xFreeColorsReq);
  2306. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2307. RT_COLORMAP,
  2308. SecurityWriteAccess);
  2309. if (pcmp) {
  2310. int count;
  2311. int retval;
  2312. if (pcmp->flags & AllAllocated)
  2313. return (BadAccess);
  2314. count = ((client->req_len << 2) - sizeof(xFreeColorsReq)) >> 2;
  2315. retval = FreeColors(pcmp, client->index, count,
  2316. (Pixel *) & stuff[1], (Pixel) stuff->planeMask);
  2317. if (client->noClientException != Success)
  2318. return (client->noClientException);
  2319. else {
  2320. client->errorValue = clientErrorValue;
  2321. return (retval);
  2322. }
  2323. }
  2324. else {
  2325. client->errorValue = stuff->cmap;
  2326. return (BadColor);
  2327. }
  2328. }
  2329. int
  2330. ProcStoreColors(ClientPtr client)
  2331. {
  2332. ColormapPtr pcmp;
  2333. REQUEST(xStoreColorsReq);
  2334. REQUEST_AT_LEAST_SIZE(xStoreColorsReq);
  2335. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2336. RT_COLORMAP,
  2337. SecurityWriteAccess);
  2338. if (pcmp) {
  2339. int count;
  2340. int retval;
  2341. count = (client->req_len << 2) - sizeof(xStoreColorsReq);
  2342. if (count % sizeof(xColorItem))
  2343. return (BadLength);
  2344. count /= sizeof(xColorItem);
  2345. retval = StoreColors(pcmp, count, (xColorItem *) & stuff[1]);
  2346. if (client->noClientException != Success)
  2347. return (client->noClientException);
  2348. else {
  2349. client->errorValue = clientErrorValue;
  2350. return (retval);
  2351. }
  2352. }
  2353. else {
  2354. client->errorValue = stuff->cmap;
  2355. return (BadColor);
  2356. }
  2357. }
  2358. int
  2359. ProcStoreNamedColor(register ClientPtr client)
  2360. {
  2361. ColormapPtr pcmp;
  2362. REQUEST(xStoreNamedColorReq);
  2363. REQUEST_FIXED_SIZE(xStoreNamedColorReq, stuff->nbytes);
  2364. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2365. RT_COLORMAP,
  2366. SecurityWriteAccess);
  2367. if (pcmp) {
  2368. xColorItem def;
  2369. int retval;
  2370. if (OsLookupColor(pcmp->pScreen->myNum, (char *) &stuff[1],
  2371. stuff->nbytes, &def.red, &def.green, &def.blue)) {
  2372. def.flags = stuff->flags;
  2373. def.pixel = stuff->pixel;
  2374. retval = StoreColors(pcmp, 1, &def);
  2375. if (client->noClientException != Success)
  2376. return (client->noClientException);
  2377. else
  2378. return (retval);
  2379. }
  2380. return (BadName);
  2381. }
  2382. else {
  2383. client->errorValue = stuff->cmap;
  2384. return (BadColor);
  2385. }
  2386. }
  2387. int
  2388. ProcQueryColors(register ClientPtr client)
  2389. {
  2390. ColormapPtr pcmp;
  2391. REQUEST(xQueryColorsReq);
  2392. REQUEST_AT_LEAST_SIZE(xQueryColorsReq);
  2393. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2394. RT_COLORMAP,
  2395. SecurityReadAccess);
  2396. if (pcmp) {
  2397. int count, retval;
  2398. xrgb *prgbs;
  2399. xQueryColorsReply qcr = {0};
  2400. count = ((client->req_len << 2) - sizeof(xQueryColorsReq)) >> 2;
  2401. prgbs = (xrgb *) ALLOCATE_LOCAL(count * sizeof(xrgb));
  2402. memset(prgbs, 0, count * sizeof(xrgb));
  2403. if (!prgbs && count)
  2404. return (BadAlloc);
  2405. if ((retval = QueryColors(pcmp, count, (Pixel *) & stuff[1], prgbs))) {
  2406. if (prgbs)
  2407. DEALLOCATE_LOCAL(prgbs);
  2408. if (client->noClientException != Success)
  2409. return (client->noClientException);
  2410. else {
  2411. client->errorValue = clientErrorValue;
  2412. return (retval);
  2413. }
  2414. }
  2415. qcr.type = X_Reply;
  2416. qcr.length = (count * sizeof(xrgb)) >> 2;
  2417. qcr.sequenceNumber = client->sequence;
  2418. qcr.nColors = count;
  2419. WriteReplyToClient(client, sizeof(xQueryColorsReply), &qcr);
  2420. if (count) {
  2421. client->pSwapReplyFunc = (ReplySwapPtr) SQColorsExtend;
  2422. WriteSwappedDataToClient(client, count * sizeof(xrgb), prgbs);
  2423. }
  2424. if (prgbs)
  2425. DEALLOCATE_LOCAL(prgbs);
  2426. return (client->noClientException);
  2427. }
  2428. else {
  2429. client->errorValue = stuff->cmap;
  2430. return (BadColor);
  2431. }
  2432. }
  2433. int
  2434. ProcLookupColor(register ClientPtr client)
  2435. {
  2436. ColormapPtr pcmp;
  2437. REQUEST(xLookupColorReq);
  2438. REQUEST_FIXED_SIZE(xLookupColorReq, stuff->nbytes);
  2439. pcmp = (ColormapPtr) SecurityLookupIDByType(client, stuff->cmap,
  2440. RT_COLORMAP,
  2441. SecurityReadAccess);
  2442. if (pcmp) {
  2443. xLookupColorReply lcr = {0};
  2444. if (OsLookupColor
  2445. (pcmp->pScreen->myNum, (char *) &stuff[1], stuff->nbytes,
  2446. &lcr.exactRed, &lcr.exactGreen, &lcr.exactBlue)) {
  2447. lcr.type = X_Reply;
  2448. lcr.length = 0;
  2449. lcr.sequenceNumber = client->sequence;
  2450. lcr.screenRed = lcr.exactRed;
  2451. lcr.screenGreen = lcr.exactGreen;
  2452. lcr.screenBlue = lcr.exactBlue;
  2453. (*pcmp->pScreen->ResolveColor) (&lcr.screenRed,
  2454. &lcr.screenGreen,
  2455. &lcr.screenBlue, pcmp->pVisual);
  2456. WriteReplyToClient(client, sizeof(xLookupColorReply), &lcr);
  2457. return (client->noClientException);
  2458. }
  2459. return (BadName);
  2460. }
  2461. else {
  2462. client->errorValue = stuff->cmap;
  2463. return (BadColor);
  2464. }
  2465. }
  2466. int
  2467. ProcCreateCursor(register ClientPtr client)
  2468. {
  2469. CursorPtr pCursor;
  2470. PixmapPtr src;
  2471. PixmapPtr msk;
  2472. unsigned char *srcbits;
  2473. unsigned char *mskbits;
  2474. unsigned short width, height;
  2475. long n;
  2476. CursorMetricRec cm;
  2477. REQUEST(xCreateCursorReq);
  2478. REQUEST_SIZE_MATCH(xCreateCursorReq);
  2479. LEGAL_NEW_RESOURCE(stuff->cid, client);
  2480. src = (PixmapPtr) SecurityLookupIDByType(client, stuff->source,
  2481. RT_PIXMAP, SecurityReadAccess);
  2482. msk = (PixmapPtr) SecurityLookupIDByType(client, stuff->mask,
  2483. RT_PIXMAP, SecurityReadAccess);
  2484. if (src == (PixmapPtr) NULL) {
  2485. client->errorValue = stuff->source;
  2486. return (BadPixmap);
  2487. }
  2488. if (msk == (PixmapPtr) NULL) {
  2489. if (stuff->mask != None) {
  2490. client->errorValue = stuff->mask;
  2491. return (BadPixmap);
  2492. }
  2493. }
  2494. else if (src->drawable.width != msk->drawable.width
  2495. || src->drawable.height != msk->drawable.height
  2496. || src->drawable.depth != 1 || msk->drawable.depth != 1)
  2497. return (BadMatch);
  2498. width = src->drawable.width;
  2499. height = src->drawable.height;
  2500. if (stuff->x > width || stuff->y > height)
  2501. return (BadMatch);
  2502. n = BitmapBytePad(width) * height;
  2503. srcbits = malloc(n);
  2504. if (!srcbits)
  2505. return (BadAlloc);
  2506. mskbits = malloc(n);
  2507. if (!mskbits) {
  2508. free(srcbits);
  2509. return (BadAlloc);
  2510. }
  2511. /* zeroing the (pad) bits helps some ddx cursor handling */
  2512. bzero((char *) srcbits, n);
  2513. (*src->drawable.pScreen->GetImage) ((DrawablePtr) src, 0, 0, width, height,
  2514. XYPixmap, 1, (pointer) srcbits);
  2515. if (msk == (PixmapPtr) NULL) {
  2516. unsigned char *bits = mskbits;
  2517. while (--n >= 0)
  2518. *bits++ = ~0;
  2519. }
  2520. else {
  2521. /* zeroing the (pad) bits helps some ddx cursor handling */
  2522. bzero((char *) mskbits, n);
  2523. (*msk->drawable.pScreen->GetImage) ((DrawablePtr) msk, 0, 0, width,
  2524. height, XYPixmap, 1,
  2525. (pointer) mskbits);
  2526. }
  2527. cm.width = width;
  2528. cm.height = height;
  2529. cm.xhot = stuff->x;
  2530. cm.yhot = stuff->y;
  2531. pCursor = AllocCursor(srcbits, mskbits, &cm,
  2532. stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2533. stuff->backRed, stuff->backGreen, stuff->backBlue);
  2534. if (pCursor && AddResource(stuff->cid, RT_CURSOR, (pointer) pCursor))
  2535. return (client->noClientException);
  2536. return BadAlloc;
  2537. }
  2538. int
  2539. ProcCreateGlyphCursor(register ClientPtr client)
  2540. {
  2541. CursorPtr pCursor;
  2542. int res;
  2543. REQUEST(xCreateGlyphCursorReq);
  2544. REQUEST_SIZE_MATCH(xCreateGlyphCursorReq);
  2545. LEGAL_NEW_RESOURCE(stuff->cid, client);
  2546. res = AllocGlyphCursor(stuff->source, stuff->sourceChar,
  2547. stuff->mask, stuff->maskChar,
  2548. stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2549. stuff->backRed, stuff->backGreen, stuff->backBlue,
  2550. &pCursor, client);
  2551. if (res != Success)
  2552. return res;
  2553. if (AddResource(stuff->cid, RT_CURSOR, (pointer) pCursor))
  2554. return client->noClientException;
  2555. return BadAlloc;
  2556. }
  2557. int
  2558. ProcFreeCursor(register ClientPtr client)
  2559. {
  2560. CursorPtr pCursor;
  2561. REQUEST(xResourceReq);
  2562. REQUEST_SIZE_MATCH(xResourceReq);
  2563. pCursor = (CursorPtr) SecurityLookupIDByType(client, stuff->id,
  2564. RT_CURSOR,
  2565. SecurityDestroyAccess);
  2566. if (pCursor) {
  2567. FreeResource(stuff->id, RT_NONE);
  2568. return (client->noClientException);
  2569. }
  2570. else {
  2571. client->errorValue = stuff->id;
  2572. return (BadCursor);
  2573. }
  2574. }
  2575. int
  2576. ProcQueryBestSize(register ClientPtr client)
  2577. {
  2578. xQueryBestSizeReply reply = {0};
  2579. DrawablePtr pDraw;
  2580. ScreenPtr pScreen;
  2581. REQUEST(xQueryBestSizeReq);
  2582. REQUEST_SIZE_MATCH(xQueryBestSizeReq);
  2583. if ((stuff->class != CursorShape) &&
  2584. (stuff->class != TileShape) && (stuff->class != StippleShape)) {
  2585. client->errorValue = stuff->class;
  2586. return (BadValue);
  2587. }
  2588. SECURITY_VERIFY_GEOMETRABLE(pDraw, stuff->drawable, client,
  2589. SecurityReadAccess);
  2590. if (stuff->class != CursorShape && pDraw->type == UNDRAWABLE_WINDOW)
  2591. return (BadMatch);
  2592. pScreen = pDraw->pScreen;
  2593. (*pScreen->QueryBestSize) (stuff->class, &stuff->width,
  2594. &stuff->height, pScreen);
  2595. reply.type = X_Reply;
  2596. reply.length = 0;
  2597. reply.sequenceNumber = client->sequence;
  2598. reply.width = stuff->width;
  2599. reply.height = stuff->height;
  2600. WriteReplyToClient(client, sizeof(xQueryBestSizeReply), &reply);
  2601. return (client->noClientException);
  2602. }
  2603. int
  2604. ProcSetScreenSaver(register ClientPtr client)
  2605. {
  2606. int blankingOption, exposureOption;
  2607. REQUEST(xSetScreenSaverReq);
  2608. REQUEST_SIZE_MATCH(xSetScreenSaverReq);
  2609. blankingOption = stuff->preferBlank;
  2610. if ((blankingOption != DontPreferBlanking) &&
  2611. (blankingOption != PreferBlanking) &&
  2612. (blankingOption != DefaultBlanking)) {
  2613. client->errorValue = blankingOption;
  2614. return BadValue;
  2615. }
  2616. exposureOption = stuff->allowExpose;
  2617. if ((exposureOption != DontAllowExposures) &&
  2618. (exposureOption != AllowExposures) &&
  2619. (exposureOption != DefaultExposures)) {
  2620. client->errorValue = exposureOption;
  2621. return BadValue;
  2622. }
  2623. if (stuff->timeout < -1) {
  2624. client->errorValue = stuff->timeout;
  2625. return BadValue;
  2626. }
  2627. if (stuff->interval < -1) {
  2628. client->errorValue = stuff->interval;
  2629. return BadValue;
  2630. }
  2631. if (blankingOption == DefaultBlanking)
  2632. ScreenSaverBlanking = defaultScreenSaverBlanking;
  2633. else
  2634. ScreenSaverBlanking = blankingOption;
  2635. if (exposureOption == DefaultExposures)
  2636. ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
  2637. else
  2638. ScreenSaverAllowExposures = exposureOption;
  2639. if (stuff->timeout >= 0)
  2640. ScreenSaverTime = stuff->timeout * MILLI_PER_SECOND;
  2641. else
  2642. ScreenSaverTime = defaultScreenSaverTime;
  2643. if (stuff->interval >= 0)
  2644. ScreenSaverInterval = stuff->interval * MILLI_PER_SECOND;
  2645. else
  2646. ScreenSaverInterval = defaultScreenSaverInterval;
  2647. SetScreenSaverTimer();
  2648. return (client->noClientException);
  2649. }
  2650. int
  2651. ProcGetScreenSaver(register ClientPtr client)
  2652. {
  2653. xGetScreenSaverReply rep;
  2654. REQUEST_SIZE_MATCH(xReq);
  2655. rep.type = X_Reply;
  2656. rep.length = 0;
  2657. rep.sequenceNumber = client->sequence;
  2658. rep.timeout = ScreenSaverTime / MILLI_PER_SECOND;
  2659. rep.interval = ScreenSaverInterval / MILLI_PER_SECOND;
  2660. rep.preferBlanking = ScreenSaverBlanking;
  2661. rep.allowExposures = ScreenSaverAllowExposures;
  2662. WriteReplyToClient(client, sizeof(xGetScreenSaverReply), &rep);
  2663. return (client->noClientException);
  2664. }
  2665. int
  2666. ProcChangeHosts(register ClientPtr client)
  2667. {
  2668. REQUEST(xChangeHostsReq);
  2669. int result;
  2670. REQUEST_FIXED_SIZE(xChangeHostsReq, stuff->hostLength);
  2671. if (stuff->mode == HostInsert)
  2672. result = AddHost(client, (int) stuff->hostFamily,
  2673. stuff->hostLength, (pointer) &stuff[1]);
  2674. else if (stuff->mode == HostDelete)
  2675. result = RemoveHost(client, (int) stuff->hostFamily,
  2676. stuff->hostLength, (pointer) &stuff[1]);
  2677. else {
  2678. client->errorValue = stuff->mode;
  2679. return BadValue;
  2680. }
  2681. if (!result)
  2682. result = client->noClientException;
  2683. return (result);
  2684. }
  2685. int
  2686. ProcListHosts(register ClientPtr client)
  2687. {
  2688. xListHostsReply reply;
  2689. int len, nHosts, result;
  2690. pointer pdata;
  2691. /* REQUEST(xListHostsReq); */
  2692. REQUEST_SIZE_MATCH(xListHostsReq);
  2693. result = GetHosts(&pdata, &nHosts, &len, &reply.enabled);
  2694. if (result != Success)
  2695. return (result);
  2696. reply.type = X_Reply;
  2697. reply.sequenceNumber = client->sequence;
  2698. reply.nHosts = nHosts;
  2699. reply.length = len >> 2;
  2700. WriteReplyToClient(client, sizeof(xListHostsReply), &reply);
  2701. if (nHosts) {
  2702. client->pSwapReplyFunc = (ReplySwapPtr) SLHostsExtend;
  2703. WriteSwappedDataToClient(client, len, pdata);
  2704. }
  2705. free(pdata);
  2706. return (client->noClientException);
  2707. }
  2708. int
  2709. ProcChangeAccessControl(register ClientPtr client)
  2710. {
  2711. int result;
  2712. REQUEST(xSetAccessControlReq);
  2713. REQUEST_SIZE_MATCH(xSetAccessControlReq);
  2714. if ((stuff->mode != EnableAccess) && (stuff->mode != DisableAccess)) {
  2715. client->errorValue = stuff->mode;
  2716. return BadValue;
  2717. }
  2718. result = ChangeAccessControl(client, stuff->mode == EnableAccess);
  2719. if (!result)
  2720. result = client->noClientException;
  2721. return (result);
  2722. }
  2723. int
  2724. ProcKillClient(register ClientPtr client)
  2725. {
  2726. REQUEST(xResourceReq);
  2727. ClientPtr killclient;
  2728. REQUEST_SIZE_MATCH(xResourceReq);
  2729. if (stuff->id == AllTemporary) {
  2730. CloseDownRetainedResources();
  2731. return (client->noClientException);
  2732. }
  2733. if ((killclient = LookupClient(stuff->id, client))) {
  2734. CloseDownClient(killclient);
  2735. /* if an LBX proxy gets killed, isItTimeToYield will be set */
  2736. if (isItTimeToYield || (client == killclient)) {
  2737. /* force yield and return Success, so that Dispatch()
  2738. * doesn't try to touch client
  2739. */
  2740. isItTimeToYield = TRUE;
  2741. return (Success);
  2742. }
  2743. return (client->noClientException);
  2744. }
  2745. else {
  2746. client->errorValue = stuff->id;
  2747. return (BadValue);
  2748. }
  2749. }
  2750. int
  2751. ProcSetFontPath(register ClientPtr client)
  2752. {
  2753. unsigned char *ptr;
  2754. unsigned long nbytes, total;
  2755. long nfonts;
  2756. int n, result;
  2757. int error;
  2758. REQUEST(xSetFontPathReq);
  2759. REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
  2760. nbytes = (client->req_len << 2) - sizeof(xSetFontPathReq);
  2761. total = nbytes;
  2762. ptr = (unsigned char *) &stuff[1];
  2763. nfonts = stuff->nFonts;
  2764. while (--nfonts >= 0) {
  2765. if ((total == 0) || (total < (n = (*ptr + 1))))
  2766. return (BadLength);
  2767. total -= n;
  2768. ptr += n;
  2769. }
  2770. if (total >= 4)
  2771. return (BadLength);
  2772. result = SetFontPath(client, stuff->nFonts, (unsigned char *) &stuff[1],
  2773. &error);
  2774. if (!result) {
  2775. result = client->noClientException;
  2776. client->errorValue = error;
  2777. }
  2778. return (result);
  2779. }
  2780. int
  2781. ProcGetFontPath(register ClientPtr client)
  2782. {
  2783. xGetFontPathReply reply;
  2784. int stringLens, numpaths;
  2785. unsigned char *bufferStart;
  2786. /* REQUEST (xReq); */
  2787. REQUEST_SIZE_MATCH(xReq);
  2788. bufferStart = GetFontPath(&numpaths, &stringLens);
  2789. reply.type = X_Reply;
  2790. reply.sequenceNumber = client->sequence;
  2791. reply.length = (stringLens + numpaths + 3) >> 2;
  2792. reply.nPaths = numpaths;
  2793. WriteReplyToClient(client, sizeof(xGetFontPathReply), &reply);
  2794. if (stringLens || numpaths)
  2795. (void) WriteToClient(client, stringLens + numpaths,
  2796. (char *) bufferStart);
  2797. return (client->noClientException);
  2798. }
  2799. int
  2800. ProcChangeCloseDownMode(register ClientPtr client)
  2801. {
  2802. REQUEST(xSetCloseDownModeReq);
  2803. REQUEST_SIZE_MATCH(xSetCloseDownModeReq);
  2804. if ((stuff->mode == AllTemporary) ||
  2805. (stuff->mode == RetainPermanent) || (stuff->mode == RetainTemporary)) {
  2806. client->closeDownMode = stuff->mode;
  2807. return (client->noClientException);
  2808. }
  2809. else {
  2810. client->errorValue = stuff->mode;
  2811. return (BadValue);
  2812. }
  2813. }
  2814. int
  2815. ProcForceScreenSaver(register ClientPtr client)
  2816. {
  2817. REQUEST(xForceScreenSaverReq);
  2818. REQUEST_SIZE_MATCH(xForceScreenSaverReq);
  2819. if ((stuff->mode != ScreenSaverReset) && (stuff->mode != ScreenSaverActive)) {
  2820. client->errorValue = stuff->mode;
  2821. return BadValue;
  2822. }
  2823. SaveScreens(SCREEN_SAVER_FORCER, (int) stuff->mode);
  2824. return client->noClientException;
  2825. }
  2826. int
  2827. ProcNoOperation(register ClientPtr client)
  2828. {
  2829. REQUEST_AT_LEAST_SIZE(xReq);
  2830. /* noop -- don't do anything */
  2831. return (client->noClientException);
  2832. }
  2833. /**********************
  2834. * CloseDownClient
  2835. *
  2836. * Client can either mark his resources destroy or retain. If retained and
  2837. * then killed again, the client is really destroyed.
  2838. *********************/
  2839. char dispatchExceptionAtReset = 0;
  2840. void
  2841. CloseDownClient(register ClientPtr client)
  2842. {
  2843. Bool really_close_down = client->clientGone ||
  2844. client->closeDownMode == DestroyAll;
  2845. if (!client->clientGone) {
  2846. /* ungrab server if grabbing client dies */
  2847. if (grabState != GrabNone && grabClient == client) {
  2848. UngrabServer(client);
  2849. }
  2850. BITCLEAR(grabWaiters, client->index);
  2851. DeleteClientFromAnySelections(client);
  2852. ReleaseActiveGrabs(client);
  2853. DeleteClientFontStuff(client);
  2854. if (!really_close_down) {
  2855. /* This frees resources that should never be retained
  2856. * no matter what the close down mode is. Actually we
  2857. * could do this unconditionally, but it's probably
  2858. * better not to traverse all the client's resources
  2859. * twice (once here, once a few lines down in
  2860. * FreeClientResources) in the common case of
  2861. * really_close_down == TRUE.
  2862. */
  2863. FreeClientNeverRetainResources(client);
  2864. client->clientState = ClientStateRetained;
  2865. if (ClientStateCallback) {
  2866. NewClientInfoRec clientinfo;
  2867. clientinfo.client = client;
  2868. clientinfo.prefix = (xConnSetupPrefix *) NULL;
  2869. clientinfo.setup = (xConnSetup *) NULL;
  2870. CallCallbacks((&ClientStateCallback), (pointer) &clientinfo);
  2871. }
  2872. }
  2873. client->clientGone = TRUE; /* so events aren't sent to client */
  2874. if (ClientIsAsleep(client))
  2875. ClientSignal(client);
  2876. ProcessWorkQueueZombies();
  2877. CloseDownConnection(client);
  2878. /* If the client made it to the Running stage, nClients has
  2879. * been incremented on its behalf, so we need to decrement it
  2880. * now. If it hasn't gotten to Running, nClients has *not*
  2881. * been incremented, so *don't* decrement it.
  2882. */
  2883. if (client->clientState != ClientStateInitial &&
  2884. client->clientState != ClientStateAuthenticating) {
  2885. --nClients;
  2886. }
  2887. }
  2888. if (really_close_down) {
  2889. if (client->clientState == ClientStateRunning && nClients == 0)
  2890. dispatchException |= dispatchExceptionAtReset;
  2891. client->clientState = ClientStateGone;
  2892. if (ClientStateCallback) {
  2893. NewClientInfoRec clientinfo;
  2894. clientinfo.client = client;
  2895. clientinfo.prefix = (xConnSetupPrefix *) NULL;
  2896. clientinfo.setup = (xConnSetup *) NULL;
  2897. CallCallbacks((&ClientStateCallback), (pointer) &clientinfo);
  2898. }
  2899. FreeClientResources(client);
  2900. if (client->index < nextFreeClientID)
  2901. nextFreeClientID = client->index;
  2902. clients[client->index] = NullClient;
  2903. #ifdef SMART_SCHEDULE
  2904. SmartLastClient = NullClient;
  2905. #endif
  2906. free(client);
  2907. while (!clients[currentMaxClients - 1])
  2908. currentMaxClients--;
  2909. }
  2910. }
  2911. static void
  2912. KillAllClients()
  2913. {
  2914. int i;
  2915. for (i = 1; i < currentMaxClients; i++)
  2916. if (clients[i]) {
  2917. /* Make sure Retained clients are released. */
  2918. clients[i]->closeDownMode = DestroyAll;
  2919. CloseDownClient(clients[i]);
  2920. }
  2921. }
  2922. /*********************
  2923. * CloseDownRetainedResources
  2924. *
  2925. * Find all clients that are gone and have terminated in RetainTemporary
  2926. * and destroy their resources.
  2927. *********************/
  2928. void
  2929. CloseDownRetainedResources()
  2930. {
  2931. int i;
  2932. ClientPtr client;
  2933. for (i = 1; i < currentMaxClients; i++) {
  2934. client = clients[i];
  2935. if (client && (client->closeDownMode == RetainTemporary)
  2936. && (client->clientGone))
  2937. CloseDownClient(client);
  2938. }
  2939. }
  2940. extern int clientPrivateLen;
  2941. extern unsigned *clientPrivateSizes;
  2942. extern unsigned totalClientSize;
  2943. void
  2944. InitClient(ClientPtr client, int i, pointer ospriv)
  2945. {
  2946. client->index = i;
  2947. client->sequence = 0;
  2948. client->clientAsMask = ((Mask) i) << CLIENTOFFSET;
  2949. client->clientGone = FALSE;
  2950. if (i) {
  2951. client->closeDownMode = DestroyAll;
  2952. client->lastDrawable = (DrawablePtr) WindowTable[0];
  2953. client->lastDrawableID = WindowTable[0]->drawable.id;
  2954. }
  2955. else {
  2956. client->closeDownMode = RetainPermanent;
  2957. client->lastDrawable = (DrawablePtr) NULL;
  2958. client->lastDrawableID = INVALID;
  2959. }
  2960. client->lastGC = (GCPtr) NULL;
  2961. client->lastGCID = INVALID;
  2962. client->numSaved = 0;
  2963. client->saveSet = (SaveSetElt *) NULL;
  2964. client->noClientException = Success;
  2965. #ifdef DEBUG
  2966. client->requestLogIndex = 0;
  2967. #endif
  2968. client->requestVector = InitialVector;
  2969. client->osPrivate = ospriv;
  2970. client->swapped = FALSE;
  2971. client->big_requests = FALSE;
  2972. client->priority = 0;
  2973. client->clientState = ClientStateInitial;
  2974. client->replyBytesRemaining = 0;
  2975. client->fontResFunc = NULL;
  2976. #ifdef SMART_SCHEDULE
  2977. client->smart_priority = 0;
  2978. client->smart_start_tick = SmartScheduleTime;
  2979. client->smart_stop_tick = SmartScheduleTime;
  2980. client->smart_check_tick = SmartScheduleTime;
  2981. #endif
  2982. }
  2983. int
  2984. InitClientPrivates(ClientPtr client)
  2985. {
  2986. char *ptr;
  2987. DevUnion *ppriv;
  2988. unsigned *sizes;
  2989. unsigned size;
  2990. int i;
  2991. if (totalClientSize == sizeof(ClientRec))
  2992. ppriv = (DevUnion *) NULL;
  2993. else if (client->index)
  2994. ppriv = (DevUnion *) (client + 1);
  2995. else {
  2996. ppriv = malloc(totalClientSize - sizeof(ClientRec));
  2997. if (!ppriv)
  2998. return 0;
  2999. }
  3000. client->devPrivates = ppriv;
  3001. sizes = clientPrivateSizes;
  3002. ptr = (char *) (ppriv + clientPrivateLen);
  3003. for (i = clientPrivateLen; --i >= 0; ppriv++, sizes++) {
  3004. if ((size = *sizes)) {
  3005. ppriv->ptr = (pointer) ptr;
  3006. ptr += size;
  3007. }
  3008. else
  3009. ppriv->ptr = (pointer) NULL;
  3010. }
  3011. /* Allow registrants to initialize the serverClient devPrivates */
  3012. if (!client->index && ClientStateCallback) {
  3013. NewClientInfoRec clientinfo;
  3014. clientinfo.client = client;
  3015. clientinfo.prefix = (xConnSetupPrefix *) NULL;
  3016. clientinfo.setup = (xConnSetup *) NULL;
  3017. CallCallbacks((&ClientStateCallback), (pointer) &clientinfo);
  3018. }
  3019. return 1;
  3020. }
  3021. /************************
  3022. * int NextAvailableClient(ospriv)
  3023. *
  3024. * OS dependent portion can't assign client id's because of CloseDownModes.
  3025. * Returns NULL if there are no free clients.
  3026. *************************/
  3027. ClientPtr
  3028. NextAvailableClient(pointer ospriv)
  3029. {
  3030. int i;
  3031. ClientPtr client;
  3032. xReq data;
  3033. i = nextFreeClientID;
  3034. if (i == MAXCLIENTS)
  3035. return (ClientPtr) NULL;
  3036. clients[i] = client = malloc(totalClientSize);
  3037. if (!client)
  3038. return (ClientPtr) NULL;
  3039. InitClient(client, i, ospriv);
  3040. InitClientPrivates(client);
  3041. if (!InitClientResources(client)) {
  3042. free(client);
  3043. return (ClientPtr) NULL;
  3044. }
  3045. data.reqType = 1;
  3046. data.length = (sz_xReq + sz_xConnClientPrefix) >> 2;
  3047. if (!InsertFakeRequest(client, (char *) &data, sz_xReq)) {
  3048. FreeClientResources(client);
  3049. free(client);
  3050. return (ClientPtr) NULL;
  3051. }
  3052. if (i == currentMaxClients)
  3053. currentMaxClients++;
  3054. while ((nextFreeClientID < MAXCLIENTS) && clients[nextFreeClientID])
  3055. nextFreeClientID++;
  3056. if (ClientStateCallback) {
  3057. NewClientInfoRec clientinfo;
  3058. clientinfo.client = client;
  3059. clientinfo.prefix = (xConnSetupPrefix *) NULL;
  3060. clientinfo.setup = (xConnSetup *) NULL;
  3061. CallCallbacks((&ClientStateCallback), (pointer) &clientinfo);
  3062. }
  3063. return (client);
  3064. }
  3065. int
  3066. ProcInitialConnection(register ClientPtr client)
  3067. {
  3068. REQUEST(xReq);
  3069. xConnClientPrefix *prefix;
  3070. int whichbyte = 1;
  3071. prefix = (xConnClientPrefix *) ((char *) stuff + sz_xReq);
  3072. if ((prefix->byteOrder != 'l') && (prefix->byteOrder != 'B'))
  3073. return (client->noClientException = -1);
  3074. if (((*(char *) &whichbyte) && (prefix->byteOrder == 'B')) ||
  3075. (!(*(char *) &whichbyte) && (prefix->byteOrder == 'l'))) {
  3076. client->swapped = TRUE;
  3077. SwapConnClientPrefix(prefix);
  3078. }
  3079. stuff->reqType = 2;
  3080. stuff->length += ((prefix->nbytesAuthProto + (unsigned) 3) >> 2) +
  3081. ((prefix->nbytesAuthString + (unsigned) 3) >> 2);
  3082. if (client->swapped) {
  3083. swaps(&stuff->length);
  3084. }
  3085. ResetCurrentRequest(client);
  3086. return (client->noClientException);
  3087. }
  3088. int
  3089. SendConnSetup(register ClientPtr client, const char *reason)
  3090. {
  3091. xWindowRoot *root;
  3092. int i;
  3093. int numScreens;
  3094. char *lConnectionInfo;
  3095. xConnSetupPrefix *lconnSetupPrefix;
  3096. if (reason) {
  3097. xConnSetupPrefix csp;
  3098. csp.success = xFalse;
  3099. csp.lengthReason = strlen(reason);
  3100. csp.length = (csp.lengthReason + (unsigned) 3) >> 2;
  3101. csp.majorVersion = X_PROTOCOL;
  3102. csp.minorVersion = X_PROTOCOL_REVISION;
  3103. if (client->swapped)
  3104. WriteSConnSetupPrefix(client, &csp);
  3105. else
  3106. (void) WriteToClient(client, sz_xConnSetupPrefix, (char *) &csp);
  3107. (void) WriteToClient(client, (int) csp.lengthReason, reason);
  3108. return (client->noClientException = -1);
  3109. }
  3110. numScreens = screenInfo.numScreens;
  3111. lConnectionInfo = ConnectionInfo;
  3112. lconnSetupPrefix = &connSetupPrefix;
  3113. /* We're about to start speaking X protocol back to the client by
  3114. * sending the connection setup info. This means the authorization
  3115. * step is complete, and we can count the client as an
  3116. * authorized one.
  3117. */
  3118. nClients++;
  3119. client->requestVector = client->swapped ? SwappedProcVector : ProcVector;
  3120. client->sequence = 0;
  3121. ((xConnSetup *) lConnectionInfo)->ridBase = client->clientAsMask;
  3122. ((xConnSetup *) lConnectionInfo)->ridMask = RESOURCE_ID_MASK;
  3123. #ifdef MATCH_CLIENT_ENDIAN
  3124. ((xConnSetup *) lConnectionInfo)->imageByteOrder = ClientOrder(client);
  3125. ((xConnSetup *) lConnectionInfo)->bitmapBitOrder = ClientOrder(client);
  3126. #endif
  3127. /* fill in the "currentInputMask" */
  3128. root = (xWindowRoot *) (lConnectionInfo + connBlockScreenStart);
  3129. for (i = 0; i < numScreens; i++) {
  3130. unsigned int j;
  3131. xDepth *pDepth;
  3132. root->currentInputMask = WindowTable[i]->eventMask |
  3133. wOtherEventMasks(WindowTable[i]);
  3134. pDepth = (xDepth *) (root + 1);
  3135. for (j = 0; j < root->nDepths; j++) {
  3136. pDepth = (xDepth *) (((char *) (pDepth + 1)) +
  3137. pDepth->nVisuals * sizeof(xVisualType));
  3138. }
  3139. root = (xWindowRoot *) pDepth;
  3140. }
  3141. if (client->swapped) {
  3142. WriteSConnSetupPrefix(client, lconnSetupPrefix);
  3143. WriteSConnectionInfo(client,
  3144. (unsigned long) (lconnSetupPrefix->length << 2),
  3145. lConnectionInfo);
  3146. }
  3147. else {
  3148. (void) WriteToClient(client, sizeof(xConnSetupPrefix),
  3149. (char *) lconnSetupPrefix);
  3150. (void) WriteToClient(client, (int) (lconnSetupPrefix->length << 2),
  3151. lConnectionInfo);
  3152. }
  3153. client->clientState = ClientStateRunning;
  3154. if (ClientStateCallback) {
  3155. NewClientInfoRec clientinfo;
  3156. clientinfo.client = client;
  3157. clientinfo.prefix = lconnSetupPrefix;
  3158. clientinfo.setup = (xConnSetup *) lConnectionInfo;
  3159. CallCallbacks((&ClientStateCallback), (pointer) &clientinfo);
  3160. }
  3161. return (client->noClientException);
  3162. }
  3163. int
  3164. ProcEstablishConnection(register ClientPtr client)
  3165. {
  3166. const char *reason;
  3167. char *auth_proto, *auth_string;
  3168. xConnClientPrefix *prefix;
  3169. REQUEST(xReq);
  3170. prefix = (xConnClientPrefix *) ((char *) stuff + sz_xReq);
  3171. auth_proto = (char *) prefix + sz_xConnClientPrefix;
  3172. auth_string = auth_proto + ((prefix->nbytesAuthProto + 3) & ~3);
  3173. if ((prefix->majorVersion != X_PROTOCOL) ||
  3174. (prefix->minorVersion != X_PROTOCOL_REVISION))
  3175. reason = "Protocol version mismatch";
  3176. else
  3177. reason = ClientAuthorized(client,
  3178. (unsigned short) prefix->nbytesAuthProto,
  3179. auth_proto,
  3180. (unsigned short) prefix->nbytesAuthString,
  3181. auth_string);
  3182. /*
  3183. * If Kerberos is being used for this client, the clientState
  3184. * will be set to ClientStateAuthenticating at this point.
  3185. * More messages need to be exchanged among the X server, Kerberos
  3186. * server, and client to figure out if everyone is authorized.
  3187. * So we don't want to send the connection setup info yet, since
  3188. * the auth step isn't really done.
  3189. */
  3190. if (client->clientState == ClientStateCheckingSecurity)
  3191. client->clientState = ClientStateCheckedSecurity;
  3192. else if (client->clientState != ClientStateAuthenticating)
  3193. return (SendConnSetup(client, reason));
  3194. return (client->noClientException);
  3195. }
  3196. _X_EXPORT void
  3197. SendErrorToClient(ClientPtr client, unsigned majorCode, unsigned minorCode,
  3198. XID resId, int errorCode)
  3199. {
  3200. xError rep;
  3201. rep.type = X_Error;
  3202. rep.errorCode = errorCode;
  3203. rep.majorCode = majorCode;
  3204. rep.minorCode = minorCode;
  3205. rep.resourceID = resId;
  3206. WriteEventsToClient(client, 1, (xEvent *) &rep);
  3207. }
  3208. void
  3209. DeleteWindowFromAnySelections(WindowPtr pWin)
  3210. {
  3211. int i;
  3212. for (i = 0; i < NumCurrentSelections; i++)
  3213. if (CurrentSelections[i].pWin == pWin) {
  3214. if (SelectionCallback) {
  3215. SelectionInfoRec info;
  3216. info.selection = &CurrentSelections[i];
  3217. info.kind = SelectionWindowDestroy;
  3218. CallCallbacks(&SelectionCallback, &info);
  3219. }
  3220. CurrentSelections[i].pWin = (WindowPtr) NULL;
  3221. CurrentSelections[i].window = None;
  3222. CurrentSelections[i].client = NullClient;
  3223. }
  3224. }
  3225. static void
  3226. DeleteClientFromAnySelections(ClientPtr client)
  3227. {
  3228. int i;
  3229. for (i = 0; i < NumCurrentSelections; i++)
  3230. if (CurrentSelections[i].client == client) {
  3231. if (SelectionCallback) {
  3232. SelectionInfoRec info;
  3233. info.selection = &CurrentSelections[i];
  3234. info.kind = SelectionWindowDestroy;
  3235. CallCallbacks(&SelectionCallback, &info);
  3236. }
  3237. CurrentSelections[i].pWin = (WindowPtr) NULL;
  3238. CurrentSelections[i].window = None;
  3239. CurrentSelections[i].client = NullClient;
  3240. }
  3241. }
  3242. void
  3243. MarkClientException(ClientPtr client)
  3244. {
  3245. client->noClientException = -1;
  3246. }