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.
- 04-17-2007 #1Just Joined!
- Join Date
- Apr 2007
- Posts
- 8
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:
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?Code:XEvent event; while(1) { XNextEvent( display, &event ); switch( event ) { case ButtonPress: // stuff . . } }
- 04-17-2007 #2Linux 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
- 04-18-2007 #3Just 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.
- 04-19-2007 #4
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
- 04-19-2007 #5Just Joined!
- Join Date
- Apr 2007
- Posts
- 8
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??
Originally Posted by Javasnob
- 04-19-2007 #6
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
- 04-19-2007 #7Linux 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:
Instead of using XNextEvent you can also use XCheckWindowEvent() to pull a particular event.Code:XEvent event; while(1) { XNextEvent( display, &event ); switch( event ) { case ButtonPress: handleButtonPress() . . } }"Today you are freer than ever to do what you want, provided you can pay for it!" --Bad Religion
- 04-20-2007 #8Just Joined!
- Join Date
- Apr 2007
- Posts
- 8
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?
Originally Posted by Javasnob
- 04-20-2007 #9Just Joined!
- Join Date
- Apr 2007
- Posts
- 8
Think I got my answer. No callbacks, I'll provide an abstraction of the event loop.
The user code would like:
Guess that can be done.Code:while(1) { myEvent = MyGetEvent( ); // Uses XCheckNextEvent or something switch( myEvent ) { case MOUSE_LBUTTON: // stuff case KEYPRESS: } }
Thanks everybody!!
- 04-20-2007 #10Just 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


Reply With Quote
