home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $VER: ModuleFuncs.c 39.0 (15.03.02)
- **
- ** Functions for showwindows.module
- **
- ** by Darius Brewka, based on Sample Library code "CLib37x" by
- ** (C) Copyright 1996-97 Andreas R. Kleinert
- ** All Rights Reserved.
- */
-
- #define __USE_SYSBASE // perhaps only recognized by SAS/C
-
- #include <exec/types.h>
- #include <exec/memory.h>
-
- #ifdef __MAXON__
- #include <clib/exec_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/dos_protos.h>
- #else
- #include <proto/exec.h>
- #include <proto/intuition.h>
- #include <proto/dos.h>
- #include <intuition/intuition.h>
- #endif
-
- #include <graphics/layers.h>
-
- #include "string.h"
-
- #include "compiler.h"
-
-
- /* some demo module specific stuff
- */
- struct Info {
- struct Info *next;
- char *Text;
- char *Icon;
- char *Tool;
- ULONG userdata;
- };
-
- struct Data {
- struct Info *first;
- struct Info *act;
- struct Screen *scr;
- BOOL nodepth;
- BOOL noactivate;
- BOOL noreposition;
- };
-
- const char ModuleDescription[] =
-
- "Displays all Workbench Windows, also you can do some operations on them:\n\
- moving the mouse on a specific Window item brings this Window to the (front)\n\
- clicking on an item, activates the specific Window and brings it to front\n\n\
- Possible Parameters NODEPTH/S,NOACTIVATE/S,NOREPOSITION/S:\n\
- -NODEPTH disables the Window to front mechanism.\n\
- -NOREPOSITION disables the Window to Front on Hit by mouse mechanism\n\
- -NOACTIVATE disables the Window activation mechanism.";
-
- const char InfoString[] = "Window Selector";
- const char WBName[] = "Workbench";
-
-
-
- /* Data for ReadArgs function in the dos.library */
-
- #define NUMARGS 3
- #define ARG_NODEPTH 0
- #define ARG_NOACTIVATE 1
- #define ARG_NOREPOSITION 2
-
- char RDArgsTemplate[] = "NODEPTH/S,NOACTIVATE/S,NOREPOSITION/S";
- ULONG args_data[NUMARGS];
-
-
-
-
-
-
- /* only helpers */
-
- /* allocString
- ** creates a copy of a given c style string in the passed memory pool
- ** and returns a pointer to it or NULL.
- **
- ** example char *name = allocString("System:", pool);
- **
- ** Note: you should free the string after you not need it amymore
- */
-
- char *allocString(char *str, APTR pool) {
- char *b;
- if (str == NULL) return NULL;
- if (strlen(str) == 0) return NULL;
- b = (char*) AllocPooled(pool, strlen(str)+1);
- if (b) {
- strncpy (b,str,strlen(str));
- }
- return b;
- }
-
- void freeString(char *str, APTR pool) {
- if (str) FreePooled(pool, str, strlen(str)+1);
- }
-
- /* isWindowValid
- ** you can not manipulate windows outside a programm it not belongs to,
- ** this Function should only be called between Forbid/Permit, or other
- ** intuition blocking functions, it returns TRUE, if the passed window is still alive.
- **
- ** this is useful, coz the showwindowsmodule could manipulate windows which are closed after
- ** the windows layer apear and operations would work on invalid pointers.
- */
-
- BOOL isWindowValid(struct Window *w) {
- struct Window *win;
- struct Screen *scr;
- BOOL back = FALSE;
-
- scr=LockPubScreen((UBYTE*) WBName);
- if (scr) {
- win = scr->FirstWindow;
- while (win && !back) {
- if (win == w) back = TRUE;
- win = win->NextWindow;
- }
- UnlockPubScreen(NULL, scr);
- }
- return back;
- }
-
- STRPTR SAVEDS ASM GetModuleInfo() {
- return (STRPTR) &InfoString;
- }
-
- STRPTR SAVEDS ASM GetModuleDescription() {
- return (STRPTR) &ModuleDescription;
- }
-
- APTR SAVEDS ASM *InitModule(REG(a0) struct RDArgs *rdArgs, REG(a1) APTR pool) {
-
- struct Data *back;
- struct Info *info, *pred;
- struct Window *win;
- int i;
-
- /* clear args don't clear this line if you wish to use args
- ** note you could stil set default values here instead of clearing them */
-
- for (i = 0; i < NUMARGS; i++) args_data[i] = 0;
-
- /* parse the arguments by a Template
- ** not need to check here if function succeeded, coz showwindows doesn't do any
- ** important things and can use default values */
-
- ReadArgs(RDArgsTemplate, args_data, rdArgs);
-
-
- /* to be sure to get a valid windows list, this example module reads the windows list inside
- a forbid()/permit() call and copies the names of them in a allocated single-linked buffer. */
-
- back = AllocPooled(pool, sizeof(struct Data)); /* allocate space for a data buffer */
- if (back) { /* a pointer to this buffer will be returned to AmiStart */
-
- /* set the arguments in the allocated data buffer structure */
- if (args_data[ARG_NODEPTH] !=0) back->nodepth = TRUE; else back->nodepth = FALSE;
- if (args_data[ARG_NOACTIVATE] !=0) back->noactivate = TRUE; else back->noactivate = FALSE;
- if (args_data[ARG_NOREPOSITION] !=0) back->noreposition = TRUE; else back->noreposition = FALSE;
-
- back->scr=LockPubScreen((UBYTE*) WBName);
- if (back->scr) {
- Forbid(); /* switch of multitask */
- win = back->scr->FirstWindow; /* get pointer to the first window */
- while (win) { /* any more windows? */
- if (win->Title) {
- info = AllocPooled(pool, sizeof(struct Info)); /* if so allocate a single linked entry */
- if (info) {
- info->Text = allocString(win->Title, pool); /* add a copy of the windowname to the entry */
- if (info->Text) { /* if name could be duplicated */
- info->Icon = allocString("modules/showwindows", pool); /* add the iconname to the linked entry */
- if (back->first) { /* is already an item linked? */
- pred->next = info; /* if so link new one behind the last one */
- pred = info;
- info->userdata = (ULONG) win; /* set user data, here userdata is the windowpointer itself */
- } else {
- info->userdata = (ULONG) win; /* if this is the first entry, set its pointer to the buffer firstentry */
- back->first = info;
- pred = info;
- }
- } else FreePooled(pool, info, sizeof(struct Info)); /* free last entry if title could not be allocated */
- }
- }
- win = win->NextWindow; /* get next window pointer and loop */
- }
- back->act = NULL; /* act is a pointer to the actual entry, which can be accessed
- ** by data getting functions, here it is NULL, to make NextModuleData
- ** working correct */
-
- Permit(); /* enable multitask */
-
- UnlockPubScreen(NULL, back->scr);
-
- } else {
- FreePooled(pool, back, sizeof(struct Data)); /* free data buffer if an error occurs */
- back = NULL;
- }
- }
-
- return (APTR*) back;
-
- }
-
- BOOL SAVEDS ASM NextModuleData(REG(a0) APTR dat) {
- struct Data *data = (struct Data *) dat;
- BOOL back;
-
- back = FALSE;
-
- if (data->act) { /* is actual pointer set?, if so set act to act->next and return true id act != NULL */
- data->act = data->act->next;
- if (data->act) back = TRUE;
- } else {
- data->act = data->first; /* here if act == NULL, NextModuleData sets act to act->first and returns true if this is not NULL */
- if (data->act) back = TRUE; /* act will be set by InitModule() to NULL to make sure NextModuleData returns the first entry by first
- ** calling NextModuleData */
- }
- return back;
- }
-
- void SAVEDS ASM DisposeModule(REG(a0) APTR dat, REG(a1) APTR pool) {
-
- struct Info *info, *next;
- struct Data *data = (struct Data *) dat;
- if (data == NULL) return;
- info = data->first;
- while(info) { /* frees all memory allocated in InitModule() and stored in data returned by InitModule() */
- next = info->next;
- freeString(info->Text, pool);
- freeString(info->Icon, pool);
- freeString(info->Tool, pool);
- FreePooled(pool, info, sizeof(struct Info));
- info = next;
- }
- FreePooled(pool, data, sizeof(struct Data));
-
- }
-
- STRPTR SAVEDS ASM GetModuleName(REG(a0) APTR data) {
- return ((struct Data *) data)->act->Text;
- }
-
- STRPTR SAVEDS ASM GetModuleIcon(REG(a0) APTR data) {
- return ((struct Data *) data)->act->Icon;
- }
-
- STRPTR SAVEDS ASM GetModuleTool(REG(a0) APTR data) {
- return ((struct Data *) data)->act->Tool;
- }
-
- ULONG SAVEDS ASM GetModuleUserData(REG(a0) APTR data) {
- return ((struct Data *) data)->act->userdata;
- }
-
- APTR SAVEDS ASM OpenModule() {
- /* no global installation needed here!, return something to make sure AmiStart uses this module */
- return (APTR) 2;
- }
-
- void SAVEDS ASM CloseModule(REG(a0) APTR init) {
- }
-
- void SAVEDS ASM HitModule(REG(a0) APTR dat, REG(d0) ULONG userdata) {
- struct Data *data = (struct Data *) dat;
- struct Window *win;
- struct Screen *scr;
- struct Layer *l;
- struct Layer_Info *li;
-
- if (!data->noreposition) {
- Forbid();
- win = NULL;
- l = NULL;
- scr=LockPubScreen((UBYTE*) WBName);
- if (scr) {
-
- li = &scr->LayerInfo;
- if (li) l = li->top_layer;
- if (l) l = l->back;
- if (l) win = (struct Window *) l->Window;
- UnlockPubScreen(NULL, scr);
- }
- if (win) {
- if (isWindowValid((struct Window *) userdata)) {
- if (win != ((struct Window *) userdata)) MoveWindowInFrontOf((struct Window *) userdata, win);
- }
- }
-
- Permit();
- }
- }
-
- ULONG SAVEDS ASM DoModuleCommand(REG(a0) APTR dat, REG(d0) ULONG userdata) {
- struct Data *data = (struct Data *) dat;
-
- if (!data->nodepth) {
- Forbid();
- if (isWindowValid((struct Window *) userdata)) WindowToFront((struct Window *) userdata);
- Permit();
- }
- if(!data->noactivate) {
- Forbid();
- if (isWindowValid((struct Window *) userdata)) ActivateWindow((struct Window *) userdata);
- Permit();
- }
- return 1;
- }
-
- ULONG SAVEDS ASM GetModuleFlags(REG(a0) APTR dat) {
- return 0;
- }
-
- APTR SAVEDS ASM GetSpecialInfo(REG(a0) APTR dat) {
- return NULL;
- }
-
- STRPTR SAVEDS ASM GetModuleConfig(REG(a0) APTR dat) {
- return NULL;
- }
-