protocol-common.c revision 1b5d61b8
16747b715Smrg/**
26747b715Smrg * Copyright © 2009 Red Hat, Inc.
36747b715Smrg *
46747b715Smrg *  Permission is hereby granted, free of charge, to any person obtaining a
56747b715Smrg *  copy of this software and associated documentation files (the "Software"),
66747b715Smrg *  to deal in the Software without restriction, including without limitation
76747b715Smrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
86747b715Smrg *  and/or sell copies of the Software, and to permit persons to whom the
96747b715Smrg *  Software is furnished to do so, subject to the following conditions:
106747b715Smrg *
116747b715Smrg *  The above copyright notice and this permission notice (including the next
126747b715Smrg *  paragraph) shall be included in all copies or substantial portions of the
136747b715Smrg *  Software.
146747b715Smrg *
156747b715Smrg *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
166747b715Smrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
176747b715Smrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
186747b715Smrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
196747b715Smrg *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
206747b715Smrg *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
216747b715Smrg *  DEALINGS IN THE SOFTWARE.
226747b715Smrg */
236747b715Smrg
246747b715Smrg#ifdef HAVE_DIX_CONFIG_H
256747b715Smrg#include <dix-config.h>
266747b715Smrg#endif
276747b715Smrg
2835c4bbdfSmrg#include <errno.h>
296747b715Smrg#include <stdint.h>
3035c4bbdfSmrg#include "extinit.h"            /* for XInputExtensionInit */
316747b715Smrg#include "exglobals.h"
3235c4bbdfSmrg#include "xkbsrv.h"             /* for XkbInitPrivates */
3335c4bbdfSmrg#include "xserver-properties.h"
3435c4bbdfSmrg#include "syncsrv.h"
3535c4bbdfSmrg#include <X11/extensions/XI2.h>
366747b715Smrg
371b5d61b8Smrg#define INSIDE_PROTOCOL_COMMON
386747b715Smrg#include "protocol-common.h"
396747b715Smrg
406747b715Smrgstruct devices devices;
416747b715SmrgScreenRec screen;
426747b715SmrgWindowRec root;
436747b715SmrgWindowRec window;
4435c4bbdfSmrgstatic ClientRec server_client;
456747b715Smrg
4635c4bbdfSmrgvoid *global_userdata;
476747b715Smrg
481b5d61b8Smrgint enable_GrabButton_wrap = 1;
491b5d61b8Smrgint enable_XISetEventMask_wrap = 1;
501b5d61b8Smrg
5135c4bbdfSmrgstatic void
5235c4bbdfSmrgfake_init_sprite(DeviceIntPtr dev)
536747b715Smrg{
546747b715Smrg    SpritePtr sprite;
5535c4bbdfSmrg
566747b715Smrg    sprite = dev->spriteInfo->sprite;
576747b715Smrg
586747b715Smrg    sprite->spriteTraceSize = 10;
596747b715Smrg    sprite->spriteTrace = calloc(sprite->spriteTraceSize, sizeof(WindowPtr));
606747b715Smrg    sprite->spriteTraceGood = 1;
616747b715Smrg    sprite->spriteTrace[0] = &root;
626747b715Smrg    sprite->hot.x = SPRITE_X;
636747b715Smrg    sprite->hot.y = SPRITE_Y;
646747b715Smrg    sprite->hotPhys.x = sprite->hot.x;
656747b715Smrg    sprite->hotPhys.y = sprite->hot.y;
666747b715Smrg    sprite->win = &window;
676747b715Smrg    sprite->hotPhys.pScreen = &screen;
686747b715Smrg    sprite->physLimits.x1 = 0;
696747b715Smrg    sprite->physLimits.y1 = 0;
706747b715Smrg    sprite->physLimits.x2 = screen.width;
716747b715Smrg    sprite->physLimits.y2 = screen.height;
726747b715Smrg}
736747b715Smrg
7435c4bbdfSmrg/* This is essentially CorePointerProc with ScrollAxes added */
7535c4bbdfSmrgstatic int
7635c4bbdfSmrgTestPointerProc(DeviceIntPtr pDev, int what)
7735c4bbdfSmrg{
7835c4bbdfSmrg#define NBUTTONS 10
7935c4bbdfSmrg#define NAXES 4
8035c4bbdfSmrg    BYTE map[NBUTTONS + 1];
8135c4bbdfSmrg    int i = 0;
8235c4bbdfSmrg    Atom btn_labels[NBUTTONS] = { 0 };
8335c4bbdfSmrg    Atom axes_labels[NAXES] = { 0 };
8435c4bbdfSmrg
8535c4bbdfSmrg    switch (what) {
8635c4bbdfSmrg    case DEVICE_INIT:
8735c4bbdfSmrg        for (i = 1; i <= NBUTTONS; i++)
8835c4bbdfSmrg            map[i] = i;
8935c4bbdfSmrg
9035c4bbdfSmrg        btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
9135c4bbdfSmrg        btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
9235c4bbdfSmrg        btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
9335c4bbdfSmrg        btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
9435c4bbdfSmrg        btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
9535c4bbdfSmrg        btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
9635c4bbdfSmrg        btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
9735c4bbdfSmrg        /* don't know about the rest */
9835c4bbdfSmrg
9935c4bbdfSmrg        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
10035c4bbdfSmrg        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
10135c4bbdfSmrg        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_VSCROLL);
10235c4bbdfSmrg        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HSCROLL);
10335c4bbdfSmrg
10435c4bbdfSmrg        if (!InitPointerDeviceStruct
10535c4bbdfSmrg            ((DevicePtr) pDev, map, NBUTTONS, btn_labels,
10635c4bbdfSmrg             (PtrCtrlProcPtr) NoopDDA, GetMotionHistorySize(), NAXES,
10735c4bbdfSmrg             axes_labels)) {
10835c4bbdfSmrg            ErrorF("Could not initialize device '%s'. Out of memory.\n",
10935c4bbdfSmrg                   pDev->name);
11035c4bbdfSmrg            return BadAlloc;
11135c4bbdfSmrg        }
11235c4bbdfSmrg        pDev->valuator->axisVal[0] = screenInfo.screens[0]->width / 2;
11335c4bbdfSmrg        pDev->last.valuators[0] = pDev->valuator->axisVal[0];
11435c4bbdfSmrg        pDev->valuator->axisVal[1] = screenInfo.screens[0]->height / 2;
11535c4bbdfSmrg        pDev->last.valuators[1] = pDev->valuator->axisVal[1];
11635c4bbdfSmrg
11735c4bbdfSmrg        /* protocol-xiquerydevice.c relies on these increment */
11835c4bbdfSmrg        SetScrollValuator(pDev, 2, SCROLL_TYPE_VERTICAL, 2.4, SCROLL_FLAG_NONE);
11935c4bbdfSmrg        SetScrollValuator(pDev, 3, SCROLL_TYPE_HORIZONTAL, 3.5,
12035c4bbdfSmrg                          SCROLL_FLAG_PREFERRED);
12135c4bbdfSmrg        break;
12235c4bbdfSmrg
12335c4bbdfSmrg    case DEVICE_CLOSE:
12435c4bbdfSmrg        break;
12535c4bbdfSmrg
12635c4bbdfSmrg    default:
12735c4bbdfSmrg        break;
12835c4bbdfSmrg    }
12935c4bbdfSmrg
13035c4bbdfSmrg    return Success;
13135c4bbdfSmrg
13235c4bbdfSmrg#undef NBUTTONS
13335c4bbdfSmrg#undef NAXES
13435c4bbdfSmrg}
13535c4bbdfSmrg
1366747b715Smrg/**
1376747b715Smrg * Create and init 2 master devices (VCP + VCK) and two slave devices, one
1386747b715Smrg * default mouse, one default keyboard.
1396747b715Smrg */
14035c4bbdfSmrgstruct devices
14135c4bbdfSmrginit_devices(void)
1426747b715Smrg{
1436747b715Smrg    ClientRec client;
14435c4bbdfSmrg    struct devices local_devices;
14535c4bbdfSmrg    int ret;
14635c4bbdfSmrg
14735c4bbdfSmrg    /*
14835c4bbdfSmrg     * Put a unique name in display pointer so that when tests are run in
14935c4bbdfSmrg     * parallel, their xkbcomp outputs to /tmp/server-<display>.xkm don't
15035c4bbdfSmrg     * stomp on each other.
15135c4bbdfSmrg     */
15235c4bbdfSmrg#ifdef HAVE_GETPROGNAME
15335c4bbdfSmrg    display = getprogname();
15435c4bbdfSmrg#elif HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
15535c4bbdfSmrg    display = program_invocation_short_name;
15635c4bbdfSmrg#endif
1576747b715Smrg
1586747b715Smrg    client = init_client(0, NULL);
1596747b715Smrg
16035c4bbdfSmrg    AllocDevicePair(&client, "Virtual core", &local_devices.vcp, &local_devices.vck,
1616747b715Smrg                    CorePointerProc, CoreKeyboardProc, TRUE);
16235c4bbdfSmrg    inputInfo.pointer = local_devices.vcp;
1636747b715Smrg
16435c4bbdfSmrg    inputInfo.keyboard = local_devices.vck;
16535c4bbdfSmrg    ret = ActivateDevice(local_devices.vcp, FALSE);
16635c4bbdfSmrg    assert(ret == Success);
16735c4bbdfSmrg    /* This may fail if xkbcomp fails or xkb-config is not found. */
16835c4bbdfSmrg    ret = ActivateDevice(local_devices.vck, FALSE);
16935c4bbdfSmrg    assert(ret == Success);
17035c4bbdfSmrg    EnableDevice(local_devices.vcp, FALSE);
17135c4bbdfSmrg    EnableDevice(local_devices.vck, FALSE);
17235c4bbdfSmrg
17335c4bbdfSmrg    AllocDevicePair(&client, "", &local_devices.mouse, &local_devices.kbd,
17435c4bbdfSmrg                    TestPointerProc, CoreKeyboardProc, FALSE);
17535c4bbdfSmrg    ret = ActivateDevice(local_devices.mouse, FALSE);
17635c4bbdfSmrg    assert(ret == Success);
17735c4bbdfSmrg    ret = ActivateDevice(local_devices.kbd, FALSE);
17835c4bbdfSmrg    assert(ret == Success);
17935c4bbdfSmrg    EnableDevice(local_devices.mouse, FALSE);
18035c4bbdfSmrg    EnableDevice(local_devices.kbd, FALSE);
18135c4bbdfSmrg
18235c4bbdfSmrg    local_devices.num_devices = 4;
18335c4bbdfSmrg    local_devices.num_master_devices = 2;
18435c4bbdfSmrg
18535c4bbdfSmrg    fake_init_sprite(local_devices.mouse);
18635c4bbdfSmrg    fake_init_sprite(local_devices.vcp);
18735c4bbdfSmrg
18835c4bbdfSmrg    return local_devices;
18935c4bbdfSmrg}
1906747b715Smrg
1916747b715Smrg/* Create minimal client, with the given buffer and len as request buffer */
19235c4bbdfSmrgClientRec
19335c4bbdfSmrginit_client(int len, void *data)
1946747b715Smrg{
1956747b715Smrg    ClientRec client = { 0 };
1966747b715Smrg
1976747b715Smrg    /* we store the privates now and reassign it after the memset. this way
1986747b715Smrg     * we can share them across multiple test runs and don't have to worry
1996747b715Smrg     * about freeing them after each test run. */
2006747b715Smrg
2016747b715Smrg    client.index = CLIENT_INDEX;
2026747b715Smrg    client.clientAsMask = CLIENT_MASK;
2036747b715Smrg    client.sequence = CLIENT_SEQUENCE;
2046747b715Smrg    client.req_len = len;
2056747b715Smrg
2066747b715Smrg    client.requestBuffer = data;
2076747b715Smrg    dixAllocatePrivates(&client.devPrivates, PRIVATE_CLIENT);
2086747b715Smrg    return client;
2096747b715Smrg}
2106747b715Smrg
21135c4bbdfSmrgvoid
21235c4bbdfSmrginit_window(WindowPtr local_window, WindowPtr parent, int id)
2136747b715Smrg{
21435c4bbdfSmrg    memset(local_window, 0, sizeof(*local_window));
21535c4bbdfSmrg
21635c4bbdfSmrg    local_window->drawable.id = id;
21735c4bbdfSmrg    if (parent) {
21835c4bbdfSmrg        local_window->drawable.x = 30;
21935c4bbdfSmrg        local_window->drawable.y = 50;
22035c4bbdfSmrg        local_window->drawable.width = 100;
22135c4bbdfSmrg        local_window->drawable.height = 200;
2226747b715Smrg    }
22335c4bbdfSmrg    local_window->parent = parent;
22435c4bbdfSmrg    local_window->optional = calloc(1, sizeof(WindowOptRec));
22535c4bbdfSmrg    assert(local_window->optional);
2266747b715Smrg}
2276747b715Smrg
2286747b715Smrgextern DevPrivateKeyRec miPointerScreenKeyRec;
2296747b715Smrgextern DevPrivateKeyRec miPointerPrivKeyRec;
2306747b715Smrg
2316747b715Smrg/* Needed for the screen setup, otherwise we crash during sprite initialization */
23235c4bbdfSmrgstatic Bool
23335c4bbdfSmrgdevice_cursor_init(DeviceIntPtr dev, ScreenPtr local_screen)
23435c4bbdfSmrg{
23535c4bbdfSmrg    return TRUE;
23635c4bbdfSmrg}
23735c4bbdfSmrg
23835c4bbdfSmrgstatic void
23935c4bbdfSmrgdevice_cursor_cleanup(DeviceIntPtr dev, ScreenPtr local_screen)
24035c4bbdfSmrg{
24135c4bbdfSmrg}
24235c4bbdfSmrg
24335c4bbdfSmrgstatic Bool
24435c4bbdfSmrgset_cursor_pos(DeviceIntPtr dev, ScreenPtr local_screen, int x, int y, Bool event)
24535c4bbdfSmrg{
24635c4bbdfSmrg    return TRUE;
24735c4bbdfSmrg}
24835c4bbdfSmrg
24935c4bbdfSmrgvoid
25035c4bbdfSmrginit_simple(void)
2516747b715Smrg{
2526747b715Smrg    screenInfo.numScreens = 1;
2536747b715Smrg    screenInfo.screens[0] = &screen;
2546747b715Smrg
2556747b715Smrg    screen.myNum = 0;
2566747b715Smrg    screen.id = 100;
2576747b715Smrg    screen.width = 640;
2586747b715Smrg    screen.height = 480;
2596747b715Smrg    screen.DeviceCursorInitialize = device_cursor_init;
26035c4bbdfSmrg    screen.DeviceCursorCleanup = device_cursor_cleanup;
2616747b715Smrg    screen.SetCursorPosition = set_cursor_pos;
2626747b715Smrg
2636747b715Smrg    dixResetPrivates();
2646747b715Smrg    InitAtoms();
2656747b715Smrg    XkbInitPrivates();
26635c4bbdfSmrg    dixRegisterPrivateKey(&XIClientPrivateKeyRec, PRIVATE_CLIENT,
26735c4bbdfSmrg                          sizeof(XIClientRec));
2686747b715Smrg    dixRegisterPrivateKey(&miPointerScreenKeyRec, PRIVATE_SCREEN, 0);
2696747b715Smrg    dixRegisterPrivateKey(&miPointerPrivKeyRec, PRIVATE_DEVICE, 0);
2706747b715Smrg    XInputExtensionInit();
2716747b715Smrg
2726747b715Smrg    init_window(&root, NULL, ROOT_WINDOW_ID);
2736747b715Smrg    init_window(&window, &root, CLIENT_WINDOW_ID);
2746747b715Smrg
27535c4bbdfSmrg    serverClient = &server_client;
27635c4bbdfSmrg    InitClient(serverClient, 0, (void *) NULL);
27735c4bbdfSmrg    if (!InitClientResources(serverClient)) /* for root resources */
27835c4bbdfSmrg        FatalError("couldn't init server resources");
27935c4bbdfSmrg    SyncExtensionInit();
28035c4bbdfSmrg
2816747b715Smrg    devices = init_devices();
2826747b715Smrg}
2836747b715Smrg
28435c4bbdfSmrgvoid
28535c4bbdfSmrg__wrap_WriteToClient(ClientPtr client, int len, void *data)
2866747b715Smrg{
28735c4bbdfSmrg    assert(reply_handler != NULL);
2886747b715Smrg
28935c4bbdfSmrg    (*reply_handler) (client, len, data, global_userdata);
2906747b715Smrg}
2911b5d61b8Smrg
2921b5d61b8Smrg/* dixLookupWindow requires a lot of setup not necessary for this test.
2931b5d61b8Smrg * Simple wrapper that returns either one of the fake root window or the
2941b5d61b8Smrg * fake client window. If the requested ID is neither of those wanted,
2951b5d61b8Smrg * return whatever the real dixLookupWindow does.
2961b5d61b8Smrg */
2971b5d61b8Smrgint
2981b5d61b8Smrg__wrap_dixLookupWindow(WindowPtr *win, XID id, ClientPtr client, Mask access)
2991b5d61b8Smrg{
3001b5d61b8Smrg    if (id == root.drawable.id) {
3011b5d61b8Smrg        *win = &root;
3021b5d61b8Smrg        return Success;
3031b5d61b8Smrg    }
3041b5d61b8Smrg    else if (id == window.drawable.id) {
3051b5d61b8Smrg        *win = &window;
3061b5d61b8Smrg        return Success;
3071b5d61b8Smrg    }
3081b5d61b8Smrg
3091b5d61b8Smrg    return __real_dixLookupWindow(win, id, client, access);
3101b5d61b8Smrg}
3111b5d61b8Smrg
3121b5d61b8Smrgextern ClientRec client_window;
3131b5d61b8Smrg
3141b5d61b8Smrgint
3151b5d61b8Smrg__wrap_dixLookupClient(ClientPtr *pClient, XID rid, ClientPtr client,
3161b5d61b8Smrg                       Mask access)
3171b5d61b8Smrg{
3181b5d61b8Smrg    if (rid == ROOT_WINDOW_ID)
3191b5d61b8Smrg        return BadWindow;
3201b5d61b8Smrg
3211b5d61b8Smrg    if (rid == CLIENT_WINDOW_ID) {
3221b5d61b8Smrg        *pClient = &client_window;
3231b5d61b8Smrg        return Success;
3241b5d61b8Smrg    }
3251b5d61b8Smrg
3261b5d61b8Smrg    return __real_dixLookupClient(pClient, rid, client, access);
3271b5d61b8Smrg}
328