home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / AmigaSystem / AmiStart / ModulesSDK.lha / source / ModuleFuncs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-25  |  9.2 KB  |  332 lines

  1. /*
  2. **      $VER: ModuleFuncs.c 39.0 (15.03.02)
  3. **
  4. **      Functions for showwindows.module
  5. **
  6. **        by Darius Brewka, based on Sample Library code "CLib37x" by
  7. **          (C) Copyright 1996-97 Andreas R. Kleinert
  8. **          All Rights Reserved.
  9. */
  10.  
  11. #define __USE_SYSBASE        // perhaps only recognized by SAS/C
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15.  
  16. #ifdef __MAXON__
  17. #include <clib/exec_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/dos_protos.h>
  20. #else
  21. #include <proto/exec.h>
  22. #include <proto/intuition.h>
  23. #include <proto/dos.h>
  24. #include <intuition/intuition.h>
  25. #endif
  26.  
  27. #include <graphics/layers.h>
  28.  
  29. #include "string.h"
  30.  
  31. #include "compiler.h"
  32.  
  33.  
  34. /* some demo module specific stuff
  35. */
  36.     struct    Info {
  37.         struct    Info    *next;
  38.                 char    *Text;
  39.                 char    *Icon;
  40.                 char    *Tool;
  41.                 ULONG    userdata;
  42.     };
  43.  
  44.     struct    Data {
  45.         struct    Info    *first;
  46.         struct    Info    *act;
  47.         struct    Screen    *scr;
  48.                 BOOL    nodepth;
  49.                 BOOL    noactivate;
  50.                 BOOL    noreposition;
  51.     };
  52.  
  53.     const char    ModuleDescription[] =
  54.  
  55. "Displays all Workbench Windows, also you can do some operations on them:\n\
  56. moving the mouse on a specific Window item brings this Window to the (front)\n\
  57. clicking on an item, activates the specific Window and brings it to front\n\n\
  58. Possible Parameters NODEPTH/S,NOACTIVATE/S,NOREPOSITION/S:\n\
  59. -NODEPTH disables the Window to front mechanism.\n\
  60. -NOREPOSITION disables the Window to Front on Hit by mouse mechanism\n\
  61. -NOACTIVATE disables the Window activation mechanism.";
  62.  
  63.     const char    InfoString[] = "Window Selector";
  64.     const char    WBName[] = "Workbench";
  65.  
  66.  
  67.  
  68.     /*    Data for ReadArgs function in the dos.library */
  69.  
  70. #define NUMARGS    3
  71. #define    ARG_NODEPTH 0
  72. #define ARG_NOACTIVATE 1
  73. #define ARG_NOREPOSITION 2
  74.  
  75.     char    RDArgsTemplate[] = "NODEPTH/S,NOACTIVATE/S,NOREPOSITION/S";
  76.     ULONG    args_data[NUMARGS];
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /* only helpers */
  84.  
  85. /* allocString
  86. ** creates a copy of a given c style string in the passed memory pool
  87. ** and returns a pointer to it or NULL.
  88. **
  89. ** example char *name = allocString("System:", pool);
  90. **
  91. ** Note: you should free the string after you not need it amymore
  92. */
  93.  
  94. char *allocString(char *str, APTR pool) {
  95.     char *b;
  96.     if (str == NULL) return NULL;
  97.     if (strlen(str) == 0) return NULL;
  98.     b = (char*) AllocPooled(pool, strlen(str)+1);
  99.     if (b) {
  100.         strncpy (b,str,strlen(str));
  101.     }
  102.     return b;
  103. }
  104.  
  105. void freeString(char *str, APTR pool) {
  106.     if (str) FreePooled(pool, str, strlen(str)+1);
  107. }
  108.  
  109. /* isWindowValid
  110. ** you can not manipulate windows outside a programm it not belongs to,
  111. ** this Function should only be called between  Forbid/Permit, or other
  112. ** intuition blocking functions, it returns TRUE, if the passed window is still alive.
  113. **
  114. ** this is useful, coz the showwindowsmodule could manipulate windows which are closed after
  115. ** the windows layer apear and operations would work on invalid pointers.
  116. */
  117.  
  118. BOOL isWindowValid(struct Window *w) {
  119.     struct    Window    *win;
  120.     struct    Screen    *scr;
  121.             BOOL    back = FALSE;
  122.  
  123.     scr=LockPubScreen((UBYTE*) WBName);
  124.     if (scr) {
  125.         win = scr->FirstWindow;
  126.         while (win && !back) {
  127.             if (win == w) back = TRUE;
  128.             win = win->NextWindow;
  129.         }
  130.         UnlockPubScreen(NULL, scr);
  131.     }
  132.     return back;
  133. }
  134.  
  135. STRPTR SAVEDS ASM GetModuleInfo() {
  136.     return (STRPTR) &InfoString;
  137. }
  138.  
  139. STRPTR SAVEDS ASM GetModuleDescription() {
  140.     return (STRPTR) &ModuleDescription;
  141. }
  142.  
  143. APTR SAVEDS ASM *InitModule(REG(a0) struct RDArgs *rdArgs, REG(a1) APTR pool) {
  144.  
  145.     struct    Data    *back;
  146.     struct    Info    *info, *pred;
  147.     struct    Window    *win;
  148.     int        i;
  149.  
  150.     /* clear args don't clear this line if you wish to use args
  151.     ** note you could stil set default values here instead of clearing them */
  152.  
  153.     for (i = 0; i < NUMARGS; i++) args_data[i] = 0;
  154.  
  155.     /* parse the arguments by a Template
  156.     ** not need to check here if function succeeded, coz showwindows doesn't do any
  157.     ** important things and can use default values */
  158.  
  159.     ReadArgs(RDArgsTemplate, args_data, rdArgs);
  160.  
  161.  
  162.     /* to be sure to get a valid windows list, this example module reads the windows list inside
  163.     a forbid()/permit() call and copies the names of them in a allocated single-linked buffer. */
  164.  
  165.     back = AllocPooled(pool, sizeof(struct Data));    /* allocate space for a data buffer */
  166.     if (back) {                                        /* a pointer to this buffer will be returned to AmiStart */
  167.  
  168.         /* set the arguments in the allocated data buffer structure */
  169.         if (args_data[ARG_NODEPTH] !=0) back->nodepth = TRUE; else back->nodepth = FALSE;
  170.         if (args_data[ARG_NOACTIVATE] !=0) back->noactivate = TRUE; else back->noactivate = FALSE;
  171.         if (args_data[ARG_NOREPOSITION] !=0) back->noreposition = TRUE; else back->noreposition = FALSE;
  172.  
  173.         back->scr=LockPubScreen((UBYTE*) WBName);
  174.         if (back->scr) {
  175.             Forbid();                                /* switch of multitask */
  176.             win = back->scr->FirstWindow;            /* get pointer to the first window */
  177.             while (win) {                            /* any more windows? */
  178.                 if (win->Title) {
  179.                     info = AllocPooled(pool, sizeof(struct Info));    /* if so allocate a single linked entry */
  180.                     if (info) {
  181.                         info->Text = allocString(win->Title, pool);    /* add a copy of the windowname to the entry */
  182.                         if (info->Text) {                            /* if name could be duplicated */
  183.                             info->Icon = allocString("modules/showwindows", pool);    /* add the iconname to the linked entry */
  184.                             if (back->first) {                                        /* is already an item linked? */
  185.                                 pred->next = info;                                    /* if so link new one behind the last one */
  186.                                 pred = info;
  187.                                 info->userdata = (ULONG) win;                        /* set user data, here userdata is the windowpointer itself */
  188.                             } else {
  189.                                 info->userdata = (ULONG) win;                        /* if this is the first entry, set its pointer to the buffer firstentry */
  190.                                 back->first = info;
  191.                                 pred = info;
  192.                             }
  193.                         } else FreePooled(pool, info, sizeof(struct Info));            /* free last entry if title could not be allocated */
  194.                     }
  195.                 }
  196.                 win = win->NextWindow;                                                /* get next window pointer and loop */
  197.             }
  198.             back->act = NULL;                                                        /* act is a pointer to the actual entry, which can be accessed
  199.                                                                                     ** by data getting functions, here it is NULL, to make NextModuleData
  200.                                                                                     ** working correct */
  201.  
  202.             Permit();                                                                /* enable multitask */
  203.  
  204.             UnlockPubScreen(NULL, back->scr);
  205.  
  206.         } else {
  207.             FreePooled(pool, back, sizeof(struct Data));                            /* free data buffer if an error occurs */
  208.             back = NULL;
  209.         }
  210.     }
  211.  
  212.     return (APTR*) back;
  213.  
  214. }
  215.  
  216. BOOL SAVEDS ASM NextModuleData(REG(a0) APTR dat) {
  217.     struct    Data    *data = (struct Data *) dat;
  218.             BOOL    back;
  219.  
  220.     back = FALSE;
  221.  
  222.     if (data->act) {                        /* is actual pointer set?, if so set act to act->next and return true id act != NULL */
  223.         data->act = data->act->next;
  224.         if (data->act) back = TRUE;
  225.     } else {
  226.         data->act = data->first;            /* here if act == NULL, NextModuleData sets act to act->first and returns true if this is not NULL */
  227.         if (data->act) back = TRUE;            /* act will be set by InitModule() to NULL to make sure NextModuleData returns the first entry by first
  228.                                             ** calling NextModuleData */
  229.     }
  230.     return back;
  231. }
  232.  
  233. void SAVEDS ASM DisposeModule(REG(a0) APTR dat, REG(a1) APTR pool) {
  234.  
  235.     struct    Info    *info, *next;
  236.     struct    Data    *data = (struct Data *) dat;
  237.     if (data == NULL) return;
  238.     info = data->first;
  239.     while(info) {                /* frees all memory allocated in InitModule() and stored in data returned by InitModule() */
  240.         next = info->next;
  241.         freeString(info->Text, pool);
  242.         freeString(info->Icon, pool);
  243.         freeString(info->Tool, pool);
  244.         FreePooled(pool, info, sizeof(struct Info));
  245.         info = next;
  246.     }
  247.     FreePooled(pool, data, sizeof(struct Data));
  248.  
  249. }
  250.  
  251. STRPTR SAVEDS ASM GetModuleName(REG(a0) APTR data) {
  252.     return ((struct Data *) data)->act->Text;
  253. }
  254.  
  255. STRPTR SAVEDS ASM GetModuleIcon(REG(a0) APTR data) {
  256.     return ((struct Data *) data)->act->Icon;
  257. }
  258.  
  259. STRPTR SAVEDS ASM GetModuleTool(REG(a0) APTR data) {
  260.     return ((struct Data *) data)->act->Tool;
  261. }
  262.  
  263. ULONG SAVEDS ASM GetModuleUserData(REG(a0) APTR data) {
  264.     return ((struct Data *) data)->act->userdata;
  265. }
  266.  
  267. APTR SAVEDS ASM OpenModule() {
  268.     /* no global installation needed here!, return something to make sure AmiStart uses this module */
  269.     return (APTR) 2;
  270. }
  271.  
  272. void SAVEDS ASM CloseModule(REG(a0) APTR init) {
  273. }
  274.  
  275. void SAVEDS ASM HitModule(REG(a0) APTR dat, REG(d0) ULONG userdata) {
  276.     struct     Data     *data = (struct Data *) dat;
  277.     struct    Window    *win;
  278.     struct    Screen    *scr;
  279.     struct    Layer    *l;
  280.     struct    Layer_Info    *li;
  281.  
  282.     if (!data->noreposition) {
  283.         Forbid();
  284.         win = NULL;
  285.         l = NULL;
  286.         scr=LockPubScreen((UBYTE*) WBName);
  287.         if (scr) {
  288.  
  289.             li = &scr->LayerInfo;
  290.             if (li) l = li->top_layer;
  291.             if (l) l = l->back;
  292.             if (l) win = (struct Window *) l->Window;
  293.             UnlockPubScreen(NULL, scr);
  294.         }
  295.         if (win) {
  296.             if (isWindowValid((struct Window *) userdata)) {
  297.             if (win != ((struct Window *) userdata)) MoveWindowInFrontOf((struct Window *) userdata, win);
  298.             }
  299.         }
  300.  
  301.         Permit();
  302.     }
  303. }
  304.  
  305. ULONG SAVEDS ASM DoModuleCommand(REG(a0) APTR dat, REG(d0) ULONG userdata) {
  306.     struct Data *data = (struct Data *) dat;
  307.  
  308.     if (!data->nodepth) {
  309.         Forbid();
  310.         if (isWindowValid((struct Window *) userdata)) WindowToFront((struct Window *) userdata);
  311.         Permit();
  312.     }
  313.     if(!data->noactivate) {
  314.         Forbid();
  315.         if (isWindowValid((struct Window *) userdata)) ActivateWindow((struct Window *) userdata);
  316.         Permit();
  317.     }
  318.     return 1;
  319. }
  320.  
  321. ULONG SAVEDS ASM GetModuleFlags(REG(a0) APTR dat) {
  322.     return 0;
  323. }
  324.  
  325. APTR SAVEDS ASM GetSpecialInfo(REG(a0) APTR dat) {
  326.     return NULL;
  327. }
  328.  
  329. STRPTR SAVEDS ASM GetModuleConfig(REG(a0) APTR dat) {
  330.     return NULL;
  331. }
  332.