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#include <stdint.h>
31#include <X11/Xatom.h>
32#include "input.h"
33#include "inputstr.h"
34#include "scrnintstr.h"
35#include "windowstr.h"
36#include "exevents.h"
37#include "extinit.h"
38#include "xkbsrv.h"
39#include "xserver-properties.h"
40#include "syncsrv.h"
41
42#include "tests-common.h"
43
44/**
45 */
46
47/* from Xext/xtest.c */
48extern DeviceIntPtr xtestpointer, xtestkeyboard;
49
50/* Needed for the screen setup, otherwise we crash during sprite initialization */
51static Bool
52device_cursor_init(DeviceIntPtr dev, ScreenPtr screen)
53{
54    return TRUE;
55}
56
57static void
58device_cursor_cleanup(DeviceIntPtr dev, ScreenPtr screen)
59{
60}
61
62static void
63xtest_init_devices(void)
64{
65    ScreenRec screen = {0};
66    ClientRec server_client = {0};
67    WindowRec root = {{0}};
68    WindowOptRec optional = {0};
69
70    /* random stuff that needs initialization */
71    root.drawable.id = 0xab;
72    root.optional = &optional;
73    screen.root = &root;
74    screenInfo.numScreens = 1;
75    screenInfo.screens[0] = &screen;
76    screen.myNum = 0;
77    screen.id = 100;
78    screen.width = 640;
79    screen.height = 480;
80    screen.DeviceCursorInitialize = device_cursor_init;
81    screen.DeviceCursorCleanup = device_cursor_cleanup;
82    dixResetPrivates();
83    serverClient = &server_client;
84    InitClient(serverClient, 0, (void *) NULL);
85    if (!InitClientResources(serverClient)) /* for root resources */
86        FatalError("couldn't init server resources");
87    InitAtoms();
88    SyncExtensionInit();
89
90    /* this also inits the xtest devices */
91    InitCoreDevices();
92
93    assert(xtestpointer);
94    assert(xtestkeyboard);
95    assert(IsXTestDevice(xtestpointer, NULL));
96    assert(IsXTestDevice(xtestkeyboard, NULL));
97    assert(IsXTestDevice(xtestpointer, inputInfo.pointer));
98
99    assert(IsXTestDevice(xtestkeyboard, inputInfo.keyboard));
100    assert(GetXTestDevice(inputInfo.pointer) == xtestpointer);
101
102    assert(GetXTestDevice(inputInfo.keyboard) == xtestkeyboard);
103}
104
105/**
106 * Each xtest devices has a property attached marking it. This property
107 * cannot be changed.
108 */
109static void
110xtest_properties(void)
111{
112    int rc;
113    char value = 1;
114    XIPropertyValuePtr prop;
115    Atom xtest_prop = XIGetKnownProperty(XI_PROP_XTEST_DEVICE);
116
117    rc = XIGetDeviceProperty(xtestpointer, xtest_prop, &prop);
118    assert(rc == Success);
119    assert(prop);
120
121    rc = XIGetDeviceProperty(xtestkeyboard, xtest_prop, &prop);
122    assert(rc == Success);
123    assert(prop != NULL);
124
125    rc = XIChangeDeviceProperty(xtestpointer, xtest_prop,
126                                XA_INTEGER, 8, PropModeReplace, 1, &value,
127                                FALSE);
128    assert(rc == BadAccess);
129    rc = XIChangeDeviceProperty(xtestkeyboard, xtest_prop,
130                                XA_INTEGER, 8, PropModeReplace, 1, &value,
131                                FALSE);
132    assert(rc == BadAccess);
133}
134
135int
136xtest_test(void)
137{
138    xtest_init_devices();
139    xtest_properties();
140
141    return 0;
142}
143