summaryrefslogtreecommitdiff
path: root/src/cookie.c
blob: db0a20f9ea34f41aeae946b03bc5b8797ac3788f (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
#include "module.h"
#include "except.h"
#include "conn.h"
#include "cookie.h"
#include "error.h"
#include "reply.h"

/*
 * Helpers
 */


/*
 * Infrastructure
 */

static PyObject *
xpybCookie_new(PyTypeObject *self, PyObject *args, PyObject *kw)
{
    return PyType_GenericNew(self, args, kw);
}

static void
xpybCookie_dealloc(xpybCookie *self)
{
    Py_CLEAR(self->reply_type);
    Py_CLEAR(self->request);
    Py_CLEAR(self->conn);
    self->ob_type->tp_free((PyObject *)self);
}


/*
 * Members
 */


/*
 * Methods
 */

static PyObject *
xpybCookie_check(xpybCookie *self, PyObject *args)
{
    xcb_generic_error_t *error;

    if (!(self->request->is_void && self->request->is_checked)) {
	PyErr_SetString(xpybExcept_base, "Request is not void and checked.");
	return NULL;
    }
    if (xpybConn_invalid(self->conn))
	return NULL;

    error = xcb_request_check(self->conn->conn, self->cookie);
    if (xpybError_set(self->conn, error))
	return NULL;

    Py_RETURN_NONE;
}

static PyObject *
xpybCookie_reply(xpybCookie *self, PyObject *args)
{
    xcb_generic_error_t *error;
    xcb_generic_reply_t *data;
    PyObject *shim, *reply;

    /* Check arguments and connection. */
    if (self->request->is_void) {
	PyErr_SetString(xpybExcept_base, "Request has no reply.");
	return NULL;
    }
    if (xpybConn_invalid(self->conn))
	return NULL;

    /* Make XCB call */
    data = xcb_wait_for_reply(self->conn->conn, self->cookie.sequence, &error);
    if (xpybError_set(self->conn, error))
	return NULL;
    if (data == NULL) {
	PyErr_SetString(PyExc_IOError, "I/O error on X server connection.");
	return NULL;
    }

    /* Create a shim protocol object */
    shim = PyBuffer_FromMemory(data, 32 + data->length * 4);
    if (shim == NULL)
	goto err1;

    /* Call the reply type object to get a new xcb.Reply instance */
    reply = PyObject_CallFunctionObjArgs((PyObject *)self->reply_type, shim, NULL);
    Py_DECREF(shim);
    return reply;
err1:
    free(data);
    return NULL;
}

static PyMethodDef xpybCookie_methods[] = {
    { "check",
      (PyCFunction)xpybCookie_check,
      METH_NOARGS,
      "Raise an error if one occurred on the request." },

    { "reply",
      (PyCFunction)xpybCookie_reply,
      METH_NOARGS,
      "Return the reply or raise an error." },

    { NULL } /* terminator */
};


/*
 * Definition
 */

PyTypeObject xpybCookie_type = {
    PyObject_HEAD_INIT(NULL)
    .tp_name = "xcb.Cookie",
    .tp_basicsize = sizeof(xpybCookie),
    .tp_new = xpybCookie_new,
    .tp_dealloc = (destructor)xpybCookie_dealloc,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_doc = "XCB generic cookie object",
    .tp_methods = xpybCookie_methods
};


/*
 * Module init
 */
int xpybCookie_modinit(PyObject *m)
{
    if (PyType_Ready(&xpybCookie_type) < 0)
        return -1;
    Py_INCREF(&xpybCookie_type);
    if (PyModule_AddObject(m, "Cookie", (PyObject *)&xpybCookie_type) < 0)
	return -1;

    return 0;
}