File: xtrap.c

package info (click to toggle)
xkeycaps 2.47-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,268 kB
  • sloc: ansic: 40,772; sh: 132; makefile: 11
file content (149 lines) | stat: -rw-r--r-- 4,574 bytes parent folder | download | duplicates (10)
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
/* xkeycaps, Copyright (c) 1991, 1992, 1996 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#ifdef HAVE_XTRAP	/* whole file */

#define NEED_EVENTS
#define NEED_REPLIES
#include <X11/Xproto.h>
#include <X11/extensions/xtrap/xtraplib.h>
#include <X11/extensions/xtrap/xtraplibp.h>

#include <stdio.h>
#include "KbdWidgetP.h"

static void xkeycaps_xtrap_key_event_callback ();

void
#ifdef __STDC__
xkeycaps_xtrap_open_connection (KeyboardWidget widget)
#else /* ! __STDC__ */
xkeycaps_xtrap_open_connection (widget)
     KeyboardWidget widget;
#endif /* ! __STDC__ */
{
  XETC *tc = 0;
  if (((KeyboardWidget) widget)->keyboard.use_xtrap)
    {
      tc = XECreateTC (XtDisplay (widget), 0, 0);
      if (! tc)
	fprintf (stderr, "%s: XTrap initialization failed for display %s\n",
		 progname, DisplayString (XtDisplay (widget)));
    }
  if (tc)
    {
      EventFlags events;
      XETrapSetTimestamps (tc, True, False);
      memset (events, 0, sizeof (events));
      BitTrue (events, KeyPress);
      BitTrue (events, KeyRelease);
      XETrapSetEvents (tc, True, events);
      XEAddEventCBs (tc, events, xkeycaps_xtrap_key_event_callback,
		     (XtPointer) XtWindow (widget));
      XEStartTrapRequest (tc);
    }
  widget->keyboard.trap_data = (char *) tc;
}

void
#ifdef __STDC__
xkeycaps_xtrap_main_loop (KeyboardWidget keyboard, XtAppContext app)
#else /* ! __STDC__ */
xkeycaps_xtrap_main_loop (keyboard, app)
     KeyboardWidget keyboard;
     XtAppContext app;
#endif /* ! __STDC__ */
{
  XEvent event;
  XETC *tc = (XETC *) keyboard->keyboard.trap_data;
  XtInputMask imask;
  while (1)
    {
      imask = XETrapAppPending (app);
      
      /* Check to see what's going on so that we don't block in either
	 NextEvent or ProcessEvent since neither of these routines can
	 correctly deal with XTrap Events.
       */
      if (imask & XtIMXEvent)
	{
	  XtAppNextEvent (app, &event);
	  xkeycaps_DispatchEvent_hook (&event);
	}
      else if (imask & (XtIMTimer | XtIMAlternateInput))
	XtAppProcessEvent (app, (XtIMTimer | XtIMAlternateInput));
      else
	/* Nothing going on, so we need to block */
	XETrapWaitForSomething (app);
      
      XETrapDispatchEvent (&event, tc);
    }
}


void
#ifdef __STDC__
xkeycaps_xtrap_simulate_event (KeyboardWidget keyboard, XEvent *event)
#else /* ! __STDC__ */
xkeycaps_xtrap_simulate_event (keyboard, event)
     KeyboardWidget keyboard;
     XEvent *event;
#endif /* ! __STDC__ */
{
#if 0
  if (XESimulateXEventRequest ((XETC *) keyboard->keyboard.trap_data,
			       event->xkey.type, event->xkey.keycode,
			       /* event->xkey.x, event->xkey.y, */
			       0, 0,
			       0))
    fprintf (stderr, "%s: XTrap: KeyPress simulation failed\n", progname);
#endif
}


static void
#ifdef __STDC__
xkeycaps_xtrap_key_event_callback (XETC *tc, XETrapDatum *data, Window window)
#else /* ! __STDC__ */
xkeycaps_xtrap_key_event_callback (tc, data, window)
     XETC *tc;
     XETrapDatum *data;
     Window window;
#endif /* ! __STDC__ */
{
  XEvent event;
  if (data->u.event.u.u.type != KeyPress &&
      data->u.event.u.u.type != KeyRelease)
    return;
  event.xkey.serial    = 0;
  event.xkey.display   = tc->dpy;
  event.xkey.window    = window;
  event.xkey.type      = data->u.event.u.u.type;
  event.xkey.keycode   = data->u.event.u.u.detail;
  event.xkey.root      = data->u.event.u.keyButtonPointer.root;
  event.xkey.subwindow = data->u.event.u.keyButtonPointer.child;
  event.xkey.time      = data->u.event.u.keyButtonPointer.time;
  event.xkey.state     = data->u.event.u.keyButtonPointer.state;
  event.xkey.x_root    = data->u.event.u.keyButtonPointer.rootX;
  event.xkey.y_root    = data->u.event.u.keyButtonPointer.rootY;
  event.xkey.x         = data->u.event.u.keyButtonPointer.eventX;
  event.xkey.y         = data->u.event.u.keyButtonPointer.eventY;
  event.xkey.send_event= -1;

/*  fprintf (stderr, "simulating %s (%d, %d)\n",
	   (event.xkey.type == KeyPress ? "KeyPress" :
	    (event.xkey.type == KeyRelease ? "KeyRelease" : "???")),
	   event.xkey.type, event.xkey.keycode);
*/
  XtDispatchEvent (&event);
}

#endif /* HAVE_XTRAP */