gdkinput-ps2.c 39.2 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
15 16 17 18 19 20 21 22 23 24 25
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gdk/gdk.h>
#include <gdk/gdkinternals.h>
#include "gdkinputprivate.h"
#include "gdkkeysyms.h"
#include "gdkprivate-fb.h"
#include <unistd.h>
Elliot Lee's avatar
Elliot Lee committed
26
#include <errno.h>
Elliot Lee's avatar
Elliot Lee committed
27 28 29 30 31 32
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/vt.h>
33
#include <sys/time.h>
Elliot Lee's avatar
Elliot Lee committed
34
#include <sys/kd.h>
35
#include <sys/types.h>
Elliot Lee's avatar
Elliot Lee committed
36
#include <ctype.h>
37
#include <stdlib.h>
Elliot Lee's avatar
Elliot Lee committed
38
#include <string.h>
Elliot Lee's avatar
Works.  
Elliot Lee committed
39
#include <math.h>
40 41 42 43
#include <linux/fb.h>

/* Two minutes */
#define BLANKING_TIMEOUT 120*1000
44

Elliot Lee's avatar
Elliot Lee committed
45
/*
46
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
Elliot Lee's avatar
Elliot Lee committed
47 48 49 50 51 52 53 54
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

typedef struct {
  gint fd, fd_tag;

55
  gdouble x, y;
Elliot Lee's avatar
Elliot Lee committed
56 57 58
  GdkWindow *prev_window;
  gboolean button1_pressed, button2_pressed, button3_pressed;
  gboolean click_grab;
59

60 61
  guchar mouse_packet[5];
  int packet_nbytes;
62
} MouseDevice;
Elliot Lee's avatar
Elliot Lee committed
63 64 65 66 67

typedef struct {
  gint fd, fd_tag, consfd;

  int vtnum, prev_vtnum;
68
  guint modifier_state;
Elliot Lee's avatar
Elliot Lee committed
69 70 71
  gboolean caps_lock : 1;
} Keyboard;

72 73
static guint blanking_timer = 0;

Elliot Lee's avatar
Elliot Lee committed
74 75
static Keyboard * tty_keyboard_open(void);

76
static MouseDevice *gdk_fb_mouse = NULL;
Elliot Lee's avatar
Elliot Lee committed
77 78
static Keyboard *keyboard = NULL;

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#ifndef VESA_NO_BLANKING
#define VESA_NO_BLANKING        0
#define VESA_VSYNC_SUSPEND      1
#define VESA_HSYNC_SUSPEND      2
#define VESA_POWERDOWN          3
#endif

#if 0
static gboolean
input_activity_timeout(gpointer p)
{
  blanking_timer = 0;
  ioctl(gdk_display->fd, FBIOBLANK, VESA_POWERDOWN);
  return FALSE;
}
#endif

/* This is all very broken :( */
static void
98
input_activity (void)
99 100
{
#if 0
101 102
  if (blanking_timer)
    g_source_remove (blanking_timer);
103
  else
104
    gdk_fb_redraw_all ();
105

106
  blanking_timer = g_timeout_add (BLANKING_TIMEOUT, input_activity_timeout, NULL);
107 108 109
#endif
}

Elliot Lee's avatar
Elliot Lee committed
110
static void
111 112 113
send_button_event (MouseDevice *mouse,
		   guint button,
		   gboolean press_event,
114
		   guint32 the_time)
Elliot Lee's avatar
Elliot Lee committed
115 116 117 118 119 120
{
  GdkEvent *event;
  gint x, y;
  GdkWindow *window;
  int nbuttons = 0;

121
  if (_gdk_fb_pointer_grab_window_events)
122
    window = _gdk_fb_pointer_grab_window_events;
Elliot Lee's avatar
Elliot Lee committed
123
  else
124
    window = gdk_window_at_pointer(NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
125

126
  event = gdk_event_make (window, press_event ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE, FALSE);
Elliot Lee's avatar
Elliot Lee committed
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
  if (event)
    {
      gdk_window_get_origin (window, &x, &y);
      x = mouse->x - x;
      y = mouse->y - y;
      
      event->button.x = x;
      event->button.y = y;
      event->button.button = button;
      event->button.state = (mouse->button1_pressed?GDK_BUTTON1_MASK:0) |
	(mouse->button2_pressed ? GDK_BUTTON2_MASK : 0) |
	(mouse->button3_pressed ? GDK_BUTTON3_MASK : 0) |
	(1 << (button + 8)) /* badhack */ |
	keyboard->modifier_state;
      event->button.device = gdk_core_pointer;
      event->button.x_root = mouse->x;
      event->button.y_root = mouse->y;
      
      event->button.time = the_time;
      
#if 0
      g_message ("Button #%d %s [%d, %d] in %p",
		 button, press_event?"pressed":"released",
		 x, y, window);
      
      /* Debugging aid */
      if (window && window != gdk_parent_root)
	{
	  GdkGC *tmp_gc;
	  
	  tmp_gc = gdk_gc_new (window);
	  GDK_GC_FBDATA (tmp_gc)->values.foreground.pixel = 0;
	  gdk_fb_draw_rectangle (GDK_DRAWABLE_IMPL(window), tmp_gc, TRUE, 0, 0,
				 GDK_DRAWABLE_IMPL_FBDATA(window)->width, GDK_DRAWABLE_IMPL_FBDATA(window)->height);
	  gdk_gc_unref(tmp_gc);
	}
#endif
      
      gdk_event_queue_append (event);
      
      /* For double-clicks */
      if (press_event)
	gdk_event_button_generate (event);
      
    }
  
174
  if (mouse->button1_pressed)
Elliot Lee's avatar
Elliot Lee committed
175
    nbuttons++;
176
  if (mouse->button2_pressed)
Elliot Lee's avatar
Elliot Lee committed
177
    nbuttons++;
178
  if (mouse->button3_pressed)
Elliot Lee's avatar
Elliot Lee committed
179 180
    nbuttons++;

181
  /* Handle implicit button grabs: */
182
  if (press_event && nbuttons == 1)
Elliot Lee's avatar
Elliot Lee committed
183
    {
184 185 186 187
      gdk_fb_pointer_grab (window, FALSE,
			   gdk_window_get_events (window),
			   NULL, NULL,
			   GDK_CURRENT_TIME, TRUE);
Elliot Lee's avatar
Elliot Lee committed
188 189
      mouse->click_grab = TRUE;
    }
190
  else if (!press_event && nbuttons == 0 && mouse->click_grab)
Elliot Lee's avatar
Elliot Lee committed
191
    {
192
      gdk_fb_pointer_ungrab (GDK_CURRENT_TIME, TRUE);
Elliot Lee's avatar
Elliot Lee committed
193 194 195 196 197
      mouse->click_grab = FALSE;
    }
}

static GdkPixmap *last_contents = NULL;
Elliot Lee's avatar
Elliot Lee committed
198
static GdkPoint last_location, last_contents_size;
Elliot Lee's avatar
Elliot Lee committed
199
static GdkCursor *last_cursor = NULL;
200
static GdkFBDrawingContext *gdk_fb_cursor_dc = NULL;
Elliot Lee's avatar
Elliot Lee committed
201
static GdkFBDrawingContext cursor_dc_dat;
Elliot Lee's avatar
Elliot Lee committed
202
static GdkGC *cursor_gc;
203
static gint cursor_visibility_count = 1;
Elliot Lee's avatar
Elliot Lee committed
204

Elliot Lee's avatar
Elliot Lee committed
205
static GdkFBDrawingContext *
206
gdk_fb_cursor_dc_reset (void)
Elliot Lee's avatar
Elliot Lee committed
207
{
208 209
  if (gdk_fb_cursor_dc)
    gdk_fb_drawing_context_finalize (gdk_fb_cursor_dc);
Elliot Lee's avatar
Elliot Lee committed
210 211

  gdk_fb_cursor_dc = &cursor_dc_dat;
212 213 214 215 216
  gdk_fb_drawing_context_init (gdk_fb_cursor_dc,
			       GDK_DRAWABLE_IMPL(gdk_parent_root),
			       cursor_gc,
			       TRUE,
			       FALSE);
Elliot Lee's avatar
Elliot Lee committed
217 218 219 220

  return gdk_fb_cursor_dc;
}

Elliot Lee's avatar
Elliot Lee committed
221
void
222
gdk_fb_cursor_hide (void)
Elliot Lee's avatar
Elliot Lee committed
223
{
Elliot Lee's avatar
Elliot Lee committed
224 225
  GdkFBDrawingContext *mydc = gdk_fb_cursor_dc;

226
  cursor_visibility_count--;
227 228 229
  g_assert (cursor_visibility_count <= 0);
  
  if (cursor_visibility_count < 0)
230 231
    return;

232 233
  if (!mydc)
    mydc = gdk_fb_cursor_dc_reset ();
Elliot Lee's avatar
Elliot Lee committed
234

235
  if (last_contents)
Elliot Lee's avatar
Elliot Lee committed
236
    {
237
      gdk_gc_set_clip_mask (cursor_gc, NULL);
Elliot Lee's avatar
Elliot Lee committed
238
      /* Restore old picture */
239 240 241 242 243 244 245 246 247
      gdk_fb_draw_drawable_3 (GDK_DRAWABLE_IMPL(gdk_parent_root),
			      cursor_gc,
			      GDK_DRAWABLE_IMPL(last_contents),
			      mydc,
			      0, 0,
			      last_location.x,
			      last_location.y,
			      last_contents_size.x,
			      last_contents_size.y);
Elliot Lee's avatar
Elliot Lee committed
248 249 250 251
    }
}

void
252
gdk_fb_cursor_invalidate (void)
Elliot Lee's avatar
Elliot Lee committed
253
{
254
  if (last_contents)
255
    {
256
      gdk_pixmap_unref (last_contents);
257 258
      last_contents = NULL;
    }
Elliot Lee's avatar
Elliot Lee committed
259 260 261
}

void
Elliot Lee's avatar
Elliot Lee committed
262
gdk_fb_cursor_unhide()
Elliot Lee's avatar
Elliot Lee committed
263
{
Elliot Lee's avatar
Elliot Lee committed
264
  GdkFBDrawingContext *mydc = gdk_fb_cursor_dc;
265 266 267 268 269
  GdkCursorPrivateFB *last_private;
  GdkDrawableFBData *pixmap_last;
  
  last_private = GDK_CURSOR_FB (last_cursor);
  pixmap_last = GDK_DRAWABLE_IMPL_FBDATA (last_private->cursor);
270
  cursor_visibility_count++;
271 272
  g_assert (cursor_visibility_count <= 1);
  if (cursor_visibility_count < 1)
273 274
    return;

275 276
  if (!mydc)
    mydc = gdk_fb_cursor_dc_reset ();
Elliot Lee's avatar
Elliot Lee committed
277

278
  if (last_cursor)
Elliot Lee's avatar
Elliot Lee committed
279
    {
280
      if (!last_contents ||
281 282
	  pixmap_last->width > GDK_DRAWABLE_IMPL_FBDATA (last_contents)->width ||
	  pixmap_last->height > GDK_DRAWABLE_IMPL_FBDATA (last_contents)->height)
Elliot Lee's avatar
Elliot Lee committed
283
	{
284 285
	  if (last_contents)
	    gdk_pixmap_unref (last_contents);
Elliot Lee's avatar
Elliot Lee committed
286

287
	  last_contents = gdk_pixmap_new (gdk_parent_root,
288 289
					  pixmap_last->width,
					  pixmap_last->height,
290
					  GDK_DRAWABLE_IMPL_FBDATA (gdk_parent_root)->depth);
Elliot Lee's avatar
Elliot Lee committed
291 292
	}

293 294 295 296 297 298 299
      gdk_gc_set_clip_mask (cursor_gc, NULL);
      gdk_fb_draw_drawable_2 (GDK_DRAWABLE_IMPL (last_contents),
			      cursor_gc,
			      GDK_DRAWABLE_IMPL (gdk_parent_root),
			      last_location.x,
			      last_location.y,
			      0, 0,
300 301
			      pixmap_last->width,
			      pixmap_last->height,
302
			      TRUE, FALSE);
303 304 305 306
      last_contents_size.x = pixmap_last->width;
      last_contents_size.y = pixmap_last->height;
      
      gdk_gc_set_clip_mask (cursor_gc, last_private->mask);
307
      gdk_gc_set_clip_origin (cursor_gc,
308 309
			      last_location.x,
			      last_location.y);
310 311

      gdk_fb_cursor_dc_reset ();
312
      gdk_fb_draw_drawable_3 (GDK_DRAWABLE_IMPL (gdk_parent_root),
313
			      cursor_gc,
314
			      GDK_DRAWABLE_IMPL (last_private->cursor),
315 316 317
			      mydc,
			      0, 0,
			      last_location.x, last_location.y,
318 319
			      pixmap_last->width,
			      pixmap_last->height);
Elliot Lee's avatar
Elliot Lee committed
320 321
    }
  else
322
    gdk_fb_cursor_invalidate ();
Elliot Lee's avatar
Elliot Lee committed
323 324
}

325
gboolean
326
gdk_fb_cursor_region_need_hide (GdkRegion *region)
327 328 329
{
  GdkRectangle testme;

330
  if (!last_cursor)
331 332 333 334
    return FALSE;

  testme.x = last_location.x;
  testme.y = last_location.y;
335 336
  testme.width = GDK_DRAWABLE_IMPL_FBDATA (GDK_CURSOR_FB (last_cursor)->cursor)->width;
  testme.height = GDK_DRAWABLE_IMPL_FBDATA (GDK_CURSOR_FB (last_cursor)->cursor)->height;
337

338
  return (gdk_region_rect_in (region, &testme) != GDK_OVERLAP_RECTANGLE_OUT);
339 340 341
}

gboolean
342
gdk_fb_cursor_need_hide (GdkRectangle *rect)
343 344 345
{
  GdkRectangle testme;

346
  if (!last_cursor)
347 348 349 350
    return FALSE;

  testme.x = last_location.x;
  testme.y = last_location.y;
351 352
  testme.width = GDK_DRAWABLE_IMPL_FBDATA (GDK_CURSOR_FB (last_cursor)->cursor)->width;
  testme.height = GDK_DRAWABLE_IMPL_FBDATA (GDK_CURSOR_FB (last_cursor)->cursor)->height;
353

354
  return gdk_rectangle_intersect (rect, &testme, &testme);
355 356
}

Elliot Lee's avatar
Elliot Lee committed
357
void
358
gdk_fb_get_cursor_rect (GdkRectangle *rect)
Elliot Lee's avatar
Elliot Lee committed
359
{
360
  if (last_cursor)
Elliot Lee's avatar
Elliot Lee committed
361 362 363
    {
      rect->x = last_location.x;
      rect->y = last_location.y;
364 365
      rect->width = GDK_DRAWABLE_IMPL_FBDATA (GDK_CURSOR_FB (last_cursor)->cursor)->width;
      rect->height = GDK_DRAWABLE_IMPL_FBDATA (GDK_CURSOR_FB (last_cursor)->cursor)->height;
Elliot Lee's avatar
Elliot Lee committed
366 367 368 369 370 371 372 373 374
    }
  else
    {
      rect->x = rect->y = -1;
      rect->width = rect->height = 0;
    }
}

static void
375
move_pointer (MouseDevice *mouse, GdkWindow *in_window)
Elliot Lee's avatar
Elliot Lee committed
376 377 378
{
  GdkCursor *the_cursor;

379
  if (!cursor_gc)
Elliot Lee's avatar
Elliot Lee committed
380 381
    {
      GdkColor white, black;
382 383 384 385 386
      cursor_gc = gdk_gc_new (gdk_parent_root);
      gdk_color_black (gdk_colormap_get_system (), &black);
      gdk_color_white (gdk_colormap_get_system (), &white);
      gdk_gc_set_foreground (cursor_gc, &black);
      gdk_gc_set_background (cursor_gc, &white);
Elliot Lee's avatar
Elliot Lee committed
387 388
    }

389
  gdk_fb_cursor_hide ();
Elliot Lee's avatar
Elliot Lee committed
390

391 392 393 394 395 396 397 398 399 400 401 402
  if (_gdk_fb_pointer_grab_window)
    {
      if (_gdk_fb_pointer_grab_cursor)
	the_cursor = _gdk_fb_pointer_grab_cursor;
      else
	{
	  GdkWindow *win = _gdk_fb_pointer_grab_window;
	  while (!GDK_WINDOW_IMPL_FBDATA (win)->cursor && GDK_WINDOW_OBJECT (win)->parent)
	    win = (GdkWindow *)GDK_WINDOW_OBJECT (win)->parent;
	  the_cursor = GDK_WINDOW_IMPL_FBDATA (win)->cursor;
	}
    }
Elliot Lee's avatar
Elliot Lee committed
403 404
  else
    {
405 406 407
      while (!GDK_WINDOW_IMPL_FBDATA (in_window)->cursor && GDK_WINDOW_P (in_window)->parent)
	in_window = (GdkWindow *)GDK_WINDOW_P (in_window)->parent;
      the_cursor = GDK_WINDOW_IMPL_FBDATA (in_window)->cursor;
Elliot Lee's avatar
Elliot Lee committed
408 409
    }

410 411
  last_location.x = mouse->x - GDK_CURSOR_FB (the_cursor)->hot_x;
  last_location.y = mouse->y - GDK_CURSOR_FB (the_cursor)->hot_y;
Elliot Lee's avatar
Elliot Lee committed
412

413 414 415 416
  if (the_cursor)
    gdk_cursor_ref (the_cursor);
  if (last_cursor)
    gdk_cursor_unref (last_cursor);
Elliot Lee's avatar
Elliot Lee committed
417 418
  last_cursor = the_cursor;

419
  gdk_fb_cursor_unhide ();
Elliot Lee's avatar
Elliot Lee committed
420 421
}

422 423 424
void
gdk_fb_cursor_reset(void)
{
425
  GdkWindow *win = gdk_window_at_pointer (NULL, NULL);
426

427
  move_pointer (gdk_fb_mouse, win);
428 429
}

430
void
431 432
gdk_fb_window_send_crossing_events (GdkWindow *dest,
				    GdkCrossingMode mode)
433
{
434 435 436 437
  GdkWindow *c;
  GdkWindow *win, *last, *next;
  GdkEvent *event;
  gint x, y, x_int, y_int;
438
  GdkModifierType my_mask;
439 440
  GList *path, *list;
  gboolean non_linear;
441
  gboolean only_grabbed_window;
442 443
  GdkWindow *a;
  GdkWindow *b;
444

445 446
  if (gdk_fb_mouse->prev_window == NULL)
    gdk_fb_mouse->prev_window = gdk_window_ref (gdk_parent_root);
447

448 449 450 451
  if (mode == GDK_CROSSING_UNGRAB)
    a = _gdk_fb_pointer_grab_window;
  else
    a = gdk_fb_mouse->prev_window;
452
  b = dest;
453

454 455 456
  /* When grab in progress only send normal crossing events about
   * the grabbed window.
   */
457
  only_grabbed_window = (_gdk_fb_pointer_grab_window_events != NULL) &&
458 459
                        (mode == GDK_CROSSING_NORMAL);
  
460 461 462 463
  if (a==b)
    return;

  gdk_input_get_mouseinfo (&x, &y, &my_mask);
464

465
  c = gdk_fb_find_common_ancestor (a, b);
466

467
  non_linear = (c != a) && (c != b);
468 469 470 471 472

  if (!only_grabbed_window || (a == _gdk_fb_pointer_grab_window))
    event = gdk_event_make (a, GDK_LEAVE_NOTIFY, TRUE);
  else
    event = NULL;
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  if (event)
    {
      event->crossing.subwindow = NULL;
      gdk_window_get_root_origin (a, &x_int, &y_int);
      event->crossing.x = x - x_int;
      event->crossing.y = y - y_int;
      event->crossing.x_root = x;
      event->crossing.y_root = y;
      event->crossing.mode = mode;
      if (non_linear)
	event->crossing.detail = GDK_NOTIFY_NONLINEAR;
      else if (c==a)
	event->crossing.detail = GDK_NOTIFY_INFERIOR;
      else
	event->crossing.detail = GDK_NOTIFY_ANCESTOR;
      event->crossing.focus = FALSE;
      event->crossing.state = my_mask;
    }
  
  /* Traverse up from a to (excluding) c */
  if (c != a)
    {
      last = a;
      win = GDK_WINDOW (GDK_WINDOW_OBJECT (a)->parent);
      while (win != c)
498
	{
499 500 501 502
	  if (!only_grabbed_window || (win == _gdk_fb_pointer_grab_window))
	    event = gdk_event_make (win, GDK_LEAVE_NOTIFY, TRUE);
	  else
	    event = NULL;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	  if (event)
	    {
	      event->crossing.subwindow = gdk_window_ref (last);
	      gdk_window_get_root_origin (win, &x_int, &y_int);
	      event->crossing.x = x - x_int;
	      event->crossing.y = y - y_int;
	      event->crossing.x_root = x;
	      event->crossing.y_root = y;
	      event->crossing.mode = mode;
	      if (non_linear)
		event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
	      else
		event->crossing.detail = GDK_NOTIFY_VIRTUAL;
	      event->crossing.focus = FALSE;
	      event->crossing.state = my_mask;
	    }
	  last = win;
	  win = GDK_WINDOW (GDK_WINDOW_OBJECT (win)->parent);
521
	}
522 523 524 525 526 527
    }
  
  /* Traverse down from c to b */
  if (c != b) 
    {
      path = NULL;
528
      win = GDK_WINDOW( GDK_WINDOW_OBJECT (b)->parent);
529
      while (win != c)
530
	{
531 532
	  path = g_list_prepend (path, win);
	  win = GDK_WINDOW( GDK_WINDOW_OBJECT (win)->parent);
533
	}
534 535 536
      
      list = path;
      while (list) 
537
	{
538 539 540 541 542 543 544
	  win = (GdkWindow *)list->data;
	  list = g_list_next (list);
	  if (list)
	    next = (GdkWindow *)list->data;
	  else 
	    next = b;
	  
545 546 547 548
	  if (!only_grabbed_window || (win == _gdk_fb_pointer_grab_window))
	    event = gdk_event_make (win, GDK_ENTER_NOTIFY, TRUE);
	  else
	    event = NULL;
549
	  if (event)
550
	    {
551 552 553 554 555 556 557 558 559
	      event->crossing.subwindow = gdk_window_ref (next);
	      gdk_window_get_root_origin (win, &x_int, &y_int);
	      event->crossing.x = x - x_int;
	      event->crossing.y = y - y_int;
	      event->crossing.x_root = x;
	      event->crossing.y_root = y;
	      event->crossing.mode = mode;
	      if (non_linear)
		event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
560
	      else
561 562 563
		event->crossing.detail = GDK_NOTIFY_VIRTUAL;
	      event->crossing.focus = FALSE;
	      event->crossing.state = my_mask;
564
	    }
565
	}
566 567
      g_list_free (path);
    }
568

569 570 571 572
  if (!only_grabbed_window || (b == _gdk_fb_pointer_grab_window))
    event = gdk_event_make (b, GDK_ENTER_NOTIFY, TRUE);
  else
    event = NULL;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  if (event)
    {
      event->crossing.subwindow = NULL;
      gdk_window_get_root_origin (b, &x_int, &y_int);
      event->crossing.x = x - x_int;
      event->crossing.y = y - y_int;
      event->crossing.x_root = x;
      event->crossing.y_root = y;
      event->crossing.mode = mode;
      if (non_linear)
	event->crossing.detail = GDK_NOTIFY_NONLINEAR;
      else if (c==a)
	event->crossing.detail = GDK_NOTIFY_ANCESTOR;
      else
	event->crossing.detail = GDK_NOTIFY_INFERIOR;
      event->crossing.focus = FALSE;
      event->crossing.state = my_mask;
590
    }
591

592 593 594 595 596 597
  if ((mode != GDK_CROSSING_GRAB) &&
      (b != gdk_fb_mouse->prev_window))
    {
      gdk_window_unref (gdk_fb_mouse->prev_window);
      gdk_fb_mouse->prev_window = gdk_window_ref (b);
    }
598 599 600
}

static void
601 602
handle_mouse_input(MouseDevice *mouse,
		   gboolean got_motion)
603 604 605 606
{
  GdkWindow *mousewin;
  GdkEvent *event;
  gint x, y;
607
  GdkWindow *win, *grabwin;
608 609
  guint state;

610
  if (_gdk_fb_pointer_grab_confine)
611 612 613 614
    mousewin = _gdk_fb_pointer_grab_confine;
  else
    mousewin = gdk_parent_root;

615 616 617 618 619 620 621 622 623
  if (mouse->x < GDK_DRAWABLE_IMPL_FBDATA (mousewin)->llim_x)
    mouse->x = GDK_DRAWABLE_IMPL_FBDATA (mousewin)->llim_x;
  else if (mouse->x > (GDK_DRAWABLE_IMPL_FBDATA (mousewin)->lim_x - 1))
    mouse->x = GDK_DRAWABLE_IMPL_FBDATA (mousewin)->lim_x - 1;

  if (mouse->y < GDK_DRAWABLE_IMPL_FBDATA (mousewin)->llim_y)
    mouse->y = GDK_DRAWABLE_IMPL_FBDATA (mousewin)->llim_y;
  else if (mouse->y > (GDK_DRAWABLE_IMPL_FBDATA (mousewin)->lim_y - 1))
    mouse->y = GDK_DRAWABLE_IMPL_FBDATA (mousewin)->lim_y - 1;
624

625
  if (!got_motion)
626 627
    return;

628
  win = gdk_window_at_pointer (NULL, NULL);
629
  if (_gdk_fb_pointer_grab_window_events)
630 631 632
    grabwin = _gdk_fb_pointer_grab_window_events;
  else
    grabwin = win;
633

634 635 636
  move_pointer (mouse, grabwin);
  
  gdk_window_get_origin (grabwin, &x, &y);
637 638 639 640
  x = mouse->x - x;
  y = mouse->y - y;


641 642 643 644
  state = (mouse->button1_pressed?GDK_BUTTON1_MASK:0) |
    (mouse->button2_pressed?GDK_BUTTON2_MASK:0) |
    (mouse->button3_pressed?GDK_BUTTON3_MASK:0) |
    keyboard->modifier_state;
645

646
  event = gdk_event_make (grabwin, GDK_MOTION_NOTIFY, TRUE);
647
  if (event)
648 649 650 651 652 653 654 655
    {
      event->motion.x = x;
      event->motion.y = y;
      event->motion.state = state;
      event->motion.is_hint = FALSE;
      event->motion.device = gdk_core_pointer;
      event->motion.x_root = mouse->x;
      event->motion.y_root = mouse->y;
656
      event->motion.time = gdk_fb_get_time ();
657 658
    }

659
  if (win != mouse->prev_window)
660 661
    gdk_fb_window_send_crossing_events (win,
					GDK_CROSSING_NORMAL);
662

663
  input_activity ();
664 665 666
}

static gboolean
667 668 669 670
pull_fidmour_packet (MouseDevice *mouse,
		     gboolean *btn_down,
		     gdouble *x,
		     gdouble *y)
671 672 673
{
  gboolean keep_reading = TRUE;

674
  while (keep_reading)
675 676 677
    {
      int n;

678
      n = read (mouse->fd, mouse->mouse_packet + mouse->packet_nbytes, 5 - mouse->packet_nbytes);
679
      if (n < 0)
680
	return FALSE;
681
      else if (n == 0)
682
	{
683 684
	  g_error ("EOF on mouse device!");
	  g_source_remove (mouse->fd_tag);
685 686 687
	  return FALSE;
	}

688
      mouse->packet_nbytes += n;
689 690

      n = 0;
691
      if (!(mouse->mouse_packet[0] & 0x80))
692 693 694
	{
	  int i;
	  /* We haven't received any of the packet yet but there is no header at the beginning */
695
	  for (i = 1; i < mouse->packet_nbytes; i++)
696
	    {
697
	      if (mouse->mouse_packet[i] & 0x80)
698 699 700 701 702 703
		{
		  n = i;
		  break;
		}
	    }
	}
704 705
      else if (mouse->packet_nbytes > 1 &&
	       ((mouse->mouse_packet[0] & 0x90) == 0x90))
706 707 708 709
	{
	  /* eat the 0x90 and following byte, no clue what it's for */
	  n = 2;
	}
710
      else if (mouse->packet_nbytes == 5)
711
	{
712
	  switch (mouse->mouse_packet[0] & 0xF)
713 714 715 716 717 718 719 720
	    {
	    case 2:
	      *btn_down = 0;
	      break;
	    case 1:
	    case 0:
	      *btn_down = 1;
	      break;
Elliot Lee's avatar
Works.  
Elliot Lee committed
721
	    default:
722
	      g_assert_not_reached ();
Elliot Lee's avatar
Works.  
Elliot Lee committed
723
	      break;
724
	    }
Elliot Lee's avatar
Works.  
Elliot Lee committed
725

726
	  *x = mouse->mouse_packet[1] + (mouse->mouse_packet[2] << 7);
727
	  if (*x > 8192)
Elliot Lee's avatar
Works.  
Elliot Lee committed
728
	    *x -= 16384;
729
	  *y = mouse->mouse_packet[3] + (mouse->mouse_packet[4] << 7);
730
	  if (*y > 8192)
Elliot Lee's avatar
Works.  
Elliot Lee committed
731 732 733 734
	    *y -= 16384;
	  /* Now map touchscreen coords to screen coords */
	  *x *= ((double)gdk_display->modeinfo.xres)/4096.0;
	  *y *= ((double)gdk_display->modeinfo.yres)/4096.0;
735 736 737 738
	  n = 5;
	  keep_reading = FALSE;
	}

739
      if (n)
740
	{
741 742
	  memmove (mouse->mouse_packet, mouse->mouse_packet+n, mouse->packet_nbytes-n);
	  mouse->packet_nbytes -= n;
743
	}
744
    }
745 746

  return TRUE;
747 748
}

Elliot Lee's avatar
Elliot Lee committed
749
static gboolean
750 751 752
handle_input_fidmour (GIOChannel *gioc,
		      GIOCondition cond,
		      gpointer data)
Elliot Lee's avatar
Elliot Lee committed
753
{
754
  MouseDevice *mouse = data;
Elliot Lee's avatar
Works.  
Elliot Lee committed
755
  gdouble x, y, oldx, oldy;
756 757
  gboolean got_motion = FALSE;
  gboolean btn_down;
758
  guint32 the_time;
759

760 761
  the_time = gdk_fb_get_time ();
  
Elliot Lee's avatar
Works.  
Elliot Lee committed
762 763
  oldx = mouse->x;
  oldy = mouse->y;
764
  while (pull_fidmour_packet (mouse, &btn_down, &x, &y))
765
    {
766
      if (fabs(x - mouse->x) >= 1.0 || fabs(x - mouse->y) >= 1.0)
Elliot Lee's avatar
Works.  
Elliot Lee committed
767 768 769 770 771
	{
	  got_motion = TRUE;
	  mouse->x = x;
	  mouse->y = y;
	}
772

773
      if (btn_down != mouse->button1_pressed)
774
	{
775
	  if (got_motion)
776
	    {
777
	      handle_mouse_input (mouse, TRUE);
778 779 780 781
	      got_motion = FALSE;
	    }

	  mouse->button1_pressed = btn_down;
782
	  send_button_event (mouse, 1, btn_down, the_time);
783 784 785
	}
    }

786 787
  if (got_motion)
    handle_mouse_input (mouse, TRUE);
788 789 790 791 792

  return TRUE;
}

static gboolean
793 794 795
handle_input_ps2 (GIOChannel *gioc,
		  GIOCondition cond,
		  gpointer data)
796 797
{
  MouseDevice *mouse = data;
798
  int n, dx=0, dy=0;
Elliot Lee's avatar
Elliot Lee committed
799
  gboolean new_button1, new_button2, new_button3;
800
  guint32 the_time;
801
  gboolean got_motion = FALSE;
802
  guchar *buf;
Elliot Lee's avatar
Elliot Lee committed
803

804
  the_time = gdk_fb_get_time ();
Elliot Lee's avatar
Elliot Lee committed
805

806
  while (1) /* Go through as many mouse events as we can */
Elliot Lee's avatar
Elliot Lee committed
807
    {
808 809 810 811 812 813 814 815 816 817 818 819 820 821
      n = read (mouse->fd, mouse->mouse_packet + mouse->packet_nbytes, 3 - mouse->packet_nbytes);
      if (n<=0) /* error or nothing to read */
	break;
      
      mouse->packet_nbytes += n;
      
      if (mouse->packet_nbytes < 3) /* Mouse packet not finished */
	break;

      mouse->packet_nbytes = 0;

      /* Finished reading a packet */
      buf = mouse->mouse_packet;
      
822 823 824
      new_button1 = (buf[0] & 1) && 1;
      new_button3 = (buf[0] & 2) && 1;
      new_button2 = (buf[0] & 4) && 1;
Elliot Lee's avatar
Elliot Lee committed
825

826 827 828 829
      if (got_motion &&
	  (new_button1 != mouse->button1_pressed ||
	   new_button2 != mouse->button2_pressed ||
	   new_button3 != mouse->button3_pressed))
830 831 832 833
	{
	  /* If a mouse button state changes we need to get correct ordering with enter/leave events,
	     so push those out via handle_mouse_input */
	  got_motion = FALSE;
834
	  handle_mouse_input (mouse, TRUE);
835 836
	}

837
      if (new_button1 != mouse->button1_pressed)
838 839
	{
	  mouse->button1_pressed = new_button1; 
840
	  send_button_event (mouse, 1, new_button1, the_time);
841 842
	}

843
      if (new_button2 != mouse->button2_pressed)
844 845
	{
	  mouse->button2_pressed = new_button2;
846
	  send_button_event (mouse, 2, new_button2, the_time);
847 848
	}

849
      if (new_button3 != mouse->button3_pressed)
850 851
	{
	  mouse->button3_pressed = new_button3; 
852
	  send_button_event (mouse, 3, new_button3, the_time);
853 854
	}

855
      if (buf[1] != 0)
856 857 858
	dx = ((buf[0] & 0x10) ? ((gint)buf[1])-256 : buf[1]);
      else
	dx = 0;
859
      if (buf[2] != 0)
860 861 862 863 864 865
	dy = -((buf[0] & 0x20) ? ((gint)buf[2])-256 : buf[2]);
      else
	dy = 0;

      mouse->x += dx;
      mouse->y += dy;
866
      if (dx || dy)
867 868
	got_motion = TRUE;
    }
Elliot Lee's avatar
Elliot Lee committed
869

870 871
  if (got_motion)
    handle_mouse_input (mouse, TRUE);
Elliot Lee's avatar
Elliot Lee committed
872 873 874 875

  return TRUE;
}

876 877 878 879 880 881 882 883 884
static gboolean
handle_input_ms (GIOChannel *gioc,
		 GIOCondition cond,
		 gpointer data)
{
  MouseDevice *mouse = data;
  guchar byte1, byte2, byte3;
  int n, dx=0, dy=0;
  gboolean new_button1, new_button2, new_button3;
885
  guint32 the_time;
886

887
  the_time = gdk_fb_get_time ();
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

  n = read (mouse->fd, &byte1, 1);
  if ( (n!=1) || (byte1 & 0x40) != 0x40)
    return TRUE;
  
  n = read (mouse->fd, &byte2, 1);
  if ( (n!=1) || (byte2 & 0x40) != 0x00)
    return TRUE;
  
  n = read (mouse->fd, &byte3, 1);
  if (n!=1)
    return TRUE;
  
  new_button1 = (byte1 & 0x20) && 1;
  new_button2 = (byte1 & 0x10) && 1;
  new_button3 = 0;
  
  if (new_button1 != mouse->button1_pressed)
    {
      mouse->button1_pressed = new_button1; 
      send_button_event (mouse, 1, new_button1, the_time);
    }
  
  if (new_button2 != mouse->button2_pressed)
    {
      mouse->button2_pressed = new_button2;
      send_button_event (mouse, 2, new_button2, the_time);
    }
  
  if (new_button3 != mouse->button3_pressed)
    {
      mouse->button3_pressed = new_button3; 
      send_button_event (mouse, 3, new_button3, the_time);
    }
  
  dx = (signed char)(((byte1 & 0x03) << 6) | (byte2 & 0x3F));
  dy = (signed char)(((byte1 & 0x0C) << 4) | (byte3 & 0x3F));
  
  mouse->x += dx;
  mouse->y += dy;
  
  if (dx || dy)
    handle_mouse_input (mouse, TRUE);
  
  return TRUE;
}

935
static MouseDevice *
Elliot Lee's avatar
Elliot Lee committed
936 937
mouse_open(void)
{
938
  MouseDevice *retval;
Elliot Lee's avatar
Elliot Lee committed
939 940 941
  guchar buf[7];
  int i = 0;
  GIOChannel *gioc;
942 943
  char *mousedev, *ctmp;
  int mode;
944 945 946 947 948 949 950 951
  struct termios tty;
  enum { PS2_MOUSE, FIDMOUR_MOUSE, MS_MOUSE, UNKNOWN_MOUSE } type;
  int flags;
  fd_set fds;
  char c;
  struct timeval tv;
      
  retval = g_new0 (MouseDevice, 1);
952 953
  retval->fd = -1;
  mode = O_RDWR;
954 955
  ctmp = getenv ("GDK_MOUSETYPE");
  if (ctmp)
Elliot Lee's avatar
Elliot Lee committed
956
    {
957
      if (!strcmp (ctmp, "fidmour"))
958
	type = FIDMOUR_MOUSE;
959
      else if (!strcmp (ctmp, "ps2"))
960
	type = PS2_MOUSE;
961 962
      else if (!strcmp (ctmp, "ms"))
	type = MS_MOUSE;
963 964
      else
	{
965
	  g_print ("Unknown mouse type %s\n", ctmp);
966 967
	  type = UNKNOWN_MOUSE;
	}
Elliot Lee's avatar
Elliot Lee committed
968
    }
969 970
  else
    type = PS2_MOUSE;
Elliot Lee's avatar
Elliot Lee committed
971

972
  switch (type)
Elliot Lee's avatar
Elliot Lee committed
973
    {
974 975 976 977
    case PS2_MOUSE:
      mousedev = "/dev/psaux";
      mode = O_RDWR;
      break;
978 979 980 981
    case MS_MOUSE:
      mousedev = "/dev/ttyS0";
      mode = O_RDWR;
      break;
982 983 984 985 986 987 988
    case FIDMOUR_MOUSE:
      mousedev = "/dev/fidmour";
      mode = O_RDONLY;
      break;
    default:
      goto error;
      break;