/* V1.2 15:02:96 */
/* Mouse.c Project Calgor */

#include <hold/extra.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <devices/inputevent.h>
#include <stdio.h>
#include <signal.h>
#include <graphics/gfx.h>

#include <proto/exec.h>
#include <clib/intuition_protos.h>
#include <clib/console_protos.h>

extern void alert(char *);
extern void menu_opt(void);
extern void anim_opt(void);
extern void clean_local(void);

extern struct BitMap ft_bitmap;
extern UBYTE *keybufptr;
extern ULONG modeID;
extern UWORD wb_version;

void back_drop(void);
void safeclose(struct MsgPort *, struct Window *);
void shutdown(void);
void handler(int);
void clearkeybuf(void);

struct Window *my_window = NULL;

/* Declare and initialize your NewWindow structure: */
struct NewWindow back_window =
{
  0,            /* LeftEdge    x position of the window. */
  0,            /* TopEdge     y positio of the window. */
  640,           /* Width       640 pixels wide. */
  512,           /* Height      512 lines high. */
  0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  IDCMP_MOUSEBUTTONS|IDCMP_ACTIVEWINDOW|IDCMP_INACTIVEWINDOW|IDCMP_RAWKEY,
  WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP|WFLG_BORDERLESS,
  NULL,          /* FirstGadget No Custom Gadgets. */
  NULL,          /* CheckMark   Use Intuition's default CheckMark (v). */
  NULL,          /* Title       Title of the window. */
  NULL,          /* Screen      Connected to the Escreen  */
  NULL,          /* BitMap      No Custom BitMap. */
  0,             /* MinWidth    We do not need to care about these */
  0,             /* MinHeight   since we havent supplied the window with */
  0,             /* MaxWidth    a Sizing Gadget. */
  0,             /* MaxHeight */
  CUSTOMSCREEN   /* Type        Connected to the My custom Screen. */
};

struct ExtNewScreen Escreen=
{
  0,            /* LeftEdge  Should always be 0. */
  0,            /* TopEdge   Top of the display.*/
  640,          /* Width  */
  512,          /* Height */
  4,            /* Depth     16 colours. */
  0,            /* DetailPen Text should be drawn with colour reg. 0 */
  1,            /* BlockPen  Blocks should be drawn with colour reg. 1 */
  HIRES|SPRITES,
  CUSTOMSCREEN | NS_EXTENDED, /* Type      Customized screen. */
  NULL,         /* Font      Default font. */
  NULL,         /* Title     The screen' title. */
  NULL,         /* Gadget    Must for the moment be NULL. */
  NULL,         /* BitMap    No special CustomBitMap. */
  NULL          /* TagItems */
};

struct InputEvent ievent = { NULL };

struct TagItem NSTags[2] =
{
  {SA_DisplayID,NULL},
  {TAG_DONE,NULL}
};

struct Screen *my_screen = NULL;

SHORT squeek[2];
BOOL lmbpress;
BOOL wactive;
BOOL keypress;

void back_drop(void)
{
/* Open Screen  */

  NSTags[0].ti_Data = modeID;
  Escreen.Extension = NSTags;
  if(wb_version<37)
    Escreen.ViewModes |= LACE;
  my_screen = (struct Screen *) OpenScreen( (struct NewScreen *) &Escreen );
  if (my_screen==NULL){
     clean_local();
     alert("Failed open screen (back_drop)\n");
  }
  back_window.Screen = my_screen;  /* Window uses my Screen */
  /* Open Window */
  my_window = (struct Window *) OpenWindow( &back_window );
  /* Have we opened the window succesfully? */
  if(my_window == NULL){
    clean_local();
    alert("Failed open window mouse.c (back_drop)\n");
  }
  ClearPointer(my_window);
}


void safeclose(struct MsgPort *mp, struct Window *win){
struct IntuiMessage *msg=NULL;
struct Node *succ=NULL;;

   msg = (struct IntuiMessage *) mp->mp_MsgList.lh_Head;
   while( succ=msg->ExecMessage.mn_Node.ln_Succ ){
     if(msg->IDCMPWindow==win){
       Remove( (struct Node *) msg);
       ReplyMsg((struct Message *) msg);
     }
     msg = (struct IntuiMessage *) succ;
   }
}

void shutdown(void){
  if(my_window){
    Forbid();
    safeclose(my_window->UserPort,my_window);
    my_window->UserPort=NULL;
    ModifyIDCMP(my_window,0L);
    Permit();
    CloseWindow(my_window);
    my_window=NULL;
  }
  if(my_screen){
    CloseScreen(my_screen);
    my_screen=NULL;
  }
}


void handler(int pulse)
{
long actual;
/* SHORT lop; */
ULONG class;
USHORT code;
SHORT mousex, mousey;
struct IntuiMessage *message;

      /* Wait (1L << my_window->UserPort->mp_SigBit); */
      keypress = wactive = lmbpress = FALSE;
      squeek[0]=squeek[1]=0;
      actual = 0L;
      while(message = (struct IntuiMessage *)
        GetMsg(my_window->UserPort)){
          class = message->Class;
          code = message->Code;
          mousex = message->MouseX;
          mousey = message->MouseY;
          ievent.ie_position.ie_addr = *((APTR *) message->IAddress);
          ievent.ie_Class = IECLASS_RAWKEY;
          ievent.ie_Code  = message->Code;
          ievent.ie_Qualifier = message->Qualifier;
          ReplyMsg( (struct Message *) message);
          switch(class){
            case(IDCMP_RAWKEY):
              keypress = TRUE;
              actual=RawKeyConvert(&ievent,keybufptr,(KEYBUFF-1),NULL);
              if(actual==-1)
                clearkeybuf();
              /* Check Key */
              /* for(lop=0;lop<actual;lop++)
                  printf("%X ",*(keybufptr+lop) );
                  printf("\n"); */
              break;
            case(IDCMP_MOUSEBUTTONS):
               switch(code){
                 case SELECTDOWN:
                   squeek[0]=mousex;   /* Squeek 0 x coordinate */
                   squeek[1]=mousey;   /* Squeek 1 y coordinate */
                    lmbpress=TRUE;
                    wactive=TRUE;
                   break;
                 case SELECTUP:
                   break;
                 case MENUDOWN:
                   break;
                 case MENUUP:
                   break;
                 default:
                   clean_local();
                   alert("Unknown code mouse.c (mouse options)\n");
                   break;
               }
            case(IDCMP_ACTIVEWINDOW):
              wactive=TRUE;
              lmbpress=TRUE;
              break;
            case(IDCMP_INACTIVEWINDOW):
              break;
            break;
          }
      }
      signal(SIGINT,handler);
}

void clearkeybuf(void){
int loop;

    for(loop=0;loop<KEYBUFF;loop++)
      *(keybufptr+loop)= '\0';
}
