/* Copyright 1997 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.
 */
/*************************************************/
/* File   : Utils.h                              */
/* Purpose: Infrequently altered utilities       */
/* Author : A.D.Hodgkinson                       */
/* History: 18-Oct-96: Created                   */
/*************************************************/

/* Useful macros */

#define Beep          (_swi(0x107,0))
#define StrNCpy0(x,y) {strncpy(x,y,sizeof(x)-1); x[sizeof(x)-1]=0;}
#define WordAlign(a)  (void *) ((((unsigned int) (a)) + (sizeof(int) - 1)) & (~(sizeof(int) - 1)))

#define ChkError(e)   (show_error_cont(e))
#define RetError(fn)  {_kernel_oserror * utils_error = (fn); if (utils_error) return utils_error;}

#ifdef TRACE
  #define ChkTrace(e) (show_error_cont(e))
#else
  #define ChkTrace(e) (e)
#endif

/* Known error numbers */

#define Utils_Error_OS_Escape      17              /* Offers only 'Continue'      */
#define Utils_Error_Custom_Normal  (1u<<30)        /* Offers 'Continue' and 'Quit */
#define Utils_Error_Custom_Fatal   ((1u<<30) + 1)  /* Offers only 'Quit'          */
#define Utils_Error_Custom_Message ((1u<<30) + 2)  /* Offers only 'Continue'      */

/* Miscellaneous function prototypes */

char            * lookup_token                     (char * s, int flag, char * arg);
char            * lookup_choice                    (char * s, int flag, char * arg);

void              show_error                       (_kernel_oserror * e);
void              show_error_cont                  (_kernel_oserror * e);
void              show_error_ret                   (_kernel_oserror * e);
int               report_toolbox_error             (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);

void              make_no_fetch_memory_error       (int stage);
void              make_no_cont_memory_error        (int stage);
void              make_no_table_memory_error       (int stage);
void              make_no_memory_error             (int stage);

void              show_centred                     (ObjectId o);
_kernel_oserror * set_corrected_extent             (unsigned int f, ObjectId o, BBox * w);
int               find_behind                      (int w);
_kernel_oserror * find_tool_sizes                  (int * theight, int * hheight, int * vwidth);

void              register_null_claimant           (int eventcode, WimpEventHandler * handler, browser_data * handle);
void              deregister_null_claimant         (int eventcode, WimpEventHandler * handler, browser_data * handle);

BBox            * intersection                     (BBox * a, BBox * b);
BBox            * set_graphics_intersection        (BBox * rbox, BBox * cbox);
void              restore_graphics_intersection    (BBox * cbox);

void              read_os_to_points                (void);
void              convert_pair_to_os               (int x, int y, int * osx, int * osy);
void              convert_pair_to_points           (int x, int y, int * mpx, int * mpy);
void              convert_to_os                    (int x, int * osx);
void              convert_to_points                (int x, int * mpx);
void              convert_box_to_os                (const BBox * mp, BBox * os);
void              convert_box_to_points            (const BBox * os, BBox * mp);

_kernel_oserror * read_sprite_size                 (char * name, int * width, int * height);
_kernel_oserror * set_gadget_state                 (ObjectId o, ComponentId c, int grey_state);

void              anti_twitter                     (WimpRedrawWindowBlock * r);

int               adjust                           (void);

int               hide_gadget                      (ObjectId o, ComponentId c);
int               show_gadget                      (ObjectId o, ComponentId c);
int               gadget_hidden                    (ObjectId o, ComponentId c);
void              slab_gadget                      (ObjectId o, ComponentId c);
void              slab_gadget_in                   (ObjectId o, ComponentId c);
void              slab_gadget_out                  (ObjectId o, ComponentId c);

_kernel_oserror * copy_toolaction_info             (ObjectId src_o, ComponentId src_c, ObjectId dst_o, ComponentId dst_c);
_kernel_oserror * set_window_flags                 (int window_handle, unsigned int clear_word, unsigned int eor_word);

int               debounce_keypress                (void);

int               task_from_window                 (int window_handle);
int               is_known_browser                 (browser_data * b);

int               encode_base64                    (const char * in, int len, char * out);

_kernel_oserror * utils_stop_webserv               (void);