summaryrefslogtreecommitdiff
path: root/gdk/x11/gdkinput.c
blob: ad4b1fcc9334ca6c2207a28e511740e22a396c84 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* 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.
 */
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "../config.h"
#include "gdk.h"
#include "gdkx.h"
#include "gdkprivate.h"
#include "gdkinput.h"


/* Forward declarations */

static gint gdk_input_enable_window (GdkWindow *window,
				     GdkDevicePrivate *gdkdev);
static gint gdk_input_disable_window (GdkWindow *window,
				      GdkDevicePrivate *gdkdev);
static GdkInputWindow *gdk_input_window_find (GdkWindow *window);
static GdkDevicePrivate *gdk_input_find_device (guint32 id);


/* Incorporate the specific routines depending on compilation options */

static GdkAxisUse gdk_input_core_axes[] = { GDK_AXIS_X, GDK_AXIS_Y };

static GdkDeviceInfo gdk_input_core_info =
{
  GDK_CORE_POINTER,
  "Core Pointer",
  GDK_SOURCE_MOUSE,
  GDK_MODE_SCREEN,
  TRUE,
  2,
  gdk_input_core_axes
};

/* Global variables  */

GdkInputVTable    gdk_input_vtable;
/* information about network port and host for gxid daemon */
gchar            *gdk_input_gxid_host;
gint              gdk_input_gxid_port;
gint              gdk_input_ignore_core;

/* Local variables */

static GList            *gdk_input_devices;
static GList            *gdk_input_windows;

#include "gdkinputnone.h"
#include "gdkinputcommon.h"
#include "gdkinputxfree.h"
#include "gdkinputgxi.h"

GList *
gdk_input_list_devices ()
{
  return gdk_input_devices;
}

void
gdk_input_set_source (guint32 deviceid, GdkInputSource source)
{
  GdkDevicePrivate *gdkdev = gdk_input_find_device(deviceid);
  g_return_if_fail (gdkdev != NULL);

  gdkdev->info.source = source;
}

gint
gdk_input_set_mode (guint32 deviceid, GdkInputMode mode)
{
  if (deviceid == GDK_CORE_POINTER)
    return FALSE;

  if (gdk_input_vtable.set_mode)
    return gdk_input_vtable.set_mode(deviceid,mode);
  else
    return FALSE;
}

void
gdk_input_set_axes (guint32 deviceid, GdkAxisUse *axes)
{
  if (deviceid != GDK_CORE_POINTER && gdk_input_vtable.set_axes)
    gdk_input_vtable.set_axes (deviceid, axes);
}

GdkTimeCoord *
gdk_input_motion_events (GdkWindow *window,
			 guint32 deviceid,
			 guint32 start,
			 guint32 stop,
			 gint *nevents_return)
{
  XTimeCoord *xcoords;
  GdkTimeCoord *coords;
  int i;

  if (deviceid == GDK_CORE_POINTER)
    {
      xcoords = XGetMotionEvents (gdk_display,
				  ((GdkWindowPrivate *)window)->xwindow,
				  start, stop, nevents_return);
      if (xcoords)
	{
	  coords = g_new (GdkTimeCoord, *nevents_return);
	  for (i=0; i<*nevents_return; i++)
	    {
	      coords[i].time = xcoords[i].time;
	      coords[i].x = xcoords[i].x;
	      coords[i].y = xcoords[i].y;
	      coords[i].pressure = 0.5;
	      coords[i].xtilt = 0.0;
	      coords[i].ytilt = 0.0;
	    }

	  XFree(xcoords);

	  return coords;
	}
      else
	return NULL;
    }
  else
    {
      if (gdk_input_vtable.motion_events)
	{
	  return gdk_input_vtable.motion_events(window,
						deviceid, start, stop,
						nevents_return);
	}
      else
	{
	  *nevents_return = 0;
	  return NULL;
	}
    }
}

static gint
gdk_input_enable_window (GdkWindow *window, GdkDevicePrivate *gdkdev)
{
  if (gdk_input_vtable.enable_window)
    return gdk_input_vtable.enable_window (window, gdkdev);
  else
    return TRUE;
}

static gint
gdk_input_disable_window (GdkWindow *window, GdkDevicePrivate *gdkdev)
{
  if (gdk_input_vtable.disable_window)
    return gdk_input_vtable.disable_window(window,gdkdev);
  else
    return TRUE;
}


static GdkInputWindow *
gdk_input_window_find(GdkWindow *window)
{
  GList *tmp_list;

  for (tmp_list=gdk_input_windows; tmp_list; tmp_list=tmp_list->next)
    if (((GdkInputWindow *)(tmp_list->data))->window == window)
      return (GdkInputWindow *)(tmp_list->data);

  return NULL;      /* Not found */
}

/* FIXME: this routine currently needs to be called between creation
   and the corresponding configure event (because it doesn't get the
   root_relative_geometry).  This should work with
   gtk_window_set_extension_events, but will likely fail in other
   cases */

void
gdk_input_set_extension_events (GdkWindow *window, gint mask,
				GdkExtensionMode mode)
{
  GList *tmp_list;
  GdkInputWindow *iw;

  g_return_if_fail (window != NULL);

  if (mode == GDK_EXTENSION_EVENTS_NONE)
    mask = 0;

  if (mask != 0)
    {
      iw = g_new(GdkInputWindow,1);

      iw->window = window;
      iw->mode = mode;

      iw->obscuring = NULL;
      iw->num_obscuring = 0;
      iw->grabbed = FALSE;

      gdk_input_windows = g_list_append(gdk_input_windows,iw);
      ((GdkWindowPrivate *)window)->extension_events = mask;

      /* Add enter window events to the event mask */
      /* FIXME, this is not needed for XINPUT_NONE */
      gdk_window_set_events (window,
			     gdk_window_get_events (window) | 
			     GDK_ENTER_NOTIFY_MASK);
    }
  else
    {
      iw = gdk_input_window_find (window);
      if (iw)
	{
	  gdk_input_windows = g_list_remove(gdk_input_windows,iw);
	  g_free(iw);
	}

      ((GdkWindowPrivate *)window)->extension_events = 0;
    }

  for (tmp_list = gdk_input_devices; tmp_list; tmp_list = tmp_list->next)
    {
      GdkDevicePrivate *gdkdev = (GdkDevicePrivate *)(tmp_list->data);

      if (gdkdev->info.deviceid != GDK_CORE_POINTER)
	{
	  if (mask != 0 && gdkdev->info.mode != GDK_MODE_DISABLED
	      && (gdkdev->info.has_cursor || mode == GDK_EXTENSION_EVENTS_ALL))
	    gdk_input_enable_window(window,gdkdev);
	  else
	    gdk_input_disable_window(window,gdkdev);
	}
    }
}

void
gdk_input_window_destroy (GdkWindow *window)
{
  GdkInputWindow *input_window;

  input_window = gdk_input_window_find (window);
  g_return_if_fail (input_window != NULL);

  gdk_input_windows = g_list_remove(gdk_input_windows,input_window);
  g_free(input_window);
}

void
gdk_input_exit (void)
{
  GList *tmp_list;
  GdkDevicePrivate *gdkdev;

  for (tmp_list = gdk_input_devices; tmp_list; tmp_list = tmp_list->next)
    {
      gdkdev = (GdkDevicePrivate *)(tmp_list->data);
      if (gdkdev->info.deviceid != GDK_CORE_POINTER)
	{
	  gdk_input_set_mode(gdkdev->info.deviceid,GDK_MODE_DISABLED);

	  g_free(gdkdev->info.name);
#ifndef XINPUT_NONE	  
	  g_free(gdkdev->axes);
#endif	  
	  g_free(gdkdev->info.axes);
	  g_free(gdkdev);
	}
    }

  g_list_free(gdk_input_devices);

  for (tmp_list = gdk_input_windows; tmp_list; tmp_list = tmp_list->next)
    {
      g_free(tmp_list->data);
    }
  g_list_free(gdk_input_windows);
}

static GdkDevicePrivate *
gdk_input_find_device(guint32 id)
{
  GList *tmp_list = gdk_input_devices;
  GdkDevicePrivate *gdkdev;
  while (tmp_list)
    {
      gdkdev = (GdkDevicePrivate *)(tmp_list->data);
      if (gdkdev->info.deviceid == id)
	return gdkdev;
      tmp_list = tmp_list->next;
    }
  return NULL;
}

void
gdk_input_window_get_pointer (GdkWindow       *window,
			      guint32	  deviceid,
			      gdouble         *x,
			      gdouble         *y,
			      gdouble         *pressure,
			      gdouble         *xtilt,
			      gdouble         *ytilt,
			      GdkModifierType *mask)
{
  if (gdk_input_vtable.get_pointer)
    gdk_input_vtable.get_pointer (window, deviceid, x, y, pressure,
				  xtilt, ytilt, mask);
}