home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_X11.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  9KB  |  349 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Michael Schoene & Heinz W. Werntges.
  3.    All rights reserved. Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** to_x11.c: X11 preview part of project "hp2xx" (UNIX only)
  25.  **
  26.  ** 92/01/15  V 1.00  HWW  Derived from to_vga.c (V1.01b)
  27.  **              X11 essentials due to M. Schoene
  28.  ** 92/01/28  V 1.01  HWW  Window offset user-defined via -o -O
  29.  ** 92/02/03  V 1.02  HWW  bug fixes, error handling
  30.  ** 92/05/19  V 1.02b HWW  Abort if color mode
  31.  ** 92/05/25  V 1.10  HWW  8 Colors supported
  32.  ** 93/01/06  V 1.10b HWW  Improved selection of foreground color
  33.  **
  34.  **          NOTE: Color assignment leaves something to be desired
  35.  **            with some X11 servers.
  36.  **/
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <X11/Xlib.h>
  42. #include <X11/Xutil.h>
  43.  
  44. #include "bresnham.h"
  45. #include "hp2xx.h"
  46. #include "x11.h"
  47.  
  48.  
  49.  
  50.  
  51. #define WIN_NAME    "x11"        /* Window name         */
  52. #define PROG_NAME    "hp2xx"        /* Program name            */
  53.  
  54.  
  55.  
  56. /**
  57.  ** Global variables for X11
  58.  **/
  59.  
  60. static Display*        XDisplay = NULL;    /* Workstation id    */
  61. static int        XScreen;
  62. static Window        XRoot;            /* Number of root window    */
  63. static Visual*        XVisual = NULL;
  64.  
  65. static GC        XGcWin;
  66. static Window        XWin;            /* Window id        */
  67.  
  68. static    unsigned long    col_table[CMS_SIZE];
  69. static    XColor        Xcol;
  70. static    Colormap    def_cmap;
  71.  
  72. /**
  73.  ** Screen sizes
  74.  **/
  75.  
  76. static int    scr_width;
  77. static int    scr_height;
  78.  
  79.  
  80. /**
  81.  ** Window sizes
  82.  **/
  83.  
  84. static int    width;
  85. static int    bytes;
  86. static int    height;
  87.  
  88.  
  89.  
  90. /**
  91.  ** Initialize X11 and open window
  92.  **/
  93.  
  94. int    win_open( int x, int y, int w, int h )
  95. {
  96.     char*            DisplayName = NULL;
  97.     char**            argv;
  98.     XSizeHints        Hints;
  99.     unsigned long        ValueMask;
  100.     XSetWindowAttributes    WinAttr;
  101.     XEvent            Event;
  102.  
  103.  
  104.     /**
  105.      ** Simulate command line arguments
  106.      **/
  107.  
  108.     argv = (char**) malloc( 2 * sizeof( char* ) );
  109.     argv[0] = PROG_NAME;
  110.     argv[1] = NULL;
  111.  
  112.     /**
  113.      ** X11 server reachable ?
  114.      **/
  115.  
  116.     if ((XDisplay = (Display *) XOpenDisplay( DisplayName )) == NULL)
  117.     {
  118.         fprintf( stderr, "No X11 server found !\n" );
  119.         return( NO_SERVER );
  120.     }
  121.  
  122.     XScreen        = DefaultScreen( XDisplay );
  123.     XRoot        = RootWindow( XDisplay, XScreen );
  124.     XVisual        = DefaultVisual( XDisplay, XScreen );
  125.     XGcWin        = DefaultGC( XDisplay, XScreen );
  126.  
  127.     scr_width    = WidthOfScreen( ScreenOfDisplay( XDisplay, XScreen ) );
  128.     scr_height    = HeightOfScreen(ScreenOfDisplay( XDisplay, XScreen ) );
  129.     if (x+w > scr_width || y+h > scr_height)
  130.     {
  131.         fprintf( stderr, "Window exceeds screen limits !\n" );
  132.         return( SIZE );
  133.     }
  134.  
  135.     /**
  136.      ** Set window attributes
  137.      **/
  138.  
  139.     WinAttr.background_pixel= WhitePixel( XDisplay, XScreen );
  140.     WinAttr.border_pixel    = WhitePixel( XDisplay, XScreen );
  141.     WinAttr.backing_store    = Always;
  142.     ValueMask = CWBackPixel | CWBorderPixel | CWBackingStore;
  143.  
  144.     /**
  145.      ** Create Window
  146.      **/
  147.  
  148.     XWin = XCreateWindow( XDisplay, XRoot,
  149.                           x, y, w, h,
  150.                           1, 0,
  151.                           CopyFromParent,
  152.                           CopyFromParent,
  153.                           ValueMask, &WinAttr );
  154.  
  155.     /**
  156.      ** Define window properties
  157.      **/
  158.  
  159.     Hints.flags = PSize | PMinSize | PMaxSize | USPosition;
  160.     Hints.x        = x;
  161.     Hints.y        = y;
  162.     Hints.width    = Hints.min_width  = Hints.max_width  = w;
  163.     Hints.height= Hints.min_height = Hints.max_height = h;
  164.  
  165.     XSetStandardProperties( XDisplay, XWin,
  166.                     WIN_NAME, WIN_NAME, NULL,
  167.                     argv, 1, &Hints );
  168.  
  169. /**
  170.  ** Define color table (compatible to SunView and Turbo-C usage)
  171.  **/
  172.  
  173.   def_cmap    = DefaultColormap( XDisplay, XScreen );
  174.   if (DefaultDepth( XDisplay, XScreen ) < 4)
  175.   {
  176.     col_table[BLACK]    = WhitePixel( XDisplay, XScreen );
  177.     col_table[WHITE]    = BlackPixel( XDisplay, XScreen );
  178.     col_table[GRAY]        = col_table[WHITE];
  179.     col_table[RED]        = col_table[WHITE];
  180.     col_table[GREEN]    = col_table[WHITE];
  181.     col_table[BLUE]        = col_table[WHITE];
  182.     col_table[CYAN]        = col_table[WHITE];
  183.     col_table[MAGENTA]    = col_table[WHITE];
  184.     col_table[YELLOW]    = col_table[WHITE];
  185.     col_table[LIGHTGRAY]    = col_table[WHITE];
  186.     col_table[LIGHTRED]    = col_table[WHITE];
  187.     col_table[LIGHTGREEN]    = col_table[WHITE];
  188.     col_table[LIGHTBLUE]    = col_table[WHITE];
  189.     col_table[LIGHTCYAN]    = col_table[WHITE];
  190.     col_table[LIGHTMAGENTA]    = col_table[WHITE];
  191.   }
  192.   else
  193.   {
  194.     XParseColor( XDisplay, def_cmap, "gray",&Xcol );
  195.     XAllocColor( XDisplay, def_cmap, &Xcol );
  196.     col_table[GRAY] = Xcol.pixel;
  197.  
  198.     XParseColor( XDisplay, def_cmap, "orange red",&Xcol );
  199.     XAllocColor( XDisplay, def_cmap, &Xcol );
  200.     col_table[RED] = Xcol.pixel;
  201.  
  202.     XParseColor( XDisplay, def_cmap, "pale green",&Xcol );
  203.     XAllocColor( XDisplay, def_cmap, &Xcol );
  204.     col_table[GREEN] = Xcol.pixel;
  205.  
  206.     XParseColor( XDisplay, def_cmap, "medium blue",&Xcol );
  207.     XAllocColor( XDisplay, def_cmap, &Xcol );
  208.     col_table[BLUE] = Xcol.pixel;
  209.  
  210.     XParseColor( XDisplay, def_cmap, "medium aquamarine",&Xcol );
  211.     XAllocColor( XDisplay, def_cmap, &Xcol );
  212.     col_table[CYAN] = Xcol.pixel;
  213.  
  214.     XParseColor( XDisplay, def_cmap, "medium violet red",&Xcol );
  215.     XAllocColor( XDisplay, def_cmap, &Xcol );
  216.     col_table[MAGENTA] = Xcol.pixel;
  217.  
  218.     XParseColor( XDisplay, def_cmap, "yellow",&Xcol );
  219.     XAllocColor( XDisplay, def_cmap, &Xcol );
  220.     col_table[YELLOW] = Xcol.pixel;
  221.  
  222.     XParseColor( XDisplay, def_cmap, "light gray",&Xcol );
  223.     XAllocColor( XDisplay, def_cmap, &Xcol );
  224.     col_table[LIGHTGRAY] = Xcol.pixel;
  225.  
  226.     XParseColor( XDisplay, def_cmap, "red",&Xcol );
  227.     XAllocColor( XDisplay, def_cmap, &Xcol );
  228.     col_table[LIGHTRED] = Xcol.pixel;
  229.  
  230.     XParseColor( XDisplay, def_cmap, "green",&Xcol );
  231.     XAllocColor( XDisplay, def_cmap, &Xcol );
  232.     col_table[LIGHTGREEN] = Xcol.pixel;
  233.  
  234.     XParseColor( XDisplay, def_cmap, "blue",&Xcol );
  235.     XAllocColor( XDisplay, def_cmap, &Xcol );
  236.     col_table[LIGHTBLUE] = Xcol.pixel;
  237.  
  238.     XParseColor( XDisplay, def_cmap, "cyan",&Xcol );
  239.     XAllocColor( XDisplay, def_cmap, &Xcol );
  240.     col_table[LIGHTCYAN] = Xcol.pixel;
  241.  
  242.     XParseColor( XDisplay, def_cmap, "magenta",&Xcol );
  243.     XAllocColor( XDisplay, def_cmap, &Xcol );
  244.     col_table[LIGHTMAGENTA] = Xcol.pixel;
  245.  
  246.     XParseColor( XDisplay, def_cmap, "black",&Xcol );
  247.     XAllocColor( XDisplay, def_cmap, &Xcol );
  248.     col_table[WHITE] = Xcol.pixel;
  249.  
  250.     XParseColor( XDisplay, def_cmap, "white",&Xcol );
  251.     XAllocColor( XDisplay, def_cmap, &Xcol );
  252.     col_table[BLACK] = Xcol.pixel;
  253.   }
  254.  
  255.     /**
  256.      **  Set foreground and background colors
  257.      **/
  258.  
  259.     XSetState (     XDisplay, XGcWin,
  260.             col_table[BLACK], col_table[WHITE],
  261.             GXcopy, AllPlanes );
  262.  
  263.     /**
  264.      ** Define permitted events for this window
  265.      **/
  266.  
  267.     XSelectInput( XDisplay, XWin, ExposureMask | VisibilityChangeMask);
  268.  
  269.     /**
  270.      ** Display window
  271.      **/
  272.  
  273.     XMapWindow( XDisplay, XWin );
  274.     do {
  275.         XNextEvent( XDisplay, &Event);
  276.     } while (Event.type != Expose && Event.type != VisibilityNotify);
  277.  
  278.     width    = w;
  279.     height    = h;
  280.     bytes    = (w + 7) / 8;
  281.  
  282.     free( (char*) argv );
  283.     return( 0 );
  284. }
  285.  
  286.  
  287.  
  288. void    win_close()
  289. {
  290.     XDestroyWindow( XDisplay, XWin );
  291.     XCloseDisplay( XDisplay );
  292. }
  293.  
  294.  
  295.  
  296.  
  297. #define    setXcolor(col) XSetForeground (XDisplay, XGcWin, col_table[col])
  298.  
  299.  
  300.  
  301.  
  302. void    PicBuf_to_X11 (PicBuf *picbuf, PAR *p)
  303. /**
  304.  ** Interface to higher-level routines,
  305.  **   similar in structure to other previewers
  306.  **/
  307. {
  308. int    row_c, x, y;
  309. RowBuf    *row;
  310.  
  311.   if (!p->quiet)
  312.   {
  313.     fprintf(stderr, "\nX11 preview follows.\n");
  314.     fprintf(stderr, "Press <return> to end graphics mode\n");
  315.   }
  316.  
  317.   if (win_open( (int)(p->xoff * p->dpi_x / 25.4),
  318.         (int)(p->yoff * p->dpi_y / 25.4),
  319.         picbuf->nb << 3, picbuf->nr ) )
  320.     return;
  321.  
  322.   /* Backward since highest index is lowest line on screen! */
  323.   for (row_c=0, y=picbuf->nr-1; row_c < picbuf->nr; row_c++, y--)
  324.   {
  325.     row = get_RowBuf (picbuf, row_c);
  326.     for (x=0; x < picbuf->nc; x++)
  327.     {
  328.         switch (index_from_RowBuf(row, x, picbuf))
  329.         {
  330.         case xxBackground:            continue;
  331.         case xxForeground:setXcolor (WHITE);    break;
  332.         case xxRed:    setXcolor (RED);    break;
  333.         case xxGreen:    setXcolor (GREEN);    break;
  334.         case xxBlue:    setXcolor (BLUE);    break;
  335.         case xxCyan:    setXcolor (CYAN);    break;
  336.         case xxMagenta:    setXcolor (MAGENTA);    break;
  337.         case xxYellow:    setXcolor (YELLOW);    break;
  338.         default:                continue;
  339.         }
  340.         XDrawPoint (XDisplay, XWin, XGcWin, x, y);
  341.     }
  342.   }
  343.  
  344.   XFlush( XDisplay );
  345.   SilentWait();
  346.   win_close();
  347. }
  348.  
  349.