Xlib Fundamentals
the concept of windows in X
Windows in X X Window Programming Lab
Xlib Fundamentals
Xlib is a set of library routines used to implement the basic X calls.
X toolkits not only provide all the functionality of Xlib rou- tines and is much easier to use.
It is still important to understand how the Xlib works.
- Creating and displaying the window - Drawing with X
- Text - Color - Events
J. Park 1/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Windows are what X is all about
Each window is a data structure stored in the X server.
Identied by an ID of Window type (usually an unsigned 32 bit integer).
To create a single window application 1. Connect to the server
2. Check the environment 3. Create the window
4. Display the window
| !!!! |
N. Destroy window and close the display
J. Park 2/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Connecting to the server and Checking the environment
Make the initial connection: XOpenDisplay()
Display *display;
display = XOpenDisplay ( (char *) NULL);
Determine which screen you are connected to: DefaultScreen()
int screen;
screen = DefaultScreen(display);
Ask the X server what the characteristics of the screen are:
DisplayWidth (display,screen) DisplayHeight (display,screen) DefaultDepth (display,screen)
J. Park 3/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Creating the window
XCreateSimpleWindow(), XCreateWindow()
#include <X11/Xlib.h>
Display *display;
Window parent_window;
int x, y;
unsigned int width, height;
unsigned int border_width;
int depth;
unsigned int class;
Visual *visual;
unsigned long valuemask;
XSetWindowAttributes *attributes;
window = XCreateWindow (display, parent_window, x, y, width, height, border_width, depth, class, valuemask, attributes );
J. Park 4/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
XCreateWindow()
Returns the widow ID
parameters:
{
target display{
parent's id{
desired pixel location of the TLC of this new window{
size of the window in pixels{
how wide the window's border should be (in pixels)J. Park 5/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
{
number of bit planes available{
class: InputOutput, InputOnly or CopyFromParent{
The attributes structure contains 15 elds related to the window. The valuemask variable is a bit mask, indicating which of the 15 elds you have actually specied.background pixel - the color of the window background
border pixel - the color of the window border
event mask - ask for window expose events
override redirect - allows you to override the window manager
J. Park 6/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Display *display;
int screen;
XSetWindowAttributes attributes;
unsigned long attr_mask;
attributes.background_pixel = WhitePixel(display, screen);
attributes.border_pixel = BlackPixel(display, screen);
attributes.event_mask = ExposureMask;
attributes.override_redirect = False;
attr_mask = CWBackPixel | CWBorderPixel | CWEventMask | CWOverrideRedirect;
J. Park 7/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Getting ready to display the window
Before we can display the window we need to provide some hints about it to the window manager: data structure
XSize- Hints
{
As with XSetAttributes, there is a mask, this time stored inside the structure in the eld ags (e.g.,USPosition - user specied x, y
USSize - user specied width, height PPosition - program specied position PSize - program specied size
PMinSize - min size PMaxSize - max size
PAspect - min and max aspect ratios)
J. Park 8/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Once the size hints have been set up they are passed to thewindow manager using the
XSetNormalHints
function.Display * display;
Window window;
XSizeHints hints;
XSetNormalHints(display, window, &hints);
Initialize the window's name (before we actually display the window) to be shown in the windows title bar:
XStoreName
function
Display * display;
Window window;
char window_name [ ];
XStoreName (display, window, window_name);
J. Park 9/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Displaying the window
XMapWindow(), XMapRaised()
Display * display;
Window window;
XMapWindow(display,window);
XMapRaised(display,window);
To be sure that the window is displayed we must ush the cache.
XFlush(display);
J. Park 10/11 CSE 180 (U Penn)
Windows in X X Window Programming Lab
Cleaning up
Once our program has nished we should destroy all windows and close the display.
XDestroyWindow(display, window);
XCloseDisplay(display);
J. Park 11/11 CSE 180 (U Penn)