Update NEWS
[dbus-python-phuang.git] / _dbus_bindings / mainloop.c
blobb3fdfb74ee016724e3dc0038e52cc472706ce52a
1 /* Implementation of main-loop integration for dbus-python.
3 * Copyright (C) 2006 Collabora Ltd.
5 * Licensed under the Academic Free License version 2.1
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include "dbus_bindings-internal.h"
27 /* Native mainloop wrapper ========================================= */
29 PyDoc_STRVAR(NativeMainLoop_tp_doc,
30 "Object representing D-Bus main loop integration done in native code.\n"
31 "Cannot be instantiated directly.\n"
34 static PyTypeObject NativeMainLoop_Type;
36 DEFINE_CHECK(NativeMainLoop)
38 typedef struct {
39 PyObject_HEAD
40 /* Called with the GIL held, should set a Python exception on error */
41 dbus_bool_t (*set_up_connection_cb)(DBusConnection *, void *);
42 dbus_bool_t (*set_up_server_cb)(DBusServer *, void *);
43 /* Called in a destructor. Must not touch the exception state (use
44 * PyErr_Fetch and PyErr_Restore if necessary). */
45 void (*free_cb)(void *);
46 void *data;
47 } NativeMainLoop;
49 static void NativeMainLoop_tp_dealloc(NativeMainLoop *self)
51 if (self->data && self->free_cb) {
52 (self->free_cb)(self->data);
54 PyObject_Del((PyObject *)self);
57 static PyTypeObject NativeMainLoop_Type = {
58 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
60 "dbus.mainloop.NativeMainLoop",
61 sizeof(NativeMainLoop),
63 (destructor)NativeMainLoop_tp_dealloc, /* tp_dealloc */
64 0, /* tp_print */
65 0, /* tp_getattr */
66 0, /* tp_setattr */
67 0, /* tp_compare */
68 0, /* tp_repr */
69 0, /* tp_as_number */
70 0, /* tp_as_sequence */
71 0, /* tp_as_mapping */
72 0, /* tp_hash */
73 0, /* tp_call */
74 0, /* tp_str */
75 0, /* tp_getattro */
76 0, /* tp_setattro */
77 0, /* tp_as_buffer */
78 Py_TPFLAGS_DEFAULT, /* tp_flags */
79 NativeMainLoop_tp_doc, /* tp_doc */
80 0, /* tp_traverse */
81 0, /* tp_clear */
82 0, /* tp_richcompare */
83 0, /* tp_weaklistoffset */
84 0, /* tp_iter */
85 0, /* tp_iternext */
86 0, /* tp_methods */
87 0, /* tp_members */
88 0, /* tp_getset */
89 0, /* tp_base */
90 0, /* tp_dict */
91 0, /* tp_descr_get */
92 0, /* tp_descr_set */
93 0, /* tp_dictoffset */
94 0, /* tp_init */
95 0, /* tp_alloc */
96 /* deliberately not callable! */
97 0, /* tp_new */
100 /* Internal C API for Connection, Bus, Server ======================= */
102 dbus_bool_t
103 dbus_py_check_mainloop_sanity(PyObject *mainloop)
105 if (NativeMainLoop_Check(mainloop)) {
106 return TRUE;
108 PyErr_SetString(PyExc_TypeError,
109 "A dbus.mainloop.NativeMainLoop instance is required");
110 return FALSE;
113 dbus_bool_t
114 dbus_py_set_up_connection(PyObject *conn, PyObject *mainloop)
116 if (NativeMainLoop_Check(mainloop)) {
117 /* Native mainloops are allowed to do arbitrary strange things */
118 NativeMainLoop *nml = (NativeMainLoop *)mainloop;
119 DBusConnection *dbc = DBusPyConnection_BorrowDBusConnection(conn);
121 if (!dbc) {
122 return FALSE;
124 return (nml->set_up_connection_cb)(dbc, nml->data);
126 PyErr_SetString(PyExc_TypeError,
127 "A dbus.mainloop.NativeMainLoop instance is required");
128 return FALSE;
131 /* C API ============================================================ */
133 PyObject *
134 DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb)(DBusConnection *, void *),
135 dbus_bool_t (*server_cb)(DBusServer *, void *),
136 void (*free_cb)(void *),
137 void *data)
139 NativeMainLoop *self = PyObject_New(NativeMainLoop, &NativeMainLoop_Type);
140 if (self) {
141 self->data = data;
142 self->free_cb = free_cb;
143 self->set_up_connection_cb = conn_cb;
144 self->set_up_server_cb = server_cb;
146 return (PyObject *)self;
149 /* Null mainloop implementation ===================================== */
151 static dbus_bool_t
152 noop_main_loop_cb(void *conn_or_server UNUSED, void *data UNUSED)
154 return TRUE;
157 #define noop_conn_cb ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb))
158 #define noop_server_cb ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb))
160 /* Initialization =================================================== */
162 dbus_bool_t
163 dbus_py_init_mainloop(void)
165 if (PyType_Ready (&NativeMainLoop_Type) < 0) return 0;
167 return 1;
170 dbus_bool_t
171 dbus_py_insert_mainloop_types(PyObject *this_module)
173 PyObject *null_main_loop = DBusPyNativeMainLoop_New4(noop_conn_cb,
174 noop_server_cb,
175 NULL,
176 NULL);
177 if (!null_main_loop) return 0;
179 if (PyModule_AddObject (this_module, "NativeMainLoop",
180 (PyObject *)&NativeMainLoop_Type) < 0) return 0;
181 if (PyModule_AddObject (this_module, "NULL_MAIN_LOOP",
182 null_main_loop) < 0) return 0;
183 return 1;
186 /* vim:set ft=c cino< sw=4 sts=4 et: */