home *** CD-ROM | disk | FTP | other *** search
- /*
- * Misc. console.device/window functions
- *
- * Feel free to use this code anywhere you please...Just keep
- * "this" information in the source.
- * Phillip Lindsay
- */
-
- #include <stdio.h>
- #include "estruct.h"
- #include "edef.h"
-
- #if AMIGA
- #undef TRUE
- #undef FALSE
- #include <exec/types.h>
- #include <exec/nodes.h>
- #include <exec/lists.h>
- #include <exec/tasks.h>
- #include <exec/ports.h>
- #include <exec/io.h>
- #include <devices/console.h>
- #include <devices/conunit.h>
- #include <libraries/dos.h>
- #include <libraries/dosextens.h>
- #include <graphics/clip.h>
- #include <graphics/view.h>
- #include <graphics/rastport.h>
- #include <graphics/layers.h>
- #include <graphics/text.h>
- #include <intuition/intuition.h>
-
- LONG OpenConsole();
- VOID cleanup();
- extern struct Screen *OpenScreen();
- extern struct Window *OpenWindow();
- extern struct MsgPort *CreatePort();
- extern struct IOStdReq *CreateStdIO();
- extern LONG OpenDevice();
- extern struct Task *FindTask();
- extern LONG DoIO();
-
- #define CONSOLENAME "console.device"
-
- #define WINFLAGS1 WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH
- #define WINFLAGS2 SMART_REFRESH | ACTIVATE | NOCAREREFRESH
- #define WINFLAGS (WINFLAGS1 | WINFLAGS2)
-
- #define SCRWINFLAGS1 BORDERLESS | BACKDROP | NOCAREREFRESH | ACTIVATE
- #define SCRWINFLAGS SCRWINFLAGS1 | SMART_REFRESH
-
- /*
- * Intuition global variables
- */
-
- struct TextAttr SafeFont =
- {
- (UBYTE *)"topaz.font",
- TOPAZ_EIGHTY,
- 0,
- FS_NORMAL
- };
-
- struct IntuitionBase *IntuitionBase=NULL; /* library bases */
- struct GfxBase *GfxBase=NULL;
- /* struct DiskfontBase *DiskfontBase=NULL; */
- struct Window *EmacsWindow=NULL; /* Our window */
- struct MsgPort *consoleport=NULL; /* I/O port */
- struct IOStdReq *consoleWriteMsg=NULL; /* I/O messages */
- struct IOStdReq *consoleReadMsg=NULL;
- struct Window *savewindow=-1L;
-
- char screenkey[] = "EmacsScreen";
-
- /*
- * Open a console device, given a read request
- * and a write request message.
- */
-
- LONG OpenConsole(writerequest,readrequest,window)
- struct IOStdReq *writerequest;
- struct IOStdReq *readrequest;
- struct Window *window;
- {
- LONG error;
- writerequest->io_Data = (APTR) window;
- writerequest->io_Length = (ULONG) sizeof(*window);
- error = OpenDevice(CONSOLENAME, 0L, writerequest, 0L);
-
- /* clone required parts of the request */
- readrequest->io_Device = writerequest->io_Device;
- readrequest->io_Unit = writerequest->io_Unit;
- return( (LONG) error);
- }
-
- /*
- * Output a single character
- * to a specified console
- */
-
- LONG ConPutChar(request,character)
- struct IOStdReq *request;
- char character;
- {
- request->io_Command = CMD_WRITE;
- request->io_Data = (APTR)&character;
- request->io_Length = (ULONG)1;
- return( (LONG) DoIO(request) );
- }
-
- /*
- * Output a NULL-terminated string of
- * characters to a console
- */
-
- LONG ConPutStr(request,string)
- struct IOStdReq *request;
- char *string;
- {
- request->io_Command = CMD_WRITE;
- request->io_Data = (APTR)string;
- request->io_Length = (LONG)-1;
- return( (LONG) DoIO(request) );
- }
-
- /*
- * Write out a string of predetermined
- * length to the console
- */
-
- LONG ConWrite(request,string,len)
- struct IOStdReq *request;
- char *string;
- LONG len;
- {
- request->io_Command = CMD_WRITE;
- request->io_Data = (APTR)string;
- request->io_Length = (ULONG)len;
- return( (LONG) DoIO(request) );
- }
-
- /*
- * read a character.
- */
-
- LONG GetChar(request,whereto)
- struct IOStdReq *request;
- char *whereto;
- {
- request->io_Command = CMD_READ;
- request->io_Data = (APTR)whereto;
- request->io_Length = (LONG)1;
- return((LONG)DoIO(request));
- }
-
- VOID CursorOff(request)
- struct IOStdReq *request;
- {
- register struct IOStdReq *req=request;
-
- req->io_Command = CMD_WRITE;
- req->io_Data = (APTR) "\X9B0 p";
- req->io_Length = -1L;
- DoIO(req);
- }
-
- VOID CursorOn(request)
- struct IOStdReq *request;
- {
- register struct IOStdReq *req=request;
-
- req->io_Command = CMD_WRITE;
- req->io_Data = (APTR) "\X9B p";
- req->io_Length = -1L;
- DoIO(req);
- }
-
- /*
- * memfill() - fill memory with supplied byte value for "size" bytes
- */
- void memfill(source,size,value)
- UBYTE *source;
- ULONG size;
- UBYTE value;
- {
- register UBYTE *msource=source,
- mvalue=value,
- *mend = &source[size];
-
- while( msource < mend ) *msource++ = mvalue;
-
- }
-
- LONG screendefaults(width,height,modes)
- SHORT *width, *height;
- UWORD *modes;
- {
- struct Screen wbscreen;
-
- if(!(GetScreenData(&wbscreen,(ULONG)sizeof(struct Screen),WBENCHSCREEN,0L)))
- return( (LONG) FALSE);
-
- *width = wbscreen.Width;
- *height = wbscreen.Height;
- *modes = wbscreen.ViewPort.Modes;
-
- return( (LONG) TRUE);
- }
-
- struct Screen *InitScreen(title,width,height,modes)
- UBYTE *title;
- SHORT width,height;
- UWORD modes;
- {
- struct NewScreen newscreen;
-
- memfill(&newscreen,(ULONG)sizeof(struct NewScreen),0L);
-
- #ifdef DEBUG
- kprintf("Opening Screen..w:%d h:%d m:%d\n",width,height,modes);
- #endif
-
- newscreen.Width = width;
- newscreen.Height = height;
- newscreen.Depth = 1;
- newscreen.BlockPen = 1;
- newscreen.ViewModes = modes;
- newscreen.Type = CUSTOMSCREEN;
- newscreen.Font = &SafeFont;
- newscreen.DefaultTitle = title;
-
- return((struct Screen *)OpenScreen(&newscreen));
- }
-
- /*
- Basically this function opens a window, but depending on what is
- passed as arguments a window will be opened in their own screen.
- winx = -1, opens a window on a workbench size/mode screen
- winx = -1, winy = -1
- opens a window on a interlace screen based on workbench screen
- */
- struct Window *InitWindow(winx,winy,wwidth,wheight,title)
- WORD winx,winy,wwidth,wheight;
- UBYTE *title;
- {
- struct NewWindow newwindow;
- SHORT width,height;
- UWORD modes;
-
- if(!(screendefaults(&width,&height,&modes)))
- return( (struct Window *) 0L);
-
- #ifdef DEBUG
- kprintf("InitWindow: x=%d,y=%d,wx=%d,wy=%d,title=%s\n",
- winx,winy,wwidth,wheight,title);
- #endif
-
- memfill(&newwindow,(ULONG)sizeof(struct NewWindow),0L);
- if(winx == -1)
- {
- if(winy == -1 && !(modes & LACE))
- {
- modes |= LACE; /* here we make a magic assumption */
- height *= 2;
- }
- if(!(newwindow.Screen = InitScreen(title,width,height,modes)))
- return( (struct Window *) 0L);
-
- ShowTitle(newwindow.Screen,(LONG) FALSE);
- newwindow.Screen->UserData = (UBYTE *) screenkey;
- winx = 0;
- winy = (SHORT) newwindow.Screen->BarHeight;
- wwidth = width;
- wheight = height - newwindow.Screen->BarHeight;
- newwindow.Type = CUSTOMSCREEN;
- newwindow.Flags = SCRWINFLAGS;
- title = NULL; /* so we don't get a title in window */
- }
-
- newwindow.LeftEdge = winx;
- newwindow.TopEdge = winy;
- newwindow.Width = (!wwidth ? width : wwidth);
- newwindow.Height = (!wheight ? height : wheight);
- newwindow.DetailPen = -1L;
- newwindow.BlockPen = -1L;
-
- if(newwindow.Type != CUSTOMSCREEN)
- {
- newwindow.Title = title;
- newwindow.Flags = WINFLAGS;
- newwindow.MinWidth = 71L;
- newwindow.MinHeight = 22L;
- newwindow.MaxWidth = -1L;
- newwindow.MaxHeight = -1L;
- newwindow.Type = WBENCHSCREEN;
- }
-
- return((struct Window *)OpenWindow(&newwindow));
-
- }
-
- /*
- this function close a window and if the window is attached to a
- screen "we opened" that screen will also be closed
- */
-
- void CloseScrWin(win)
- struct Window *win;
- {
- struct Screen *scr;
-
- scr = win->WScreen;
-
- CloseWindow(win);
-
- /* make sure this is OUR screen */
- if(((scr->Flags & SCREENTYPE) != WBENCHSCREEN) &&
- !(strcmp(scr->UserData,screenkey)))
- CloseScreen(scr);
- }
-
- /*
- This function determines the number of rows/cols available on a given
- window. If the window type becomes more complicated these routines
- might need some additional logic. (ie. worry about borders and such )
- */
-
- UWORD getrows(Win,Font)
- struct Window *Win;
- struct TextFont *Font;
- {
- register struct TextFont *font=Font;
- register struct Window *win=Win;
-
- return( (UWORD) win->Height/font->tf_YSize);
- }
-
- UWORD getcols(Win,Font)
- struct Window *Win;
- struct TextFont *Font;
- {
- register struct TextFont *font=Font;
- register struct Window *win=Win;
-
- return( (UWORD) win->Width/font->tf_XSize);
- }
-
- /*
- here is the Amiga specific cleanup code for MicroEMACS.
- */
-
- void cleanup()
- {
- struct Process *myproc;
-
- if(savewindow != -1L)
- {
- myproc = (struct Process *) FindTask(0L);
- Forbid();
- myproc->pr_WindowPtr = (APTR) savewindow;
- Permit();
- }
- if(consoleReadMsg)
- {
- if(consoleReadMsg->io_Device)
- CloseDevice(consoleReadMsg);
- DeleteStdIO(consoleReadMsg);
- }
- if(consoleWriteMsg) DeleteStdIO(consoleWriteMsg);
- if(consoleport) DeletePort(consoleport);
- if(EmacsWindow) CloseScrWin(EmacsWindow);
- if(GfxBase) CloseLibrary(GfxBase);
- if(IntuitionBase) CloseLibrary(IntuitionBase);
-
- }
-
- /* end of console.c */
- #endif
-