Mouse 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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   : Mouse.c                                */
17
/*                                                 */
18
/* Purpose: Mouse pointer related functions for    */
19 20
/*          the browser.                           */
/*                                                 */
21
/* Author : A.D.Hodgkinson                         */
22 23
/*                                                 */
/* History: 14-Mar-97: Created.                    */
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/***************************************************/

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

#include "swis.h"
#include "kernel.h"

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

#include "Handlers.h"

#include "Mouse.h"

/* Locals */

static int mouse_watch = 0;

static int mouse_x;
static int mouse_y;
static int mouse_time;
static int mouse_on    = 1;
static int mouse_shape = 1;
static int mouse_used  = 0;

/*************************************************/
/* mouse_watch_pointer_control()                 */
/*                                               */
/* Controls watching movement of the mouse       */
/* pointer; if turned on, when the pointer moves */
/* the function will ensure that the pointer is  */
/* also visible.                                 */
/*                                               */
/* Parameters: 1 to turn watching on, 0 to turn  */
/*             it off.                           */
/*************************************************/

void mouse_watch_pointer_control(int on)
{
  if (on)
  {
    if (!mouse_watch)
    {
      mouse_x = mouse_y = mouse_time = -1;
      register_null_claimant(Wimp_ENull, mouse_watch_pointer, NULL);
      mouse_watch = 1;
    }
  }
  else
  {
    if (mouse_watch)
    {
      deregister_null_claimant(Wimp_ENull, mouse_watch_pointer, NULL);
      mouse_watch = 0;
    }
  }
}

/*************************************************/
/* mouse_watch_pointer()                         */
/*                                               */
/* Watches to see if the mouse pointer moves,    */
/* and if so, ensures the pointer is visible.    */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
/*************************************************/

int mouse_watch_pointer(int eventcode, WimpPollBlock * block, IdBlock * idb, void * handle)
{
  WimpGetPointerInfoBlock i;

  if (!wimp_get_pointer_info(&i))
  {
    if (mouse_x    < 0) mouse_x = i.x;
    if (mouse_y    < 0) mouse_y = i.y;
    if (mouse_time < 0) _swix(OS_ReadMonotonicTime, _OUT(0), &mouse_time);

    if (mouse_x != i.x || mouse_y != i.y)
    {
      if (!mouse_on) mouse_pointer_on();

      mouse_x = mouse_y = mouse_time = -1;

      mouse_used = 1;
    }
    else if (mouse_on)
    {
      int t;

      _swix(OS_ReadMonotonicTime, _OUT(0), &t);

      if (t - mouse_time > 500) mouse_pointer_off();
    }
  }

  return 0;
}

/*************************************************/
/* mouse_pointer_on()                            */
/*                                               */
/* Turns the mouse pointer on immediately.       */
/*************************************************/

void mouse_pointer_on(void)
{
  if (!mouse_on)
  {
    mouse_x = mouse_y = mouse_time = -1;
    mouse_on = 1;
    mouse_set_pointer_shape(mouse_shape);
  }
}

/*************************************************/
/* mouse_pointer_off()                           */
/*                                               */
/* Turns the mouse pointer off immediately.      */
/*************************************************/

void mouse_pointer_off(void)
{
  if (mouse_on && !mouse_used)
  {
    mouse_x = mouse_y = mouse_time = -1;
    mouse_set_pointer_shape(Mouse_Shape_Off);
    mouse_on = 0;
  }
}

/*************************************************/
/* mouse_force_unused()                          */
/*                                               */
/* Resets the internal mouse_used flag back to   */
/* zero, so that the mouse pointer may be        */
/* turned off (otherwise the user has moved the  */
/* pointer and it shouldn't be switched off).    */
/*************************************************/

void mouse_force_unused(void)
{
  mouse_used = 0;
}

/*************************************************/
/* mouse_set_pointer_shape()                     */
/*                                               */
/* Sets the shape of the pointer; shape 0 turns  */
/* it off. If mouse_on is set to zero, this will */
/* *not* override that setting.                  */
/*                                               */
/* Parameters: Pointer shape; see Mouse.h for    */
/*             definitions of shapes.            */
/*************************************************/

void mouse_set_pointer_shape(int shape)
{
  if (shape) mouse_shape = shape;

  /* If the mouse is turned off, don't turn it back on... */

  if (shape && !mouse_on) return;

  /* Otherwise change to the required shape */

  switch (shape)
  {
    case Mouse_Shape_Off:    _swix(OS_CLI,
                                   _IN(0),

                                   "Pointer 0");
    break;

    default: /* Catchall - use normal pointer, so no 'break' */

211 212
    case Mouse_Shape_Normal:    _swix(OS_CLI,
                                      _IN(0),
213

214
                                      "Pointer 1");
215 216
    break;

217 218 219 220 221 222 223
    case Mouse_Shape_Link:      _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
                                      "ptr_link",
                                      2,
224 225
                                      controls.ptrlnkactvx,
                                      controls.ptrlnkactvy,
226 227
                                      0,
                                      0);
228 229
    break;

230 231 232 233 234 235 236
    case Mouse_Shape_Map:       _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
                                      "ptr_map",
                                      2,
237 238
                                      controls.ptrmapactvx,
                                      controls.ptrmapactvy,
239 240
                                      0,
                                      0);
241 242
    break;

243 244 245 246 247 248 249
    case Mouse_Shape_UD:        _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
                                      "ptr_ud",
                                      2,
250 251
                                      controls.ptrudactvx,
                                      controls.ptrudactvy,
252 253
                                      0,
                                      0);
254 255
    break;

256 257 258 259 260 261 262
    case Mouse_Shape_LR:        _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
                                      "ptr_lr",
                                      2,
263 264
                                      controls.ptrlractvx,
                                      controls.ptrlractvy,
265 266
                                      0,
                                      0);
267 268
    break;

269 270 271 272 273 274 275
    case Mouse_Shape_UDLR:      _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
                                      "ptr_udlr",
                                      2,
276 277
                                      controls.ptrudlractvx,
                                      controls.ptrudlractvy,
278 279
                                      0,
                                      0);
280 281
    break;

282 283 284 285 286 287 288
    case Mouse_Shape_NoResize:  _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
                                      "ptr_noresize",
                                      2,
289 290
                                      controls.ptrnoractvx,
                                      controls.ptrnoractvy,
291 292 293 294 295 296 297 298 299
                                      0,
                                      0);
    break;

    case Mouse_Shape_ToScroll:  _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
300
                                      "ptr_toscroll",
301
                                      2,
302 303
                                      controls.ptrtosactvx,
                                      controls.ptrtosactvy,
304 305 306 307 308 309 310 311 312
                                      0,
                                      0);
    break;

    case Mouse_Shape_Scrolling: _swix(OS_SpriteOp,
                                      _INR(0,7),

                                      292,
                                      sprite_block,
313
                                      "ptr_scroll",
314
                                      2,
315 316
                                      controls.ptrscractvx,
                                      controls.ptrscractvy,
317 318
                                      0,
                                      0);
319 320 321 322 323 324 325 326 327
    break;
  }
}

/*************************************************/
/* mouse_pointer_is_on()                         */
/*                                               */
/* States whether the pointer is on or off.      */
/*                                               */
328
/* Returns:    1 if the pointer is on, else 0.   */
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/*************************************************/

int mouse_pointer_is_on(void)
{
  if (mouse_on) return 1;

  return 0;
}

/*************************************************/
/* mouse_to()                                    */
/*                                               */
/* Moves the mouse to a given screen coordinate. */
/*                                               */
/* Parameters: X coordinate to move to;          */
344
/*                                               */
345
/*             Y coordinate to move to;          */
346
/*                                               */
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*             1 to always move, else only move  */
/*             if mouse_used is zero (so it      */
/*             looks as if the user has not yet  */
/*             used or does not have a mouse).   */
/*************************************************/

void mouse_to(int x, int y, int now)
{
  char block[5];

  if (!now && mouse_used) return;

  block[0] = 3;
  block[1] = x & 0xff;
  block[2] = x >> 8;
  block[3] = y & 0xff;
  block[4] = y >> 8;

  _swix(OS_Word,
        _INR(0, 1),

        21,
        block);
}

/*************************************************/
/* mouse_rectangle()                             */
/*                                               */
/* Constrains the mouse pointer to a given       */
/* rectangle, or returns the last rectangle set  */
/* with this function.                           */
/*                                               */
/* Parameters: Pointer to a BBox, in which the   */
/*             new rectangle is held, or NULL to */
/*             set no new rectangle;             */
382
/*                                               */
383 384 385 386 387 388 389 390 391 392 393 394 395 396 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
/*             1 to rectangle the pointer now,   */
/*             else only rectangle it if it is   */
/*             not flagged as being used.        */
/*                                               */
/* Returns:    Pointer to a BBox, in which the   */
/*             previously set coordinates are    */
/*             stored if the BBox pointer given  */
/*             as a parameter is NULL (else the  */
/*             coordinates in the given BBox     */
/*             will be the same as the coords    */
/*             in the returned BBox).            */
/*************************************************/

BBox * mouse_rectangle(BBox * rect, int now)
{
  char        block[9];
  static BBox last_rect;

  if (!now && mouse_used) return &last_rect;

  if (rect)
  {
    block[0] = 1;
    block[1] = rect->xmin & 0xff;
    block[2] = rect->xmin >> 8;
    block[3] = rect->ymin & 0xff;
    block[4] = rect->ymin >> 8;
    block[5] = rect->xmax & 0xff;
    block[6] = rect->xmax >> 8;
    block[7] = rect->ymax & 0xff;
    block[8] = rect->ymax >> 8;

    _swix(OS_Word,
          _INR(0, 1),

          21,
          block);
  }

  last_rect = *rect;

  return &last_rect;
}