| 1 |
/** |
| 2 |
* Copyright © 2009 Red Hat, Inc. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* Test relies on assert() */ |
| 25 |
#undef NDEBUG |
| 26 |
|
| 27 |
#ifdef HAVE_DIX_CONFIG_H |
| 28 |
#include <dix-config.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <errno.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include "extinit.h" /* for XInputExtensionInit */ |
| 34 |
#include "exglobals.h" |
| 35 |
#include "xkbsrv.h" /* for XkbInitPrivates */ |
| 36 |
#include "xserver-properties.h" |
| 37 |
#include "syncsrv.h" |
| 38 |
#include <X11/extensions/XI2.h> |
| 39 |
|
| 40 |
#include "protocol-common.h" |
| 41 |
|
| 42 |
struct devices devices; |
| 43 |
ScreenRec screen; |
| 44 |
WindowRec root; |
| 45 |
WindowRec window; |
| 46 |
static ClientRec server_client; |
| 47 |
|
| 48 |
static void |
| 49 |
fake_init_sprite(DeviceIntPtr dev) |
| 50 |
{ |
| 51 |
SpritePtr sprite; |
| 52 |
|
| 53 |
sprite = dev->spriteInfo->sprite; |
| 54 |
|
| 55 |
sprite->spriteTraceSize = 10; |
| 56 |
sprite->spriteTrace = calloc(sprite->spriteTraceSize, sizeof(WindowPtr)); |
| 57 |
sprite->spriteTraceGood = 1; |
| 58 |
sprite->spriteTrace[0] = &root; |
| 59 |
sprite->hot.x = SPRITE_X; |
| 60 |
sprite->hot.y = SPRITE_Y; |
| 61 |
sprite->hotPhys.x = sprite->hot.x; |
| 62 |
sprite->hotPhys.y = sprite->hot.y; |
| 63 |
sprite->win = &window; |
| 64 |
sprite->hotPhys.pScreen = &screen; |
| 65 |
sprite->physLimits.x1 = 0; |
| 66 |
sprite->physLimits.y1 = 0; |
| 67 |
sprite->physLimits.x2 = screen.width; |
| 68 |
sprite->physLimits.y2 = screen.height; |
| 69 |
} |
| 70 |
|
| 71 |
/* This is essentially CorePointerProc with ScrollAxes added */ |
| 72 |
static int |
| 73 |
TestPointerProc(DeviceIntPtr pDev, int what) |
| 74 |
{ |
| 75 |
#define NBUTTONS 10 |
| 76 |
#define NAXES 4 |
| 77 |
BYTE map[NBUTTONS + 1]; |
| 78 |
int i = 0; |
| 79 |
Atom btn_labels[NBUTTONS] = { 0 }; |
| 80 |
Atom axes_labels[NAXES] = { 0 }; |
| 81 |
|
| 82 |
switch (what) { |
| 83 |
case DEVICE_INIT: |
| 84 |
for (i = 1; i <= NBUTTONS; i++) |
| 85 |
map[i] = i; |
| 86 |
|
| 87 |
btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT); |
| 88 |
btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE); |
| 89 |
btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT); |
| 90 |
btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP); |
| 91 |
btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN); |
| 92 |
btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT); |
| 93 |
btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT); |
| 94 |
/* don't know about the rest */ |
| 95 |
|
| 96 |
axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X); |
| 97 |
axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y); |
| 98 |
axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_VSCROLL); |
| 99 |
axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HSCROLL); |
| 100 |
|
| 101 |
if (!InitPointerDeviceStruct |
| 102 |
((DevicePtr) pDev, map, NBUTTONS, btn_labels, |
| 103 |
(PtrCtrlProcPtr) NoopDDA, GetMotionHistorySize(), NAXES, |
| 104 |
axes_labels)) { |
| 105 |
ErrorF("Could not initialize device '%s'. Out of memory.\n", |
| 106 |
pDev->name); |
| 107 |
return BadAlloc; |
| 108 |
} |
| 109 |
pDev->valuator->axisVal[0] = screenInfo.screens[0]->width / 2; |
| 110 |
pDev->last.valuators[0] = pDev->valuator->axisVal[0]; |
| 111 |
pDev->valuator->axisVal[1] = screenInfo.screens[0]->height / 2; |
| 112 |
pDev->last.valuators[1] = pDev->valuator->axisVal[1]; |
| 113 |
|
| 114 |
/* protocol-xiquerydevice.c relies on these increment */ |
| 115 |
SetScrollValuator(pDev, 2, SCROLL_TYPE_VERTICAL, 2.4, SCROLL_FLAG_NONE); |
| 116 |
SetScrollValuator(pDev, 3, SCROLL_TYPE_HORIZONTAL, 3.5, |
| 117 |
SCROLL_FLAG_PREFERRED); |
| 118 |
break; |
| 119 |
|
| 120 |
case DEVICE_CLOSE: |
| 121 |
break; |
| 122 |
|
| 123 |
default: |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
return Success; |
| 128 |
|
| 129 |
#undef NBUTTONS |
| 130 |
#undef NAXES |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Create and init 2 master devices (VCP + VCK) and two slave devices, one |
| 135 |
* default mouse, one default keyboard. |
| 136 |
*/ |
| 137 |
struct devices |
| 138 |
init_devices(void) |
| 139 |
{ |
| 140 |
ClientRec client; |
| 141 |
struct devices local_devices; |
| 142 |
int ret; |
| 143 |
|
| 144 |
/* |
| 145 |
* Put a unique name in display pointer so that when tests are run in |
| 146 |
* parallel, their xkbcomp outputs to /tmp/server-<display>.xkm don't |
| 147 |
* stomp on each other. |
| 148 |
*/ |
| 149 |
#ifdef HAVE_GETPROGNAME |
| 150 |
display = getprogname(); |
| 151 |
#elif HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME |
| 152 |
display = program_invocation_short_name; |
| 153 |
#endif |
| 154 |
|
| 155 |
client = init_client(0, NULL); |
| 156 |
|
| 157 |
AllocDevicePair(&client, "Virtual core", &local_devices.vcp, &local_devices.vck, |
| 158 |
CorePointerProc, CoreKeyboardProc, TRUE); |
| 159 |
inputInfo.pointer = local_devices.vcp; |
| 160 |
|
| 161 |
inputInfo.keyboard = local_devices.vck; |
| 162 |
ret = ActivateDevice(local_devices.vcp, FALSE); |
| 163 |
assert(ret == Success); |
| 164 |
/* This may fail if xkbcomp fails or xkb-config is not found. */ |
| 165 |
ret = ActivateDevice(local_devices.vck, FALSE); |
| 166 |
assert(ret == Success); |
| 167 |
EnableDevice(local_devices.vcp, FALSE); |
| 168 |
EnableDevice(local_devices.vck, FALSE); |
| 169 |
|
| 170 |
AllocDevicePair(&client, "", &local_devices.mouse, &local_devices.kbd, |
| 171 |
TestPointerProc, CoreKeyboardProc, FALSE); |
| 172 |
ret = ActivateDevice(local_devices.mouse, FALSE); |
| 173 |
assert(ret == Success); |
| 174 |
ret = ActivateDevice(local_devices.kbd, FALSE); |
| 175 |
assert(ret == Success); |
| 176 |
EnableDevice(local_devices.mouse, FALSE); |
| 177 |
EnableDevice(local_devices.kbd, FALSE); |
| 178 |
|
| 179 |
local_devices.num_devices = 4; |
| 180 |
local_devices.num_master_devices = 2; |
| 181 |
|
| 182 |
fake_init_sprite(local_devices.mouse); |
| 183 |
fake_init_sprite(local_devices.vcp); |
| 184 |
|
| 185 |
return local_devices; |
| 186 |
} |
| 187 |
|
| 188 |
/* Create minimal client, with the given buffer and len as request buffer */ |
| 189 |
ClientRec |
| 190 |
init_client(int len, void *data) |
| 191 |
{ |
| 192 |
ClientRec client = { 0 }; |
| 193 |
|
| 194 |
/* we store the privates now and reassign it after the memset. this way |
| 195 |
* we can share them across multiple test runs and don't have to worry |
| 196 |
* about freeing them after each test run. */ |
| 197 |
|
| 198 |
client.index = CLIENT_INDEX; |
| 199 |
client.clientAsMask = CLIENT_MASK; |
| 200 |
client.sequence = CLIENT_SEQUENCE; |
| 201 |
client.req_len = len; |
| 202 |
|
| 203 |
client.requestBuffer = data; |
| 204 |
dixAllocatePrivates(&client.devPrivates, PRIVATE_CLIENT); |
| 205 |
return client; |
| 206 |
} |
| 207 |
|
| 208 |
void |
| 209 |
init_window(WindowPtr local_window, WindowPtr parent, int id) |
| 210 |
{ |
| 211 |
memset(local_window, 0, sizeof(*local_window)); |
| 212 |
|
| 213 |
local_window->drawable.id = id; |
| 214 |
if (parent) { |
| 215 |
local_window->drawable.x = 30; |
| 216 |
local_window->drawable.y = 50; |
| 217 |
local_window->drawable.width = 100; |
| 218 |
local_window->drawable.height = 200; |
| 219 |
} |
| 220 |
local_window->parent = parent; |
| 221 |
local_window->optional = calloc(1, sizeof(WindowOptRec)); |
| 222 |
assert(local_window->optional); |
| 223 |
} |
| 224 |
|
| 225 |
extern DevPrivateKeyRec miPointerScreenKeyRec; |
| 226 |
extern DevPrivateKeyRec miPointerPrivKeyRec; |
| 227 |
|
| 228 |
/* Needed for the screen setup, otherwise we crash during sprite initialization */ |
| 229 |
static Bool |
| 230 |
device_cursor_init(DeviceIntPtr dev, ScreenPtr local_screen) |
| 231 |
{ |
| 232 |
return TRUE; |
| 233 |
} |
| 234 |
|
| 235 |
static void |
| 236 |
device_cursor_cleanup(DeviceIntPtr dev, ScreenPtr local_screen) |
| 237 |
{ |
| 238 |
} |
| 239 |
|
| 240 |
static Bool |
| 241 |
set_cursor_pos(DeviceIntPtr dev, ScreenPtr local_screen, int x, int y, Bool event) |
| 242 |
{ |
| 243 |
return TRUE; |
| 244 |
} |
| 245 |
|
| 246 |
void |
| 247 |
init_simple(void) |
| 248 |
{ |
| 249 |
screenInfo.numScreens = 1; |
| 250 |
screenInfo.screens[0] = &screen; |
| 251 |
|
| 252 |
screen.myNum = 0; |
| 253 |
screen.id = 100; |
| 254 |
screen.width = 640; |
| 255 |
screen.height = 480; |
| 256 |
screen.DeviceCursorInitialize = device_cursor_init; |
| 257 |
screen.DeviceCursorCleanup = device_cursor_cleanup; |
| 258 |
screen.SetCursorPosition = set_cursor_pos; |
| 259 |
screen.root = &root; |
| 260 |
|
| 261 |
dixResetPrivates(); |
| 262 |
InitAtoms(); |
| 263 |
XkbInitPrivates(); |
| 264 |
dixRegisterPrivateKey(&XIClientPrivateKeyRec, PRIVATE_CLIENT, |
| 265 |
sizeof(XIClientRec)); |
| 266 |
dixRegisterPrivateKey(&miPointerScreenKeyRec, PRIVATE_SCREEN, 0); |
| 267 |
dixRegisterPrivateKey(&miPointerPrivKeyRec, PRIVATE_DEVICE, 0); |
| 268 |
XInputExtensionInit(); |
| 269 |
|
| 270 |
init_window(&root, NULL, ROOT_WINDOW_ID); |
| 271 |
init_window(&window, &root, CLIENT_WINDOW_ID); |
| 272 |
|
| 273 |
serverClient = &server_client; |
| 274 |
InitClient(serverClient, 0, (void *) NULL); |
| 275 |
if (!InitClientResources(serverClient)) /* for root resources */ |
| 276 |
FatalError("couldn't init server resources"); |
| 277 |
SyncExtensionInit(); |
| 278 |
|
| 279 |
devices = init_devices(); |
| 280 |
} |
| 281 |
|
| 282 |
WRAP_FUNCTION(WriteToClient, void, ClientPtr client, int len, void *data) |
| 283 |
{ |
| 284 |
IMPLEMENT_WRAP_FUNCTION(WriteToClient, client, len, data); |
| 285 |
} |
| 286 |
|
| 287 |
WRAP_FUNCTION(XISetEventMask, int, |
| 288 |
DeviceIntPtr dev, WindowPtr win, ClientPtr client, |
| 289 |
int len, unsigned char *mask) |
| 290 |
{ |
| 291 |
IMPLEMENT_WRAP_FUNCTION_WITH_RETURN(XISetEventMask, dev, win, client, len, mask); |
| 292 |
} |
| 293 |
|
| 294 |
WRAP_FUNCTION(AddResource, Bool, XID id, RESTYPE type, void *value) |
| 295 |
{ |
| 296 |
IMPLEMENT_WRAP_FUNCTION_WITH_RETURN(AddResource, id, type, value); |
| 297 |
} |
| 298 |
|
| 299 |
WRAP_FUNCTION(GrabButton, int, |
| 300 |
ClientPtr client, DeviceIntPtr dev, |
| 301 |
DeviceIntPtr modifier_device, int button, |
| 302 |
GrabParameters *param, enum InputLevel grabtype, |
| 303 |
GrabMask *mask) |
| 304 |
{ |
| 305 |
IMPLEMENT_WRAP_FUNCTION_WITH_RETURN(GrabButton, client, dev, modifier_device, |
| 306 |
button, param, grabtype, mask); |
| 307 |
} |
| 308 |
|
| 309 |
/* dixLookupWindow requires a lot of setup not necessary for this test. |
| 310 |
* Simple wrapper that returns either one of the fake root window or the |
| 311 |
* fake client window. If the requested ID is neither of those wanted, |
| 312 |
* return whatever the real dixLookupWindow does. |
| 313 |
*/ |
| 314 |
WRAP_FUNCTION(dixLookupWindow, int, |
| 315 |
WindowPtr *win, XID id, ClientPtr client, Mask access) |
| 316 |
{ |
| 317 |
if (id == root.drawable.id) { |
| 318 |
*win = &root; |
| 319 |
return Success; |
| 320 |
} |
| 321 |
else if (id == window.drawable.id) { |
| 322 |
*win = &window; |
| 323 |
return Success; |
| 324 |
} |
| 325 |
|
| 326 |
return __real_dixLookupWindow(win, id, client, access); |
| 327 |
} |
| 328 |
|
| 329 |
extern ClientRec client_window; |
| 330 |
|
| 331 |
WRAP_FUNCTION(dixLookupClient, int, |
| 332 |
ClientPtr *pClient, XID rid, ClientPtr client, Mask access) |
| 333 |
{ |
| 334 |
if (rid == ROOT_WINDOW_ID) |
| 335 |
return BadWindow; |
| 336 |
|
| 337 |
if (rid == CLIENT_WINDOW_ID) { |
| 338 |
*pClient = &client_window; |
| 339 |
return Success; |
| 340 |
} |
| 341 |
|
| 342 |
return __real_dixLookupClient(pClient, rid, client, access); |
| 343 |
} |