/* 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:
 * 
 * 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;
struct Library *IconBase;
struct Library *WorkbenchBase;

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

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_dobj = NULL;
  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_dobj = GetDefDiskObject (WBDISK);
  if (!g_dobj)
    return;
  g_dobj->do_Type = 0;

  g_app_port = CreateMsgPort ();
  if (!g_app_port) {
    FreeDiskObject (g_dobj);
    return;
  }
  g_app_sigbit = 1<<g_app_port->mp_SigBit;

  g_app_icon = AddAppIconA (0, 0, (UBYTE *) "CD-DA", g_app_port,
  			    NULL, g_dobj, NULL);
  if (!g_app_icon) {
    DeleteMsgPort (g_app_port);
    FreeDiskObject (g_dobj);
    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);
  }
  if (g_dobj)
    FreeDiskObject (g_dobj);

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