Thursday, January 31, 2008

Extension Revisited For Linux: gluttonCloseRequestedFunc

A few days ago I posted that I had written a glutton extension for the GLUT api called gluttonCloseRequestedFunc. The intended purpose of this function is to provide a callback function that is called when a user has decided to close a window. During this function, the application has the opportunity to do things like display a dialog box to ask the user to confirm the close. In the event the callback function determines that the close should be canceled, it can set the parameter "*Action" to the value gluttonCloseRequestedIgnore, and glutton will then ignore the close request rather than close the window.

At the time, I had only implemented the extension for Windows, and promised to revisit this one for Linux. As it turns out, implementing it on Linux was extremely simple, because the Linux implementation of freeglut already had 99% of what was necessary to get this to work. This means that it is very likely that this will work for other *nix/X11 implementations with no additional work, but I don't have access to such environments, so I cannot tell with certainty.

Anyway, as I said, the implementation is very simple. I merely copied the Windows portion that does the callback into glutMainLoopEvent's handler for ClientMessage events. More specifically, for fgDisplay.DeleteWindow ClientMessage events:



case ClientMessage:
/* Destroy the window when the WM_DELETE_WINDOW message arrives */
if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
{
GETWINDOW( xclient );
#ifdef GLUTTON
{
int Action = gluttonCloseRequestedDefault;
INVOKE_WCB( *window , GluttonCloseRequested , ( &Action ) );
switch( Action )
{
case gluttonCloseRequestedDefault: break;
/* proceed to close the window (fgDestroyWindow, below) */
case gluttonCloseRequestedIgnore: return;
/* ignore the message. */
}
}
#endif

fgDestroyWindow ( window );



I honestly didn't expect this to be as easy as it was. I was expecting to have to go through the pain of telling X that I wanted it to send a ClientMessage to glutton, and to deal with finding all of the "atoms" involved. Many thanks to the freeglut developers for relieving me of /that/ burden!

Next time: keystroke management for linux. [ Edit: No need - I cannot reproduce the keystroke bug on Fedora 8, so I won't be spending my time on trying to fix it :-) ]

No comments: