Xlib and the pains of X11 programming
Im trying to learn to program using XLib and X11 on unix. Ive got a heap of books and ive read a fair bit of them. It seems pretty straight forward, like most other programing things its just a bunch of methods ya call to do stuff.
However, Its soooo painful and drawn out compared to Cocoa programming *sigh*
Ive got an example here that takes a good 5 pages of code to display a window with some text in it. You have to make pointers to window objects and pixmaps and events and fonts and etc etc etc. Once thats all declared you use a good 10-12 lines of code to initialise the display and screen pointers and attempt to open the display. If all goes well you get a number back that refers to the particular screen which you can use later on when drawing to the screen and stuff – easy huh?
From here, you create the window, type in around 10 lines of code to initialise the pointer variables and call the create window method, if all goes well you get a reference back to the window object – yay!
Next, you gotta create a pixmap icon for the app incase the window manager iconifies the app at some stage while its running. Insert another 3 lines for this. Moving right along.
From here it gets interesting, you have send send a HEAP of properties and variables to the window manager for it to be able to handle displaying your fancy new window. All up you need around 1.5 pages of code (including all the #ifdef statements to take into account difference versions of X11 etc etc).
Next, tell the display and window what events you want it to look out for, simple one this, only 1 line.
Ok, after all that starting code is done, we can actually try to display the window. To do so we map the window to the display using a pretty simple method, easy
After that, flush the output buffer and it should be good to go.
Setting up the event loop is the next thing to do. Takes about a page of code for the main loop to handle refresh events, redraw events, mouse events, keyboard events, etc etc
Once thats done, you code the methods you call for each event in the event loop
From what ive seen, the minimum this takes is about 2 pages of code but im guessing it could easily get up to many pages of code pretty fast.
And thats it, simple huh? Compile the code using the gg compiler through the command line making sure to include the path to the X11 libraries and also the actual X11 libraries themselves. Run the code and ou get a pretty ugly window pop up that goes away when you click the mouse. nice
So yeah, its not too bad i guess. Makes me almost cry though when i can do the same thing with XCode and Interface Builder with a few clicks of the mouse and writing no code at all
Im sure theres a valid reason for using XLib and X11, possibly the fact that it should run on any unix/linux platform, but damn its ugly.
*sigh*
Posted in General 6 Comments »