File: ffi.py

package info (click to toggle)
xcffib 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: python: 2,293; haskell: 915; xml: 680; makefile: 84; sh: 14
file content (246 lines) | stat: -rw-r--r-- 8,950 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright 2014 Tycho Andersen
# Copyright 2014 Sean Vig
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from cffi import FFI

CONSTANTS = [
    ("X_PROTOCOL", 11),
    ("X_PROTOCOL_REVISION", 0),
    ("X_TCP_PORT", 6000),

    ("XCB_NONE", 0),
    ("XCB_COPY_FROM_PARENT", 0),
    ("XCB_CURRENT_TIME", 0),
    ("XCB_NO_SYMBOL", 0),

    ("XCB_CONN_ERROR", 1),
    ("XCB_CONN_CLOSED_EXT_NOTSUPPORTED", 2),
    ("XCB_CONN_CLOSED_MEM_INSUFFICIENT", 3),
    ("XCB_CONN_CLOSED_REQ_LEN_EXCEED", 4),
    ("XCB_CONN_CLOSED_PARSE_ERR", 5),
    ("XCB_CONN_CLOSED_INVALID_SCREEN", 6),
    ("XCB_CONN_CLOSED_FDPASSING_FAILED", 7),

    ("XCB_REQUEST_CHECKED", 1 << 0)
]


# constants
CDEF = '\n'.join("#define %s %d" % (c, v) for c, v in CONSTANTS)

# types
CDEF += """
    // xcb.h
    typedef struct {
        uint8_t  response_type;  /**< Type of the response */
        uint8_t  pad0;           /**< Padding */
        uint16_t sequence;       /**< Sequence number */
        uint32_t length;         /**< Length of the response */
    } xcb_generic_reply_t;

    typedef struct {
        uint8_t  response_type;  /**< Type of the response */
        uint8_t  pad0;           /**< Padding */
        uint16_t sequence;       /**< Sequence number */
        uint32_t pad[7];         /**< Padding */
        uint32_t full_sequence;  /**< full sequence */
    } xcb_generic_event_t;

    typedef struct {
        uint8_t  response_type;  /**< Type of the response */
        uint8_t  error_code;     /**< Error code */
        uint16_t sequence;       /**< Sequence number */
        uint32_t resource_id;     /** < Resource ID for requests with side effects only */
        uint16_t minor_code;      /** < Minor opcode of the failed request */
        uint8_t  major_code;       /** < Major opcode of the failed request */
        uint8_t  pad0;
        uint32_t pad[5];         /**< Padding */
        uint32_t full_sequence;  /**< full sequence */
    } xcb_generic_error_t;

    typedef struct {
        unsigned int sequence;  /**< Sequence number */
    } xcb_void_cookie_t;

    typedef struct xcb_auth_info_t {
        int   namelen;
        char *name;
        int   datalen;
        char *data;
    } xcb_auth_info_t;

    typedef ... xcb_connection_t;

    // xproto.h
    typedef uint32_t xcb_colormap_t;
    typedef uint32_t xcb_drawable_t;
    typedef uint32_t xcb_pixmap_t;
    typedef uint32_t xcb_visualid_t;
    typedef uint32_t xcb_window_t;

    typedef struct xcb_query_extension_reply_t {
        uint8_t  response_type;
        uint8_t  pad0;
        uint16_t sequence;
        uint32_t length;
        uint8_t  present;
        uint8_t  major_opcode;
        uint8_t  first_event;
        uint8_t  first_error;
    } xcb_query_extension_reply_t;

    typedef struct xcb_setup_t {
        uint8_t       status; /**<  */
        uint8_t       pad0; /**<  */
        uint16_t      protocol_major_version; /**<  */
        uint16_t      protocol_minor_version; /**<  */
        uint16_t      length; /**<  */
        uint32_t      release_number; /**<  */
        uint32_t      resource_id_base; /**<  */
        uint32_t      resource_id_mask; /**<  */
        uint32_t      motion_buffer_size; /**<  */
        uint16_t      vendor_len; /**<  */
        uint16_t      maximum_request_length; /**<  */
        uint8_t       roots_len; /**<  */
        uint8_t       pixmap_formats_len; /**<  */
        uint8_t       image_byte_order; /**<  */
        uint8_t       bitmap_format_bit_order; /**<  */
        uint8_t       bitmap_format_scanline_unit; /**<  */
        uint8_t       bitmap_format_scanline_pad; /**<  */
        uint8_t       min_keycode; /**<  */
        uint8_t       max_keycode; /**<  */
        uint8_t       pad1[4]; /**<  */
    } xcb_setup_t;

    typedef struct xcb_visualtype_t {
        xcb_visualid_t visual_id; /**<  */
        uint8_t        _class; /**<  */
        uint8_t        bits_per_rgb_value; /**<  */
        uint16_t       colormap_entries; /**<  */
        uint32_t       red_mask; /**<  */
        uint32_t       green_mask; /**<  */
        uint32_t       blue_mask; /**<  */
        uint8_t        pad0[4]; /**<  */
    } xcb_visualtype_t;

    typedef struct xcb_screen_t {
        xcb_window_t   root; /**<  */
        xcb_colormap_t default_colormap; /**<  */
        uint32_t       white_pixel; /**<  */
        uint32_t       black_pixel; /**<  */
        uint32_t       current_input_masks; /**<  */
        uint16_t       width_in_pixels; /**<  */
        uint16_t       height_in_pixels; /**<  */
        uint16_t       width_in_millimeters; /**<  */
        uint16_t       height_in_millimeters; /**<  */
        uint16_t       min_installed_maps; /**<  */
        uint16_t       max_installed_maps; /**<  */
        xcb_visualid_t root_visual; /**<  */
        uint8_t        backing_stores; /**<  */
        uint8_t        save_unders; /**<  */
        uint8_t        root_depth; /**<  */
        uint8_t        allowed_depths_len; /**<  */
    } xcb_screen_t;

    typedef struct xcb_screen_iterator_t {
        xcb_screen_t *data; /**<  */
        int           rem; /**<  */
        int           index; /**<  */
    } xcb_screen_iterator_t;

    xcb_screen_iterator_t
    xcb_setup_roots_iterator (const xcb_setup_t *R  /**< */);

    void
    xcb_screen_next (xcb_screen_iterator_t *i  /**< */);

    // render.h
    typedef uint32_t xcb_render_pictformat_t;

    typedef struct xcb_render_directformat_t {
        uint16_t red_shift; /**<  */
        uint16_t red_mask; /**<  */
        uint16_t green_shift; /**<  */
        uint16_t green_mask; /**<  */
        uint16_t blue_shift; /**<  */
        uint16_t blue_mask; /**<  */
        uint16_t alpha_shift; /**<  */
        uint16_t alpha_mask; /**<  */
    } xcb_render_directformat_t;

    typedef struct xcb_render_pictforminfo_t {
        xcb_render_pictformat_t   id; /**<  */
        uint8_t                   type; /**<  */
        uint8_t                   depth; /**<  */
        uint8_t                   pad0[2]; /**<  */
        xcb_render_directformat_t direct; /**<  */
        xcb_colormap_t            colormap; /**<  */
    } xcb_render_pictforminfo_t;

    // xcbext.h
    typedef struct xcb_extension_t {
        const char *name;
        int global_id;
    } xcb_extension_t;

    typedef struct {
        size_t count;
        xcb_extension_t *ext;
        uint8_t opcode;
        uint8_t isvoid;
    } xcb_protocol_request_t;

    // sys/uio.h
    struct iovec
    {
      void *iov_base; /* BSD uses caddr_t (1003.1g requires void *) */
      size_t iov_len; /* Must be size_t (1003.1g) */
    };

    // need to manually free some things that XCB allocates
    void free(void *ptr);
"""

# connection manipulation, mostly generated with:
# grep -v '^[ \/\}#]' xcb.h | grep -v '^typedef' | grep -v '^extern'
CDEF += """
    int xcb_flush(xcb_connection_t *c);
    uint32_t xcb_get_maximum_request_length(xcb_connection_t *c);
    void xcb_prefetch_maximum_request_length(xcb_connection_t *c);
    xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c);
    xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c);
    const xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext);
    const xcb_setup_t *xcb_get_setup(xcb_connection_t *c);
    int xcb_get_file_descriptor(xcb_connection_t *c);
    int xcb_connection_has_error(xcb_connection_t *c);
    xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info);
    void xcb_disconnect(xcb_connection_t *c);
    int xcb_parse_display(const char *name, char **host, int *display, int *screen);
    xcb_connection_t *xcb_connect(const char *displayname, int *screenp);
    xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *display, xcb_auth_info_t *auth, int *screen);
    uint32_t xcb_generate_id(xcb_connection_t *c);
    xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie);
"""

CDEF += """
    unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *request);
    void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e);
    int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error);
    void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence);
"""


ffi = FFI()
ffi.cdef(CDEF)