1.3.4 The Main Program

After all the preceding build-up and the other functions referenced (but not shown here) have been implemented, the code for the main client program is very small.

(defun just-say-lisp (host &optional (font-name "fg-16"))
  (let* ((display   (OPEN-DISPLAY host))
         (screen    (first (DISPLAY-ROOTS display)))
         (fg-color  (SCREEN-BLACK-PIXEL screen))
         (bg-color  (SCREEN-WHITE-PIXEL screen))
         (nice-font (OPEN-FONT display font-name))

         ;; Create a menu as a child of the root window.
         (a-menu       (create-menu (SCREEN-ROOT screen)
                                    fg-color bg-color nice-font)))

    (setf (menu-title a-menu) "Please pick your favorite language:")
    (menu-set-item-list a-menu "Fortran" "APL" "Forth" "Lisp")

    ;; Bedevil the user until he picks a nice programming language
    (unwind-protect
        (loop
          ;; Determine the current root window position of the pointer
          (multiple-value-bind (x y) (QUERY-POINTER (SCREEN-ROOT screen))

            (let ((choice (menu-choose a-menu x y)))
              (when (string-equal "Lisp" choice)
                (return)))))

      (CLOSE-DISPLAY display))))
        

Note that the main program event loop lies in the body of an unwind-protect form. This is a good programming technique because, without this protection, an unexpected error could cause the program to terminate without freeing the server resources it has created. Server resources are CLX objects which refer to objects actually stored on the X server. Examples of these are window, font, pixmap, cursor, colormap, and gcontext objects. These server resources are created and destroyed by user requests. Server resources created by a client are also destroyed when its display connection is closed. If client resources are repeatedly created without being destroyed, then the server will eventually run out of memory and fail.

Most server resources are potentially sharable between applications. In fact, windows are manipulated explicitly by window manager programs. Fonts and cursors are typically shared automatically since the X server loads and unloads font storage as needed. gcontext objects are not ordinarily shared between client applications.