Debian Codegraph @

nx-X11/lib/src/OpenDis.c

raw
1 /**************************************************************************/
2 /* */
3 /* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
4 /* Copyright (c) 2008-2017 Oleksandr Shneyder <[email protected]> */
5 /* Copyright (c) 2011-2022 Mike Gabriel <[email protected]>*/
6 /* Copyright (c) 2014-2019 Mihai Moldovan <[email protected]> */
7 /* Copyright (c) 2014-2022 Ulrich Sibiller <[email protected]> */
8 /* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
9 /* */
10 /* nx-X11, NX protocol compression and NX extensions to this software */
11 /* are copyright of the aforementioned persons and companies. */
12 /* */
13 /* Redistribution and use of the present software is allowed according */
14 /* to terms specified in the file LICENSE which comes in the source */
15 /* distribution. */
16 /* */
17 /* All rights reserved. */
18 /* */
19 /* NOTE: This software has received contributions from various other */
20 /* contributors, only the core maintainers and supporters are listed as */
21 /* copyright holders. Please contact us, if you feel you should be listed */
22 /* as copyright holder, as well. */
23 /* */
24 /**************************************************************************/
25
26 /*
27
28 Copyright 1985, 1986, 1998 The Open Group
29
30 Permission to use, copy, modify, distribute, and sell this software and its
31 documentation for any purpose is hereby granted without fee, provided that
32 the above copyright notice appear in all copies and that both that
33 copyright notice and this permission notice appear in supporting
34 documentation.
35
36 The above copyright notice and this permission notice shall be included in
37 all copies or substantial portions of the Software.
38
39 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46 Except as contained in this notice, the name of The Open Group shall not be
47 used in advertising or otherwise to promote the sale, use or other dealings
48 in this Software without prior written authorization from The Open Group.
49
50 */
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55 #include "Xlibint.h"
56 #if USE_XCB
57 #include "Xxcbint.h"
58 #else /* !USE_XCB */
59 #include <nx-X11/Xtrans/Xtrans.h>
60 #include <nx-X11/extensions/bigreqstr.h>
61 #endif /* USE_XCB */
62 #include <nx-X11/Xatom.h>
63 #include <nx-X11/Xresource.h>
64 #include <stdio.h>
65 #include "Xintconn.h"
66
67 #ifdef XKB
68 #include "XKBlib.h"
69 #endif /* XKB */
70
71 #ifdef NX_TRANS_SOCKET
72 extern void *_X11TransSocketProxyConnInfo(XtransConnInfo);
73 #endif
74
75 #if !USE_XCB
76 #define bignamelen (sizeof(XBigReqExtensionName) - 1)
77
78 typedef struct {
79 unsigned long seq;
80 int opcode;
81 } _XBigReqState;
82 #endif /* !USE_XCB */
83
84 #ifdef XTHREADS
85 #include "locking.h"
86 int (*_XInitDisplayLock_fn)(Display *dpy) = NULL;
87 void (*_XFreeDisplayLock_fn)(Display *dpy) = NULL;
88
89 #define InitDisplayLock(d) (_XInitDisplayLock_fn ? (*_XInitDisplayLock_fn)(d) : Success)
90 #define FreeDisplayLock(d) if (_XFreeDisplayLock_fn) (*_XFreeDisplayLock_fn)(d)
91 #else
92 #define InitDisplayLock(dis) Success
93 #define FreeDisplayLock(dis)
94 #endif /* XTHREADS */
95
96 static xReq _dummy_request = {
97 0, 0, 0
98 };
99
100 static void OutOfMemory(Display *dpy, char *setup);
101 #if !USE_XCB
102 static Bool _XBigReqHandler(Display *dpy, xReply *rep, char *buf, int len,
103 XPointer data);
104 #endif /* !USE_XCB */
105
106 /*
107 * Connects to a server, creates a Display object and returns a pointer to
108 * the newly created Display back to the caller.
109 */
110 Display *
111 XOpenDisplay (
112 register _Xconst char *display)
113 {
114 register Display *dpy; /* New Display object being created. */
115 register int i;
116 int j, k; /* random iterator indexes */
117 char *display_name; /* pointer to display name */
118 #if !USE_XCB
119 int endian; /* to determine which endian. */
120 xConnClientPrefix client; /* client information */
121 int idisplay; /* display number */
122 int prefixread = 0; /* setup prefix already read? */
123 char *conn_auth_name, *conn_auth_data;
124 int conn_auth_namelen, conn_auth_datalen;
125 #endif /* !USE_XCB */
126 char *setup = NULL; /* memory allocated at startup */
127 char *fullname = NULL; /* expanded name of display */
128 int iscreen; /* screen number */
129 xConnSetupPrefix prefix; /* prefix information */
130 int vendorlen; /* length of vendor string */
131 union {
132 xConnSetup *setup;
133 char *failure;
134 char *vendor;
135 xPixmapFormat *sf;
136 xWindowRoot *rp;
137 xDepth *dp;
138 xVisualType *vp;
139 } u; /* proto data returned from server */
140 long setuplength; /* number of bytes in setup message */
141 long usedbytes = 0; /* number of bytes we have processed */
142 unsigned long mask;
143 long int conn_buf_size;
144 char *xlib_buffer_size;
145
146 #if !USE_XCB
147 bzero((char *) &client, sizeof(client));
148 bzero((char *) &prefix, sizeof(prefix));
149 #endif /* !USE_XCB */
150
151 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
152 fprintf(stderr, "\nXOpenDisplay: Called with display [%s].\n", display);
153 #endif
154 /*
155 * If the display specifier string supplied as an argument to this
156 * routine is NULL or a pointer to NULL, read the DISPLAY variable.
157 */
158 if (display == NULL || *display == '\0') {
159 if ((display_name = getenv("DISPLAY")) == NULL) {
160 /* Oops! No DISPLAY environment variable - error. */
161 return(NULL);
162 }
163 }
164 else {
165 /* Display is non-NULL, copy the pointer */
166 display_name = (char *)display;
167 }
168
169 /*
170 * Set the default error handlers. This allows the global variables to
171 * default to NULL for use with shared libraries.
172 */
173 if (_XErrorFunction == NULL) (void) XSetErrorHandler (NULL);
174 if (_XIOErrorFunction == NULL) (void) XSetIOErrorHandler (NULL);
175
176 /*
177 * Attempt to allocate a display structure. Return NULL if allocation fails.
178 */
179 if ((dpy = Xcalloc(1, sizeof(Display))) == NULL) {
180 return(NULL);
181 }
182
183 if ((dpy->display_name = strdup(display_name)) == NULL) {
184 OutOfMemory(dpy, setup);
185 return(NULL);
186 }
187
188 /*
189 * Call the Connect routine to get the transport connection object.
190 * If NULL is returned, the connection failed.
191 */
192
193 #if USE_XCB
194 if(!_XConnectXCB(dpy, display, &iscreen)) {
195 OutOfMemory(dpy, setup);
196 return NULL;
197 }
198 #else /* !USE_XCB */
199 if ((dpy->trans_conn = _X11TransConnectDisplay (
200 display_name, &fullname, &idisplay,
201 &iscreen, &conn_auth_name,
202 &conn_auth_namelen, &conn_auth_data,
203 &conn_auth_datalen)) == NULL) {
204 Xfree ((char *) dpy);
205 return(NULL);
206 }
207
208 dpy->fd = _X11TransGetConnectionNumber (dpy->trans_conn);
209 #endif /* USE_XCB */
210
211 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
212 fprintf(stderr, "\nXOpenDisplay: Connected display with dpy->fd = [%d].\n", dpy->fd);
213 #endif
214 /* Initialize as much of the display structure as we can.
215 * Initialize pointers to NULL so that XFreeDisplayStructure will
216 * work if we run out of memory before we finish initializing.
217 */
218 dpy->display_name = fullname;
219 dpy->keysyms = (KeySym *) NULL;
220 dpy->modifiermap = NULL;
221 dpy->lock_meaning = NoSymbol;
222 dpy->keysyms_per_keycode = 0;
223 dpy->xdefaults = (char *)NULL;
224 dpy->scratch_length = 0L;
225 dpy->scratch_buffer = NULL;
226 dpy->key_bindings = NULL;
227 dpy->ext_procs = (_XExtension *)NULL;
228 dpy->ext_data = (XExtData *)NULL;
229 dpy->ext_number = 0;
230 dpy->event_vec[X_Error] = _XUnknownWireEvent;
231 dpy->event_vec[X_Reply] = _XUnknownWireEvent;
232 dpy->wire_vec[X_Error] = _XUnknownNativeEvent;
233 dpy->wire_vec[X_Reply] = _XUnknownNativeEvent;
234 for (i = KeyPress; i < LASTEvent; i++) {
235 dpy->event_vec[i] = _XWireToEvent;
236 dpy->wire_vec[i] = NULL;
237 }
238 for (i = LASTEvent; i < 128; i++) {
239 dpy->event_vec[i] = _XUnknownWireEvent;
240 dpy->wire_vec[i] = _XUnknownNativeEvent;
241 }
242 dpy->resource_id = 0;
243 dpy->db = (struct _XrmHashBucketRec *)NULL;
244 dpy->cursor_font = None;
245 dpy->flags = 0;
246 dpy->async_handlers = NULL;
247 dpy->screens = NULL;
248 dpy->vendor = NULL;
249 dpy->buffer = NULL;
250 dpy->atoms = NULL;
251 dpy->error_vec = NULL;
252 dpy->context_db = NULL;
253 dpy->free_funcs = NULL;
254 dpy->pixmap_format = NULL;
255 dpy->cms.clientCmaps = NULL;
256 dpy->cms.defaultCCCs = NULL;
257 dpy->cms.perVisualIntensityMaps = NULL;
258 dpy->im_filters = NULL;
259 dpy->bigreq_size = 0;
260 dpy->lock = NULL;
261 dpy->lock_fns = NULL;
262 dpy->qfree = NULL;
263 dpy->next_event_serial_num = 1;
264 dpy->im_fd_info = NULL;
265 dpy->im_fd_length = 0;
266 dpy->conn_watchers = NULL;
267 dpy->watcher_count = 0;
268 dpy->filedes = NULL;
269 dpy->flushes = NULL;
270 dpy->xcmisc_opcode = 0;
271 dpy->xkb_info = NULL;
272 dpy->exit_handler_data = NULL;
273
274 /*
275 * Setup other information in this display structure.
276 */
277 dpy->vnumber = X_PROTOCOL;
278 dpy->resource_alloc = _XAllocID;
279 dpy->idlist_alloc = _XAllocIDs;
280 dpy->synchandler = NULL;
281 dpy->savedsynchandler = NULL;
282 X_DPY_SET_REQUEST(dpy, 0);
283 X_DPY_SET_LAST_REQUEST_READ(dpy, 0);
284 dpy->default_screen = iscreen; /* Value returned by ConnectDisplay */
285 dpy->last_req = (char *)&_dummy_request;
286 dpy->error_threads = NULL;
287 dpy->exit_handler = _XDefaultIOErrorExit;
288
289 /* Initialize the display lock */
290 if (InitDisplayLock(dpy) != 0) {
291 OutOfMemory (dpy, setup);
292 return(NULL);
293 }
294
295 if (!_XPollfdCacheInit(dpy)) {
296 OutOfMemory (dpy, setup);
297 return(NULL);
298 }
299
300 /* Set up the output buffers. */
301 #ifndef XLIBDEFAULTBUFSIZE
302 #define XLIBDEFAULTBUFSIZE 16384 /* 16k */
303 #endif
304 #ifndef XLIBMINBUFSIZE
305 #define XLIBMINBUFSIZE BUFSIZE /* old default buffer size */
306 #endif
307 xlib_buffer_size = getenv("XLIBBUFFERSIZE");
308
309 #ifdef __sun /* Backwards compatibility for old Solaris libX11 name */
310 if (xlib_buffer_size == NULL)
311 xlib_buffer_size = getenv("XSUNBUFFERSIZE");
312 #endif
313
314 if (xlib_buffer_size == NULL)
315 conn_buf_size = XLIBDEFAULTBUFSIZE;
316 else
317 conn_buf_size = 1024 * strtol(xlib_buffer_size, NULL, 10);
318 if (conn_buf_size < XLIBMINBUFSIZE)
319 conn_buf_size = XLIBMINBUFSIZE;
320 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
321 fprintf (stderr, "Xlib: Running with XLIBBUFFERSIZE [%d] XLIBMINBUFSIZE [%d] "
322 "buffer size [%ld].\n", XLIBDEFAULTBUFSIZE, XLIBMINBUFSIZE, conn_buf_size);
323 #endif
324
325 if ((dpy->bufptr = dpy->buffer = Xcalloc(1, conn_buf_size)) == NULL) {
326 OutOfMemory (dpy, setup);
327 return(NULL);
328 }
329 dpy->bufmax = dpy->buffer + conn_buf_size;
330 #if USE_XCB
331 dpy->xcb->real_bufmax = dpy->bufmax;
332 dpy->bufmax = dpy->buffer;
333 #endif
334
335 /* Set up the input event queue and input event queue parameters. */
336 dpy->head = dpy->tail = NULL;
337 dpy->qlen = 0;
338
339 /* Set up free-function record */
340 if ((dpy->free_funcs = Xcalloc(1, sizeof(_XFreeFuncRec))) == NULL) {
341 OutOfMemory (dpy, setup);
342 return(NULL);
343 }
344
345 #if !USE_XCB
346 /*
347 * The xConnClientPrefix describes the initial connection setup information
348 * and is followed by the authorization information. Sites that are interested
349 * in security are strongly encouraged to use an authentication and
350 * authorization system such as Kerberos.
351 */
352 endian = 1;
353 if (*(char *) &endian)
354 client.byteOrder = '\154'; /* 'l' */
355 else
356 client.byteOrder = '\102'; /* 'B' */
357 client.majorVersion = X_PROTOCOL;
358 client.minorVersion = X_PROTOCOL_REVISION;
359 client.nbytesAuthProto = conn_auth_namelen;
360 client.nbytesAuthString = conn_auth_datalen;
361 prefixread = _XSendClientPrefix(dpy, &client,
362 conn_auth_name, conn_auth_data,
363 &prefix);
364 if (prefixread < 0)
365 {
366 _XDisconnectDisplay (dpy->trans_conn);
367 Xfree ((char *)dpy);
368 return(NULL);
369 }
370 if (conn_auth_name) Xfree(conn_auth_name);
371 if (conn_auth_data) Xfree(conn_auth_data);
372 /*
373 * Now see if connection was accepted...
374 */
375 /* these internal functions expect the display to be locked */
376 LockDisplay(dpy);
377
378 if (prefixread == 0)
379 _XRead (dpy, (char *)&prefix,(long)SIZEOF(xConnSetupPrefix));
380
381 /* an Authenticate reply we weren't expecting? */
382 if (prefix.success != xTrue && prefix.success != xFalse) {
383 fprintf (stderr,
384 "Xlib: unexpected connection setup reply from server, type %d.\r\n",
385 prefix.success);
386 _XDisconnectDisplay (dpy->trans_conn);
387 Xfree ((char *)dpy);
388 return(NULL);
389 }
390
391 if (prefix.majorVersion != X_PROTOCOL) {
392 /* XXX - printing messages marks a bad programming interface */
393 #ifdef NX_TRANS_SOCKET
394 if (_X11TransSocketProxyConnInfo(dpy->trans_conn) == NULL) {
395 fprintf (stderr, "Xlib: client uses different protocol version (%d) "
396 "than server (%d)!\r\n", X_PROTOCOL, prefix.majorVersion);
397 }
398 #else
399 fprintf (stderr,
400 "Xlib: client uses different protocol version (%d) than server (%d)!\r\n",
401 X_PROTOCOL, prefix.majorVersion);
402 #endif
403 _XDisconnectDisplay (dpy->trans_conn);
404 Xfree ((char *)dpy);
405 return(NULL);
406 }
407 #endif /* !USE_XCB */
408
409 #if USE_XCB
410 {
411 const struct xcb_setup_t *xcbsetup = xcb_get_setup(dpy->xcb->connection);
412 memcpy(&prefix, xcbsetup, sizeof(prefix));
413 setuplength = prefix.length << 2;
414 setup = (char *) xcbsetup;
415 setup += SIZEOF(xConnSetupPrefix);
416 u.setup = (xConnSetup *) setup;
417 }
418 #else /* !USE_XCB */
419 setuplength = prefix.length << 2;
420 if ( (u.setup = (xConnSetup *)
421 (setup = Xmalloc ((unsigned) setuplength))) == NULL) {
422 _XDisconnectDisplay (dpy->trans_conn);
423 Xfree ((char *)dpy);
424 return(NULL);
425 }
426 _XRead (dpy, (char *)u.setup, setuplength);
427
428 /*
429 * If the connection was not accepted by the server due to problems,
430 * give error message to the user....
431 */
432 if (prefix.success != xTrue) {
433 /* XXX - printing messages marks a bad programming interface */
434 fprintf (stderr,
435 "Xlib: connection to \"%s\" refused by server\r\nXlib: ",
436 fullname);
437
438 if (prefix.lengthReason > setuplength) {
439 fprintf (stderr, "Xlib: Broken initial reply: length of reason > length of packet\r\n");
440 }else{
441 (void) fwrite (u.failure, (size_t)sizeof(char),
442 (size_t)prefix.lengthReason, stderr);
443 (void) fwrite ("\r\n", sizeof(char), 2, stderr);
444 }
445
446 OutOfMemory(dpy, setup);
447 return (NULL);
448 }
449 #endif /* USE_XCB */
450
451 /*
452 * Check if the reply was long enough to get any information out of it.
453 */
454 usedbytes = sz_xConnSetup;
455 if (setuplength < usedbytes ) {
456 fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
457 OutOfMemory(dpy, setup);
458 return (NULL);
459 }
460
461 /*
462 * We succeeded at authorization, so let us move the data into
463 * the display structure.
464 */
465 dpy->proto_major_version= prefix.majorVersion;
466 dpy->proto_minor_version= prefix.minorVersion;
467 dpy->release = u.setup->release;
468 dpy->resource_base = u.setup->ridBase;
469 dpy->resource_mask = u.setup->ridMask;
470 dpy->min_keycode = u.setup->minKeyCode;
471 dpy->max_keycode = u.setup->maxKeyCode;
472 dpy->motion_buffer = u.setup->motionBufferSize;
473 dpy->nformats = u.setup->numFormats;
474 dpy->nscreens = u.setup->numRoots;
475 dpy->byte_order = u.setup->imageByteOrder;
476 dpy->bitmap_unit = u.setup->bitmapScanlineUnit;
477 dpy->bitmap_pad = u.setup->bitmapScanlinePad;
478 dpy->bitmap_bit_order = u.setup->bitmapBitOrder;
479 dpy->max_request_size = u.setup->maxRequestSize;
480 mask = dpy->resource_mask;
481 dpy->resource_shift = 0;
482 if (!mask)
483 {
484 fprintf (stderr, "Xlib: connection to \"%s\" invalid setup\n",
485 fullname);
486 OutOfMemory(dpy, setup);
487 return (NULL);
488 }
489
490 while (!(mask & 1)) {
491 dpy->resource_shift++;
492 mask = mask >> 1;
493 }
494 dpy->resource_max = (dpy->resource_mask >> dpy->resource_shift) - 5;
495 /*
496 * now extract the vendor string... String must be null terminated,
497 * padded to multiple of 4 bytes.
498 */
499 /* Check for a sane vendor string length */
500 if (u.setup->nbytesVendor > 256) {
501 OutOfMemory(dpy, setup);
502 return (NULL);
503 }
504
505 dpy->vendor = Xmalloc(u.setup->nbytesVendor + 1);
506 if (dpy->vendor == NULL) {
507 OutOfMemory(dpy, setup);
508 return (NULL);
509 }
510 vendorlen = u.setup->nbytesVendor;
511
512 /*
513 * validate setup length
514 */
515 usedbytes += (vendorlen + 3) & ~3;
516 if (setuplength < usedbytes) {
517 fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
518 OutOfMemory(dpy, setup);
519 return (NULL);
520 }
521
522 u.setup = (xConnSetup *) (((char *) u.setup) + sz_xConnSetup);
523 (void) strncpy(dpy->vendor, u.vendor, (size_t) vendorlen);
524 dpy->vendor[vendorlen] = '\0';
525 vendorlen = (vendorlen + 3) & ~3; /* round up */
526 u.vendor += vendorlen;
527
528 /*
529 * Now iterate down setup information.....
530 */
531 dpy->pixmap_format = Xcalloc(dpy->nformats, sizeof(ScreenFormat));
532 if (dpy->pixmap_format == NULL) {
533 OutOfMemory (dpy, setup);
534 return(NULL);
535 }
536 /*
537 * First decode the Z axis Screen format information.
538 */
539 usedbytes += dpy->nformats * sz_xPixmapFormat;
540
541 if (setuplength < usedbytes) {
542 fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
543 OutOfMemory (dpy, setup);
544 return(NULL);
545 }
546
547 for (i = 0; i < dpy->nformats; i++) {
548 register ScreenFormat *fmt = &dpy->pixmap_format[i];
549 fmt->depth = u.sf->depth;
550 fmt->bits_per_pixel = u.sf->bitsPerPixel;
551 fmt->scanline_pad = u.sf->scanLinePad;
552 fmt->ext_data = NULL;
553 u.sf = (xPixmapFormat *) (((char *) u.sf) + sz_xPixmapFormat);
554 }
555
556 /*
557 * next the Screen structures.
558 */
559 dpy->screens = Xcalloc(dpy->nscreens, sizeof(Screen));
560 if (dpy->screens == NULL) {
561 OutOfMemory (dpy, setup);
562 return(NULL);
563 }
564
565 /*
566 * Now go deal with each screen structure.
567 */
568 for (i = 0; i < dpy->nscreens; i++) {
569 register Screen *sp = &dpy->screens[i];
570 VisualID root_visualID;
571
572 usedbytes += sz_xWindowRoot;
573 if (setuplength < usedbytes) {
574 fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
575 OutOfMemory (dpy, setup);
576 return(NULL);
577 }
578
579 root_visualID = u.rp->rootVisualID;
580 sp->display = dpy;
581 sp->root = u.rp->windowId;
582 sp->cmap = u.rp->defaultColormap;
583 sp->white_pixel = u.rp->whitePixel;
584 sp->black_pixel = u.rp->blackPixel;
585 sp->root_input_mask = u.rp->currentInputMask;
586 sp->width = u.rp->pixWidth;
587 sp->height = u.rp->pixHeight;
588 sp->mwidth = u.rp->mmWidth;
589 sp->mheight = u.rp->mmHeight;
590 sp->min_maps = u.rp->minInstalledMaps;
591 sp->max_maps = u.rp->maxInstalledMaps;
592 sp->backing_store= u.rp->backingStore;
593 sp->save_unders = u.rp->saveUnders;
594 sp->root_depth = u.rp->rootDepth;
595 sp->ndepths = u.rp->nDepths;
596 sp->ext_data = NULL;
597 u.rp = (xWindowRoot *) (((char *) u.rp) + sz_xWindowRoot);
598 /*
599 * lets set up the depth structures.
600 */
601 sp->depths = Xcalloc(sp->ndepths, sizeof(Depth));
602 if (sp->depths == NULL) {
603 OutOfMemory (dpy, setup);
604 return(NULL);
605 }
606 /*
607 * for all depths on this screen.
608 */
609 for (j = 0; j < sp->ndepths; j++) {
610 Depth *dp = &sp->depths[j];
611
612 usedbytes += sz_xDepth;
613 if (setuplength < usedbytes) {
614 fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
615 OutOfMemory (dpy, setup);
616 return(NULL);
617 }
618
619 dp->depth = u.dp->depth;
620 dp->nvisuals = u.dp->nVisuals;
621 u.dp = (xDepth *) (((char *) u.dp) + sz_xDepth);
622 if (dp->nvisuals > 0) {
623 dp->visuals = Xcalloc(dp->nvisuals, sizeof(Visual));
624 if (dp->visuals == NULL) {
625 OutOfMemory (dpy, setup);
626 return(NULL);
627 }
628 for (k = 0; k < dp->nvisuals; k++) {
629 register Visual *vp = &dp->visuals[k];
630
631 usedbytes += sz_xVisualType;
632 if (setuplength < usedbytes) {
633 fprintf (stderr, "Xlib: Broken initial reply: Too short (%ld)\n", setuplength);
634 OutOfMemory (dpy, setup);
635 return(NULL);
636 }
637
638 vp->visualid = u.vp->visualID;
639 vp->class = u.vp->class;
640 vp->bits_per_rgb= u.vp->bitsPerRGB;
641 vp->map_entries = u.vp->colormapEntries;
642 vp->red_mask = u.vp->redMask;
643 vp->green_mask = u.vp->greenMask;
644 vp->blue_mask = u.vp->blueMask;
645 vp->ext_data = NULL;
646 u.vp = (xVisualType *) (((char *) u.vp) +
647 sz_xVisualType);
648 }
649 if (dp->depth == 32 && getenv ("XLIB_SKIP_ARGB_VISUALS"))
650 {
651 Xfree (dp->visuals);
652 dp->visuals = NULL;
653 dp->nvisuals = 0;
654 }
655 } else {
656 dp->visuals = (Visual *) NULL;
657 }
658 }
659 sp->root_visual = _XVIDtoVisual(dpy, root_visualID);
660 }
661
662 if(usedbytes != setuplength){
663 /* Sanity check, shouldn't happen. */
664 fprintf(stderr, "Xlib: Did not parse entire setup message: "
665 "parsed: %ld, message: %ld\n",
666 usedbytes, setuplength);
667 OutOfMemory(dpy, setup);
668 return(NULL);
669 }
670
671 /*
672 * Now start talking to the server to setup all other information...
673 */
674
675 #if !USE_XCB
676 Xfree (setup); /* all finished with setup information */
677 #endif /* !USE_XCB */
678
679 /*
680 * Make sure default screen is legal.
681 */
682 if (iscreen >= dpy->nscreens) {
683 OutOfMemory(dpy, (char *) NULL);
684 return(NULL);
685 }
686
687 #if !USE_XCB
688 /*
689 * finished calling internal routines, now unlock for external routines
690 */
691 UnlockDisplay(dpy);
692 #endif /* !USE_XCB */
693
694 #if USE_XCB
695 /*
696 * get availability of large requests
697 */
698 dpy->bigreq_size = xcb_get_maximum_request_length(dpy->xcb->connection);
699 if(dpy->bigreq_size <= dpy->max_request_size)
700 dpy->bigreq_size = 0;
701 #endif /* USE_XCB */
702
703 /*
704 * Set up other stuff clients are always going to use.
705 */
706 for (i = 0; i < dpy->nscreens; i++) {
707 register Screen *sp = &dpy->screens[i];
708 XGCValues values;
709 values.foreground = sp->black_pixel;
710 values.background = sp->white_pixel;
711 if ((sp->default_gc = XCreateGC (dpy, sp->root,
712 GCForeground|GCBackground,
713 &values)) == NULL) {
714 OutOfMemory(dpy, (char *) NULL);
715 return (NULL);
716 }
717 }
718 /*
719 * call into synchronization routine so that all programs can be
720 * forced synchronous
721 */
722 (void) XSynchronize(dpy, _Xdebug);
723
724 /*
725 * get the resource manager database off the root window.
726 */
727 LockDisplay(dpy);
728 {
729 xGetPropertyReply reply;
730 xGetPropertyReq *req;
731 #if !USE_XCB
732 _XAsyncHandler async;
733 _XBigReqState async_state;
734 xQueryExtensionReq *qreq;
735 xBigReqEnableReq *breq;
736 xBigReqEnableReply brep;
737
738 GetReq(QueryExtension, qreq);
739 async_state.seq = dpy->request;
740 async_state.opcode = 0;
741 async.next = dpy->async_handlers;
742 async.handler = _XBigReqHandler;
743 async.data = (XPointer)&async_state;
744 dpy->async_handlers = &async;
745 qreq->nbytes = bignamelen;
746 qreq->length += (bignamelen+3)>>2;
747 Data(dpy, XBigReqExtensionName, bignamelen);
748 #endif /* !USE_XCB */
749
750 GetReq (GetProperty, req);
751 req->window = RootWindow(dpy, 0);
752 req->property = XA_RESOURCE_MANAGER;
753 req->type = XA_STRING;
754 req->delete = False;
755 req->longOffset = 0;
756 req->longLength = 100000000L;
757
758 if (_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
759 if (reply.format == 8 && reply.propertyType == XA_STRING &&
760 (reply.nItems + 1 > 0) &&
761 (reply.nItems <= req->longLength * 4) &&
762 (dpy->xdefaults = Xmalloc (reply.nItems + 1))) {
763 _XReadPad (dpy, dpy->xdefaults, reply.nItems);
764 dpy->xdefaults[reply.nItems] = '\0';
765 }
766 else if (reply.propertyType != None)
767 _XEatDataWords(dpy, reply.length);
768 }
769 #if !USE_XCB
770 DeqAsyncHandler(dpy, &async);
771 if (async_state.opcode) {
772 GetReq(BigReqEnable, breq);
773 breq->reqType = async_state.opcode;
774 breq->brReqType = X_BigReqEnable;
775 if (_XReply(dpy, (xReply *)&brep, 0, xFalse))
776 dpy->bigreq_size = brep.max_request_size;
777 }
778 #endif /* !USE_XCB */
779 }
780 UnlockDisplay(dpy);
781
782 #ifdef MOTIFBC
783 {
784 extern Display *_XHeadOfDisplayList;
785 _XHeadOfDisplayList = dpy;
786 }
787 #endif
788 #ifdef XKB
789 XkbUseExtension(dpy,NULL,NULL);
790 #endif
791 /*
792 * and return successfully
793 */
794 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
795 fprintf(stderr, "XOpenDisplay: Returning display at [%p].\n", dpy);
796 #endif
797 return(dpy);
798 }
799
800 #if !USE_XCB
801 static Bool
802 _XBigReqHandler(
803 register Display *dpy,
804 register xReply *rep,
805 char *buf,
806 int len,
807 XPointer data)
808 {
809 _XBigReqState *state;
810 xQueryExtensionReply replbuf;
811 xQueryExtensionReply *repl;
812
813 state = (_XBigReqState *)data;
814 if (dpy->last_request_read != state->seq)
815 return False;
816 if (rep->generic.type == X_Error)
817 return True;
818 repl = (xQueryExtensionReply *)
819 _XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
820 (SIZEOF(xQueryExtensionReply) - SIZEOF(xReply)) >> 2,
821 True);
822 if (repl->present)
823 state->opcode = repl->major_opcode;
824 return True;
825 }
826 #endif /* !USE_XCB */
827
828
829 /* XFreeDisplayStructure frees all the storage associated with a
830 * Display. It is used by XOpenDisplay if it runs out of memory,
831 * and also by XCloseDisplay. It needs to check whether all pointers
832 * are non-NULL before dereferencing them, since it may be called
833 * by XOpenDisplay before the Display structure is fully formed.
834 * XOpenDisplay must be sure to initialize all the pointers to NULL
835 * before the first possible call on this.
836 */
837
838 void _XFreeDisplayStructure(Display *dpy)
839 {
840 /* move all cookies in the EQ to the jar, then free them. */
841 if (dpy->qfree) {
842 _XQEvent *qelt = dpy->qfree;
843 while (qelt) {
844 if (_XIsEventCookie(dpy, &qelt->event))
845 _XStoreEventCookie(dpy, &qelt->event);
846 qelt = qelt->next;
847 }
848 }
849 if (dpy->cookiejar)
850 _XFreeEventCookies(dpy);
851 while (dpy->ext_procs) {
852 _XExtension *ext = dpy->ext_procs;
853 dpy->ext_procs = ext->next;
854 Xfree (ext->name);
855 Xfree (ext);
856 }
857 if (dpy->im_filters)
858 (*dpy->free_funcs->im_filters)(dpy);
859 if (dpy->cms.clientCmaps)
860 (*dpy->free_funcs->clientCmaps)(dpy);
861 if (dpy->cms.defaultCCCs)
862 (*dpy->free_funcs->defaultCCCs)(dpy);
863 if (dpy->cms.perVisualIntensityMaps)
864 (*dpy->free_funcs->intensityMaps)(dpy);
865 if (dpy->atoms)
866 (*dpy->free_funcs->atoms)(dpy);
867 if (dpy->modifiermap)
868 (*dpy->free_funcs->modifiermap)(dpy->modifiermap);
869 if (dpy->key_bindings)
870 (*dpy->free_funcs->key_bindings)(dpy);
871 if (dpy->context_db)
872 (*dpy->free_funcs->context_db)(dpy);
873 if (dpy->xkb_info)
874 (*dpy->free_funcs->xkb)(dpy);
875
876 /* if RM database was allocated by XGetDefault() free it */
877 if (dpy->db && (dpy->flags & XlibDisplayDfltRMDB))
878 XrmDestroyDatabase(dpy->db);
879
880 if (dpy->screens) {
881 register int i;
882
883 for (i = 0; i < dpy->nscreens; i++) {
884 Screen *sp = &dpy->screens[i];
885
886 if (sp->depths) {
887 register int j;
888
889 for (j = 0; j < sp->ndepths; j++) {
890 Depth *dp = &sp->depths[j];
891
892 if (dp->visuals) {
893 register int k;
894
895 for (k = 0; k < dp->nvisuals; k++)
896 _XFreeExtData (dp->visuals[k].ext_data);
897 Xfree (dp->visuals);
898 }
899 }
900
901 Xfree (sp->depths);
902 }
903
904 _XFreeExtData (sp->ext_data);
905 }
906
907 Xfree (dpy->screens);
908 }
909
910 if (dpy->pixmap_format) {
911 register int i;
912
913 for (i = 0; i < dpy->nformats; i++)
914 _XFreeExtData (dpy->pixmap_format[i].ext_data);
915 Xfree (dpy->pixmap_format);
916 }
917
918 free(dpy->display_name);
919
920 Xfree (dpy->vendor);
921 Xfree (dpy->buffer);
922 Xfree (dpy->keysyms);
923 Xfree (dpy->xdefaults);
924 Xfree (dpy->error_vec);
925
926 _XFreeExtData (dpy->ext_data);
927
928 Xfree (dpy->free_funcs);
929 Xfree (dpy->scratch_buffer);
930 FreeDisplayLock(dpy);
931
932 if (dpy->qfree) {
933 register _XQEvent *qelt = dpy->qfree;
934
935 while (qelt) {
936 register _XQEvent *qnxt = qelt->next;
937 Xfree (qelt);
938 qelt = qnxt;
939 }
940 }
941 while (dpy->im_fd_info) {
942 struct _XConnectionInfo *conni = dpy->im_fd_info;
943 dpy->im_fd_info = conni->next;
944 Xfree (conni->watch_data);
945 Xfree (conni);
946 }
947 if (dpy->conn_watchers) {
948 struct _XConnWatchInfo *watcher = dpy->conn_watchers;
949 dpy->conn_watchers = watcher->next;
950 Xfree (watcher);
951 }
952
953 Xfree (dpy->filedes);
954
955 #if USE_XCB
956 _XFreeX11XCBStructure(dpy);
957 #endif /* USE_XCB */
958
959 Xfree (dpy);
960 }
961
962 /* OutOfMemory is called if malloc fails. XOpenDisplay returns NULL
963 after this returns. */
964
965 static void OutOfMemory(Display *dpy, char *setup)
966 {
967 #if USE_XCB
968 if(dpy->xcb->connection)
969 xcb_disconnect(dpy->xcb->connection);
970 #else /* !USE_XCB */
971 _XDisconnectDisplay (dpy->trans_conn);
972 #endif /* USE_XCB */
973 _XFreeDisplayStructure (dpy);
974 #if !USE_XCB
975 if (setup) Xfree (setup);
976 #endif /* !USE_XCB */
977 }