home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / avogl.tar.gz / avogl.tar / vogl / drivers / apollo.c < prev    next >
C/C++ Source or Header  |  1992-09-22  |  12KB  |  591 lines

  1. /*
  2.  *    Apollo driver for vogl.
  3.  */
  4.  
  5. #include "/sys/ins/base.ins.c"
  6. #include "/sys/ins/gpr.ins.c"
  7. #include "/sys/ins/pad.ins.c"
  8. #include "/sys/ins/kbd.ins.c"
  9. #include <stdio.h>
  10. #include "vogl.h"
  11.  
  12. #define FONTBASE "/sys/dm/fonts/"
  13.  
  14. #define MAXCOLORS        256
  15. #define MIN(x,y)        ((x) < (y) ? (x) : (y))
  16. #define COLOR_ENTRY(r, g, b)    (gpr_$pixel_value_t) ((r << 16) | (g << 8) | b)
  17.  
  18. static ios_$id_t        stream_id;
  19. static gpr_$pixel_value_t      color_value[MAXCOLORS];
  20. static gpr_$pixel_value_t      old_color_value[MAXCOLORS];
  21.  
  22. static gpr_$offset_t            init_bitmap_size;   
  23. static gpr_$bitmap_desc_t       current_bitmap, front_bitmap, back_bitmap;       
  24. static gpr_$attribute_desc_t       attribs;
  25. static gpr_$window_t           source;
  26. static gpr_$position_t           dest_pos;
  27.  
  28. static gpr_$display_mode_t      mode = gpr_$direct;
  29. static gpr_$plane_t             hi_plane_id = 0;    
  30. static boolean                  delete_display;    
  31. static status_$t                status;
  32. static gpr_$keyset_t            keys, mouse_keys;
  33.  
  34. static pad_$window_list_t     window_info;
  35. static short             n_windows;
  36. static gpr_$disp_char_t        disp;
  37. static short            disp_len;
  38.  
  39. static int             first_time, back_used;
  40. static int             current_color;
  41. static short             font_id;
  42.  
  43. /*
  44.  * Do nothing
  45.  *
  46.  */
  47. int
  48. noop()
  49. {
  50.     return(-1);
  51. }
  52.  
  53. /*
  54.  * APOLLO_init
  55.  *
  56.  *    initialises window to occupy current window
  57.  */
  58. APOLLO_init()
  59. {
  60.     pad_$window_desc_t     window;
  61.     int     size, prefx, prefy, prefxs, prefys, x, y, w, h;
  62.     short     i;
  63.  
  64.     pad_$set_scale(stream_$stdout,
  65.                1,
  66.                1,
  67.                status);
  68.  
  69.     pad_$inq_windows(stream_$stdout,
  70.              window_info,
  71.                   10,
  72.                n_windows,
  73.                  status);
  74.  
  75.     w = window_info[0].width;
  76.     h = window_info[0].height;
  77.     x = window_info[0].left;
  78.     y = window_info[0].top;
  79.  
  80.     getprefposandsize(&prefx, &prefy, &prefxs, &prefys);
  81.  
  82.     if (prefx > -1) {
  83.         x = prefx;
  84.         y = prefy;
  85.     }
  86.  
  87.     if (prefxs > -1) {
  88.         w = prefxs;
  89.         h = prefys;
  90.     }
  91.  
  92.     size = MIN(w, h);
  93.     vdevice.sizeX = vdevice.sizeY = size - 1;
  94.     vdevice.sizeSx = w - 1;
  95.     vdevice.sizeSy = h - 1;
  96.  
  97.         init_bitmap_size.x_size = w;
  98.         init_bitmap_size.y_size = h;
  99.  
  100.     source.window_base.x_coord = source.window_base.y_coord = 0;
  101.     source.window_size.x_size = init_bitmap_size.x_size;
  102.     source.window_size.y_size = init_bitmap_size.y_size;
  103.     dest_pos.x_coord = dest_pos.y_coord = 0;
  104.  
  105.     /*
  106.      * Inquire about the actual display ....
  107.      */
  108.  
  109.     gpr_$inq_disp_characteristics(mode,
  110.                       stream_$stdout,
  111.                       (short)60,
  112.                       disp,
  113.                       disp_len,
  114.                       status);
  115.  
  116.     vdevice.depth = disp.n_planes;
  117.     hi_plane_id = disp.n_planes - 1;
  118.  
  119.     if (prefx == -1 && prefxs == -1) {
  120.         stream_id = stream_$stdout;
  121.     } else {
  122.         window.top = y;
  123.         window.left = x;
  124.         window.width = w;
  125.         window.height = h;
  126.  
  127.         pad_$create_window("", 0,
  128.                 pad_$transcript, 
  129.                 1, 
  130.                 window,
  131.                 stream_id,
  132.                 status
  133.         );
  134.  
  135.         pad_$set_auto_close(stream_id, 1, true, status);
  136.     }
  137.  
  138.         gpr_$init(mode,
  139.                   stream_id,
  140.                   init_bitmap_size,
  141.                   hi_plane_id,
  142.                   front_bitmap,
  143.                   status);
  144.  
  145.     current_bitmap = front_bitmap;
  146.  
  147.     gpr_$set_auto_refresh(true, status);
  148.   
  149.     gpr_$set_cursor_active(false,status);
  150.  
  151.     /*  Set up all the character stuff  */
  152.  
  153.     first_time = 1;
  154.  
  155.     /*  create a key set for the event interupts  */
  156.  
  157.     lib_$init_set(keys, (short)256);
  158.     lib_$init_set(mouse_keys, (short)6);
  159.  
  160.     lib_$add_to_set(mouse_keys, (short)6, KBD_$M1D);
  161.     lib_$add_to_set(mouse_keys, (short)6, KBD_$M2D);
  162.     lib_$add_to_set(mouse_keys, (short)6, KBD_$M3D);
  163.     lib_$add_to_set(mouse_keys, (short)6, KBD_$M1U);
  164.     lib_$add_to_set(mouse_keys, (short)6, KBD_$M2U);
  165.     lib_$add_to_set(mouse_keys, (short)6, KBD_$M3U);
  166.  
  167.     for (i = 0; i < 128; i++) 
  168.         lib_$add_to_set(keys, (short)256, (short)i);
  169.  
  170.     gpr_$enable_input(gpr_$keystroke, keys, status);
  171.     gpr_$enable_input(gpr_$buttons, keys, status);
  172.     gpr_$enable_input(gpr_$locator, keys, status);
  173.  
  174.     /*  set default color (colour)   */
  175.  
  176.     if (disp.n_planes > 1) {
  177.         gpr_$inq_color_map(0L, (short)MAXCOLORS, old_color_value, status);
  178.         gpr_$inq_color_map(0L, (short)MAXCOLORS, color_value, status);
  179.  
  180.         color_value[0] = COLOR_ENTRY(0,0,0);         /* color--black   */
  181.         color_value[1] = COLOR_ENTRY(255,0,0);       /* color--red     */
  182.         color_value[2] = COLOR_ENTRY(0,255,0);       /* color--green   */
  183.         color_value[3] = COLOR_ENTRY(255,255,0);     /* color--yellow  */
  184.         color_value[4] = COLOR_ENTRY(0,0,255);       /* color--blue    */
  185.         color_value[5] = COLOR_ENTRY(255,0,255);     /* color--magenta */
  186.         color_value[6] = COLOR_ENTRY(0,255,255);     /* color--cyan    */
  187.         color_value[7] = COLOR_ENTRY(255,255,255);   /* color--white   */
  188.  
  189.         /* modify color table */
  190.         gpr_$acquire_display(status);
  191.         gpr_$set_color_map((long)0,
  192.                     (short)MAXCOLORS,
  193.                     color_value,
  194.                     status);
  195.         gpr_$release_display(status);
  196.     }
  197.  
  198.     back_used = 0;
  199.  
  200.     return(1);
  201. }
  202.  
  203.  
  204. /*
  205.  * APOLLO_draw
  206.  *
  207.  *    Draw a lines from the current graphics position to (x, y).
  208.  */
  209. APOLLO_draw(x, y)
  210.     int    x, y;
  211. {
  212.     gpr_$acquire_display(status);
  213.     gpr_$move((short)vdevice.cpVx, (short)(vdevice.sizeSy - vdevice.cpVy), status);
  214.     gpr_$line((short)x, (short)(vdevice.sizeSy - y), status);
  215.     gpr_$release_display(status);
  216.     vdevice.cpVx = x;
  217.     vdevice.cpVy = y;
  218. }
  219.  
  220. /*
  221.  * APOLLO_getkey
  222.  *
  223.  *    grab a character from the keyboard
  224.  */
  225. int
  226. APOLLO_getkey()
  227. {
  228.     gpr_$event_t     et;
  229.     char         ed;
  230.     gpr_$position_t  pos;
  231.  
  232.  
  233.     et = gpr_$no_event;
  234.     while (et != gpr_$keystroke)
  235.         (void)gpr_$event_wait(et, ed, pos, status);
  236.  
  237.     /*
  238.      * What for this stupid gpr event system map the return key
  239.      * to a SYN character
  240.      */
  241.  
  242.     if ((ed & 0x7f) == 0x16)
  243.         ed = 13;
  244.  
  245.     return(ed);
  246. }
  247.  
  248. /*
  249.  * APOLLO_checkkey
  250.  *
  251.  *    checks if there is key waiting in the keyboard
  252.  */
  253. int
  254. APOLLO_checkkey()
  255. {
  256.     gpr_$event_t     et;
  257.     char             ed;
  258.     gpr_$position_t  pos;
  259.  
  260.     et = gpr_$no_event;
  261.  
  262.     (void)gpr_$cond_event_wait(et, ed, pos, status);
  263.  
  264.     return((et == gpr_$keystroke ? 1 : 0));
  265. }
  266.  
  267. /*
  268.  * APOLLO_locator
  269.  *
  270.  *      return the window location of the cursor, plus which mouse button,
  271.  * if any, is been pressed.
  272.  *
  273.  *    LOCATOR - needs to return straight away.
  274.  */
  275. int
  276. APOLLO_locator(wx, wy)
  277.     int    *wx, *wy;
  278. {
  279.     gpr_$event_t     et;
  280.     char             ed;
  281.     gpr_$position_t  pos;
  282.     gpr_$position_t  origin;
  283.  
  284.     gpr_$bitmap_desc_t    curs_pat;
  285.     gpr_$raster_op_array_t    curs_raster_op;
  286.     boolean            active;
  287.     
  288.  
  289.  
  290.     (void)gpr_$cond_event_wait(et, ed, pos, status);
  291.  
  292.     gpr_$inq_cursor(curs_pat, curs_raster_op, active, pos, origin, status);
  293.  
  294.     *wx = (int)pos.x_coord;
  295.     *wy = (int)vdevice.sizeSy - (int)pos.y_coord;
  296.  
  297.     if (ed < 'a')            /* absorb button up */
  298.         return(0);
  299.  
  300.     return(1 << ((int)ed - 'a'));
  301. }
  302.  
  303. /*
  304.  * APOLLO_clear
  305.  *
  306.  *    clear the window to the current color.
  307.  */
  308. APOLLO_clear()
  309. {
  310.         int     x[4], y[4];
  311.  
  312.         if (vdevice.maxVx != vdevice.sizeSx
  313.            || vdevice.maxVy != vdevice.sizeSy
  314.            || vdevice.minVx != vdevice.sizeSx
  315.            || vdevice.minVy != vdevice.sizeSy) {
  316.                 x[0] = x[1] = vdevice.maxVx;
  317.                 y[0] = vdevice.maxVy;
  318.                 y[1] = y[2] = vdevice.minVy;
  319.                 x[2] = x[3] = vdevice.minVx;
  320.                 y[3] = vdevice.maxVy;
  321.  
  322.                 APOLLO_fill(4, x, y);
  323.         } else {
  324.         gpr_$acquire_display(status);
  325.         gpr_$clear((long)current_color, status);
  326.         gpr_$release_display(status);
  327.         }
  328.  
  329. }
  330.  
  331. /*
  332.  * APOLLO_exit
  333.  *
  334.  *    reset the window back to normal mode (sigh)
  335.  */
  336. APOLLO_exit()
  337. {
  338.     if (disp.n_planes > 1) {
  339.         gpr_$acquire_display(status);
  340.         gpr_$set_color_map((long)0,
  341.                     (short)MAXCOLORS,
  342.                     old_color_value,
  343.                     status);
  344.         gpr_$release_display(status);
  345.     }
  346.     gpr_$terminate(delete_display,status); /*Terminate gpr.*/
  347. }
  348.  
  349. /*
  350.  * APOLLO_color
  351.  *
  352.  *    change the drawing color.
  353.  */
  354. APOLLO_color(ind)
  355.     int    ind;
  356. {
  357.     if (disp.n_planes <= 1) {
  358.         if (ind > 0)
  359.             current_color = 1;
  360.  
  361.         if (disp.invert)
  362.             current_color = !current_color;
  363.  
  364.     } else {
  365.         current_color = ind % MAXCOLORS;
  366.     }
  367.  
  368.     gpr_$set_draw_value((int)current_color, status); 
  369.     gpr_$set_text_value((int)current_color, status);
  370.     gpr_$set_fill_value((int)current_color, status);
  371.     /*
  372.      * GPR manual says that this sets text background to
  373.      * 'transparent'
  374.      */
  375.     gpr_$set_text_background_value((int)-1, status);
  376. }
  377.  
  378. /*
  379.  * APOLLO_mapcolor
  380.  *
  381.  *    set a colormap entry.
  382.  */
  383. APOLLO_mapcolor(in, r, g, b)
  384.     int    in;
  385.     short    r, g, b;
  386. {
  387.     if (in < 0 || in > MAXCOLORS)
  388.         verror("APOLLO_mapcolor: index out of range");
  389.     
  390.     color_value[in] = COLOR_ENTRY(r, g, b);
  391.  
  392.     /* modify color table */
  393.  
  394.     gpr_$acquire_display(status);
  395.     gpr_$set_color_map((long)0,
  396.                 (short)MAXCOLORS,
  397.                 color_value,
  398.                 status);
  399.     gpr_$release_display(status);
  400. }
  401.  
  402. /*
  403.  * APOLLO_fill
  404.  *
  405.  *    draw a filled polygon
  406.  */
  407. APOLLO_fill(n, x, y)
  408.     int    n;
  409.     int    *x, *y;
  410. {
  411.     short     nx[256], ny[256], startx, starty;
  412.     int    i;
  413.  
  414.     for (i = 1; i < n; i++) {
  415.         ny[i-1] = vdevice.sizeSy - y[i];
  416.         nx[i-1] = x[i];
  417.     }
  418.  
  419.     startx = x[0];
  420.     starty = vdevice.sizeSy - y[0];
  421.  
  422.     gpr_$acquire_display(status);
  423.     gpr_$start_pgon(startx, starty, status);
  424.  
  425.     gpr_$pgon_polyline(nx, ny, (short)(n - 1), status);
  426.     gpr_$close_fill_pgon(status);
  427.     gpr_$release_display(status);
  428. }
  429.  
  430. /*
  431.  * APOLLO_font
  432.  *
  433.  *    load in a hardware font
  434.  */
  435. APOLLO_font(fontfile)
  436.     char    *fontfile;
  437. {
  438.     gpr_$offset_t start;
  439.     short xy_end;
  440.     char    buf[256];
  441.  
  442.     if (first_time) {
  443.         gpr_$unload_font_file(font_id, status);
  444.         first_time = 0;
  445.     }
  446.  
  447.     strcpy(buf, FONTBASE);
  448.     strcat(buf, fontfile);
  449.     gpr_$load_font_file(buf, (short)strlen(buf), font_id, status);
  450.  
  451.     if (status.all) 
  452.         return (0);
  453.  
  454.     gpr_$set_text_font(font_id, status);
  455.  
  456.     /* see above comment about &'s */
  457.  
  458.     gpr_$inq_text_offset("H", (short)1, start, xy_end, status);
  459.  
  460.     vdevice.hwidth = xy_end;
  461.     vdevice.hheight = start.y_size;
  462.  
  463.     return (1);
  464. }
  465.  
  466. /*
  467.  * APOLLO_char
  468.  *
  469.  *    output a character
  470.  */
  471. APOLLO_char(c)
  472.     char    c;
  473. {
  474.     char s[2];
  475.     s[1] = '\0';
  476.     s[0] = c;
  477.     gpr_$acquire_display(status);
  478.     gpr_$move((short)vdevice.cpVx, (short)(vdevice.sizeSy - vdevice.cpVy), status);
  479.     gpr_$text(s[0], (short)1, status);
  480.     gpr_$release_display(status);
  481. }
  482.  
  483. /*
  484.  * APOLLO_string
  485.  *
  486.  *    display a string
  487.  */
  488. APOLLO_string(s)
  489.     char    s[];
  490. {
  491.     short    len;
  492.  
  493.     len = (short)strlen(s);
  494.     gpr_$acquire_display(status);
  495.     gpr_$move((short)vdevice.cpVx, (short)(vdevice.sizeSy - vdevice.cpVy), status);
  496.     gpr_$text(s[0], len, status);
  497.     gpr_$release_display(status);
  498. }
  499.  
  500. /*
  501.  * APOLLO_backb
  502.  *
  503.  *    Allocates and sets the back display buffer.
  504.  */
  505. int
  506. APOLLO_backb()
  507. {
  508.     if (!back_used) {
  509.         gpr_$allocate_attribute_block(attribs, status);
  510.         gpr_$allocate_bitmap(init_bitmap_size, (short)hi_plane_id, attribs, back_bitmap, status);
  511.         gpr_$enable_input(gpr_$keystroke, keys, status);
  512.         gpr_$enable_input(gpr_$buttons, keys, status);
  513.         gpr_$enable_input(gpr_$locator, keys, status);
  514.         back_used = 1;
  515.         if (status.all) {
  516.             fprintf(stderr, "APOLLO_backb: problem (status = %d)\n", status);
  517.             error_$print(status);
  518.             return(-1);
  519.         }
  520.     }
  521.  
  522.     gpr_$set_bitmap(back_bitmap, status);
  523.     gpr_$set_color_map((long)0,
  524.                 (short)MAXCOLORS,
  525.                 color_value,
  526.                 status);
  527.  
  528.     current_bitmap = back_bitmap;
  529.  
  530.     return(1);
  531. }
  532.  
  533. /*
  534.  * APOLLO_swapb
  535.  *
  536.  *    Swap the front and back buffers' role - actually just copy
  537.  *    the back buffer to the display buffer.
  538.  */
  539. APOLLO_swapb()
  540. {
  541.     gpr_$acquire_display(status);
  542.     gpr_$set_bitmap(front_bitmap, status);
  543.     gpr_$pixel_blt(back_bitmap, source, dest_pos, status);
  544.     gpr_$set_bitmap(back_bitmap, status);
  545.     gpr_$release_display(status);
  546. }
  547.  
  548. /*
  549.  * APOLLO_frontb
  550.  *
  551.  *    Make sure we are drawing in the front (display) buffer
  552.  */
  553. APOLLO_frontb()
  554. {
  555.     current_bitmap = front_bitmap;
  556.     gpr_$set_bitmap(front_bitmap, status);
  557. }
  558.  
  559.  
  560. static DevEntry apdev = {
  561.     "apollo",
  562.     "f16.b",
  563.     "f7x13.b",
  564.     APOLLO_backb,
  565.     APOLLO_char,
  566.     APOLLO_checkkey,
  567.     APOLLO_clear,
  568.     APOLLO_color,
  569.     APOLLO_draw,
  570.     APOLLO_exit,
  571.     APOLLO_fill,
  572.     APOLLO_font,
  573.     APOLLO_frontb,
  574.     APOLLO_getkey,
  575.     APOLLO_init,
  576.     APOLLO_locator,
  577.     APOLLO_mapcolor,
  578.     APOLLO_string,
  579.     APOLLO_swapb
  580. };
  581.  
  582. /*
  583.  * _APOLLO_devcpy
  584.  *
  585.  *      copy the apollo device into vdevice.dev.
  586.  */
  587. _APOLLO_devcpy()
  588. {
  589.     vdevice.dev = apdev;
  590. }
  591.