home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / dgtool / dgtool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-20  |  9.0 KB  |  431 lines

  1. /*
  2.  * Copyright (c) 1989 Southwest Research Institute
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by Southwest Research Institute.  The name of Southwest Research
  11.  * Institute may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char     sccsid[] = "@(#)dgtool.c    1.9    6/29/89        Copyright (c) 1989\
  20.  by Southwest Research Institute, San Antonio, Texas";
  21. #endif
  22.  
  23. /* 
  24.  * path name for `dg' - this is system dependent
  25.  */
  26.  
  27.  
  28. #define DG    "/usr/local/bin/dg"
  29.  
  30. #include <stdio.h>
  31. #include <strings.h>
  32. #include <suntool/sunview.h>
  33. #include <suntool/panel.h>
  34. #include <suntool/canvas.h>
  35. #include <sunwindow/win_cursor.h>
  36. #include <suntool/tty.h>
  37. #include <sundev/kbio.h>
  38. #include <sundev/kbd.h>
  39. #include <sgtty.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43.  
  44. #define NUM_KEYS_TO_SAVE    8
  45.  
  46. Frame    base_frame;
  47. Tty    tty_win;
  48. static void    input_proc();
  49. static Notify_value    my_tty_interposer();
  50. static Notify_value    my_destroy_interposer();
  51. static int    kb_fd;
  52.  
  53. static struct kiockey save_keys[NUM_KEYS_TO_SAVE] = {
  54.                     {0, 0x45, 0, ""}, /* up arrow */
  55.                     {0, 0x5d, 0, ""}, /* right arrow */
  56.                     {0, 0x5b, 0, ""}, /*left arrow */
  57.                     {0, 0x71, 0, ""}, /*down arrow */
  58.                     {SHIFTMASK, 0x45, 0, ""}, /* up arrow */
  59.                     {SHIFTMASK, 0x5d, 0, ""}, /* right arrow */
  60.                     {SHIFTMASK, 0x5b, 0, ""}, /*left arrow */
  61.                     {SHIFTMASK, 0x71, 0, ""}, /*down arrow */
  62. };
  63.  
  64. static void    get_keys();
  65. static void    fix_keys();
  66. static void    reset_keys();
  67. static void    do_key();
  68. static void    check_baud();
  69. static void    do_c_key();
  70.  
  71. /* support an icon */
  72. static short    icon_image[] = {
  73. #include    "dgtool.icon"
  74. };
  75.  
  76. DEFINE_ICON_FROM_IMAGE(dgtool_icon, icon_image);
  77.  
  78. struct baud_tab {
  79.     char    *baud_string;
  80.     int baud_rate;
  81. } baud_tab[] = {
  82.     "50",   B50,
  83.     "75",   B75,
  84.     "110",  B110,
  85.     "134",  B134,
  86.     "150",  B150,
  87.     "200",  B200,
  88.     "300",  B300,
  89.     "600",  B600,
  90.     "1200", B1200,
  91.     "1800", B1800,
  92.     "2400", B2400,
  93.     "4800", B4800,
  94.     "9600", B9600,
  95.     "19200",    EXTA,
  96.     NULL,   0
  97. };
  98.  
  99. main(argc, argv)
  100. int             argc;
  101. char          **argv;
  102. {
  103.     int    test_fd;
  104.  
  105.     kb_fd = open("/dev/kbd", 2);
  106.     if (kb_fd < 0) {
  107.         fprintf(stderr, "dgtool: keyboard open failed\n");
  108.         exit(1);
  109.     }
  110.  
  111.     base_frame = window_create(0, FRAME,
  112.                    WIN_ROWS,    25,
  113.                    WIN_COLUMNS,    80,
  114.                    FRAME_LABEL, "dgtool",
  115.                    FRAME_ICON, &dgtool_icon,
  116.                    FRAME_ARGC_PTR_ARGV, &argc, argv,
  117.                    0);
  118.  
  119.     if ((argc > 3) || (argc < 2)) {
  120.         fprintf(stderr, "dgtool /dev/tty?|host [baud] [window args]\n");
  121.         exit(1);
  122.     }
  123.  
  124.     if ( ((test_fd = open(argv[1], 2)) < 0 ) && (gethostbyname(argv[0]) != NULL) ) {
  125.         fprintf(stderr, "dgtool: can't open the line to the DG or find a host\n");
  126.         exit(1);
  127.     } else {
  128.          if (test_fd >= 0)
  129.         close(test_fd);
  130.     }
  131.     if (argc == 3) {
  132.         check_baud(argv[2]);
  133.     }
  134.  
  135.     argv[0] = DG;
  136.  
  137.     tty_win = window_create(base_frame, TTY, TTY_ARGV, argv, 0);
  138.  
  139.     window_set(tty_win, WIN_CONSUME_KBD_EVENTS,
  140.             WIN_ASCII_EVENTS,
  141.             WIN_RIGHT_KEYS,
  142.             WIN_TOP_KEYS,
  143.             0,
  144.             0);
  145.     window_set(tty_win, WIN_IGNORE_KBD_EVENT, WIN_UP_EVENTS, 0);
  146.  
  147.     notify_interpose_destroy_func(base_frame, my_destroy_interposer);
  148.     notify_interpose_event_func(tty_win, my_tty_interposer, NOTIFY_SAFE);
  149.  
  150.     window_main_loop(base_frame);
  151.     exit(0);
  152. }
  153.  
  154.  
  155.  
  156. static void
  157. get_keys()
  158. {
  159.      register int i;
  160.  
  161.      for( i = 0; i < NUM_KEYS_TO_SAVE; i++) {
  162.       if (ioctl(kb_fd, KIOCGETKEY, &save_keys[i]) < 0) {
  163.            fprintf(stderr, "dgtool: getkeys - kb ioctl failed\n");
  164.            exit(1);
  165.       }
  166.      }
  167. }
  168.  
  169. static void
  170. reset_keys()
  171. {
  172.      register int i;
  173.  
  174.      for( i = 0; i < NUM_KEYS_TO_SAVE; i++) {
  175.       if (ioctl(kb_fd, KIOCSETKEY, &save_keys[i]) < 0) {
  176.            fprintf(stderr, "dgtool: reset_keys - kb ioctl failed\n");
  177.            exit(1);
  178.       }
  179.      }
  180. }
  181.  
  182. static void
  183. fix_keys()
  184. {
  185.     struct kiockey    tmp_key;
  186.  
  187.     tmp_key = save_keys[0];    /* up arrow */
  188.     tmp_key.kio_entry = 027;
  189.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  190.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  191.         exit(1);
  192.     }
  193.  
  194.     tmp_key = save_keys[4];    /* shifted up arrow */
  195.     strcpy(tmp_key.kio_string, "\036\027");
  196.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  197.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  198.         exit(1);
  199.     }
  200.  
  201.     tmp_key = save_keys[1];    /* right arrow */
  202.     tmp_key.kio_entry = 030;
  203.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  204.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  205.         exit(1);
  206.     }
  207.     tmp_key = save_keys[5];    /* shifted right arrow */
  208.     strcpy(tmp_key.kio_string, "\036\030");
  209.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  210.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  211.         exit(1);
  212.     }
  213.  
  214.     tmp_key = save_keys[2];    /* left arrow */
  215.     tmp_key.kio_entry = 031;
  216.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  217.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  218.         exit(1);
  219.     }
  220.     tmp_key = save_keys[6];    /* shifted left arrow */
  221.     strcpy(tmp_key.kio_string, "\036\031");
  222.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  223.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  224.         exit(1);
  225.     }
  226.  
  227.     tmp_key = save_keys[3];    /* down arrow */
  228.     tmp_key.kio_entry = 032;
  229.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  230.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  231.         exit(1);
  232.     }
  233.     tmp_key = save_keys[7];    /* shifted down arrow */
  234.     strcpy(tmp_key.kio_string, "\036\032");
  235.     if (ioctl(kb_fd, KIOCSETKEY, &tmp_key) < 0) {
  236.         fprintf(stderr, "dgtool: kb ioctl failed\n");
  237.         exit(1);
  238.     }
  239.  
  240. }
  241.  
  242. static Notify_value
  243. my_destroy_interposer(frame, status)
  244. Frame    frame;
  245. Destroy_status    status;
  246. {
  247.     if (status == DESTROY_CLEANUP) {
  248.         reset_keys();
  249.     }
  250.     return(notify_next_destroy_func(frame, status));
  251. }
  252.  
  253. static Notify_value
  254. my_tty_interposer(frame, event, arg, type)
  255. Frame    frame;
  256. Event    *event;
  257. Notify_arg    arg;
  258. Notify_event_type    type;
  259. {
  260.     register int    input;
  261.     register int    shift, ctrl;
  262.     char    baby_buffer;    /* for arrow keys */
  263.     Notify_value    value;
  264.  
  265.     input = event_id(event);
  266.  
  267.     if (input == KBD_USE) {
  268.         get_keys();
  269.         fix_keys();
  270.     }
  271.  
  272.     if (input == KBD_DONE) {
  273.         reset_keys();
  274.     }
  275.  
  276.     if ((event_is_key_top(event)) || (event_is_key_right(event)) ) {
  277.  
  278.         if ( event_ctrl_is_down(event)) {
  279.             ctrl = 1;
  280.         } else {
  281.             ctrl = 0;
  282.         }
  283.  
  284.         if ( event_shift_is_down(event)) {
  285.             shift = 1;
  286.         } else {
  287.             shift = 0;
  288.         }
  289.  
  290.         switch(input) {
  291.         /*
  292.          * function keys
  293.          */
  294.         case KEY_RIGHT(6):
  295.             do_key(0160, shift, ctrl);
  296.             break;
  297.         case KEY_TOP(1):
  298.             do_key(0161, shift, ctrl);
  299.             break;
  300.         case KEY_TOP(2):
  301.             do_key(0162, shift, ctrl);
  302.             break;
  303.         case KEY_TOP(3):
  304.             do_key(0163, shift, ctrl);
  305.             break;
  306.         case KEY_TOP(4):
  307.             do_key(0164, shift, ctrl);
  308.             break;
  309.         case KEY_TOP(5):
  310.             do_key(0165, shift, ctrl);
  311.             break;
  312.         case KEY_TOP(6):
  313.             do_key(0166, shift, ctrl);
  314.             break;
  315.         case KEY_TOP(7):
  316.             do_key(0167, shift, ctrl);
  317.             break;
  318.         case KEY_TOP(8):
  319.             do_key(0170, shift, ctrl);
  320.             break;
  321.         case KEY_TOP(9):
  322.             do_key(0171, shift, ctrl);
  323.             break;
  324.         case KEY_RIGHT(1):
  325.             do_key(0172, shift, ctrl);
  326.             break;
  327.         case KEY_RIGHT(2):
  328.             do_key(0173, shift, ctrl);
  329.             break;
  330.         case KEY_RIGHT(3):
  331.             do_key(0174, shift, ctrl);
  332.             break;
  333.         case KEY_RIGHT(4):
  334.             do_key(0175, shift, ctrl);
  335.             break;
  336.         case KEY_RIGHT(5):
  337.             do_key(0176, shift, ctrl);
  338.             break;
  339.         /*
  340.          * home
  341.          */
  342.         case KEY_RIGHT(11):
  343.             baby_buffer = 010;
  344.             ttysw_input(tty_win, &baby_buffer, 1);
  345.             break;
  346.         /*
  347.          * "c" keys
  348.          */
  349.         case KEY_RIGHT(7):
  350.             do_c_key(0134, shift);
  351.             break;
  352.         case KEY_RIGHT(9):
  353.             do_c_key(0135, shift);
  354.             break;
  355.         case KEY_RIGHT(13):
  356.             do_c_key(0136, shift);
  357.             break;
  358.         case KEY_RIGHT(15):
  359.             do_c_key(0137, shift);
  360.             break;
  361.         default:
  362.             break;
  363.         }
  364.         return(NOTIFY_DONE);
  365.     }
  366.     value = notify_next_event_func(frame, event, arg, type);
  367.     return(value);
  368. }
  369.  
  370. static void
  371. do_key(code, shift, ctrl)
  372. int    code, shift, ctrl;
  373. {
  374.     register char    out_code;
  375.     static char    buffer[2] = "\036";
  376.  
  377.  
  378.     if (shift && ctrl) {
  379.         out_code = code - 0120;
  380.     } else if (shift) {
  381.         out_code = code - 020;
  382.     } else if (ctrl) {
  383.         out_code = code - 0100;
  384.     } else {
  385.         out_code = code;
  386.     }
  387.     buffer[1] = out_code;
  388.     ttysw_input(tty_win, buffer, 2);
  389. }
  390.  
  391. static void
  392. check_baud(string)
  393. char *string;
  394. {
  395.     struct baud_tab *btp;
  396.  
  397.     btp = baud_tab;
  398.  
  399.     while(btp->baud_string != NULL) {
  400.         if (strcmp (string, btp->baud_string) == 0)
  401.             return;
  402.         btp++;
  403.     }
  404.     fprintf(stderr, "dgtool: unknown baud rate\n");
  405.     fprintf(stderr, "known baud rates are:\n");
  406.  
  407.     btp = baud_tab;
  408.     while(btp->baud_string != NULL) {
  409.         fprintf(stderr, "\t%s\n", btp->baud_string);
  410.         btp++;
  411.     }
  412.     exit(1);
  413. }
  414.  
  415. static void
  416. do_c_key(code, shift)
  417. int    code, shift;
  418. {
  419.     register char    out_code;
  420.     static char    buffer[2] = "\036";
  421.  
  422.  
  423.     if (shift) {
  424.         out_code = code - 04;
  425.     } else {
  426.         out_code = code;
  427.     }
  428.     buffer[1] = out_code;
  429.     ttysw_input(tty_win, buffer, 2);
  430. }
  431.