XExtToWire.c 20.9 KB
Newer Older
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
/************************************************************

Copyright 1989, 1998  The Open Group

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.

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
OPEN GROUP 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 name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.

			All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Hewlett-Packard not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

********************************************************/

/****************************************************************
 *
 *	XExtToWire.c - reformat an XEvent into a wire event.
 */

52 53 54 55
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

56 57 58 59
#include <X11/extensions/XIproto.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/extutil.h>
60 61
#include <X11/extensions/Xge.h>
#include <X11/extensions/geproto.h>
62
#include "XIint.h"
63

Julien Cristau's avatar
Julien Cristau committed
64
_X_HIDDEN Status
65 66 67 68 69
_XiEventToWire(
    register Display	 *dpy,		/* pointer to display structure */
    register XEvent	 *re,		/* pointer to client event */
    register xEvent	**event,	/* wire protocol event */
    register int	 *count)
Daniel Stone's avatar
Daniel Stone committed
70 71
{
    XExtDisplayInfo *info = XInput_find_display(dpy);
72 73
    int i;

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
    if ((re->type & 0x7f) != GenericEvent) {
        switch ((re->type & 0x7f) - info->codes->first_event) {
            case XI_DeviceKeyPress:
            case XI_DeviceKeyRelease:
                {
                    register XDeviceKeyEvent *ev = (XDeviceKeyEvent *) re;
                    register deviceKeyButtonPointer *kev;
                    register deviceValuator *vev;

                    *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;
                }
            case XI_ProximityIn:
            case XI_ProximityOut:
                {
                    register XProximityNotifyEvent *ev = (XProximityNotifyEvent *) re;
                    register deviceKeyButtonPointer *pev;
                    register deviceValuator *vev;

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

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

                    vev = (deviceValuator *)++ pev;
                    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;
                }
            case XI_DeviceButtonPress:
            case XI_DeviceButtonRelease:
                {
                    register XDeviceButtonEvent *ev = (XDeviceButtonEvent *) re;
                    register deviceKeyButtonPointer *bev;
                    register deviceValuator *vev;

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

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

                    vev = (deviceValuator *)++ bev;
                    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;
                }
            case XI_DeviceMotionNotify:
                {
                    register XDeviceMotionEvent *ev = (XDeviceMotionEvent *) re;
                    register deviceKeyButtonPointer *mev;
                    register deviceValuator *vev;

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

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

                    vev = (deviceValuator *)++ mev;
                    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;
                }
            case XI_DeviceFocusIn:
            case XI_DeviceFocusOut:
                {
                    register XDeviceFocusChangeEvent *ev = (XDeviceFocusChangeEvent *) re;
                    register deviceFocus *fev;

                    *count = 1;
                    fev = (deviceFocus *) Xmalloc(*count * sizeof(xEvent));
                    if (!fev)
                        return (_XUnknownNativeEvent(dpy, re, *event));
                    *event = (xEvent *) fev;

                    fev->type = ev->type;
                    fev->window = ev->window;
                    fev->mode = ev->mode;
                    fev->detail = ev->detail;
                    fev->time = ev->time;
                    fev->deviceid = ev->deviceid;
                    break;
                }
            case XI_DeviceMappingNotify:
                {
                    register XDeviceMappingEvent *ev = (XDeviceMappingEvent *) re;
                    register deviceMappingNotify *mev;

                    *count = 1;
                    mev = (deviceMappingNotify *) Xmalloc(*count * sizeof(xEvent));
                    if (!mev)
                        return (_XUnknownNativeEvent(dpy, re, *event));
                    *event = (xEvent *) mev;

                    mev->type = ev->type;
                    mev->firstKeyCode = ev->first_keycode;
                    mev->request = ev->request;
                    mev->count = ev->count;
                    mev->time = ev->time;
                    mev->deviceid = ev->deviceid;
                    break;
                }
            case XI_DeviceStateNotify:
                {
                    register XDeviceStateNotifyEvent *ev = (XDeviceStateNotifyEvent *) re;
                    register deviceStateNotify *sev;
                    register xEvent *tev;
                    XInputClass *any = (XInputClass *) & ev->data[0];
                    unsigned char *sav_id;

                    *count = 1;

                    for (i = 0; i < ev->num_classes; i++) {
                        if (any->class == KeyClass) {
                            XKeyStatus *k = (XKeyStatus *) any;

                            if (k->num_keys > 32)
                                (*count)++;
                        } else if (any->class == ButtonClass) {
                            XButtonStatus *b = (XButtonStatus *) any;

                            if (b->num_buttons > 32)
                                (*count)++;
                        } else if (any->class == ValuatorClass) {
                            XValuatorStatus *v = (XValuatorStatus *) any;

                            if (v->num_valuators > 3)
                                (*count)++;
                        }
                        any = (XInputClass *) ((char *)any + any->length);
                    }

                    sev = (deviceStateNotify *) Xmalloc(*count * sizeof(xEvent));
                    if (!sev)
                        return (_XUnknownNativeEvent(dpy, re, *event));
                    *event = (xEvent *) sev;
                    tev = (xEvent *) (sev + 1);

                    sev->type = ev->type;
                    sev->deviceid = ev->deviceid;
                    sav_id = &(sev->deviceid);
                    sev->time = ev->time;
                    sev->classes_reported = 0;

                    any = (XInputClass *) & ev->data[0];
                    for (i = 0; i < ev->num_classes; i++) {
                        if (any->class == KeyClass) {
                            XKeyStatus *k = (XKeyStatus *) any;
                            register deviceKeyStateNotify *kev;

                            sev->classes_reported |= (1 << KeyClass);
                            sev->num_keys = k->num_keys;
                            memcpy((char *)(sev->keys), (char *)(k->keys), 4);
                            if (k->num_keys > 32) {
                                kev = (deviceKeyStateNotify *) tev++;
                                kev->type = info->codes->first_event +
                                    XI_DeviceKeystateNotify;
                                kev->deviceid = ev->deviceid;
                                *sav_id |= MORE_EVENTS;
                                sav_id = &(kev->deviceid);
                                memcpy((char *)(kev->keys), (char *)(&k->keys[4]), 28);
                            }
                        } else if (any->class == ButtonClass) {
                            XButtonStatus *b = (XButtonStatus *) any;
                            register deviceButtonStateNotify *bev;

                            sev->classes_reported |= (1 << ButtonClass);
                            sev->num_buttons = b->num_buttons;
                            memcpy((char *)(sev->buttons), (char *)(b->buttons), 4);
                            if (b->num_buttons > 32) {
                                bev = (deviceButtonStateNotify *) tev++;
                                bev->type = info->codes->first_event +
                                    XI_DeviceButtonstateNotify;
                                bev->deviceid = ev->deviceid;
                                *sav_id |= MORE_EVENTS;
                                sav_id = &(bev->deviceid);
                                memcpy((char *)(bev->buttons), (char *)(&b->buttons[4]),
                                        28);
                            }
                        } else if (any->class == ValuatorClass) {
                            XValuatorStatus *val = (XValuatorStatus *) any;
                            register deviceValuator *vev;

                            sev->classes_reported |= (1 << ValuatorClass);
                            sev->num_valuators = val->num_valuators < 3 ?
                                val->num_valuators : 3;
                            switch (sev->num_valuators) {
                                case 3:
                                    sev->valuator2 = val->valuators[2];
                                case 2:
                                    sev->valuator1 = val->valuators[1];
                                case 1:
                                    sev->valuator0 = val->valuators[0];
                            }
                            if (val->num_valuators > 3) {
                                vev = (deviceValuator *) tev++;
                                vev->type = info->codes->first_event + XI_DeviceValuator;
                                vev->deviceid = ev->deviceid;
                                vev->first_valuator = 3;
                                vev->num_valuators = val->num_valuators - 3;
                                *sav_id |= MORE_EVENTS;
                                sav_id = &(vev->deviceid);
                                i = val->num_valuators;
                                if (i > 6)
                                    i = 6;
                                switch (i) {
                                    case 6:
                                        vev->valuator2 = val->valuators[5];
                                    case 5:
                                        vev->valuator1 = val->valuators[4];
                                    case 4:
                                        vev->valuator0 = val->valuators[3];
                                }
                            }
                        }
                        any = (XInputClass *) ((char *)any + any->length);
                    }
                    break;
                }
            case XI_ChangeDeviceNotify:
                {
                    register XChangeDeviceNotifyEvent *ev = (XChangeDeviceNotifyEvent *) re;
                    register changeDeviceNotify *cev;

                    *count = 1;
                    cev = (changeDeviceNotify *) Xmalloc(*count * sizeof(xEvent));
                    if (!cev)
                        return (_XUnknownNativeEvent(dpy, re, *event));
                    *event = (xEvent *) cev;

                    cev->type = ev->type;
                    cev->request = ev->request;
                    cev->time = ev->time;
                    cev->deviceid = ev->deviceid;
                    break;
                }
            default:
                return (_XUnknownNativeEvent(dpy, re, *event));
        } /* switch */
    } else /* if type != GenericEvent */
Daniel Stone's avatar
Daniel Stone committed
462
    {
463
#if 0
464 465
        switch (((XGenericEvent*)re)->evtype)
        {
466
            case XI_HierarchyChangedNotify:
467
                {
468 469
                    XDeviceHierarchyChangedEvent *ev =
                        (XDeviceHierarchyChangedEvent*)re;
470
                    XIDeviceHierarchyEvent *dhcev;
471
                    *count = 1;
472
                    dhcev = (XIDeviceHierarchyEvent *)Xmalloc(*count * sizeof(xEvent));
473
                    if (!dhcev)
474 475
                        return (_XUnknownNativeEvent(dpy, re, *event));

476 477 478
                    *event = (xEvent *) dhcev;
                    dhcev->type = ev->type;
                    dhcev->time = ev->time;
479 480 481 482
                    break;
                }
            default:
                return (_XUnknownNativeEvent(dpy, re, *event));
483
        }
484
#endif
485
    }
Daniel Stone's avatar
Daniel Stone committed
486 487
    return (1);
}