[comp.sys.amiga] single char input

herr@calgary.UUCP (Charles Herr) (05/31/88)

 I am having a problem with a single-char input routine I have been
working on. The RKM says that the routines QConRead() and ConGetChar()
should work. However the screens open up fine, as I can see them after
they are opened, BUT the systems freezes up with a task held/Software Error
requestor. I have just about lost all my hair over this one.. can anyone
help????
  I suspect that the problem lies in on of the 2 aformentioned routines.
I am using Dos1.2 with Manx 3.4a and use the following to compile it:
		cc test.c
		ln -o test test.o -lc

Also I am most likely including far to many ".h" files but that is all
for future expansion and should not matter.

Code follows....
------------------------------------------------------------------------------
#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/text.h>
#include <graphics/regions.h>
#include <graphics/copper.h>
#include <graphics/gels.h>
#include <devices/keymap.h>
#include <devices/console.h>
#include <hardware/blit.h>
#include <stdio.h>
#include <ctype.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <devices/timer.h>

#include <functions.h>
#undef NULL
#define   NULL   ((void *)0)

#define INTUITION_REV 1L
#define GRAPHICS_REV  1L

extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct IOStdReq *ConReadMsg;
struct MsgPort *ConReadPort;
struct IOStdReq *ConWriteMsg;
struct MsgPort *ConWritePort;

char conchar;

struct NewScreen NewScreen = {
	0,0,	/* screen XY origin relative to View */
	640,200,	/* screen width and height */
	4,	/* screen depth (number of bitplanes) */
	0,1,	/* detail and block pens */
	HIRES,	/* display modes for this screen */
	CUSTOMSCREEN,	/* screen type */
	NULL,	/* pointer to default screen font */
	(UBYTE *)"Test Screen", /*screen title */
	NULL,	/* first in list of custom screen gadgets */
	NULL	/* pointer to custom BitMap structure */
};

USHORT colors[] = {
	0x0000,	/* color #0 */	0x0A00,	/* color #1 */	0x0007,	/* color #2 */
	0x000F,	/* color #3 */	0x0070,	/* color #4 */	0x00F0,	/* color #5 */
	0x0700,	/* color #6 */	0x0F00,	/* color #7 */	0x0FF0,	/* color #8 */
	0x00FF,	/* color #9 */	0x0F0F,	/* color #10 */	0x0FFF,	/* color #11 */
	0x0777,	/* color #12 */	0x0F70,	/* color #13 */	0x0606,	/* color #14 */
	0x0F06	/* color #15 */
};

struct NewWindow NewWindow = {
	0,1,	/* window XY origin relative to TopLeft of screen */
	640,199,	/* window width and height */
	0,1,	/* detail and block pens */
        CLOSEWINDOW,
	WINDOWDEPTH+WINDOWCLOSE+ACTIVATE,	/* other window flags */
	NULL,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	(UBYTE *)"Test Window",	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	5,5,	/* minimum width and height */
	640,200,	/* maximum width and height */
	CUSTOMSCREEN	/* destination screen type */
};

struct Screen *myscreen;            /* ptr to applications screen */
struct Window *mywindow;            /* ptr to applications window */
struct ViewPort *myviewport;
struct RastPort *myrastport;
FILE *fp;

main()
{
 char ch;
 char ConGetChar();
 
 fp = fopen("tracer","w");
 if(fp == NULL)
   exit(1);
   
 fprintf(fp,"Going into InitDevs\n");
 InitDevs();
 fprintf(fp,"Exited InitDevs\n"); 
 fprintf(fp,"Waiting for characters\n"); 
 ch =  ConGetChar(); 
 while(ch != 'q')
   {
    fprintf(fp,"Character = %c\n",ch);
    ch =  ConGetChar(); 
   }   
 fprintf(fp,"Cleaning up\n"); 
 fclose(fp);
 cleanup("",0);
}


InitDevs()
{
 int	i;
 BYTE	*b,*c;

 fprintf(fp,"Opening Intuition\n"); 
 IntuitionBase = (struct IntuitionBase *)
    OpenLibrary("intuition.library", INTUITION_REV);
 if( IntuitionBase == NULL )
    cleanup("can't open intuition",1);

 fprintf(fp,"Opening Graphics\n"); 
  GfxBase = (struct GfxBase *)
    OpenLibrary("graphics.library",GRAPHICS_REV);
 if( GfxBase == NULL )
    cleanup("can't open graphics library",2);

 fprintf(fp,"Opening screen\n"); 
 if ((myscreen = (struct Screen *)OpenScreen(&NewScreen)) == NULL)
	cleanup("can't open screen",3);
    NewWindow.Screen = myscreen;

 fprintf(fp,"Opening window\n"); 
 if ((mywindow = (struct Window *)OpenWindow(&NewWindow)) == NULL)
     cleanup("can't open window",4);

 myviewport   = (struct ViewPort *)ViewPortAddress(mywindow);
 myrastport   = (struct RastPort *)mywindow->RPort;

 LoadRGB4(myviewport,(struct ColorMap *)colors,16L);
 SetAPen(mywindow->RPort,1L);
 
 fprintf(fp,"Opening Conwriteport\n"); 
 ConWritePort = CreatePort("con.write",0);
 if(ConWritePort == 0)
   cleanup("can't open conwriteport",6);

 fprintf(fp,"Opening Conwritemsg\n"); 
 ConWriteMsg = CreateStdIO(ConWritePort);
 if(ConWriteMsg == 0) 
    cleanup("can't open conreadmsg",9);

 fprintf(fp,"Opening Conreadport\n"); 
 ConReadPort = CreatePort("con.read",0);
 if(ConReadPort == 0)
   cleanup("can't open conreadport",10);

 fprintf(fp,"Opening Conreadmsg\n"); 
 ConReadMsg = CreateStdIO(ConReadPort);
 if(ConReadMsg == 0) 
    cleanup("can't open conreadmsg",11);
 
 ConWriteMsg -> io_Data = (APTR) mywindow;
 ConWriteMsg -> io_Length = sizeof(*mywindow);
 fprintf(fp,"Opening Console\n"); 
 i = OpenDevice("console.device",0,ConWriteMsg,0);
 if(i)  
    cleanup("can't open condevice",12);
 ConReadMsg -> io_Device =  ConWriteMsg -> io_Device; 
 ConReadMsg -> io_Unit =  ConWriteMsg -> io_Unit; 
 QConRead(&conchar);

}


cleanup(reason, fault)
char *reason;
int fault;
{
    switch(fault) {
	case 0:		/* quitting close everything */
        case 12:
               DeleteStdIO(ConReadMsg);
        case 11:
               DeletePort(ConReadPort);
        case 10:
               DeleteStdIO(ConWriteMsg);
        case 9:
               DeletePort(ConWritePort);

	case 4:		/* error opening window */
	if (mywindow != NULL) CloseWindow( mywindow );
	CloseScreen( myscreen );

	case 3:		/* error opening screen */
	case 2:		/* error opening graphics library */
	case 1:		/* error opening intuition */
	default:
	if (*reason) puts (reason);
	}
    exit(fault);
}

QConRead(ch)
char *ch;
{
  ConReadMsg -> io_Command = CMD_READ;
  ConReadMsg -> io_Data = (APTR)ch;
  ConReadMsg -> io_Length = 1;
  SendIO(ConReadMsg);
  return;
}

char ConGetChar()
{
    char temp;
    while((GetMsg(ConReadPort) == NULL))
         WaitPort(ConReadPort);
    temp = conchar;
    QConRead(&conchar);
    return(temp);
}