c - how to add Xorg event handing in g_main_loop -



c - how to add Xorg event handing in g_main_loop -

i have a lightweight application catches xorg , dbus events. in order initialized dbus loop , started g_main_loop, don't know how add together xorg event handling in natural way:

gmainloop * mainloop = null; mainloop = g_main_loop_new(null,false); dbus_g_thread_init (); dbus_init(); // <<<<<<<<<<<<<<<<<<<<<<<<< //1 way using timeout //g_timeout_add(100, kbdd_default_iter, mainloop); //2nd way using pthread //gthread * t = g_thread_create(kbdd_default_loop, null, false, null); //>>>>>>>>>>>>>>>>>>>>>>>>>>> g_main_loop_run(mainloop);

in default iter i'm checking if there waiting x-event , handle them.

both ways seems bad, first because have unneeded calls checking event, sec because create additional thread , have create additional locks.

p.s. know can utilize gtk lib, don't want have dependencies on toolkit.

if want add together xorg event handling main loop without using timeout (which state wasteful), you'll need add together source polls x connection. that, you'll need below xlib abstraction layer underlying x connection file descriptor. that's finish programme below does. adaptation of c. tronche's first-class x11 tutorial utilize glib main loop polling. drew "foundations of gtk+ development" andrew krause.

if doesn't seem "natural", that's because uncertainty there "natural" way - you're re-implementing core part of gdk here.

/* needed break 'display' struct internals. */ #define xlib_illegal_access #include <x11/xlib.h> // every xlib programme must include #include <assert.h> // include test homecoming values lazy way #include <glib.h> typedef struct _x11_source { gsource source; display *dpy; window w; } x11_source_t; static gboolean x11_fd_prepare(gsource *source, gint *timeout) { *timeout = -1; homecoming false; } static gboolean x11_fd_check (gsource *source) { homecoming true; } static gboolean x11_fd_dispatch(gsource* source, gsourcefunc callback, gpointer user_data) { static gint counter = 0; display *dpy = ((x11_source_t*)source)->dpy; window window = ((x11_source_t*)source)->w; xevent e; while (xcheckwindowevent(dpy, window, enterwindowmask, &e)) { if (e.type == enternotify) g_print("we're in!!! (%d)\n", ++counter); } homecoming true; } static gboolean msg_beacon(gpointer data) { static gint counter = 0; g_print("beacon %d\n", ++counter); homecoming true; } int main() { display *dpy = xopendisplay(null); assert(dpy); int blackcolor = blackpixel(dpy, defaultscreen(dpy)); int whitecolor = whitepixel(dpy, defaultscreen(dpy)); window w = xcreatesimplewindow(dpy, defaultrootwindow(dpy), 0, 0, 200, 100, 0, blackcolor, blackcolor); xselectinput(dpy, w, structurenotifymask | enterwindowmask); xmapwindow(dpy, w); (;;) { xevent e; xnextevent(dpy, &e); if (e.type == mapnotify) break; } gmainloop *mainloop = null; mainloop = g_main_loop_new(null, false); /* beacon demonstrate we're not blocked. */ g_timeout_add(300, msg_beacon, mainloop); gpollfd dpy_pollfd = {dpy->fd, g_io_in | g_io_hup | g_io_err, 0}; gsourcefuncs x11_source_funcs = { x11_fd_prepare, x11_fd_check, x11_fd_dispatch, null, /* finalize */ null, /* closure_callback */ null /* closure_marshal */ }; gsource *x11_source = g_source_new(&x11_source_funcs, sizeof(x11_source_t)); ((x11_source_t*)x11_source)->dpy = dpy; ((x11_source_t*)x11_source)->w = w; g_source_add_poll(x11_source, &dpy_pollfd); g_source_attach(x11_source, null); g_main_loop_run(mainloop); homecoming 0; }

c glib

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -