common 7.63 KB
Newer Older
Richard Leggett's avatar
Richard Leggett committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*---------------------------------------------------------------------------*/
16
/* File:    common.c                                                         */
Richard Leggett's avatar
Richard Leggett committed
17 18 19 20 21
/* Purpose: Commonly used Wimp routines                                      */
/*---------------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include "kernel.h"
22 23
#include "tboxlibs/wimplib.h"
#include "tboxlibs/toolbox.h"
Richard Leggett's avatar
Richard Leggett committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "swis.h"
#include "common.h"

/* Global messages file descriptor and string pointer */
MessagesFD    *message_block;
char          *message_buffer;


/*---------------------------------------------------------------------------*
 * error_trap                                                                *
 *                                                                           *
 * Wrap around a function to report an error if one occurred eg.:            *
 *  error_trap(event_initialise(&idb), 0);                                   *
 *                                                                           *
 * In: err - a kernel_oserror block                                          *
 *     type - 0 means non-fatal, otherwise program will exit after reporting *
 *---------------------------------------------------------------------------*/

void error_trap(_kernel_oserror *err, int err_type)
{
    /* Report the appropriate error, has one occured */
    if (err != NULL)
    {
47
        wimp_report_error(err, 0, messages_lookup("_TaskName"), 0, 0, 0);
Richard Leggett's avatar
Richard Leggett committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

        /* Now, if it was a fatal error (type != 0), exit at once */
        if (err_type != 0) exit(0);
    }
}


/*---------------------------------------------------------------------------*
 * messages_register                                                         *
 *                                                                           *
 * Registers the message file descriptor with the library so that it knows   *
 * where to find the message block for lookups.                              *
 *                                                                           *
 * In:                                                                       *
 *---------------------------------------------------------------------------*/

void messages_register(MessagesFD *messagefd_point, char *messagebuff_point)
{
  message_block = messagefd_point;
  message_buffer = messagebuff_point;
}


/*---------------------------------------------------------------------------*
 * messages_lookup                                                           *
 *                                                                           *
 * Searches the 'messages' file for the text represented by the token string *
 * and returns a pointer to a buffer storing this text                       *
 *                                                                           *
 * In:                                                                       *
 *---------------------------------------------------------------------------*/

80
char *messages_lookup(const char *token_string)
Richard Leggett's avatar
Richard Leggett committed
81 82 83 84 85 86 87 88 89 90 91 92 93
{
  _kernel_swi_regs regs;

  regs.r[0] = (int)message_block;
  regs.r[1] = (int)token_string;
  regs.r[2] = (int)message_buffer;
  regs.r[3] = 255;
  _kernel_swi(MessageTrans_Lookup, &regs, &regs);

  return message_buffer;
}


94
char *messages_lookup_with_parameter(char *token_string, char *parameter)
Richard Leggett's avatar
Richard Leggett committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
{
  _kernel_swi_regs regs;

  regs.r[0] = (int)message_block;
  regs.r[1] = (int)token_string;
  regs.r[2] = (int)message_buffer;
  regs.r[3] = 255;
  regs.r[4] = (int)parameter;
  _kernel_swi(MessageTrans_Lookup, &regs, &regs);

  return message_buffer;
}


/*---------------------------------------------------------------------------*
 * common_read_screensize                                                    *
 *                                                                           *
112
 * Return the size of the screen in OS units                                 *
Richard Leggett's avatar
Richard Leggett committed
113 114
 *---------------------------------------------------------------------------*/

115
_kernel_oserror *common_read_screensize(int *x, int *y)
Richard Leggett's avatar
Richard Leggett committed
116
{
117
    _kernel_oserror *e;
Richard Leggett's avatar
Richard Leggett committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    int              xeig, yeig, xpix, ypix;

    e=_swix(OS_ReadModeVariable, _INR(0,1)|_OUT(2), -1, 4, &xeig); if (e) return e;
    e=_swix(OS_ReadModeVariable, _INR(0,1)|_OUT(2), -1, 5, &yeig); if (e) return e;
    e=_swix(OS_ReadModeVariable, _INR(0,1)|_OUT(2), -1, 11, &xpix); if (e) return e;
    e=_swix(OS_ReadModeVariable, _INR(0,1)|_OUT(2), -1, 12, &ypix); if (e) return e;

    *x = xpix << xeig;
    *y = ypix << yeig;

    return NULL;
}


/*---------------------------------------------------------------------------*
 * common_error                                                              *
 *                                                                           *
 * Given a string, return a kernel_oserror compatible error.                 *
 *---------------------------------------------------------------------------*/

138
_kernel_oserror *common_error(char *s)
Richard Leggett's avatar
Richard Leggett committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
    static _kernel_oserror e;
    e.errnum=0;
    strcpy(e.errmess, s);

    return &e;
}


/*---------------------------------------------------------------------------*
 * strncmpa                                                                  *
 *                                                                           *
 * Compare n letters of a string, case insensitive.                          *
 *                                                                           *
 * In: str1 -> first string                                                  *
 *     str2 -> second string                                                 *
 *     size = number of characters to compare.                               *
 *                                                                           *
 * Returns: 0 if strings equal, otherwise number of characters they differ   *
 *          by.                                                              *
 *---------------------------------------------------------------------------*/

161
int strncmpa(char *str1, char *str2, int size)
Richard Leggett's avatar
Richard Leggett committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
    int n;
    int equal=size;

    for (n=0; n<size; n++)
    {
        if (str1[n]==str2[n]) equal--;
        else if (str1[n]==str2[n]-32) equal--;
        else if (str1[n]-32==str2[n]) equal--;
    }

    return equal;
}


/*---------------------------------------------------------------------------*
 * read_cmos_value                                                           *
 *                                                                           *
 * Read a byte of CMOS ram                                                   *
 *                                                                           *
 * In: location = CMOS location to read                                      *
 *                                                                           *
 * Returns: contents of location.                                            *
 *---------------------------------------------------------------------------*/

int read_cmos_value(int location)
{
    int result;
    _swix(OS_Byte, _INR(0,1)|_OUT(2), 161, location, &result);
    return result;
}