File: VXInput.c

package info (click to toggle)
via 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,480 kB
  • ctags: 2,266
  • sloc: ansic: 30,757; makefile: 101; sh: 46
file content (343 lines) | stat: -rwxr-xr-x 9,479 bytes parent folder | download | duplicates (2)
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
/*
 *  $Id: VXInput.c 3177 2008-04-01 14:47:24Z karstenm $
 *
 *  This file contains VX input-callback registration routines.
 */

/*
 *  Copyright 1993, 1994 University of British Columbia
 *
 *  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 appears in all copies and that both that
 *  copyright notice and this permission notice appear in supporting
 *  documentation. UBC makes no representations about the suitability of this
 *  software for any purpose. It is provided "as is" without express or
 *  implied warranty.
 *
 *  Author: Daniel Ko, UBC Laboratory for Computational Intelligence
 */

/* From the Vista library: */
#include "viaio/Vlib.h"
#include "viaio/mu.h"
#include "viaio/os.h"
#include "viaio/VImage.h"
#include "viaio/VImageView.h"
#include "viaio/VList.h"
#include "viaio/VX.h"
#include "viaio/VXPrivate.h"

/* From X11R5 Xt and Motif: */
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/keysym.h>
#include <Xm/Xm.h>

/* File identification string: */
VRcsId ("$Id: VXInput.c 3177 2008-04-01 14:47:24Z karstenm $");

/*
 * Record for storing input related data.
 */

typedef struct {
    VList input_cb_list[VXnInputTypes];	/* array of input callback lists */
} InputRec;
static InputRec l_input;

/*
 * Type: VXInputCb
 *
 * Record for storing an input-callback and its associated data.
 */
typedef struct {
    VXInputCallback callback;	/* callback function */
    VPointer client_data;	/* client data */
} VXInputCb;


/* Later in this file: */
static void DispatchKeyPress (Widget, VList, XKeyPressedEvent *,
			      Boolean *);
static void DispatchButtonPress (Widget, VList, XButtonPressedEvent *,
				 Boolean *);
static void DispatchButtonRelease (Widget, VList, XButtonReleasedEvent *,
				   Boolean *);
static void DispatchPointerMotion (Widget, VList, XMotionEvent *,
				   Boolean *);


/*
 * Extern: VX_InitInput
 *
 * Initialize the menu module.
 */

extern VBoolean VX_InitInput (void)
{
    int i;

    for (i = 0; i < VXnInputTypes; i++)
	l_input.input_cb_list[i] = NULL;
    return TRUE;
}


/*
 * Local: DispatchKeyPress
 *
 * Call the input callbacks associated with the VXIkeyPress user input.
 */

static void DispatchKeyPress (Widget w, VList callback_list,
			      XKeyPressedEvent *event,
			      Boolean *continue_to_dispatch)
{
    VXInputDataRec input_data;
    double row, column;
    VXInputCb *icb;
    char key[256];
    KeySym keySym;
    XComposeStatus compose;

    /* Determine the image coordinate of the user input: */
    if (VX_App.v.image == NULL ||
	!VImageViewClipToImage (VX_App.x.imageView, event->x, event->y,
				& row, & column)) {
	return;
    } else {
	input_data.row = (int) row;
	input_data.column = (int) column;
    }

    /* Set the input type: */
    input_data.input_type = VXIkeyPress;

    /* Find the ASCII code of the key being pressed: */
    (void) XLookupString(event, key, sizeof (key), &keySym, &compose);
    input_data.value =
	(keySym < XK_space || keySym > XK_asciitilde) ? 0 : key[0];

    /* Set the modifiers field: */
    /* ==> This relies on a correspondence between X and VX definitions. */
    input_data.modifiers = event->state;

    /* call each callback associated with the user input */
    icb = (VXInputCb *) VListFirst (callback_list);
    while (icb != NULL) {
	/* Call the callback function: */
	(icb->callback)(&input_data, icb->client_data);
	icb = (VXInputCb *) VListNext (callback_list);
    }
}


/*
 * Local: DispatchButtonPress
 *
 * Call the input callbacks associated with the VXIbuttonPress user input.
 */

static void DispatchButtonPress (Widget w, VList callback_list,
				 XButtonPressedEvent *event,
				 Boolean *continue_to_dispatch)
{
    VXInputDataRec input_data;
    double row, column;
    VXInputCb *icb;

    /* Determine the image coordinate of the user input: */
    if (VX_App.v.image == NULL ||
	!VImageViewClipToImage (VX_App.x.imageView, event->x, event->y,
				&row, &column)) {
	return;
    } else {
	input_data.row = (int) row;
	input_data.column = (int) column;
    }

    /* Set the input type: */
    /* ==> This relies on a correspondence between X and VX definitions. */
    input_data.input_type = VXIbuttonPress;

    /* Set the value field to the button number: */
    /* ==> This relies on a correspondence between X and VX definitions. */
    input_data.value = event->button;

    /* Set the modifer field: */
    input_data.modifiers = event->state;

    /* call each callback associated with the VXIbuttonPress user input */
    icb = (VXInputCb *) VListFirst (callback_list);
    while (icb != NULL) {
	/* Call the callback function: */
	(icb->callback)(&input_data, icb->client_data);
	icb = (VXInputCb *) VListNext (callback_list);
    }
}


/*
 * Local: DispatchButtonRelease
 *
 * Call the input callbacks associated with the VXIbuttonRelease user input.
 */

static void DispatchButtonRelease (Widget w, VList callback_list,
				   XButtonReleasedEvent *event,
				   Boolean *continue_to_dispatch)
{
    VXInputDataRec input_data;
    double row, column;
    VXInputCb *icb;

    /* Determine the image coordinate of the user input: */
    if (VX_App.v.image == NULL ||
	!VImageViewClipToImage (VX_App.x.imageView, event->x, event->y,
				&row, &column)) {
	return;
    } else {
	input_data.row = (int) row;
	input_data.column = (int) column;
    }

    /* Set the input type: */
    input_data.input_type = VXIbuttonRelease;

    /* Set the value field to the button number: */
    /* ==> This relies on a correspondence between X and VX definitions. */
    input_data.value = event->button;

    /* Set the modifer field: */
    input_data.modifiers = (VXModifierMask) (event->state);

    /* call each callback associated with the VXIbuttonRelease user input */
    icb = (VXInputCb *) VListFirst (callback_list);
    while (icb != NULL) {
	/* Call the callback function: */
	(icb->callback)(&input_data, icb->client_data);
	icb = (VXInputCb *) VListNext (callback_list);
    }
}

/*
 * Local: DispatchPointerMotion
 *
 * Call the input callbacks associated with the VXIPointerMotion user input.
 */

static void DispatchPointerMotion (Widget w, VList callback_list,
				   XMotionEvent *event,
				   Boolean *continue_to_dispatch)
{
    VXInputDataRec input_data;
    double row, column;
    VXInputCb *icb;
    int root_x, root_y, win_x, win_y;
    unsigned int keys_buttons;
    Window root, child;

    /* Only want hints: */
    XQueryPointer (XtDisplay(VX_App.x.imageView), XtWindow(VX_App.x.imageView),
		   &root, &child, &root_x, &root_y, &win_x, &win_y,
		   &keys_buttons);

    /* Determine the image coordinate of the user input: */
    if (VX_App.v.image == NULL ||
	!VImageViewClipToImage (VX_App.x.imageView, win_x, win_y,
				&row, &column)) {
	return;
    } else {
	input_data.row = (int) row;
	input_data.column = (int) column;
    }

    /* Set the input type: */
    input_data.input_type = VXIpointerMotion;

    /* Set the modifier field: */
    input_data.modifiers = (VXModifierMask) (event->state);

    /* Call each callback associated with the VXIpointerMotion user input: */
    icb = (VXInputCb *) VListFirst (callback_list);
    while (icb != NULL) {
	/* Call the callback function: */
	(icb->callback)(&input_data, icb->client_data);
	icb = (VXInputCb *) VListNext (callback_list);
    }
}


/*
 * VXAddInputCallback
 *
 * Associate a callback with a user input.
 */

void VXAddInputCallback (VXInputType input_type, VXInputCallback callback,
			 VPointer client_data)
{
    VXInputCb *icb;
    XtEventHandler event_handler = NULL;
    EventMask event_mask = 0;

    if (! VX_App.initialized)
	VError ("VXAddInputCallback: VX not initialized");

    if (VX_App.in_main_loop)
	VError ("VXAddInputCallback: Main event loop already started");

    /* Check if callback is NULL: */
    if (callback == NULL)
	VError ("VXAddInputCallback: Callback function cannot be NULL");

    /* Check validity of input_type: */
    if (input_type < 0 || input_type >= VXnInputTypes)
	VError ("VXAddInputCallback: Invalid input type %d", input_type);

    /* Allocate a structure to store callback and client_data: */
    icb = VMalloc (sizeof (VXInputCb));
    icb->callback = callback;
    icb->client_data = client_data;

    /* If the appropriate callback list is NULL, allocate space for the list
       and register the appropriate event handler */
    if (l_input.input_cb_list[input_type] == NULL) {

	l_input.input_cb_list[input_type] = VListCreate ();

	switch (input_type) {

	case VXIkeyPress:
	    event_handler = (XtEventHandler) DispatchKeyPress;
	    event_mask = KeyPressMask;
	    break;

	case VXIbuttonPress:
	    event_handler = (XtEventHandler) DispatchButtonPress;
	    event_mask = ButtonPressMask;
	    break;

	case VXIbuttonRelease:
	    event_handler = (XtEventHandler) DispatchButtonRelease;
	    event_mask = ButtonReleaseMask;
	    break;

	case VXIpointerMotion:
	    event_handler = (XtEventHandler) DispatchPointerMotion;
	    event_mask = PointerMotionMask | PointerMotionHintMask;
	    break;

	default:
	    break;
	}

	XtAddEventHandler (VX_App.x.imageView, event_mask, FALSE,
			   event_handler,
			   (XtPointer) l_input.input_cb_list[input_type]);
    }

    /* Add the callback structure to the appropriate list: */
    VListAppend (l_input.input_cb_list[input_type], (VPointer) icb);
}