
#include <stdint.h>

#include "swis.h"

#include "Global/NewErrors.h"

#include "usbjoystick.h"
#include "errors.h"


// MessageTrans_ErrorLookup wants an error block of {error number, token},
// not a full _kernel_oserror - the token is a lookup key into
// Resources.UK.Messages, not the message text itself. Keeping the token
// to 7 characters (+ implicit NUL) so it fits in this fixed 8-byte field.
typedef struct {
  uint32_t num;
  const char token[8];
} msgerr;

static const msgerr errors[Error_MAX] =
{
  { ErrorNumber_Syntax,       "Syntax"  },
  { ErrorNumber_BadNoParms,   "BadParm" },
  { ErrorNumber_TooManyParms, "TooMany" },
  { ErrorNumber_NoSuchSWI,    "NoSWI"   },
  { ErrorNumber_ModuleBadSWI, "BadSWI"  },
  { ErrorBase_Joystick + 1,   "BadJoy"  },
  { ErrorBase_Joystick + 2,   "NoUSB"   },
  { ErrorBase_Joystick + 3,   "NotActv" },
  { ErrorBase_Joystick + 4,   "NotOpen" },
  { ErrorBase_Joystick + 5,   "NoChcW"  },
};


_kernel_oserror *geterror(Error e)
{
  // R2=0 tells MessageTrans to use one of its own internal result
  // buffers, and MessageTrans_ErrorLookup always sets V on return - so
  // this call's own "error" result (via _swix's usual _kernel_oserror*
  // return convention) IS the looked-up, ready-to-return error block.
  return _swix(MessageTrans_ErrorLookup, _INR(0,2), &errors[e], &messages, 0);
}


_kernel_oserror *geterror_p(Error e, const char *arg0)
{
  return _swix(MessageTrans_ErrorLookup, _INR(0,2)|_IN(4), &errors[e], &messages, 0, arg0);
}
