summaryrefslogtreecommitdiff
path: root/gtk/gtksocket.c
blob: a936605d9fdd5a08589200c961d2d992152ec080 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
373
374
375
376
377
378
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
584
585
586
587
588
589
590
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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
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
835
836
837
838
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
/* GTK - The GIMP Toolkit
 * 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 Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* By Owen Taylor <otaylor@gtk.org>              98/4/4 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * 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 "gdk/gdkkeysyms.h"
#include "gtkmain.h"
#include "gtkwindow.h"
#include "gtksignal.h"
#include "gtksocket.h"
#include "gtkdnd.h"

#include "x11/gdkx.h"
#include "x11/gdkscreen-x11.h"

#include "xembed.h"

/* Forward declararations */

static void            gtk_socket_class_init           (GtkSocketClass   *klass);
static void            gtk_socket_init                 (GtkSocket        *socket);
static void            gtk_socket_realize              (GtkWidget        *widget);
static void            gtk_socket_unrealize            (GtkWidget        *widget);
static void            gtk_socket_size_request         (GtkWidget        *widget,
							GtkRequisition   *requisition);
static void            gtk_socket_size_allocate        (GtkWidget        *widget,
							GtkAllocation    *allocation);
static void            gtk_socket_hierarchy_changed    (GtkWidget        *widget);
static void            gtk_socket_grab_notify          (GtkWidget        *widget,
							gboolean          was_grabbed);
static gboolean        gtk_socket_key_press_event      (GtkWidget        *widget,
							GdkEventKey      *event);
static gboolean        gtk_socket_focus_in_event       (GtkWidget        *widget,
							GdkEventFocus    *event);
static void            gtk_socket_claim_focus          (GtkSocket        *socket);
static gboolean        gtk_socket_focus_out_event      (GtkWidget        *widget,
							GdkEventFocus    *event);
static void            gtk_socket_send_configure_event (GtkSocket        *socket);
static gboolean        gtk_socket_focus                (GtkContainer     *container,
							GtkDirectionType  direction);
static GdkFilterReturn gtk_socket_filter_func          (GdkXEvent        *gdk_xevent,
							GdkEvent         *event,
							gpointer          data);

static void send_xembed_message (GtkSocket *socket,
				 glong      message,
				 glong      detail,
				 glong      data1,
				 glong      data2,
				 guint32    time);

/* From Tk */
#define EMBEDDED_APP_WANTS_FOCUS NotifyNormal+20

/* Local data */

static GtkWidgetClass *parent_class = NULL;

GtkType
gtk_socket_get_type (void)
{
  static GtkType socket_type = 0;

  if (!socket_type)
    {
      static const GTypeInfo socket_info =
      {
	sizeof (GtkSocketClass),
	NULL,           /* base_init */
	NULL,           /* base_finalize */
	(GClassInitFunc) gtk_socket_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (GtkSocket),
	16,             /* n_preallocs */
	(GInstanceInitFunc) gtk_socket_init,
      };

      socket_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkSocket", &socket_info, 0);
    }

  return socket_type;
}

static void
gtk_socket_class_init (GtkSocketClass *class)
{
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;

  parent_class = gtk_type_class (GTK_TYPE_CONTAINER);

  widget_class->realize = gtk_socket_realize;
  widget_class->unrealize = gtk_socket_unrealize;
  widget_class->size_request = gtk_socket_size_request;
  widget_class->size_allocate = gtk_socket_size_allocate;
  widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
#if 0  
  widget_class->grab_notify = gtk_socket_grab_notify;
#endif  
  widget_class->key_press_event = gtk_socket_key_press_event;
  widget_class->focus_in_event = gtk_socket_focus_in_event;
  widget_class->focus_out_event = gtk_socket_focus_out_event;

  container_class->focus = gtk_socket_focus;
}

static void
gtk_socket_init (GtkSocket *socket)
{
  socket->request_width = 0;
  socket->request_height = 0;
  socket->current_width = 0;
  socket->current_height = 0;
  
  socket->plug_window = NULL;
  socket->same_app = FALSE;
  socket->focus_in = FALSE;
  socket->have_size = FALSE;
  socket->need_map = FALSE;
}

GtkWidget*
gtk_socket_new (void)
{
  GtkSocket *socket;

  socket = g_object_new (GTK_TYPE_SOCKET, NULL);

  return GTK_WIDGET (socket);
}

void           
gtk_socket_steal (GtkSocket *socket, GdkNativeWindow id)
{
  GtkWidget *widget;
  gpointer user_data = NULL;
  
  widget = GTK_WIDGET (socket);
  
  socket->plug_window = gdk_window_lookup (id);

  gdk_error_trap_push ();

  if (socket->plug_window)
    gdk_window_get_user_data (socket->plug_window,
                              &user_data);
  
  if (user_data)      
    {
      /*
	GtkWidget *child_widget;

	child_widget = GTK_WIDGET (socket->plug_window->user_data);
      */

      g_warning("Stealing from same app not yet implemented");
      
      socket->same_app = TRUE;
    }
  else
    {
      socket->plug_window = gdk_window_foreign_new_for_display (GTK_WIDGET_GET_DISPLAY(widget),
       								id);
      if (!socket->plug_window) /* was deleted before we could get it */
	{
	  gdk_error_trap_pop ();
	  return;
	}
	
      socket->same_app = FALSE;
      socket->have_size = FALSE;

      XSelectInput (GDK_WINDOW_XDISPLAY (socket->plug_window),
		    GDK_WINDOW_XROOTWIN(socket->plug_window),
		    StructureNotifyMask | PropertyChangeMask);

      gtk_widget_queue_resize (widget);
    }

  gdk_window_hide (socket->plug_window);
  gdk_window_reparent (socket->plug_window, widget->window, 0, 0);

  gdk_flush ();
  gdk_error_trap_pop ();
  
  socket->need_map = TRUE;
}

static void
gtk_socket_realize (GtkWidget *widget)
{
  GtkSocket *socket;
  GdkWindowAttr attributes;
  gint attributes_mask;
  XWindowAttributes xattrs;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SOCKET (widget));

  socket = GTK_SOCKET (widget);
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = GDK_FOCUS_CHANGE_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, socket);

  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);

  XGetWindowAttributes (GDK_WINDOW_XDISPLAY (widget->window),
			GDK_WINDOW_XWINDOW (widget->window),
			&xattrs);

  XSelectInput (GDK_WINDOW_XDISPLAY (widget->window),
		GDK_WINDOW_XWINDOW(widget->window), 
		xattrs.your_event_mask | 
		SubstructureNotifyMask | SubstructureRedirectMask);

  gdk_window_add_filter (widget->window, gtk_socket_filter_func, widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  /* We sync here so that we make sure that if the XID for
   * our window is passed to another application, SubstructureRedirectMask
   * will be set by the time the other app creates its window.
   */
  gdk_flush();
}

static void
gtk_socket_unrealize (GtkWidget *widget)
{
  GtkSocket *socket;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SOCKET (widget));

  socket = GTK_SOCKET (widget);

  if (socket->plug_window)
    {
      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
      if (toplevel && GTK_IS_WINDOW (toplevel))
	gtk_window_remove_embedded_xid (GTK_WINDOW (toplevel), 
					GDK_WINDOW_XWINDOW (socket->plug_window));

      socket->plug_window = NULL;
    }

#if 0  
  if (socket->grabbed_keys)
    {
      g_hash_table_foreach (socket->grabbed_keys, (GHFunc)g_free, NULL);
      g_hash_table_destroy (socket->grabbed_keys);
      socket->grabbed_keys = NULL;
    }
#endif
  
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}
  
static void 
gtk_socket_size_request (GtkWidget      *widget,
			 GtkRequisition *requisition)
{
  GtkSocket *socket;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SOCKET (widget));
  g_return_if_fail (requisition != NULL);
  
  socket = GTK_SOCKET (widget);

  if (!socket->have_size && socket->plug_window)
    {
      XSizeHints hints;
      long supplied;

      gdk_error_trap_push ();
      
      if (XGetWMNormalHints (GDK_WINDOW_XDISPLAY (socket->plug_window),
			     GDK_WINDOW_XWINDOW (socket->plug_window),
			     &hints, &supplied))
	{
	  /* This is obsolete, according the X docs, but many programs
	   * still use it */
	  if (hints.flags & (PSize | USSize))
	    {
	      socket->request_width = hints.width;
	      socket->request_height = hints.height;
	    }
	  else if (hints.flags & PMinSize)
	    {
	      socket->request_width = hints.min_width;
	      socket->request_height = hints.min_height;
	    }
	  else if (hints.flags & PBaseSize)
	    {
	      socket->request_width = hints.base_width;
	      socket->request_height = hints.base_height;
	    }
	}
      socket->have_size = TRUE;	/* don't check again? */

      gdk_error_trap_pop ();
    }

  requisition->width = MAX (socket->request_width, 1);
  requisition->height = MAX (socket->request_height, 1);
}

static void
gtk_socket_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
  GtkSocket *socket;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_SOCKET (widget));
  g_return_if_fail (allocation != NULL);

  socket = GTK_SOCKET (widget);

  widget->allocation = *allocation;
  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);

      if (socket->plug_window)
	{
	  gdk_error_trap_push ();
	  
	  if (!socket->need_map &&
	      (allocation->width == socket->current_width) &&
	      (allocation->height == socket->current_height))
	    {
	      gtk_socket_send_configure_event (socket);
	      GTK_NOTE(PLUGSOCKET, 
		       g_message ("GtkSocket - allocated no change: %d %d",
				  allocation->width, allocation->height));
	    }
	  else
	    {
	      gdk_window_move_resize (socket->plug_window,
				      0, 0,
				      allocation->width, allocation->height);
	      GTK_NOTE(PLUGSOCKET,
		       g_message ("GtkSocket - allocated: %d %d",
				  allocation->width, allocation->height));
	      socket->current_width = allocation->width;
	      socket->current_height = allocation->height;
	    }

	  if (socket->need_map)
	    {
	      gdk_window_show (socket->plug_window);
	      socket->need_map = FALSE;
	    }

	  gdk_flush ();
	  gdk_error_trap_pop ();
	}
    }
}

#if 0

typedef struct
{
  guint			 accelerator_key;
  GdkModifierType	 accelerator_mods;
} GrabbedKey;

static guint
grabbed_key_hash (gconstpointer a)
{
  const GrabbedKey *key = a;
  guint h;
  
  h = key->accelerator_key << 16;
  h ^= key->accelerator_key >> 16;
  h ^= key->accelerator_mods;

  return h;
}

static gboolean
grabbed_key_equal (gconstpointer a, gconstpointer b)
{
  const GrabbedKey *keya = a;
  const GrabbedKey *keyb = b;

  return (keya->accelerator_key == keyb->accelerator_key &&
	  keya->accelerator_mods == keyb->accelerator_mods);
}

static void
add_grabbed_key (GtkSocket      *socket,
		 guint           hardware_keycode,
		 GdkModifierType mods)
{
  GrabbedKey key;
  GrabbedKey *new_key;
  GrabbedKey *found_key;

  if (socket->grabbed_keys)
    {
      key.accelerator_key = hardware_keycode;
      key.accelerator_mods = mods;

      found_key = g_hash_table_lookup (socket->grabbed_keys, &key);

      if (found_key)
	{
	  g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
		     hardware_keycode, mods);
	  return;
	}
    }
  
  if (!socket->grabbed_keys)
    socket->grabbed_keys = g_hash_table_new (grabbed_key_hash, grabbed_key_equal);

  new_key = g_new (GrabbedKey, 1);
  
  new_key->accelerator_key = hardware_keycode;
  new_key->accelerator_mods = mods;

  g_hash_table_insert (socket->grabbed_keys, new_key, new_key);
}

static void
remove_grabbed_key (GtkSocket      *socket,
		    guint           hardware_keycode,
		    GdkModifierType mods)
{
  GrabbedKey key;
  GrabbedKey *found_key = NULL;

  if (socket->grabbed_keys)
    {
      key.accelerator_key = hardware_keycode;
      key.accelerator_mods = mods;

      found_key = g_hash_table_lookup (socket->grabbed_keys, &key);
    }

  if (found_key)
    {
      g_hash_table_remove (socket->grabbed_keys, &key);
      g_free (found_key);
    }
  else
    g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
	       hardware_keycode, mods);
}

static gboolean
toplevel_key_press_handler (GtkWidget   *toplevel,
			    GdkEventKey *event,
			    GtkSocket   *socket)
{
  GrabbedKey search_key;

  search_key.accelerator_key = event->hardware_keycode;
  search_key.accelerator_mods = event->state;

  if (socket->grabbed_keys &&
      g_hash_table_lookup (socket->grabbed_keys, &search_key))
    {
      gtk_socket_key_press_event (GTK_WIDGET (socket), event);
      gtk_signal_emit_stop_by_name (GTK_OBJECT (toplevel), "key_press_event");

      return TRUE;
    }
  else
    return FALSE;
}

#endif

static void
toplevel_focus_in_handler (GtkWidget     *toplevel,
			   GdkEventFocus *event,
			   GtkSocket     *socket)
{
  /* It appears spurious focus in events can occur when
   *  the window is hidden. So we'll just check to see if
   *  the window is visible before actually handling the
   *  event. (Comment from gtkwindow.c)
   */
  if (GTK_WIDGET_VISIBLE (toplevel))
    send_xembed_message (socket, XEMBED_WINDOW_ACTIVATE, 0, 0, 0,
			 gtk_get_current_event_time ()); /* Will be GDK_CURRENT_TIME */
}

static void
toplevel_focus_out_handler (GtkWidget     *toplevel,
			    GdkEventFocus *event,
			    GtkSocket     *socket)
{
  send_xembed_message (socket, XEMBED_WINDOW_DEACTIVATE, 0, 0, 0,
		       gtk_get_current_event_time ()); /* Will be GDK_CURRENT_TIME */
}

static void
gtk_socket_hierarchy_changed (GtkWidget *widget)
{
  GtkSocket *socket = GTK_SOCKET (widget);
  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);

  if (toplevel && !GTK_IS_WINDOW (toplevel))
    toplevel = NULL;

  if (toplevel != socket->toplevel)
    {
      if (socket->toplevel)
	{
#if 0
	  gtk_signal_disconnect_by_func (GTK_OBJECT (socket->toplevel), GTK_SIGNAL_FUNC (toplevel_key_press_handler), socket);
#endif	  
	  gtk_signal_disconnect_by_func (GTK_OBJECT (socket->toplevel), GTK_SIGNAL_FUNC (toplevel_focus_in_handler), socket);
	  gtk_signal_disconnect_by_func (GTK_OBJECT (socket->toplevel), GTK_SIGNAL_FUNC (toplevel_focus_out_handler), socket);
	}

      socket->toplevel = toplevel;

      if (toplevel)
	{
#if 0
	  gtk_signal_connect (GTK_OBJECT (socket->toplevel), "key_press_event",
			      GTK_SIGNAL_FUNC (toplevel_key_press_handler), socket);
#endif
	  gtk_signal_connect (GTK_OBJECT (socket->toplevel), "focus_in_event",
			      GTK_SIGNAL_FUNC (toplevel_focus_in_handler), socket);
	  gtk_signal_connect (GTK_OBJECT (socket->toplevel), "focus_out_event",
			      GTK_SIGNAL_FUNC (toplevel_focus_out_handler), socket);
	}
    }
}

static void
gtk_socket_grab_notify (GtkWidget *widget,
			gboolean   was_grabbed)
{
  send_xembed_message (GTK_SOCKET (widget),
		       was_grabbed ? XEMBED_MODALITY_OFF : XEMBED_MODALITY_ON,
		       0, 0, 0, gtk_get_current_event_time ());
}

static gboolean
gtk_socket_key_press_event (GtkWidget   *widget,
			    GdkEventKey *event)
{
  GtkSocket *socket = GTK_SOCKET (widget);
  
  if (socket->plug_window)
    {
      XEvent xevent;
      
      xevent.xkey.type = KeyPress;
      xevent.xkey.display = GDK_WINDOW_XDISPLAY (event->window);
      xevent.xkey.window = GDK_WINDOW_XWINDOW (socket->plug_window);
      xevent.xkey.root = GDK_WINDOW_XROOTWIN (event->window);
      xevent.xkey.time = event->time;
      /* FIXME, the following might cause problems for non-GTK apps */
      xevent.xkey.x = 0;
      xevent.xkey.y = 0;
      xevent.xkey.x_root = 0;
      xevent.xkey.y_root = 0;
      xevent.xkey.state = event->state;
      xevent.xkey.keycode = event->hardware_keycode;
      xevent.xkey.same_screen = TRUE; /* FIXME ? */
      
      gdk_error_trap_push ();
      XSendEvent (GDK_WINDOW_XDISPLAY (socket->plug_window),
		  GDK_WINDOW_XWINDOW (socket->plug_window),
		  False, NoEventMask, &xevent);
      gdk_flush ();
      gdk_error_trap_pop ();
      
      return TRUE;
    }
  else
    return FALSE;
}

static gboolean
gtk_socket_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
{
  GtkSocket *socket = GTK_SOCKET (widget);

  if (!GTK_WIDGET_HAS_FOCUS (widget))
    {
      GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
  
      if (socket->plug_window)
	{
          send_xembed_message (socket, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0,
			       gtk_get_current_event_time ());
	}
    }
  
  return TRUE;
}

static gboolean
gtk_socket_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
{
  GtkSocket *socket = GTK_SOCKET (widget);

  GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);

#if 0
  GtkWidget *toplevel;
  toplevel = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
  
  if (toplevel)
    {
      XSetInputFocus (GDK_WINDOW_XDISPLAY (toplevel->window),
		      GDK_WINDOW_XWINDOW (toplevel->window),
		      RevertToParent, CurrentTime); /* FIXME? */
    }

#endif      

  if (socket->plug_window)
    {
      send_xembed_message (socket, XEMBED_FOCUS_OUT, 0, 0, 0,
			   gtk_get_current_event_time ());
    }

  socket->focus_in = FALSE;
  
  return TRUE;
}

static void
gtk_socket_claim_focus (GtkSocket *socket)
{
      
  socket->focus_in = TRUE;
  
  /* Oh, the trickery... */
  
  GTK_WIDGET_SET_FLAGS (socket, GTK_CAN_FOCUS);
  gtk_widget_grab_focus (GTK_WIDGET (socket));
  GTK_WIDGET_UNSET_FLAGS (socket, GTK_CAN_FOCUS);
  
  /* FIXME: we might grab the focus even if we don't have
   * it as an app... (and see _focus_in ()) */
  if (socket->plug_window)
    {
#if 0      
      gdk_error_trap_push ();
      XSetInputFocus (GDK_WINDOW_XDISPLAY (socket->plug_window),
		      GDK_WINDOW_XWINDOW (socket->plug_window),
		      RevertToParent, GDK_CURRENT_TIME);
      gdk_flush ();
      gdk_error_trap_pop ();
#endif
    }
}

static gboolean
gtk_socket_focus (GtkContainer *container, GtkDirectionType direction)
{
  GtkSocket *socket;
  gint detail = -1;

  g_return_val_if_fail (GTK_IS_SOCKET (container), FALSE);
  
  socket = GTK_SOCKET (container);

  if (!GTK_WIDGET_HAS_FOCUS (container))
    {
      switch (direction)
	{
	case GTK_DIR_UP:
	case GTK_DIR_LEFT:
	case GTK_DIR_TAB_BACKWARD:
	  detail = XEMBED_FOCUS_LAST;
	  break;
	case GTK_DIR_DOWN:
	case GTK_DIR_RIGHT:
	case GTK_DIR_TAB_FORWARD:
	  detail = XEMBED_FOCUS_FIRST;
	  break;
	}
      
      send_xembed_message (socket, XEMBED_FOCUS_IN, detail, 0, 0,
			   gtk_get_current_event_time ());

      GTK_WIDGET_SET_FLAGS (container, GTK_HAS_FOCUS);
      gtk_widget_grab_focus (GTK_WIDGET (container));
 
      return TRUE;
    }
  else
    return FALSE;

#if 0
  if (!socket->focus_in && socket->plug_window)
    {
      XEvent xevent;

      gtk_socket_claim_focus (socket);
      
      xevent.xkey.type = KeyPress;
      xevent.xkey.display = GDK_WINDOW_XDISPLAY (socket->plug_window);
      xevent.xkey.window = GDK_WINDOW_XWINDOW (socket->plug_window);
      xevent.xkey.root = GDK_WINDOW_XROOTWIN (socket->plug_window); /* FIXME */
      xevent.xkey.time = GDK_CURRENT_TIME; /* FIXME */
      /* FIXME, the following might cause big problems for
       * non-GTK apps */
      xevent.xkey.x = 0;
      xevent.xkey.y = 0;
      xevent.xkey.x_root = 0;
      xevent.xkey.y_root = 0;
      xevent.xkey.state = 0;
      xevent.xkey.same_screen = TRUE; /* FIXME ? */

      switch (direction)
	{
	case GTK_DIR_UP:
	  xevent.xkey.keycode =  XKeysymToKeycode(xevent.xkey.display,
						  GDK_Up);
	  break;
	case GTK_DIR_DOWN:
	  xevent.xkey.keycode =  XKeysymToKeycode(xevent.xkey.display, GDK_Down);
	  break;
	case GTK_DIR_LEFT:
	  xevent.xkey.keycode =  XKeysymToKeycode(xevent.xkey.display, GDK_Left);
	  break;
	case GTK_DIR_RIGHT:
	  xevent.xkey.keycode =  XKeysymToKeycode(xevent.xkey.display, GDK_Right);
	  break;
	case GTK_DIR_TAB_FORWARD:
	  xevent.xkey.keycode =  XKeysymToKeycode(xevent.xkey.display, GDK_Tab);
	  break;
	case GTK_DIR_TAB_BACKWARD:
	  xevent.xkey.keycode =  XKeysymToKeycode(xevent.xkey.display, GDK_Tab);
	  xevent.xkey.state = ShiftMask;
	  break;
	}


      gdk_error_trap_push ();
      XSendEvent (xevent.xkey.display,
		  GDK_WINDOW_XWINDOW (socket->plug_window),
		  False, NoEventMask, &xevent);
      gdk_flush();
      gdk_error_trap_pop ();
      
      return TRUE;
    }
  else
    {
      return FALSE;
    }
#endif  
}

static void
gtk_socket_send_configure_event (GtkSocket *socket)
{
  XEvent event;

  g_return_if_fail (socket->plug_window != NULL);

  event.xconfigure.type = ConfigureNotify;
  event.xconfigure.display = GDK_WINDOW_XDISPLAY (socket->plug_window);

  event.xconfigure.event = GDK_WINDOW_XWINDOW (socket->plug_window);
  event.xconfigure.window = GDK_WINDOW_XWINDOW (socket->plug_window);

  event.xconfigure.x = 0;
  event.xconfigure.y = 0;
  event.xconfigure.width = GTK_WIDGET(socket)->allocation.width;
  event.xconfigure.height = GTK_WIDGET(socket)->allocation.height;

  event.xconfigure.border_width = 0;
  event.xconfigure.above = None;
  event.xconfigure.override_redirect = False;

  gdk_error_trap_push ();
  XSendEvent (GDK_WINDOW_XDISPLAY (socket->plug_window),
	      GDK_WINDOW_XWINDOW (socket->plug_window),
	      False, NoEventMask, &event);
  gdk_flush ();
  gdk_error_trap_pop ();
}

static void
gtk_socket_add_window (GtkSocket *socket, GdkNativeWindow xid)
{
  socket->plug_window = gdk_window_lookup (xid);
  socket->same_app = TRUE;

  if (!socket->plug_window)
    {
      GtkWidget *toplevel;
      GdkDragProtocol protocol;
      
      socket->plug_window = 
      	gdk_window_foreign_new_for_display (GTK_WIDGET_GET_DISPLAY(socket),
      					    xid);
      if (!socket->plug_window) /* Already gone */
	return;
	
      socket->same_app = FALSE;

      gdk_error_trap_push ();
      XSelectInput (GDK_WINDOW_XDISPLAY (socket->plug_window),
		    GDK_WINDOW_XWINDOW(socket->plug_window),
		    StructureNotifyMask | PropertyChangeMask);
      
      if (gdk_drag_get_protocol (GDK_WINDOW_DISPLAY(socket->plug_window), xid, &protocol))
	gtk_drag_dest_set_proxy (GTK_WIDGET (socket), socket->plug_window, 
				 protocol, TRUE);
      gdk_flush ();
      gdk_error_trap_pop ();

      gdk_window_add_filter (socket->plug_window, 
			     gtk_socket_filter_func, socket);

      /* Add a pointer to the socket on our toplevel window */

      toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
      if (toplevel && GTK_IS_WINDOW (toplevel))
	{
	  gtk_window_add_embedded_xid (GTK_WINDOW (toplevel), xid);
	}

      gtk_widget_queue_resize (GTK_WIDGET (socket));
    }
}


static void
send_xembed_message (GtkSocket *socket,
		     glong      message,
		     glong      detail,
		     glong      data1,
		     glong      data2,
		     guint32    time)
{
  GTK_NOTE(PLUGSOCKET,
	   g_message ("GtkSocket: Sending XEMBED message of type %d", message));
  
  if (socket->plug_window)
    {
      XEvent xevent;

      xevent.xclient.window = GDK_WINDOW_XWINDOW (socket->plug_window);
      xevent.xclient.type = ClientMessage;
      xevent.xclient.message_type = gdk_display_atom (gdk_window_get_display(socket->plug_window), "_XEMBED", FALSE);

      xevent.xclient.format = 32;
      xevent.xclient.data.l[0] = time;
      xevent.xclient.data.l[1] = message;
      xevent.xclient.data.l[2] = detail;
      xevent.xclient.data.l[3] = data1;
      xevent.xclient.data.l[4] = data2;

      gdk_error_trap_push ();
      XSendEvent (GDK_WINDOW_XDISPLAY(socket->plug_window),
		  GDK_WINDOW_XWINDOW (socket->plug_window),
		  False, NoEventMask, &xevent);
      gdk_flush ();
      gdk_error_trap_pop ();
    }
}

static void
handle_xembed_message (GtkSocket *socket,
		       glong      message,
		       glong      detail,
		       glong      data1,
		       glong      data2,
		       guint32    time)
{
  switch (message)
    {
    case XEMBED_EMBEDDED_NOTIFY:
    case XEMBED_WINDOW_ACTIVATE:
    case XEMBED_WINDOW_DEACTIVATE:
    case XEMBED_MODALITY_ON:
    case XEMBED_MODALITY_OFF:
    case XEMBED_FOCUS_IN:
    case XEMBED_FOCUS_OUT:
      g_warning ("GtkSocket: Invalid _XEMBED message of type %ld received", message);
      break;
      
    case XEMBED_REQUEST_FOCUS:
      gtk_socket_claim_focus (socket);
      break;

    case XEMBED_FOCUS_NEXT:
    case XEMBED_FOCUS_PREV:
      {
	GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
	if (toplevel && GTK_IS_CONTAINER (toplevel))
	  {
	    gtk_container_focus (GTK_CONTAINER (toplevel),
				 (message == XEMBED_FOCUS_NEXT ?
				  GTK_DIR_TAB_FORWARD : GTK_DIR_TAB_BACKWARD));
	  }
	break;
      }
      
    case XEMBED_GRAB_KEY:
#if 0
      add_grabbed_key (socket, data1, data2);
#endif
      break; 
    case XEMBED_UNGRAB_KEY:
#if 0      
      remove_grabbed_key (socket, data1, data2);
#endif
      break;
      
    default:
      GTK_NOTE(PLUGSOCKET,
	       g_message ("GtkSocket: Ignoring unknown _XEMBED message of type %ld", message));
      break;
    }
}

static GdkFilterReturn
gtk_socket_filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
{
  GtkSocket *socket;
  GtkWidget *widget;
  XEvent *xevent;

  GdkFilterReturn return_val;
  
  socket = GTK_SOCKET (data);
  widget = GTK_WIDGET (socket);
  xevent = (XEvent *)gdk_xevent;
  

  return_val = GDK_FILTER_CONTINUE;

  switch (xevent->type)
    {
    case CreateNotify:
      {
	XCreateWindowEvent *xcwe = &xevent->xcreatewindow;

	if (!socket->plug_window)
	  {
	    gtk_socket_add_window (socket, xcwe->window);

	    gdk_error_trap_push ();
	    gdk_window_move_resize(socket->plug_window,
				   0, 0,
				   widget->allocation.width, 
				   widget->allocation.height);
	    gdk_flush ();
	    gdk_error_trap_pop ();
	
	    socket->request_width = xcwe->width;
	    socket->request_height = xcwe->height;
	    socket->have_size = TRUE;

	    GTK_NOTE(PLUGSOCKET,
		     g_message ("GtkSocket - window created with size: %d %d",
				socket->request_width,
				socket->request_height));
	  }
	
	return_val = GDK_FILTER_REMOVE;
	
	break;
      }

    case ConfigureRequest:
      {
	XConfigureRequestEvent *xcre = &xevent->xconfigurerequest;
	
	if (!socket->plug_window)
	  gtk_socket_add_window (socket, xcre->window);
	
	if (xcre->window == GDK_WINDOW_XWINDOW (socket->plug_window))
	  {
	    if (xcre->value_mask & (CWWidth | CWHeight))
	      {
		socket->request_width = xcre->width;
		socket->request_height = xcre->height;
		socket->have_size = TRUE;
		
		GTK_NOTE(PLUGSOCKET,
			 g_message ("GtkSocket - configure request: %d %d",
				    socket->request_width,
				    socket->request_height));
		
		gtk_widget_queue_resize (widget);
	      }
	    else if (xcre->value_mask & (CWX | CWY))
	      {
		gtk_socket_send_configure_event (socket);
	      }
	    /* Ignore stacking requests. */
	    
	    return_val = GDK_FILTER_REMOVE;
	  }
	break;
      }

    case DestroyNotify:
      {
	XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;

	if (socket->plug_window &&
	    (xdwe->window == GDK_WINDOW_XWINDOW (socket->plug_window)))
	  {
	    GtkWidget *toplevel;

	    GTK_NOTE(PLUGSOCKET,
		     g_message ("GtkSocket - destroy notify"));
	    
	    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
	    if (toplevel && GTK_IS_WINDOW (toplevel))
	      gtk_window_remove_embedded_xid (GTK_WINDOW (toplevel), xdwe->window);
	    
	    gdk_window_destroy_notify (socket->plug_window);
	    gtk_widget_destroy (widget);

	    socket->plug_window = NULL;
	    
	    return_val = GDK_FILTER_REMOVE;
	  }
	break;
      }

    case FocusIn:
      if (xevent->xfocus.mode == EMBEDDED_APP_WANTS_FOCUS)
	{
	  gtk_socket_claim_focus (socket);
	}
      else if (xevent->xfocus.detail == NotifyInferior)
	{
#if 0
	  GtkWidget *toplevel;
	  toplevel = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
	  
	  if (toplevel)
	    {
	      XSetInputFocus (GDK_DISPLAY (),
			      GDK_WINDOW_XWINDOW (toplevel->window),
			      RevertToParent, CurrentTime); /* FIXME? */
	    }
#endif
	}
      return_val = GDK_FILTER_REMOVE;
      break;
    case FocusOut:
      return_val = GDK_FILTER_REMOVE;
      break;
    case MapRequest:
      if (!socket->plug_window)
	gtk_socket_add_window (socket, xevent->xmaprequest.window);
	
      if (xevent->xmaprequest.window ==
	  GDK_WINDOW_XWINDOW (socket->plug_window))
	{
	  GTK_NOTE(PLUGSOCKET,
		   g_message ("GtkSocket - Map Request"));
	  
	  gdk_error_trap_push ();
	  gdk_window_show (socket->plug_window);
	  gdk_flush ();
	  gdk_error_trap_pop ();

	  return_val = GDK_FILTER_REMOVE;
	}
      break;
    case PropertyNotify:
      if (xevent->xproperty.window ==
	  GDK_WINDOW_XWINDOW (socket->plug_window))
	{
	  GdkDragProtocol protocol;
	  GdkDisplay *display = gdk_window_get_display(socket->plug_window);

	  if ((xevent->xproperty.atom == 
	       gdk_display_atom (display, "XdndAware", FALSE)) ||

	      (xevent->xproperty.atom == 
	       gdk_display_atom (display, "_MOTIF_DRAG_RECEIVER_INFO", FALSE)))

	    {
	      gdk_error_trap_push ();
	      if (gdk_drag_get_protocol (GDK_WINDOW_DISPLAY(socket->plug_window),
					 xevent->xproperty.window,&protocol))
		gtk_drag_dest_set_proxy (GTK_WIDGET (socket),
					 socket->plug_window,
					 protocol, TRUE);
	      gdk_flush ();
	      gdk_error_trap_pop ();
	    }
	  return_val = GDK_FILTER_REMOVE;
	}
      break;
    case ClientMessage:
      if (xevent->xclient.message_type == gdk_display_atom (gdk_window_get_display(socket->plug_window), "_XEMBED", FALSE))

	{
	  handle_xembed_message (socket,
				 xevent->xclient.data.l[1],
				 xevent->xclient.data.l[2],
				 xevent->xclient.data.l[3],
				 xevent->xclient.data.l[4],
				 xevent->xclient.data.l[0]);
	  
	  
	  return_val = GDK_FILTER_REMOVE;
	}
      break;
    }
  
  return return_val;
}