home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / XLIBEMU / OPENDISP.C < prev    next >
C/C++ Source or Header  |  1994-02-20  |  6KB  |  251 lines

  1. /* $Id: opendisp.c 1.5 1994/02/20 20:00:55 ulrich Exp $ */
  2. /*
  3.  * opendisp.c
  4.  *
  5.  * X library functions for open/close display.
  6.  */
  7. #include "Xlibemu.h"
  8.  
  9. #include <pc.h>
  10. #include <dos.h>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13.  
  14. GrContext _WScreenContext;
  15.  
  16. #ifndef NO_TERM_RESTORE
  17. static int    _TermMode;
  18. static int    _TermSize;
  19. static short *    _TermSave;
  20. static int    _TermCurR;
  21. static int    _TermCurC;
  22. #endif
  23.  
  24. static int    _RootWindow = None;
  25.  
  26. int GxScreenDepth ()
  27. {
  28.   int n, i;
  29.  
  30.   n = GrNumColors ();
  31.   i = 0;
  32.   while (n > 1) { n >>= 1; i++; }
  33.   return i;
  34. }
  35.  
  36. int
  37. _WOpenConnection (Display* dpy)
  38. {
  39.   int i;
  40.   
  41.   for (i = 0; i < MAX_CONN; i++) {
  42.     if (_ConnTable[i] == 0) {
  43.       _ConnTable[i] = dpy;
  44.       return i;
  45.     }
  46.   }
  47.   return -1;
  48. }
  49.  
  50. int
  51. _WCloseConnection (Display* dpy)
  52. {
  53.   int i;
  54.   
  55.   for (i = 0; i < MAX_CONN; i++) {
  56.     if (_ConnTable[i] == dpy) {
  57.       _ConnTable[i] = 0;
  58.       return i;
  59.     }
  60.   }
  61.   return -1;
  62. }
  63.  
  64. int
  65. _WGetConnection (Display* dpy)
  66. {
  67.   int i;
  68.   
  69.   for (i = 0; i < MAX_CONN; i++) {
  70.     if (_ConnTable[i] == dpy) {
  71.       return i;
  72.     }
  73.   }
  74.   return -1;
  75. }
  76.  
  77. #ifdef GO32_NO_DPMI
  78. /* GO32 version 1.08 - 1.10 */
  79. #define ScreenRetrieve(v) \
  80.     memcpy (_TermSave, ScreenPrimary, _TermSize)
  81. #define ScreenUpdate(v) \
  82.     memcpy (ScreenPrimary, _TermSave, _TermSize);
  83. #endif
  84.  
  85.  
  86. Display *
  87. XOpenDisplay (_Xconst char* display_name)
  88. {
  89.   Display *dpy;
  90.   Screen *scr;
  91.   Window root;
  92.   Cursor cursor;
  93.   Pixmap bitmap;
  94.   XColor color;
  95.   int mode_flags = 0;
  96.   char *xsetroot;
  97.  
  98.   /*
  99.    * If the display specifier string supplied as an argument to this 
  100.    * routine is NULL or a pointer to NULL, read the DISPLAY variable.
  101.    */
  102.   if (display_name == NULL || *display_name == '\0') {
  103.     display_name = getenv ("DISPLAY");
  104.   }
  105.   if (display_name == NULL) {
  106.     display_name = "";
  107.   }
  108.  
  109.   if (GrCurrentMode() <= GR_biggest_text) {
  110.     int mode, width, height, colors;
  111.  
  112. #ifndef NO_TERM_RESTORE
  113.     /* Save current terminal mode and contents */
  114.     _TermMode = ScreenMode();
  115.     ScreenGetCursor (&_TermCurR, &_TermCurC);
  116.     _TermSize = ScreenRows() * ScreenCols() * sizeof (short);
  117.     _TermSave = (short *) Xmalloc (_TermSize);
  118.     ScreenRetrieve (_TermSave);
  119. #endif
  120.  
  121.     switch (sscanf ((char *) display_name,
  122.             "%dx%dx%d", &width, &height, &colors)) {
  123.     case 2:
  124.       mode = GR_width_height_graphics;
  125.       break;
  126.     case 3:
  127.       mode = GR_width_height_color_graphics;
  128.       break;
  129.     default:
  130.       mode = GR_default_graphics;
  131.       break;
  132.     }
  133.     GrSetMode (mode, width, height, colors);
  134.     if (GrCurrentMode() <= GR_biggest_text) {
  135.       fprintf (stderr, "Cannot open display %s\n", display_name);
  136.       exit(1);
  137.     }
  138.     GrSaveContext (&_WScreenContext);
  139.  
  140.     _WQueueInit ();
  141.     _WMouseDisplayCursor ();
  142.  
  143.     mode_flags = 1;
  144.   }
  145.  
  146.   dpy = (Display *) Xcalloc (1, sizeof (Display));
  147.   if (dpy == (Display *) 0) return 0;
  148.  
  149.   scr = (Screen *) Xcalloc (1, sizeof (Screen));
  150.   if (scr == (Screen *) 0) return 0;
  151.   
  152.   scr->display    = dpy;
  153.   scr->width    = GrScreenX ();
  154.   scr->height    = GrScreenY ();
  155.   /*
  156.    * display width 14 inch monitor == 250 mm, assume 640x480 standard resolution,
  157.    * for greater resolutions assume greater monitor.
  158.    */
  159.   scr->mwidth    = (250 * scr->width) / 640;
  160.   scr->mheight    = (250 * scr->width) / 480;
  161.  
  162.   scr->root_depth = GxScreenDepth ();
  163.   scr->root_visual = (Visual *) Xcalloc (1, sizeof (Visual));
  164.   scr->root_visual->class = PseudoColor;
  165.   scr->root_visual->map_entries = GrNumColors ();
  166.  
  167.   scr->ndepths    = 1;
  168.   scr->depths    = (Depth *) Xmalloc (scr->ndepths * sizeof(Depth));
  169.   scr->depths[0].depth = GxScreenDepth();
  170.   scr->depths[0].nvisuals = 1;
  171.   scr->depths[0].visuals = scr->root_visual;
  172.  
  173.   scr->white_pixel = GrWhite ();
  174.   scr->black_pixel = GrBlack ();
  175.  
  176.   dpy->flags |= mode_flags;
  177.   dpy->display_name = (char *) Xmalloc (strlen (display_name) + 1);
  178.   strcpy (dpy->display_name, display_name);
  179.   dpy->nscreens    = 1;
  180.   dpy->screens    = scr;
  181.   DefaultRootWindow (dpy) = None;
  182.   dpy->free_funcs = (_XFreeFuncRec *) Xcalloc (1, sizeof(_XFreeFuncRec));
  183.   dpy->min_keycode = MIN_KEYCODE;
  184.   dpy->max_keycode = MAX_KEYCODE;
  185.   dpy->fd = _WOpenConnection (dpy);
  186.  
  187.   if (_RootWindow == None) {
  188.     root = XCreateSimpleWindow (dpy, None, 0, 0, scr->width, scr->height, 
  189.                 0, scr->black_pixel, scr->black_pixel);
  190.     cursor = XCreateFontCursor (dpy, 0 /* XC_X_cursor */);
  191.     _WDefineCursor (cursor);
  192.     XDefineCursor (dpy, root, cursor);
  193.     
  194.     xsetroot = getenv ("XSETROOT");
  195.     if (! xsetroot)
  196.       xsetroot = "solid black";
  197.  
  198.     if (! strncmp (xsetroot, "solid ", 6)) {
  199.       if (XAllocNamedColor (dpy,
  200.                 DefaultColormap (dpy, DefaultScreen (dpy)),
  201.                 xsetroot + 6,
  202.                 &color, &color)) {
  203.     XSetWindowBackground (dpy, root, color.pixel);
  204.       }
  205.     }
  206.     if (! strncmp (xsetroot, "bitmap ", 7)) {
  207.       unsigned int width, height;
  208.       int x_hot, y_hot;
  209.  
  210.       if (BitmapSuccess ==
  211.       XReadBitmapFile (dpy, (Drawable) root,
  212.                xsetroot + 7,
  213.                &width, &height, &bitmap, &x_hot, &y_hot)) {
  214.     XSetWindowBackgroundPixmap (dpy, root, bitmap);
  215.       }
  216.     }
  217.     XMapWindow (dpy, root);
  218.     _RootWindow = root;
  219.   }
  220.   DefaultRootWindow (dpy) = _RootWindow;
  221.   return dpy;
  222. }
  223.  
  224. int XCloseDisplay(Display* display)
  225. {
  226.   if (display == NULL) return 0;
  227.  
  228.   if (display->flags & 1) {
  229.     _WMouseEraseCursor();
  230.     _WQueueUnInit();
  231. #ifdef NO_TERM_RESTORE
  232.     GrSetMode (GR_default_text);
  233. #else
  234.     {
  235.       union REGS r;
  236.       r.h.al = _TermMode;
  237.       r.h.ah = 0;
  238.       int86 (0x10, &r, &r);
  239.       ScreenUpdate (_TermSave);
  240.       ScreenSetCursor (_TermCurR, _TermCurC);
  241.     }
  242. #endif
  243.   }
  244.  
  245.   _WCloseConnection (display);
  246.   XFree (display->screens);
  247.   XFree (display);
  248.  
  249.   return 0;
  250. }
  251.