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#ifdef HAVE_DIX_CONFIG_H
25#include <dix-config.h>
26#endif
27
28#include <stdint.h>
29#include "extinit.h" /* for XInputExtensionInit */
30#include "exglobals.h"
31#include "xkbsrv.h" /* for XkbInitPrivates */
32#include <glib.h>
33
34#include "protocol-common.h"
35
36struct devices devices;
37ScreenRec screen;
38WindowRec root;
39WindowRec window;
40
41void *userdata;
42
43extern int CorePointerProc(DeviceIntPtr pDev, int what);
44extern int CoreKeyboardProc(DeviceIntPtr pDev, int what);
45
46static void fake_init_sprite(DeviceIntPtr dev)
47{
48    SpritePtr sprite;
49    sprite = dev->spriteInfo->sprite;
50
51    sprite->spriteTraceSize = 10;
52    sprite->spriteTrace = calloc(sprite->spriteTraceSize, sizeof(WindowPtr));
53    sprite->spriteTraceGood = 1;
54    sprite->spriteTrace[0] = &root;
55    sprite->hot.x = SPRITE_X;
56    sprite->hot.y = SPRITE_Y;
57    sprite->hotPhys.x = sprite->hot.x;
58    sprite->hotPhys.y = sprite->hot.y;
59    sprite->win = &window;
60    sprite->hotPhys.pScreen = &screen;
61    sprite->physLimits.x1 = 0;
62    sprite->physLimits.y1 = 0;
63    sprite->physLimits.x2 = screen.width;
64    sprite->physLimits.y2 = screen.height;
65}
66
67/**
68 * Create and init 2 master devices (VCP + VCK) and two slave devices, one
69 * default mouse, one default keyboard.
70 */
71struct devices init_devices(void)
72{
73    ClientRec client;
74    struct devices devices;
75
76    client = init_client(0, NULL);
77
78    AllocDevicePair(&client, "Virtual core", &devices.vcp, &devices.vck,
79                    CorePointerProc, CoreKeyboardProc, TRUE);
80    inputInfo.pointer = devices.vcp;
81    inputInfo.keyboard = devices.vck;
82    ActivateDevice(devices.vcp, FALSE);
83    ActivateDevice(devices.vck, FALSE);
84    EnableDevice(devices.vcp, FALSE);
85    EnableDevice(devices.vck, FALSE);
86
87    AllocDevicePair(&client, "", &devices.mouse, &devices.kbd,
88                    CorePointerProc, CoreKeyboardProc, FALSE);
89    ActivateDevice(devices.mouse, FALSE);
90    ActivateDevice(devices.kbd, FALSE);
91    EnableDevice(devices.mouse, FALSE);
92    EnableDevice(devices.kbd, FALSE);
93
94    devices.num_devices = 4;
95    devices.num_master_devices = 2;
96
97    fake_init_sprite(devices.mouse);
98    fake_init_sprite(devices.vcp);
99
100    return devices;
101}
102
103
104/* Create minimal client, with the given buffer and len as request buffer */
105ClientRec init_client(int len, void *data)
106{
107    ClientRec client = { 0 };
108
109    /* we store the privates now and reassign it after the memset. this way
110     * we can share them across multiple test runs and don't have to worry
111     * about freeing them after each test run. */
112
113    client.index = CLIENT_INDEX;
114    client.clientAsMask = CLIENT_MASK;
115    client.sequence = CLIENT_SEQUENCE;
116    client.req_len = len;
117
118    client.requestBuffer = data;
119    dixAllocatePrivates(&client.devPrivates, PRIVATE_CLIENT);
120    return client;
121}
122
123void init_window(WindowPtr window, WindowPtr parent, int id)
124{
125    memset(window, 0, sizeof(*window));
126
127    window->drawable.id = id;
128    if (parent)
129    {
130        window->drawable.x = 30;
131        window->drawable.y = 50;
132        window->drawable.width = 100;
133        window->drawable.height = 200;
134    }
135    window->parent = parent;
136    window->optional = calloc(1, sizeof(WindowOptRec));
137    g_assert(window->optional);
138}
139
140extern DevPrivateKeyRec miPointerScreenKeyRec;
141extern DevPrivateKeyRec miPointerPrivKeyRec;
142
143/* Needed for the screen setup, otherwise we crash during sprite initialization */
144static Bool device_cursor_init(DeviceIntPtr dev, ScreenPtr screen) { return TRUE; }
145static Bool set_cursor_pos(DeviceIntPtr dev, ScreenPtr screen, int x, int y, Bool event) { return TRUE; }
146void init_simple(void)
147{
148    screenInfo.numScreens = 1;
149    screenInfo.screens[0] = &screen;
150
151    screen.myNum = 0;
152    screen.id = 100;
153    screen.width = 640;
154    screen.height = 480;
155    screen.DeviceCursorInitialize = device_cursor_init;
156    screen.SetCursorPosition = set_cursor_pos;
157
158    dixResetPrivates();
159    InitAtoms();
160    XkbInitPrivates();
161    dixRegisterPrivateKey(&XIClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(XIClientRec));
162    dixRegisterPrivateKey(&miPointerScreenKeyRec, PRIVATE_SCREEN, 0);
163    dixRegisterPrivateKey(&miPointerPrivKeyRec, PRIVATE_DEVICE, 0);
164    XInputExtensionInit();
165
166    init_window(&root, NULL, ROOT_WINDOW_ID);
167    init_window(&window, &root, CLIENT_WINDOW_ID);
168
169    devices = init_devices();
170}
171
172void __wrap_WriteToClient(ClientPtr client, int len, void *data)
173{
174    g_assert(reply_handler != NULL);
175
176    (*reply_handler)(client, len, data, userdata);
177}
178
179