main 13.8 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright 1996 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.
 */
15
/*main.c - entry points for DrawFile module */
Neil Turton's avatar
Neil Turton committed
16

17
/*From CLib */
Neil Turton's avatar
Neil Turton committed
18 19 20 21 22 23 24
#include <kernel.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

25
/*From OSLib */
Neil Turton's avatar
Neil Turton committed
26 27 28 29 30 31 32
#include "messagetrans.h"
#include "macros.h"
#include "os.h"
#include "osfile.h"
#include "osmodule.h"
#include "resourcefs.h"

33
/*From Support */
Neil Turton's avatar
Neil Turton committed
34 35
#include "m.h"
#include "riscos.h"
36
#include "jc_trace.h"
Neil Turton's avatar
Neil Turton committed
37

38
/*Local */
Neil Turton's avatar
Neil Turton committed
39 40 41 42 43 44 45
#include "bbox.h"
#include "declare.h"
#include "main.h"
#include "files.h"
#include "render.h"
#include "verify.h"

46 47 48 49 50 51
typedef struct
{
  int object_type;
  callback_fn *fn;
}
Association;
Neil Turton's avatar
Neil Turton committed
52 53 54 55 56

static messagetrans_control_block Control_Block;

static char *Message_Data;

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static Association Verification_Table[] =
{
  {drawfile_TYPE_FONT_TABLE, &verify_font_table},
  {drawfile_TYPE_GROUP, &verify_group},
  {drawfile_TYPE_TEXT, &verify_text},
  {drawfile_TYPE_TAGGED, &verify_tagged},
  {drawfile_TYPE_TRFM_TEXT, &verify_trfm_text}
};

static Association Rendering_Table[] =
{
  {drawfile_TYPE_FONT_TABLE, &render_font_table},
  {drawfile_TYPE_TEXT, &render_text},
  {drawfile_TYPE_TEXT, &render_text_system},
  {drawfile_TYPE_PATH, &render_path},
  {drawfile_TYPE_SPRITE, &render_sprite},
  {drawfile_TYPE_GROUP, &render_group},
  {drawfile_TYPE_TAGGED, &render_tagged},
  {drawfile_TYPE_TEXT_AREA, &render_text_area},
  {drawfile_TYPE_TRFM_TEXT, &render_trfm_text},
  {drawfile_TYPE_TRFM_TEXT, &render_trfm_text_system},
  {drawfile_TYPE_TRFM_SPRITE, &render_trfm_sprite},
  {drawfile_TYPE_JPEG, &render_jpeg}
};

static Association Declaration_Table[] =
{
  {drawfile_TYPE_FONT_TABLE, &declare_font_table},
  {drawfile_TYPE_GROUP, &declare_group},
  {drawfile_TYPE_TAGGED, &declare_tagged},
  {drawfile_TYPE_TEXT_AREA, &declare_text_area}
};

static os_trfm Identity =
{
  {
    {0x10000, 0},
    {0, 0x10000},
    {0, 0}}};

callback_l main_verification_callback = NULL, main_render_callback = NULL, main_declaration_callback = NULL;
Neil Turton's avatar
Neil Turton committed
98 99 100

#ifdef USE_ATEXIT
/*------------------------------------------------------------------------*/
101 102 103 104
static void Exit(void)
{
  (void) main_terminate(SKIP, SKIP, SKIP);
}
Neil Turton's avatar
Neil Turton committed
105 106
#endif
/*------------------------------------------------------------------------*/
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static os_error *Create_List(Association table[], int count,
                             callback_l * l_out)

{
  os_error *error = NULL;
  int i;
  callback_l l;
  bool done_new = FALSE;

  tracef("main_initialise: creating callback list\n");
  if ((error = callback_new(&l)) != NULL)
    goto finish;
  done_new = TRUE;

  for (i = 0; i < count; i++)
    if ((error = callback_register(l, table[i].fn, NULL, 1,
                                   table[i].object_type)) != NULL)
Neil Turton's avatar
Neil Turton committed
124 125
      goto finish;

126 127
  if (l_out != NULL)
    *l_out = l;
Neil Turton's avatar
Neil Turton committed
128 129

finish:
130 131 132 133 134
  if (error != NULL)
  {
    if (done_new)
      (void) callback_delete(l);
  }
Neil Turton's avatar
Neil Turton committed
135

136
  return error;
Neil Turton's avatar
Neil Turton committed
137 138
}
/*------------------------------------------------------------------------*/
139 140
_kernel_oserror *main_initialise(char *tail, int podule_base,
                                 void *workspace)
Neil Turton's avatar
Neil Turton committed
141

142 143 144 145
{
  os_error *error = NULL;
  int size;
  bool done_open_file = FALSE;
Neil Turton's avatar
Neil Turton committed
146
#ifdef STANDALONE
147
  bool done_register_files = FALSE;
Neil Turton's avatar
Neil Turton committed
148
#endif
149 150
  bits flags;
  char *message_file_name;
Neil Turton's avatar
Neil Turton committed
151

152
  trace_initialise("DrawFile$Trace");
Neil Turton's avatar
Neil Turton committed
153

154 155
  tracef("main_initialise\n");
  tail = tail, podule_base = podule_base, workspace = workspace;
Neil Turton's avatar
Neil Turton committed
156

157 158
  if ((error = riscos_territory(territory_CURRENT)) != NULL)
    goto finish;
Neil Turton's avatar
Neil Turton committed
159 160

#ifdef STANDALONE
161 162 163 164 165 166
/* Register files. */
  tracef("main_initialise: registering messages from data at 0x%X\n" _
         files_messages());
  if ((error = xresourcefs_register_files(files_messages())) != NULL)
    goto finish;
  done_register_files = TRUE;
Neil Turton's avatar
Neil Turton committed
167 168
#endif

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  tracef("main_initialise: looking up DrawFile$Path\n");
  message_file_name = getenv("DrawFile$Path") == NULL ?
      "Resources:Resources.DrawFile.Messages" : "DrawFile:Messages";

  if ((error = xmessagetrans_file_info(message_file_name, &flags, &size))
      != NULL)
    goto finish;

  if ((flags & messagetrans_DIRECT_ACCESS) != 0)
    Message_Data = NULL;
  else
  {
    if ((Message_Data = m_ALLOC(size)) == NULL)
    {
      error = main_error_lookup(1, "NoMem");
Neil Turton's avatar
Neil Turton committed
184
      goto finish;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    }
  }

  if ((error = xmessagetrans_open_file(&Control_Block, message_file_name,
                                       Message_Data)) != NULL)
    goto finish;
  done_open_file = TRUE;

/* Make the verification callback list. */
  tracef("main_initialise: creating verification callback list\n");
  if ((error = Create_List(Verification_Table,
           COUNT(Verification_Table), &main_verification_callback)) != NULL ||
      (error = Create_List(Rendering_Table,
                    COUNT(Rendering_Table), &main_render_callback)) != NULL ||
      (error = Create_List(Declaration_Table,
               COUNT(Declaration_Table), &main_declaration_callback)) != NULL)
    goto finish;
Neil Turton's avatar
Neil Turton committed
202 203

#ifdef USE_ATEXIT
204
  atexit(&Exit);
Neil Turton's avatar
Neil Turton committed
205 206 207
#endif

finish:
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  if (error != NULL)
  {
    callback_delete(main_declaration_callback);
    callback_delete(main_render_callback);
    callback_delete(main_verification_callback);

    if (done_open_file)
    {
      os_error *error1 = xmessagetrans_close_file(&Control_Block);
      if (error == NULL)
        error = error1;
    }

    m_FREE(Message_Data, size);

       #ifdef STANDALONE
    if (done_register_files)
    {
      os_error *error1 = xresourcefs_deregister_files
      ((resourcefs_file_list *) files_messages());
      if (error == NULL)
        error = error1;
    }
       #endif
  }

  return (_kernel_oserror *) error;
}
/*------------------------------------------------------------------------*/
_kernel_oserror *main_terminate(int fatal, int instance, void *workspace)
Neil Turton's avatar
Neil Turton committed
238

239 240
{
  fatal = fatal, instance = instance, workspace = workspace;
Neil Turton's avatar
Neil Turton committed
241

242 243 244 245 246 247 248 249 250 251
  tracef("main_terminate\n");
  callback_delete(main_declaration_callback);
  callback_delete(main_render_callback);
  callback_delete(main_verification_callback);
  (void) xmessagetrans_close_file(&Control_Block);
  m_FREE(Message_Data, 0);
#ifdef STANDALONE
  (void) xresourcefs_deregister_files(files_messages());
#endif
  trace_terminate();
Neil Turton's avatar
Neil Turton committed
252

253
  return NULL;
Neil Turton's avatar
Neil Turton committed
254 255
}
/*------------------------------------------------------------------------*/
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
_kernel_oserror *main_swi(int swi, _kernel_swi_regs * regs, void *workspace)

{
  os_error *error = NULL;

  tracef("main_swi\n");
  workspace = workspace;

  if ((error = verify_diagram((drawfile_diagram *) regs->r[1],
                              regs->r[2])) != NULL)
    goto finish;

  switch (swi)
  {
  case DrawFile_Render & 0x1F:
    tracef("main_swi: despatching Render\n");
    if ((error = render((bits) regs->r[0],
                        (drawfile_diagram *) regs->r[1], regs->r[2],
                        regs->r[3] != 0 ? (os_trfm *) regs->r[3] : &Identity,
                        (os_box *) regs->r[4], regs->r[5])) != NULL)
      goto finish;
    break;

  case DrawFile_BBox & 0x1F:
    tracef("main_swi: despatching BBox\n");
    if ((error = bbox((bits) regs->r[0],
                      (drawfile_diagram *) regs->r[1], regs->r[2],
                      regs->r[3] != 0 ? (os_trfm *) regs->r[3] : &Identity,
                      (os_box *) regs->r[4])) != NULL)
      goto finish;
    break;
Neil Turton's avatar
Neil Turton committed
287

288 289 290 291 292 293
  case DrawFile_DeclareFonts & 0x1F:
    tracef("main_swi: despatching DeclareFonts\n");
    if ((error = declare((bits) regs->r[0],
                         (drawfile_diagram *) regs->r[1], regs->r[2])) != NULL)
      goto finish;
    break;
Neil Turton's avatar
Neil Turton committed
294

295 296 297 298 299
  default:
    error = main_error_lookup(0x1E8, "BadSwi", "DrawFile");
    goto finish;
    break;
  }
Neil Turton's avatar
Neil Turton committed
300

301 302
finish:
  return (_kernel_oserror *) error;
Neil Turton's avatar
Neil Turton committed
303 304
}
/*------------------------------------------------------------------------*/
305 306 307 308 309
_kernel_oserror *main_service(int service, _kernel_swi_regs * regs,
                              void *workspace)
{
  bool unclaimed = TRUE;
  os_error *error = NULL;
Neil Turton's avatar
Neil Turton committed
310

311
  workspace = workspace;
Neil Turton's avatar
Neil Turton committed
312

313 314 315 316 317 318 319 320 321 322
  switch (service)
  {
  case Service_ResourceFSStarting:
#ifdef STANDALONE
    tracef("main_service: servicing ResourceFSStarting\n");
    (*(void (*)(resourcefs_file_list *, int, int, int)) regs->r[2])
        (files_messages(), regs->r[1], regs->r[2],
         regs->r[3]);
#endif
    break;
Neil Turton's avatar
Neil Turton committed
323

324 325 326 327 328 329
  case DrawFile_Render:
    switch (regs->r[0])
    {
    case drawfile_TYPE_FONT_TABLE:
      render_font_table(NULL, (void *) regs->r[2], &unclaimed);
      break;
Neil Turton's avatar
Neil Turton committed
330

331 332
    case drawfile_TYPE_TEXT:
      render_text(NULL, (void *) regs->r[2], &unclaimed);
Neil Turton's avatar
Neil Turton committed
333 334
      break;

335 336
    case drawfile_TYPE_PATH:
      render_path(NULL, (void *) regs->r[2], &unclaimed);
Neil Turton's avatar
Neil Turton committed
337 338
      break;

339 340
    case drawfile_TYPE_SPRITE:
      render_sprite(NULL, (void *) regs->r[2], &unclaimed);
Neil Turton's avatar
Neil Turton committed
341 342
      break;

343 344
    case drawfile_TYPE_GROUP:
      render_group(NULL, (void *) regs->r[2], &unclaimed);
Neil Turton's avatar
Neil Turton committed
345 346
      break;

347 348 349
    case drawfile_TYPE_TAGGED:
      render_tagged(NULL, (void *) regs->r[2], &unclaimed);
      break;
Neil Turton's avatar
Neil Turton committed
350

351 352 353
    case drawfile_TYPE_TEXT_AREA:
      render_text_area(NULL, (void *) regs->r[2], &unclaimed);
      break;
Neil Turton's avatar
Neil Turton committed
354

355 356 357 358 359 360 361 362 363 364
    case drawfile_TYPE_TRFM_TEXT:
      render_trfm_text(NULL, (void *) regs->r[2], &unclaimed);
      break;

    case drawfile_TYPE_TRFM_SPRITE:
      render_trfm_sprite(NULL, (void *) regs->r[2], &unclaimed);
      break;

    case drawfile_TYPE_JPEG:
      render_jpeg(NULL, (void *) regs->r[2], &unclaimed);
Neil Turton's avatar
Neil Turton committed
365
      break;
366 367 368 369
    }
    if (!unclaimed)
      regs->r[1] = 0;
    break;
Neil Turton's avatar
Neil Turton committed
370

371 372 373 374 375 376
  default:
    error = main_error_lookup(0x1E8, "BadService", "DrawFile");
    break;
  }

  return((_kernel_oserror *) error);
Neil Turton's avatar
Neil Turton committed
377 378
}
/*------------------------------------------------------------------------*/
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
_kernel_oserror *main_command(char *tail, int argc, int cmd_no,
                              void *workspace)
{
  os_error *error = NULL;

  tracef("main_command\n");

  switch (cmd_no)
  {
  case main_COMMAND_RENDER:
    {                                  /* Render */
      os_trfm m;
      struct
      {
        char *file, *m[3][2];
        bool bbox, suppress;
        char *flatness;
        char argd[1024];
Neil Turton's avatar
Neil Turton committed
397
      }
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
      argl;
      int obj_type, size, flatness, i, j;
      drawfile_diagram *diagram = NULL;

      static int Scale[3][2] =
      {
        {1 << 16, 1 << 16},
        {1 << 16, 1 << 16},
        {draw_OS_UNIT, draw_OS_UNIT}};

      NOT_USED(argc)
          NOT_USED(workspace)

          tracef("tail \"%.*s\"\n" _ riscos_strlen(tail) _ tail);

      if ((error = xos_read_args("file/a,m00,m01,m10,m11,m20,m21,"
                                 "bbox/s,suppress/s,flatness/k", tail,
                                 (char *) &argl, sizeof argl, NULL)) != NULL)
        goto rendered;

      tracef("decoding is:\nfile \"%s\"\nm %s %s %s %s %s %s\n"
             "bbox %s\nsuppress %s\nflatness %s\n" _
             argl.file _ argl.m[0][0] != NULL ? argl.m[0][0] : "NULL" _
             argl.m[0][1] != NULL ? argl.m[0][1] : "NULL" _
             argl.m[1][0] != NULL ? argl.m[1][0] : "NULL" _
             argl.m[1][1] != NULL ? argl.m[1][1] : "NULL" _
             argl.m[2][0] != NULL ? argl.m[2][0] : "NULL" _
             argl.m[2][1] != NULL ? argl.m[2][1] : "NULL" _
             WHETHER(argl.bbox) _ WHETHER(argl.suppress) _
             argl.flatness != NULL ? argl.flatness : "NULL");

      if ((error = xosfile_read_no_path(argl.file, &obj_type, NULL, NULL,
                                        &size, NULL)) != NULL)
        goto rendered;
      if (obj_type != osfile_IS_FILE)
      {
        error = xosfile_make_error(argl.file, obj_type);
        goto rendered;
      }

/* Decode the matrix. */
      for (j = 0; j < 2; j++)
        for (i = 0; i < 3; i++)
          if (argl.m[i][j] == NULL || riscos_scan_fixed
              (argl.m[i][j], &m.entries[i][j], Scale[i][j]) ==
              0)
            m.entries[i][j] = Identity.entries[i][j];

      tracef("matrix ((0x%X 0x%X) (0x%X 0x%X) (%d %d))\n" _
             m.entries[0][0] _ m.entries[0][1] _
             m.entries[1][0] _ m.entries[1][1] _
             m.entries[2][0] _ m.entries[2][1]);

      if (argl.flatness == NULL ||
          riscos_scan_fixed(argl.flatness, &flatness, 256) == 0)
        flatness = -1;

      tracef("flatness %d\n" _ flatness);

      if ((diagram = m_ALLOC(size)) == NULL)
      {
        error = main_error_lookup(os_GLOBAL_NO_MEM, "NoMem");
        goto rendered;
      }

      if ((error = xosfile_load_stamped_no_path(argl.file,
                     (byte *) diagram, NULL, NULL, NULL, NULL, NULL)) != NULL)
        goto rendered;

      if ((error = verify_diagram(diagram, size)) != NULL)
        goto rendered;

      if ((error = render((argl.bbox ? drawfile_RENDER_BBOXES : NONE) |
                          (argl.suppress ? drawfile_RENDER_SUPPRESS : NONE) |
                     (flatness != -1 ? drawfile_RENDER_GIVEN_FLATNESS : NONE),
                          diagram, size, &m, NULL, flatness)) != NULL)
        goto rendered;

    rendered:
      m_FREE(diagram, size);
    }
    break;
  }
Neil Turton's avatar
Neil Turton committed
481

482
  return (_kernel_oserror *) error;
Neil Turton's avatar
Neil Turton committed
483 484
}
/*------------------------------------------------------------------------*/
485
os_error *main_error_lookup(int no, char *token,...)
Neil Turton's avatar
Neil Turton committed
486

487 488 489 490 491
{
  va_list list;
  char *p[4];
  int i;
  os_error error;
Neil Turton's avatar
Neil Turton committed
492

493
  tracef("main_error_lookup\n");
Neil Turton's avatar
Neil Turton committed
494

495 496 497 498 499
/* Assume that 4 args are always given. */
  va_start(list, token);
  for (i = 0; i < 4; i++)
    p[i] = va_arg(list, char *);
  va_end(list);
Neil Turton's avatar
Neil Turton committed
500

501 502
  error.errnum = no;
  strcpy(error.errmess, token);
Neil Turton's avatar
Neil Turton committed
503

504 505
  return xmessagetrans_error_lookup(&error, &Control_Block,
                                    NULL, 0, p[0], p[1], p[2], p[3]);
Neil Turton's avatar
Neil Turton committed
506
}