home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / VOGLE / DRIVERS / APOLLO.C next >
C/C++ Source or Header  |  2000-02-11  |  12KB  |  577 lines

  1. /*
  2.  *    Apollo driver for vogle.
  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 "vogle.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.     gpr_$acquire_display(status);
  311.     gpr_$clear((long)current_color, status);
  312.     gpr_$release_display(status);
  313. }
  314.  
  315. /*
  316.  * APOLLO_exit
  317.  *
  318.  *    reset the window back to normal mode (sigh)
  319.  */
  320. APOLLO_exit()
  321. {
  322.     if (disp.n_planes > 1) {
  323.         gpr_$acquire_display(status);
  324.         gpr_$set_color_map((long)0,
  325.                     (short)MAXCOLORS,
  326.                     old_color_value,
  327.                     status);
  328.         gpr_$release_display(status);
  329.     }
  330.     gpr_$terminate(delete_display,status); /*Terminate gpr.*/
  331. }
  332.  
  333. /*
  334.  * APOLLO_color
  335.  *
  336.  *    change the drawing color.
  337.  */
  338. APOLLO_color(ind)
  339.     int    ind;
  340. {
  341.     if (disp.n_planes <= 1) {
  342.         if (ind > 0)
  343.             current_color = 1;
  344.  
  345.         if (disp.invert)
  346.             current_color = !current_color;
  347.  
  348.     } else {
  349.         current_color = ind % MAXCOLORS;
  350.     }
  351.  
  352.     gpr_$set_draw_value((int)current_color, status); 
  353.     gpr_$set_text_value((int)current_color, status);
  354.     gpr_$set_fill_value((int)current_color, status);
  355.     /*
  356.      * GPR manual says that this sets text background to
  357.      * 'transparent'
  358.      */
  359.     gpr_$set_text_background_value((int)-1, status);
  360. }
  361.  
  362. /*
  363.  * APOLLO_mapcolor
  364.  *
  365.  *    set a colormap entry.
  366.  */
  367. APOLLO_mapcolor(in, r, g, b)
  368.     int    in;
  369.     short    r, g, b;
  370. {
  371.     if (in < 0 || in > MAXCOLORS)
  372.         verror("APOLLO_mapcolor: index out of range");
  373.     
  374.     color_value[in] = COLOR_ENTRY(r, g, b);
  375.  
  376.     /* modify color table */
  377.  
  378.     gpr_$acquire_display(status);
  379.     gpr_$set_color_map((long)0,
  380.                 (short)MAXCOLORS,
  381.                 color_value,
  382.                 status);
  383.     gpr_$release_display(status);
  384. }
  385.  
  386. /*
  387.  * APOLLO_fill
  388.  *
  389.  *    draw a filled polygon
  390.  */
  391. APOLLO_fill(n, x, y)
  392.     int    n;
  393.     int    *x, *y;
  394. {
  395.     short     nx[256], ny[256], startx, starty;
  396.     int    i;
  397.  
  398.     for (i = 1; i < n; i++) {
  399.         ny[i-1] = vdevice.sizeSy - y[i];
  400.         nx[i-1] = x[i];
  401.     }
  402.  
  403.     startx = x[0];
  404.     starty = vdevice.sizeSy - y[0];
  405.  
  406.     gpr_$acquire_display(status);
  407.     gpr_$start_pgon(startx, starty, status);
  408.  
  409.     gpr_$pgon_polyline(nx, ny, (short)(n - 1), status);
  410.     gpr_$close_fill_pgon(status);
  411.     gpr_$release_display(status);
  412. }
  413.  
  414. /*
  415.  * APOLLO_font
  416.  *
  417.  *    load in a hardware font
  418.  */
  419. APOLLO_font(fontfile)
  420.     char    *fontfile;
  421. {
  422.     gpr_$offset_t start;
  423.     short xy_end;
  424.     char    buf[256];
  425.  
  426.     if (first_time) {
  427.         gpr_$unload_font_file(font_id, status);
  428.         first_time = 0;
  429.     }
  430.  
  431.     strcpy(buf, FONTBASE);
  432.     strcat(buf, fontfile);
  433.     gpr_$load_font_file(buf, (short)strlen(buf), font_id, status);
  434.  
  435.     if (status.all) 
  436.         return (0);
  437.  
  438.     gpr_$set_text_font(font_id, status);
  439.  
  440.     /* see above comment about &'s */
  441.  
  442.     gpr_$inq_text_offset("H", (short)1, start, xy_end, status);
  443.  
  444.     vdevice.hwidth = xy_end;
  445.     vdevice.hheight = start.y_size;
  446.  
  447.     return (1);
  448. }
  449.  
  450. /*
  451.  * APOLLO_char
  452.  *
  453.  *    output a character
  454.  */
  455. APOLLO_char(c)
  456.     char    c;
  457. {
  458.     char s[2];
  459.     s[1] = '\0';
  460.     s[0] = c;
  461.     gpr_$acquire_display(status);
  462.     gpr_$move((short)vdevice.cpVx, (short)(vdevice.sizeSy - vdevice.cpVy), status);
  463.     gpr_$text(s[0], (short)1, status);
  464.     gpr_$release_display(status);
  465. }
  466.  
  467. /*
  468.  * APOLLO_string
  469.  *
  470.  *    display a string
  471.  */
  472. APOLLO_string(s)
  473.     char    s[];
  474. {
  475.     short    len;
  476.  
  477.     len = (short)strlen(s);
  478.     gpr_$acquire_display(status);
  479.     gpr_$move((short)vdevice.cpVx, (short)(vdevice.sizeSy - vdevice.cpVy), status);
  480.     gpr_$text(s[0], len, status);
  481.     gpr_$release_display(status);
  482. }
  483.  
  484. /*
  485.  * APOLLO_backb
  486.  *
  487.  *    Allocates and sets the back display buffer.
  488.  */
  489. int
  490. APOLLO_backb()
  491. {
  492.     if (!back_used) {
  493.         gpr_$allocate_attribute_block(attribs, status);
  494.         gpr_$allocate_bitmap(init_bitmap_size, (short)hi_plane_id, attribs, back_bitmap, status);
  495.         gpr_$enable_input(gpr_$keystroke, keys, status);
  496.         gpr_$enable_input(gpr_$buttons, keys, status);
  497.         gpr_$enable_input(gpr_$locator, keys, status);
  498.         back_used = 1;
  499.         if (status.all) {
  500.             fprintf(stderr, "APOLLO_backb: problem (status = %d)\n", status);
  501.             error_$print(status);
  502.             return(-1);
  503.         }
  504.     }
  505.  
  506.     gpr_$set_bitmap(back_bitmap, status);
  507.     gpr_$set_color_map((long)0,
  508.                 (short)MAXCOLORS,
  509.                 color_value,
  510.                 status);
  511.  
  512.     current_bitmap = back_bitmap;
  513.  
  514.     return(1);
  515. }
  516.  
  517. /*
  518.  * APOLLO_swapb
  519.  *
  520.  *    Swap the front and back buffers' role - actually just copy
  521.  *    the back buffer to the display buffer.
  522.  */
  523. APOLLO_swapb()
  524. {
  525.     gpr_$acquire_display(status);
  526.     gpr_$set_bitmap(front_bitmap, status);
  527.     gpr_$pixel_blt(back_bitmap, source, dest_pos, status);
  528.     gpr_$set_bitmap(back_bitmap, status);
  529.     gpr_$release_display(status);
  530. }
  531.  
  532. /*
  533.  * APOLLO_frontb
  534.  *
  535.  *    Make sure we are drawing in the front (display) buffer
  536.  */
  537. APOLLO_frontb()
  538. {
  539.     current_bitmap = front_bitmap;
  540.     gpr_$set_bitmap(front_bitmap, status);
  541. }
  542.  
  543.  
  544. static DevEntry apdev = {
  545.     "apollo",
  546.     "f16.b",
  547.     "f7x13.b",
  548.     APOLLO_backb,
  549.     APOLLO_char,
  550.     APOLLO_checkkey,
  551.     APOLLO_clear,
  552.     APOLLO_color,
  553.     APOLLO_draw,
  554.     APOLLO_exit,
  555.     APOLLO_fill,
  556.     APOLLO_font,
  557.     APOLLO_frontb,
  558.     APOLLO_getkey,
  559.     APOLLO_init,
  560.     APOLLO_locator,
  561.     APOLLO_mapcolor,
  562.     noop,
  563.     APOLLO_string,
  564.     APOLLO_swapb,
  565.     noop
  566. };
  567.  
  568. /*
  569.  * _APOLLO_devcpy
  570.  *
  571.  *      copy the apollo device into vdevice.dev.
  572.  */
  573. _APOLLO_devcpy()
  574. {
  575.     vdevice.dev = apdev;
  576. }
  577.