gdkevents-win32.c 81.5 KB
Newer Older
1
2
3
4
5
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 1998-1999 Tor Lillqvist
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
8
9
10
11
12
 * 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
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
17
18
19
20
21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 * 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/. 
 */

#include "config.h"

/* Cannot use TrackMouseEvent, as the stupid WM_MOUSELEAVE message
 * doesn't tell us where the mouse has gone. Thus we cannot use it to
 * generate a correct GdkNotifyType. Pity, as using TrackMouseEvent
 * otherwise would make it possible to reliably generate
 * GDK_LEAVE_NOTIFY events, which would help get rid of those pesky
 * tooltips sometimes popping up in the wrong place.
 */
/* define USE_TRACKMOUSEEVENT */

#include <stdio.h>

41
#include "gdkprivate-win32.h"
42
43
44
45
46
47
48
49
50
51

#include <objbase.h>
#include <imm.h>

#ifdef HAVE_DIMM_H
#include <dimm.h>
#else
#include "surrogate-dimm.h"
#endif

Tor Lillqvist's avatar
Tor Lillqvist committed
52
53
54
55
56
#include "gdk.h"
#include "gdkinternals.h"
#include "gdkinput-win32.h"

#include "gdkkeysyms.h"
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

#define PING() printf("%s: %d\n",__FILE__,__LINE__),fflush(stdout)

typedef struct _GdkIOClosure GdkIOClosure;
typedef struct _GdkEventPrivate GdkEventPrivate;

typedef enum
{
  /* Following flag is set for events on the event queue during
   * translation and cleared afterwards.
   */
  GDK_EVENT_PENDING = 1 << 0
} GdkEventFlags;

struct _GdkIOClosure
{
  GdkInputFunction function;
  GdkInputCondition condition;
  GdkDestroyNotify notify;
  gpointer data;
};

struct _GdkEventPrivate
{
  GdkEvent event;
  guint    flags;
};

/* 
 * Private function declarations
 */

static GdkFilterReturn
90
		 gdk_event_apply_filters(MSG      *msg,
91
92
93
					 GdkEvent *event,
					 GList    *filters);
static gboolean  gdk_event_translate	(GdkEvent *event, 
94
					 MSG      *msg,
95
96
97
98
					 gboolean *ret_val_flagp,
					 gint     *ret_valp);
static gboolean  gdk_event_prepare      (gpointer  source_data, 
				 	 GTimeVal *current_time,
99
100
					 gint     *timeout,
					 gpointer  user_data);
101
static gboolean  gdk_event_check        (gpointer  source_data,
102
103
				 	 GTimeVal *current_time,
					 gpointer  user_data);
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
static gboolean  gdk_event_dispatch     (gpointer  source_data,
					 GTimeVal *current_time,
					 gpointer  user_data);

/* Private variable declarations
 */

static GdkWindow *p_grab_window = NULL; /* Window that currently
					 * holds the pointer grab
					 */

static GdkWindow *k_grab_window = NULL; /* Window the holds the
					 * keyboard grab
					 */

static GList *client_filters;	/* Filters for client messages */

static gboolean p_grab_automatic;
static GdkEventMask p_grab_mask;
static gboolean p_grab_owner_events, k_grab_owner_events;
static HCURSOR p_grab_cursor;

static GSourceFuncs event_funcs = {
  gdk_event_prepare,
  gdk_event_check,
  gdk_event_dispatch,
  (GDestroyNotify)g_free
};

GPollFD event_poll_fd;

135
136
137
static GdkWindow *current_window = NULL;
static gint current_x, current_y;
static gdouble current_x_root, current_y_root;
138
static UINT gdk_ping_msg;
139
140
static gboolean ignore_wm_char = FALSE;
static gboolean is_altgr_key = FALSE;
141

142
143
static IActiveIMMApp *active_imm_app = NULL;
static IActiveIMMMessagePumpOwner *active_imm_msgpump_owner = NULL;
144
145

typedef BOOL (WINAPI *PFN_TrackMouseEvent) (LPTRACKMOUSEEVENT);
146
static PFN_TrackMouseEvent track_mouse_event = NULL;
147

148
static gboolean use_ime_composition = FALSE;
149

150
151
152
153
154
static LRESULT 
real_window_procedure (HWND   hwnd,
		       UINT   message,
		       WPARAM wparam,
		       LPARAM lparam)
155
{
156
  GdkEventPrivate event;
157
158
159
160
161
162
163
  GdkEvent *eventp;
  MSG msg;
  DWORD pos;
  LRESULT lres;
  gint ret_val;
  gboolean ret_val_flag;

164
  msg.hwnd = hwnd;
165
  msg.message = message;
166
167
  msg.wParam = wparam;
  msg.lParam = lparam;
168
169
170
171
172
  msg.time = GetTickCount ();
  pos = GetMessagePos ();
  msg.pt.x = LOWORD (pos);
  msg.pt.y = HIWORD (pos);

173
174
  event.flags = GDK_EVENT_PENDING;
  if (gdk_event_translate (&event.event, &msg, &ret_val_flag, &ret_val))
175
    {
176
      event.flags &= ~GDK_EVENT_PENDING;
177
#if 1
178
      if (event.event.any.type == GDK_CONFIGURE)
179
180
	{
	  /* Compress configure events */
181
	  GList *list = gdk_queued_events;
182
183
184

	  while (list != NULL
		 && (((GdkEvent *)list->data)->any.type != GDK_CONFIGURE
185
		     || ((GdkEvent *)list->data)->any.window != event.event.any.window))
186
187
188
	    list = list->next;
	  if (list != NULL)
	    {
189
190
	      GDK_NOTE (EVENTS, g_print ("... compressing an CONFIGURE event\n"));

191
192
	      *((GdkEvent *)list->data) = event.event;
	      gdk_drawable_unref (event.event.any.window);
193
194
195
196
197
	      /* Wake up WaitMessage */
	      PostMessage (NULL, gdk_ping_msg, 0, 0);
	      return FALSE;
	    }
	}
198
      else if (event.event.any.type == GDK_EXPOSE)
199
200
	{
	  /* Compress expose events */
201
	  GList *list = gdk_queued_events;
202
203
204

	  while (list != NULL
		 && (((GdkEvent *)list->data)->any.type != GDK_EXPOSE
205
		     || ((GdkEvent *)list->data)->any.window != event.event.any.window))
206
207
208
209
210
	    list = list->next;
	  if (list != NULL)
	    {
	      GdkRectangle u;

211
	      GDK_NOTE (EVENTS, g_print ("... compressing an EXPOSE event\n"));
212
	      gdk_rectangle_union (&event.event.expose.area,
213
214
215
				   &((GdkEvent *)list->data)->expose.area,
				   &u);
	      ((GdkEvent *)list->data)->expose.area = u;
216
	      gdk_drawable_unref (event.event.any.window);
217
#if 0
218
219
	      /* Wake up WaitMessage */
	      PostMessage (NULL, gdk_ping_msg, 0, 0);
220
#endif
221
222
223
224
225
	      return FALSE;
	    }
	}
#endif
      eventp = gdk_event_new ();
226
      *((GdkEventPrivate *) eventp) = event;
227
228
229
230
231
232
233

      /* Philippe Colantoni <colanton@aris.ss.uci.edu> suggests this
       * in order to handle events while opaque resizing neatly.  I
       * don't want it as default. Set the
       * GDK_EVENT_FUNC_FROM_WINDOW_PROC env var to get this
       * behaviour.
       */
234
      if (gdk_event_func_from_window_proc && gdk_event_func)
235
236
237
	{
	  GDK_THREADS_ENTER ();
	  
238
	  (*gdk_event_func) (eventp, gdk_event_data);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
	  gdk_event_free (eventp);
	  
	  GDK_THREADS_LEAVE ();
	}
      else
	{
	  gdk_event_queue_append (eventp);
#if 1
	  /* Wake up WaitMessage */
	  PostMessage (NULL, gdk_ping_msg, 0, 0);
#endif
	}
      
      if (ret_val_flag)
	return ret_val;
      else
	return FALSE;
    }

  if (ret_val_flag)
    return ret_val;
  else
    {
262
263
264
      if (active_imm_app == NULL
	  || (*active_imm_app->lpVtbl->OnDefWindowProc) (active_imm_app, hwnd, message, wparam, lparam, &lres) == S_FALSE)
	return DefWindowProc (hwnd, message, wparam, lparam);
265
266
267
268
269
      else
	return lres;
    }
}

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
LRESULT CALLBACK
gdk_window_procedure (HWND   hwnd,
		      UINT   message,
		      WPARAM wparam,
		      LPARAM lparam)
{
  LRESULT retval;

  GDK_NOTE (EVENTS, g_print ("gdk_window_procedure: %#lx %s\n",
			     (gulong) hwnd, gdk_win32_message_name (message)));

  retval = real_window_procedure (hwnd, message, wparam, lparam);

  GDK_NOTE (EVENTS, g_print ("gdk_window_procedure: %#lx returns %ld\n",
			     (gulong) hwnd, retval));

  return retval;
}

289
290
291
292
void 
gdk_events_init (void)
{
  HRESULT hres;
293
#ifdef USE_TRACKMOUSEEVENT
294
295
  HMODULE user32, imm32;
  HINSTANCE commctrl32;
296
#endif
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

  gdk_ping_msg = RegisterWindowMessage ("gdk-ping");
  GDK_NOTE (EVENTS, g_print ("gdk-ping = %#.03x\n",
			     gdk_ping_msg));

  g_source_add (GDK_PRIORITY_EVENTS, TRUE, &event_funcs, NULL, NULL, NULL);

  event_poll_fd.fd = G_WIN32_MSG_HANDLE;
  event_poll_fd.events = G_IO_IN;
  
  g_main_add_poll (&event_poll_fd, GDK_PRIORITY_EVENTS);

  hres = CoCreateInstance (&CLSID_CActiveIMM,
			   NULL,
			   CLSCTX_ALL,
			   &IID_IActiveIMMApp,
313
			   (LPVOID *) &active_imm_app);
314
315
316
  
  if (hres == S_OK)
    {
317
      GDK_NOTE (EVENTS, g_print ("IActiveIMMApp created %p\n",
318
319
				 active_imm_app));
      (*active_imm_app->lpVtbl->Activate) (active_imm_app, TRUE);
320
      
321
      hres = (*active_imm_app->lpVtbl->QueryInterface) (active_imm_app, &IID_IActiveIMMMessagePumpOwner, &active_imm_msgpump_owner);
322
      GDK_NOTE (EVENTS, g_print ("IActiveIMMMessagePumpOwner created %p\n",
323
324
				 active_imm_msgpump_owner));
      (active_imm_msgpump_owner->lpVtbl->Start) (active_imm_msgpump_owner);
325
326
327
328
    }

#ifdef USE_TRACKMOUSEEVENT
  user32 = GetModuleHandle ("user32.dll");
329
  if ((track_mouse_event = GetProcAddress (user32, "TrackMouseEvent")) == NULL)
330
331
    {
      if ((commctrl32 = LoadLibrary ("commctrl32.dll")) != NULL)
332
	track_mouse_event = (PFN_TrackMouseEvent)
333
334
	  GetProcAddress (commctrl32, "_TrackMouseEvent");
    }
335
  if (track_mouse_event != NULL)
336
337
    GDK_NOTE (EVENTS, g_print ("Using TrackMouseEvent to detect leave events\n"));
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
338
  if (IS_WIN_NT () && (windows_version & 0xFF) == 5)
339
340
341
342
343
344
    {
      /* On Win2k (Beta 3, at least) WM_IME_CHAR doesn't seem to work
       * correctly for non-Unicode applications. Handle
       * WM_IME_COMPOSITION with GCS_RESULTSTR instead, fetch the
       * Unicode char from the IME with ImmGetCompositionStringW().
       */
345
      use_ime_composition = TRUE;
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    }
}

/*
 *--------------------------------------------------------------
 * gdk_events_pending
 *
 *   Returns if events are pending on the queue.
 *
 * Arguments:
 *
 * Results:
 *   Returns TRUE if events are pending
 *
 * Side effects:
 *
 *--------------------------------------------------------------
 */

gboolean
gdk_events_pending (void)
{
  MSG msg;

370
371
  return (gdk_event_queue_find_first() ||
	  PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE));
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
}

/*
 *--------------------------------------------------------------
 * gdk_event_get_graphics_expose
 *
 *   Waits for a GraphicsExpose or NoExpose event
 *
 * Arguments:
 *
 * Results: 
 *   For GraphicsExpose events, returns a pointer to the event
 *   converted into a GdkEvent Otherwise, returns NULL.
 *
 * Side effects:
 *
 *-------------------------------------------------------------- */

GdkEvent*
gdk_event_get_graphics_expose (GdkWindow *window)
{
393
  MSG msg;
394
395
396
397
398
399
  GdkEvent *event;

  g_return_val_if_fail (window != NULL, NULL);
  
  GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose\n"));

400
#if 0 /* ??? */
401
402
403
  /* Some nasty bugs here, just return NULL for now. */
  return NULL;
#else
404
  if (PeekMessage (&msg, GDK_WINDOW_HWND (window), WM_PAINT, WM_PAINT, PM_REMOVE))
405
406
407
    {
      event = gdk_event_new ();
      
408
      if (gdk_event_translate (event, &msg, NULL, NULL))
409
410
411
412
413
414
415
416
417
	return event;
      else
	gdk_event_free (event);
    }
  
  return NULL;	
#endif
}

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
static char *
event_mask_string (GdkEventMask mask)
{
  static char bfr[500];
  char *p = bfr;

  *p = '\0';
#define BIT(x) \
  if (mask & GDK_##x##_MASK) \
    p += sprintf (p, "%s" #x, (p > bfr ? " " : ""))
  BIT(EXPOSURE);
  BIT(POINTER_MOTION);
  BIT(POINTER_MOTION_HINT);
  BIT(BUTTON_MOTION);
  BIT(BUTTON1_MOTION);
  BIT(BUTTON2_MOTION);
  BIT(BUTTON3_MOTION);
  BIT(BUTTON_PRESS);
  BIT(BUTTON_RELEASE);
  BIT(KEY_PRESS);
  BIT(KEY_RELEASE);
  BIT(ENTER_NOTIFY);
  BIT(LEAVE_NOTIFY);
  BIT(FOCUS_CHANGE);
  BIT(STRUCTURE);
  BIT(PROPERTY_CHANGE);
  BIT(VISIBILITY_NOTIFY);
  BIT(PROXIMITY_IN);
  BIT(PROXIMITY_OUT);
  BIT(SUBSTRUCTURE);
  BIT(SCROLL);
#undef BIT

  return bfr;
}

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 *--------------------------------------------------------------
 * gdk_pointer_grab
 *
 *   Grabs the pointer to a specific window
 *
 * Arguments:
 *   "window" is the window which will receive the grab
 *   "owner_events" specifies whether events will be reported as is,
 *     or relative to "window"
 *   "event_mask" masks only interesting events
 *   "confine_to" limits the cursor movement to the specified window
 *   "cursor" changes the cursor for the duration of the grab
 *   "time" specifies the time
 *
 * Results:
 *
 * Side effects:
 *   requires a corresponding call to gdk_pointer_ungrab
 *
 *--------------------------------------------------------------
 */

477
GdkGrabStatus
478
479
480
481
482
483
gdk_pointer_grab (GdkWindow    *window,
		  gboolean	owner_events,
		  GdkEventMask	event_mask,
		  GdkWindow    *confine_to,
		  GdkCursor    *cursor,
		  guint32	time)
484
{
485
486
  HWND hwnd_confined_to;
  HCURSOR hcursor;
487
488
489
490
491
492
493
494
495
  GdkCursorPrivate *cursor_private;
  gint return_val;

  g_return_val_if_fail (window != NULL, 0);
  g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
  g_return_val_if_fail (confine_to == NULL || GDK_IS_WINDOW (confine_to), 0);
  
  cursor_private = (GdkCursorPrivate*) cursor;
  
Tor Lillqvist's avatar
Tor Lillqvist committed
496
  if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
497
    hwnd_confined_to = NULL;
498
  else
499
    hwnd_confined_to = GDK_WINDOW_HWND (confine_to);
500
501
  
  if (!cursor)
502
    hcursor = NULL;
503
  else
504
    hcursor = cursor_private->hcursor;
505
  
Tor Lillqvist's avatar
Tor Lillqvist committed
506
507
508
509
510
511
  return_val = _gdk_input_grab_pointer (window,
					owner_events,
					event_mask,
					confine_to,
					time);

512
  if (return_val == GDK_GRAB_SUCCESS)
513
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
514
515
      if (!GDK_WINDOW_DESTROYED (window))
	{
516
517
	  GDK_NOTE (EVENTS, g_print ("gdk_pointer_grab: %#lx %s %#lx %s\n",
				     (gulong) GDK_WINDOW_HWND (window),
Tor Lillqvist's avatar
Tor Lillqvist committed
518
				     (owner_events ? "TRUE" : "FALSE"),
519
				     (gulong) hcursor,
Tor Lillqvist's avatar
Tor Lillqvist committed
520
521
522
523
524
				     event_mask_string (event_mask)));
	  p_grab_mask = event_mask;
	  p_grab_owner_events = (owner_events != 0);
	  p_grab_automatic = FALSE;
	  
525
#if 1 /* Menus don't work if we use mouse capture. Pity, because many other
526
527
       * things work better with mouse capture.
       */
Tor Lillqvist's avatar
Tor Lillqvist committed
528
	  SetCapture (GDK_WINDOW_HWND (window));
529
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
530
531
	  return_val = GDK_GRAB_SUCCESS;
	}
532
      else
533
	return_val = GDK_GRAB_ALREADY_GRABBED;
534
535
    }
  
536
  if (return_val == GDK_GRAB_SUCCESS)
537
538
    {
      p_grab_window = window;
539
      p_grab_cursor = hcursor;
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    }
  
  return return_val;
}

/*
 *--------------------------------------------------------------
 * gdk_pointer_ungrab
 *
 *   Releases any pointer grab
 *
 * Arguments:
 *
 * Results:
 *
 * Side effects:
 *
 *--------------------------------------------------------------
 */

void
gdk_pointer_ungrab (guint32 time)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
563
564
565
566
  GDK_NOTE (EVENTS, g_print ("gdk_pointer_ungrab\n"));

  _gdk_input_ungrab_pointer (time);
  
567
#if 1
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
  if (GetCapture () != NULL)
    ReleaseCapture ();
#endif

  p_grab_window = NULL;
}

/*
 *--------------------------------------------------------------
 * gdk_pointer_is_grabbed
 *
 *   Tell wether there is an active x pointer grab in effect
 *
 * Arguments:
 *
 * Results:
 *
 * Side effects:
 *
 *--------------------------------------------------------------
 */

590
gboolean
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
gdk_pointer_is_grabbed (void)
{
  return p_grab_window != NULL;
}

/*
 *--------------------------------------------------------------
 * gdk_keyboard_grab
 *
 *   Grabs the keyboard to a specific window
 *
 * Arguments:
 *   "window" is the window which will receive the grab
 *   "owner_events" specifies whether events will be reported as is,
 *     or relative to "window"
 *   "time" specifies the time
 *
 * Results:
 *
 * Side effects:
 *   requires a corresponding call to gdk_keyboard_ungrab
 *
 *--------------------------------------------------------------
 */

616
GdkGrabStatus
617
618
619
gdk_keyboard_grab (GdkWindow *window,
		   gboolean   owner_events,
		   guint32    time)
620
621
622
623
624
625
{
  gint return_val;
  
  g_return_val_if_fail (window != NULL, 0);
  g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
  
626
627
  GDK_NOTE (EVENTS, g_print ("gdk_keyboard_grab %#lx\n",
			     (gulong) GDK_WINDOW_HWND (window)));
628

Tor Lillqvist's avatar
Tor Lillqvist committed
629
  if (!GDK_WINDOW_DESTROYED (window))
630
631
    {
      k_grab_owner_events = owner_events != 0;
632
      return_val = GDK_GRAB_SUCCESS;
633
634
    }
  else
635
    return_val = GDK_GRAB_ALREADY_GRABBED;
636

637
  if (return_val == GDK_GRAB_SUCCESS)
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
    k_grab_window = window;
  
  return return_val;
}

/*
 *--------------------------------------------------------------
 * gdk_keyboard_ungrab
 *
 *   Releases any keyboard grab
 *
 * Arguments:
 *
 * Results:
 *
 * Side effects:
 *
 *--------------------------------------------------------------
 */

void
gdk_keyboard_ungrab (guint32 time)
{
  GDK_NOTE (EVENTS, g_print ("gdk_keyboard_ungrab\n"));

  k_grab_window = NULL;
}

static GdkFilterReturn
667
gdk_event_apply_filters (MSG      *msg,
668
669
670
671
672
673
674
675
676
677
678
679
680
			 GdkEvent *event,
			 GList    *filters)
{
  GdkEventFilter *filter;
  GList *tmp_list;
  GdkFilterReturn result;
  
  tmp_list = filters;
  
  while (tmp_list)
    {
      filter = (GdkEventFilter *) tmp_list->data;
      
681
      result = (*filter->function) (msg, event, filter->data);
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
      if (result !=  GDK_FILTER_CONTINUE)
	return result;
      
      tmp_list = tmp_list->next;
    }
  
  return GDK_FILTER_CONTINUE;
}

void 
gdk_add_client_message_filter (GdkAtom       message_type,
			       GdkFilterFunc func,
			       gpointer      data)
{
  GdkClientFilter *filter = g_new (GdkClientFilter, 1);

  filter->type = message_type;
  filter->function = func;
  filter->data = data;
  
  client_filters = g_list_prepend (client_filters, filter);
}

/* Thanks to Markus G. Kuhn <mkuhn@acm.org> for the ksysym<->Unicode
 * mapping functions, from the xterm sources.
 */

static void
build_key_event_state (GdkEvent *event)
{
  if (GetKeyState (VK_SHIFT) < 0)
    event->key.state |= GDK_SHIFT_MASK;
  if (GetKeyState (VK_CAPITAL) & 0x1)
    event->key.state |= GDK_LOCK_MASK;
716
  if (!is_altgr_key)
717
718
719
720
    {
      if (GetKeyState (VK_CONTROL) < 0)
	{
	  event->key.state |= GDK_CONTROL_MASK;
721
#if 0
722
723
	  if (event->key.keyval < ' ')
	    event->key.keyval += '@';
724
#endif
725
	}
726
#if 0
727
728
729
730
731
      else if (event->key.keyval < ' ')
	{
	  event->key.state |= GDK_CONTROL_MASK;
	  event->key.keyval += '@';
	}
732
#endif
733
734
735
736
737
738
      if (GetKeyState (VK_MENU) < 0)
	event->key.state |= GDK_MOD1_MASK;
    }
}

static gint
739
build_pointer_event_state (MSG *msg)
740
741
742
743
{
  gint state;
  
  state = 0;
744
  if (msg->wParam & MK_CONTROL)
745
    state |= GDK_CONTROL_MASK;
746
  if (msg->wParam & MK_LBUTTON)
747
    state |= GDK_BUTTON1_MASK;
748
  if (msg->wParam & MK_MBUTTON)
749
    state |= GDK_BUTTON2_MASK;
750
  if (msg->wParam & MK_RBUTTON)
751
    state |= GDK_BUTTON3_MASK;
752
  if (msg->wParam & MK_SHIFT)
753
754
755
756
757
758
759
760
761
762
    state |= GDK_SHIFT_MASK;
  if (GetKeyState (VK_MENU) < 0)
    state |= GDK_MOD1_MASK;
  if (GetKeyState (VK_CAPITAL) & 0x1)
    state |= GDK_LOCK_MASK;

  return state;
}

static void
763
build_keypress_event (GdkWindowImplWin32 *impl,
764
		      GdkEvent           *event,
765
		      MSG                *msg)
766
{
767
  HIMC himc;
768
  gint i, bytecount, ucount, ucleft, len;
769
770
771
772
  guchar buf[100], *bp;
  wchar_t wbuf[100], *wcp;

  event->key.type = GDK_KEY_PRESS;
773
  event->key.time = msg->time;
774
  event->key.state = 0;
775
  
776
  if (msg->message == WM_IME_COMPOSITION)
777
    {
778
      himc = ImmGetContext (msg->hwnd);
779

780
      bytecount = ImmGetCompositionStringW (himc, GCS_RESULTSTR,
781
782
783
784
785
					    wbuf, sizeof (wbuf));
      ucount = bytecount / 2;
    }
  else
    {
786
      if (msg->message == WM_CHAR || msg->message == WM_SYSCHAR)
787
	{
788
	  bytecount = MIN ((msg->lParam & 0xFFFF), sizeof (buf));
789
	  for (i = 0; i < bytecount; i++)
790
	    buf[i] = msg->wParam;
791
792
793
794
	}
      else /* WM_IME_CHAR */
	{
	  event->key.keyval = GDK_VoidSymbol;
795
	  if (msg->wParam & 0xFF00)
796
797
798
799
	    {
	      /* Contrary to some versions of the documentation,
	       * the lead byte is the most significant byte.
	       */
800
801
	      buf[0] = ((msg->wParam >> 8) & 0xFF);
	      buf[1] = (msg->wParam & 0xFF);
802
803
804
805
	      bytecount = 2;
	    }
	  else
	    {
806
	      buf[0] = (msg->wParam & 0xFF);
807
808
809
810
811
812
813
814
	      bytecount = 1;
	    }
	}

      /* Convert from the window's current code page
       * to Unicode. Then convert to UTF-8.
       * We don't handle the surrogate stuff. Should we?
       */
815
      ucount = MultiByteToWideChar (impl->charset_info.ciACP,
816
817
818
819
820
821
				    0, buf, bytecount,
				    wbuf, sizeof (wbuf) / sizeof (wbuf[0]));
      
    }
  if (ucount == 0)
    event->key.keyval = GDK_VoidSymbol;
822
  else if (msg->message == WM_CHAR || msg->message == WM_SYSCHAR)
823
824
825
826
827
828
829
830
831
832
833
834
    {
      if (msg->wParam < ' ')
	{
	  event->key.keyval = msg->wParam + '@';
	  /* This is needed in case of Alt+nnn or Alt+0nnn (on the numpad)
	   * where nnn<32
	   */
	  event->key.state |= GDK_CONTROL_MASK;
	}
      else
	event->key.keyval = gdk_unicode_to_keyval (wbuf[0]);
    }
835
836
837

  build_key_event_state (event);

838
  /* Build UTF-8 string */
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
  ucleft = ucount;
  len = 0;
  wcp = wbuf;
  while (ucleft-- > 0)
    {
      wchar_t c = *wcp++;

      if (c < 0x80)
	len += 1;
      else if (c < 0x800)
	len += 2;
      else
	len += 3;
    }

  event->key.string = g_malloc (len + 1);
  event->key.length = len;
  
  ucleft = ucount;
  wcp = wbuf;
  bp = event->key.string;
  while (ucleft-- > 0)
    {
      int first;
      wchar_t c = *wcp++;

      if (c < 0x80)
	{
	  first = 0;
	  len = 1;
	}
      else if (c < 0x800)
	{
	  first = 0xc0;
	  len = 2;
	}
      else
	{
	  first = 0xe0;
	  len = 3;
	}

#if 1      
      /* Woo-hoo! */
      switch (len)
	{
	case 3: bp[2] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
	case 2: bp[1] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
	case 1: bp[0] = c | first;
	}
#else
      for (i = len - 1; i > 0; --i)
	{
	  bp[i] = (c & 0x3f) | 0x80;
	  c >>= 6;
	}
      bp[0] = c | first;
#endif

      bp += len;
    }
  *bp = 0;
}

static void
904
build_keyrelease_event (GdkWindowImplWin32 *impl,
905
			GdkEvent           *event,
906
			MSG                *msg)
907
908
909
910
911
{
  guchar buf;
  wchar_t wbuf;

  event->key.type = GDK_KEY_RELEASE;
912
  event->key.time = msg->time;
913
  event->key.state = 0;
914

915
916
917
  if (msg->message == WM_CHAR || msg->message == WM_SYSCHAR)
    if (msg->wParam < ' ')
      event->key.keyval = msg->wParam + '@';
918
919
    else
      {
920
921
	buf = msg->wParam;
	MultiByteToWideChar (impl->charset_info.ciACP,
922
923
			     0, &buf, 1, &wbuf, 1);

924
	event->key.keyval = gdk_unicode_to_keyval (wbuf);
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
      }
  else
    event->key.keyval = GDK_VoidSymbol;
  build_key_event_state (event);
  event->key.string = NULL;
  event->key.length = 0;
}

static void
print_event_state (gint state)
{
  if (state & GDK_SHIFT_MASK)
    g_print ("SHIFT ");
  if (state & GDK_LOCK_MASK)
    g_print ("LOCK ");
  if (state & GDK_CONTROL_MASK)
    g_print ("CONTROL ");
  if (state & GDK_MOD1_MASK)
    g_print ("MOD1 ");
  if (state & GDK_BUTTON1_MASK)
    g_print ("BUTTON1 ");
  if (state & GDK_BUTTON2_MASK)
    g_print ("BUTTON2 ");
  if (state & GDK_BUTTON3_MASK)
    g_print ("BUTTON3 ");
}

static void
print_event (GdkEvent *event)
{
  gchar *escaped, *kvname;

  switch (event->any.type)
    {
    case GDK_NOTHING: g_print ("GDK_NOTHING "); break;
    case GDK_DELETE: g_print ("GDK_DELETE "); break;
    case GDK_DESTROY: g_print ("GDK_DESTROY "); break;
    case GDK_EXPOSE: g_print ("GDK_EXPOSE "); break;
    case GDK_MOTION_NOTIFY: g_print ("GDK_MOTION_NOTIFY "); break;
    case GDK_BUTTON_PRESS: g_print ("GDK_BUTTON_PRESS "); break;
    case GDK_2BUTTON_PRESS: g_print ("GDK_2BUTTON_PRESS "); break;
    case GDK_3BUTTON_PRESS: g_print ("GDK_3BUTTON_PRESS "); break;
    case GDK_BUTTON_RELEASE: g_print ("GDK_BUTTON_RELEASE "); break;
    case GDK_KEY_PRESS: g_print ("GDK_KEY_PRESS "); break;
    case GDK_KEY_RELEASE: g_print ("GDK_KEY_RELEASE "); break;
    case GDK_ENTER_NOTIFY: g_print ("GDK_ENTER_NOTIFY "); break;
    case GDK_LEAVE_NOTIFY: g_print ("GDK_LEAVE_NOTIFY "); break;
    case GDK_FOCUS_CHANGE: g_print ("GDK_FOCUS_CHANGE "); break;
    case GDK_CONFIGURE: g_print ("GDK_CONFIGURE "); break;
    case GDK_MAP: g_print ("GDK_MAP "); break;
    case GDK_UNMAP: g_print ("GDK_UNMAP "); break;
    case GDK_PROPERTY_NOTIFY: g_print ("GDK_PROPERTY_NOTIFY "); break;
    case GDK_SELECTION_CLEAR: g_print ("GDK_SELECTION_CLEAR "); break;
    case GDK_SELECTION_REQUEST: g_print ("GDK_SELECTION_REQUEST "); break;
    case GDK_SELECTION_NOTIFY: g_print ("GDK_SELECTION_NOTIFY "); break;
    case GDK_PROXIMITY_IN: g_print ("GDK_PROXIMITY_IN "); break;
    case GDK_PROXIMITY_OUT: g_print ("GDK_PROXIMITY_OUT "); break;
    case GDK_DRAG_ENTER: g_print ("GDK_DRAG_ENTER "); break;
    case GDK_DRAG_LEAVE: g_print ("GDK_DRAG_LEAVE "); break;
    case GDK_DRAG_MOTION: g_print ("GDK_DRAG_MOTION "); break;
    case GDK_DRAG_STATUS: g_print ("GDK_DRAG_STATUS "); break;
    case GDK_DROP_START: g_print ("GDK_DROP_START "); break;
    case GDK_DROP_FINISHED: g_print ("GDK_DROP_FINISHED "); break;
    case GDK_CLIENT_EVENT: g_print ("GDK_CLIENT_EVENT "); break;
    case GDK_VISIBILITY_NOTIFY: g_print ("GDK_VISIBILITY_NOTIFY "); break;
    case GDK_NO_EXPOSE: g_print ("GDK_NO_EXPOSE "); break;
991
    case GDK_SCROLL: g_print ("GDK_SCROLL "); break;
992
    }
993
  g_print ("%#lx ", (gulong) GDK_WINDOW_HWND (event->any.window));
994
995
996
997
998
999
1000

  switch (event->any.type)
    {
    case GDK_EXPOSE:
      g_print ("%dx%d@+%d+%d %d",
	       event->expose.area.width,
	       event->expose.area.height,