home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_OpenDisplay.c < prev    next >
C/C++ Source or Header  |  2000-10-11  |  10KB  |  411 lines

  1.  
  2. #define INCL_DOSPROCESS
  3. #include "Xlib_private.h"
  4. #include <netdb.h>
  5.  
  6. extern PID mypid;
  7. HMODULE module;
  8. unsigned int _XmaxKeyCode, _XminKeyCode;
  9. KeySym *_XkeyMap;
  10.  
  11. extern Bool _XWireToEvent();
  12. extern Status _XUnknownNativeEvent();
  13. extern Bool _XUnknownWireEvent();
  14. extern Bool OsInitColors();
  15.  
  16. void Xlib_InitAtoms(Display*);
  17. void Xlib_buildmark(char*, int);
  18. Bool XkbUseExtension(Display*,int*,int*);
  19. void _XInitKeyMapping(void);
  20. void Xlib_InitOS2I18N(void);
  21.  
  22. void pthread_setfuncs(void *mutex, void *event);
  23.  
  24.  
  25. #ifndef BUFSIZE
  26. #define BUFSIZE 2048
  27. #endif
  28.  
  29. static ScreenFormat ScreenFmtTmpl[] = {
  30.     {NULL, 24, 32, 4},
  31.     {NULL, 16, 16, 2},
  32.     {NULL, 8,  8, 1},
  33.     {NULL, 1,  1, 1}
  34. };
  35.  
  36. /* XFreeDisplayStructure frees all the storage associated with a 
  37.  * Display.  It is used by XOpenDisplay if it runs out of memory,
  38.  * and also by XCloseDisplay.   It needs to check whether all pointers
  39.  * are non-NULL before dereferencing them, since it may be called
  40.  * by XOpenDisplay before the Display structure is fully formed.
  41.  * XOpenDisplay must be sure to initialize all the pointers to NULL
  42.  * before the first possible call on this.
  43.  */
  44.  
  45. void _XFreeDisplayStructure(dpy)
  46.     register Display *dpy;
  47. {
  48.     DBUG_ENTER("_XFreeDisplayStructure")
  49.     while (dpy->ext_procs) {
  50.         _XExtension *ext = dpy->ext_procs;
  51.         dpy->ext_procs = ext->next;
  52.         if (ext->name)
  53.         Xfree (ext->name);
  54.         Xfree ((char *)ext);
  55.     }
  56.     if (dpy->im_filters)
  57.        (*dpy->free_funcs->im_filters)(dpy);
  58.     if (dpy->cms.clientCmaps)
  59.        (*dpy->free_funcs->clientCmaps)(dpy);
  60.     if (dpy->cms.defaultCCCs)
  61.        (*dpy->free_funcs->defaultCCCs)(dpy);
  62.     if (dpy->cms.perVisualIntensityMaps)
  63.        (*dpy->free_funcs->intensityMaps)(dpy);
  64.     if (dpy->atoms)
  65.         (*dpy->free_funcs->atoms)(dpy);
  66.     if (dpy->modifiermap)
  67.        (*dpy->free_funcs->modifiermap)(dpy->modifiermap);
  68.     if (dpy->key_bindings)
  69.        (*dpy->free_funcs->key_bindings)(dpy);
  70.     if (dpy->context_db)
  71.        (*dpy->free_funcs->context_db)(dpy);
  72.     if (dpy->xkb_info)
  73.        (*dpy->free_funcs->xkb)(dpy);
  74.  
  75.     if (dpy->screens) {
  76.         register int i;
  77.  
  78.             for (i = 0; i < dpy->nscreens; i++) {
  79.         Screen *sp = &dpy->screens[i];
  80.  
  81.         if (sp->depths) {
  82.            register int j;
  83.  
  84.            for (j = 0; j < sp->ndepths; j++) {
  85.             Depth *dp = &sp->depths[j];
  86.  
  87.             if (dp->visuals) {
  88.                register int k;
  89.  
  90.                for (k = 0; k < dp->nvisuals; k++)
  91.                  _XFreeExtData (dp->visuals[k].ext_data);
  92.                Xfree ((char *) dp->visuals);
  93.                }
  94.             }
  95.  
  96.            Xfree ((char *) sp->depths);
  97.            }
  98.  
  99.         _XFreeExtData (sp->ext_data);
  100.         }
  101.  
  102.         Xfree ((char *)dpy->screens);
  103.         }
  104.     
  105.     if (dpy->pixmap_format) {
  106.         register int i;
  107.  
  108.         for (i = 0; i < dpy->nformats; i++)
  109.           _XFreeExtData (dpy->pixmap_format[i].ext_data);
  110.             Xfree ((char *)dpy->pixmap_format);
  111.         }
  112.  
  113.     if (dpy->display_name)
  114.        Xfree (dpy->display_name);
  115.     if (dpy->vendor)
  116.        Xfree (dpy->vendor);
  117.  
  118.         if (dpy->private12)
  119.        Xfree (dpy->private12);
  120.     if (dpy->keysyms && dpy->keysyms != _XkeyMap)
  121.        Xfree ((char *) dpy->keysyms);
  122.     if (dpy->xdefaults)
  123.        Xfree (dpy->xdefaults);
  124.     if (dpy->error_vec)
  125.         Xfree ((char *)dpy->error_vec);
  126.  
  127.     _XFreeExtData (dpy->ext_data);
  128.     if (dpy->free_funcs)
  129.         Xfree ((char *)dpy->free_funcs);
  130.      if (dpy->scratch_buffer)
  131.          Xfree (dpy->scratch_buffer);
  132.     /*FreeDisplayLock(dpy);*/
  133.  
  134.     /*if (dpy->qfree) {
  135.         register _XQEvent *qelt = dpy->qfree;
  136.  
  137.         while (qelt) {
  138.         register _XQEvent *qnxt = qelt->next;
  139.         Xfree ((char *) qelt);
  140.         qelt = qnxt;
  141.         }
  142.     }*/
  143.  
  144.     while (dpy->im_fd_info) {
  145.         struct _XConnectionInfo *conni = dpy->im_fd_info;
  146.         dpy->im_fd_info = conni->next;
  147.         if (conni->watch_data)
  148.         Xfree (conni->watch_data);
  149.         Xfree (conni);
  150.     }
  151.     if (dpy->conn_watchers) {
  152.         struct _XConnWatchInfo *watcher = dpy->conn_watchers;
  153.         dpy->conn_watchers = watcher->next;
  154.         Xfree (watcher);
  155.     }
  156.     if (dpy->filedes)
  157.         Xfree (dpy->filedes);
  158.  
  159.     Xfree ((char *)dpy);
  160.     DBUG_VOID_RETURN;
  161. }
  162.  
  163. Display *new_display(_Xconst char *displayname)
  164. {
  165.     DBUG_ENTER("new_display")
  166.     Display *tmpdisplay = malloc(sizeof(Display));
  167.     char hostname[256];
  168.     int i;
  169.     extern XID _XAllocID();
  170.     extern void _XAllocIDs();
  171.  
  172.     memset(tmpdisplay, 0, sizeof(Display));
  173.  
  174.     tmpdisplay->fd = pmout[0];
  175.  
  176.     if (!displayname) {
  177.         gethostname(hostname,255);
  178.         strcat(hostname,":0");
  179.     }
  180.  
  181.     tmpdisplay->proto_major_version = X_PROTOCOL;
  182.     tmpdisplay->proto_minor_version = X_PROTOCOL_REVISION;
  183.     tmpdisplay->vendor = strdup("Everblue");
  184.     tmpdisplay->release = 0;
  185.  
  186.     tmpdisplay->ext_data = NULL;
  187.     
  188.     tmpdisplay->display_name = strdup(displayname?displayname:hostname);
  189.     tmpdisplay->qlen = 0;
  190.  
  191.     tmpdisplay->nformats = sizeof(ScreenFmtTmpl) / sizeof(ScreenFormat);
  192.     tmpdisplay->pixmap_format = (ScreenFormat *)Xmalloc(sizeof(ScreenFmtTmpl));
  193.     memcpy(tmpdisplay->pixmap_format, ScreenFmtTmpl, sizeof(ScreenFmtTmpl));        
  194.  
  195.     tmpdisplay->event_vec[0] = _XUnknownWireEvent;
  196.     tmpdisplay->event_vec[1] = _XUnknownWireEvent;
  197.     tmpdisplay->wire_vec[0]  = _XUnknownNativeEvent;
  198.     tmpdisplay->wire_vec[1]  = _XUnknownNativeEvent;
  199.     for (i = KeyPress; i < LASTEvent; i++) {
  200.         tmpdisplay->event_vec[i]     = _XWireToEvent;
  201.         tmpdisplay->wire_vec[i]     = NULL;
  202.     }
  203.     for (i = LASTEvent; i < 128; i++) {
  204.         tmpdisplay->event_vec[i]     = _XUnknownWireEvent;
  205.         tmpdisplay->wire_vec[i]     = _XUnknownNativeEvent;
  206.     }
  207.     tmpdisplay->next_event_serial_num = 1;
  208.  
  209.     tmpdisplay->private8 = X_PROTOCOL;
  210.     tmpdisplay->resource_alloc = _XAllocID;
  211.     tmpdisplay->idlist_alloc = _XAllocIDs;
  212.  
  213.     _XInitKeyMapping();
  214.     tmpdisplay->min_keycode = _XminKeyCode;
  215.     tmpdisplay->max_keycode = _XmaxKeyCode;
  216.     tmpdisplay->keysyms = _XkeyMap;
  217.     tmpdisplay->keysyms_per_keycode = 4;
  218.     tmpdisplay->mode_switch = Mod1Mask;
  219.  
  220.     tmpdisplay->nscreens = 1;
  221.     tmpdisplay->default_screen = 0;
  222.     tmpdisplay->screens = new_screen(tmpdisplay);
  223.  
  224.     tmpdisplay->bitmap_bit_order = LSBFirst;
  225.     tmpdisplay->byte_order = LSBFirst;
  226.  
  227.     tmpdisplay->bigreq_size = 1L<<24;
  228.     tmpdisplay->max_request_size = BUFSIZE;
  229.     tmpdisplay->private13 = tmpdisplay->private12 = Xcalloc(1, BUFSIZE);
  230.     tmpdisplay->private14 = tmpdisplay->private12 + BUFSIZE;
  231.  
  232.     tmpdisplay->free_funcs = (_XFreeFuncRec *)Xcalloc(1, sizeof(_XFreeFuncRec));
  233.  
  234.     DBUG_RETURN(tmpdisplay);
  235. }
  236.     
  237. int oldapptype;
  238.  
  239. int XCloseDisplay(Display* dpy)
  240. {
  241.     DBUG_ENTER("XCloseDisplay")
  242.     register _XExtension *ext;
  243.     register int i;
  244.  
  245.     if (!(dpy->flags & XlibDisplayClosing))
  246.     {
  247.         dpy->flags |= XlibDisplayClosing;
  248.         for (i = 0; i < dpy->nscreens; i++) {
  249.             register Screen *sp = &dpy->screens[i];
  250.             XFreeGC (dpy, sp->default_gc);
  251.         }
  252.         if (dpy->cursor_font != None) {
  253.             XUnloadFont (dpy, dpy->cursor_font);
  254.         }
  255.         XSync(dpy, 1);  /* throw away pending events, catch errors */
  256.         /* call out to any extensions interested */
  257.         for (ext = dpy->ext_procs; ext; ext = ext->next) {
  258.             if (ext->close_display)
  259.             (*ext->close_display)(dpy, &ext->codes);
  260.         }
  261.         /* if the closes generated more protocol, sync them up */
  262.         if (dpy->request != dpy->last_request_read)
  263.             XSync(dpy, 1);
  264.     }
  265.  
  266.     if (xinitialized && dpy == maindisplay) {
  267.         APIRET rc;
  268.         PPIB pib = NULL;
  269.  
  270.         WinPostMsg(mainhwnd, WM_QUIT, 0, 0);
  271.  
  272.         while (xinitialized) _sleep2(10);
  273.  
  274.         /* now to reverse what XOpenDisplay does... */
  275.  
  276.         close(pmout[1]); close(pmout[0]);
  277.  
  278.         WinDestroyMsgQueue(mainhmq);
  279.         WinTerminate(mainhab);
  280.  
  281.         (ULONG) mainthread = (ULONG) maindisplay = 
  282.         (ULONG) mainhmq = (ULONG) mainhab = (ULONG) mainhwnd = 0L;
  283.  
  284.         pthread_mutex_destroy(&evmutex);
  285.         _XFreeMutex(_Xglobal_lock);
  286.  
  287.         if (oldapptype != 3) {
  288.             pthread_setfuncs((void *)NULL, (void *)NULL);
  289.  
  290.             rc = DosGetInfoBlocks(NULL,&pib);
  291.             pib->pib_ultype = oldapptype;
  292.         }
  293.  
  294.         /*x11_console_notice("X closed.");*/
  295.     
  296.         /*free_display(display);*/
  297.     
  298.     }
  299.     _XFreeDisplayStructure(dpy);
  300.     DBUG_RETURN(0);
  301. }
  302.  
  303.  
  304. Display *XOpenDisplay(_Xconst char *display)
  305. {
  306.     DBUG_ENTER("XOpenDisplay")
  307.     int res;
  308.     char *display_name;
  309.     Display *result = NULL;
  310.  
  311.     if (display == NULL || *display == '\0') {
  312.         display_name = strdup(getenv("DISPLAY"));
  313.         /* Absolute fallback force local since we really
  314.          * don't have a display anyway. ;)
  315.          */
  316.         if(display == NULL || *display == '\0')
  317.             display_name = "localhost:0.0";
  318.     }
  319.     else {
  320.         /* Display is non-NULL, copy the pointer */
  321.         display_name = strdup((char *)display);
  322.     }
  323.  
  324. /*
  325.  * Set the default error handlers.  This allows the global variables to
  326.  * default to NULL for use with shared libraries.
  327.  */
  328.     if (_XErrorFunction == NULL) (void) XSetErrorHandler (NULL);
  329.     if (_XIOErrorFunction == NULL) (void) XSetIOErrorHandler (NULL);
  330.  
  331.     if (!xinitialized) {
  332.         APIRET rc;
  333.         PPIB pib = NULL;
  334.  
  335.         /* Make this a PM app */
  336.         rc = DosGetInfoBlocks(NULL,&pib);
  337.         oldapptype = pib->pib_ultype;
  338.         pib->pib_ultype = 3;
  339.         mypid = pib->pib_ulpid;
  340.  
  341.         pthread_setfuncs((void *)WinRequestMutexSem, (void *)WinWaitEventSem);
  342.  
  343.         DosQueryModuleHandle("X11.DLL", &module);
  344.  
  345.         memset(GCList, 0, sizeof(GCList));
  346.  
  347.         pthread_init();
  348.  
  349. #ifdef OS2I18N
  350.         Xlib_InitOS2I18N();
  351. #endif
  352.  
  353.         pthread_mutex_init(&evmutex, NULL);
  354.         
  355.         _XCreateMutex(_Xglobal_lock);
  356.     
  357.         mainhab = WinInitialize(0);
  358.         mainhmq = WinCreateMsgQueue(mainhab, 0);
  359.         pmatomtbl = WinQuerySystemAtomTable();
  360.  
  361.         hwndDesktop = WinQueryDesktopWindow(mainhab, NULLHANDLE);
  362.  
  363.         pipe(pmout);
  364.  
  365.         mainthread = pthread_self();
  366.         res = pthread_create(&pmthread, NULL, pm_thread, NULL);
  367.     }
  368.  
  369.     if (!maindisplay) {
  370.         char buildmark[80];
  371.         maindisplay = new_display(display_name);
  372.  
  373.         while (!xinitialized) _sleep2(10);
  374.  
  375.         Xlib_InitAtoms(maindisplay);
  376.  
  377.         Xlib_buildmark(buildmark,sizeof(buildmark));
  378.         fprintf(stderr,"PM-Xlib built %s,\n",buildmark);
  379.  
  380.         result = maindisplay;
  381.     } else
  382.         result = new_display(display_name);
  383.  
  384.     /*
  385.      * call into synchronization routine so that all programs can be
  386.      * forced synchronous
  387.      */
  388.     (void) XSynchronize(result, _Xdebug);
  389.  
  390.     if (result == maindisplay) {
  391.         extern Display *_XHeadOfDisplayList;
  392.         OsInitColors();
  393.         _XHeadOfDisplayList = result;
  394.     }
  395.  
  396.     XkbUseExtension(result, NULL, NULL);
  397.  
  398.     /*
  399.         XEvent new;
  400.         extern int serial;
  401.         new.xany.serial = serial++;
  402.         new.xany.display = result;
  403.         new.xany.window = None;
  404.         new.xany.send_event = True;
  405.         new.type = MappingNotify;
  406.         new.xmapping.request = MappingModifier;
  407.         XRefreshKeyboardMapping(&new);
  408.     */
  409.  
  410.     DBUG_RETURN(result);
  411. }