/*
 * Copyright (c) 1996 Mark Brinicombe.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Mark Brinicombe.
 * 4. The name of the company nor the name of the author may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * RiscBSD kernel project
 *
 * cmos.c
 *
 * CMOS RAM configuration window management routines
 *
 * Created      : 19/07/96
 * Last updated : 19/07/96
 */

/* Include standard header files */

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

#include "wimp.h"
#include "Event.h"       /* Event despatcher                             */
#include "Error.h"       /* Error despatcher                             */
#include "Window.h"      /* Window handling automation                   */
#include "Icon.h"        /* Icon handling automation                     */
#include "WimpSWIs.h"    /* Default event handlers                       */
#include "IconExt.h"     /* Icon handling automation                     */
#include "Screen.h"      /* Getting screen size info, etc   */
#include "Msgs.h"
#include "Menu.h"
#include "SWI.h"
#include "dlswis.h"
#include "swiv.h"

#include "icons.h"
#include "prototypes.h"
#include "cmos.h"

/* Declare global variables */

window_handle cmos_window = NULL;

/* Declare external variables */

/* Local function prototypes */

BOOL Cmos_Close(event_pollblock *event, void *reference);
BOOL Cmos_Click(event_pollblock *event, void *reference);
void Cmos_SetIcons(void);

/* Now for the main code */

/*
 * Open the CMOS configuration window
 */

BOOL Cmos_OpenWindow(event_pollblock *event, void *reference)
  {
    window_state wstate;

/* Are we already open ? */

    if (cmos_window)
      {
        Wimp_GetWindowState(cmos_window, &wstate);
        wstate.openblock.behind = -1;
        Wimp_OpenWindow(&(wstate.openblock));
        return(1);
      }

/* Create and open our main windows from the template "control". */

    cmos_window = Window_Create("cmos", 0);

    if (!cmos_window) Msgs_ReportFatal(0x00000000,
      "err.1", "cmos");

/* Claim window-close events for the config window */

    Event_Claim(event_CLOSE, cmos_window, event_ANY, Cmos_Close, NULL);

/* Set the correct state for all the icons */

    Cmos_SetIcons();

/* Add mouse click handlers */

    Event_Claim(event_CLICK, cmos_window, CMOS_WINDOW_ICON_AUTOBOOT,
      Cmos_Click, NULL);
    Event_Claim(event_CLICK, cmos_window, CMOS_WINDOW_ICON_REBOOT,
      Cmos_Click, NULL);

/* Set the correct state for opening */

    Wimp_GetWindowState(cmos_window, &wstate);
    wstate.openblock.behind = -1;
    Wimp_OpenWindow(&(wstate.openblock));
    return(1);
  }


/*
 * Close the configuration window.
 */

BOOL Cmos_Close(event_pollblock *event, void *reference)
  {
    Window_Delete(cmos_window);
    cmos_window = NULL;
    return(1);
  }


/*
 * Set the configuration pane icons to the correct states so that
 * they match the config structure.
 */

void Cmos_SetIcons(void)
  {
    int byte;

    swix(OS_Byte, IN(R0|R1)|OUT(R2), 161, CMOS_BYTE_BOOTCONF, &byte);
    Icon_SetSelect(cmos_window, CMOS_WINDOW_ICON_AUTOBOOT,
      (byte & CMOS_BIT_AUTOBOOT));
    Icon_SetSelect(cmos_window, CMOS_WINDOW_ICON_REBOOT,
      (byte & CMOS_BIT_REBOOT));
  }


BOOL Cmos_Click(event_pollblock *event, void *reference)
  {
    int byte;

    swix(OS_Byte, IN(R0|R1)|OUT(R2), 161, CMOS_BYTE_BOOTCONF, &byte);
    byte &= ~(CMOS_BIT_AUTOBOOT | CMOS_BIT_REBOOT);
    if (Icon_GetSelect(cmos_window, CMOS_WINDOW_ICON_AUTOBOOT))
      byte |= CMOS_BIT_AUTOBOOT;
    if (Icon_GetSelect(cmos_window, CMOS_WINDOW_ICON_REBOOT))
      byte |= CMOS_BIT_REBOOT;
    swix(OS_Byte, IN(R0|R1|R2), 162, CMOS_BYTE_BOOTCONF, byte);
    return(1);
  }

/* End of cmos.c */
