home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / ddx-mips.zip / MIPSGENF.C < prev    next >
C/C++ Source or Header  |  1992-08-17  |  8KB  |  327 lines

  1. /*
  2.  * $XConsortium$
  3.  *
  4.  * Copyright 1991 MIPS Computer Systems, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of MIPS not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  MIPS makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * MIPS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL MIPS
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #ident    "$Header: mipsGenFb.c,v 1.4 92/08/17 16:22:31 dd Exp $"
  24.  
  25. /*
  26.  * Color frame buffer support
  27.  *
  28.  * Uses frame buffer device (e.g. /dev/fb0).  This is available in RISC/os
  29.  * 5.0B and later, for the RC4230 and related systems.
  30.  */
  31.  
  32. #if MIPS_RISCOS_VERSION >= 5
  33.  
  34. #include <sys/types.h>
  35. #include <sys/mman.h>
  36. #include <sys/fb_ioctl.h>    /* RISC/os 5.0B and later only */
  37.  
  38. #include <X.h>
  39. #include <Xproto.h>
  40. #include <misc.h>
  41. #include <cursorstr.h>
  42. #include <input.h>
  43. #include <scrnintstr.h>
  44. #include <servermd.h>
  45.  
  46. #include "mipsFb.h"
  47.  
  48. extern caddr_t mmap();
  49. extern char *mipsMapit();
  50.  
  51. static int BlankFb();
  52. static int WriteCMapFb();
  53. static void CloseFb();
  54.  
  55. #ifdef FB_FORMAT_CURSOR        /* RISC/os 5.01 and later only */
  56. #define    CURSOR_SUPPORT
  57. #endif
  58.  
  59. #ifdef CURSOR_SUPPORT
  60. static Bool RealizeCursorFb();
  61. static int SetCursorFb();
  62. static int MoveCursorFb();
  63. static int RecolorCursorFb();
  64. #endif /* CURSOR_SUPPORT */
  65.  
  66. Bool
  67. mipsMapFb(pm)
  68.     MipsScreenPtr pm;
  69. {
  70.     int fd = pm->fd;
  71.     struct fb_type fb_type;
  72.     struct fb_mapinfo fb_mapinfo;
  73.  
  74.     static int save_fd[MAXSCREENS];
  75.  
  76.     /*
  77.      * We don't really want to close the frame buffer device
  78.      * because the terminal emulator screen will appear momentarily.
  79.      * It's easier to dup here than to figure out how to avoid
  80.      * reopening it (but stupid).
  81.      */
  82.     if (save_fd[pm->unit] <= 0)
  83.         save_fd[pm->unit] = dup(fd) + 1;
  84.  
  85.     if (ioctl(fd, FB_GET_TYPE, &fb_type) < 0)
  86.         return FALSE;
  87.  
  88.     pm->scr_width = fb_type.width;
  89.     pm->scr_height = fb_type.height;
  90.  
  91.     fb_mapinfo.depth = 0;
  92.     if (ioctl(fd, FB_GET_MAPINFO, &fb_mapinfo) < 0)
  93.         return FALSE;
  94.  
  95.     pm->depth = fb_mapinfo.depth;
  96.     pm->bitsPerPixel = fb_mapinfo.bits_per_pixel;
  97.     pm->fb_width = fb_mapinfo.pixels_per_line;
  98.  
  99.     {
  100.         caddr_t addr;
  101.         off_t off = fb_mapinfo.mmap_offset;
  102.         size_t size = fb_mapinfo.mmap_size;
  103.  
  104.         if ((int) off & 0xff)
  105.             addr = (caddr_t) mipsMapit((char *) 0,
  106.                 (int) off, (int) size);
  107.         else if ((addr = mmap((caddr_t) 0, size,
  108.                 PROT_READ | PROT_WRITE, MAP_SHARED,
  109.                 fd, off)) != (caddr_t) -1)
  110.             pm->fb_mmap_size = (int) size;
  111.         else
  112.             addr = 0;
  113.  
  114.         if (addr == 0)
  115.             return FALSE;
  116.  
  117.         pm->fbnorm = (unsigned char *) addr;
  118.         pm->fbnocache = (unsigned char *) addr;
  119.     }
  120.  
  121.     pm->dpi = mipsMonitorDPI(pm);
  122.  
  123.     pm->Blank = (void (*)()) BlankFb;
  124.     pm->WriteCMap = (void (*)()) WriteCMapFb;
  125.     pm->Close = CloseFb;
  126.  
  127.     if (!(pm->option & MIPS_SCR_NOHW)) {
  128. #ifdef CURSOR_SUPPORT
  129.         struct fb_cursor_xy cursize;
  130.  
  131.         if (ioctl(fd, FB_GET_CURSOR_SIZE, &cursize) >= 0) {
  132.             pm->cap = MIPS_SCR_CURSOR;
  133.             pm->RealizeCursor = RealizeCursorFb;
  134.             pm->SetCursor = (void (*) ()) SetCursorFb;
  135.             pm->MoveCursor = (void (*) ()) MoveCursorFb;
  136.             pm->RecolorCursor = (void (*) ()) RecolorCursorFb;
  137.         }
  138. #endif /* CURSOR_SUPPORT */
  139.  
  140. #ifdef FB_REMAP
  141.         if (pm->fb_mmap_size) {
  142.             struct fb_map fb_map;
  143.  
  144.             fb_map.addr = (caddr_t) pm->fbnorm;
  145.             fb_map.size = pm->fb_mmap_size;
  146.             if (ioctl(fd, FB_REMAP, &fb_map) < 0)
  147.                 Error("mipsMapFb -- FB_REMAP");
  148.         }
  149. #endif /* FB_REMAP */
  150.     }
  151.  
  152.     return TRUE;
  153. }
  154.  
  155. static int
  156. BlankFb(pm, on)
  157.     MipsScreenPtr pm;
  158.     Bool on;
  159. {
  160.     int video = (on != SCREEN_SAVER_ON);
  161.  
  162.     return ioctl(pm->fd, FB_SET_VIDEO, &video);
  163. }
  164.  
  165. static int
  166. WriteCMapFb(pm, index, count, rgb)
  167.     MipsScreenPtr pm;
  168.     int index;
  169.     int count;
  170.     unsigned char *rgb;
  171. {
  172.     struct fb_colormap fb_colormap;
  173.  
  174.     fb_colormap.map = 0;
  175.     fb_colormap.index = index;
  176.     fb_colormap.count = count;
  177.     fb_colormap.rgb = rgb;
  178.  
  179.     return ioctl(pm->fd, FB_SET_COLORMAP, &fb_colormap);
  180. }
  181.  
  182. static void
  183. CloseFb(pm)
  184.     MipsScreenPtr pm;
  185. {
  186.     caddr_t addr;
  187.     size_t size;
  188.  
  189. #ifdef CURSOR_SUPPORT
  190.     /* disable HW cursor */
  191.     (void) SetCursorFb(pm, (CursorPtr) 0, (pointer) 0, 0, 0);
  192. #endif /* CURSOR_SUPPORT */
  193.  
  194.     if (addr = (caddr_t) pm->fbnorm)
  195.         if (size = (size_t) pm->fb_mmap_size) {
  196. #ifdef FB_UNMAP
  197.             struct fb_map fb_map;
  198.  
  199.             fb_map.addr = addr;
  200.             fb_map.size = size;
  201.             (void) ioctl(pm->fd, FB_UNMAP, &fb_map);
  202. #endif /* FB_UNMAP */
  203.  
  204.             (void) munmap(addr, size);
  205.         }
  206.         else
  207.             (void) shmdt((char *) addr);
  208.  
  209.     (void) close(pm->fd);
  210. }
  211.  
  212. #ifdef CURSOR_SUPPORT
  213.  
  214. struct priv_shape {
  215.     int shape_bytes;
  216.     char shape_data[4];
  217. };
  218.  
  219. static Bool
  220. RealizeCursorFb(pm, pCurs, pPriv)
  221.     MipsScreenPtr pm;
  222.     CursorPtr pCurs;
  223.     pointer *pPriv;
  224. {
  225.     CursorBitsPtr bits = pCurs->bits;
  226.     struct priv_shape *priv_shape;
  227.     struct fb_cursor_shape shape;
  228.  
  229.     shape.size.x = bits->width;
  230.     shape.size.y = bits->height;
  231.  
  232.     /* XXX handle padding and bit order */
  233.     shape.image = (unsigned int *) bits->source;
  234.     shape.mask = (unsigned int *) bits->mask;
  235.     shape.shape_data = 0;
  236.  
  237.     if (ioctl(pm->fd, FB_FORMAT_CURSOR, &shape) < 0)
  238.         return FALSE;
  239.  
  240.     priv_shape = (struct priv_shape *)
  241.         xalloc(sizeof (struct priv_shape) + shape.shape_bytes - 4);
  242.     *pPriv = (pointer) priv_shape;
  243.     if (priv_shape == 0)
  244.         return FALSE;
  245.  
  246.     shape.shape_data = priv_shape->shape_data;
  247.     if (ioctl(pm->fd, FB_FORMAT_CURSOR, &shape) < 0) {
  248.         xfree(priv_shape);
  249.         return FALSE;
  250.     }
  251.     priv_shape->shape_bytes = shape.shape_bytes;
  252.  
  253.     return TRUE;
  254. }
  255.  
  256. static int
  257. SetCursorFb(pm, pCurs, priv, x, y)
  258.     MipsScreenPtr pm;
  259.     CursorPtr pCurs;
  260.     pointer priv;
  261.     int x, y;
  262. {
  263.     struct fb_cursor curs;
  264.  
  265.     curs.set = FB_CUR_SETCUR;
  266.     curs.enable = 0;
  267.  
  268.     if (pCurs) {
  269.         curs.set = FB_CUR_SETCUR | FB_CUR_SETPOS |
  270.             FB_CUR_SETHOT | FB_CUR_SETCOLORMAP | FB_CUR_SETSHAPE;
  271.  
  272.         curs.enable = 1;
  273.  
  274.         curs.rgb[0] = pCurs->backRed >> 8;
  275.         curs.rgb[1] = pCurs->backGreen >> 8;
  276.         curs.rgb[2] = pCurs->backBlue >> 8;
  277.         curs.rgb[3] = pCurs->foreRed >> 8;
  278.         curs.rgb[4] = pCurs->foreGreen >> 8;
  279.         curs.rgb[5] = pCurs->foreBlue >> 8;
  280.  
  281.         curs.pos.x = x;
  282.         curs.pos.y = y;
  283.  
  284.         curs.hot.x = pCurs->bits->xhot;
  285.         curs.hot.y = pCurs->bits->yhot;
  286.  
  287.         curs.shape_bytes = ((struct priv_shape *) priv)->shape_bytes;
  288.         curs.shape_data = ((struct priv_shape *) priv)->shape_data;
  289.     }
  290.  
  291.     return ioctl(pm->fd, FB_SET_CURSOR, &curs);
  292. }
  293.  
  294. static int
  295. MoveCursorFb(pm, x, y)
  296.     MipsScreenPtr pm;
  297.     int x, y;
  298. {
  299.     struct fb_cursor_xy pos;
  300.  
  301.     pos.x = x;
  302.     pos.y = y;
  303.  
  304.     return ioctl(pm->fd, FB_SET_CURSOR_POSITION, &pos);
  305. }
  306.  
  307. static int
  308. RecolorCursorFb(pm, pCurs)
  309.     MipsScreenPtr pm;
  310.     CursorPtr pCurs;
  311. {
  312.     struct fb_cursor curs;
  313.  
  314.     curs.set = FB_CUR_SETCOLORMAP;
  315.     curs.rgb[0] = pCurs->backRed >> 8;
  316.     curs.rgb[1] = pCurs->backGreen >> 8;
  317.     curs.rgb[2] = pCurs->backBlue >> 8;
  318.     curs.rgb[3] = pCurs->foreRed >> 8;
  319.     curs.rgb[4] = pCurs->foreGreen >> 8;
  320.     curs.rgb[5] = pCurs->foreBlue >> 8;
  321.  
  322.     return ioctl(pm->fd, FB_SET_CURSOR, &curs);
  323. }
  324. #endif /* CURSOR_SUPPORT */
  325.  
  326. #endif /* MIPS_RISCOS_VERSION >= 5 */
  327.