/****************************************************************/ 
/*        */ 
/* FILE : commnd.c      */ 
/* Contains routines :     */ 
/*  ExitCi, HelpCi, PrintCi,   */ 
/*  Dispon, Dispoff, Timeon, Timeoff, WaitCi, */ 
/*  ConvCi, InvConvCi, GrabCi, CgrabCi, GreadCi, */ 
/*  GwritCi, PrintPicCi, ImtopsCi, ShellCi,  */ 
/*  DeleteCi, ResizeCi, OpenCi, CloseCi, TitleCi. */ 
/*        */ 
/****************************************************************/ 
 
#include <string.h> 
/* #include <tos.h>  */
#include <time.h>
#include "window.h" 
#include "aimhelp.h" 
#include "parse.h" 
#include "bblib.h" 
#include "display.h" 
#include "commnd.h" 
#include "aim.h" 
 
#define EQUAL 0 
#define EOS    '\0' 
#define ESC    '\33' 
 
extern char help_file[100]; 
extern char *shell; 
/* Dpg extern OBJECT *menu_addr; 
extern GRECT *full; */
extern int gl_wbox, gl_hbox; 
extern int work_out[57]; 
extern int gl_apid; 
extern char **entri; 
extern long help_index[]; 
extern short gamma, Con_Perc; 
extern int disp_on, time_on; 
 
int 
ExitCi(void)
{
void wimp_exit(void);
void emergency_free(void);
extern FILE  *macro[16], *mp;
extern int i_macro;
extern int alloc_ptr;
int i; 

    if (alloc_ptr >0) emergency_free();
    visdelay_end();
    pointer_reset_shape();
 /* set jump buffer for ESC action */
    for(i=i_macro;i>=0;i--) {
         mp = macro[i];
         if (mp !=NULL)fclose(mp);
      }
wimp_exit();
exit(0);
   return NO_ERROR; 
} 
 
int 
HelpCi(char *p) 
{ 
int i; 
char buffer[2000];
 
 if (*p == 0) 
{ wprintf("%s\n",prt_help(help_index[HELP_HELP],buffer)); return NO_ERROR; }
 if (strcmp("macro", p) == EQUAL) 
{ wprintf("%s\n",prt_help(help_index[HELP_MACRO],buffer)); return NO_ERROR; }
 if (strcmp("log", p) == EQUAL) 
{ wprintf("%s\n",prt_help(help_index[HELP_LOG],buffer)); return NO_ERROR; }
 switch(i = stablk(p, 0)) 
 { 
 case -1 : printw("No help on unknown command.\n"); 
    return NO_ERROR; 
 case -2 : printw("No help on ambiguous command.\n"); 
    return NO_ERROR; 
 default : wprintf("%s\n",prt_help(help_index[cmd_help(i)],buffer)); return NO_ERROR;
 } 
} 

int 
PrintCi(char *p) 
{ 
   short im,k,l,res; 
 
   k = intarg(&p,NULL,"column",0,512,128); 
   l = intarg(&p,NULL,"row   ",0,512,128); 
   im = chrarg(&p,NULL,GenIm,Image,0); 
 
   res = pre_op(wp[im], wp[im], 0); 
   if (res == NO_ERROR) windat(wp[im],k,l); 
/* Dpg
   graf_mouse(ARROW, 0x0L); 
*/
   if (res != NO_ERROR) return res; 
   if (time_on) ptimer(); 
   return NO_ERROR; 
}

int
WaitCi(char *p) 
{ 
 int centi_sec,t;
 wimp_eventstr *e;
 wimp_emask mask;
 BOOL flag;
 extern int timer(void);
 os_regset regs;
 int escape_state;

 centi_sec = intarg(&p,NULL,"Number of centi-seconds",0,3200000,10);
 if (centi_sec == 0) return(NO_ERROR);
 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]; 

 t=timer();
 flag=FALSE;
 mask=event_getmask();

 event_setmask(~wimp_EMNULL & mask); 
 while(flag==FALSE)
  {
/*   event_process(); */
                         /* Keep Windowmanager happy */
   wimpt_poll(0,e);
   e = wimpt_last_event();
   if (e->e == wimp_EKEY) break;
   if (centi_sec !=0 && alarm_timedifference(t,timer()) >= centi_sec) flag=TRUE;
  }
 event_setmask(mask);
 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(NO_ERROR);
 }
 
 
 
int 
Dispon(char *p) 
{ 
   disp_on = intarg(&p,NULL,"display mode",0,5,1); 
   return NO_ERROR; 
} 
 
int 
Dispoff(void)
{ 
   disp_on = 0; 
   return NO_ERROR; 
} 
 
int 
Timeon(void)
{ 
   time_on = TRUE; 
/* Dpg
   menu_icheck(menu_addr, TIMER, TRUE); 
*/
   return NO_ERROR; 
} 
 
int 
Timeoff(void)
{ 
   time_on = FALSE; 
/* Dpg
   menu_icheck(menu_addr, TIMER, FALSE); 
*/
   return NO_ERROR; 
} 
#if !defined(ARCHIMEDES)  
/* Dpg **************************** */
int 
ConvCi(char *p) 
{ 
   short inr,ing,inb,out,res; 
 
   inr = chrarg(&p,NULL,"Input Image Red <A,B,C,D>",Image,0); 
   ing = chrarg(&p,NULL,"Input Image Green <A,B,C,D>",Image,1); 
   inb = chrarg(&p,NULL,"Input Image Blue <A,B,C,D>",Image,2); 
 
   out = chrarg(&p,NULL,OutIm,Image,3); 
 
   res = ((WindowType(wp[inr]) != GREY) || (WindowType(wp[ing]) != GREY) || 
       (WindowType(wp[inb]) != GREY)) ? ERR_NO_GREY : NO_ERROR; 
 
   if (res == NO_ERROR) res = pre_op(wp[inr],wp[ing],1); 
   if (res == NO_ERROR) col_conv(wp[inr],wp[ing],wp[inb],wp[out]); 
 
   if (res == NO_ERROR) wp[out] ->Vbitplane = -1; 
 
   return post_op(wp[out], res); 
} 
 
int 
InvConvCi(char *p) 
{ 
   short outr,outg,outb,in,res; 
 
   in = chrarg(&p,NULL,InpIm,Image,0); 
 
   outr = chrarg(&p,NULL,"Output Image Red <A,B,C,D>",Image,1); 
   outg = chrarg(&p,NULL,"Output Image Green <A,B,C,D>",Image,2); 
   outb = chrarg(&p,NULL,"Output Image Blue <A,B,C,D>",Image,3); 
 
   res = (WindowType(wp[in]) == COLOUR) ? NO_ERROR : ERR_NO_COLOR; 
 
   if (res == NO_ERROR) 
   { 
      col2bw(wp[in],wp[outr],wp[outg],wp[outb]); 
 
      wp[outr] ->Vbitplane = -1; 
      wp[outg] ->Vbitplane = -1; 
      wp[outb] ->Vbitplane = -1; 
   } 
   if (disp_on) 
   { 
 if (res == NO_ERROR) res = gdisp(wp[outr],gamma,Con_Perc); 
 if (res == NO_ERROR) res = gdisp(wp[outg],gamma,Con_Perc); 
   } 
   return post_op(wp[outb], res); 
} 
 
int 
GrabCi(void)
{ 
     return grab(); 
} 
 
int 
CgrabCi(void)
{ 
    int status; 
 
    if ((status = col_grab(wp[0],wp[1],wp[2])) != NO_ERROR) 
 return status; 
    else 
    { 
 col_conv(wp[0],wp[1],wp[2],wp[3]); 
 wp[0]->Vbitplane = -1; 
 wp[1]->Vbitplane = -1; 
 wp[2]->Vbitplane = -1; 
 wp[3]->Vbitplane = -1; 
 
 gdisp(wp[0],gamma,Con_Perc); 
 gdisp(wp[1],gamma,Con_Perc); 
 gdisp(wp[2],gamma,Con_Perc); 
 gdisp(wp[3],gamma,Con_Perc); 
    } 
    return NO_ERROR; 
} 
 
int 
GreadCi(char *p) 
{ 
   short im, res; 
 
   im = chrarg(&p,NULL,GenIm,Image,0); 
   WindowType(wp[im]) = GREY; 
   res = pre_op(wp[im],wp[im],0); 
   if (res == NO_ERROR) res = frame_r(wp[im]); 
   if (res == NO_ERROR) wp[im]->Vbitplane = -1; 
   return post_op(wp[im], res); 
} 
 
int 
GwritCi(char *p) 
{ 
   short im,res; 
   im = chrarg(&p,NULL,GenIm,Image,0); 
   res = pre_op(wp[im],wp[im],0); 
   if (res == NO_ERROR) res = frame_w(wp[im]); 
   graf_mouse(ARROW,0x0L); 
   if (res != NO_ERROR) return res; 
   if (time_on) ptimer(); 
   return NO_ERROR; 
} 
 
int 
PrintPicCi(char *p) 
{ 
short in, f, res; 
 
   res = (resolution == HIGHRES) ? NO_ERROR : ERR_PRINT; 
 
   in = chrarg(&p,NULL,InpIm,Image,0); 
   f = intarg(&p,NULL,"magnification",1,5,1); 
 
   if (res == NO_ERROR) res = pre_op(wp[in], wp[in], 0); 
   if (res == NO_ERROR) res = printpic(wp[in],f); 
 
   graf_mouse(ARROW, 0x0L); 
   if (res != NO_ERROR) return res; 
   if (time_on) ptimer(); 
   return NO_ERROR; 
} 
 
int 
ImtopsCi(char *p) 
{ 
int in, res; 
char* file; 
char buffer[128]; 
 
 in = chrarg(&p, NULL, InpIm, Image, 0); 
 res = (WindowType(wp[in]) == COLOUR) ? ERR_NO_GREY : NO_ERROR; 
 if (res == NO_ERROR) 
     file = strarg(&p, NULL, "postscript output file", "image.ps", buffer); 
 if (file == NULL) return ERR_ABORT; 
 if (res == NO_ERROR) res = pre_op(wp[in], wp[in], 0); 
 if (res == NO_ERROR) res = imtops(wp[in], file); 
 
 graf_mouse(ARROW, 0x0L); 
 if (res != NO_ERROR) return res; 
 if (time_on) ptimer(); 
 return NO_ERROR; 
} 


int 
ShellCi(void)
{ 
COMMAND com; 
int status; 
 
 if (shell == NULL) return ERR_NOSHELL; 
 com.command_tail[0] = 0; 
 com.length = 0; 
 menu_bar(menu_addr, 0); 
 status = (int)Pexec(0, shell, &com, NULL); 
 menu_ienable(menu_addr, DESK, 1); 
 menu_ienable(menu_addr, FILEOPS, 1); 
 menu_ienable(menu_addr, GREYOPS, 1); 
 menu_ienable(menu_addr, BINOPS, 1); 
 menu_ienable(menu_addr, UTILS, 1); 
 menu_bar(menu_addr, 1); 
 if (status < 0) 
  return ERR_EXEC; 
 else 
 { 
  /* hack to redraw everything on the screen */ 
  form_dial (FMD_FINISH,0,0,0,0,0,0,work_out[0],work_out[1]); 
  return NO_ERROR; 
 } 
} 

 
int 
DeleteCi(char* p) 
{ 
int im, res; 
 
 im = chrarg(&p, NULL, GenIm, Image, 0); 
 pre_op(wp[im], wp[im], 0); 
 res = set_impar(wp[im], 256, 256, EMPTY); 
 if (res != NO_ERROR) return res; 
 WindowName(wp[im])[0] = '\0'; 
 ImageName(wp[im])[1] = '\0'; 
 icon_set(WindowIhandle(wp[im]), IC_NAME, ImageName(wp[im])); 
 if (WindowVisible(wp[im])) 
 { 
  wind_set(WindowVport(wp[im]), WF_NAME, ImageName(wp[im])); 
  send_redraw(WindowVport(wp[im])); 
 } 
 return post_op(NULL, NO_ERROR); 
} 
 
int 
ResizeCi(char* s) 
{ 
int im, x, y, w, h, new_w, new_h, res, factor, w_def, h_def, vis; 
 
    im = chrarg(&s, NULL, GenIm, Image, 0); 
    w_def = WindowWidth(wp[im]); 
    h_def = WindowHeight(wp[im]); 
    factor = w_def / full->g_w; 
    factor = max(h_def / full->g_h, factor); 
    factor = max(factor, 1); 
    w_def = w_def/factor; 
    h_def = h_def/factor; 
    new_w = intarg(&s, NULL, "window width", 3 * gl_hbox, full->g_w, w_def); 
    new_h = intarg(&s, NULL, "window height", 3 * gl_wbox, full->g_h, h_def); 
 
    /* get current window size */ 
    if (WindowVisible(wp[im])) 
 wind_get(WindowVport(wp[im]), WF_WORKXYWH, &x, &y, &w, &h); 
    else 
 wind_calc(WC_WORK, IMAGE_WINDOW, wp[im]->box.g_x, wp[im]->box.g_y, 
  wp[im]->box.g_w, wp[im]->box.g_h, &x, &y, &w, &h); 
 
    form_alert(1,"[1][voor pre_op][ OK ]"); 
    /* do the intro */ 
    res = pre_op(wp[im], wp[im], 0); 
 
    form_alert(1,"[1][voor if change][ OK ]"); 
    /* check if there is a change */ 
    if (((new_w != w) || (new_h != h)) && (res == NO_ERROR)) 
    { 
 /* compute new border size and store it in wp->box */ 
 wind_calc(WC_BORDER, IMAGE_WINDOW, x, y, new_w, new_h, &wp[im]->box.g_x, 
  &wp[im]->box.g_y, &wp[im]->box.g_w, &wp[im]->box.g_h); 
 
 form_alert(1,"[1][voor set_FDB][ OK ]"); 
 /* 
  * fool set_FDB into thinking that the window is closed 
  * in this way set_FDB gets the window size from wp->box 
  */ 
 vis = WindowVisible(wp[im]); 
 WindowVisible(wp[im]) = FALSE; 
 res = set_FDB(wp[im]); 
 WindowVisible(wp[im]) = vis; /* restore visibility flag */ 
 
 form_alert(1,"[1][voor disp][ OK ]"); 
 if (res == NO_ERROR) 
 { 
     /* if image is empty you don't want to compute an image */ 
     if (WindowType(wp[im]) != EMPTY) 
     { 
  if (WindowBitplane(wp[im]) == -1) 
      res = gdisp_core(wp[im], gamma, Con_Perc); 
  else 
      res = bdisp_core(wp[im]); 
     } 
     if (WindowVisible(wp[im])) 
  wind_set(WindowVport(wp[im]), WF_CURRXYWH, wp[im]->box.g_x, 
   wp[im]->box.g_y, wp[im]->box.g_w, wp[im]->box.g_h); 
 } 
    } 
    form_alert(1,"[1][voor post_op][ OK ]"); 
    return post_op(NULL, res); 
} 
 
int 
OpenCi(char* s) 
{ 
int im; 
 
 im = chrarg(&s, NULL, GenIm, Image, 0); 
 pre_op(wp[im], wp[im], 0); 
 WindowReopen(wp[im]); 
 return post_op(NULL, NO_ERROR); 
} 
 
int 
CloseCi(char* s) 
{ 
int im; 
 
 im = chrarg(&s, NULL, GenIm, Image, 0); 
 pre_op(wp[im], wp[im], 0); 
 WindowClose(wp[im]); 
 return post_op(NULL, NO_ERROR); 
} 
 
int 
TitleCi(char *s) 
{ 
    int im, bitplane; 
    char *title, buffer[128]; 
 
    im = chrarg(&s, NULL, GenIm, Image, 0); 
    title = strarg(&s, NULL, "window title", "title", buffer); 
 
    bitplane = WindowBitplane(wp[im]); 
 
    wind_set(WindowVport(wp[im]), WF_TOP); 
    if (bitplane != -1) 
 sprintf(ImageName(wp[im])+1, " %s b%d %.25s", WindowName(wp[im]), 
  bitplane+1, title); 
    else 
 sprintf(ImageName(wp[im])+1, " %s %.28s", WindowName(wp[im]), title); 
 
    wind_set(WindowVport(wp[im]), WF_NAME, ImageName(wp[im])); 
    icon_set(WindowIhandle(wp[im]), IC_NAME, ImageName(wp[im])); 
    return post_op(NULL, NO_ERROR); 
} 
#endif
