Re: gdk events - cunsumes much CPU time



Am 27 Jun 2003 15:08:38 -0400 schrieb Owen Taylor:

On Fri, 2003-06-27 at 16:29, Andreas Volz wrote:
Hi,

I used this in a gdk app to get the events:

for (;;)
  {
    if ((event=gdk_event_get()))
    {
      if (event->type == GDK_BUTTON_PRESS)
      {
        switch (event->button.button)
        {
          /* left button pressed */
        case 1:
          start_app ("xterm");
          break;

          /* middle button pressed */
        case 2:
          start_app ("gExec2");
          break;

          /* right button pressed */
        case 3:
          start_app ("xsteak");
          break;
        }

        printf("button press: button = %d\n", event->button.button);
      }
    }
  }

It works like it should, but there is a little problem. My app
cunsumes more than 95% CPU time and this is to much ;-)

Where is the problem? Is perhaps gdk_event_get() not the correct
function for this problem?

What are you trying to do? Are you trying to write a GDK app
without using GTK+? Without using the GLib main loop...?

Yes, it's a pure gdk app with only gdk_init() at the moment. I don't
know how to use the glib main loop. Could you give me a small example
how to create a glib main loop and how to catch gdk events other than I
tried to do?

This is my current full main code:
int main(int argc, char **argv)
{
  GdkWindow *root;
  GdkEvent *event;

  gdk_init(&argc, &argv);

  root = gdk_window_foreign_new (gdk_x11_get_default_root_xwindow());
  gdk_window_set_events (root, GDK_BUTTON_PRESS_MASK);

  for (;;)
  {
    if ((event=gdk_event_get()))
    {
      if (event->type == GDK_BUTTON_PRESS)
      {         
        switch (event->button.button)
        {
          /* left button pressed */
        case 1:
          start_app ("xterm");
          break;
          
          /* middle button pressed */
        case 2:
          //if (event->button.state & GDK_CONTROL_MASK)
          start_app ("gExec2");
          break;
          
          /* right button pressed */
        case 3:
          start_app ("xsteak");
          break;
        }
                
        printf("button press: button = %d\n", event->button.button);    
      }
    }
    usleep (1 * 1000);
  }

  return 0;
}

thanks
Andreas



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]