3 # jdahlin is the most coolest and awesomest person in the world
4 # and wrote all the good parts of this code. all the bad parts
5 # where python conditionals have a ( ) around them, thus violating
6 # PEP-8 were written by the lame wannabe python programmer seth
8 #FIXME: find memory leaks that I am sure exist
10 cdef extern from "sys/types.h":
15 cdef extern from "sys/cdefs.h":
18 cdef extern from "stdlib.h":
19 cdef void *malloc(size_t size)
20 cdef void free(void *ptr)
21 cdef void *calloc(size_t nmemb, size_t size)
23 cdef extern from "Python.h":
24 void Py_XINCREF (object)
25 void Py_XDECREF (object)
26 object PyString_FromStringAndSize(char *, int)
27 ctypedef void *PyGILState_STATE
29 PyGILState_STATE PyGILState_Ensure()
30 void PyGILState_Release(PyGILState_STATE)
32 ctypedef struct DBusError:
42 ctypedef struct DBusMessageIter:
58 ctypedef struct DBusObjectPathVTable:
59 DBusObjectPathUnregisterFunction unregister_function
60 DBusObjectPathMessageFunction message_function
61 void (* dbus_internal_pad1) (void *)
62 void (* dbus_internal_pad2) (void *)
63 void (* dbus_internal_pad3) (void *)
64 void (* dbus_internal_pad4) (void *)
66 class DBusException(Exception):
69 class ConnectionError(Exception):
72 class ObjectPath(str):
73 def __init__(self, value):
74 str.__init__(self, value)
77 def __init__(self, value):
78 str.__init__(self, value)
80 class SignatureIter(object):
81 def __init__(self, string):
83 self.remaining = string
86 if self.remaining == '':
89 signature = self.remaining
94 for marker in range(0, end):
95 cur_sig = ord(signature[marker])
97 if cur_sig == TYPE_ARRAY:
99 elif cur_sig == DICT_ENTRY_BEGIN or cur_sig == STRUCT_BEGIN:
100 if block_type == None:
103 if block_type == cur_sig:
104 block_depth = block_depth + 1
106 elif cur_sig == DICT_ENTRY_END:
107 if block_type == DICT_ENTRY_BEGIN:
108 block_depth = block_depth - 1
114 elif cur_sig == STRUCT_END:
115 if block_type == STRUCT_BEGIN:
116 block_depth = block_depth - 1
128 self.remaining = signature[end:]
129 return Signature(signature[0:end])
131 class Signature(str):
132 """An iterable method signature. Iterating gives the signature of each
134 def __init__(self, value):
135 return str.__init__(self, value)
138 return SignatureIter(self)
140 class VariantSignature(object):
141 """A fake method signature which when iterated, is an endless stream
142 of variants (handy with zip()). It has no string representation."""
150 def __init__(self, value):
151 int.__init__(self, value)
154 def __init__(self, value):
155 int.__init__(self, value)
158 def __init__(self, value):
159 int.__init__(self, value)
162 def __init__(self, value):
164 raise TypeError('Unsigned integers must not have a negitive value')
165 int.__init__(self, value)
168 def __init__(self, value):
169 int.__init__(self, value)
172 def __init__(self, value):
174 raise TypeError('Unsigned integers must not have a negitive value')
175 long.__init__(self, value)
178 def __init__(self, value):
179 long.__init__(self, value)
182 def __init__(self, value):
184 raise TypeError('Unsigned integers must not have a negitive value')
185 long.__init__(self, value)
188 def __init__(self, value):
189 float.__init__(self, value)
191 class String(unicode):
192 def __init__(self, value):
193 unicode.__init__(self, value)
196 def __init__(self, value, type=None, signature=None):
197 if signature and type:
198 raise TypeError('Can not mix type and signature arguments in a D-BUS Array')
201 self.signature = signature
202 list.__init__(self, value)
205 def __init__(self, value, type=None, signature=None):
207 if signature and type:
208 raise TypeError('Can not mix type and signature arguments in a D-BUS Variant')
211 self.signature = signature
214 return repr(self.value)
217 return str(self.value)
220 def __init__(self, value):
221 tuple.__init__(self, value)
223 class Dictionary(dict):
224 def __init__(self, value, key_type=None, value_type=None, signature=None):
225 if key_type and not value_type:
226 raise TypeError('When specifying a key_type you must also have a value_type in a D-BUS Dictionary')
227 elif value_type and not key_type:
228 raise TypeError('When specifying a value_type you must also have a key_type in a D-BUS Dictionary')
229 elif key_type and signature:
230 raise TypeError('Can not mix type arguments with signature arguments in a D-BUS Dictionary')
232 self.key_type = key_type
233 self.value_type = value_type
234 self.signature = signature
235 dict.__init__(self, value)
237 #forward delcerations
239 cdef class PendingCall
241 cdef class MessageIter
243 cdef void _GIL_safe_cunregister_function_handler (DBusConnection *connection,
247 tup = <object>user_data
248 assert (type(tup) == tuple)
251 conn.__cinit__(None, connection)
257 cdef void cunregister_function_handler (DBusConnection *connection,
259 cdef PyGILState_STATE gil
260 gil = PyGILState_Ensure()
262 _GIL_safe_cunregister_function_handler (connection, user_data);
264 PyGILState_Release(gil)
268 cdef DBusHandlerResult _GIL_safe_cmessage_function_handler (
269 DBusConnection *connection,
275 tup = <object>user_data
276 assert (type(tup) == tuple)
278 message = EmptyMessage()
280 #we don't own the message so we need to ref it
281 dbus_message_ref(msg)
282 message._set_msg(msg)
284 conn.__cinit__(None, connection)
288 retval = function(*args)
291 retval = DBUS_HANDLER_RESULT_HANDLED
294 cdef DBusHandlerResult cmessage_function_handler (DBusConnection *connection,
297 cdef PyGILState_STATE gil
298 gil = PyGILState_Ensure()
300 return _GIL_safe_cmessage_function_handler (connection, msg, user_data);
302 PyGILState_Release(gil)
305 cdef class Connection:
306 def __init__(self, address=None, Connection _conn=None):
307 cdef DBusConnection *c_conn
314 if (address != None or _conn != None):
315 self.__cinit__(c_address, c_conn)
317 # hack to be able to pass in a c pointer to the constructor
318 # while still alowing python programs to create a Connection object
319 cdef __cinit__(self, address, DBusConnection *_conn):
321 dbus_error_init(&error)
324 dbus_connection_ref(self.conn)
326 self.conn = dbus_connection_open(address,
328 if dbus_error_is_set(&error):
329 errormsg = error.message
330 dbus_error_free (&error)
331 raise DBusException, errormsg
333 def __dealloc__(self):
334 if self.conn != NULL:
335 dbus_connection_unref(self.conn)
337 cdef _set_conn(self, DBusConnection *conn):
340 cdef DBusConnection *_get_conn(self):
343 def get_unique_name(self):
344 return bus_get_unique_name(self)
347 dbus_connection_close(self.conn)
349 def get_is_connected(self):
350 return dbus_connection_get_is_connected(self.conn)
352 def get_is_authenticated(self):
353 return dbus_connection_get_is_authenticated(self.conn)
356 dbus_connection_flush(self.conn)
358 def borrow_message(self):
361 m._set_msg(dbus_connection_borrow_message(self.conn))
364 def return_message(self, Message message):
365 cdef DBusMessage *msg
366 msg = message._get_msg()
367 dbus_connection_return_message(self.conn, msg)
369 def steal_borrowed_message(self, Message message):
370 cdef DBusMessage *msg
371 msg = message._get_msg()
372 dbus_connection_steal_borrowed_message(self.conn,
375 def pop_message(self):
376 cdef DBusMessage *msg
379 msg = dbus_connection_pop_message(self.conn)
387 def get_dispatch_status(self):
388 return dbus_connection_get_dispatch_status(self.conn)
391 return dbus_connection_dispatch(self.conn)
393 def send(self, Message message):
394 #cdef dbus_uint32_t client_serial
395 #if type(message) != Message:
397 cdef DBusMessage *msg
398 msg = message._get_msg()
399 retval = dbus_connection_send(self.conn,
404 def send_with_reply_handlers(self, Message message, timeout_milliseconds, reply_handler, error_handler):
407 (retval, pending_call) = self.send_with_reply(message, timeout_milliseconds)
409 pending_call.set_notify(reply_handler, error_handler)
413 return (retval, pending_call)
415 def send_with_reply(self, Message message, timeout_milliseconds):
416 cdef dbus_bool_t retval
417 cdef DBusPendingCall *cpending_call
418 cdef DBusMessage *msg
419 cdef PendingCall pending_call
423 msg = message._get_msg()
425 retval = dbus_connection_send_with_reply(self.conn,
428 timeout_milliseconds)
430 if (cpending_call != NULL):
431 pending_call = PendingCall()
432 pending_call.__cinit__(cpending_call)
436 return (retval, pending_call)
438 def send_with_reply_and_block(self, Message message,
439 timeout_milliseconds=-1):
440 cdef DBusMessage * retval
442 cdef DBusMessage *msg
445 dbus_error_init(&error)
447 msg = message._get_msg()
449 retval = dbus_connection_send_with_reply_and_block(
452 timeout_milliseconds,
455 if dbus_error_is_set(&error):
456 errormsg = error.message
457 dbus_error_free (&error)
458 raise DBusException, errormsg
460 assert(retval != NULL)
467 def set_watch_functions(self, add_function, remove_function, data):
470 def set_timeout_functions(self, add_function, remove_function, data):
473 def set_wakeup_main_function(self, wakeup_main_function, data):
476 # FIXME: set_dispatch_status_function, get_unix_user, set_unix_user_function
478 def add_filter(self, filter_function):
479 user_data = (filter_function,)
480 Py_XINCREF(user_data)
482 return dbus_connection_add_filter(self.conn,
483 cmessage_function_handler,
488 #FIXME: remove_filter
489 # this is pretty tricky, we want to only remove the filter
490 # if we truly have no more calls to our message_function_handler...ugh
492 def set_data(self, slot, data):
495 def get_data(self, slot):
498 def set_max_message_size(self, size):
499 dbus_connection_set_max_message_size(self.conn, size)
501 def get_max_message_size(self):
502 return dbus_connection_get_max_message_size(self.conn)
504 def set_max_received_size(self, size):
505 dbus_connection_set_max_received_size(self.conn, size)
507 def get_max_received_size(self):
508 return dbus_connection_get_max_received_size(self.conn)
510 def get_outgoing_size(self):
511 return dbus_connection_get_outgoing_size(self.conn)
513 # preallocate_send, free_preallocated_send, send_preallocated
515 def register_object_path(self, path, unregister_cb, message_cb):
516 cdef DBusObjectPathVTable cvtable
518 cvtable.unregister_function = cunregister_function_handler
519 cvtable.message_function = cmessage_function_handler
521 user_data = (message_cb, unregister_cb)
522 Py_XINCREF(user_data)
524 return dbus_connection_register_object_path(self.conn, path, &cvtable,
527 def register_fallback(self, path, unregister_cb, message_cb):
528 cdef DBusObjectPathVTable cvtable
530 cvtable.unregister_function = cunregister_function_handler
531 cvtable.message_function = cmessage_function_handler
533 user_data = (message_cb, unregister_cb)
534 Py_XINCREF(user_data)
536 return dbus_connection_register_fallback(self.conn, path, &cvtable,
539 #FIXME: unregister_object_path , see problems with remove_filter
541 def list_registered (self, parent_path):
542 cdef char **cchild_entries
543 cdef dbus_bool_t retval
545 retval = dbus_connection_list_registered(self.conn, parent_path, &cchild_entries)
548 #FIXME: raise out of memory exception?
554 while (cchild_entries[i] != NULL):
555 child_entries.append(cchild_entries[i])
558 dbus_free_string_array(cchild_entries)
562 cdef void _GIL_safe_pending_call_notification (DBusPendingCall *pending_call,
564 cdef DBusMessage *dbus_message
567 (reply_handler, error_handler) = <object>user_data
569 dbus_message = dbus_pending_call_steal_reply(pending_call)
570 message = EmptyMessage()
571 message._set_msg(dbus_message)
573 type = message.get_type()
575 if type == MESSAGE_TYPE_METHOD_RETURN:
576 args = message.get_args_list()
578 elif type == MESSAGE_TYPE_ERROR:
579 args = message.get_args_list()
581 error_handler(DBusException(args[0]))
583 error_handler(DBusException(""))
585 error_handler(DBusException('Unexpected Message Type: ' + message.type_to_name(type)))
587 dbus_pending_call_unref(pending_call)
588 Py_XDECREF(<object>user_data)
590 cdef void _pending_call_notification(DBusPendingCall *pending_call,
592 cdef PyGILState_STATE gil
593 gil = PyGILState_Ensure()
595 _GIL_safe_pending_call_notification (pending_call, user_data);
597 PyGILState_Release(gil)
599 cdef void _pending_call_free_user_data(void *data):
600 call_tuple = <object>data
601 Py_XDECREF(call_tuple)
603 cdef class PendingCall:
604 cdef DBusPendingCall *pending_call
606 def __init__(self, PendingCall _pending_call=None):
607 self.pending_call = NULL
608 if (_pending_call != None):
609 self.__cinit__(_pending_call.pending_call)
611 cdef void __cinit__(self, DBusPendingCall *_pending_call):
612 self.pending_call = _pending_call
613 dbus_pending_call_ref(self.pending_call)
615 def __dealloc__(self):
616 if self.pending_call != NULL:
617 dbus_pending_call_unref(self.pending_call)
619 cdef DBusPendingCall *_get_pending_call(self):
620 return self.pending_call
623 dbus_pending_call_cancel(self.pending_call)
625 def get_completed(self):
626 return dbus_pending_call_get_completed(self.pending_call)
630 message = EmptyMessage()
631 message._set_msg(dbus_pending_call_steal_reply(self.pending_call))
635 dbus_pending_call_block(self.pending_call)
637 def set_notify(self, reply_handler, error_handler):
638 user_data = (reply_handler, error_handler)
639 Py_XINCREF(user_data)
640 dbus_pending_call_ref(self.pending_call)
641 dbus_pending_call_set_notify(self.pending_call, _pending_call_notification,
642 <void *>user_data, _pending_call_free_user_data)
646 cdef DBusWatch* watch
651 cdef __cinit__(self, DBusWatch *cwatch):
655 return dbus_watch_get_fd(self.watch)
657 # FIXME: not picked up correctly by extract.py
658 #def get_flags(self):
659 # return dbus_watch_get_flags(self.watch)
661 def handle(self, flags):
662 return dbus_watch_handle(self.watch, flags)
664 def get_enabled(self):
665 return dbus_watch_get_enabled(self.watch)
667 cdef class MessageIter:
668 cdef DBusMessageIter *iter
669 cdef DBusMessageIter real_iter
670 cdef dbus_uint32_t level
672 def __init__(self, level=0):
673 self.iter = &self.real_iter
676 raise TypeError, 'Type recurion is too deep'
678 cdef __cinit__(self, DBusMessageIter *iter):
679 self.real_iter = iter[0]
681 cdef DBusMessageIter *_get_iter(self):
685 return dbus_message_iter_has_next(self.iter)
688 return dbus_message_iter_next(self.iter)
690 def get(self, arg_type=None):
691 if(arg_type == None):
692 arg_type = self.get_arg_type()
694 if arg_type == TYPE_INVALID:
695 raise TypeError, 'Invalid arg type in MessageIter'
696 elif arg_type == TYPE_STRING:
697 retval = self.get_string()
698 elif arg_type == TYPE_INT16:
699 retval = self.get_int16()
700 elif arg_type == TYPE_UINT16:
701 retval = self.get_uint16()
702 elif arg_type == TYPE_INT32:
703 retval = self.get_int32()
704 elif arg_type == TYPE_UINT32:
705 retval = self.get_uint32()
706 elif arg_type == TYPE_INT64:
707 retval = self.get_int64()
708 elif arg_type == TYPE_UINT64:
709 retval = self.get_uint64()
710 elif arg_type == TYPE_DOUBLE:
711 retval = self.get_double()
712 elif arg_type == TYPE_BYTE:
713 retval = self.get_byte()
714 elif arg_type == TYPE_BOOLEAN:
715 retval = self.get_boolean()
716 elif arg_type == TYPE_SIGNATURE:
717 retval = self.get_signature()
718 elif arg_type == TYPE_ARRAY:
719 array_type = self.get_element_type()
720 if array_type == TYPE_DICT_ENTRY:
721 retval = self.get_dict()
723 retval = self.get_array(array_type)
724 elif arg_type == TYPE_OBJECT_PATH:
725 retval = self.get_object_path()
726 elif arg_type == TYPE_STRUCT:
727 retval = self.get_struct()
728 elif arg_type == TYPE_VARIANT:
729 retval = self.get_variant()
730 elif arg_type == TYPE_DICT_ENTRY:
731 raise TypeError, 'Dictionary Entries can only appear as part of an array container'
733 raise TypeError, 'Unknown arg type %d in MessageIter' % (arg_type)
737 def get_arg_type(self):
738 return dbus_message_iter_get_arg_type(self.iter)
740 def get_element_type(self):
741 return dbus_message_iter_get_element_type(self.iter)
745 dbus_message_iter_get_basic(self.iter, <char *>&c_val)
748 def get_boolean(self):
749 cdef dbus_bool_t c_val
750 dbus_message_iter_get_basic(self.iter, <dbus_bool_t *>&c_val)
757 def get_signature(self):
758 signature_string = self.get_string()
759 return Signature(signature_string)
762 cdef dbus_int16_t c_val
763 dbus_message_iter_get_basic(self.iter, <dbus_int16_t *>&c_val)
767 def get_uint16(self):
768 cdef dbus_uint16_t c_val
769 dbus_message_iter_get_basic(self.iter, <dbus_uint16_t *>&c_val)
773 cdef dbus_int32_t c_val
774 dbus_message_iter_get_basic(self.iter, <dbus_int32_t *>&c_val)
777 def get_uint32(self):
778 cdef dbus_uint32_t c_val
779 dbus_message_iter_get_basic(self.iter, <dbus_uint32_t *>&c_val)
783 cdef dbus_int64_t c_val
784 dbus_message_iter_get_basic(self.iter, <dbus_int64_t *>&c_val)
787 def get_uint64(self):
788 cdef dbus_uint64_t c_val
789 dbus_message_iter_get_basic(self.iter, <dbus_uint64_t *>&c_val)
792 def get_double(self):
794 dbus_message_iter_get_basic(self.iter, <double *>&c_val)
797 def get_string(self):
799 dbus_message_iter_get_basic(self.iter, <char **>&c_str)
800 ret = c_str.decode('utf8')
804 def get_object_path(self):
805 object_path_string = self.get_string()
806 return ObjectPath(object_path_string)
809 cdef DBusMessageIter c_dict_iter
810 cdef MessageIter dict_iter
811 level = self.level + 1
813 dbus_message_iter_recurse(self.iter, <DBusMessageIter *>&c_dict_iter)
814 dict_iter = MessageIter(level)
815 dict_iter.__cinit__(&c_dict_iter)
818 cur_arg_type = dict_iter.get_arg_type()
819 while cur_arg_type == TYPE_DICT_ENTRY:
820 if cur_arg_type != TYPE_DICT_ENTRY:
821 raise TypeError, "Dictionary elements must be of type TYPE_DICT_ENTRY '%s != %s'" % (TYPE_DICT_ENTRY, cur_arg_type)
823 dict_entry = dict_iter.get_struct()
824 if len(dict_entry) != 2:
825 raise TypeError, "Dictionary entries must be structs of two elements. This entry had %i elements.'" % (len(dict_entry))
827 python_dict[dict_entry[0]] = dict_entry[1]
830 cur_arg_type = dict_iter.get_arg_type()
834 def get_array(self, type):
835 cdef DBusMessageIter c_array_iter
836 cdef MessageIter array_iter
837 level = self.level + 1
839 dbus_message_iter_recurse(self.iter, <DBusMessageIter *>&c_array_iter)
840 array_iter = MessageIter(level)
841 array_iter.__cinit__(&c_array_iter)
844 cur_arg_type = array_iter.get_arg_type()
845 while cur_arg_type != TYPE_INVALID:
846 if cur_arg_type != type:
847 raise TypeError, "Array elements must be of the same type '%s != %s'" % (type, cur_arg_type)
849 value = array_iter.get(type)
850 python_list.append(value)
853 cur_arg_type = array_iter.get_arg_type()
857 def get_variant(self):
858 cdef DBusMessageIter c_var_iter
859 cdef MessageIter var_iter
860 level = self.level + 1
862 dbus_message_iter_recurse(self.iter, <DBusMessageIter *>&c_var_iter)
863 var_iter = MessageIter(level)
864 var_iter.__cinit__(&c_var_iter)
866 return var_iter.get()
868 def get_struct(self):
869 cdef DBusMessageIter c_struct_iter
870 cdef MessageIter struct_iter
871 level = self.level + 1
873 dbus_message_iter_recurse(self.iter, <DBusMessageIter *>&c_struct_iter)
874 struct_iter = MessageIter(level)
875 struct_iter.__cinit__(&c_struct_iter)
878 while struct_iter.get_arg_type() != TYPE_INVALID:
879 value = struct_iter.get()
880 python_list.append(value)
884 return tuple(python_list)
886 def python_value_to_dbus_sig(self, value, level = 0):
889 raise TypeError, 'Type recurion is too deep'
904 elif (ptype == str or ptype == unicode):
911 dict_list = value.items()
912 key, value = dict_list[0]
914 ret = str(chr(TYPE_ARRAY)) + str(chr(DICT_ENTRY_BEGIN))
915 ret = ret + self.python_value_to_dbus_sig(key, level)
916 ret = ret + self.python_value_to_dbus_sig(value, level)
917 ret = ret + str(chr(DICT_ENTRY_END))
920 ret = str(chr(STRUCT_BEGIN))
922 ret = ret + self.python_value_to_dbus_sig(item, level)
923 ret = ret + str(chr(STRUCT_END))
925 ret = str(chr(TYPE_ARRAY))
926 ret = ret + self.python_value_to_dbus_sig(value[0], level)
927 elif isinstance(value, ObjectPath) or value == ObjectPath:
929 ret = TYPE_OBJECT_PATH
931 elif isinstance(value, ByteArray) or value == ByteArray:
932 ret = str(chr(TYPE_ARRAY)) + str(chr(TYPE_BYTE))
933 elif isinstance(value, Signature) or value == Signature:
936 elif isinstance(value, Byte) or value == Byte:
939 elif isinstance(value, Boolean) or value == Boolean:
942 elif isinstance(value, Int16) or value == Int16:
945 elif isinstance(value, UInt16) or value == UInt16:
948 elif isinstance(value, Int32) or value == Int32:
951 elif isinstance(value, UInt32) or value == UInt32:
954 elif isinstance(value, Int64) or value == Int64:
957 elif isinstance(value, UInt64) or value == UInt64:
960 elif isinstance(value, Double) or value == Double:
963 elif isinstance(value, String) or value == String:
966 elif isinstance(value, Array):
967 ret = str(chr(TYPE_ARRAY))
968 if value.type == None:
970 ret = ret + value.signature
972 ret = ret + self.python_value_to_dbus_sig(value[0], level)
974 ret = ret + self.python_value_to_dbus_sig(value.type, level)
976 elif isinstance(value, Struct) or value == Struct:
977 ret = str(chr(STRUCT_BEGIN))
979 ret = ret + self.python_value_to_dbus_sig(item, level)
980 ret = ret + str(chr(STRUCT_END))
981 elif isinstance(value, Dictionary):
982 ret = str(chr(TYPE_ARRAY)) + str(chr(DICT_ENTRY_BEGIN))
984 if value.key_type and value.value_type:
985 ret = ret + self.python_value_to_dbus_sig(value.key_type, level)
986 ret = ret + self.python_value_to_dbus_sig(value.value_type, level)
987 elif value.signature:
988 ret = ret + value.signature
990 dict_list = value.items()
992 key, val = dict_list[0]
993 ret = ret + self.python_value_to_dbus_sig(key, level)
994 ret = ret + self.python_value_to_dbus_sig(val, level)
996 ret = ret + str(chr(DICT_ENTRY_END))
997 elif isinstance(value, Variant) or value == Variant:
998 ret = ret + str(chr(TYPE_VARIANT))
1000 raise TypeError, "Argument of unknown type '%s'" % (ptype)
1004 def append_strict(self, value, sig):
1006 if sig == TYPE_INVALID or sig == None:
1007 raise TypeError, 'Invalid arg type sent to append_strict'
1009 sig_type = ord(sig[0])
1011 if sig_type == TYPE_STRING:
1012 retval = self.append_string(value)
1013 elif sig_type == TYPE_INT16:
1014 retval = self.append_int16(value)
1015 elif sig_type == TYPE_UINT16:
1016 retval = self.append_uint16(value)
1017 elif sig_type == TYPE_INT32:
1018 retval = self.append_int32(value)
1019 elif sig_type == TYPE_UINT32:
1020 retval = self.append_uint32(value)
1021 elif sig_type == TYPE_INT64:
1022 retval = self.append_int64(value)
1023 elif sig_type == TYPE_UINT64:
1024 retval = self.append_uint64(value)
1025 elif sig_type == TYPE_DOUBLE:
1026 retval = self.append_double(value)
1027 elif sig_type == TYPE_BYTE:
1028 retval = self.append_byte(value)
1029 elif sig_type == TYPE_BOOLEAN:
1030 retval = self.append_boolean(value)
1031 elif sig_type == TYPE_SIGNATURE:
1032 retval = self.append_signature(value)
1033 elif sig_type == TYPE_ARRAY:
1035 raise TypeError, "Invalid array signature in append_strict. Arrays must be followed by a type."
1037 array_type = ord(sig[1])
1038 if array_type == DICT_ENTRY_BEGIN:
1039 if ord(sig[-1]) != DICT_ENTRY_END:
1040 raise TypeError, "Invalid dict entry in append_strict. No termination in signature %s."%(sig)
1043 retval = self.append_dict(Dictionary(value, signature=tmp_sig))
1046 retval = self.append_array(Array(value, signature=tmp_sig))
1047 elif sig_type == TYPE_OBJECT_PATH:
1048 retval = self.append_object_path(value)
1049 elif sig_type == STRUCT_BEGIN:
1050 if ord(sig[-1]) != STRUCT_END:
1051 raise TypeError, "Invalid struct entry in append_strict. No termination in signature %s." % (sig)
1054 retval = self.append_struct(value, signature = tmp_sig)
1055 elif sig_type == TYPE_VARIANT:
1056 retval = self.append_variant(Variant(value))
1057 elif sig_type == DICT_ENTRY_BEGIN:
1058 raise TypeError, "Signiture is invalid in append_strict. A dict entry must be part of an array."
1060 raise TypeError, "Argument of unknown type '%s' in append_strict" % (sig)
1064 def append(self, value):
1065 value_type = type(value)
1066 if value_type == bool:
1067 retval = self.append_boolean(value)
1068 elif value_type == int:
1069 retval = self.append_int32(value)
1070 elif value_type == long:
1071 retval = self.append_int64(value)
1072 elif (value_type == str or value_type == unicode):
1073 retval = self.append_string(value)
1074 elif value_type == float:
1075 retval = self.append_double(value)
1076 elif value_type == dict:
1077 retval = self.append_dict(value)
1078 elif value_type == tuple:
1079 retval = self.append_struct(value)
1080 elif value_type == list:
1081 retval = self.append_array(value)
1082 #elif value_type == None.__class__:
1083 # retval = self.append_nil()
1084 elif isinstance(value, ObjectPath):
1085 retval = self.append_object_path(value)
1086 elif isinstance(value, ByteArray):
1087 retval = self.append_array(value)
1088 elif isinstance(value, Signature):
1089 retval = self.append_signature(value)
1090 elif isinstance(value, Byte):
1091 retval = self.append_byte(value)
1092 elif isinstance(value, Boolean):
1093 retval = self.append_boolean(value)
1094 elif isinstance(value, Int16):
1095 retval = self.append_int16(value)
1096 elif isinstance(value, UInt16):
1097 retval = self.append_uint16(value)
1098 elif isinstance(value, Int32):
1099 retval = self.append_int32(value)
1100 elif isinstance(value, UInt32):
1101 retval = self.append_uint32(value)
1102 elif isinstance(value, Int64):
1103 retval = self.append_int64(value)
1104 elif isinstance(value, UInt64):
1105 retval = self.append_uint64(value)
1106 elif isinstance(value, Double):
1107 retval = self.append_double(value)
1108 elif isinstance(value, String):
1109 retval = self.append_string(value)
1110 elif isinstance(value, Array):
1111 retval = self.append_array(value)
1112 elif isinstance(value, Struct):
1113 retval = self.append_struct(value)
1114 elif isinstance(value, Dictionary):
1115 retval = self.append_dict(value)
1116 elif isinstance(value, Variant):
1117 retval = self.append_variant(value)
1119 raise TypeError, "Argument of unknown type '%s'" % (value_type)
1123 def append_boolean(self, value):
1124 cdef dbus_bool_t c_value
1126 return dbus_message_iter_append_basic(self.iter, TYPE_BOOLEAN, <dbus_bool_t *>&c_value)
1128 def append_byte(self, value):
1130 if type(value) == str and len(value) == 1:
1132 elif type(value) == Byte:
1137 return dbus_message_iter_append_basic(self.iter, TYPE_BYTE, <char *>&b)
1139 def append_int16(self, value):
1140 cdef dbus_int32_t c_value
1142 return dbus_message_iter_append_basic(self.iter, TYPE_INT16, <dbus_int32_t *>&c_value)
1144 def append_uint16(self, value):
1145 cdef dbus_uint32_t c_value
1147 return dbus_message_iter_append_basic(self.iter, TYPE_UINT16, <dbus_uint32_t *>&c_value)
1149 def append_int32(self, value):
1150 cdef dbus_int32_t c_value
1152 return dbus_message_iter_append_basic(self.iter, TYPE_INT32, <dbus_int32_t *>&c_value)
1154 def append_uint32(self, value):
1155 cdef dbus_uint32_t c_value
1157 return dbus_message_iter_append_basic(self.iter, TYPE_UINT32, <dbus_uint32_t *>&c_value)
1159 def append_int64(self, value):
1160 cdef dbus_int64_t c_value
1162 return dbus_message_iter_append_basic(self.iter, TYPE_INT64, <dbus_int64_t *>&c_value)
1164 def append_uint64(self, value):
1165 cdef dbus_uint64_t c_value
1167 return dbus_message_iter_append_basic(self.iter, TYPE_UINT64, <dbus_uint64_t *>&c_value)
1169 def append_double(self, value):
1172 return dbus_message_iter_append_basic(self.iter, TYPE_DOUBLE, <double *>&c_value)
1174 def append_string(self, value):
1176 tmp = value.encode('utf8')
1178 return dbus_message_iter_append_basic(self.iter, TYPE_STRING, <char **>&c_value)
1180 def append_object_path(self, value):
1183 return dbus_message_iter_append_basic(self.iter, TYPE_OBJECT_PATH, <char **>&c_value)
1185 def append_signature(self, value):
1188 return dbus_message_iter_append_basic(self.iter, TYPE_SIGNATURE, <char **>&c_value)
1191 def append_dict(self, python_dict):
1192 cdef DBusMessageIter c_dict_iter, c_dict_entry_iter
1193 cdef MessageIter dict_iter, dict_entry_iter
1195 level = self.level + 1
1200 sig = str(chr(DICT_ENTRY_BEGIN))
1202 if isinstance(python_dict, Dictionary):
1203 key = python_dict.key_type
1204 value = python_dict.value_type
1205 signature = python_dict.signature
1207 dict_list = python_dict.items()
1210 sig = sig + signature
1212 if not (key and value):
1213 key, value = dict_list[0]
1215 sig = sig + self.python_value_to_dbus_sig(key)
1216 sig = sig + self.python_value_to_dbus_sig(value)
1218 sig = sig + str(chr(DICT_ENTRY_END))
1220 dbus_message_iter_open_container(self.iter, TYPE_ARRAY, sig, <DBusMessageIter *>&c_dict_iter)
1221 dict_iter = MessageIter(level)
1222 dict_iter.__cinit__(&c_dict_iter)
1224 for key, value in dict_list:
1225 dbus_message_iter_open_container(dict_iter.iter, TYPE_DICT_ENTRY, sig, <DBusMessageIter *>&c_dict_entry_iter)
1226 dict_entry_iter = MessageIter(level)
1227 dict_entry_iter.__cinit__(&c_dict_entry_iter)
1230 signature_iter = iter(Signature(signature))
1231 tmp_sig = signature_iter.next()
1232 if not dict_entry_iter.append_strict(key, tmp_sig):
1233 dbus_message_iter_close_container(dict_iter.iter, dict_entry_iter.iter)
1234 dbus_message_iter_close_container(self.iter, dict_iter.iter)
1237 tmp_sig = signature_iter.next()
1238 if not dict_entry_iter.append_strict(value, tmp_sig):
1239 dbus_message_iter_close_container(dict_iter.iter, dict_entry_iter.iter)
1240 dbus_message_iter_close_container(self.iter, dict_iter.iter)
1244 if not dict_entry_iter.append(key):
1245 dbus_message_iter_close_container(dict_iter.iter, dict_entry_iter.iter)
1246 dbus_message_iter_close_container(self.iter, dict_iter.iter)
1249 if not dict_entry_iter.append(value):
1250 dbus_message_iter_close_container(dict_iter.iter, dict_entry_iter.iter)
1251 dbus_message_iter_close_container(self.iter, dict_iter.iter)
1254 dbus_message_iter_close_container(dict_iter.iter, dict_entry_iter.iter)
1256 dbus_message_iter_close_container(self.iter, dict_iter.iter)
1260 def append_struct(self, python_struct, signature = None):
1261 cdef DBusMessageIter c_struct_iter
1262 cdef MessageIter struct_iter
1264 level = self.level + 1
1265 dbus_message_iter_open_container(self.iter, TYPE_STRUCT, NULL, <DBusMessageIter *>&c_struct_iter)
1266 struct_iter = MessageIter(level)
1267 struct_iter.__cinit__(&c_struct_iter)
1269 signature_iter = iter(Signature(signature))
1270 for item in python_struct:
1272 sig = signature_iter.next()
1275 dbus_message_iter_close_container(self.iter, struct_iter.iter)
1278 if not struct_iter.append_strict(item, sig):
1279 dbus_message_iter_close_container(self.iter, struct_iter.iter)
1282 if not struct_iter.append(item):
1283 dbus_message_iter_close_container(self.iter, struct_iter.iter)
1286 dbus_message_iter_close_container(self.iter, struct_iter.iter)
1290 def append_array(self, python_list):
1291 cdef DBusMessageIter c_array_iter
1292 cdef MessageIter array_iter
1294 level = self.level + 1
1297 if isinstance(python_list, Array):
1298 if python_list.type:
1299 sig = self.python_value_to_dbus_sig(python_list.type)
1300 elif python_list.signature:
1301 sig = python_list.signature
1303 sig = self.python_value_to_dbus_sig(python_list[0])
1305 sig = self.python_value_to_dbus_sig(python_list[0])
1307 dbus_message_iter_open_container(self.iter, TYPE_ARRAY, sig, <DBusMessageIter *>&c_array_iter)
1308 array_iter = MessageIter(level)
1309 array_iter.__cinit__(&c_array_iter)
1311 length = len(python_list)
1312 for item in python_list:
1313 if not array_iter.append_strict(item, sig):
1314 dbus_message_iter_close_container(self.iter, array_iter.iter)
1317 dbus_message_iter_close_container(self.iter, array_iter.iter)
1321 def append_variant(self, value):
1322 cdef DBusMessageIter c_variant_iter
1323 cdef MessageIter variant_iter
1325 level = self.level + 1
1328 sig = value.signature
1330 sig = self.python_value_to_dbus_sig(value.type)
1332 sig = self.python_value_to_dbus_sig(value.value)
1334 dbus_message_iter_open_container(self.iter, TYPE_VARIANT, sig, <DBusMessageIter *>&c_variant_iter)
1336 variant_iter = MessageIter(level)
1337 variant_iter.__cinit__(&c_variant_iter)
1339 if not variant_iter.append(value.value):
1340 dbus_message_iter_close_container(self.iter, variant_iter.iter)
1343 dbus_message_iter_close_container(self.iter, variant_iter.iter)
1347 cdef DBusMessageIter c_array_iter
1348 cdef MessageIter array_iter
1350 value_at_iter = True
1352 while (value_at_iter):
1353 type = self.get_arg_type()
1354 if type == TYPE_INVALID:
1356 elif type == TYPE_STRING:
1357 str = iter.get_string()
1358 arg = 'string:%s\n' % (str)
1359 elif type == TYPE_OBJECT_PATH:
1360 path = iter.get_object_path()
1361 arg = 'object_path:%s\n' % (path)
1362 elif type == TYPE_INT32:
1363 num = iter.get_int32()
1364 arg = 'int32:%d\n' % (num)
1365 elif type == TYPE_UINT32:
1366 num = iter.get_uint32()
1367 arg = 'uint32:%u\n' % (num)
1368 elif type == TYPE_INT64:
1369 num = iter.get_int64()
1370 arg = 'int64:%d\n' % (num)
1371 elif type == TYPE_UINT64:
1372 num = iter.get_uint64()
1373 arg = 'uint64:%u\n' % (num)
1374 elif type == TYPE_DOUBLE:
1375 num = iter.get_double()
1376 arg = 'double:%f\n' % (num)
1377 elif type == TYPE_BYTE:
1378 num = iter.get_byte()
1379 arg = 'byte:%x(%s)\n' % (num, str(chr(num)))
1380 elif type == TYPE_BOOLEAN:
1381 bool = iter.get_boolean()
1386 arg = 'boolean:%s\n' % (str)
1387 elif type == TYPE_ARRAY:
1388 dbus_message_iter_recurse(self.iter, <DBusMessageIter *>&c_array_iter)
1389 array_iter = MessageIter(self.level + 1)
1390 array_iter.__cinit__(&c_array_iter)
1391 if array_iter.has_next():
1392 arg = 'array [' + str(array_iter) + ']'
1396 arg = '(unknown arg type %d)\n' % type
1398 retval = retval + arg
1399 value_at_iter = self.next()
1404 (MESSAGE_TYPE_INVALID, MESSAGE_TYPE_METHOD_CALL, MESSAGE_TYPE_METHOD_RETURN, MESSAGE_TYPE_ERROR, MESSAGE_TYPE_SIGNAL) = range(5)
1405 (TYPE_INVALID, TYPE_BYTE, TYPE_BOOLEAN, TYPE_INT16, TYPE_UINT16, TYPE_INT32, TYPE_UINT32, TYPE_INT64, TYPE_UINT64, TYPE_DOUBLE, TYPE_STRING, TYPE_OBJECT_PATH, TYPE_SIGNATURE, TYPE_ARRAY, TYPE_STRUCT, STRUCT_BEGIN, STRUCT_END, TYPE_VARIANT, TYPE_DICT_ENTRY, DICT_ENTRY_BEGIN, DICT_ENTRY_END) = (0, ord('y'), ord('b'), ord('n'), ord('q'), ord('i'), ord('u'), ord('x'), ord('t'), ord('d'), ord('s'), ord('o'), ord('g'), ord('a'), ord('r'), ord('('), ord(')'), ord('v'), ord('e'), ord('{'), ord('}'))
1406 (HANDLER_RESULT_HANDLED, HANDLER_RESULT_NOT_YET_HANDLED, HANDLER_RESULT_NEED_MEMORY) = range(3)
1409 cdef DBusMessage *msg
1411 def __init__(self, message_type=MESSAGE_TYPE_INVALID,
1412 service=None, path=None, dbus_interface=None, method=None,
1413 Message method_call=None,
1415 Message reply_to=None, error_name=None, error_message=None,
1422 cdef DBusMessage *cmsg
1425 if (dbus_interface != None):
1426 ciface = dbus_interface
1429 if (service != None):
1433 if message_type == MESSAGE_TYPE_METHOD_CALL:
1434 self.msg = dbus_message_new_method_call(cservice, path, ciface, method)
1435 elif message_type == MESSAGE_TYPE_METHOD_RETURN:
1436 cmsg = method_call._get_msg()
1437 self.msg = dbus_message_new_method_return(cmsg)
1438 elif message_type == MESSAGE_TYPE_SIGNAL:
1439 self.msg = dbus_message_new_signal(path, ciface, name)
1440 elif message_type == MESSAGE_TYPE_ERROR:
1441 cmsg = reply_to._get_msg()
1442 self.msg = dbus_message_new_error(cmsg, error_name, error_message)
1445 def __dealloc__(self):
1446 if self.msg != NULL:
1447 dbus_message_unref(self.msg)
1449 def type_to_name(self, type):
1450 if type == MESSAGE_TYPE_SIGNAL:
1452 elif type == MESSAGE_TYPE_METHOD_CALL:
1453 return "method call"
1454 elif type == MESSAGE_TYPE_METHOD_RETURN:
1455 return "method return"
1456 elif type == MESSAGE_TYPE_ERROR:
1459 return "(unknown message type)"
1462 message_type = self.get_type()
1463 sender = self.get_sender()
1466 sender = "(no sender)"
1468 if (message_type == MESSAGE_TYPE_METHOD_CALL) or (message_type == MESSAGE_TYPE_SIGNAL):
1469 retval = '%s interface=%s; member=%s; sender=%s' % (self.type_to_name(message_type),
1470 self.get_interface(),
1473 elif message_type == MESSAGE_TYPE_METHOD_RETURN:
1474 retval = '%s sender=%s' % (self.type_to_name(message_type),
1476 elif message_type == MESSAGE_TYPE_ERROR:
1477 retval = '%s name=%s; sender=%s' % (self.type_to_name(message_type),
1478 self.get_error_name(),
1481 retval = "Message of unknown type %d" % (message_type)
1484 # FIXME: should really use self.convert_to_tuple() here
1486 iter = self.get_iter()
1488 retval = retval + "\n" + str(iter)
1492 cdef _set_msg(self, DBusMessage *msg):
1495 cdef DBusMessage *_get_msg(self):
1498 def get_iter(self, append=False):
1499 cdef DBusMessageIter iter
1500 cdef MessageIter message_iter
1501 cdef DBusMessage *msg
1503 msg = self._get_msg()
1506 dbus_message_iter_init_append(msg, &iter)
1508 dbus_message_iter_init(msg, &iter)
1510 message_iter = MessageIter(0)
1511 message_iter.__cinit__(&iter)
1515 def get_args_list(self):
1518 iter = self.get_iter()
1520 retval.append(iter.get())
1521 except TypeError, e:
1524 value_at_iter = iter.next()
1525 while (value_at_iter):
1526 retval.append(iter.get())
1527 value_at_iter = iter.next()
1531 # FIXME: implement dbus_message_copy?
1534 return dbus_message_get_type(self.msg)
1536 def set_path(self, object_path):
1537 return dbus_message_set_path(self.msg, object_path)
1540 return dbus_message_get_path(self.msg)
1542 def set_interface(self, interface):
1543 return dbus_message_set_interface(self.msg, interface)
1545 def get_interface(self):
1546 return dbus_message_get_interface(self.msg)
1548 def set_member(self, member):
1549 return dbus_message_set_member(self.msg, member)
1551 def get_member(self):
1552 return dbus_message_get_member(self.msg)
1554 def set_error_name(self, name):
1555 return dbus_message_set_error_name(self.msg, name)
1557 def get_error_name(self):
1558 return dbus_message_get_error_name(self.msg)
1560 def set_destination(self, destination):
1561 return dbus_message_set_destination(self.msg, destination)
1563 def get_destination(self):
1564 return dbus_message_get_destination(self.msg)
1566 def set_sender(self, sender):
1567 return dbus_message_set_sender(self.msg, sender)
1569 def get_sender(self):
1571 sender = dbus_message_get_sender(self.msg)
1572 if (sender == NULL):
1577 def set_no_reply(self, no_reply):
1578 dbus_message_set_no_reply(self.msg, no_reply)
1580 def get_no_reply(self):
1581 return dbus_message_get_no_reply(self.msg)
1583 def is_method_call(self, interface, method):
1584 return dbus_message_is_method_call(self.msg, interface, method)
1586 def is_signal(self, interface, signal_name):
1587 return dbus_message_is_signal(self.msg, interface, signal_name)
1589 def is_error(self, error_name):
1590 return dbus_message_is_error(self.msg, error_name)
1592 def has_destination(self, service):
1593 return dbus_message_has_destination(self.msg, service)
1595 def has_sender(self, service):
1596 return dbus_message_has_sender(self.msg, service)
1598 def get_serial(self):
1599 return dbus_message_get_serial(self.msg)
1601 def set_reply_serial(self, reply_serial):
1602 return dbus_message_set_reply_serial(self.msg, reply_serial)
1604 def get_reply_serial(self):
1605 return dbus_message_get_reply_serial(self.msg)
1607 #FIXME: dbus_message_get_path_decomposed
1609 # FIXME: all the different dbus_message_*args* methods
1611 class Signal(Message):
1612 def __init__(self, spath, sinterface, sname):
1613 Message.__init__(self, MESSAGE_TYPE_SIGNAL, path=spath, dbus_interface=sinterface, name=sname)
1615 class EmptyMessage(Message):
1617 Message.__init__(self, _create=False)
1619 class MethodCall(Message):
1620 def __init__(self, mpath, minterface, mmethod):
1621 Message.__init__(self, MESSAGE_TYPE_METHOD_CALL, path=mpath, dbus_interface=minterface, method=mmethod)
1623 class MethodReturn(Message):
1624 def __init__(self, method_call):
1625 Message.__init__(self, MESSAGE_TYPE_METHOD_RETURN, method_call=method_call)
1627 class Error(Message):
1628 def __init__(self, reply_to, error_name, error_message):
1629 Message.__init__(self, MESSAGE_TYPE_ERROR, reply_to=reply_to, error_name=error_name, error_message=error_message)
1632 cdef DBusServer *server
1633 def __init__(self, address):
1634 cdef DBusError error
1635 dbus_error_init(&error)
1636 self.server = dbus_server_listen(address,
1638 if dbus_error_is_set(&error):
1639 errormsg = error.message
1640 dbus_error_free (&error)
1641 raise DBusException, errormsg
1643 def disconnect(self):
1644 dbus_server_disconnect(self.server)
1646 def get_is_connected(self):
1647 return dbus_server_get_is_connected(self.server)
1649 # def set_new_connection_function(self, function, data):
1650 # dbus_server_set_new_connection_function(self.conn, function,
1653 # def set_watch_functions(self, add_function, remove_function, data):
1654 # dbus_server_set_watch_functions(self.server,
1655 # add_function, remove_function,
1658 # def set_timeout_functions(self, add_function, remove_function, data):
1659 # dbus_server_set_timeout_functions(self.server,
1660 # add_function, remove_function,
1663 # def handle_watch(self, watch, condition):
1664 # dbus_server_handle_watch(self.conn, watch, condition)
1666 BUS_SESSION = DBUS_BUS_SESSION
1667 BUS_SYSTEM = DBUS_BUS_SYSTEM
1668 BUS_STARTER = DBUS_BUS_STARTER
1670 def bus_get (bus_type, private=False):
1671 cdef DBusError error
1672 cdef Connection conn
1673 cdef DBusConnection *connection
1675 dbus_error_init(&error)
1677 connection = dbus_bus_get_private(bus_type,
1680 connection = dbus_bus_get(bus_type,
1683 if dbus_error_is_set(&error):
1684 errormsg = error.message
1685 dbus_error_free(&error)
1686 raise DBusException, errormsg
1689 conn.__cinit__(None, connection)
1692 def bus_get_unique_name(Connection connection):
1693 cdef DBusConnection *conn
1694 conn = connection._get_conn()
1695 return dbus_bus_get_unique_name(conn)
1697 def bus_get_unix_user(Connection connection, service_name):
1698 cdef DBusError error
1699 dbus_error_init(&error)
1701 cdef DBusConnection *conn
1703 conn = connection._get_conn()
1704 retval = dbus_bus_get_unix_user(conn, service_name, &error)
1706 if dbus_error_is_set(&error):
1707 errormsg = error.message
1708 dbus_error_free(&error)
1709 raise DBusException, errormsg
1713 # these are defines, not enums, so they aren't auto generated
1714 DBUS_START_REPLY_SUCCESS = 0
1715 DBUS_START_REPLY_ALREADY_RUNNING = 1
1717 def bus_start_service_by_name(Connection connection, service_name, flags=0):
1718 cdef DBusError error
1719 dbus_error_init(&error)
1720 cdef dbus_bool_t retval
1721 cdef dbus_uint32_t results
1722 cdef DBusConnection *conn
1724 conn = connection._get_conn()
1726 retval = dbus_bus_start_service_by_name(conn, service_name, flags, &results, &error)
1728 if dbus_error_is_set(&error):
1729 errormsg = error.message
1730 dbus_error_free(&error)
1731 raise DBusException, errormsg
1733 return (retval, results)
1735 def bus_register(Connection connection):
1736 cdef DBusError error
1737 dbus_error_init(&error)
1738 cdef dbus_bool_t retval
1739 cdef DBusConnection *conn
1741 conn = connection._get_conn()
1742 retval = dbus_bus_register(conn,
1744 if dbus_error_is_set(&error):
1746 dbus_error_free(&error)
1747 raise DBusException, errormsg
1751 NAME_FLAG_PROHIBIT_REPLACEMENT = 0x1
1752 NAME_FLAG_REPLACE_EXISTING = 0x2
1754 REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
1755 REQUEST_NAME_REPLY_IN_QUEUE = 2
1756 REQUEST_NAME_REPLY_EXISTS = 3
1757 REQUEST_NAME_REPLY_ALREADY_OWNER = 4
1759 def bus_request_name(Connection connection, service_name, flags=0):
1760 cdef DBusError error
1761 dbus_error_init(&error)
1763 cdef DBusConnection *conn
1765 conn = connection._get_conn()
1766 retval = dbus_bus_request_name(conn,
1770 if dbus_error_is_set(&error):
1771 errormsg = error.message
1772 dbus_error_free(&error)
1773 raise DBusException, errormsg
1777 RELEASE_NAME_REPLY_RELEASED = 1
1778 RELEASE_NAME_REPLY_NON_EXISTENT = 2
1779 RELEASE_NAME_REPLY_NOT_OWNER = 3
1781 def bus_release_name(Connection connection, service_name):
1782 cdef DBusError error
1783 dbus_error_init(&error)
1785 cdef DBusConnection *conn
1787 conn = connection._get_conn()
1788 retval = dbus_bus_release_name(conn,
1791 if dbus_error_is_set(&error):
1792 errormsg = error.message
1793 dbus_error_free(&error)
1794 raise DBusException, errormsg
1798 def bus_name_has_owner(Connection connection, service_name):
1799 cdef DBusError error
1800 dbus_error_init(&error)
1801 cdef dbus_bool_t retval
1802 cdef DBusConnection *conn
1804 conn = connection._get_conn()
1805 retval = dbus_bus_name_has_owner(conn,
1808 if dbus_error_is_set(&error):
1809 errormsg = error.message
1810 dbus_error_free(&error)
1811 raise DBusException, errormsg
1815 def bus_add_match(Connection connection, rule):
1816 cdef DBusError error
1817 cdef DBusConnection *conn
1819 dbus_error_init(&error)
1821 conn = connection._get_conn()
1822 dbus_bus_add_match (conn, rule, &error)
1824 if dbus_error_is_set(&error):
1825 errormsg = error.message
1826 dbus_error_free(&error)
1827 raise DBusException, errormsg
1829 def bus_remove_match(Connection connection, rule):
1830 cdef DBusError error
1831 cdef DBusConnection *conn
1833 dbus_error_init(&error)
1835 conn = connection._get_conn()
1836 dbus_bus_remove_match (conn, rule, &error)
1838 if dbus_error_is_set(&error):
1839 errormsg = error.message
1840 dbus_error_free(&error)
1841 raise DBusException, errormsg