Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:42:24+00:00 2026-05-28T14:42:24+00:00

I have a lightweight application that catches Xorg and dbus events. In order to

  • 0

I have a lightweight application that catches Xorg and dbus events. In order to do this I initialized dbus loop and started g_main_loop, but I don’t know how to add Xorg event handling in a 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 is waiting X-event and handle them.

Both ways seems bad, first because I have unneeded calls for checking event, second because I make an additional thread and have to make additional locks.

P.S. I know I can use gtk lib, but I don’t want to have dependencies on any toolkit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:42:25+00:00Added an answer on May 28, 2026 at 2:42 pm

    If you want to add Xorg event handling to the main loop without using a timeout (which as you state is wasteful), you’ll need to add a source that polls the X connection. For that, you’ll need to get below the Xlib abstraction layer to get the underlying X connection file descriptor. That’s what the complete program below does. It is an adaptation of C. Tronche’s excellent X11 tutorial to use the glib main loop for polling. I also drew from “Foundations of GTK+ Development” by Andrew Krause.

    If this doesn’t seem very “natural”, that’s because I doubt there is a very “natural” way to do this – you’re really re-implementing a core part of GDK here.

    /* needed to break into 'Display' struct internals. */
    #define XLIB_ILLEGAL_ACCESS
    
    #include <X11/Xlib.h> // Every Xlib program must include this
    #include <assert.h>   // I include this to test return values the 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;
      return FALSE;
    }
    
    static gboolean
    x11_fd_check (GSource *source)
    {
      return 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);
        }
    
      return TRUE;
    }
    
    static gboolean
    msg_beacon(gpointer data)
    {
      static gint counter = 0;
      g_print("Beacon %d\n", ++counter);
      return 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);
    
          for (;;) {
        XEvent e;
        XNextEvent(dpy, &e);
        if (e.type == MapNotify)
          break; 
          }
    
          GMainLoop *mainloop = NULL;
          mainloop = g_main_loop_new(NULL, FALSE);
    
          /* beacon to 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);
    
          return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an administrative page in a web application that resets the cache, but
We have an application that uses Active Directory for its Authentication. This includes Kerberos
I'm developing a lightweight shopping cart but have become stumped with products that have
I have a small lightweight application that is used as part of a larger
I have a J2EE application that receives and process messages (events). These messages contain
In my C++ application, I have Pod objects that internally stores information about Monkey
We have a Java application that stores RSA public keys and allows a user
I'm writing an application that'll display a lightweight dashboard to the computer's secondary display
I have currently have a web application that caches a large amount of data
I have an efficient C# application that receives 80 bytes of data at a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.