summaryrefslogtreecommitdiff
path: root/hw/dmx/dmxdbus.c
blob: 0ea462824b94b7d92f41e2c05a24f0b644cace07 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * Copyright © 2006-2007 Daniel Stone
 * Copyright © 2008 Novell, Inc.
 *
 * 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.
 *
 * Author: Daniel Stone <daniel@fooishbar.org>
 *         David Reveman <davidr@novell.com>
 */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "config-backends.h"
#include "dixstruct.h"
#include "opaque.h" /* for 'display': there should be a better way. */
#include "dmxdbus.h"
#include "dmxextension.h"

#define API_VERSION 3

#define MATCH_RULE "type='method_call',interface='org.x.config.dmx'"
#define MALFORMED_MSG "[dmx/dbus] malformed message, dropping"

struct connection_info {
    char           busobject[32];
    char           busname[64];
    DBusConnection *connection;
};

static void
reset_info (struct connection_info *info)
{
    info->connection = NULL;
    info->busname[0] = '\0';
    info->busobject[0] = '\0';
}

static int
enable_screen (DBusMessage *message,
	       DBusMessage *reply,
	       DBusError   *error)
{
    DMXScreenAttributesRec attr;
    uint32_t               screen;

    if (!dbus_message_get_args (message, error,
				DBUS_TYPE_UINT32,
				&screen,
				DBUS_TYPE_INVALID))
    {
	DebugF (MALFORMED_MSG ": %s, %s", error->name, error->message);
	return BadValue;
    }

    if (screen >= dmxGetNumScreens ())
    {
	dbus_set_error (error,
			DMX_ERROR_INVALID_SCREEN,
			"Screen %d does not exist", screen);
	return BadValue;
    }

    dmxGetScreenAttributes (screen, &attr);

    if (!attr.name || !*attr.name)
    {
	dbus_set_error (error,
			DBUS_ERROR_FAILED,
			"No back-end server attached to screen %d",
			screen);
	return BadValue;
    }

    dmxEnableScreen(screen);

    return Success;
}

static int
disable_screen (DBusMessage *message,
		DBusMessage *reply,
		DBusError   *error)
{
    DMXScreenAttributesRec attr;
    uint32_t               screen;

    if (!dbus_message_get_args (message, error,
				DBUS_TYPE_UINT32,
				&screen,
				DBUS_TYPE_INVALID))
    {
	DebugF (MALFORMED_MSG ": %s, %s", error->name, error->message);
	return BadValue;
    }

    if (screen >= dmxGetNumScreens ())
    {
	dbus_set_error (error,
			DMX_ERROR_INVALID_SCREEN,
			"Screen %d does not exist", screen);
	return BadValue;
    }

    dmxGetScreenAttributes (screen, &attr);

    if (!attr.name || !*attr.name)
    {
	dbus_set_error (error,
			DBUS_ERROR_FAILED,
			"No back-end server attached to screen %d",
			screen);
	return BadValue;
    }

    dmxDisableScreen(screen);

    return Success;
}

static int
attach_screen (DBusMessage *message,
	       DBusMessage *reply,
	       DBusError   *error)
{
    DMXScreenAttributesRec attr;
    uint32_t               window, screen, auth_type_len, auth_data_len;
    char                   *display, *auth_type, *auth_data, *name;
    int                    ret;

    if (!dbus_message_get_args (message, error,
				DBUS_TYPE_UINT32,
				&screen,
				DBUS_TYPE_STRING,
				&display,
				DBUS_TYPE_STRING,
				&name,
				DBUS_TYPE_UINT32,
				&window,
				DBUS_TYPE_ARRAY,  DBUS_TYPE_BYTE,
				&auth_type, &auth_type_len,
				DBUS_TYPE_ARRAY,  DBUS_TYPE_BYTE,
				&auth_data, &auth_data_len,
				DBUS_TYPE_INVALID))
    {
	DebugF (MALFORMED_MSG ": %s, %s", error->name, error->message);
	return BadValue;
    }

    if (!*name)
    {
	dbus_set_error (error,
			DBUS_ERROR_FAILED,
			"Cannot use empty string for screen name");
	return BadValue;
    }

    if (screen >= dmxGetNumScreens ())
    {
	dbus_set_error (error,
			DMX_ERROR_INVALID_SCREEN,
			"Screen %d does not exist", screen);
	return BadValue;
    }

    dmxGetScreenAttributes (screen, &attr);

    if (attr.name && *attr.name)
    {
	dbus_set_error (error,
			DMX_ERROR_SCREEN_IN_USE,
			"Back-end server already attached to screen %d",
			screen);
	return BadValue;
    }

    memset (&attr, 0, sizeof (attr));

    attr.name        = name;
    attr.displayName = display;

    ret = dmxAttachScreen (screen,
			   &attr,
			   window,
			   auth_type,
			   auth_type_len,
			   auth_data,
			   auth_data_len,
			   (dmxErrorSetProcPtr) dbus_set_error,
			   error,
			   DBUS_ERROR_FAILED);

    if (ret != Success)
    {
        DebugF ("[dmx/dbus] dmxAttachScreen failed\n");
        return ret;
    }

    return Success;
}

static int
detach_screen (DBusMessage *message,
	       DBusMessage *reply,
	       DBusError   *error)
{
    uint32_t screen;
    int      ret;

    if (!dbus_message_get_args (message, error,
				DBUS_TYPE_UINT32,
				&screen,
				DBUS_TYPE_INVALID))
   { 
       DebugF (MALFORMED_MSG ": %s, %s", error->name, error->message);
       return BadValue;
    }

    ret = dmxDetachScreen (screen);
    if (ret != Success)
    {
        DebugF ("[dmx/dbus] dmxDetachScreen failed\n");
        return ret;
    }

    return Success;
}

static int
add_input (DBusMessage *message,
	   DBusMessage *reply,
	   DBusError   *error)
{
    DMXInputAttributesRec attr;
    DBusMessageIter       iter;
    uint32_t              screen, id;
    dbus_bool_t           core;
    int                   input_id, ret;

    dbus_message_iter_init_append (reply, &iter);

    if (!dbus_message_get_args (message, error,
				DBUS_TYPE_UINT32,
				&screen,
				DBUS_TYPE_BOOLEAN,
				&core,
				DBUS_TYPE_INVALID))
    {
	DebugF (MALFORMED_MSG ": %s, %s", error->name, error->message);
	return BadValue;
    }

    memset (&attr, 0, sizeof (attr));

    attr.physicalScreen = screen;
    attr.inputType      = 2;

    ret = dmxAddInput (&attr, &input_id);
    if (ret != Success)
    {
	DebugF ("[dmx/dbus] dmxAddInput failed\n");
	return ret;
    }

    id = input_id;

    if (!dbus_message_iter_append_basic (&iter,
					 DBUS_TYPE_UINT32,
					 &id))
    {
	ErrorF ("[dmx/dbus] couldn't append to iterator\n");
	dmxRemoveInput (id);
	return BadAlloc;
    }

    return Success;
}

static int
remove_input (DBusMessage *message,
	      DBusMessage *reply,
	      DBusError   *error)
{
    uint32_t id;
    int      ret;

    if (!dbus_message_get_args (message, error,
				DBUS_TYPE_UINT32,
				&id,
				DBUS_TYPE_INVALID))
    {
	DebugF (MALFORMED_MSG ": %s, %s", error->name, error->message);
	return BadValue;
    }

    ret = dmxRemoveInput (id);
    if (ret != Success)
    {
	DebugF ("[dmx/dbus] dmxRemoveInput failed\n");
	return ret;
    }

    return Success;
}

static int
list_screens (DBusMessage *message,
	      DBusMessage *reply,
	      DBusError   *error)
{
    DBusMessageIter iter, subiter;
    int		    screen;

    dbus_message_iter_init_append (reply, &iter);

    for (screen = 0; screen < dmxGetNumScreens (); screen++)
    {
	DMXScreenAttributesRec attribs;

	dmxGetScreenAttributes (screen, &attribs);

	if (!attribs.name || !*attribs.name)
	    continue;

        if (!dbus_message_iter_open_container (&iter,
					       DBUS_TYPE_STRUCT,
					       NULL,
					       &subiter))
	{
            ErrorF ("[dmx/dbus] couldn't init container\n");
            return BadAlloc;
        }

        if (!dbus_message_iter_append_basic (&subiter,
					     DBUS_TYPE_UINT32,
					     &screen))
	{
            ErrorF ("[dmx/dbus] couldn't append to iterator\n");
            return BadAlloc;
        }

        if (!dbus_message_iter_append_basic (&subiter,
					     DBUS_TYPE_STRING,
					     &attribs.name))
	{
            ErrorF("[dmx/dbus] couldn't append to iterator\n");
            return BadAlloc;
        }

        if (!dbus_message_iter_close_container (&iter, &subiter))
	{
            ErrorF ("[dmx/dbus] couldn't close container\n");
            return BadAlloc;
        }
    }

    return Success;
}

static int
get_version (DBusMessage *message,
	     DBusMessage *reply,
	     DBusError   *error)
{
    DBusMessageIter iter;
    unsigned int    version = API_VERSION;

    dbus_message_iter_init_append (reply, &iter);

    if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &version))
    {
        ErrorF ("[dmx/dbus] couldn't append version\n");
        return BadAlloc;
    }

    return Success;
}

static DBusHandlerResult
message_handler (DBusConnection *connection,
		 DBusMessage    *message,
		 void           *data)
{
    struct connection_info *info = data;
    DBusMessage            *reply;
    DBusError              error;

    /* ret is the overall D-Bus handler result, whereas err is the internal
     * X error from our individual functions. */
    int                    err, ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    DebugF ("[dmx/dbus] received a message for %s\n",
	    dbus_message_get_interface (message));

    dbus_error_init (&error);

    reply = dbus_message_new_method_return (message);
    if (!reply)
    {
        ErrorF ("[dmx/dbus] failed to create reply\n");
        ret = DBUS_HANDLER_RESULT_NEED_MEMORY;
        goto err_start;
    }

    if (strcmp (dbus_message_get_member (message), "enableScreen") == 0)
        err = enable_screen (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "disableScreen") == 0)
        err = disable_screen (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "attachScreen") == 0)
        err = attach_screen (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "detachScreen") == 0)
        err = detach_screen (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "addInput") == 0)
        err = add_input (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "removeInput") == 0)
        err = remove_input (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "listScreens") == 0)
        err = list_screens (message, reply, &error);
    else if (strcmp (dbus_message_get_member (message), "version") == 0)
        err = get_version (message, reply, &error);
    else
        goto err_reply;

    /* Failure to allocate is a special case. */
    if (err == BadAlloc)
    {
        ret = DBUS_HANDLER_RESULT_NEED_MEMORY;
        goto err_reply;
    }

    if (err != Success)
    {
	dbus_message_unref (reply);

	reply = dbus_message_new_error_printf (message,
					       error.name,
					       error.message);

	if (!reply)
	{
	    ErrorF ("[dmx/dbus] failed to create reply\n");
	    ret = DBUS_HANDLER_RESULT_NEED_MEMORY;
	    goto err_start;
	}
    }

    /* While failure here is always an OOM, we don't return that,
     * since that would result in devices being double-added/removed. */
    if (dbus_connection_send (info->connection, reply, NULL))
	dbus_connection_flush (info->connection);
    else
        ErrorF ("[dmx/dbus] failed to send reply\n");

    ret = DBUS_HANDLER_RESULT_HANDLED;

err_reply:
    dbus_message_unref (reply);
err_start:
    dbus_error_free (&error);

    return ret;
}

static void
connect_hook (DBusConnection *connection,
	      void           *data)
{
    DBusObjectPathVTable   vtable = { .message_function = message_handler };
    DBusError              error;
    struct connection_info *info = data;

    info->connection = connection;

    dbus_error_init (&error);

    /* blocks until we get a reply. */
    dbus_bus_add_match (info->connection, MATCH_RULE, &error);
    if (!dbus_error_is_set (&error))
    {
	if (dbus_connection_register_object_path (info->connection,
						  info->busobject,
						  &vtable,
						  info))
	{
	    DebugF ("[dbus] registered %s, %s\n", info->busname,
		    info->busobject);
	}
	else
	{
	    ErrorF ("[dmx/dbus] couldn't register object path\n");
	    dbus_bus_remove_match (info->connection, MATCH_RULE, &error);
	    reset_info (info);
	}
    }
    else
    {
        ErrorF ("[dmx/dbus] couldn't add match: %s (%s)\n", error.name,
		error.message);
	reset_info (info);
    }

    dbus_error_free (&error);
}

static void
disconnect_hook (void *data)
{
}

static struct connection_info connection_data;
static struct config_dbus_core_hook core_hook = {
    .connect    = connect_hook,
    .disconnect = disconnect_hook,
    .data       = &connection_data
};

int
dmx_dbus_init (void)
{
    snprintf (connection_data.busobject, sizeof (connection_data.busobject),
	      "/org/x/config/dmx/%d", atoi (display));

    return config_dbus_core_add_hook (&core_hook);
}

void
dmx_dbus_fini (void)
{
    config_dbus_core_remove_hook (&core_hook);
    reset_info (&connection_data);
}