/* c.events  */

#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "swis.h"        /*  Software interrupts                      */
#include "wimp.h"        /*  access to WIMP SWIs                      */
#include "wimpt.h"       /*  access to WIMPT SWIs                     */
#include "menu.h"        /*  access to MENUS                          */
#include "event.h"       /*  poll loops, etc                          */
#include "os.h"          /*  magic numbers                            */

#define CNTRL_C  0x03
#define CR     '\r'
#define LF     '\n'
#define FF     '\f'
#define ESCAPE   27
#define ARROW_UP 0x18F   
#define DEL      0x7F
#define BACKSPACE 0x08


int wgetch(void);
void wvdu(char ch);
void wprintf(const char *format, ...);
char * get_cmd(char * txt, wimp_eventstr *e);


extern int wprintf_window;
extern menu imgmenu;
extern menu main_menu;
/*extern BOOL dbox_semaphor; */


BOOL flag;

char *poll_loop(char * string,int mode,wimp_eventstr *evnt)
{
 int i,key;
 wimp_eventstr *e;
 BOOL muis=FALSE;
 os_regset regs;
 int escape_state;

 regs.r[0] = 229; /* Programmers reference manual volume 2 page 522 */
 regs.r[1] = 1;   /* Programmers reference manual volume 3 page 1145 */
 regs.r[2] = 0;
 os_swi(OS_Byte,&regs);
 escape_state=regs.r[1]; 
 key=0;
 if (mode == 1)
  {
    while(TRUE)
     {
      event_process();
      e = wimpt_last_event();
      if (e->e == wimp_EBUT) break;
      if (e->e == wimp_EKEY) 
        { if (e->data.key.chcode == ESCAPE) key=-1;
          if (e->data.key.chcode == 13) key=-2;
        }
      if (key <0) break;
     }   
   if (key <0)
    { 
     evnt->data.but.m.x=key;
    }
   else
    {
     evnt->data.but.m.x=e->data.but.m.x;
     evnt->data.but.m.y=e->data.but.m.y;
     evnt->data.but.b=e->data.but.b;
    }
  }
 if (mode==0)
  {
   i=-1;
   string[i]='\0';
   flag=FALSE;
   while(TRUE)
    {
     event_process();
     e = wimpt_last_event();
/*     if (e->e != wimp_EREDRAW) wprintf("in poll_loop evnt=%d\n",e->e); */
     if (e->e == wimp_EKEY) break;
    }
/* if (muis && !dbox_semaphor) { string=get_cmd(string,e);return(string); } */
   key=e->data.key.chcode;
   while (i<254) 
    {
     if (i==-1) i=0; else key=wgetch();
     if( (i >= 0 && isprint(key))&& key <=128 || (key == DEL && i>0) )wvdu((char)key);
     if ((key == BACKSPACE && i>0) )wvdu((char)DEL);
     switch( key )
      {
       case CR:
                  flag=TRUE;
                  wvdu((char)key);
                  break; 
       case LF:
                  flag=TRUE;
                  wvdu((char)key);
                  break; 
       case FF:
                  flag=TRUE;
                  wvdu((char)key);
                  break; 
       case CNTRL_C:   
                  flag=TRUE;
                  i=0;
                  break;
       case BACKSPACE:
       case DEL:      
                  if ( i > 0) i--;
                  break;

/*
   Made a real one with the SIGNAL option of C, Ed Doppenberg
*/
       case ESCAPE:   
                  break;
 

       default:        
                  if(isprint(key)&&key <=128) { string[i]=(char)key;i++; }
                  else wimp_processkey(key);
                  break;
      }
     if (flag) break;
    }
   string[i] ='\0';
  }
 regs.r[0] = 229;
 regs.r[1] = escape_state;
 regs.r[2] = 0;
 os_swi(OS_Byte,&regs);
 regs.r[0] = 124;
 os_swi(OS_Byte,&regs);
 return(string);
}

char * get_cmd(char * txt, wimp_eventstr *e)
 {
  BOOL fl;
  char *tmp,*endeze,rest[80];
  if ((endeze=tmp=(char *)calloc(256,1))==NULL) return(strcpy(txt,'\0'));
       wimp_decode_menu(menu_syshandle(imgmenu),e->data.menu,tmp);
       tmp[strlen(tmp)-1]='\0'; 
       tmp=(char *)(tmp+strlen(tmp));
       while(*tmp !='.' && tmp >= endeze) tmp--;
       tmp++;
/*       fl=dialogue(rest,tmp);*/
       if (fl) { strcat(tmp,rest);
               }
       else *tmp='\0';
       strcpy(txt,tmp); 
 free(endeze);
 return(txt);
 }


int wgetch(void)
 {
  wimp_eventstr *e;
  event_process();
  e = wimpt_last_event();
  if (e->e == wimp_EKEY) return(e->data.key.chcode);
 
  while(TRUE)
   {
    event_process();               /* Keep Windowmanager happy */
    e = wimpt_last_event();
    if (e->e == wimp_EKEY) break;
  }
 return(e->data.key.chcode);
 }
