summaryrefslogtreecommitdiff
path: root/src/XpExtUtil.c
blob: 5a40d846be289ca1d0c2947b378569a3a91a4ce0 (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
/* $Xorg: XpExtUtil.c,v 1.3 2000/08/17 19:46:06 cpqbld Exp $ */
/******************************************************************************
 ******************************************************************************
 **
 ** (c) Copyright 1996 Hewlett-Packard Company
 ** (c) Copyright 1996 International Business Machines Corp.
 ** (c) Copyright 1996 Sun Microsystems, Inc.
 ** (c) Copyright 1996 Novell, Inc.
 ** (c) Copyright 1996 Digital Equipment Corp.
 ** (c) Copyright 1996 Fujitsu Limited
 ** (c) Copyright 1996 Hitachi, Ltd.
 ** 
 ** Permission is hereby granted, free of charge, to any person obtaining a copy
 ** of this software and associated documentation files (the "Software"), to deal
 ** in the Software without restriction, including without limitation the rights
 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 ** copies of the Software, and to permit persons to whom the Software is
 ** furnished to do so, subject to the following conditions:
 **
 ** The above copyright notice and this permission notice shall be included in
 ** all copies or substantial portions of the Software.
 **
 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 ** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 ** IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 **
 ** Except as contained in this notice, the names of the copyright holders shall
 ** not be used in advertising or otherwise to promote the sale, use or other
 ** dealings in this Software without prior written authorization from said
 ** copyright holders.
 **
 ******************************************************************************
 *****************************************************************************/
/* $XFree86: xc/lib/Xp/XpExtUtil.c,v 1.7 2002/10/16 00:37:31 dawes Exp $ */

#define NEED_EVENTS
#define NEED_REPLIES
#include <stdio.h>
#include <X11/extensions/Printstr.h>
#include <X11/Xlibint.h>
#include "XpExtUtil.h"
#include <X11/Xos.h>

#define ENQUEUE_EVENT   True
#define DONT_ENQUEUE    False

static XExtensionInfo     xp_info_data;
static XExtensionInfo     *xp_info = &xp_info_data;
static /* const */ char   *xp_extension_name = XP_PRINTNAME;

static int    XpClose();
static char   *XpError();
static Bool   XpWireToEvent();
static Status XpEventToWire();

#define XpCheckExtension(dpy,i,val) \
  XextCheckExtension (dpy, i, xp_extension_name, val)

static /* const */ XExtensionHooks xpprint_extension_hooks = {
    NULL,			/* create_gc */
    NULL,			/* copy_gc */
    NULL,			/* flush_gc */
    NULL,			/* free_gc */
    NULL,			/* create_font */
    NULL,			/* free_font */
    XpClose,			/* close_display */
    XpWireToEvent,		/* wire_to_event */
    XpEventToWire,		/* event_to_wire */
    NULL,			/* error */
    XpError,			/* error_string */
};

typedef struct {
    int     present;
    short   major_version;
    short   minor_version;
} XPrintLocalExtensionVersion;

typedef struct _xpPrintData {
    XEvent              data;
    XPrintLocalExtensionVersion   *vers;
} xpPrintData;

static char *XpErrorList[ /* XP_ERRORS */ ] = {
	"XPBadContext",
	"XPBadSequence",
	"XPBadResourceID"
};

XEXT_GENERATE_FIND_DISPLAY (xp_find_display, xp_info, 
	xp_extension_name, &xpprint_extension_hooks, XP_EVENTS, NULL)

static XEXT_GENERATE_ERROR_STRING (XpError, xp_extension_name,
				   XP_ERRORS, XpErrorList)

/*******************************************************************
 *
 * XP Print extension versions.
 */

static XPrintLocalExtensionVersion xpprintversions[] = {{XP_ABSENT,0,0},
	{XP_PRESENT, XP_PROTO_MAJOR, XP_PROTO_MINOR}};


/***********************************************************************
 *
 * Check to see if the Xp Print extension is installed in the server.
 * Also check to see if the version is >= the requested version.
 *
 * xpprintversions[] shows all revisions of this library, past to present.
 * xpprintversions[version_index] shows which version *this* library is.
 */

int XpCheckExtInitUnlocked(dpy, version_index)
    register	Display *dpy;
    register	int	version_index;
{
    XExtDisplayInfo 	*info = xp_find_display (dpy);

    XpCheckExtension (dpy, info, -1);


    /*
     * Check for broken condition - a partial info world.
     */
    if (info->data) {
	if (!(((xpPrintData *) info->data)->vers)) {
	    return (-1);
	}
    }

    if (info->data == NULL) {
	/*
	 * Hang a Xp private data struct.   Use it for version
	 * information.
	 */
	info->data = (caddr_t) Xmalloc (sizeof (xpPrintData));
	if (!info->data) {
	    return (-1);
	}

	((xpPrintData *) info->data)->vers =
	    (XPrintLocalExtensionVersion *) Xmalloc(sizeof(XPrintLocalExtensionVersion));
	if (!(((xpPrintData *) info->data)->vers)) {
	    return (-1);
	}

	/*
	 * Set present to false so that XpQueryVersion will fill
	 * it in vs using its own cache which now contains garbage.
	 */
	((xpPrintData *) info->data)->vers->present = False;

	((xpPrintData *) info->data)->vers->present = XpQueryVersion(dpy,
	    &(((xpPrintData *) info->data)->vers->major_version),
	    &(((xpPrintData *) info->data)->vers->minor_version) );
    }
    if (xpprintversions[version_index].major_version > XP_DONT_CHECK) {
	if ( ( ((xpPrintData *) info->data)->vers->major_version
		< xpprintversions[version_index].major_version) ||
	     ( (((xpPrintData *) info->data)->vers->major_version
		== xpprintversions[version_index].major_version) &&
	     (((xpPrintData *) info->data)->vers->minor_version
		< xpprintversions[version_index].minor_version))) {
	    return (-1);
	}
    }
    
    return (0);
}

int XpCheckExtInit(dpy, version_index)
    register	Display *dpy;
    register	int	version_index;
{
    int retval;
    
    _XLockMutex(_Xglobal_lock);
    
    retval = XpCheckExtInitUnlocked(dpy, version_index);
    
    _XUnlockMutex(_Xglobal_lock);

    return retval;
}

/***********************************************************************
 *
 * Close display routine.
 *
 */

static int
XpClose (dpy, codes)
    Display *dpy;
    XExtCodes *codes;
    {
    XExtDisplayInfo 	*info = xp_find_display (dpy);

    _XLockMutex(_Xglobal_lock);
    if ( ((xpPrintData *) info->data)) {
	if (((xpPrintData *) info->data)->vers) {
	    XFree((char *)((xpPrintData *) info->data)->vers);
	}
	XFree((char *)info->data);
	info->data = (char *) NULL;	/* NULL since tests are run on */
    }
    _XUnlockMutex(_Xglobal_lock);

    return XextRemoveDisplay (xp_info, dpy);
    }


/******************************************************************************
 *
 * Handle extension events.
 */

/********************************************************************
 *
 * Reformat a wire event into an XEvent structure of the right type.
 */
static Bool
XpWireToEvent (dpy, re, event)
    Display	*dpy;
    XEvent	*re;
    xEvent	*event;
{
    XExtDisplayInfo *info = xp_find_display (dpy);


    /*
     * type, serial, send_event, display are common to all events.
     */
    re->type = event->u.u.type & 0x7f;
    ((XAnyEvent *)re)->serial = _XSetLastRequestRead(dpy,
                                     (xGenericReply *)event);
    ((XAnyEvent *)re)->send_event = ((event->u.u.type & 0x80) != 0);
    ((XAnyEvent *)re)->display = dpy;


    switch (re->type - info->codes->first_event)
	{

	case XPPrintNotify:
	    {
	    register XPPrintEvent  *ev = (XPPrintEvent *)     re;
	    xPrintPrintEvent      *ev2 = (xPrintPrintEvent *) event;

	    /* type, serial, send_event, *display */

	    ev->context		= (XPContext) ev2->printContext;
	    ev->cancel		= (Bool) ev2->cancel;
	    ev->detail		= (int) ev2->detail;

	    return (ENQUEUE_EVENT);
	    break;
	    }

	case XPAttributeNotify:
	    {
	    register XPAttributeEvent  *ev = (XPAttributeEvent *)     re;
	    xPrintAttributeEvent       *ev2 = (xPrintAttributeEvent *) event;

	    /* type, serial, send_event, *display */

	    ev->context		= (XPContext) ev2->printContext;
	    ev->detail		= (int) ev2->detail;

	    return (ENQUEUE_EVENT);
	    break;
	    }

	default:
	    printf ("XpWireToEvent: UNKNOWN WIRE EVENT! type=%d\n",re->type);
	    break;
	}

    return (DONT_ENQUEUE);
}

/********************************************************************
 *
 * Reformat an XEvent into a wire event.
 */
static Status
XpEventToWire(dpy, re, event, count)
    register Display *dpy;      /* pointer to display structure */
    register XEvent *re;        /* pointer to client event */
    register xEvent **event;    /* wire protocol event */
    register int *count;
{
    XExtDisplayInfo *info = (XExtDisplayInfo *) xp_find_display (dpy);

    switch ((re->type & 0x7f) - info->codes->first_event)
        {
#ifdef PRINT_SomeEventExample2
        case PRINT_SomeEventExample:
            {
            register XDeviceKeyEvent *ev = (XDeviceKeyEvent*) re;
            register deviceKeyButtonPointer *kev;
            register deviceValuator *vev;
	    int i;

            *count = 2;
            kev = (deviceKeyButtonPointer *) Xmalloc (*count * sizeof (xEvent));
            if (!kev)
                return(_XUnknownNativeEvent(dpy, re, event));
            *event = (xEvent *) kev;

            kev->type           = ev->type;
            kev->root           = ev->root;
            kev->event          = ev->window;
            kev->child          = ev->subwindow;
            kev->time           = ev->time;
            kev->event_x        = ev->x ;
            kev->event_y        = ev->y ;
            kev->root_x         = ev->x_root;
            kev->root_y         = ev->y_root;
            kev->state          = ev->state;
            kev->same_screen    = ev->same_screen;
            kev->detail         = ev->keycode;
            kev->deviceid       = ev->deviceid | MORE_EVENTS;

            vev = (deviceValuator *) ++kev;
            vev->type = info->codes->first_event + XI_DeviceValuator;
            vev->deviceid = ev->deviceid;
            vev->device_state = ev->device_state;
            vev->first_valuator = ev->first_axis;
            vev->num_valuators = ev->axes_count;
            i = vev->num_valuators;
            if (i > 6) i = 6;
            switch (i)
                {
                case 6: vev->valuator5 = ev->axis_data[5];
                case 5: vev->valuator4 = ev->axis_data[4];
                case 4: vev->valuator3 = ev->axis_data[3];
                case 3: vev->valuator2 = ev->axis_data[2];
                case 2: vev->valuator1 = ev->axis_data[1];
                case 1: vev->valuator0 = ev->axis_data[0];
                }
            break;
            }
#endif /* PRINT_SomeEventExample2 */

        default:
            return(_XUnknownNativeEvent(dpy, re, *event));
        }
}