Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi... I want to write a simple graphics library that works on top of XLib(as a term project). And I want to provide an abstract keyboard and mouse handling support. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    8

    Question Writing callback functions in XLib


    Hi...
    I want to write a simple graphics library that works on top of XLib(as a term project). And I want to provide an abstract keyboard and mouse handling support. But XLib handles these as events, so a typical event-handling routine would be:

    Code:
    XEvent event;
    while(1)
    {
      XNextEvent( display, &event );
      switch( event )
      {
        case ButtonPress:
          // stuff
       .
       .
      }
    }
    I can't use code like this in my library, so I think I will have to write a callback function for the events. So how do I accomplish this? Or is there a way without using callback functions?

  2. #2
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    The Hot Humid South
    Posts
    602
    Well, I'm not sure if this post is acceptable to forum rules seeing as it is for homework, so I'll wait until a moderator replies to it before I post anything else.

    EDIT: Either way, I do think I'm allowed to post this (great resource, and how I first learned Xlib):
    http://users.actcom.co.il/~choo/lupg...ogramming.html
    "Today you are freer than ever to do what you want, provided you can pay for it!" --Bad Religion

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    8

    No, not a homework

    No, this is not a homework... I am supposed to do any project, I thought up of this... And right now, I've hit a roadblock.... if I can't do this, I'll have to move on to some other project, so please help!!
    @bidi : I've been through that tutorial, that doesn't answer my question.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    If you want to implement callbacks in C, I'd recommend you use function pointers.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  6. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    8
    Quote Originally Posted by Javasnob
    If you want to implement callbacks in C, I'd recommend you use function pointers.
    Of couse, I have to use function pointers, but how do I get XLib to call that function ( or any function for that matter ) when the event occurs??

  7. #6
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    You don't. With Xlib, you have to poll for events. However, this isn't the interface your library has to expose to your clients, you could take a hint from GTK and do something like this:
    Code:
    void MyLibraryXLoop(void) {
      XEvent ev;
      while(1) {
         XNextEvent(dsp, &ev);
         switch(ev.type) {
             // Call relevant callback
         }
      }
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  8. #7
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    The Hot Humid South
    Posts
    602
    Well, you HAVE to use an event loop in order to get it to work, no matter what. In your case, you can try:
    Code:
    XEvent event;
    while(1)
    {
      XNextEvent( display, &event );
      switch( event )
      {
        case ButtonPress:
          handleButtonPress()
       .
       .
      }
    }
    Instead of using XNextEvent you can also use XCheckWindowEvent() to pull a particular event.
    "Today you are freer than ever to do what you want, provided you can pay for it!" --Bad Religion

  9. #8
    Just Joined!
    Join Date
    Apr 2007
    Posts
    8

    Arrow

    Quote Originally Posted by Javasnob
    You don't. With Xlib, you have to poll for events. However, this isn't the interface your library has to expose to your clients, you could take a hint from GTK and do something like this:
    Code:
    void MyLibraryXLoop(void) {
      XEvent ev;
      while(1) {
         XNextEvent(dsp, &ev);
         switch(ev.type) {
             // Call relevant callback
         }
      }
    That's a good idea, but once I write a while(1) loop in my function, the user code from that point ahead won't be executed, right?

  10. #9
    Just Joined!
    Join Date
    Apr 2007
    Posts
    8

    Post

    Think I got my answer. No callbacks, I'll provide an abstraction of the event loop.
    The user code would like:
    Code:
    while(1)
    {
       myEvent = MyGetEvent( );   // Uses XCheckNextEvent or something
       switch( myEvent )
       {
           case MOUSE_LBUTTON:
                   // stuff
           case KEYPRESS:
                   
       }
    }
    Guess that can be done.
    Thanks everybody!!

  11. #10
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    As per my understanding, you can accomplish this with help of "libdl" library.

    It provides the api to read the Function addresses you are interested in.

    For more information read the manual pages for dlopen(), dlsym etc..

    --Regards,
    rajesh

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •