gdkinput.c 12.5 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
24
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 26
 */

27
#include "config.h"
28

Elliot Lee's avatar
Elliot Lee committed
29 30 31 32
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

33
#include "gdkx.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
34 35 36
#include "gdkinput.h"
#include "gdkprivate.h"
#include "gdkinputprivate.h"
37 38
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
39
#include "gdkalias.h"
Elliot Lee's avatar
Elliot Lee committed
40

41 42 43 44
static GdkDeviceAxis gdk_input_core_axes[] = {
  { GDK_AXIS_X, 0, 0 },
  { GDK_AXIS_Y, 0, 0 }
};
Elliot Lee's avatar
Elliot Lee committed
45

Alex Larsson's avatar
Alex Larsson committed
46
void
47
_gdk_init_input_core (GdkDisplay *display)
Alex Larsson's avatar
Alex Larsson committed
48
{
Owen Taylor's avatar
Owen Taylor committed
49
  GdkDevicePrivate *private;
50

51
  display->core_pointer = g_object_new (GDK_TYPE_DEVICE, NULL);
Owen Taylor's avatar
Owen Taylor committed
52
  private = (GdkDevicePrivate *)display->core_pointer;
53

54 55 56 57 58 59 60 61
  display->core_pointer->name = "Core Pointer";
  display->core_pointer->source = GDK_SOURCE_MOUSE;
  display->core_pointer->mode = GDK_MODE_SCREEN;
  display->core_pointer->has_cursor = TRUE;
  display->core_pointer->num_axes = 2;
  display->core_pointer->axes = gdk_input_core_axes;
  display->core_pointer->num_keys = 0;
  display->core_pointer->keys = NULL;
Owen Taylor's avatar
Owen Taylor committed
62 63

  private->display = display;
Alex Larsson's avatar
Alex Larsson committed
64 65
}

66
static void gdk_device_class_init (GdkDeviceClass *klass);
67
static void gdk_device_dispose    (GObject        *object);
68

69 70
static gpointer gdk_device_parent_class = NULL;

Alex Larsson's avatar
Alex Larsson committed
71 72 73 74 75 76 77
GType
gdk_device_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
78
      const GTypeInfo object_info =
Matthias Clasen's avatar
Matthias Clasen committed
79 80 81 82
	{
	  sizeof (GdkDeviceClass),
	  (GBaseInitFunc) NULL,
	  (GBaseFinalizeFunc) NULL,
83
	  (GClassInitFunc) gdk_device_class_init,
Matthias Clasen's avatar
Matthias Clasen committed
84 85 86 87 88 89
	  NULL,           /* class_finalize */
	  NULL,           /* class_data */
	  sizeof (GdkDevicePrivate),
	  0,              /* n_preallocs */
	  (GInstanceInitFunc) NULL,
	};
90

Alex Larsson's avatar
Alex Larsson committed
91
      object_type = g_type_register_static (G_TYPE_OBJECT,
92 93
					    g_intern_static_string ("GdkDevice"),
					    &object_info, 0);
Alex Larsson's avatar
Alex Larsson committed
94
    }
95

Alex Larsson's avatar
Alex Larsson committed
96 97 98
  return object_type;
}

99 100 101 102 103
static void
gdk_device_class_init (GdkDeviceClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

104 105
  gdk_device_parent_class = g_type_class_peek_parent (klass);

106
  object_class->dispose  = gdk_device_dispose;
107 108 109
}

static void
110
gdk_device_dispose (GObject *object)
111
{
112
  GdkDevicePrivate *gdkdev = (GdkDevicePrivate *) object;
113

114
  if (gdkdev->display && !GDK_IS_CORE (gdkdev))
115
    {
116
#ifndef XINPUT_NONE
117
      if (gdkdev->xdevice)
118 119 120 121
	{
	  XCloseDevice (GDK_DISPLAY_XDISPLAY (gdkdev->display), gdkdev->xdevice);
	  gdkdev->xdevice = NULL;
	}
122
      g_free (gdkdev->axes);
Thomas Jaeger's avatar
Thomas Jaeger committed
123
      g_free (gdkdev->axis_data);
124
      gdkdev->axes = NULL;
Thomas Jaeger's avatar
Thomas Jaeger committed
125
      gdkdev->axis_data = NULL;
126 127
#endif /* !XINPUT_NONE */

128 129 130
      g_free (gdkdev->info.name);
      g_free (gdkdev->info.keys);
      g_free (gdkdev->info.axes);
131 132 133 134

      gdkdev->info.name = NULL;
      gdkdev->info.keys = NULL;
      gdkdev->info.axes = NULL;
135 136
    }

137
  G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
138 139
}

140 141 142 143 144
/**
 * gdk_devices_list:
 *
 * Returns the list of available input devices for the default display.
 * The list is statically allocated and should not be freed.
145
 *
146 147
 * Return value: a list of #GdkDevice
 **/
Elliot Lee's avatar
Elliot Lee committed
148
GList *
149
gdk_devices_list (void)
Elliot Lee's avatar
Elliot Lee committed
150
{
Owen Taylor's avatar
Owen Taylor committed
151
  return gdk_display_list_devices (gdk_display_get_default ());
152 153 154 155
}

/**
 * gdk_display_list_devices:
Matthias Clasen's avatar
Matthias Clasen committed
156
 * @display: a #GdkDisplay
157 158 159
 *
 * Returns the list of available input devices attached to @display.
 * The list is statically allocated and should not be freed.
160
 *
161
 * Return value: a list of #GdkDevice
Matthias Clasen's avatar
Matthias Clasen committed
162 163
 *
 * Since: 2.2
164
 **/
165
GList *
166 167 168
gdk_display_list_devices (GdkDisplay *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
169

170
  return GDK_DISPLAY_X11 (display)->input_devices;
Elliot Lee's avatar
Elliot Lee committed
171 172 173
}

void
174 175
gdk_device_set_source (GdkDevice      *device,
		       GdkInputSource  source)
Elliot Lee's avatar
Elliot Lee committed
176
{
177
  g_return_if_fail (device != NULL);
Elliot Lee's avatar
Elliot Lee committed
178

179
  device->source = source;
Elliot Lee's avatar
Elliot Lee committed
180 181
}

182 183 184 185 186
void
gdk_device_set_key (GdkDevice      *device,
		    guint           index,
		    guint           keyval,
		    GdkModifierType modifiers)
Elliot Lee's avatar
Elliot Lee committed
187
{
188 189
  g_return_if_fail (device != NULL);
  g_return_if_fail (index < device->num_keys);
Elliot Lee's avatar
Elliot Lee committed
190

191 192
  device->keys[index].keyval = keyval;
  device->keys[index].modifiers = modifiers;
Elliot Lee's avatar
Elliot Lee committed
193 194 195
}

void
196 197 198
gdk_device_set_axis_use (GdkDevice   *device,
			 guint        index,
			 GdkAxisUse   use)
Elliot Lee's avatar
Elliot Lee committed
199
{
200 201
  g_return_if_fail (device != NULL);
  g_return_if_fail (index < device->num_axes);
Elliot Lee's avatar
Elliot Lee committed
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  device->axes[index].use = use;

  switch (use)
    {
    case GDK_AXIS_X:
    case GDK_AXIS_Y:
      device->axes[index].min = 0.;
      device->axes[index].max = 0.;
      break;
    case GDK_AXIS_XTILT:
    case GDK_AXIS_YTILT:
      device->axes[index].min = -1.;
      device->axes[index].max = 1;
      break;
    default:
      device->axes[index].min = 0.;
      device->axes[index].max = 1;
      break;
    }
222 223
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static gboolean
impl_coord_in_window (GdkWindow *window,
		      int impl_x,
		      int impl_y)
{
  GdkWindowObject *priv = (GdkWindowObject *)window;

  if (impl_x < priv->abs_x ||
      impl_x > priv->abs_x + priv->width)
    return FALSE;
  if (impl_y < priv->abs_y ||
      impl_y > priv->abs_y + priv->height)
    return FALSE;
  return TRUE;
}

240 241 242 243 244 245 246 247
/**
 * gdk_device_get_history:
 * @device: a #GdkDevice
 * @window: the window with respect to which which the event coordinates will be reported
 * @start: starting timestamp for range of events to return
 * @stop: ending timestamp for the range of events to return
 * @events: location to store a newly-allocated array of #GdkTimeCoord, or %NULL
 * @n_events: location to store the length of @events, or %NULL
248
 *
249 250 251 252 253 254
 * Obtains the motion history for a device; given a starting and
 * ending timestamp, return all events in the motion history for
 * the device in the given range of time. Some windowing systems
 * do not support motion history, in which case, %FALSE will
 * be returned. (This is not distinguishable from the case where
 * motion history is supported and no events were found.)
255
 *
256 257 258
 * Return value: %TRUE if the windowing system supports motion history and
 *  at least one event was found.
 **/
259 260 261 262 263 264 265
gboolean
gdk_device_get_history  (GdkDevice         *device,
			 GdkWindow         *window,
			 guint32            start,
			 guint32            stop,
			 GdkTimeCoord    ***events,
			 gint              *n_events)
Elliot Lee's avatar
Elliot Lee committed
266
{
267
  GdkTimeCoord **coords = NULL;
268
  GdkWindow *impl_window;
269 270
  gboolean result = FALSE;
  int tmp_n_events = 0;
Elliot Lee's avatar
Elliot Lee committed
271

272
  g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), FALSE);
273

274 275
  impl_window = _gdk_window_get_impl_window (window);

276
  if (GDK_WINDOW_DESTROYED (window))
277 278
    /* Nothing */ ;
  else if (GDK_IS_CORE (device))
Elliot Lee's avatar
Elliot Lee committed
279
    {
280
      XTimeCoord *xcoords;
281

282
      xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
283
				  GDK_DRAWABLE_XID (impl_window),
284
				  start, stop, &tmp_n_events);
Elliot Lee's avatar
Elliot Lee committed
285 286
      if (xcoords)
	{
287
	  GdkWindowObject *priv = (GdkWindowObject *)window;
288 289
          int i, j;

290
	  coords = _gdk_device_allocate_history (device, tmp_n_events);
291
	  j = 0;
292 293

	  for (i = 0; i < tmp_n_events; i++)
Elliot Lee's avatar
Elliot Lee committed
294
	    {
295 296 297 298 299 300 301
	      if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
		{
		  coords[j]->time = xcoords[i].time;
		  coords[j]->axes[0] = xcoords[i].x - priv->abs_x;
		  coords[j]->axes[1] = xcoords[i].y - priv->abs_y;
		  j++;
		}
Elliot Lee's avatar
Elliot Lee committed
302 303
	    }

304
	  XFree (xcoords);
Elliot Lee's avatar
Elliot Lee committed
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
          /* free the events we allocated too much */
          for (i = j; i < tmp_n_events; i++)
            {
              g_free (coords[i]);
              coords[i] = NULL;
            }

          tmp_n_events = j;

          if (tmp_n_events > 0)
            {
              result = TRUE;
            }
          else
            {
              gdk_device_free_history (coords, tmp_n_events);
              coords = NULL;
            }
Elliot Lee's avatar
Elliot Lee committed
324 325 326
	}
    }
  else
327 328 329 330
    result = _gdk_device_get_history (device, window, start, stop, &coords, &tmp_n_events);

  if (n_events)
    *n_events = tmp_n_events;
331

332 333 334 335 336 337
  if (events)
    *events = coords;
  else if (coords)
    gdk_device_free_history (coords, tmp_n_events);

  return result;
Elliot Lee's avatar
Elliot Lee committed
338 339
}

340
GdkTimeCoord **
341 342
_gdk_device_allocate_history (GdkDevice *device,
			      gint       n_events)
Elliot Lee's avatar
Elliot Lee committed
343
{
344 345 346 347 348 349 350 351
  GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
  gint i;

  for (i=0; i<n_events; i++)
    result[i] = g_malloc (sizeof (GdkTimeCoord) -
			  sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));

  return result;
Elliot Lee's avatar
Elliot Lee committed
352 353
}

354
void
355 356
gdk_device_free_history (GdkTimeCoord **events,
			 gint           n_events)
Elliot Lee's avatar
Elliot Lee committed
357
{
358
  gint i;
359

360 361
  for (i=0; i<n_events; i++)
    g_free (events[i]);
Elliot Lee's avatar
Elliot Lee committed
362

363 364
  g_free (events);
}
Elliot Lee's avatar
Elliot Lee committed
365

366 367
static void
unset_extension_events (GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
368
{
369 370 371 372
  GdkWindowObject *window_private;
  GdkWindowObject *impl_window;
  GdkDisplayX11 *display_x11;
  GdkInputWindow *iw;
Elliot Lee's avatar
Elliot Lee committed
373

374 375 376
  window_private = (GdkWindowObject*) window;
  impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
  iw = impl_window->input_window;
377

378
  display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
Elliot Lee's avatar
Elliot Lee committed
379

380 381 382 383 384 385 386 387 388 389 390 391 392
  if (window_private->extension_events != 0)
    {
      g_assert (iw != NULL);
      g_assert (g_list_find (iw->windows, window) != NULL);

      iw->windows = g_list_remove (iw->windows, window);
      if (iw->windows == NULL)
	{
	  impl_window->input_window = NULL;
	  display_x11->input_windows = g_list_remove (display_x11->input_windows, iw);
	  g_free (iw);
	}
    }
Elliot Lee's avatar
Elliot Lee committed
393

394 395
  window_private->extension_events = 0;
}
Elliot Lee's avatar
Elliot Lee committed
396 397 398 399 400

void
gdk_input_set_extension_events (GdkWindow *window, gint mask,
				GdkExtensionMode mode)
{
401
  GdkWindowObject *window_private;
402
  GdkWindowObject *impl_window;
Elliot Lee's avatar
Elliot Lee committed
403
  GdkInputWindow *iw;
404
  GdkDisplayX11 *display_x11;
405 406 407
#ifndef XINPUT_NONE
  GList *tmp_list;
#endif
Elliot Lee's avatar
Elliot Lee committed
408 409

  g_return_if_fail (window != NULL);
410
  g_return_if_fail (GDK_WINDOW_IS_X11 (window));
411

412
  window_private = (GdkWindowObject*) window;
413
  display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (window));
414
  if (GDK_WINDOW_DESTROYED (window))
415
    return;
Elliot Lee's avatar
Elliot Lee committed
416

417 418 419 420 421
  impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);

  if (mode == GDK_EXTENSION_EVENTS_ALL && mask != 0)
    mask |= GDK_ALL_DEVICES_MASK;

Elliot Lee's avatar
Elliot Lee committed
422 423 424
  if (mode == GDK_EXTENSION_EVENTS_NONE)
    mask = 0;

425
  iw = impl_window->input_window;
426

Elliot Lee's avatar
Elliot Lee committed
427 428
  if (mask != 0)
    {
429
      if (!iw)
430 431
	{
	  iw = g_new0 (GdkInputWindow,1);
Elliot Lee's avatar
Elliot Lee committed
432

433
	  iw->impl_window = (GdkWindow *)impl_window;
Elliot Lee's avatar
Elliot Lee committed
434

435 436
	  iw->windows = NULL;
	  iw->grabbed = FALSE;
Elliot Lee's avatar
Elliot Lee committed
437

438
	  display_x11->input_windows = g_list_append (display_x11->input_windows, iw);
Elliot Lee's avatar
Elliot Lee committed
439

440
#ifndef XINPUT_NONE
441 442 443
	  /* we might not receive ConfigureNotify so get the root_relative_geometry
	   * now, just in case */
	  _gdk_input_get_root_relative_geometry (window, &iw->root_x, &iw->root_y);
444
#endif /* !XINPUT_NONE */
445 446 447 448 449 450
	  impl_window->input_window = iw;
	}

      if (window_private->extension_events == 0)
	iw->windows = g_list_append (iw->windows, window);
      window_private->extension_events = mask;
Elliot Lee's avatar
Elliot Lee committed
451 452 453
    }
  else
    {
454
      unset_extension_events (window);
Elliot Lee's avatar
Elliot Lee committed
455 456
    }

457
#ifndef XINPUT_NONE
458
  for (tmp_list = display_x11->input_devices; tmp_list; tmp_list = tmp_list->next)
Elliot Lee's avatar
Elliot Lee committed
459
    {
460
      GdkDevicePrivate *gdkdev = tmp_list->data;
Elliot Lee's avatar
Elliot Lee committed
461

462
      if (!GDK_IS_CORE (gdkdev))
463
	_gdk_input_select_events ((GdkWindow *)impl_window, gdkdev);
Elliot Lee's avatar
Elliot Lee committed
464
    }
465
#endif /* !XINPUT_NONE */
Elliot Lee's avatar
Elliot Lee committed
466 467 468
}

void
469
_gdk_input_window_destroy (GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
470
{
471
  unset_extension_events (window);
Elliot Lee's avatar
Elliot Lee committed
472 473
}

474 475
/**
 * gdk_device_get_axis:
476
 * @device: a #GdkDevice
477 478 479
 * @axes: pointer to an array of axes
 * @use: the use to look for
 * @value: location to store the found value.
480
 *
481 482
 * Interprets an array of double as axis values for a given device,
 * and locates the value in the array for a given axis use.
483
 *
484
 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
485 486
 **/
gboolean
Owen Taylor's avatar
Owen Taylor committed
487 488 489 490
gdk_device_get_axis (GdkDevice  *device,
		     gdouble    *axes,
		     GdkAxisUse  use,
		     gdouble    *value)
Elliot Lee's avatar
Elliot Lee committed
491
{
492
  gint i;
493

494
  g_return_val_if_fail (device != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
495

496 497
  if (axes == NULL)
    return FALSE;
498

499 500 501 502 503 504 505
  for (i=0; i<device->num_axes; i++)
    if (device->axes[i].use == use)
      {
	if (value)
	  *value = axes[i];
	return TRUE;
      }
506

507
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
508
}
509 510 511

#define __GDK_INPUT_C__
#include "gdkaliasdef.c"