dbus 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,641 @@
1
+ /*--------------------------------------------------
2
+ * D-BUS bindings for Ruby
3
+ * (C) Copyright 2004 Leon Breedt
4
+ * (C) Copyright 2004 Provenco Group Ltd
5
+ *
6
+ * Licensed under the same terms as the D-BUS library
7
+ *--------------------------------------------------*/
8
+
9
+ #include "ruby-dbus.h"
10
+
11
+ VALUE cDBusMessage;
12
+
13
+ RDBUS_GENERATE_NEW(cDBusMessage, DBusMessage *, dbus_message_unref)
14
+ RDBUS_GENERATE_GET(DBusMessage)
15
+
16
+ /*
17
+ * call-seq:
18
+ * new(type) => self
19
+ *
20
+ * Creates a new DBusMessage of the specified type, which may be one
21
+ * of the DBus MESSAGE_TYPE constants.
22
+ */
23
+ static VALUE
24
+ cDBusMessage_new(VALUE klass, VALUE type)
25
+ {
26
+ DBusMessage *message;
27
+ int message_type;
28
+
29
+ message_type = NUM2INT(type);
30
+ switch (message_type) {
31
+ case DBUS_MESSAGE_TYPE_INVALID:
32
+ case DBUS_MESSAGE_TYPE_METHOD_CALL:
33
+ case DBUS_MESSAGE_TYPE_METHOD_RETURN:
34
+ case DBUS_MESSAGE_TYPE_ERROR:
35
+ case DBUS_MESSAGE_TYPE_SIGNAL:
36
+ /* fall-through */
37
+ break;
38
+ default:
39
+ rb_raise(eDBusError, "unsupported DBusMessageType %d", message_type);
40
+ }
41
+ message = NULL;
42
+ message = dbus_message_new(message_type);
43
+ RDBUS_RAISE_IF(message == NULL, "failed to create DBusMessage");
44
+ return RDBUS_NEW(cDBusMessage, message);
45
+ }
46
+
47
+ /*
48
+ * call-seq:
49
+ * new_method_call(service, path, interface, method_name) => self
50
+ *
51
+ * Creates a new DBusMessage representing a method call request on a remote
52
+ * object.
53
+ *
54
+ * - +service+ must contain the service to send the message to
55
+ * - +path+ must contain object path the message should be sent to
56
+ * - +interface+ must indicate the interface the method will be
57
+ * invoked on
58
+ * - +method_name+ must contain the name of a method defined in the given
59
+ * interface
60
+ */
61
+ static VALUE
62
+ cDBusMessage_new_method_call(VALUE klass, VALUE service, VALUE path,
63
+ VALUE interface, VALUE method)
64
+ {
65
+ DBusMessage *message;
66
+
67
+ message = NULL;
68
+ message = dbus_message_new_method_call(
69
+ StringValuePtr(service),
70
+ StringValuePtr(path),
71
+ StringValuePtr(interface),
72
+ StringValuePtr(method)
73
+ );
74
+ RDBUS_RAISE_IF(message == NULL, "failed to create DBusMessage");
75
+ return RDBUS_NEW(cDBusMessage, message);
76
+ }
77
+
78
+ /*
79
+ * call-seq:
80
+ * new_method_return(invocation_message) => self
81
+ *
82
+ * Creates a new DBusMessage representing a reply to a method call.
83
+ *
84
+ * The original method invocation request message must be provided in
85
+ * +invocation_message+.
86
+ */
87
+ static VALUE
88
+ cDBusMessage_new_method_return(VALUE klass, VALUE method_call_msg)
89
+ {
90
+ DBusMessage *message;
91
+
92
+ message = NULL;
93
+ message = dbus_message_new_method_return(MSG_GET(method_call_msg));
94
+ RDBUS_RAISE_IF(!message, "failed to create DBusMessage");
95
+ return RDBUS_NEW(cDBusMessage, message);
96
+ }
97
+
98
+ /*
99
+ * call-seq:
100
+ * new_signal(path, interface, signal_name) => self
101
+ *
102
+ * Creates a new DBusMessage representing a signal emission.
103
+ *
104
+ * The given +path+ must be the path to the object emitting the signal,
105
+ * +interface+ the interface the signal is being emitted from, and +signal_name+
106
+ * the name of the signal.
107
+ *
108
+ * The signal name can subsequently be retrieved from the +member+ field
109
+ * of the message.
110
+ */
111
+ static VALUE
112
+ cDBusMessage_new_signal(VALUE klass, VALUE path, VALUE interface, VALUE name)
113
+ {
114
+ DBusMessage *message;
115
+
116
+ message = NULL;
117
+ message = dbus_message_new_signal(
118
+ StringValuePtr(path),
119
+ StringValuePtr(interface),
120
+ StringValuePtr(name)
121
+ );
122
+ RDBUS_RAISE_IF(!message, "failed to create DBusMessage");
123
+ return RDBUS_NEW(cDBusMessage, message);
124
+ }
125
+
126
+ /*
127
+ * call-seq:
128
+ * new_error(request_message, error_name, error_message)
129
+ *
130
+ * Creates a new DBusMessage of type MESSAGE_TYPE_ERROR.
131
+ *
132
+ * Messages of this type are sent when a request fails for some reason.
133
+ * The error name has to be in a valid D-BUS error name format, meaning
134
+ * it has to contain at least one period.
135
+ */
136
+ static VALUE
137
+ cDBusMessage_new_error(VALUE klass, VALUE reply_to, VALUE error_name,
138
+ VALUE error_message)
139
+ {
140
+ DBusMessage *message;
141
+
142
+ message = NULL;
143
+ message = dbus_message_new_error(
144
+ MSG_GET(reply_to),
145
+ StringValuePtr(error_name),
146
+ StringValuePtr(error_message)
147
+ );
148
+ RDBUS_RAISE_IF(!message, "failed to create DBusMessage");
149
+ return RDBUS_NEW(cDBusMessage, message);
150
+ }
151
+
152
+ /*
153
+ * call-seq:
154
+ * get_type => type
155
+ *
156
+ * Returns an integer representing the message type. Its value will
157
+ * be one of the MESSAGE_TYPE constants.
158
+ */
159
+ static VALUE
160
+ cDBusMessage_get_type(VALUE self)
161
+ {
162
+ return INT2FIX(dbus_message_get_type(MSG_GET(self)));
163
+ }
164
+
165
+ #define MESSAGE_STR_SETTER_BODY(name) \
166
+ const char *str_value = StringValuePtr(value); \
167
+ if (!dbus_message_set_ ##name(MSG_GET(self), str_value)) \
168
+ return Qfalse; \
169
+ return Qtrue; \
170
+
171
+ /*
172
+ * call-seq:
173
+ * set_path(object_path) => true|false
174
+ *
175
+ * Sets the object path this message is being sent to (for a method call), or
176
+ * the path a signal is being emitted from (for a signal).
177
+ *
178
+ * Returns +false+ if not enough memory.
179
+ */
180
+ static VALUE
181
+ cDBusMessage_set_path(VALUE self, VALUE value)
182
+ {
183
+ MESSAGE_STR_SETTER_BODY(path);
184
+ }
185
+
186
+ /*
187
+ * call-seq:
188
+ * set_interface(interface) => true|false
189
+ *
190
+ * Sets the interface this message is being sent to (for a method call), or
191
+ * the interface a signal is being emitted from (for a signal).
192
+ *
193
+ * Returns +false+ if not enough memory.
194
+ */
195
+ static VALUE
196
+ cDBusMessage_set_interface(VALUE self, VALUE value)
197
+ {
198
+ MESSAGE_STR_SETTER_BODY(interface);
199
+ }
200
+
201
+ /*
202
+ * call-seq:
203
+ * set_member(member) => true|false
204
+ *
205
+ * Sets the interface member being invoked (for a method call), or emitted
206
+ * (for a signal).
207
+ *
208
+ * Returns +false+ if not enough memory.
209
+ */
210
+ static VALUE
211
+ cDBusMessage_set_member(VALUE self, VALUE value)
212
+ {
213
+ MESSAGE_STR_SETTER_BODY(member);
214
+ }
215
+
216
+ /*
217
+ * call-seq:
218
+ * set_error_name(name) => true|false
219
+ *
220
+ * Sets the name of the error (for an error message).
221
+ *
222
+ * Returns +false+ if not enough memory.
223
+ */
224
+ static VALUE
225
+ cDBusMessage_set_error_name(VALUE self, VALUE value)
226
+ {
227
+ MESSAGE_STR_SETTER_BODY(error_name);
228
+ }
229
+
230
+ /*
231
+ * call-seq:
232
+ * set_destination(service) => true|false
233
+ *
234
+ * Sets the message's destination service.
235
+ *
236
+ * Returns +false+ if not enough memory.
237
+ */
238
+ static VALUE
239
+ cDBusMessage_set_destination(VALUE self, VALUE value)
240
+ {
241
+ MESSAGE_STR_SETTER_BODY(destination);
242
+ }
243
+
244
+ /*
245
+ * call-seq:
246
+ * set_sender(service) => true|false
247
+ *
248
+ * Sets the service sending this message.
249
+ *
250
+ * Returns +false+ if not enough memory.
251
+ */
252
+ static VALUE
253
+ cDBusMessage_set_sender(VALUE self, VALUE value)
254
+ {
255
+ MESSAGE_STR_SETTER_BODY(sender);
256
+ }
257
+
258
+ #define MESSAGE_STR_GETTER_BODY(name) \
259
+ const char *value = dbus_message_get_ ##name(MSG_GET(self)); \
260
+ if (value == NULL) \
261
+ return Qnil; \
262
+ return rb_str_new2(value); \
263
+
264
+ /*
265
+ * call-seq:
266
+ * get_path => objectpath
267
+ *
268
+ * Gets the object path this message is being sent to (for a method call),
269
+ * or being emitted from (for a signal).
270
+ */
271
+ static VALUE
272
+ cDBusMessage_get_path(VALUE self)
273
+ {
274
+ MESSAGE_STR_GETTER_BODY(path);
275
+ }
276
+
277
+ /*
278
+ * call-seq:
279
+ * get_path => objectpath
280
+ *
281
+ * Gets the interface this message is being sent to.
282
+ */
283
+ static VALUE
284
+ cDBusMessage_get_interface(VALUE self)
285
+ {
286
+ MESSAGE_STR_GETTER_BODY(interface);
287
+ }
288
+
289
+ /*
290
+ * call-seq:
291
+ * get_member => membername
292
+ *
293
+ * Gets the interface member being invoked (for a method call), or
294
+ * emitted (for a signal).
295
+ */
296
+ static VALUE
297
+ cDBusMessage_get_member(VALUE self)
298
+ {
299
+ MESSAGE_STR_GETTER_BODY(member);
300
+ }
301
+
302
+ /*
303
+ * call-seq:
304
+ * get_error_name => name
305
+ *
306
+ * Gets the error name (for an error message).
307
+ */
308
+ static VALUE
309
+ cDBusMessage_get_error_name(VALUE self)
310
+ {
311
+ MESSAGE_STR_GETTER_BODY(error_name);
312
+ }
313
+
314
+ /*
315
+ * call-seq:
316
+ * get_destination => service
317
+ *
318
+ * Gets the destination service of this message.
319
+ */
320
+ static VALUE
321
+ cDBusMessage_get_destination(VALUE self)
322
+ {
323
+ MESSAGE_STR_GETTER_BODY(destination);
324
+ }
325
+
326
+ /*
327
+ * call-seq:
328
+ * get_sender => service
329
+ *
330
+ * Gets the sending service of this message.
331
+ */
332
+ static VALUE
333
+ cDBusMessage_get_sender(VALUE self)
334
+ {
335
+ MESSAGE_STR_GETTER_BODY(sender);
336
+ }
337
+
338
+ /*
339
+ * call-seq:
340
+ * get_signature => service
341
+ *
342
+ * Gets the type signature.
343
+ *
344
+ * A type signature is the type of the message argument values in the message
345
+ * payload. The signature is a string consisting of TYPE_xxx type codes:
346
+ */
347
+ static VALUE
348
+ cDBusMessage_get_signature(VALUE self)
349
+ {
350
+ MESSAGE_STR_GETTER_BODY(signature);
351
+ }
352
+
353
+ /*
354
+ * call-seq:
355
+ * set_no_reply(true|false) => nil
356
+ *
357
+ * Indicates whether or not the message wants a reply. If set, there is no
358
+ * way to know whether a message arrived successfully at the destination.
359
+ */
360
+ static VALUE
361
+ cDBusMessage_set_no_reply(VALUE self, VALUE value)
362
+ {
363
+ dbus_bool_t val;
364
+
365
+ if (TYPE(value) == T_FALSE || TYPE(value) == T_NIL)
366
+ val = FALSE;
367
+ else
368
+ val = TRUE;
369
+ dbus_message_set_no_reply(MSG_GET(self), val);
370
+ return Qnil;
371
+ }
372
+
373
+ /*
374
+ * call-seq:
375
+ * get_no_reply => true|false
376
+ *
377
+ * Returns +true+ if the message sender is not expecting a reply message.
378
+ */
379
+ static VALUE
380
+ cDBusMessage_get_no_reply(VALUE self)
381
+ {
382
+ if (!dbus_message_get_no_reply(MSG_GET(self)))
383
+ return Qfalse;
384
+ return Qtrue;
385
+ }
386
+
387
+ /*
388
+ * call-seq:
389
+ * is_method_call(interface, method_name) => true | false
390
+ *
391
+ * Returns +true+ if the message is a method call message with the given
392
+ * method name and interface fields.
393
+ */
394
+ static VALUE
395
+ cDBusMessage_is_method_call(VALUE self, VALUE interface, VALUE method)
396
+ {
397
+ if (!dbus_message_is_method_call(MSG_GET(self), StringValuePtr(interface),
398
+ StringValuePtr(method)))
399
+ return Qfalse;
400
+ return Qtrue;
401
+ }
402
+
403
+ /*
404
+ * call-seq:
405
+ * is_signal(interface, signal_name) => true | false
406
+ *
407
+ * Returns +true+ if the message is a signal message with the given interface
408
+ * and signal name fields.
409
+ */
410
+ static VALUE
411
+ cDBusMessage_is_signal(VALUE self, VALUE interface, VALUE signal_name)
412
+ {
413
+ if (!dbus_message_is_signal(MSG_GET(self), StringValuePtr(interface),
414
+ StringValuePtr(signal_name)))
415
+ return Qfalse;
416
+ return Qtrue;
417
+ }
418
+
419
+ /*
420
+ * call-seq:
421
+ * is_error(error_name)
422
+ *
423
+ * Returns +true+ if the message is an error message with the given error
424
+ * name field.
425
+ */
426
+ static VALUE
427
+ cDBusMessage_is_error(VALUE self, VALUE error)
428
+ {
429
+ if (!dbus_message_is_error(MSG_GET(self), StringValuePtr(error)))
430
+ return Qfalse;
431
+ return Qtrue;
432
+ }
433
+
434
+ /*
435
+ * call-seq:
436
+ * has_destination(service) => true | false
437
+ *
438
+ * Returns +true+ if the message has a destination service with the given
439
+ * service name.
440
+ */
441
+ static VALUE
442
+ cDBusMessage_has_destination(VALUE self, VALUE service)
443
+ {
444
+ if (!dbus_message_has_destination(MSG_GET(self), StringValuePtr(service)))
445
+ return Qfalse;
446
+ return Qtrue;
447
+ }
448
+
449
+ /*
450
+ * call-seq:
451
+ * has_sender(service) => true | false
452
+ *
453
+ * Returns +true+ if the message has the given service name as its sender.
454
+ */
455
+ static VALUE
456
+ cDBusMessage_has_sender(VALUE self, VALUE service)
457
+ {
458
+ if (!dbus_message_has_sender(MSG_GET(self), StringValuePtr(service)))
459
+ return Qfalse;
460
+ return Qtrue;
461
+ }
462
+
463
+ /*
464
+ * call-seq:
465
+ * has_signature(signature) => true | false
466
+ *
467
+ * Returns +true+ if the message has the given type signature.
468
+ * See DBusMessage#get_signature for more details on type signatures.
469
+ */
470
+ static VALUE
471
+ cDBusMessage_has_signature(VALUE self, VALUE signature)
472
+ {
473
+ if (!dbus_message_has_signature(MSG_GET(self), StringValuePtr(signature)))
474
+ return Qfalse;
475
+ return Qtrue;
476
+ }
477
+
478
+ /*
479
+ * call-seq:
480
+ * get_serial => serial
481
+ *
482
+ * Returns the client serial number of the message. This can't be set.
483
+ */
484
+ static VALUE
485
+ cDBusMessage_get_serial(VALUE self)
486
+ {
487
+ return INT2NUM(dbus_message_get_serial(MSG_GET(self)));
488
+ }
489
+
490
+ /*
491
+ * call-seq:
492
+ * get_reply_serial => serial
493
+ *
494
+ * Returns the serial number (see DbusMessage#get_serial) of the message
495
+ * that this message is a reply to.
496
+ */
497
+ static VALUE
498
+ cDBusMessage_get_reply_serial(VALUE self)
499
+ {
500
+ return INT2NUM(dbus_message_get_reply_serial(MSG_GET(self)));
501
+ }
502
+
503
+ /*
504
+ * call-seq:
505
+ * set_reply_serial(serial) => true
506
+ *
507
+ * Sets the serial number (see DbusMessage#get_serial) of the message
508
+ * that this message is a reply to.
509
+ */
510
+ static VALUE
511
+ cDBusMessage_set_reply_serial(VALUE self, VALUE serial)
512
+ {
513
+ if (!dbus_message_set_reply_serial(MSG_GET(self), NUM2UINT(serial)))
514
+ return Qfalse;
515
+ return Qtrue;
516
+ }
517
+
518
+ /*
519
+ * call-seq:
520
+ * set_auto_activation(true|false) => nil
521
+ *
522
+ * Indicates that the addressed service will be auto-activated before
523
+ * the message is delivered. If the service fails to activate, an
524
+ * activation error reply message will be received.
525
+ */
526
+ static VALUE
527
+ cDBusMessage_set_auto_activation(VALUE self, VALUE value)
528
+ {
529
+ dbus_bool_t val;
530
+
531
+ if (TYPE(value) == T_FALSE || TYPE(value) == T_NIL)
532
+ val = FALSE;
533
+ else
534
+ val = TRUE;
535
+ dbus_message_set_auto_activation(MSG_GET(self), val);
536
+ return Qnil;
537
+ }
538
+
539
+ /*
540
+ * call-seq:
541
+ * get_auto_activation => true|false
542
+ *
543
+ * Returns +true+ if the message will cause the addressed service
544
+ * to be auto-activated.
545
+ */
546
+ static VALUE
547
+ cDBusMessage_get_auto_activation(VALUE self)
548
+ {
549
+ if (!dbus_message_get_auto_activation(MSG_GET(self)))
550
+ return Qfalse;
551
+ return Qtrue;
552
+ }
553
+
554
+ /*
555
+ * call-seq:
556
+ * get_iter => message iterator
557
+ *
558
+ * Returns a DBusMessageIter iterator instance for this message. The
559
+ * iterator can be used to iterate over the values (arguments) appended
560
+ * to the message.
561
+ *
562
+ * To append values to the message, use DBusMessage#get_append_iter instead,
563
+ * as its position will already be at the end, and by using it you will avoid
564
+ * D-BUS error messages.
565
+ */
566
+ static VALUE
567
+ cDBusMessage_get_iter(VALUE self)
568
+ {
569
+ return RDBUS_NEW(cDBusMessageIter, rdbus_message_iter_new(MSG_GET(self), 0));
570
+ }
571
+
572
+ /*
573
+ * call-seq:
574
+ * get_append_iter => message append iterator
575
+ *
576
+ * Returns a DBusMessageIter iterator instance for appending values to
577
+ * this message. The position of the iterator is at the end of any existing
578
+ * values.
579
+ */
580
+ static VALUE
581
+ cDBusMessage_get_append_iter(VALUE self)
582
+ {
583
+ return RDBUS_NEW(cDBusMessageIter, rdbus_message_iter_new(MSG_GET(self), 1));
584
+ }
585
+
586
+ /*
587
+ * Document-class: DBus::Binding::DBusMessage
588
+ *
589
+ * Represents a message sent over or received from another application
590
+ * over the bus.
591
+ *
592
+ * A message has a type (DBusMessage#get_type) field indicating the nature
593
+ * of the message, and this can be used to decide which additional fields
594
+ * will contain useful data.
595
+ *
596
+ * In addition, a message can have application-specific values added to it,
597
+ * these are called +values+ or +arguments+. The order and type of the
598
+ * appended values define the message <i>type signature</i> (see
599
+ * DBusMessage#get_signature).
600
+ */
601
+ void Init_dbus_message()
602
+ {
603
+ cDBusMessage = rb_define_class_under(mDBusBinding, "DBusMessage", rb_cObject);
604
+
605
+ rb_define_singleton_method(cDBusMessage, "new", cDBusMessage_new, 1);
606
+ rb_define_singleton_method(cDBusMessage, "new_method_call", cDBusMessage_new_method_call, 4);
607
+ rb_define_singleton_method(cDBusMessage, "new_method_return", cDBusMessage_new_method_return, 1);
608
+ rb_define_singleton_method(cDBusMessage, "new_signal", cDBusMessage_new_signal, 3);
609
+ rb_define_singleton_method(cDBusMessage, "new_error", cDBusMessage_new_error, 3);
610
+
611
+ rb_define_method(cDBusMessage, "get_type", cDBusMessage_get_type, 0);
612
+ rb_define_method(cDBusMessage, "get_path", cDBusMessage_get_path, 0);
613
+ rb_define_method(cDBusMessage, "set_path", cDBusMessage_set_path, 1);
614
+ rb_define_method(cDBusMessage, "get_interface", cDBusMessage_get_interface, 0);
615
+ rb_define_method(cDBusMessage, "set_interface", cDBusMessage_set_interface, 1);
616
+ rb_define_method(cDBusMessage, "get_member", cDBusMessage_get_member, 0);
617
+ rb_define_method(cDBusMessage, "set_member", cDBusMessage_set_member, 1);
618
+ rb_define_method(cDBusMessage, "get_error_name", cDBusMessage_get_error_name, 0);
619
+ rb_define_method(cDBusMessage, "set_error_name", cDBusMessage_set_error_name, 1);
620
+ rb_define_method(cDBusMessage, "get_destination", cDBusMessage_get_destination, 0);
621
+ rb_define_method(cDBusMessage, "set_destination", cDBusMessage_set_destination, 1);
622
+ rb_define_method(cDBusMessage, "get_sender", cDBusMessage_get_sender, 0);
623
+ rb_define_method(cDBusMessage, "set_sender", cDBusMessage_set_sender, 1);
624
+ rb_define_method(cDBusMessage, "get_signature", cDBusMessage_get_signature, 0);
625
+ rb_define_method(cDBusMessage, "get_no_reply", cDBusMessage_get_no_reply, 0);
626
+ rb_define_method(cDBusMessage, "set_no_reply", cDBusMessage_set_no_reply, 1);
627
+ rb_define_method(cDBusMessage, "is_method_call", cDBusMessage_is_method_call, 2);
628
+ rb_define_method(cDBusMessage, "is_signal", cDBusMessage_is_signal, 2);
629
+ rb_define_method(cDBusMessage, "is_error", cDBusMessage_is_error, 1);
630
+ rb_define_method(cDBusMessage, "has_destination", cDBusMessage_has_destination, 1);
631
+ rb_define_method(cDBusMessage, "has_sender", cDBusMessage_has_sender, 1);
632
+ rb_define_method(cDBusMessage, "has_signature", cDBusMessage_has_signature, 1);
633
+ rb_define_method(cDBusMessage, "get_serial", cDBusMessage_get_serial, 0);
634
+ rb_define_method(cDBusMessage, "get_reply_serial", cDBusMessage_get_reply_serial, 0);
635
+ rb_define_method(cDBusMessage, "set_reply_serial", cDBusMessage_set_reply_serial, 1);
636
+ rb_define_method(cDBusMessage, "get_auto_activation", cDBusMessage_get_auto_activation, 0);
637
+ rb_define_method(cDBusMessage, "set_auto_activation", cDBusMessage_set_auto_activation, 1);
638
+
639
+ rb_define_method(cDBusMessage, "get_iter", cDBusMessage_get_iter, 0);
640
+ rb_define_method(cDBusMessage, "get_append_iter", cDBusMessage_get_append_iter, 0);
641
+ }