gdkinput-ps2.c 28.6 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 26 27 28 29 30 31 32 33
 * 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>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/vt.h>
#include <sys/kd.h>
#include <ctype.h>
34
#include <stdlib.h>
Elliot Lee's avatar
Elliot Lee committed
35
#include <string.h>
Elliot Lee's avatar
Elliot Lee committed
36 37

/*
38
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
Elliot Lee's avatar
Elliot Lee committed
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
 * 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;

  gint x, y;
  GdkWindow *prev_window;
  gboolean button1_pressed, button2_pressed, button3_pressed;
  gboolean click_grab;
} PS2Mouse;

typedef struct {
  gint fd, fd_tag, consfd;

  int vtnum, prev_vtnum;
  guchar states[256];
  gboolean is_ext : 1;
  gboolean caps_lock : 1;
} Keyboard;

static Keyboard * tty_keyboard_open(void);
static guint keyboard_get_state(Keyboard *k);

65
PS2Mouse *gdk_fb_ps2mouse = NULL;
Elliot Lee's avatar
Elliot Lee committed
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
static Keyboard *keyboard = NULL;
FILE *debug_out;

static guint multiclick_tag;
static GdkEvent *multiclick_event = NULL;

static gboolean
click_event_timeout(gpointer x)
{
  switch(multiclick_event->type)
    {
    case GDK_BUTTON_RELEASE:
      gdk_event_free(multiclick_event);
      break;
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
      gdk_event_queue_append(multiclick_event);
      break;
    default:
      break;
    }

  multiclick_event = NULL;
  multiclick_tag = 0;

  return FALSE;
}

static void
send_button_event(PS2Mouse *mouse, guint button, gboolean press_event, time_t the_time)
{
  GdkEvent *event;
  gint x, y;
  GdkWindow *window;
  int nbuttons = 0;

102 103
  if(_gdk_fb_pointer_grab_window_events)
    window = _gdk_fb_pointer_grab_window_events;
Elliot Lee's avatar
Elliot Lee committed
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
  else
    window = gdk_window_get_pointer(NULL, NULL, NULL, NULL);

  gdk_window_get_origin(window, &x, &y);
  x = mouse->x - x;
  y = mouse->y - y;

  if(!press_event
     && multiclick_event
     && multiclick_event->button.button == button
     && multiclick_event->button.window == window
     && ABS(multiclick_event->button.x - x) < 3
     && ABS(multiclick_event->button.y - y) < 3)
    {
      multiclick_event->button.time = the_time;

      /* Just change multiclick_event into a different event */
      switch(multiclick_event->button.type)
	{
	default:
	  g_assert_not_reached();

	case GDK_BUTTON_RELEASE:
	  multiclick_event->button.type = GDK_2BUTTON_PRESS;
	  return;

	case GDK_2BUTTON_PRESS:
	  multiclick_event->button.type = GDK_3BUTTON_PRESS;
	  return;

	case GDK_3BUTTON_PRESS:
	  gdk_event_queue_append(multiclick_event); multiclick_event = NULL;
	  g_source_remove(multiclick_tag); multiclick_tag = 0;
	}
    }

  event = gdk_event_make(window, press_event?GDK_BUTTON_PRESS:GDK_BUTTON_RELEASE, FALSE);

  if(!event)
    return;

  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_get_state(keyboard);
153
  event->button.device = gdk_core_pointer;
Elliot Lee's avatar
Elliot Lee committed
154 155 156 157 158 159 160 161 162 163
  event->button.x_root = mouse->x;
  event->button.y_root = mouse->y;

  if(mouse->button1_pressed)
    nbuttons++;
  if(mouse->button2_pressed)
    nbuttons++;
  if(mouse->button3_pressed)
    nbuttons++;

164
  if(press_event && nbuttons == 1)
Elliot Lee's avatar
Elliot Lee committed
165
    {
166
      gdk_fb_pointer_grab(window, FALSE, gdk_window_get_events(window), NULL, NULL, GDK_CURRENT_TIME, TRUE);
Elliot Lee's avatar
Elliot Lee committed
167 168 169 170
      mouse->click_grab = TRUE;
    }
  else if(!press_event && nbuttons == 0 && mouse->click_grab)
    {
171
      gdk_fb_pointer_ungrab(GDK_CURRENT_TIME, TRUE);
Elliot Lee's avatar
Elliot Lee committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185
      mouse->click_grab = FALSE;
    }

#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;
186 187
      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);
Elliot Lee's avatar
Elliot Lee committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
      gdk_gc_unref(tmp_gc);
    }
#endif

  if(!press_event && !multiclick_tag)
    {
      multiclick_tag = g_timeout_add(250, click_event_timeout, NULL);
      multiclick_event = gdk_event_copy(event);
    }

  gdk_event_queue_append(event);
}

static GdkPixmap *last_contents = NULL;
Elliot Lee's avatar
Elliot Lee committed
202
static GdkPoint last_location, last_contents_size;
Elliot Lee's avatar
Elliot Lee committed
203
static GdkCursor *last_cursor = NULL;
Elliot Lee's avatar
Elliot Lee committed
204 205
GdkFBDrawingContext *gdk_fb_cursor_dc = NULL;
static GdkFBDrawingContext cursor_dc_dat;
Elliot Lee's avatar
Elliot Lee committed
206
static GdkGC *cursor_gc;
207
static gint cursor_visibility_count = 1;
Elliot Lee's avatar
Elliot Lee committed
208

Elliot Lee's avatar
Elliot Lee committed
209 210 211 212 213 214 215
static GdkFBDrawingContext *
gdk_fb_cursor_dc_reset(void)
{
  if(gdk_fb_cursor_dc)
    gdk_fb_drawing_context_finalize(gdk_fb_cursor_dc);

  gdk_fb_cursor_dc = &cursor_dc_dat;
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 222 223
void
gdk_fb_cursor_hide(void)
{
Elliot Lee's avatar
Elliot Lee committed
224 225
  GdkFBDrawingContext *mydc = gdk_fb_cursor_dc;

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

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

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

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

void
Elliot Lee's avatar
Elliot Lee committed
259
gdk_fb_cursor_unhide()
Elliot Lee's avatar
Elliot Lee committed
260
{
Elliot Lee's avatar
Elliot Lee committed
261 262
  GdkFBDrawingContext *mydc = gdk_fb_cursor_dc;

263 264 265 266 267
  cursor_visibility_count++;
  g_assert(cursor_visibility_count <= 1);
  if(cursor_visibility_count < 1)
    return;

Elliot Lee's avatar
Elliot Lee committed
268 269 270
  if(!mydc)
    mydc = gdk_fb_cursor_dc_reset();

Elliot Lee's avatar
Elliot Lee committed
271 272 273
  if(last_cursor)
    {
      if(!last_contents
274 275
	 || GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->width > GDK_DRAWABLE_IMPL_FBDATA(last_contents)->width
	 || GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->height > GDK_DRAWABLE_IMPL_FBDATA(last_contents)->height)
Elliot Lee's avatar
Elliot Lee committed
276 277 278 279 280
	{
	  if(last_contents)
	    gdk_pixmap_unref(last_contents);

	  last_contents = gdk_pixmap_new(gdk_parent_root,
281 282 283
					 GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->width,
					 GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->height,
					 GDK_DRAWABLE_IMPL_FBDATA(gdk_parent_root)->depth);
Elliot Lee's avatar
Elliot Lee committed
284 285 286
	}

      gdk_gc_set_clip_mask(cursor_gc, NULL);
287
      gdk_fb_draw_drawable_2(GDK_DRAWABLE_IMPL(last_contents), cursor_gc, GDK_DRAWABLE_IMPL(gdk_parent_root), last_location.x,
Elliot Lee's avatar
Elliot Lee committed
288
			     last_location.y, 0, 0,
289 290 291 292
			     GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->width,
			     GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->height, TRUE, FALSE);
      last_contents_size.x = GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->width;
      last_contents_size.y = GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->height;
Elliot Lee's avatar
Elliot Lee committed
293
      gdk_gc_set_clip_mask(cursor_gc, GDK_CURSOR_FB(last_cursor)->mask);
294 295
      gdk_gc_set_clip_origin(cursor_gc, last_location.x + GDK_CURSOR_FB(last_cursor)->mask_off_x,
			     last_location.y + GDK_CURSOR_FB(last_cursor)->mask_off_y);
Elliot Lee's avatar
Elliot Lee committed
296 297

      gdk_fb_cursor_dc_reset();
298 299 300 301
      gdk_fb_draw_drawable_3(GDK_DRAWABLE_IMPL(gdk_parent_root), cursor_gc, GDK_DRAWABLE_IMPL(GDK_CURSOR_FB(last_cursor)->cursor),
			     mydc, 0, 0, last_location.x, last_location.y,
			     GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->width,
			     GDK_DRAWABLE_IMPL_FBDATA(GDK_CURSOR_FB(last_cursor)->cursor)->height);
Elliot Lee's avatar
Elliot Lee committed
302 303 304 305 306
    }
  else
    gdk_fb_cursor_invalidate();
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
gboolean
gdk_fb_cursor_region_need_hide(GdkRegion *region)
{
  GdkRectangle testme;

  if(!last_cursor)
    return FALSE;

  testme.x = last_location.x;
  testme.y = last_location.y;
  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;

  return (gdk_region_rect_in(region, &testme)!=GDK_OVERLAP_RECTANGLE_OUT);
}

gboolean
gdk_fb_cursor_need_hide(GdkRectangle *rect)
{
  GdkRectangle testme;

  if(!last_cursor)
    return FALSE;

  testme.x = last_location.x;
  testme.y = last_location.y;
  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;

  return gdk_rectangle_intersect(rect, &testme, &testme);
}

Elliot Lee's avatar
Elliot Lee committed
339 340 341 342 343 344 345
void
gdk_fb_get_cursor_rect(GdkRectangle *rect)
{
  if(last_cursor)
    {
      rect->x = last_location.x;
      rect->y = last_location.y;
346 347
      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
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
    }
  else
    {
      rect->x = rect->y = -1;
      rect->width = rect->height = 0;
    }
}

static void
move_pointer(PS2Mouse *mouse, GdkWindow *in_window)
{
  GdkCursor *the_cursor;

  if(!cursor_gc)
    {
      GdkColor white, black;
      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);
    }

  gdk_fb_cursor_hide();

373
  if(_gdk_fb_pointer_grab_window && _gdk_fb_pointer_grab_cursor)
Elliot Lee's avatar
Elliot Lee committed
374 375 376
    the_cursor = _gdk_fb_pointer_grab_cursor;
  else
    {
377 378 379
      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
380 381
    }

Elliot Lee's avatar
Elliot Lee committed
382 383 384
  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
385 386 387 388 389 390 391 392 393
  if(the_cursor)
    gdk_cursor_ref(the_cursor);
  if(last_cursor)
    gdk_cursor_unref(last_cursor);
  last_cursor = the_cursor;

  gdk_fb_cursor_unhide();
}

394 395 396 397 398 399 400 401
void
gdk_fb_cursor_reset(void)
{
  GdkWindow *win = gdk_window_get_pointer(NULL, NULL, NULL, NULL);

  move_pointer(gdk_fb_ps2mouse, win);
}

402
void gdk_fb_window_visibility_crossing(GdkWindow *window, gboolean is_show, gboolean is_grab)
403 404 405 406 407 408
{
  gint winx, winy;
  GdkModifierType my_mask;

  gdk_input_ps2_get_mouseinfo(&winx, &winy, &my_mask);

409 410 411 412 413 414
  if(is_grab
     || (winx >= GDK_DRAWABLE_IMPL_FBDATA(window)->llim_x
	 && winx < GDK_DRAWABLE_IMPL_FBDATA(window)->lim_x
	 && winy >= GDK_DRAWABLE_IMPL_FBDATA(window)->llim_y
	 && winy < GDK_DRAWABLE_IMPL_FBDATA(window)->lim_y)
     )
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
    {
      GdkWindow *oldwin, *newwin, *curwin;
      GdkEvent *event;

      curwin = gdk_window_get_pointer(NULL, NULL, NULL, NULL);

      if(is_show)
	{
	  /* Window is about to be shown */
	  oldwin = curwin;
	  newwin = window;
	}
      else
	{
	  /* Window is about to be hidden */
	  oldwin = window;
	  newwin = curwin;
	}

      event = gdk_event_make(oldwin, GDK_LEAVE_NOTIFY, TRUE);
      if(event)
	{
	  guint x_int, y_int;
	  event->crossing.subwindow = gdk_window_ref(newwin);
	  gdk_window_get_root_origin(oldwin, &x_int, &y_int);
	  event->crossing.x = winx - x_int;
	  event->crossing.y = winy - y_int;
	  event->crossing.x_root = winx;
	  event->crossing.y_root = winy;
444 445 446 447 448 449 450 451 452
	  if(is_grab)
	    {
	      if(is_show)
		event->crossing.mode = GDK_CROSSING_GRAB;
	      else
		event->crossing.mode = GDK_CROSSING_UNGRAB;
	    }
	  else
	    event->crossing.mode = GDK_CROSSING_NORMAL;
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
	  event->crossing.detail = GDK_NOTIFY_UNKNOWN;
	  event->crossing.focus = FALSE;
	  event->crossing.state = my_mask;
	}

      event = gdk_event_make(newwin, GDK_ENTER_NOTIFY, TRUE);
      if(event)
	{
	  guint x_int, y_int;
	  event->crossing.subwindow = gdk_window_ref(oldwin);
	  gdk_window_get_root_origin(newwin, &x_int, &y_int);
	  event->crossing.x = winx - x_int;
	  event->crossing.y = winy - y_int;
	  event->crossing.x_root = winx;
	  event->crossing.y_root = winy;
	  event->crossing.mode = GDK_CROSSING_NORMAL;
	  event->crossing.detail = GDK_NOTIFY_UNKNOWN;
	  event->crossing.focus = FALSE;
	  event->crossing.state = my_mask;
	}

      if(gdk_fb_ps2mouse->prev_window)
	gdk_window_unref(gdk_fb_ps2mouse->prev_window);
      gdk_fb_ps2mouse->prev_window = gdk_window_ref(newwin);
    }
}

Elliot Lee's avatar
Elliot Lee committed
480 481 482 483 484 485 486
static gboolean
handle_input(GIOChannel *gioc, GIOCondition cond, gpointer data)
{
  guchar buf[3];
  int n, left, dx=0, dy=0;
  PS2Mouse *mouse = data;
  gboolean new_button1, new_button2, new_button3;
Elliot Lee's avatar
Elliot Lee committed
487 488
  time_t the_time;
  GTimeVal curtime;
Elliot Lee's avatar
Elliot Lee committed
489
  GdkWindow *mousewin;
490
  gboolean got_motion = FALSE;
Elliot Lee's avatar
Elliot Lee committed
491

Elliot Lee's avatar
Elliot Lee committed
492 493 494
  g_get_current_time(&curtime);
  the_time = curtime.tv_usec;

495
  while(1) /* Go through as many mouse events as we can */
Elliot Lee's avatar
Elliot Lee committed
496
    {
497 498 499
      for(left = sizeof(buf); left > 0; )
	{
	  n = read(mouse->fd, buf+sizeof(buf)-left, left);
Elliot Lee's avatar
Elliot Lee committed
500

501 502 503 504
	  if(n <= 0)
	    {
	      if(left != sizeof(buf))
		continue; /* XXX FIXME - this will be slow compared to turning on blocking mode, etc. */
Elliot Lee's avatar
Elliot Lee committed
505

506 507
	      goto done_reading_mouse_events;
	    }
Elliot Lee's avatar
Elliot Lee committed
508

509 510
	  left -= n;
	}
Elliot Lee's avatar
Elliot Lee committed
511

512 513 514
      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
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
      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);
	}

      if(buf[1] != 0)
	dx = ((buf[0] & 0x10) ? ((gint)buf[1])-256 : buf[1]);
      else
	dx = 0;
      if(buf[2] != 0)
	dy = -((buf[0] & 0x20) ? ((gint)buf[2])-256 : buf[2]);
      else
	dy = 0;

      mouse->x += dx;
      mouse->y += dy;
      if(dx || dy)
	got_motion = TRUE;
    }
 done_reading_mouse_events:
Elliot Lee's avatar
Elliot Lee committed
549 550 551 552 553 554 555 556

  if(_gdk_fb_pointer_grab_confine)
    mousewin = _gdk_fb_pointer_grab_confine;
  else
    mousewin = gdk_parent_root;

  if(mouse->x < 0)
    mouse->x = 0;
557 558
  else if(mouse->x > (GDK_DRAWABLE_IMPL_FBDATA(mousewin)->lim_x - 1))
    mouse->x = GDK_DRAWABLE_IMPL_FBDATA(mousewin)->lim_x - 1;
Elliot Lee's avatar
Elliot Lee committed
559 560
  if(mouse->y < 0)
    mouse->y = 0;
561 562
  else if(mouse->y > (GDK_DRAWABLE_IMPL_FBDATA(mousewin)->lim_y - 1))
    mouse->y = GDK_DRAWABLE_IMPL_FBDATA(mousewin)->lim_y - 1;
Elliot Lee's avatar
Elliot Lee committed
563

564
  if(got_motion) {
Elliot Lee's avatar
Elliot Lee committed
565
    GdkEvent *event;
Elliot Lee's avatar
Elliot Lee committed
566
    gint x, y;
Elliot Lee's avatar
Elliot Lee committed
567 568 569 570 571
    GdkWindow *win;
    guint state;

    win = gdk_window_get_pointer(NULL, NULL, NULL, NULL);
    move_pointer(mouse, win);
572 573
    if(_gdk_fb_pointer_grab_window_events)
      win = _gdk_fb_pointer_grab_window_events;
Elliot Lee's avatar
Elliot Lee committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

    gdk_window_get_origin(win, &x, &y);
    x = mouse->x - x;
    y = mouse->y - y;


    state = (mouse->button1_pressed?GDK_BUTTON1_MASK:0)
      | (mouse->button2_pressed?GDK_BUTTON2_MASK:0)
      | (mouse->button3_pressed?GDK_BUTTON3_MASK:0)
      | keyboard_get_state(keyboard);

    event = gdk_event_make (win, GDK_MOTION_NOTIFY, TRUE);
    if(event)
      {
	event->motion.x = x;
	event->motion.y = y;
	event->motion.state = state;
	event->motion.is_hint = FALSE;
592
	event->motion.device = gdk_core_pointer;
Elliot Lee's avatar
Elliot Lee committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	event->motion.x_root = mouse->x;
	event->motion.y_root = mouse->y;
      }

    if(win != mouse->prev_window)
      {
	GdkEvent *evel;

	if(mouse->prev_window && (evel = gdk_event_make(mouse->prev_window, GDK_LEAVE_NOTIFY, TRUE)))
	  {
	    evel->crossing.subwindow = gdk_window_ref(win);
	    evel->crossing.x = x;
	    evel->crossing.y = y;
	    evel->crossing.x_root = mouse->x;
	    evel->crossing.y_root = mouse->y;
	    evel->crossing.mode = GDK_CROSSING_NORMAL;
	    evel->crossing.detail = GDK_NOTIFY_UNKNOWN;
	    evel->crossing.focus = FALSE;
	    evel->crossing.state = state;
	  }

	evel = gdk_event_make(win, GDK_ENTER_NOTIFY, TRUE);
	if(evel)
	  {
	    evel->crossing.subwindow = gdk_window_ref(mouse->prev_window?mouse->prev_window:gdk_parent_root);
	    evel->crossing.x = x;
	    evel->crossing.y = y;
	    evel->crossing.x_root = mouse->x;
	    evel->crossing.y_root = mouse->y;
	    evel->crossing.mode = GDK_CROSSING_NORMAL;
	    evel->crossing.detail = GDK_NOTIFY_UNKNOWN;
	    evel->crossing.focus = FALSE;
	    evel->crossing.state = state;
	  }

	if(mouse->prev_window)
	  gdk_window_unref(mouse->prev_window);
	mouse->prev_window = gdk_window_ref(win);
      }
  }

  return TRUE;
}

static PS2Mouse *
mouse_open(void)
{
  PS2Mouse *retval = g_new0(PS2Mouse, 1);
  guchar buf[7];
  int i = 0;
  GIOChannel *gioc;

  retval->fd = open("/dev/psaux", O_RDWR);
  if(retval->fd < 0)
    {
      g_free(retval);
      return NULL;
    }

  /* From xf86_Mouse.c */
  buf[i++] = 230; /* 1:1 scaling */
  buf[i++] = 244; /* enable mouse */
  buf[i++] = 243; /* Sample rate */
  buf[i++] = 200;
  buf[i++] = 232; /* device resolution */
  buf[i++] = 1;
  write(retval->fd, buf, i);
Elliot Lee's avatar
Elliot Lee committed
660 661 662 663 664 665 666 667 668
  if(read(retval->fd, buf, 3) < 0
     || getenv("GDK_NO_PS2MOUSE")) /* Get rid of misc garbage whatever stuff from mouse */
    {
      close(retval->fd);
      retval->fd = -1;
    }
  else
    {
      fcntl(retval->fd, F_SETFL, O_RDWR|O_NONBLOCK);
669

Elliot Lee's avatar
Elliot Lee committed
670 671 672
      gioc = g_io_channel_unix_new(retval->fd);
      retval->fd_tag = g_io_add_watch(gioc, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL, handle_input, retval);
    }
Elliot Lee's avatar
Elliot Lee committed
673 674 675 676 677 678 679 680 681 682

  retval->x = gdk_display->modeinfo.xres >> 1;
  retval->y = gdk_display->modeinfo.yres >> 1;

  return retval;
}

void
gdk_input_init (void)
{
683
  gdk_input_devices = g_list_append (NULL, gdk_core_pointer);
Elliot Lee's avatar
Elliot Lee committed
684 685 686

  gdk_input_ignore_core = FALSE;

687
  gdk_fb_ps2mouse = mouse_open();
Elliot Lee's avatar
Elliot Lee committed
688 689 690 691 692
}

void
gdk_input_ps2_get_mouseinfo(gint *x, gint *y, GdkModifierType *mask)
{
693 694
  *x = gdk_fb_ps2mouse->x;
  *y = gdk_fb_ps2mouse->y;
Elliot Lee's avatar
Elliot Lee committed
695
  *mask =
696 697 698
    (gdk_fb_ps2mouse->button1_pressed?GDK_BUTTON1_MASK:0)
    | (gdk_fb_ps2mouse->button2_pressed?GDK_BUTTON2_MASK:0)
    | (gdk_fb_ps2mouse->button3_pressed?GDK_BUTTON3_MASK:0)
Elliot Lee's avatar
Elliot Lee committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    | keyboard_get_state(keyboard);
}

/* Returns the modifier mask for the keyboard */
static guint
keyboard_get_state(Keyboard *k)
{
  guint retval = 0;
  struct {
    guchar from;
    guint to;
  } statetrans[] = {
    {0x1D, GDK_CONTROL_MASK},
    {0x9D, GDK_CONTROL_MASK},
    {0x38, GDK_MOD1_MASK},
    {0xB8, GDK_MOD1_MASK},
    {0x2A, GDK_SHIFT_MASK},
    {0x36, GDK_SHIFT_MASK}
  };
  int i;

  for(i = 0; i < sizeof(statetrans)/sizeof(statetrans[0]); i++)
    if(k->states[statetrans[i].from])
      retval |= statetrans[i].to;

  return retval;
}

static GdkWindow *
gdk_window_find_focus(void)
{
  if(_gdk_fb_keyboard_grab_window)
    return _gdk_fb_keyboard_grab_window;
  else if(GDK_WINDOW_P(gdk_parent_root)->children)
Elliot Lee's avatar
Elliot Lee committed
733 734 735 736 737 738 739 740 741 742 743 744
    {
      GList *item;
      for(item = GDK_WINDOW_P(gdk_parent_root)->children; item; item = item->next)
	{
	  GdkWindowPrivate *priv = item->data;

	  if(priv->mapped)
	    return item->data;
	}
    }

  return gdk_parent_root;
Elliot Lee's avatar
Elliot Lee committed
745 746
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
static const guint trans_table[256][3] = {
  /* 0x00 */
  {0, 0, 0},
  {GDK_Escape, 0, 0},
  {'1', '!', 0},
  {'2', '@', 0},
  {'3', '#', 0},
  {'4', '$', 0},
  {'5', '%', 0},
  {'6', '^', 0},
  {'7', '&', 0},
  {'8', '*', 0},
  {'9', '(', 0},
  {'0', ')', 0},
  {'-', '_', 0},
  {'=', '+', 0},
  {GDK_BackSpace, 0, 0},
  {GDK_Tab, 0, 0},

  /* 0x10 */
  {'q', 'Q', 0},
  {'w', 'W', 0},
  {'e', 'E', 0},
  {'r', 'R', 0},
  {'t', 'T', 0},
  {'y', 'Y', 0},
  {'u', 'U', 0},
  {'i', 'I', 0},
  {'o', 'O', 0},
  {'p', 'P', 0},
  {'[', '{', 0},
  {']', '}', 0},
  {GDK_Return, 0, 0},
  {GDK_Control_L, 0, 0}, /* mod */
  {'a', 'A', 0},
  {'s', 'S', 0},
Elliot Lee's avatar
Elliot Lee committed
783 784

	/* 0x20 */
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  {'d', 'D', 0},
  {'f', 'F', 0},
  {'g', 'G', 0},
  {'h', 'H', 0},
  {'j', 'J', 0},
  {'k', 'K', 0},
  {'l', 'L', 0},
  {';', ':', 0},
  {'\'', '"', 0},
  {'`', '~', 0},
  {GDK_Shift_L, 0, 0}, /* mod */
  {'\\', 0, 0},
  {'z', 0, 0},
  {'x', 0, 0},
  {'c', 0, 0},

  {'v', 'V', 0},
Elliot Lee's avatar
Elliot Lee committed
802 803

	/* 0x30 */
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
  {'b', 'B', 0},
  {'n', 'N', 0},
  {'m', 'M', 0},
  {',', 0, 0},
  {'.', 0, 0},
  {'/', 0, 0},
  {GDK_Shift_R, 0, 0}, /* mod */
  {GDK_KP_Multiply, 0, 0},
  {0, 0, 0},
  {GDK_space, 0, 0},
  {0, 0, 0},
  {GDK_F1, 0, 0},
  {GDK_F2, 0, 0},
  {GDK_F3, 0, 0},
  {GDK_F4, 0, 0},
  {GDK_F5, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
820 821

	/* 0x40 */
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  {GDK_F6, 0, 0},
  {GDK_F7, 0, 0},
  {GDK_F8, 0, 0},
  {GDK_F9, 0, 0},
  {GDK_F10, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
838 839

	/* 0x50 */
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {GDK_F11, 0, 0},
  {GDK_F12, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
856 857

	/* 0x60 */
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
874 875

	/* 0x70 */
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
892 893

	/* 0x80 */
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
910 911

	/* 0x90 */
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
928 929

	/* 0xA0 */
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
946 947

	/* 0xB0 */
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
964 965

	/* 0xC0 */
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {GDK_Up, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {GDK_Left, 0, 0},
  {GDK_Right, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
982 983

	/* 0xD0 */
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  {GDK_Down, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
1000 1001

	/* 0xE0 */
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
Elliot Lee's avatar
Elliot Lee committed
1018 1019

	/* 0xF0 */
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0},
};
static gboolean
handle_keyboard_input(GIOChannel *gioc, GIOCondition cond, gpointer data)
{
  guchar buf[128];
  int i, n;
  Keyboard *k = data;
  time_t now;
Elliot Lee's avatar
Elliot Lee committed
1044
  GTimeVal curtime;
1045 1046 1047 1048 1049 1050

  n = read(k->fd, buf, sizeof(buf));
  if(n <= 0)
    g_error("Nothing from keyboard!");

  /* Now turn this into a keyboard event */
Elliot Lee's avatar
Elliot Lee committed
1051 1052
  g_get_current_time(&curtime);
  now = curtime.tv_sec;
1053 1054 1055 1056

  for(i = 0; i < n; i++)
    {
      guchar base_char;
Elliot Lee's avatar
Elliot Lee committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
      GdkEvent *event;
      GdkWindow *win;
      char dummy[2];
      int mod;
      guint keyval, state;

      if(buf[i] == 0xE0 || k->is_ext) /* extended char */
	{
	  int l;

	  l = k->is_ext?0:1;
	  k->is_ext = TRUE;

	  if((i+l) >= n)
	    continue;

	  if(buf[i+l] == 0x2A
	     || buf[i+l] == 0xAA)
	    {
	      i++;
	      continue;
	    }

	  base_char = 0x80 + (buf[i+l] & 0x7F);
	  k->is_ext = FALSE;
	  i += l;
	}
      else
	base_char = buf[i] & 0x7F;

      if(base_char > sizeof(trans_table)/sizeof(trans_table[0]))
	continue;

      {
	gboolean new_state = (buf[i] & 0x80)?FALSE:TRUE;

	k->states[base_char] = new_state;
      }

      if((base_char == 0x1D) /* left Ctrl */
	 || (base_char == 0x9D) /* right Ctrl */
	 || (base_char == 0x38) /* left Alt */
	 || (base_char == 0xB8) /* right Alt */
	 || (base_char == 0x2A) /* left Shift */
	 || (base_char == 0x36) /* right Shift */)
	{
	  continue; /* Don't generate events for modifiers */
	}

      if(base_char == 0x3A /* Caps lock */)
	{
	  if(k->states[base_char])
	    k->caps_lock = !k->caps_lock;
	  ioctl(k->fd, KDSETLED, k->caps_lock?LED_CAP:0);

	  continue;
	}

      if(trans_table[base_char][0] >= GDK_F1
	 && trans_table[base_char][0] <= GDK_F35
	 && (keyboard_get_state(k) & GDK_MOD1_MASK))
	{
	  if(!k->states[base_char]) /* Only switch on release */
	    {
	      gint vtnum = trans_table[base_char][0] - GDK_F1 + 1;

	      fprintf(debug_out, "Switching VTs\n");

	      /* Do the whole funky VT switch thing */
	      ioctl(k->consfd, VT_ACTIVATE, vtnum);
	      ioctl(k->consfd, VT_WAITACTIVE, k->vtnum);
	      gdk_fb_redraw_all();
	    }

	  continue;
	}

      keyval = 0;
      state = keyboard_get_state(k);
      mod = 0;
      if(state & GDK_CONTROL_MASK)
	mod = 2;
      else if(state & GDK_SHIFT_MASK)
	mod = 1;
      do {
	keyval = trans_table[base_char][mod--];
1143
      } while(!keyval && (mod >= 0));
Elliot Lee's avatar
Elliot Lee committed
1144 1145 1146 1147 1148

      if(k->caps_lock && (keyval >= 'a')
	 && (keyval <= 'z'))
	keyval = toupper(keyval);

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
      /* handle some magic keys */
      if(state & (GDK_CONTROL_MASK|GDK_MOD1_MASK))
	{
	  if(!k->states[base_char])
	    {
	      if(keyval == GDK_BackSpace)
		exit(1);

	      if(keyval == GDK_Return)
		gdk_fb_redraw_all();

	    }

	  keyval = 0;
	}

      if(!keyval)
	continue;

Elliot Lee's avatar
Elliot Lee committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
      win = gdk_window_find_focus();
      event = gdk_event_make(win, k->states[base_char]?GDK_KEY_PRESS:GDK_KEY_RELEASE, TRUE);
      if(event)
	{
	  /* Find focused window */
	  event->key.time = now;
	  event->key.state = state;
	  event->key.keyval = keyval;
	  event->key.length = isprint(event->key.keyval)?1:0;
	  dummy[0] = event->key.keyval;
	  dummy[1] = 0;
	  event->key.string = event->key.length?g_strdup(dummy):NULL;
	}
    }

  return TRUE;
}

static Keyboard *
tty_keyboard_open(void)
{
  Keyboard *retval = g_new0(Keyboard, 1);
  GIOChannel *gioc;
  const char cursoroff_str[] = "\033[?1;0;0c";
  int n;
  struct vt_stat vs;
  char buf[32];
  struct termios ts;

  setsid();
  retval->consfd = open("/dev/console", O_RDWR);
  ioctl(retval->consfd, VT_GETSTATE, &vs);
  retval->prev_vtnum = vs.v_active;
  g_snprintf(buf, sizeof(buf), "/dev/tty%d", retval->prev_vtnum);
  ioctl(retval->consfd, KDSKBMODE, K_XLATE);

  n = ioctl(retval->consfd, VT_OPENQRY, &retval->vtnum);
  if(n < 0 || retval->vtnum == -1)
    g_error("Cannot allocate VT");

  ioctl(retval->consfd, VT_ACTIVATE, retval->vtnum);
  ioctl(retval->consfd, VT_WAITACTIVE, retval->vtnum);

  debug_out = fdopen(dup(2), "w");

#if 0
  close(0);
  close(1);
  close(2);
#endif
  g_snprintf(buf, sizeof(buf), "/dev/tty%d", retval->vtnum);
  retval->fd = open(buf, O_RDWR|O_NONBLOCK);
  if(retval->fd < 0)
    return NULL;
  if(ioctl(retval->fd, KDSKBMODE, K_RAW) < 0)
    g_warning("K_RAW failed");

  ioctl(0, TIOCNOTTY, 0);
  ioctl(retval->fd, TIOCSCTTY, 0);
  tcgetattr(retval->fd, &ts);
  ts.c_cc[VTIME] = 0;
  ts.c_cc[VMIN] = 1;
  ts.c_lflag &= ~(ICANON|ECHO|ISIG);
  ts.c_iflag = 0;
  tcsetattr(retval->fd, TCSAFLUSH, &ts);

  tcsetpgrp(retval->fd, getpgrp());

  write(retval->fd, cursoroff_str, strlen(cursoroff_str));

#if 0
  if(retval->fd != 0)
    dup2(retval->fd, 0);
  if(retval->fd != 1)
    dup2(retval->fd, 1);
  if(retval->fd != 2)
    dup2(retval->fd, 2);
#endif

  gioc = g_io_channel_unix_new(retval->fd);
  retval->fd_tag = g_io_add_watch(gioc, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL, handle_keyboard_input, retval);

  return retval;
}

void
keyboard_init(void)
{
  keyboard = tty_keyboard_open();
}

void
keyboard_shutdown(void)
{
  int tmpfd;

  ioctl(keyboard->fd, KDSKBMODE, K_XLATE);
  close(keyboard->fd);
  g_source_remove(keyboard->fd_tag);

  tmpfd = keyboard->consfd;
  ioctl(tmpfd, VT_ACTIVATE, keyboard->prev_vtnum);
  ioctl(tmpfd, VT_WAITACTIVE, keyboard->prev_vtnum);
  ioctl(tmpfd, VT_DISALLOCATE, keyboard->vtnum);
  close(tmpfd);

  g_free(keyboard);
  keyboard = NULL;
}