| 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. *) |
| 5 | module Window; |
| 6 | import Out; |
| 7 | import SYSTEM; |
| 8 | link "libX11.so.6"; |
| 9 | |
| 10 | const |
| 11 | KeyPress = 2; |
| 12 | Expose = 12; |
| 13 | KeyPressMask = 1; |
| 14 | ExposureMask = 8000H; |
| 15 | |
| 16 | type |
| 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 | |
| 29 | extern proc XOpenDisplay(name: ^U8): ^U8; |
| 30 | extern proc XDefaultScreen(d: ^U8): I32; |
| 31 | extern proc XRootWindow(d: ^U8; screen: I32): U64; |
| 32 | extern proc XBlackPixel(d: ^U8; screen: I32): U64; |
| 33 | extern proc XWhitePixel(d: ^U8; screen: I32): U64; |
| 34 | extern proc XDefaultGC(d: ^U8; screen: I32): ^U8; |
| 35 | extern proc XCreateSimpleWindow(d: ^U8; parent: U64; x, y: I32; |
| 36 | width, height, border: U32; borderColor, background: U64): U64; |
| 37 | extern proc XStoreName(d: ^U8; w: U64; name: ^U8): I32; |
| 38 | extern proc XSelectInput(d: ^U8; w: U64; mask: INT): I32; |
| 39 | extern proc XMapWindow(d: ^U8; w: U64): I32; |
| 40 | extern proc XNextEvent(d: ^U8; ev: ^U8): I32; |
| 41 | extern proc XDrawString(d: ^U8; w: U64; gc: ^U8; x, y: I32; s: ^U8; n: I32): I32; |
| 42 | extern proc XCloseDisplay(d: ^U8): I32; |
| 43 | |
| 44 | proc main*: INT; |
| 45 | const text = "hello, world: press a key"; |
| 46 | var |
| 47 | d: ^U8; s: I32; w: U64; |
| 48 | ev: [24]I64; (* XEvent: 192 bytes *) |
| 49 | key: ^KeyEvent; |
| 50 | begin |
| 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; |
| 69 | end; |
| 70 | |
| 71 | end Window. |