/*
  MidiMan - driver management for midi support

  main.h - main Riscos entry

  created  12/2021
*/

#ifndef main_h
#define main_h

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

#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif

#ifndef NULL
#define NULL 0
#endif

typedef unsigned long long int Uint64;
typedef signed long long int Int64;

#define APP_NAME      "MidiMan"
#define APP_SPRITE    "midiman"
#define APP_DIR       "<"APP_NAME"$Dir>"
#define RESOURCES_DIR "<"APP_NAME"Res$Dir>"

// MIDISupport Message numbers
#define MESSAGE_INSTALL   0x4ee80
#define MESSAGE_REMOVE    0x4ee81
#define MESSAGE_CONNECT   0x4ee86

// MIDI Serivice calls
#define Service_MIDI           0x58
#define Service_MIDIAlive         0
#define Service_MIDIDying         1
#define Service_MIDISupportAlive  4
#define Service_MIDISupportDying  5

// MIDISupport SWI numbers
#define MIDISupport_InstallDriver   0x4EE80
#define MIDISupport_RemoveDriver    0x4EE81
#define MIDISupport_DriverInfo      0x4EE82
#define MIDISupport_CreateDriver    0x4EE83
#define MIDISupport_Send            0x4EE84
#define MIDISupport_Receive         0x4EE85
#define MIDISupport_Connect         0x4EE86
#define MIDISupport_ConnectName     0x4EE87
#define MIDISupport_GetAddress      0x4EE88
#define MIDISupport_Insert          0x4EE89

// MIDISupport Driver flags
#define CAN_SEND             0
#define CAN_RECEIVE          1
#define REQUIRES_BUFFERING   2
#define CAN_RECEIVE_COMMAND  3

// windows
enum { WIN_CONNECTED,
       WIN_DRIVERS,
       WIN_DRV_PANE,
       WIN_DRV_INFO,
       WIN_MODULES,
       WIN_MOD_PANE,
       WIN_PROG_INFO,
       WIN_PLAYER_OPT,
       WIN_USB_OPT,
       WIN_ROUTE_MAP,
       WIN_ROUTE_PANE,
       WIN_SAVE,
       NUM_WINDOWS };

// menus
enum { MENU_ICONBAR,
       MENU_SOURCES,
       MENU_DESTINATIONS,
       MENU_PANIC,
       MENU_DRIVERS,
       MENU_ROUTE_MAP,
       NUM_MENUS };

// Drivers window icons
#define ICON_SRC_MENU   0
#define ICON_SRC_NUM    1
#define ICON_SRC_ARROW  2
#define ICON_DRV_NAME   3
#define ICON_DRV_INFO   4
#define ICON_DST_ARROW  5
#define ICON_DST_NUM    6
#define ICON_DST_MENU   7

// Driver Info window icons
#define ICON_INFO_NAME  2
#define ICON_INFO_VERS  4
#define ICON_INFO_DATE  5
#define ICON_INFO_SEND  7
#define ICON_INFO_RECV  9
#define ICON_INFO_BUFF 11
#define ICON_INFO_RXUN 13

// Modules window icons
#define ICON_MOD_TITLE   0
#define ICON_MOD_LOADED  1
#define ICON_MOD_VERSION 2

// Player config window icons
#define ICON_PLAYER_TEMPO 1
#define ICON_PLAYER_PITCH 3
#define ICON_PLAYER_SAVE  4
#define ICON_NO_SYSEX     5

//USB config window icons
#define ICON_USB_SAVE     0
#define ICON_LIMIT_TX     1

// Routing window icons
#define ICON_ROUTE_SRC   0
#define ICON_ROUTE_DST   1
#define ICON_ROUTE_MAP  64

// Save window icons
#define ICON_SAVE_SPR    0
#define ICON_SAVE_NAME   1

// ro.flags bitfield
#define QUIT          0
#define MESSAGES_OPEN 1
#define TEMPLATES_OK  2
#define CENTRALISE    3

// ro.panic bitfield
#define PANIC_ALL_NOTES_OFF 0
#define PANIC_RESET_CTRLS   1
#define PANIC_GM1_RESET     2
#define PANIC_GM2_RESET     3
#define PANIC_XG_RESET      4
#define PANIC_GS_RESET      5
#define PANIC_OPTIONS  6

// global variables
typedef struct ro_glbs_s
{
  int task_handle;
  int handle[NUM_WINDOWS];
  int cur_menu;
  int cur_menu_driver;
  unsigned int flags;
  unsigned int panic;
  int hotspots;
  char *route_map;

  struct vdu_s
  {
    int width;
    int height;
  } vdu;
} ro_glbs_t;

extern ro_glbs_t ro;
extern menu_t drivers_menu;
extern menu_t panic_menu;
extern menu_t iconbar_menu;
extern menu_t route_map_menu;

void action_panic(void);
void open_window_pane(int front, int win, int *blk, _kernel_swi_regs *r);
void update_player_opt_win(void);
void update_usb_opt_win(void);
void clear_connections(void);
void save_config(char *filename);

#endif
