protocol-common.c revision 35c4bbdf
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 376747b715Smrg#include "protocol-common.h" 386747b715Smrg 396747b715Smrgstruct devices devices; 406747b715SmrgScreenRec screen; 416747b715SmrgWindowRec root; 426747b715SmrgWindowRec window; 4335c4bbdfSmrgstatic ClientRec server_client; 446747b715Smrg 4535c4bbdfSmrgvoid *global_userdata; 466747b715Smrg 4735c4bbdfSmrgstatic void 4835c4bbdfSmrgfake_init_sprite(DeviceIntPtr dev) 496747b715Smrg{ 506747b715Smrg SpritePtr sprite; 5135c4bbdfSmrg 526747b715Smrg sprite = dev->spriteInfo->sprite; 536747b715Smrg 546747b715Smrg sprite->spriteTraceSize = 10; 556747b715Smrg sprite->spriteTrace = calloc(sprite->spriteTraceSize, sizeof(WindowPtr)); 566747b715Smrg sprite->spriteTraceGood = 1; 576747b715Smrg sprite->spriteTrace[0] = &root; 586747b715Smrg sprite->hot.x = SPRITE_X; 596747b715Smrg sprite->hot.y = SPRITE_Y; 606747b715Smrg sprite->hotPhys.x = sprite->hot.x; 616747b715Smrg sprite->hotPhys.y = sprite->hot.y; 626747b715Smrg sprite->win = &window; 636747b715Smrg sprite->hotPhys.pScreen = &screen; 646747b715Smrg sprite->physLimits.x1 = 0; 656747b715Smrg sprite->physLimits.y1 = 0; 666747b715Smrg sprite->physLimits.x2 = screen.width; 676747b715Smrg sprite->physLimits.y2 = screen.height; 686747b715Smrg} 696747b715Smrg 7035c4bbdfSmrg/* This is essentially CorePointerProc with ScrollAxes added */ 7135c4bbdfSmrgstatic int 7235c4bbdfSmrgTestPointerProc(DeviceIntPtr pDev, int what) 7335c4bbdfSmrg{ 7435c4bbdfSmrg#define NBUTTONS 10 7535c4bbdfSmrg#define NAXES 4 7635c4bbdfSmrg BYTE map[NBUTTONS + 1]; 7735c4bbdfSmrg int i = 0; 7835c4bbdfSmrg Atom btn_labels[NBUTTONS] = { 0 }; 7935c4bbdfSmrg Atom axes_labels[NAXES] = { 0 }; 8035c4bbdfSmrg 8135c4bbdfSmrg switch (what) { 8235c4bbdfSmrg case DEVICE_INIT: 8335c4bbdfSmrg for (i = 1; i <= NBUTTONS; i++) 8435c4bbdfSmrg map[i] = i; 8535c4bbdfSmrg 8635c4bbdfSmrg btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT); 8735c4bbdfSmrg btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE); 8835c4bbdfSmrg btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT); 8935c4bbdfSmrg btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP); 9035c4bbdfSmrg btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN); 9135c4bbdfSmrg btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT); 9235c4bbdfSmrg btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT); 9335c4bbdfSmrg /* don't know about the rest */ 9435c4bbdfSmrg 9535c4bbdfSmrg axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X); 9635c4bbdfSmrg axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y); 9735c4bbdfSmrg axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_VSCROLL); 9835c4bbdfSmrg axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HSCROLL); 9935c4bbdfSmrg 10035c4bbdfSmrg if (!InitPointerDeviceStruct 10135c4bbdfSmrg ((DevicePtr) pDev, map, NBUTTONS, btn_labels, 10235c4bbdfSmrg (PtrCtrlProcPtr) NoopDDA, GetMotionHistorySize(), NAXES, 10335c4bbdfSmrg axes_labels)) { 10435c4bbdfSmrg ErrorF("Could not initialize device '%s'. Out of memory.\n", 10535c4bbdfSmrg pDev->name); 10635c4bbdfSmrg return BadAlloc; 10735c4bbdfSmrg } 10835c4bbdfSmrg pDev->valuator->axisVal[0] = screenInfo.screens[0]->width / 2; 10935c4bbdfSmrg pDev->last.valuators[0] = pDev->valuator->axisVal[0]; 11035c4bbdfSmrg pDev->valuator->axisVal[1] = screenInfo.screens[0]->height / 2; 11135c4bbdfSmrg pDev->last.valuators[1] = pDev->valuator->axisVal[1]; 11235c4bbdfSmrg 11335c4bbdfSmrg /* protocol-xiquerydevice.c relies on these increment */ 11435c4bbdfSmrg SetScrollValuator(pDev, 2, SCROLL_TYPE_VERTICAL, 2.4, SCROLL_FLAG_NONE); 11535c4bbdfSmrg SetScrollValuator(pDev, 3, SCROLL_TYPE_HORIZONTAL, 3.5, 11635c4bbdfSmrg SCROLL_FLAG_PREFERRED); 11735c4bbdfSmrg break; 11835c4bbdfSmrg 11935c4bbdfSmrg case DEVICE_CLOSE: 12035c4bbdfSmrg break; 12135c4bbdfSmrg 12235c4bbdfSmrg default: 12335c4bbdfSmrg break; 12435c4bbdfSmrg } 12535c4bbdfSmrg 12635c4bbdfSmrg return Success; 12735c4bbdfSmrg 12835c4bbdfSmrg#undef NBUTTONS 12935c4bbdfSmrg#undef NAXES 13035c4bbdfSmrg} 13135c4bbdfSmrg 1326747b715Smrg/** 1336747b715Smrg * Create and init 2 master devices (VCP + VCK) and two slave devices, one 1346747b715Smrg * default mouse, one default keyboard. 1356747b715Smrg */ 13635c4bbdfSmrgstruct devices 13735c4bbdfSmrginit_devices(void) 1386747b715Smrg{ 1396747b715Smrg ClientRec client; 14035c4bbdfSmrg struct devices local_devices; 14135c4bbdfSmrg int ret; 14235c4bbdfSmrg 14335c4bbdfSmrg /* 14435c4bbdfSmrg * Put a unique name in display pointer so that when tests are run in 14535c4bbdfSmrg * parallel, their xkbcomp outputs to /tmp/server-<display>.xkm don't 14635c4bbdfSmrg * stomp on each other. 14735c4bbdfSmrg */ 14835c4bbdfSmrg#ifdef HAVE_GETPROGNAME 14935c4bbdfSmrg display = getprogname(); 15035c4bbdfSmrg#elif HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME 15135c4bbdfSmrg display = program_invocation_short_name; 15235c4bbdfSmrg#endif 1536747b715Smrg 1546747b715Smrg client = init_client(0, NULL); 1556747b715Smrg 15635c4bbdfSmrg AllocDevicePair(&client, "Virtual core", &local_devices.vcp, &local_devices.vck, 1576747b715Smrg CorePointerProc, CoreKeyboardProc, TRUE); 15835c4bbdfSmrg inputInfo.pointer = local_devices.vcp; 1596747b715Smrg 16035c4bbdfSmrg inputInfo.keyboard = local_devices.vck; 16135c4bbdfSmrg ret = ActivateDevice(local_devices.vcp, FALSE); 16235c4bbdfSmrg assert(ret == Success); 16335c4bbdfSmrg /* This may fail if xkbcomp fails or xkb-config is not found. */ 16435c4bbdfSmrg ret = ActivateDevice(local_devices.vck, FALSE); 16535c4bbdfSmrg assert(ret == Success); 16635c4bbdfSmrg EnableDevice(local_devices.vcp, FALSE); 16735c4bbdfSmrg EnableDevice(local_devices.vck, FALSE); 16835c4bbdfSmrg 16935c4bbdfSmrg AllocDevicePair(&client, "", &local_devices.mouse, &local_devices.kbd, 17035c4bbdfSmrg TestPointerProc, CoreKeyboardProc, FALSE); 17135c4bbdfSmrg ret = ActivateDevice(local_devices.mouse, FALSE); 17235c4bbdfSmrg assert(ret == Success); 17335c4bbdfSmrg ret = ActivateDevice(local_devices.kbd, FALSE); 17435c4bbdfSmrg assert(ret == Success); 17535c4bbdfSmrg EnableDevice(local_devices.mouse, FALSE); 17635c4bbdfSmrg EnableDevice(local_devices.kbd, FALSE); 17735c4bbdfSmrg 17835c4bbdfSmrg local_devices.num_devices = 4; 17935c4bbdfSmrg local_devices.num_master_devices = 2; 18035c4bbdfSmrg 18135c4bbdfSmrg fake_init_sprite(local_devices.mouse); 18235c4bbdfSmrg fake_init_sprite(local_devices.vcp); 18335c4bbdfSmrg 18435c4bbdfSmrg return local_devices; 18535c4bbdfSmrg} 1866747b715Smrg 1876747b715Smrg/* Create minimal client, with the given buffer and len as request buffer */ 18835c4bbdfSmrgClientRec 18935c4bbdfSmrginit_client(int len, void *data) 1906747b715Smrg{ 1916747b715Smrg ClientRec client = { 0 }; 1926747b715Smrg 1936747b715Smrg /* we store the privates now and reassign it after the memset. this way 1946747b715Smrg * we can share them across multiple test runs and don't have to worry 1956747b715Smrg * about freeing them after each test run. */ 1966747b715Smrg 1976747b715Smrg client.index = CLIENT_INDEX; 1986747b715Smrg client.clientAsMask = CLIENT_MASK; 1996747b715Smrg client.sequence = CLIENT_SEQUENCE; 2006747b715Smrg client.req_len = len; 2016747b715Smrg 2026747b715Smrg client.requestBuffer = data; 2036747b715Smrg dixAllocatePrivates(&client.devPrivates, PRIVATE_CLIENT); 2046747b715Smrg return client; 2056747b715Smrg} 2066747b715Smrg 20735c4bbdfSmrgvoid 20835c4bbdfSmrginit_window(WindowPtr local_window, WindowPtr parent, int id) 2096747b715Smrg{ 21035c4bbdfSmrg memset(local_window, 0, sizeof(*local_window)); 21135c4bbdfSmrg 21235c4bbdfSmrg local_window->drawable.id = id; 21335c4bbdfSmrg if (parent) { 21435c4bbdfSmrg local_window->drawable.x = 30; 21535c4bbdfSmrg local_window->drawable.y = 50; 21635c4bbdfSmrg local_window->drawable.width = 100; 21735c4bbdfSmrg local_window->drawable.height = 200; 2186747b715Smrg } 21935c4bbdfSmrg local_window->parent = parent; 22035c4bbdfSmrg local_window->optional = calloc(1, sizeof(WindowOptRec)); 22135c4bbdfSmrg assert(local_window->optional); 2226747b715Smrg} 2236747b715Smrg 2246747b715Smrgextern DevPrivateKeyRec miPointerScreenKeyRec; 2256747b715Smrgextern DevPrivateKeyRec miPointerPrivKeyRec; 2266747b715Smrg 2276747b715Smrg/* Needed for the screen setup, otherwise we crash during sprite initialization */ 22835c4bbdfSmrgstatic Bool 22935c4bbdfSmrgdevice_cursor_init(DeviceIntPtr dev, ScreenPtr local_screen) 23035c4bbdfSmrg{ 23135c4bbdfSmrg return TRUE; 23235c4bbdfSmrg} 23335c4bbdfSmrg 23435c4bbdfSmrgstatic void 23535c4bbdfSmrgdevice_cursor_cleanup(DeviceIntPtr dev, ScreenPtr local_screen) 23635c4bbdfSmrg{ 23735c4bbdfSmrg} 23835c4bbdfSmrg 23935c4bbdfSmrgstatic Bool 24035c4bbdfSmrgset_cursor_pos(DeviceIntPtr dev, ScreenPtr local_screen, int x, int y, Bool event) 24135c4bbdfSmrg{ 24235c4bbdfSmrg return TRUE; 24335c4bbdfSmrg} 24435c4bbdfSmrg 24535c4bbdfSmrgvoid 24635c4bbdfSmrginit_simple(void) 2476747b715Smrg{ 2486747b715Smrg screenInfo.numScreens = 1; 2496747b715Smrg screenInfo.screens[0] = &screen; 2506747b715Smrg 2516747b715Smrg screen.myNum = 0; 2526747b715Smrg screen.id = 100; 2536747b715Smrg screen.width = 640; 2546747b715Smrg screen.height = 480; 2556747b715Smrg screen.DeviceCursorInitialize = device_cursor_init; 25635c4bbdfSmrg screen.DeviceCursorCleanup = device_cursor_cleanup; 2576747b715Smrg screen.SetCursorPosition = set_cursor_pos; 2586747b715Smrg 2596747b715Smrg dixResetPrivates(); 2606747b715Smrg InitAtoms(); 2616747b715Smrg XkbInitPrivates(); 26235c4bbdfSmrg dixRegisterPrivateKey(&XIClientPrivateKeyRec, PRIVATE_CLIENT, 26335c4bbdfSmrg sizeof(XIClientRec)); 2646747b715Smrg dixRegisterPrivateKey(&miPointerScreenKeyRec, PRIVATE_SCREEN, 0); 2656747b715Smrg dixRegisterPrivateKey(&miPointerPrivKeyRec, PRIVATE_DEVICE, 0); 2666747b715Smrg XInputExtensionInit(); 2676747b715Smrg 2686747b715Smrg init_window(&root, NULL, ROOT_WINDOW_ID); 2696747b715Smrg init_window(&window, &root, CLIENT_WINDOW_ID); 2706747b715Smrg 27135c4bbdfSmrg serverClient = &server_client; 27235c4bbdfSmrg InitClient(serverClient, 0, (void *) NULL); 27335c4bbdfSmrg if (!InitClientResources(serverClient)) /* for root resources */ 27435c4bbdfSmrg FatalError("couldn't init server resources"); 27535c4bbdfSmrg SyncExtensionInit(); 27635c4bbdfSmrg 2776747b715Smrg devices = init_devices(); 2786747b715Smrg} 2796747b715Smrg 28035c4bbdfSmrgvoid 28135c4bbdfSmrg__wrap_WriteToClient(ClientPtr client, int len, void *data) 2826747b715Smrg{ 28335c4bbdfSmrg assert(reply_handler != NULL); 2846747b715Smrg 28535c4bbdfSmrg (*reply_handler) (client, len, data, global_userdata); 2866747b715Smrg} 287