/*
**    Name: odbcdm.c
**
**    Date: Sat Jul 19 16:10:20 2003
**
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "kernel.h"
#include "support.h"

#include "odbcdm.h"
#include "ODBCLib.h"

#include "iconbar.h"
#include "quit.h"
#include "event.h"

#include "rmamem.h"

#include "setjmp.h"

jmp_buf ossup_jmp_buf;

int errno;

char *__progname = "ODBC";

int riscosalloc = 0;

extern wimp_t task;              /* Task ID for this task   */

messagetrans_control_block MessageTrans_Control_Block;

int Quit_Application = FALSE;    /* Quit applucation?       */

toolbox_block block;             /* Toolbox block           */

Choices choices;

int main(int argc,char *argv[])
    {
    wimp_event_no event_no;          /* Wimp event number       */

    wimp_block poll_block;           /* Wimp poll block         */

    toolbox_action_list action_list; /* List of events          */

    wimp_message_list message_list;  /* List of messages        */

    os_error *error;

    osspriteop_area *area = NULL;    /* Application sprite area */

    int version;                     /* OS version number       */

    toolbox_o iconbar;               /* Iconbar handle          */

    _kernel_swi_regs regs;

    os_t old_time,
         poll_time;

    char choice[256];

    char *sep;

    FILE *ch;

    NOT_USED(argc);
    NOT_USED(argv);
/*    rmamem_size = 32768;*/

    regs.r[0] = SQL_DMTaskGet;

    _kernel_swi(SQL_DMTask,&regs,&regs);

    if (regs.r[1] != 0)
        {
        exit(0);
        }

    choices.quittime = 5;
    choices.nice     = 1;

    if ((ch = fopen("Choices:ODBC.ODBC","r")) != NULL)
        {
        while (!feof(ch))
            {
            fgets(choice,sizeof(choice),ch);
            sep = strchr(choice,':');

            if (sep != NULL)
                {
                *sep = '\0';
                sep++;
                if (!strcmp(choice,"QuitTime"))
                    {
                    choices.quittime = (int)atol(sep);
                    }
                if (!strcmp(choice,"Nice"))
                    {
                    choices.nice = (int)atol(sep);
                    }
                }
            }

        fclose(ch);
        }

    choices.nicecount = choices.nice;

    action_list.action_nos[0] = 0;
    message_list.messages[0]  = 0;

/* Initialise Toolbox */

    error = xtoolbox_initialise(0,
                               310,
                               &message_list,
                               &action_list,
                               "<ODBC$Dir>.Resources",
                               &MessageTrans_Control_Block,
                               &block,
                               &version,
                               &task,
                               &area);

    REPERRCONT(error);

/* Initialise support */

    init_signals();

/* Initialise eventlib */

    event_initialise(&block);

/* Set event mask */

    event_set_mask(wimp_MASK_LEAVING  |
                   wimp_MASK_ENTERING |
                   wimp_MASK_LOSE     |
                   wimp_MASK_GAIN);

/* Create iconbar icon */

    iconbar = toolbox_create_object((toolbox_create_flags)0,
                                    (toolbox_id)"IconBar");

/* Ensure client handle is null for this object */

    toolbox_set_client_handle((toolbox_handle_flags)0,
                              iconbar,
                              NULL);

/* Register some default handlers for iconbar menu operations */

/* Register iconbar click handler */

    event_register_toolbox_handler(iconbar,
                                   action_ICONBAR_CLICKED,
                                   iconbar_clicked,
                                   NULL);

/* Quit chosen */

    event_register_toolbox_handler((toolbox_o)-1,
                                   action_QUIT_QUIT,
                                   menu_quit,
                                   NULL);

/* Reset chosen */

    event_register_toolbox_handler((toolbox_o)-1,
                                   odbcdm_RESET,
                                   menu_reset,
                                   NULL);

/* Choices chosen */

    event_register_toolbox_handler((toolbox_o)-1,
                                   odbcdm_CHOICES,
                                   menu_choices,
                                   NULL);

/* Help chosen */

    event_register_toolbox_handler((toolbox_o)-1,
                                   odbcdm_HELP,
                                   menu_help,
                                   NULL);

/* Logs chosen */

    event_register_toolbox_handler((toolbox_o)-1,
                                   odbcdm_LOGS,
                                   menu_logs,
                                   NULL);

/* Null handler */

    event_register_wimp_handler((toolbox_o)-1,
                                wimp_NULL_REASON_CODE,
                                null_reason_code,
                                NULL);

/* Message handlers */
/* Quit message*/

    event_register_message_handler(message_QUIT,
                                   quit,
                                   NULL);

/* Query pending */

    event_register_message_handler(message_SQL_PENDING,
                                   message_pending,
                                   NULL);

    regs.r[0] = SQL_DMTaskSet;
    regs.r[1] = (int)task;
    regs.r[2] = 100;

    _kernel_swi(SQL_DMTask,&regs,&regs);

    regs.r[0] = SQL_QueryStatusSet;
    regs.r[1] = ODBC_STATE_READY;

    _kernel_swi(SQL_QueryStatus,&regs,&regs);

/* Poll loop */

    poll_time = os_read_monotonic_time();
    old_time  = poll_time;

    if (setjmp(ossup_jmp_buf) == 2)
        {
        Quit_Application = TRUE;
        }

    while (Quit_Application == FALSE)
        {
        poll_time = os_read_monotonic_time();

        while ((poll_time - old_time) > 0)
            {
            old_time += 500;
            }

        event_poll_idle(&event_no,
                   &poll_block,
                   old_time,
                   0);

        }

    regs.r[0] = SQL_QueryStatusSet;
    regs.r[1] = ODBC_STATE_NOSERVER;

    _kernel_swi(SQL_QueryStatus,&regs,&regs);

    regs.r[0] = SQL_DMTaskSet;
    regs.r[1] = 0;

    _kernel_swi(SQL_DMTask,&regs,&regs);

/* Deregister iconbar click handler */

    event_deregister_toolbox_handler(iconbar,
                                   action_ICONBAR_CLICKED,
                                   iconbar_clicked,
                                   NULL);

/* Quit chosen */

    event_deregister_toolbox_handler((toolbox_o)-1,
                                   action_QUIT_QUIT,
                                   menu_quit,
                                   NULL);

/* Null handler */

    event_deregister_wimp_handler((toolbox_o)-1,
                                wimp_NULL_REASON_CODE,
                                null_reason_code,
                                NULL);

/* Message handlers */
/* Quit message*/

    event_deregister_message_handler(message_QUIT,
                                   quit,
                                   NULL);

/* Reset chosen */

    event_deregister_toolbox_handler((toolbox_o)-1,
                                   odbcdm_RESET,
                                   menu_reset,
                                   NULL);
/* Choices chosen */

    event_deregister_toolbox_handler((toolbox_o)-1,
                                   odbcdm_CHOICES,
                                   menu_choices,
                                   NULL);

/* Help chosen */

    event_deregister_toolbox_handler((toolbox_o)-1,
                                   odbcdm_HELP,
                                   menu_help,
                                   NULL);
/* Logs chosen */

    event_deregister_toolbox_handler((toolbox_o)-1,
                                   odbcdm_LOGS,
                                   menu_logs,
                                   NULL);
/* Query pending */

    event_deregister_message_handler(message_SQL_PENDING,
                                   message_pending,
                                   NULL);

    exit(0);
    }
