gdkinput-x11.c 24 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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
/* 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
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#if defined(XINPUT_GXI) || defined(XINPUT_XFREE)

/* Forward declarations */
static void gdk_input_get_root_relative_geometry (Display *dpy, Window w, 
						  int *x_ret, int *y_ret,
						  int *width_ret, 
						  int *height_ret);
static GdkDevicePrivate *gdk_input_device_new(XDeviceInfo *device, 
					      gint include_core);
static void gdk_input_common_find_events(GdkWindow *window,
					 GdkDevicePrivate *gdkdev,
					 gint mask,
					 XEventClass *classes,
					 int *num_classes);
static void gdk_input_common_select_events(GdkWindow *window,
					   GdkDevicePrivate *gdkdev);
static void gdk_input_translate_coordinates(GdkDevicePrivate *gdkdev,
					    GdkInputWindow *input_window,
					    gint *axis_data,
					    gdouble *x, gdouble *y,
					    gdouble *pressure,
					    gdouble *xtilt, gdouble *ytilt);
static guint gdk_input_translate_state(guint state, guint device_state);
static gint gdk_input_common_init(gint include_core);
static gint  gdk_input_common_other_event (GdkEvent *event, 
					   XEvent *xevent, 
					   GdkInputWindow *input_window,
					   GdkDevicePrivate *gdkdev);
static void gdk_input_common_set_axes (guint32 deviceid, GdkAxisUse *axes);
static GdkTimeCoord * gdk_input_common_motion_events (GdkWindow *window,
						      guint32 deviceid,
						      guint32 start,
						      guint32 stop,
						      gint *nevents_return);
static void  gdk_input_common_get_pointer     (GdkWindow       *window,
					       guint32	   deviceid,
					       gdouble         *x,
					       gdouble         *y,
					       gdouble         *pressure,
					       gdouble         *xtilt,
					       gdouble         *ytilt,
					       GdkModifierType *mask);

62 63
#define GDK_MAX_DEVICE_CLASSES 13

Elliot Lee's avatar
Elliot Lee committed
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
/* Global variables */

static gint gdk_input_root_width;
static gint gdk_input_root_height;

static void
gdk_input_get_root_relative_geometry(Display *dpy, Window w, int *x_ret, int *y_ret,
			       int *width_ret, int *height_ret)
{
  Window root,parent;
  Window *children;
  int nchildren;
  int x,y,width,height;
  int xc,yc,widthc,heightc,border_widthc,depthc;
  
  XQueryTree(dpy,w,&root,&parent,&children,&nchildren);
  if (children) XFree(children);
  XGetGeometry(dpy,w,&root,&x,&y,&width,&height,&border_widthc,
	       &depthc);
  x += border_widthc;
  y += border_widthc;

  while (root != parent)
    {
      w = parent;
      XQueryTree(dpy,w,&root,&parent,&children,&nchildren);
      if (children) XFree(children);
      XGetGeometry(dpy,w,&root,&xc,&yc,&widthc,&heightc,
		   &border_widthc,&depthc);
      x += xc + border_widthc;
      y += yc + border_widthc;
    }

  if (x_ret)
    *x_ret = x;
  if (y_ret)
    *y_ret = y;
  if (width_ret)
    *width_ret = width;
  if (height_ret)
    *height_ret = height;
}

static GdkDevicePrivate *
gdk_input_device_new(XDeviceInfo *device, gint include_core)
{
  GdkDevicePrivate *gdkdev;
  gchar *tmp_name, *p;
  XAnyClassPtr class;
  gint i,j;

  gdkdev = g_new(GdkDevicePrivate,1);

  gdkdev->info.deviceid = device->id;
  if (device->name[0]) {
    gdkdev->info.name = g_new(char, strlen(device->name)+1);
    strcpy(gdkdev->info.name,device->name);
  } else {
    /* XFree86 3.2 gives an empty name to the default core devices,
       (fixed in 3.2A) */
    gdkdev->info.name = g_strdup("pointer");
    strcpy(gdkdev->info.name,"pointer");
    gdkdev->info.source = GDK_SOURCE_MOUSE;
  }

  gdkdev->info.mode = GDK_MODE_DISABLED;

  /* Try to figure out what kind of device this is by its name -
     could invite a very, very, long list... Lowercase name
     for comparison purposes */

  tmp_name = g_strdup(gdkdev->info.name);
  for (p = tmp_name; *p; p++)
    {
      if (*p >= 'A' && *p <= 'Z')
	*p += 'a' - 'A';
    }
  
  if (!strcmp (tmp_name, "pointer"))
    gdkdev->info.source = GDK_SOURCE_MOUSE;
  else if (!strcmp (tmp_name, "wacom") ||
	   !strcmp (tmp_name, "pen"))
    gdkdev->info.source = GDK_SOURCE_PEN;
  else if (!strcmp (tmp_name, "eraser"))
    gdkdev->info.source = GDK_SOURCE_ERASER;
  else if (!strcmp (tmp_name, "cursor"))
    gdkdev->info.source = GDK_SOURCE_CURSOR;
  else
    gdkdev->info.source = GDK_SOURCE_PEN;

  g_free(tmp_name);

  gdkdev->xdevice = NULL;

  /* step through the classes */

  gdkdev->info.num_axes = 0;
161 162
  gdkdev->info.num_keys = 0;
  gdkdev->info.keys = NULL;
Elliot Lee's avatar
Elliot Lee committed
163 164 165 166 167 168 169 170 171 172 173 174
  gdkdev->axes = 0;
  gdkdev->info.has_cursor = 0;
  gdkdev->needs_update = FALSE;
  gdkdev->claimed = FALSE;
  gdkdev->button_state = 0;

  class = device->inputclassinfo;
  for (i=0;i<device->num_classes;i++) 
    {
      switch (class->class) {
      case ButtonClass:
	{
175 176 177 178 179
	  break;
	}
      case KeyClass:
	{
	  XKeyInfo *xki = (XKeyInfo *)class;
180 181 182 183 184 185 186 187 188 189 190 191 192
	  /* Hack to catch XFree86 3.3.1 bug. Other devices better
	   * not have exactly 25 keys... 
	   */
	  if ((xki->min_keycode == 8) && (xki->max_keycode == 32))
	    {
	      gdkdev->info.num_keys = 32;
	      gdkdev->min_keycode = 0;
	    }
	  else
	    {
	      gdkdev->info.num_keys = xki->max_keycode - xki->min_keycode + 1;
	      gdkdev->min_keycode = xki->min_keycode;
	    }
193 194 195 196 197 198 199 200
	  gdkdev->info.keys = g_new (GdkDeviceKey, gdkdev->info.num_keys);

	  for (j=0; j<gdkdev->info.num_keys; j++)
	    {
	      gdkdev->info.keys[j].keyval = 0;
	      gdkdev->info.keys[j].modifiers = 0;
	    }

Elliot Lee's avatar
Elliot Lee committed
201 202 203 204 205 206 207 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 238 239 240 241 242 243 244 245 246 247 248 249 250
	  break;
	}
      case ValuatorClass:
	{
	  XValuatorInfo *xvi = (XValuatorInfo *)class;
	  gdkdev->info.num_axes = xvi->num_axes;
	  gdkdev->axes = g_new(GdkAxisInfo, xvi->num_axes);
	  gdkdev->info.axes = g_new(GdkAxisUse, xvi->num_axes);
	  for (j=0;j<xvi->num_axes;j++)
	    {
	      gdkdev->axes[j].resolution = 
		gdkdev->axes[j].xresolution = xvi->axes[j].resolution;
	      gdkdev->axes[j].min_value =
		gdkdev->axes[j].xmin_value = xvi->axes[j].min_value;
	      gdkdev->axes[j].max_value =
		gdkdev->axes[j].xmax_value = xvi->axes[j].max_value;
	      gdkdev->info.axes[j] = GDK_AXIS_IGNORE;
	    }
	  j=0;
	  if (j<xvi->num_axes)
	    gdkdev->info.axes[j++] = GDK_AXIS_X;
	  if (j<xvi->num_axes)
	    gdkdev->info.axes[j++] = GDK_AXIS_Y;
	  if (j<xvi->num_axes)
	    gdkdev->info.axes[j++] = GDK_AXIS_PRESSURE;
	  if (j<xvi->num_axes)
	    gdkdev->info.axes[j++] = GDK_AXIS_XTILT;
	  if (j<xvi->num_axes)
	    gdkdev->info.axes[j++] = GDK_AXIS_YTILT;
	  
	  /* set up reverse lookup on axis use */
	  for (j=GDK_AXIS_IGNORE;j<GDK_AXIS_LAST;j++)
	    gdkdev->axis_for_use[j] = -1;
	  
	  for (j=0;j<xvi->num_axes;j++)
	    if (gdkdev->info.axes[j] != GDK_AXIS_IGNORE)
	      gdkdev->axis_for_use[gdkdev->info.axes[j]] = j;
		       
	  break;
	}
      }
      class = (XAnyClassPtr)(((char *)class) + class->length);
    }
  /* return NULL if no axes */
  if (!gdkdev->info.num_axes || !gdkdev->axes ||
      (!include_core && device->use == IsXPointer))
    {
      g_free(gdkdev->info.name);
      if (gdkdev->axes)
	g_free(gdkdev->axes);
251 252
      if (gdkdev->info.keys)
	g_free(gdkdev->info.keys);
Elliot Lee's avatar
Elliot Lee committed
253 254 255 256 257 258 259
      g_free(gdkdev);
      return NULL;
    }

  if (device->use != IsXPointer)
      gdkdev->xdevice = XOpenDevice(gdk_display, gdkdev->info.deviceid);

260 261 262 263 264 265 266 267 268
  gdkdev->buttonpress_type = 0;
  gdkdev->buttonrelease_type = 0;
  gdkdev->keypress_type = 0;
  gdkdev->keyrelease_type = 0;
  gdkdev->motionnotify_type = 0;
  gdkdev->proximityin_type = 0;
  gdkdev->proximityout_type = 0;
  gdkdev->changenotify_type = 0;

Elliot Lee's avatar
Elliot Lee committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  return gdkdev;
}

static void
gdk_input_common_find_events(GdkWindow *window,
			     GdkDevicePrivate *gdkdev,
			     gint mask,
			     XEventClass *classes,
			     int *num_classes)
{
  gint i;
  XEventClass class;
  
  i = 0;
  /* We have to track press and release events in pairs to keep
284 285
     track of button state correctly and implement grabbing for
     the gxi support */
Elliot Lee's avatar
Elliot Lee committed
286 287
  if (mask & GDK_BUTTON_PRESS_MASK || mask & GDK_BUTTON_RELEASE_MASK)
    {
288 289 290 291 292
      DeviceButtonPress (gdkdev->xdevice, gdkdev->buttonpress_type,
			     class);
      if (class != 0)
	  classes[i++] = class;
      DeviceButtonPressGrab (gdkdev->xdevice, 0, class);
Elliot Lee's avatar
Elliot Lee committed
293 294 295 296 297 298 299 300 301 302 303 304 305
      if (class != 0)
	  classes[i++] = class;
      DeviceButtonRelease (gdkdev->xdevice, gdkdev->buttonrelease_type,
			   class);
      if (class != 0)
	  classes[i++] = class;
    }
  if (mask & GDK_POINTER_MOTION_MASK)
    {
      DeviceMotionNotify  (gdkdev->xdevice, gdkdev->motionnotify_type, class);
      if (class != 0)
	  classes[i++] = class;
    }
306 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
  else
    if (mask & (GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
		GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK |
		GDK_POINTER_MOTION_HINT_MASK))
      {
	/* Make sure gdkdev->motionnotify_type is set */
	DeviceMotionNotify  (gdkdev->xdevice, gdkdev->motionnotify_type, class);
      }
  if (mask & GDK_BUTTON1_MOTION_MASK)
    {
      DeviceButton1Motion  (gdkdev->xdevice, 0, class);
      if (class != 0)
	  classes[i++] = class;
    }
  if (mask & GDK_BUTTON2_MOTION_MASK)
    {
      DeviceButton2Motion  (gdkdev->xdevice, 0, class);
      if (class != 0)
	  classes[i++] = class;
    }
  if (mask & GDK_BUTTON3_MOTION_MASK)
    {
      DeviceButton3Motion  (gdkdev->xdevice, 0, class);
      if (class != 0)
	  classes[i++] = class;
    }
  if (mask & GDK_BUTTON_MOTION_MASK)
    {
      DeviceButtonMotion  (gdkdev->xdevice, 0, class);
      if (class != 0)
	  classes[i++] = class;
    }
Elliot Lee's avatar
Elliot Lee committed
338 339 340 341 342 343 344 345
  if (mask & GDK_POINTER_MOTION_HINT_MASK)
    {
      /* We'll get into trouble if the macros change, but at least we'll
	 know about it, and we avoid warnings now */
      DevicePointerMotionHint (gdkdev->xdevice, 0, class);
      if (class != 0)
	  classes[i++] = class;
    }
346 347 348 349 350 351 352 353 354 355 356 357
  if (mask & GDK_KEY_PRESS_MASK)
    {
      DeviceKeyPress (gdkdev->xdevice, gdkdev->keypress_type, class);
      if (class != 0)
	  classes[i++] = class;
    }
  if (mask & GDK_KEY_RELEASE_MASK)
    {
      DeviceKeyRelease (gdkdev->xdevice, gdkdev->keyrelease_type, class);
      if (class != 0)
	  classes[i++] = class;
    }
Elliot Lee's avatar
Elliot Lee committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  if (mask & GDK_PROXIMITY_IN_MASK)
    {
      ProximityIn   (gdkdev->xdevice, gdkdev->proximityin_type, class);
      if (class != 0)
	  classes[i++] = class;
    }
  if (mask & GDK_PROXIMITY_OUT_MASK)
    {
      ProximityOut  (gdkdev->xdevice, gdkdev->proximityout_type, class);
      if (class != 0)
	  classes[i++] = class;
    }

  *num_classes = i;
}

static void
gdk_input_common_select_events(GdkWindow *window,
			       GdkDevicePrivate *gdkdev)
{
378
  XEventClass classes[GDK_MAX_DEVICE_CLASSES];
Elliot Lee's avatar
Elliot Lee committed
379 380 381 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 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 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 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  gint num_classes;

  if (gdkdev->info.mode == GDK_MODE_DISABLED)
    gdk_input_common_find_events(window, gdkdev, 0, classes, &num_classes);
  else
    gdk_input_common_find_events(window, gdkdev, 
				 ((GdkWindowPrivate *)window)->extension_events,
				 classes, &num_classes);
  
  XSelectExtensionEvent (gdk_display,
			 GDK_WINDOW_XWINDOW(window),
			 classes, num_classes);
}

gint 
gdk_input_common_init(gint include_core)
{
  char **extensions;
  XDeviceInfo   *devices;
  int num_devices;
  int num_extensions, loop;
  Display *display = gdk_display;

  /* Init global vars */
  gdk_window_get_geometry(NULL,	/* use root window */
			  NULL,NULL,
			  &gdk_input_root_width,&gdk_input_root_height, 
			  NULL);

  /* Init XInput extension */
  
  extensions = XListExtensions(display, &num_extensions);
  for (loop = 0; loop < num_extensions &&
	 (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
  XFreeExtensionList(extensions);
  if (loop == num_extensions)	/* XInput extension not found */
    return FALSE;

  gdk_input_devices = 0;
  devices = XListInputDevices(display, &num_devices);
  
  for(loop=0; loop<num_devices; loop++)
    {
      GdkDevicePrivate *gdkdev = gdk_input_device_new(&devices[loop],
						      include_core);
      if (gdkdev)
	gdk_input_devices = g_list_append(gdk_input_devices, gdkdev);
    }
  XFreeDeviceList(devices);

  gdk_input_devices = g_list_append (gdk_input_devices, &gdk_input_core_info);

  return TRUE;
}

static void
gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
				 GdkInputWindow *input_window,
				 gint *axis_data,
				 gdouble *x, gdouble *y, gdouble *pressure,
				 gdouble *xtilt, gdouble *ytilt)
{
  GdkWindowPrivate *win_priv;

  int x_axis, y_axis, pressure_axis, xtilt_axis, ytilt_axis;

  double device_width, device_height;
  double x_offset, y_offset, x_scale, y_scale;

  win_priv = (GdkWindowPrivate *) input_window->window;

  x_axis = gdkdev->axis_for_use[GDK_AXIS_X];
  y_axis = gdkdev->axis_for_use[GDK_AXIS_Y];
  pressure_axis = gdkdev->axis_for_use[GDK_AXIS_PRESSURE];
  xtilt_axis = gdkdev->axis_for_use[GDK_AXIS_XTILT];
  ytilt_axis = gdkdev->axis_for_use[GDK_AXIS_YTILT];

  device_width = gdkdev->axes[x_axis].max_value - 
		   gdkdev->axes[x_axis].min_value;
  device_height = gdkdev->axes[y_axis].max_value - 
                    gdkdev->axes[y_axis].min_value;

  if (gdkdev->info.mode == GDK_MODE_SCREEN) 
    {
      x_scale = gdk_input_root_width / device_width;
      y_scale = gdk_input_root_height / device_height;

      x_offset = - input_window->root_x;
      y_offset = - input_window->root_y;
    }
  else				/* GDK_MODE_WINDOW */
    {
      double device_aspect = (device_height*gdkdev->axes[y_axis].resolution) /
	(device_width*gdkdev->axes[x_axis].resolution);

      if (device_aspect * win_priv->width >= win_priv->height)
	{
	  /* device taller than window */
	  x_scale = win_priv->width / device_width;
	  y_scale = (x_scale * gdkdev->axes[x_axis].resolution)
	    / gdkdev->axes[y_axis].resolution;

	  x_offset = 0;
	  y_offset = -(device_height * y_scale - 
			       win_priv->height)/2;
	}
      else
	{
	  /* window taller than device */
	  y_scale = win_priv->height / device_height;
	  x_scale = (y_scale * gdkdev->axes[y_axis].resolution)
	    / gdkdev->axes[x_axis].resolution;

	  y_offset = 0;
	  x_offset = - (device_width * x_scale - win_priv->width)/2;
	}
    }
  
  if (x) *x = x_offset + x_scale*axis_data[x_axis];
  if (y) *y = y_offset + y_scale*axis_data[y_axis];

  if (pressure)
    {
      if (pressure_axis != -1)
	*pressure = ((double)axis_data[pressure_axis] 
		     - gdkdev->axes[pressure_axis].min_value) 
	  / (gdkdev->axes[pressure_axis].max_value 
	     - gdkdev->axes[pressure_axis].min_value);
      else
	*pressure = 0.5;
    }

  if (xtilt)
    {
      if (xtilt_axis != -1)
	{
	  *xtilt = 2. * (double)(axis_data[xtilt_axis] - 
				 (gdkdev->axes[xtilt_axis].min_value +
				  gdkdev->axes[xtilt_axis].max_value)/2) /
	    (gdkdev->axes[xtilt_axis].max_value -
	     gdkdev->axes[xtilt_axis].min_value);
	}
      else *xtilt = 0;
    }
  
  if (ytilt)
    {
      if (ytilt_axis != -1)
	{
	  *ytilt = 2. * (double)(axis_data[ytilt_axis] - 
				 (gdkdev->axes[ytilt_axis].min_value +
				  gdkdev->axes[ytilt_axis].max_value)/2) /
	    (gdkdev->axes[ytilt_axis].max_value -
	     gdkdev->axes[ytilt_axis].min_value);
	}
      else
	*ytilt = 0;
    }
}

/* combine the state of the core device and the device state
   into one - for now we do this in a simple-minded manner -
   we just take the keyboard portion of the core device and
   the button portion (all of?) the device state.
   Any button remapping should go on here. */
static guint
gdk_input_translate_state(guint state, guint device_state)
{
  return device_state | (state & 0xFF);
}

static gint 
gdk_input_common_other_event (GdkEvent *event, 
			      XEvent *xevent, 
			      GdkInputWindow *input_window,
			      GdkDevicePrivate *gdkdev)
{
  if ((xevent->type == gdkdev->buttonpress_type) ||
      (xevent->type == gdkdev->buttonrelease_type)) 
    {
      XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *)(xevent);

      if (xdbe->type == gdkdev->buttonpress_type)
	{
	  event->button.type = GDK_BUTTON_PRESS;
	  gdkdev->button_state |= 1 << xdbe->button;
	}
      else
	{
	  event->button.type = GDK_BUTTON_RELEASE;
	  gdkdev->button_state &= ~(1 << xdbe->button);
	}
      event->button.window = input_window->window;
      event->button.time = xdbe->time;
      event->button.source = gdkdev->info.source;
      event->button.deviceid = xdbe->deviceid;

      gdk_input_translate_coordinates (gdkdev,input_window, xdbe->axis_data,
				       &event->button.x,&event->button.y,
				       &event->button.pressure,
				       &event->button.xtilt, 
				       &event->button.ytilt);
      event->button.state = gdk_input_translate_state(xdbe->state,xdbe->device_state);
      event->button.button = xdbe->button;

584 585 586 587 588 589 590 591
      if (gdk_show_events)
	g_print ("button %s:\t\twindow: %ld  device: %ld  x,y: %f %f  button: %d\n",
		 (event->button.type == GDK_BUTTON_PRESS) ? "press" : "release",
		 xdbe->window,
		 xdbe->deviceid,
		 event->button.x, event->button.y,
		 xdbe->button);

Elliot Lee's avatar
Elliot Lee committed
592 593 594
      return TRUE;
  }

595 596 597 598 599 600 601 602 603 604 605 606
  if ((xevent->type == gdkdev->keypress_type) ||
      (xevent->type == gdkdev->keyrelease_type))
    {
      XDeviceKeyEvent *xdke = (XDeviceKeyEvent *)(xevent);

      if (gdk_show_events)
	g_print ("device key %s:\twindow: %ld  device: %ld  keycode: %d\n",
		 (event->key.type == GDK_KEY_PRESS) ? "press" : "release",
		 xdke->window,
		 xdke->deviceid,
		 xdke->keycode);

607 608 609 610 611 612 613 614 615
      if (xdke->keycode < gdkdev->min_keycode ||
	  xdke->keycode >= gdkdev->min_keycode + gdkdev->info.num_keys)
	{
	  g_warning ("Invalid device key code received");
	  return FALSE;
	}
      
      event->key.keyval = gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].keyval;

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
      if (event->key.keyval == 0) 
	{
	  if (gdk_show_events)
	    g_print ("\t\ttranslation - NONE\n");
	  
	  return FALSE;
	}

      event->key.type = (xdke->type == gdkdev->keypress_type) ?
	GDK_KEY_PRESS : GDK_KEY_RELEASE;

      event->key.window = input_window->window;
      event->key.time = xdke->time;

      event->key.state = gdk_input_translate_state(xdke->state, xdke->device_state)
	| gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].modifiers;

633 634 635 636 637 638 639 640 641 642 643 644 645 646
      /* Add a string translation for the key event */
      if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xFF))
	{
	  event->key.length = 1;
	  event->key.string = g_new (gchar, 2);
	  event->key.string[0] = (gchar)event->key.keyval;
	  event->key.string[1] = 0;
	}
      else
	{
	  event->key.length = 0;
	  event->key.string = g_new0 (gchar, 1);
	}

647 648 649 650 651 652 653 654
      if (gdk_show_events)
	g_print ("\t\ttranslation - keyval: %d modifiers: %#x\n",
		 event->key.keyval,
		 event->key.state);

      return TRUE;
    }

Elliot Lee's avatar
Elliot Lee committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
  if (xevent->type == gdkdev->motionnotify_type) 
    {
      XDeviceMotionEvent *xdme = (XDeviceMotionEvent *)(xevent);

      gdk_input_translate_coordinates(gdkdev,input_window,xdme->axis_data,
				      &event->motion.x,&event->motion.y,
				      &event->motion.pressure,
				      &event->motion.xtilt, 
				      &event->motion.ytilt);

      event->motion.type = GDK_MOTION_NOTIFY;
      event->motion.window = input_window->window;
      event->motion.time = xdme->time;
      event->motion.deviceid = xdme->deviceid;
      event->motion.state = gdk_input_translate_state(xdme->state,
						      xdme->device_state);
671
      event->motion.is_hint = xdme->is_hint;
Elliot Lee's avatar
Elliot Lee committed
672 673 674 675
      event->motion.source = gdkdev->info.source;
      event->motion.deviceid = xdme->deviceid;

      if (gdk_show_events)
676
	g_print ("motion notify:\t\twindow: %ld  device: %ld  x,y: %f %f  state %#4x  hint: %s\n",
Elliot Lee's avatar
Elliot Lee committed
677 678 679
		 xdme->window,
		 xdme->deviceid,
		 event->motion.x, event->motion.y,
680 681
		 event->motion.state,
		 (xdme->is_hint) ? "true" : "false");
Elliot Lee's avatar
Elliot Lee committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 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
      
      
      return TRUE;
    }

  if (xevent->type == gdkdev->proximityin_type ||
      xevent->type == gdkdev->proximityout_type)
    {
      XProximityNotifyEvent *xpne = (XProximityNotifyEvent *)(xevent);

      event->proximity.type = (xevent->type == gdkdev->proximityin_type)?
  	GDK_PROXIMITY_IN:GDK_PROXIMITY_OUT;
      event->proximity.window = input_window->window;
      event->proximity.time = xpne->time;
      event->proximity.source = gdkdev->info.source;
      event->proximity.deviceid = xpne->deviceid;
      
      return TRUE;
  }

  return -1;			/* wasn't one of our event types */
}

static void
gdk_input_common_set_axes (guint32 deviceid, GdkAxisUse *axes)
{
  int i;
  GdkDevicePrivate *gdkdev = gdk_input_find_device(deviceid);
  g_return_if_fail (gdkdev != NULL);

  for (i=GDK_AXIS_IGNORE;i<GDK_AXIS_LAST;i++)
    {
      gdkdev->axis_for_use[i] = -1;
    }

  for (i=0;i<gdkdev->info.num_axes;i++)
    {
      gdkdev->info.axes[i] = axes[i];
      gdkdev->axis_for_use[axes[i]] = i;
    }
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
void gdk_input_common_set_key (guint32 deviceid,
			       guint   index,
			       guint   keyval,
			       GdkModifierType modifiers)
{
  GdkDevicePrivate *gdkdev = gdk_input_find_device(deviceid);
  
  gdkdev = gdk_input_find_device (deviceid);
  g_return_if_fail (gdkdev != NULL);
  g_return_if_fail (index < gdkdev->info.num_keys);

  gdkdev->info.keys[index].keyval = keyval;
  gdkdev->info.keys[index].modifiers = modifiers;
}

Elliot Lee's avatar
Elliot Lee committed
739 740 741 742 743 744 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 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static GdkTimeCoord *
gdk_input_common_motion_events (GdkWindow *window,
				guint32 deviceid,
				guint32 start,
				guint32 stop,
				gint *nevents_return)
{
  GdkTimeCoord *coords;
  XDeviceTimeCoord *device_coords;
  GdkInputWindow *input_window;
  GdkDevicePrivate *gdkdev;

  int mode_return;
  int axis_count_return;
  int i;

  gdkdev = gdk_input_find_device (deviceid);
  input_window = gdk_input_window_find (window);

  g_return_val_if_fail (gdkdev != NULL, NULL);
  g_return_val_if_fail (gdkdev->xdevice != NULL, NULL);
  g_return_val_if_fail (input_window != NULL, NULL);

  device_coords = XGetDeviceMotionEvents (gdk_display,
					  gdkdev->xdevice,
					  start, stop,
					  nevents_return, &mode_return,
					  &axis_count_return);

  if (device_coords)
    {
      coords = g_new (GdkTimeCoord, *nevents_return);
      
      for (i=0; i<*nevents_return; i++)
	{
	  gdk_input_translate_coordinates (gdkdev, input_window,
					   device_coords[i].data,
					   &coords[i].x, &coords[i].y,
					   &coords[i].pressure,
					   &coords[i].xtilt, &coords[i].ytilt);
	}
      XFreeDeviceMotionEvents (device_coords);

      return coords;
    }
  else
    return NULL;
}

static void 
gdk_input_common_get_pointer     (GdkWindow       *window,
				  guint32	   deviceid,
				  gdouble         *x,
				  gdouble         *y,
				  gdouble         *pressure,
				  gdouble         *xtilt,
				  gdouble         *ytilt,
				  GdkModifierType *mask)
{
  GdkDevicePrivate *gdkdev;
  GdkInputWindow *input_window;
  XDeviceState *state;
  XInputClass *input_class;
  gint x_int, y_int;
  gint i;

  /* we probably need to get the mask in any case */

  if (deviceid == GDK_CORE_POINTER)
    {
      gdk_window_get_pointer (window, &x_int, &y_int, mask);
      if (x) *x = x_int;
      if (y) *y = y_int;
      if (pressure) *pressure = 0.5;
      if (xtilt) *xtilt = 0;
      if (ytilt) *ytilt = 0;
    }
  else
    {
      if (mask)
	gdk_window_get_pointer (window, NULL, NULL, mask);
      
      gdkdev = gdk_input_find_device (deviceid);
      input_window = gdk_input_window_find (window);

      g_return_if_fail (gdkdev != NULL);
      g_return_if_fail (gdkdev->xdevice != NULL);
      g_return_if_fail (input_window != NULL);

      state = XQueryDeviceState (gdk_display, gdkdev->xdevice);
      input_class = state->data;
      for (i=0; i<state->num_classes; i++)
	{
	  switch (input_class->class)
	    {
	    case ValuatorClass:
835 836 837 838
	      gdk_input_translate_coordinates (gdkdev, input_window,
					       ((XValuatorState *)input_class)->valuators,
					       x, y, pressure,
					       xtilt, ytilt);
Elliot Lee's avatar
Elliot Lee committed
839 840 841 842 843 844
						       
						       
		break;
	    case ButtonClass:
	      if (mask)
		{
845 846 847 848 849
		  *mask &= 0xFF;
		  if (((XButtonState *)input_class)->num_buttons > 0)
		    *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
		  /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
		   * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
Elliot Lee's avatar
Elliot Lee committed
850 851 852 853 854 855 856 857 858
		}
	      break;
	    }
	  input_class = (XInputClass *)(((char *)input_class)+input_class->length);
	}
    }
}

#endif