3 #include <lua5.1/lua.h>
4 #include <lua5.1/lauxlib.h>
8 #define MT_DISPLAY "XCB.display"
11 extern void push_ERROR(lua_State
*L
, xcb_generic_error_t
*e
);
13 typedef void (*eventFunc
)(lua_State
*L
, xcb_generic_event_t
*);
14 eventFunc push_event
[256] = { NULL
};
16 void RegisterEvent(int index
, eventFunc func
)
18 push_event
[index
] = func
;
21 static void PushEvent(lua_State
*L
, xcb_generic_event_t
*event
)
24 lua_pushinteger(L
, event
->response_type
);
25 lua_setfield(L
, -2, "response_type");
26 lua_pushinteger(L
, event
->sequence
);
27 lua_setfield(L
, -2, "sequence");
28 lua_pushinteger(L
, event
->full_sequence
);
29 lua_setfield(L
, -2, "full_sequence");
31 if (push_event
[event
->response_type
])
32 push_event
[event
->response_type
](L
, event
);
35 static int Connect(lua_State
*L
)
39 const char *name
= NULL
;
42 name
= lua_tostring(L
, 1);
43 c
= xcb_connect(name
, &screen
);
47 lua_newuserdata(L
, sizeof(xcb_connection_t
*));
48 ((xcb_connection_t
**)lua_touserdata(L
, -1))[0] = c
;
49 luaL_getmetatable(L
, MT_DISPLAY
);
50 lua_setmetatable(L
, -2);
55 lua_pushinteger(L
, screen
);
59 static int gc_connection(lua_State
*L
)
62 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
65 lua_getfield(L
, -1, "closed");
66 if (!lua_toboolean(L
, -1))
72 static int Disconnect(lua_State
*L
)
75 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
78 lua_getfield(L
, -1, "closed");
79 if (lua_toboolean(L
, -1))
80 luaL_error(L
, "Error: already disconnected");
82 lua_pushboolean(L
, 1);
83 lua_setfield(L
, -3, "closed");
89 static int Flush(lua_State
*L
)
92 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
94 lua_pushinteger(L
, xcb_flush(c
));
98 static int GenerateID(lua_State
*L
)
100 lua_pushinteger(L
, xcb_generate_id(((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0]));
106 extern void push_Setup(lua_State
*L
, const xcb_setup_t
*x
); //Temporary until I get headers sorted out
108 static int GetSetup(lua_State
*L
)
111 const xcb_setup_t
*setup
;
113 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
116 lua_getfield(L
, -1, "closed");
117 if (lua_toboolean(L
, -1))
118 luaL_error(L
, "Error: already disconnected");
120 setup
= xcb_get_setup(c
);
122 luaL_error(L
, "Error: connection failed");
124 push_Setup(L
, setup
);
128 static int PollForEvent(lua_State
*L
)
131 xcb_generic_event_t
*e
;
133 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
136 lua_getfield(L
, -1, "closed");
137 if (lua_toboolean(L
, -1))
138 luaL_error(L
, "Error: already disconnected");
140 e
= xcb_poll_for_event(c
);
143 else if (!e
->response_type
)
144 push_ERROR(L
, (xcb_generic_error_t
*)e
);
154 static int WaitForEvent(lua_State
*L
)
157 xcb_generic_event_t
*e
;
159 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
162 lua_getfield(L
, -1, "closed");
163 if (lua_toboolean(L
, -1))
164 luaL_error(L
, "Error: already disconnected");
166 e
= xcb_wait_for_event(c
);
169 else if (!e
->response_type
)
170 push_ERROR(L
, (xcb_generic_error_t
*)e
);
180 static int HasError(lua_State
*L
)
183 c
= ((xcb_connection_t
**)luaL_checkudata(L
, 1, MT_DISPLAY
))[0];
186 lua_getfield(L
, -1, "closed");
187 if (lua_toboolean(L
, -1))
188 luaL_error(L
, "Error: already disconnected");
190 lua_pushboolean(L
, xcb_connection_has_error(c
));
196 static luaL_Reg lxcb
[] = {
197 { "connect", Connect
},
201 static luaL_Reg display_m
[] = {
202 { "__gc", gc_connection
},
203 { "disconnect", Disconnect
},
205 { "generate_id", GenerateID
},
206 { "get_setup", GetSetup
},
207 { "poll_for_event", PollForEvent
},
208 { "wait_for_event", WaitForEvent
},
209 { "has_error", HasError
},
211 { "connect_to_display_with_auth_info", ConnectToDisplayWithAuthInfo },
212 { "connect_to_fd", ConnectToFD },
213 { "get_file_descriptor", GetFileDescriptor },
214 { "get_maximum_request_length", GetMaximumRequestLength },
215 { "parse_display", ParseDisplay },
216 { "prefetch_extension_data", PrefetchExtensionData },
217 { "prefetch_maximum_request_length", PrefetchMaximumRequestLength },
218 { "query_extension_reply", QueryExtensionReply },
219 { "request_check", RequestCheck },
225 #define DLLEXPORT __declspec(dllexport)
227 /* Must be gcc if not MSC */
228 #define DLLEXPORT __attribute__((visibility("default")))
231 extern void init_xproto(lua_State
*L
); // Temporary, until I get headers sorted.
233 DLLEXPORT
int luaopen_lxcb(lua_State
*L
)
235 luaL_register(L
, "lxcb", lxcb
);
237 luaL_newmetatable(L
, MT_DISPLAY
);
238 lua_pushvalue(L
, -1);
239 lua_setfield(L
, -2, "__index");
240 luaL_register(L
, NULL
, display_m
);