/* intui.c:
 *
 * System requester stuff.
 *
 * ----------------------------------------------------------------------
 * This code is (C) Copyright 1993 by Frank Munkert.
 * All rights reserved.
 * This software may be freely distributed and redistributed for
 * non-commercial purposes, provided this notice is included.
 * ----------------------------------------------------------------------
 * History:
 * 
 * 28-Nov-93   fmu   Added custom CD-DA icon; removed GetDefDiskObject() call.
 * 09-Oct-93   fmu   SAS/C support added
 */

#include <stdarg.h>
#include <stdio.h>

#include <exec/types.h>
#include <intuition/intuition.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/icon_protos.h>
#include <clib/wb_protos.h>
#ifdef AZTEC_C
#include <pragmas/exec_lib.h>
#include <pragmas/intuition_lib.h>
#include <pragmas/icon_lib.h>
#include <pragmas/wb_lib.h>
#endif
#ifdef LATTICE
#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/wb_pragmas.h>
#endif
#if defined(_DCC) && defined(REGISTERED)
#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/wb_pragmas.h>
extern struct Library *SysBase;
#endif

#include "intui.h"

struct IntuitionBase *IntuitionBase;
extern struct Library *IconBase;
struct Library *WorkbenchBase;

struct MsgPort *g_app_port;
struct AppIcon *g_app_icon;
ULONG g_app_sigbit;

USHORT g_image_data[] = {
/* Plane 0 */
  0x0000,0x00FF,0xFE00,0x0000,0x0000,0x3F80,0x03F8,0x0000,
  0x0003,0xC000,0x0007,0x8000,0x000C,0x0000,0x0000,0x6000,
  0x0070,0x0000,0x0000,0x1C00,0x0080,0x0000,0x0000,0x0200,
  0x0300,0x0000,0x0000,0x0080,0x0400,0x0000,0x0000,0x0040,
  0x0800,0x0000,0x0000,0x0020,0x1000,0x0000,0x0000,0x0010,
  0x2000,0x0000,0x0000,0x0008,0x4000,0x000F,0xE000,0x0004,
  0x4000,0x003F,0xF800,0x0004,0x8000,0x0030,0x1800,0x0002,
  0x8000,0x0040,0x0400,0x0002,0x8000,0x0040,0x0400,0x0002,
  0x8000,0x0040,0x0400,0x0002,0x8000,0x0030,0x1800,0x0002,
  0x4000,0x003F,0xF800,0x0004,0x4000,0x000F,0xE000,0x0004,
  0x2000,0x0000,0x0000,0x0008,0x1000,0x0000,0x0000,0x0010,
  0x0800,0x0000,0x0000,0x0020,0x0400,0x0000,0x0000,0x0040,
  0x0200,0x0000,0x0000,0x0080,0x0080,0x0000,0x0000,0x0200,
  0x0070,0x0000,0x0000,0x1C00,0x000C,0x0000,0x0000,0x6000,
  0x0003,0xC000,0x0007,0x8000,0x0000,0x3F80,0x01F8,0x0000,
  0x0000,0x00FF,0xFE00,0x0000,
/* Plane 1 */
  0x0000,0x0000,0x0000,0x0000,0x0000,0x007F,0xFC00,0x0000,
  0x0000,0x3FFF,0xFFF8,0x0000,0x0003,0xFF00,0x01FF,0x8000,
  0x000F,0xE0FF,0xFE0F,0xE000,0x007F,0x1FFF,0xFFF1,0xFC00,
  0x00FC,0xFF80,0x03FE,0x7F00,0x03F3,0xF07F,0xFC1F,0x9F80,
  0x07EF,0x8FC0,0x07E3,0xEFC0,0x0F9E,0x7E3F,0xF8FC,0xF3E0,
  0x1F79,0xF9FF,0xFF3F,0x3DF0,0x3EF7,0xE7F0,0x1FCF,0xDEF8,
  0x3EEF,0xDFC0,0x07F7,0xEEF8,0x7DEF,0xDFC0,0x07F7,0xEF7C,
  0x7DDF,0xBF80,0x03FB,0xF77C,0x7DDF,0xBF80,0x03FB,0xF77C,
  0x7DDF,0xBF80,0x03FB,0xF77C,0x7DEF,0xDFC0,0x07F7,0xEF7C,
  0x3EEF,0xDFC0,0x07F7,0xEEF8,0x3EF7,0xE7F0,0x1FCF,0xDEF8,
  0x1F79,0xF9FF,0xFF3F,0x3DF0,0x0F9E,0x7E3F,0xF8FC,0xF3E0,
  0x07EF,0x8FC0,0x07E3,0xEFC0,0x03F3,0xF07F,0xFC1F,0x9F80,
  0x01FC,0xFF80,0x03FE,0x7F00,0x007F,0x1FFF,0xFFF1,0xFC00,
  0x000F,0xE0FF,0xFE0F,0xE000,0x0003,0xFF00,0x01FF,0x8000,
  0x0000,0x3FFF,0xFFF8,0x0000,0x0000,0x007F,0xFE00,0x0000,
  0x0000,0x0000,0x0000,0x0000
};

struct Image g_disk_object_image = {
  0, 0,			/* top corner */
  63, 31, 2,		/* width, height, depth */
  g_image_data,
  0x03, 0x00,		/* planepick, planeonoff */
  NULL,			/* next image */
};

UBYTE *g_tool_types[] = {
  NULL
};

struct DiskObject g_disk_object = {
  WB_DISKMAGIC,
  WB_DISKVERSION,
  {
    NULL,
    0, 0,
    63, 32,
    GADGIMAGE | GADGHCOMP,
    RELVERIFY | GADGIMMEDIATE,
    BOOLGADGET,
    (APTR) &g_disk_object_image,
    NULL,
  },
  0,
  "",
  (char **) g_tool_types,
  NO_ICON_POSITION,
  NO_ICON_POSITION,
  NULL,
  NULL,
  0
};

void Init_Intui (void)
{
  IntuitionBase = (struct IntuitionBase *)
     OpenLibrary ((UBYTE *) "intuition.library", 37);
  IconBase = OpenLibrary ((UBYTE *) "icon.library", 37);
  if (!IconBase)
    Display_Error ("cannot open icon.library");
  WorkbenchBase = OpenLibrary ((UBYTE *) "workbench.library", 37);
  if (!WorkbenchBase)
    Display_Error ("cannot open workbench.library");

  g_app_port = NULL;
  g_app_sigbit = 0;
  g_app_icon = NULL;
}

void Close_Intui (void)
{
  if (WorkbenchBase)
    CloseLibrary (WorkbenchBase);
  if (IconBase)
    CloseLibrary (IconBase);
  if (IntuitionBase)
    CloseLibrary ((struct Library *) IntuitionBase);
}

void Display_Error (char *p_message, ...)
{
  va_list arg;

  static struct EasyStruct req = {
    sizeof (struct EasyStruct),
    0,
    (UBYTE *) "CDROM Handler Error",
    NULL,
    (UBYTE *) "Abort"
  };
  
  va_start (arg, p_message);
  if (IntuitionBase) {
    req.es_TextFormat = (UBYTE *) p_message;
    EasyRequestArgs (NULL, &req, NULL, arg);
  }
  va_end (p_message);
}

void Show_CDDA_Icon (void)
{
  if (!IconBase || !WorkbenchBase)
    return;

  if (g_app_icon)
    Display_Error ("Show_CDDA_Icon called twice!");

  g_app_port = CreateMsgPort ();
  if (!g_app_port)
    return;

  g_app_sigbit = 1<<g_app_port->mp_SigBit;

  g_app_icon = AddAppIconA (0, 0, (UBYTE *) "CD-DA", g_app_port,
  			    NULL, &g_disk_object, NULL);
  if (!g_app_icon) {
    DeleteMsgPort (g_app_port);
    g_app_port = NULL;
    return;
  }
}

void Hide_CDDA_Icon (void)
{
  struct Message *msg;

  if (!IconBase || !WorkbenchBase)
    return;

  if (g_app_icon)
    RemoveAppIcon (g_app_icon);
  if (g_app_port) {
    while (msg = GetMsg (g_app_port))
      ReplyMsg (msg);
    DeleteMsgPort (g_app_port);
  }

  g_app_port = NULL;
  g_app_sigbit = 0;
  g_app_icon = NULL;
}
