diff options
author | Eamon Walsh <ewalsh@tycho.nsa.gov> | 2009-04-07 23:43:37 -0400 |
---|---|---|
committer | Eamon Walsh <ewalsh@tycho.nsa.gov> | 2009-04-07 23:43:37 -0400 |
commit | 18c98cebf34c1d88761acd1f2c191b8880cf2088 (patch) | |
tree | 32d4c942f85be3b6181d2359bc2b35ec478e8ee9 | |
parent | e3176b21a839e8bfa3ec4e56b1462839b7ea2d4c (diff) |
Add a wrap() function to create a Connection object from a raw C pointer.
This is necessary to work with xcb_connection_t pointers obtained with a
ctype call to XGetXCBConnection() for making XCB calls on a previously
opened display, for example by GtkPython.
Signed-off-by: Eamon Walsh <ewalsh@tycho.nsa.gov>
-rw-r--r-- | src/conn.c | 3 | ||||
-rw-r--r-- | src/conn.h | 1 | ||||
-rw-r--r-- | src/module.c | 45 |
3 files changed, 48 insertions, 1 deletions
@@ -46,6 +46,7 @@ xpybConn_create(PyObject *core_type) if (self->extcache == NULL) goto err; + self->wrapped = 0; self->setup = NULL; self->events = NULL; self->events_len = 0; @@ -189,7 +190,7 @@ xpybConn_dealloc(xpybConn *self) Py_CLEAR(self->setup); Py_CLEAR(self->extcache); - if (self->conn) + if (self->conn && !self->wrapped) xcb_disconnect(self->conn); for (i = 0; i < self->events_len; i++) @@ -4,6 +4,7 @@ typedef struct { PyObject_HEAD xcb_connection_t *conn; + int wrapped; PyObject *dict; int pref_screen; PyObject *core; diff --git a/src/module.c b/src/module.c index f97fc80..6ef4644 100644 --- a/src/module.c +++ b/src/module.c @@ -115,6 +115,46 @@ err: } static PyObject * +xpyb_wrap(PyObject *self, PyObject *args) +{ + PyObject *obj; + void *raw; + xpybConn *conn; + + /* Make sure core was set. */ + if (xpybModule_core == NULL) { + PyErr_SetString(xpybExcept_base, "No core protocol object has been set. Did you import xcb.xproto?"); + return NULL; + } + + /* Parse arguments and allocate new connection object */ + if (!PyArg_ParseTuple(args, "O", &obj)) + return NULL; + + conn = xpybConn_create((PyObject *)xpybModule_core); + if (conn == NULL) + return NULL; + + /* Get our pointer */ + raw = PyLong_AsVoidPtr(obj); + if (!raw || PyErr_Occurred()) { + PyErr_SetString(xpybExcept_base, "Bad pointer value passed to wrap()."); + goto err; + } + + conn->conn = raw; + conn->wrapped = 1; + + /* Load extensions */ + if (xpybConn_setup(conn) < 0) + goto err; + + return (PyObject *)conn; +err: + Py_DECREF(conn); + return NULL; +} +static PyObject * xpyb_add_core(PyObject *self, PyObject *args) { PyTypeObject *value, *setup; @@ -217,6 +257,11 @@ static PyMethodDef XCBMethods[] = { METH_VARARGS | METH_KEYWORDS, "Connects to the X server." }, + { "wrap", + (PyCFunction)xpyb_wrap, + METH_VARARGS, + "Wraps an existing XCB connection pointer." }, + { "popcount", (PyCFunction)xpyb_popcount, METH_VARARGS, |