home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 500-599 / ff562.lza / Fkeys / fkeys.c next >
C/C++ Source or Header  |  1991-11-20  |  13KB  |  490 lines

  1.         /********************************
  2.          *   Function Keys 1.1  03/89   *
  3.          *     by Torsten Jürgeleit     *
  4.          *    for Manx Aztec C v3.6a    *
  5.          *      cc fkeys                *
  6.          *      ln fkeys detach -lc     *
  7.          ********************************/
  8.  
  9.     /* Includes */
  10.  
  11. #include <exec/exec.h>
  12. #include <devices/input.h>
  13. #include <libraries/dos.h>
  14. #include <intuition/screens.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <functions.h>
  18.  
  19.     /* Defines */
  20.  
  21. #define WINDOW_WIDTH    280
  22. #define WINDOW_HEIGHT    180
  23. #define GADGET_REMOVE    0
  24. #define GADGET_CONTINUE    1
  25.  
  26. #define NEWCLI        "NewCLI >NIL: <NIL:"    /* "NewWSH >NIL: <NIL:" */
  27. #define SIGB_ACTION    ((ULONG)((struct MsgPort *)main_task)->mp_SigBit)
  28. #define SIGF_ACTION    (1L << SIGB_ACTION)
  29.  
  30. #define ACTION_NOTHING            0
  31. #define ACTION_NEWCLI            1
  32. #define ACTION_REMOVE_REQUESTER        2
  33.  
  34.     /* Declarations for DETACH (segment list splitting) */
  35.  
  36. LONG _stack        = 0x1000;
  37. LONG _priority     = 0;
  38. LONG _BackGroundIO = 0;
  39. BYTE *_procname    = "FKeys1.1";
  40.  
  41.     /* Globals */
  42.  
  43. struct GfxBase        *GfxBase;
  44. struct IntuitionBase    *IntuitionBase;
  45. struct LayersBase    *LayersBase;
  46. struct Task        *main_task;
  47. struct FileHandle    *file_handle;
  48. struct MsgPort        *input_port;
  49. struct IOStdReq        *input_req;
  50. struct Interrupt    interrupt;
  51. UBYTE action = ACTION_NOTHING;
  52.  
  53.     /* Dummy functions - not used from c.lib */
  54.  
  55. VOID _cli_parse() {}
  56. VOID _wb_parse() {}
  57.  
  58.     /* Casts */
  59.  
  60. VOID interrupt_server(), back_window_to_front(), front_window_to_back(),
  61.      activate_next_window(), activate_previous_window(),
  62.      active_window_to_front(), active_window_to_back(), quit_program(),
  63.      back_screen_to_front(), front_screen_to_back(), newcli();
  64. BOOL remove_requester();
  65. struct Window  *top_window(), *bottom_window(), *next_window(),
  66.            *previous_window();
  67.  
  68.     /* Main - installs interrupt server and performs interrupt action */
  69.  
  70.    VOID
  71. main()
  72. {
  73.    BOOL remove = FALSE;
  74.  
  75.    main_task = FindTask(NULL);
  76.    if (file_handle = Open("NIL:", MODE_NEWFILE)) {
  77.       if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L)) {
  78.      if (IntuitionBase = (struct IntuitionBase *)
  79.                      OpenLibrary("intuition.library", 0L)) {
  80.         if (FindPort(_procname)) {
  81.            DisplayBeep(NULL);
  82.         } else {
  83.            if (LayersBase = (struct LayersBase *)
  84.                     OpenLibrary("layers.library", 0L)) {
  85.           if (input_port = CreatePort(_procname, 0L)) {
  86.              if (input_req = CreateStdIO(input_port)) {
  87.             if (! OpenDevice("input.device", 0L, input_req,
  88.                                       0L)) {
  89.                interrupt.is_Code         = (VOID (*)())
  90.                               &interrupt_server;
  91.                interrupt.is_Data         = NULL;
  92.                interrupt.is_Node.ln_Name = _procname;
  93.                interrupt.is_Node.ln_Pri  = 51;   /* above of Intuition */
  94.                input_req->io_Command     = IND_ADDHANDLER;
  95.                input_req->io_Data        = (APTR)&interrupt;
  96.                if (! DoIO(input_req)) {
  97.                   do {
  98.                  Wait(SIGF_ACTION);   /* wait for action signal */
  99.                  switch (action) {
  100.                     case ACTION_NEWCLI :
  101.                        if (WBenchToFront() == FALSE) {
  102.                       DisplayBeep(NULL);
  103.                        } else {
  104.                       Execute(NEWCLI, file_handle,
  105.                                    file_handle);
  106.                        }
  107.                        break;
  108.                     case ACTION_REMOVE_REQUESTER :
  109.                        remove = remove_requester();
  110.                        break;
  111.                  }
  112.                   } while (remove != TRUE);
  113.                   input_req->io_Command = IND_REMHANDLER;
  114.                   DoIO(input_req);
  115.                   DisplayBeep(NULL);
  116.                }
  117.                CloseDevice(input_req);
  118.             }
  119.             DeleteStdIO(input_req);
  120.              }
  121.              DeletePort(input_port);
  122.           }
  123.           CloseLibrary(LayersBase);
  124.            }
  125.         }
  126.         CloseLibrary(IntuitionBase);
  127.      }
  128.      CloseLibrary(GfxBase);
  129.       }
  130.       Close(file_handle);
  131.    }
  132. }
  133.     /* Interrupt server to parse event list for function key events */
  134.  
  135.    VOID
  136. interrupt_server()
  137. {
  138. #asm
  139. IECLASS_NULL            EQU    $00
  140. IECLASS_RAWKEY            EQU    $01
  141. IECODEB_KEYUP            EQU    7
  142. IECODE_F1KEY            EQU    $50
  143. IEQUALIFIERB_REPEAT        EQU    9
  144. IEQUALIFIER_CAPSLOCK        EQU    $0004
  145. IEQUALIFIER_LCOMMAND        EQU    $0040
  146. IEQUALIFIER_REPEAT        EQU    $0200
  147. IEQUALIFIER_RELATIVEMOUSE    EQU    $8000
  148. ie_NextEvent            EQU    $00    ; APTR
  149. ie_Class            EQU    $04    ; UBYTE
  150. ie_Code                EQU    $06    ; UWORD
  151. ie_Qualifier            EQU    $08    ; UWORD
  152.  
  153.     XREF    _geta4#
  154.     XREF    _function_table
  155.  
  156.     ; (called with :  a0 = event list ptr  ,  a1 = interrupt data ptr)
  157.     movem.l    a0-a6/d0-d7,-(sp)
  158.     jsr    _geta4#        ; Aztec C need it for small code/data model
  159.     bra.s    check_next_event
  160.  
  161. event_loop:
  162.     cmp.b    #IECLASS_RAWKEY,ie_Class(a0)    ; check ie_Class
  163.     bne.s    next_event
  164.     move.w    ie_Qualifier(a0),d0        ; check ie_Qualifier
  165.     move.w    d0,d1                ; save ie_Qualifier
  166.     and.w    #~(IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_CAPSLOCK|IEQUALIFIER_REPEAT),d0
  167.     cmp.w    #IEQUALIFIER_LCOMMAND,d0    ; left Amiga key pressed ?
  168.     bne.s    next_event
  169.     move.w    ie_Code(a0),d0            ; check ie_Code
  170.     btst    #IECODEB_KEYUP,d0        ; only key down events
  171.     bne.s    next_event
  172.     sub.w    #IECODE_F1KEY,d0        ; function key pressed ?
  173.     cmp.w    #10,d0
  174.     bcc.s    next_event
  175.     btst    #IEQUALIFIERB_REPEAT,d1        ; ignore auto repeat
  176.     bne.s    clear_event
  177.  
  178. fkey_pressed:
  179.     lsl.w    #2,d0
  180.     lea    _function_table,a1
  181.     move.l    (a1,d0.w),a1            ; a1 := function ptr
  182.     move.l    a0,-(sp)            ; save event ptr
  183.     jsr    (a1)                ; call function
  184.     move.l    (sp)+,a0            ; restore event ptr
  185.  
  186. clear_event:
  187.     move.l    #IECLASS_NULL,ie_Class(a0)    ; clear event
  188.     
  189. next_event:
  190.     move.l    ie_NextEvent(a0),a0        ; get next event in list
  191.  
  192. check_next_event:
  193.     move.l    a0,d0                ; next event ptr == NULL ?
  194.     bne.s    event_loop
  195.  
  196.     movem.l    (sp)+,a0-a6/d0-d7
  197.     move.l    a0,d0                ; return event list ptr
  198. #endasm
  199. }
  200.     /* Remove and about requester */
  201.  
  202. struct TextAttr  topaz80 = { (STRPTR)"topaz.font", TOPAZ_EIGHTY, 0, 0 };
  203. struct TextAttr  topaz60 = { (STRPTR)"topaz.font", TOPAZ_SIXTY, 0, 0 };
  204.  
  205. SHORT border_vec[] = {0, 0, 81, 0, 81, 21, 0, 21, 0, 0 };
  206. struct Border  border = { -1, -1, 0, 0, JAM1, 5, &border_vec[0], NULL };
  207.  
  208. struct IntuiText  remove_text = { 3, 0, JAM1, 16, 6, &topaz80, (UBYTE *)
  209.    "Remove", NULL };
  210. struct Gadget remove_gadget = { NULL, 20, WINDOW_HEIGHT - 30, 80, 20,
  211.    GADGHCOMP, RELVERIFY, BOOLGADGET, (APTR)&border, NULL, &remove_text, 0,
  212.    NULL, GADGET_REMOVE, NULL};
  213. struct IntuiText  continue_text = { 3, 0, JAM1, 8, 6, &topaz80, (UBYTE *)
  214.    "Continue", NULL };
  215. struct Gadget continue_gadget = { &remove_gadget, WINDOW_WIDTH - 100,
  216.    WINDOW_HEIGHT - 30, 80, 20, GADGHCOMP, RELVERIFY, BOOLGADGET, (APTR)
  217.    &border, NULL, &continue_text, 0, NULL, GADGET_CONTINUE, NULL};
  218.  
  219. struct IntuiText  usage_text10 = { 2, 0, JAM1, 20, 135, &topaz80, (UBYTE *)
  220.    "F10 : show this window", NULL };
  221. struct IntuiText  usage_text9 = { 2, 0, JAM1, 20, 125, &topaz80, (UBYTE *)
  222.    "F9  : open NewCLI window"   /* "F9  : open NewWSH window"*/,
  223.    &usage_text10 };
  224. struct IntuiText  usage_text8 = { 2, 0, JAM1, 20, 115, &topaz80, (UBYTE *)
  225.    "F8  : front screen to back", &usage_text9 };
  226. struct IntuiText  usage_text7 = { 2, 0, JAM1, 20, 105, &topaz80, (UBYTE *)
  227.    "F7  : back screen to front", &usage_text8 };
  228. struct IntuiText  usage_text6 = { 2, 0, JAM1, 20, 95, &topaz80, (UBYTE *)
  229.    "F6  : active window to back", &usage_text7 };
  230. struct IntuiText  usage_text5 = { 2, 0, JAM1, 20, 85, &topaz80, (UBYTE *)
  231.    "F5  : active window to front", &usage_text6 };
  232. struct IntuiText  usage_text4 = { 2, 0, JAM1, 20, 75, &topaz80, (UBYTE *)
  233.    "F4  : activate previous window", &usage_text5 };
  234. struct IntuiText  usage_text3 = { 2, 0, JAM1, 20, 65, &topaz80, (UBYTE *)
  235.    "F3  : activate next window", &usage_text4 };
  236. struct IntuiText  usage_text2 = { 2, 0, JAM1, 20, 55, &topaz80, (UBYTE *)
  237.    "F2  : front window to back", &usage_text3 };
  238. struct IntuiText  usage_text1 = { 2, 0, JAM1, 20, 45, &topaz80, (UBYTE *)
  239.    "F1  : back window to front", &usage_text2 };
  240. struct IntuiText  title_text2 = { 3, 0, JAM1, WINDOW_WIDTH / 2 - 20 * 4, 30,
  241.    &topaz80, (UBYTE *)"by Torsten Jürgeleit", &usage_text1 };
  242. struct IntuiText  title_text1 = { 0, 0, JAM1, WINDOW_WIDTH / 2 - 17 * 5, 18,
  243.    &topaz60, (UBYTE *)"FKeys v1.1  03/89", &title_text2 };
  244.  
  245. struct NewWindow  new_window = { 320 - WINDOW_WIDTH / 2, 100 - WINDOW_HEIGHT
  246.    / 2, WINDOW_WIDTH, WINDOW_HEIGHT, 2, 3, GADGETUP, WINDOWDEPTH |
  247.    WINDOWDRAG | ACTIVATE | SMART_REFRESH | RMBTRAP, NULL, NULL, (UBYTE *)
  248.    " FKeys ", NULL, NULL, 0, 0, 0, 0, WBENCHSCREEN };
  249.  
  250. /******/
  251.  
  252.    BOOL
  253. remove_requester()
  254. {
  255.    struct Window    *window;
  256.    struct IntuiMessage    *msg;
  257.    struct Gadget    *gad;
  258.    LONG  class;
  259.    BOOL  remove = FALSE, exit = FALSE;
  260.  
  261.    if ((window = OpenWindow(&new_window)) == NULL) {
  262.       DisplayBeep(NULL);
  263.       remove = TRUE;
  264.    } else {
  265.       SetAPen(window->RPort, 1L);
  266.       RectFill(window->RPort, 5L, 12L, WINDOW_WIDTH - 7L, WINDOW_HEIGHT -
  267.                                     4L);
  268.       PrintIText(window->RPort, &title_text1, 0L, 0L);
  269.       AddGList(window, &continue_gadget, -1L, -1L, NULL);
  270.       RefreshGadgets(&continue_gadget, window, NULL);
  271.       do {
  272.      WaitPort(window->UserPort);
  273.      while (msg = (struct IntuiMessage *)GetMsg(window->UserPort)) {
  274.         class = msg->Class;
  275.         gad   = (struct Gadget *)msg->IAddress;
  276.         ReplyMsg(msg);
  277.         if (class == GADGETUP) {
  278.            if (gad->GadgetID == GADGET_REMOVE) {
  279.           remove = TRUE;
  280.            }
  281.            exit = TRUE;
  282.         }
  283.          }
  284.       } while (exit == FALSE);
  285.       CloseWindow(window);
  286.    }
  287.    return(remove);
  288. }
  289.     /* Function table and functions for f1 to f10 */
  290.  
  291. VOID (*function_table[])() = {
  292.    &back_window_to_front,    /* f1  */
  293.    &front_window_to_back,    /* f2  */
  294.    &activate_next_window,    /* f3  */
  295.    &activate_previous_window,    /* f4  */
  296.    &active_window_to_front,    /* f5  */
  297.    &active_window_to_back,    /* f6  */
  298.    &back_screen_to_front,    /* f7  */
  299.    &front_screen_to_back,    /* f8  */
  300.    &newcli,            /* f9  */
  301.    &quit_program        /* f10 */
  302. };
  303.  
  304.    VOID
  305. back_window_to_front()        /* f1 */
  306. {
  307.    struct Window  *window = bottom_window(IntuitionBase->ActiveScreen);
  308.  
  309.    if (window) {
  310.       while (window && (window->Flags & BACKDROP)) {
  311.      window = previous_window(window);   /* skip backdrop windows */
  312.       }
  313.       if (window) {
  314.      WindowToFront(window);
  315.      ActivateWindow(window);
  316.       }
  317.    }
  318. }
  319.  
  320.    VOID
  321. front_window_to_back()        /* f2 */
  322. {
  323.    struct Window  *window1 = top_window(IntuitionBase->ActiveScreen);
  324.    struct Window  *window2 = next_window(window1);
  325.  
  326.    if (window1) {
  327.       WindowToBack(window1);
  328.       if (window2) {
  329.      ActivateWindow(window2);
  330.       }
  331.    }
  332. }
  333.  
  334.    VOID
  335. activate_next_window()        /* f3 */
  336. {
  337.    struct Window  *window = next_window(IntuitionBase->ActiveWindow);
  338.  
  339.    if (window) {
  340.       ActivateWindow(window);
  341.    }
  342. }
  343.  
  344.    VOID
  345. activate_previous_window()    /* f4 */
  346. {
  347.    struct Window  *window = previous_window(IntuitionBase->ActiveWindow);
  348.  
  349.    if (window == NULL) {
  350.       window = bottom_window(IntuitionBase->ActiveScreen);
  351.    }
  352.    if (window) {
  353.       ActivateWindow(window);
  354.    }
  355. }
  356.  
  357.    VOID
  358. active_window_to_front()    /* f5 */
  359. {
  360.    struct Window  *window = IntuitionBase->ActiveWindow;
  361.  
  362.    if (window) {
  363.       WindowToFront(window);
  364.    }
  365. }
  366.  
  367.    VOID
  368. active_window_to_back()        /* f6 */
  369. {
  370.    struct Window  *window = IntuitionBase->ActiveWindow;
  371.  
  372.    if (window) {
  373.       WindowToBack(window);
  374.    }
  375. }
  376.  
  377.    VOID
  378. back_screen_to_front()        /* f7 */
  379. {
  380.    struct Screen  *screen = IntuitionBase->FirstScreen;
  381.    struct Window  *window;
  382.  
  383.    if (screen) {
  384.       while (screen->NextScreen) {
  385.      screen = screen->NextScreen;
  386.       }
  387.       ScreenToFront(screen);
  388.       if (window = top_window(screen)) {
  389.      ActivateWindow(window);
  390.       }
  391.    }
  392. }
  393.  
  394.    VOID
  395. front_screen_to_back()        /* f8 */
  396. {
  397.    struct Screen  *screen = IntuitionBase->FirstScreen;
  398.    struct Window  *window;
  399.  
  400.    if (screen) {
  401.       ScreenToBack(screen);
  402.       if (screen = IntuitionBase->FirstScreen) {
  403.      if (window = top_window(screen)) {
  404.         ActivateWindow(window);
  405.      }
  406.       }
  407.    }
  408. }
  409.  
  410.    VOID
  411. newcli()            /* f9 */
  412. {
  413.    action = ACTION_NEWCLI;
  414.    Signal(main_task, SIGB_ACTION);   /* send action signal to main task */
  415. }
  416.  
  417.    VOID
  418. quit_program()            /* f10 */
  419. {
  420.    action = ACTION_REMOVE_REQUESTER;
  421.    Signal(main_task, SIGB_ACTION);   /* send action signal to main task */
  422. }
  423.     /* Support routines for functions */
  424.  
  425.    struct Window  *
  426. top_window(screen)
  427.    struct Screen  *screen;
  428. {
  429.    struct Window  *window = NULL;
  430.    struct Layer   *layer  = screen->LayerInfo.top_layer;
  431.  
  432.    while (layer && layer->Window == NULL) {
  433.       layer = layer->back;
  434.    }
  435.    if (layer) {
  436.       window = (struct Window *)layer->Window;
  437.    }
  438.    return(window);
  439. }
  440.  
  441.    struct Window  *
  442. bottom_window(screen)
  443.    struct Screen  *screen;
  444. {
  445.    struct Window  *window = NULL;
  446.    struct Layer   *layer  = screen->LayerInfo.top_layer;
  447.  
  448.    while (layer) {
  449.       if (layer->Window) {
  450.          window = (struct Window *)layer->Window;
  451.       }
  452.       layer = layer->back;
  453.    }
  454.    return(window);
  455. }
  456.  
  457.    struct Window  *
  458. next_window(window)
  459.    struct Window  *window;
  460. {
  461.    struct Layer  *layer = window->WLayer;
  462.  
  463.    do {
  464.       layer = layer->back;
  465.    } while (layer && layer->Window == NULL);
  466.    if (layer) {
  467.       window = (struct Window *)layer->Window;
  468.    } else {
  469.       window = top_window(window->WScreen);
  470.    }
  471.    return(window);
  472. }
  473.  
  474.    struct Window  *
  475. previous_window(window)
  476.    struct Window  *window;
  477. {
  478.    struct Layer  *layer = window->WLayer;
  479.  
  480.    do {
  481.       layer = layer->front;
  482.    } while (layer && layer->Window == NULL || layer->Window == window);
  483.    if (layer) {
  484.       window = (struct Window *)layer->Window;
  485.    } else {
  486.       window = NULL;
  487.    }
  488.    return(window);
  489. }
  490.