summaryrefslogtreecommitdiff
path: root/extensions.c
blob: 53b45b3304a94496c45a306851485a8fe587f0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "extensions.h"
#include "bigreqscope.h"
#include "lbxscope.h"
#include "randrscope.h"
#include "renderscope.h"
#include "shmscope.h"
#include "wcpscope.h"

/* Extensions we know how to decode */
struct extension_decoders {
    const char *name;
    void (*init_func) (const unsigned char *buf);
};

static const struct extension_decoders decodable_extensions[] = {
    {"BIG-REQUESTS", InitializeBIGREQ},
    {"LBX", InitializeLBX},
    {"MIT-SHM", InitializeMITSHM},
    {"NCD-WinCenterPro", InitializeWCP},
    {"RANDR", InitializeRANDR},
    {"RENDER", InitializeRENDER},
    {"GLX", InitializeGLX},
    {NULL, NULL}                /* List terminator - keep last */
};

/* all extensions we know about */
struct extension_info {
    const char *name;
    unsigned char request;
    unsigned char event;
    unsigned char error;
    long query_seq;             /* sequence id of QueryExtension request */
    struct extension_info *next;
};

struct extension_info *query_list;

struct extension_info *ext_by_request[NUM_EXTENSIONS];

static void
DefineExtNameValue(int type, unsigned char value, const char *extname)
{
    int namelen = strlen(extname) + 1;
    const char *typename = NULL;
    char *exttypename;

    switch (type) {
    case REQUEST:
        typename = "-Request";
        break;
    case REPLY:
        typename = "-Reply";
        break;
    case EVENT:
        typename = "-Event";
        break;
    case ERROR:
        typename = "-Error";
        break;
    case EXTENSION:
        typename = "";
        break;
    default:
        panic("Impossible argument to DefineExtNameValue");
    }
    namelen += strlen(typename);
    exttypename = malloc(namelen);
    if (exttypename == NULL)
        panic("Can't allocate memory for ExtNameValue");
    snprintf(exttypename, namelen, "%s%s", extname, typename);
    DefineEValue(&TD[type], (unsigned long) value, exttypename);
}

void
ProcessQueryExtensionRequest(long seq, const unsigned char *buf)
{
    int namelen = IShort(&buf[4]);
    char *extname = malloc(namelen + 1);
    struct extension_info *qe;

    if (extname == NULL)
        panic("Can't allocate memory for ExtensionRequest name");
    memcpy(extname, &buf[8], namelen);
    extname[namelen] = '\0';

    for (qe = query_list; qe != NULL; qe = qe->next) {
        if (strcmp(extname, qe->name) == 0) {
            /* already in list, no need to add */
            free(extname);
            return;
        }
    }

    /* add to list */
    qe = malloc(sizeof(struct extension_info));
    if (qe == NULL)
        panic("Can't allocate memory for extension_info");
    qe->name = extname;
    qe->request = 0;
    qe->event = 0;
    qe->error = 0;
    qe->query_seq = seq;
    qe->next = query_list;
    query_list = qe;
}

void
ProcessQueryExtensionReply(long seq, const unsigned char *buf)
{
    struct extension_info *qe;
    int i;

    if (buf[8] == 0) {
        /* Extension not present, nothing to record */
        return;
    }

    for (qe = query_list; qe != NULL; qe = qe->next) {
        if (qe->query_seq == seq) {
            qe->request = buf[9];
            qe->event = buf[10];
            qe->error = buf[11];

            ext_by_request[qe->request - EXTENSION_MIN_REQ] = qe;

            DefineExtNameValue(EXTENSION, qe->request, qe->name);

            for (i = 0; decodable_extensions[i].name != NULL; i++) {
                if (strcmp(qe->name, decodable_extensions[i].name) == 0) {
                    decodable_extensions[i].init_func(buf);
                    break;
                }
            }
            if (decodable_extensions[i].name == NULL) {
                /* Not found - initialize values as best we can generically */
                DefineExtNameValue(REQUEST, qe->request, qe->name);
                DefineExtNameValue(REPLY, qe->request, qe->name);
                if (qe->event != 0) {
                    DefineExtNameValue(EVENT, qe->event, qe->name);
                }
                if (qe->error != 0) {
                    DefineExtNameValue(ERROR, qe->error, qe->name);
                }
            }

            return;
        }
    }
}

/* Decoding for specific/known extensions */

static extension_decode_req_ptr ExtensionRequestDecoder[NUM_EXTENSIONS];
static extension_decode_reply_ptr ExtensionReplyDecoder[NUM_EXTENSIONS];
static extension_decode_error_ptr ExtensionErrorDecoder[NUM_EXTENSIONS];
static extension_decode_event_ptr ExtensionEventDecoder[NUM_EXT_EVENTS];
static extension_decode_event_ptr GenericEventDecoder[NUM_EXTENSIONS];

void
InitializeExtensionDecoder(int Request, extension_decode_req_ptr reqd,
                           extension_decode_reply_ptr repd)
{
    if ((Request > EXTENSION_MAX_REQ) || (Request < EXTENSION_MIN_REQ)) {
        char errmsg[128];

        snprintf(errmsg, sizeof(errmsg), "Failed to register decoder"
                 " for invalid extension request code %d.", Request);
        warn(errmsg);
        return;
    }
    ExtensionRequestDecoder[Request - EXTENSION_MIN_REQ] = reqd;
    ExtensionReplyDecoder[Request - EXTENSION_MIN_REQ] = repd;
}

void
InitializeExtensionErrorDecoder(int Error, extension_decode_error_ptr errd)
{
    if ((Error > EXTENSION_MAX_ERR) || (Error < EXTENSION_MIN_ERR)) {
        char errmsg[128];

        snprintf(errmsg, sizeof(errmsg), "Failed to register decoder"
                 " for invalid extension error code %d.", Error);
        warn(errmsg);
        return;
    }
    ExtensionErrorDecoder[Error - EXTENSION_MIN_ERR] = errd;
}

void
InitializeExtensionEventDecoder(int Event, extension_decode_event_ptr evd)
{
    if ((Event > EXTENSION_MAX_EV) || (Event < EXTENSION_MIN_EV)) {
        char errmsg[128];

        snprintf(errmsg, sizeof(errmsg), "Failed to register decoder"
                 " for invalid extension event code %d.", Event);
        warn(errmsg);
        return;
    }
    ExtensionEventDecoder[Event - EXTENSION_MIN_EV] = evd;
}

void
InitializeGenericEventDecoder(int Request, extension_decode_event_ptr evd)
{
    if ((Request > EXTENSION_MAX_REQ) || (Request < EXTENSION_MIN_REQ)) {
        char errmsg[128];

        snprintf(errmsg, sizeof(errmsg), "Failed to register decoder"
                 " for invalid generic extension event code %d.", Request);
        warn(errmsg);
        return;
    }
    GenericEventDecoder[Request - EXTENSION_MIN_REQ] = evd;
}

void
ExtensionRequest(FD fd, const unsigned char *buf, short Request)
{
    extension_decode_req_ptr decode_req = NULL;

    if ((Request <= EXTENSION_MAX_REQ) && (Request >= EXTENSION_MIN_REQ)) {
        decode_req = ExtensionRequestDecoder[Request - EXTENSION_MIN_REQ];
    }

    if (decode_req != NULL) {
        decode_req(fd, buf);
    }
    else {
        ExtendedRequest(fd, buf);
        ReplyExpected(fd, Request);
    }
}

void
ExtensionReply(FD fd, const unsigned char *buf,
               short Request, short RequestMinor)
{
    extension_decode_reply_ptr decode_reply = NULL;

    if ((Request <= EXTENSION_MAX_REQ) && (Request >= EXTENSION_MIN_REQ)) {
        decode_reply = ExtensionReplyDecoder[Request - EXTENSION_MIN_REQ];
    }

    if (decode_reply != NULL) {
        decode_reply(fd, buf, RequestMinor);
    }
    else {
        UnknownReply(buf);
    }
}

void
ExtensionError(FD fd, const unsigned char *buf, short Error)
{
    extension_decode_error_ptr decode_error = NULL;

    if ((Error <= EXTENSION_MAX_ERR) && (Error >= EXTENSION_MIN_ERR)) {
        decode_error = ExtensionErrorDecoder[Error - EXTENSION_MIN_ERR];
    }

    if (decode_error != NULL) {
        decode_error(fd, buf);
    }
    else {
        UnknownError(buf);
    }
}

void
ExtensionEvent(FD fd, const unsigned char *buf, short Event)
{
    extension_decode_event_ptr decode_event = NULL;

    if ((Event <= EXTENSION_MAX_EV) && (Event >= EXTENSION_MIN_EV)) {
        decode_event = ExtensionEventDecoder[Event - EXTENSION_MIN_EV];
    }
    else if (Event == Event_Type_Generic) {
        int Request = IByte(&buf[1]);

        if ((Request <= EXTENSION_MAX_REQ) && (Request >= EXTENSION_MIN_REQ)) {
            decode_event = GenericEventDecoder[Request - EXTENSION_MIN_REQ];
        }
    }

    if (decode_event != NULL) {
        decode_event(fd, buf);
    }
    else if (Event == Event_Type_Generic) {
        UnknownGenericEvent(buf);
    }
    else {
        UnknownEvent(buf);
    }
}