This section describes a module that is almost identical to the previous one, but uses a function that determine the screen size. This function can also be used to determine many other window attributes.
What is needed is a variable of type XWindowAttributes. The declaration section is now:
main ()
{
Display *dpy;
Window root;
XWindowAttributes wa;
GC g;
In order to determine the window attributes, we use the
function XGetWindowAttributes. Since this function
needs both a display and a window, it can be called only
after dpy and root have been determined.
/* get attributes of the root window */ XGetWindowAttributes(dpy, root, &wa);After this, the width of the screen is wa.width, while its height is wa.height. These two variables can be used for instance to choose a pair of coordinates such that the squares are never out of the screen. For example, we can replace the instruction of drawing with:
/* draw a square */
XFillRectangle (dpy, root, g, random()%(wa.width-50),
random()%(wa.height-40), 50, 40);
This way, the x and y coordinates of the left-upper
corner of each square are between 0 and wa.width-50
and between
wa.height-40, respectively. Since the squares have
width 50 and height 40, they are all drawn inside
the screen, and they may be drawn in any position
of the screen.
To compile, test, and install, see: testing and installing a new module