raz / examples / Window.raz

71 lines · 2.2 KiB · raw · history · last change 264716cb

1(* An X11 window that says hello, and closes at a key press. An XEvent
2 is a C union: it is read as an array of words, and the member its
3 type says it holds through a pointer conversion. Compiled by make
4 check, not run: it needs a display. *)
5module Window;
6import Out;
7import SYSTEM;
8link "libX11.so.6";
9
10const
11 KeyPress = 2;
12 Expose = 12;
13 KeyPressMask = 1;
14 ExposureMask = 8000H;
15
16type
17 (* XKeyEvent, laid out as C lays it out *)
18 KeyEvent = record
19 typ: I32;
20 serial: U64;
21 sendEvent: I32;
22 display: ^U8;
23 window, root, subwindow, time: U64;
24 x, y, xRoot, yRoot: I32;
25 state, keycode: U32;
26 sameScreen: I32;
27 end;
28
29extern proc XOpenDisplay(name: ^U8): ^U8;
30extern proc XDefaultScreen(d: ^U8): I32;
31extern proc XRootWindow(d: ^U8; screen: I32): U64;
32extern proc XBlackPixel(d: ^U8; screen: I32): U64;
33extern proc XWhitePixel(d: ^U8; screen: I32): U64;
34extern proc XDefaultGC(d: ^U8; screen: I32): ^U8;
35extern proc XCreateSimpleWindow(d: ^U8; parent: U64; x, y: I32;
36 width, height, border: U32; borderColor, background: U64): U64;
37extern proc XStoreName(d: ^U8; w: U64; name: ^U8): I32;
38extern proc XSelectInput(d: ^U8; w: U64; mask: INT): I32;
39extern proc XMapWindow(d: ^U8; w: U64): I32;
40extern proc XNextEvent(d: ^U8; ev: ^U8): I32;
41extern proc XDrawString(d: ^U8; w: U64; gc: ^U8; x, y: I32; s: ^U8; n: I32): I32;
42extern proc XCloseDisplay(d: ^U8): I32;
43
44proc main*: INT;
45const text = "hello, world: press a key";
46var
47 d: ^U8; s: I32; w: U64;
48 ev: [24]I64; (* XEvent: 192 bytes *)
49 key: ^KeyEvent;
50begin
51 d := XOpenDisplay(nil);
52 if d = nil then Out.str("no display\n"); return 1; end;
53 s := XDefaultScreen(d);
54 w := XCreateSimpleWindow(d, XRootWindow(d, s), 0, 0, 320, 80, 1,
55 XBlackPixel(d, s), XWhitePixel(d, s));
56 _ := XStoreName(d, w, "raz");
57 _ := XSelectInput(d, w, ExposureMask + KeyPressMask);
58 _ := XMapWindow(d, w);
59 repeat
60 _ := XNextEvent(d, ^U8(SYSTEM.ADR(ev)));
61 if I32(ev[0]) = Expose then
62 _ := XDrawString(d, w, XDefaultGC(d, s), 20, 45, text, I32(LEN(text)));
63 end;
64 until I32(ev[0]) = KeyPress;
65 key := ^KeyEvent(SYSTEM.ADR(ev));
66 Out.str("key "); Out.int(INT(key^.keycode)); Out.ln;
67 _ := XCloseDisplay(d);
68 return 0;
69end;
70
71end Window.