Window creation/X11

From Rosetta Code
Revision as of 13:12, 15 August 2009 by A (talk | contribs) (added python version)
Task
Window creation/X11
You are encouraged to solve this task according to the task description, using any language you may know.

Create a simple X11 application, using an X11 protocol library such as Xlib, that draws a box and "Hello World" in a window

ALGOL 68

Works with: ALGOL 68G version tested with release mk15-0.8b.fc9.i386

Using the X11 & plotutil libraries is not part of any of the original UNESCO/IFIP ALGOL 68 reports. As at December 2008 only ALGOL 68G comes with these built in.

FILE window;
draw device (window, "X", "600x400");
open (window, "Hello, World!", stand draw channel);
  draw erase (window);
  draw move (window, 0.25, 0.5);
  draw colour (window, 1, 0, 0);
  draw text (window, "c", "c", "hello world");
  draw show (window);
  VOID (read char); 
close (window)

C

Library: Xlib

Compile with:

  • gcc -L/usr/X11R6/lib -lX11 hello-x.c -o hello-x

<lang c>#include <X11/Xlib.h>

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <string.h>

int main(void) {

  Display *d;
  Window w;
  XEvent e;
  char *msg = "Hello, World!";
  int s;
  d = XOpenDisplay(NULL);
  if (d == NULL) {
     fprintf(stderr, "Cannot open display\n");
     exit(1);
  }
  s = DefaultScreen(d);
  w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                          BlackPixel(d, s), WhitePixel(d, s));
  XSelectInput(d, w, ExposureMask | KeyPressMask);
  XMapWindow(d, w);
  while (1) {
     XNextEvent(d, &e);
     if (e.type == Expose) {
        XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
        XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
     }
     if (e.type == KeyPress)
        break;
  }
  XCloseDisplay(d);
  return 0;

} </lang>

Common Lisp

Translation of: C
Library: CLX

This example uses CLX, which is the de facto standard X11 library for Common Lisp. CLX is not a binding to Xlib; it is a Lisp library implementing the X11 protocol.

Note: This example was written in near-total ignorance of X11 by consulting the CLX manual to find equialents for the parts of the C example. It was also only tested with Mac OS X X11, which is not exactly normal. Do not take it as an complete example, well-tested, or good style. [Do, however, remove this disclaimer if you know better and have checked it.]

<lang lisp>;;; Single-file/interactive setup; large applications should define an ASDF system instead (cl:require :asdf) (asdf:operate 'asdf:load-op :clx) (cl:defpackage #:rc-xlib-window

 (:use #:cl #:xlib))

(cl:in-package #:rc-xlib-window)

(let ((display (open-default-display)))

 (unwind-protect
     (let* ((window (create-window :parent (screen-root (display-default-screen display))
                                   :x 10
                                   :y 10
                                   :width 100
                                   :height 100
                                   :event-mask '(:exposure :key-press)))
            (gc (create-gcontext :drawable window)))
       (map-window window)
       (event-case (display :discard-p t)
         (exposure ()
           (draw-rectangle window gc 20 20 10 10 t)
           (draw-glyphs window gc 10 40 "Hello, World!")
           nil #| continue receiving events |#)
         (key-press ()
           t #| non-nil result signals event-case to exit |#))))
   (close-display display))</lang>

OCaml

Library: OCaml-Xlib

execute as a script with:

ocaml -I +Xlib Xlib.cma script.ml

or compile to native code:

ocamlopt -I +Xlib Xlib.cmxa prog.ml -o prog

or to make a standalone script add these lines at the beginning of the file:

#!/usr/bin/env ocaml
#directory "+Xlib"
#load "Xlib.cma"

<lang ocaml>open Xlib

let () =

 let d = xOpenDisplay "" in
 let s = xDefaultScreen d in
 let w = xCreateSimpleWindow d (xRootWindow d s) 10 10 100 100 1
                               (xBlackPixel d s) (xWhitePixel d s) in
 xSelectInput d w [ExposureMask; KeyPressMask];
 xMapWindow d w;
 let msg = "Hello, World!" in
 let rec main_loop() =
   match xEventType(xNextEventFun d) with
   | Expose ->
       xFillRectangle d w (xDefaultGC d s) 20 20 10 10;
       xDrawString d w (xDefaultGC d s) 10 50 msg;
       main_loop()
   | KeyPress -> ()  (* exit main loop *)
   | _ -> main_loop()
 in
 main_loop();
 xCloseDisplay d;
</lang>

Python

Library: python-Xlib

Download Python X library from http://sourceforge.net/projects/python-xlib/ . python-xlib is a pure python library therefore the example should work anywhere where python does. Run:

  • python xlib_hello_world.py

<lang python>from Xlib import X, display

class Window:

   def __init__(self, display, msg):
       self.display = display
       self.msg = msg
       
       self.screen = self.display.screen()
       self.window = self.screen.root.create_window(
           10, 10, 100, 100, 1,
           self.screen.root_depth,
           background_pixel=self.screen.white_pixel,
           event_mask=X.ExposureMask | X.KeyPressMask,
           )
       self.gc = self.window.create_gc(
           foreground = self.screen.black_pixel,
           background = self.screen.white_pixel,
           )
       self.window.map()
   def loop(self):
       while True:
           e = self.display.next_event()
               
           if e.type == X.Expose:
               self.window.fill_rectangle(self.gc, 20, 20, 10, 10)
               self.window.draw_text(self.gc, 10, 50, self.msg)
           elif e.type == X.KeyPress:
               raise SystemExit


if __name__ == "__main__":

   Window(display.Display(), "Hello, World!").loop()

</lang> Note (stolen from CLX example): This example was written in near-total ignorance of X11 by consulting the python-xlib's examples (included in its distribution) to find equialents for the parts of the C example. Do not take it as an complete example, well-tested, or good style. [Do, however, remove this disclaimer if you know better and have checked it.]