]> gitweb.factorcode.org Git - factor.git/blob - basis/x11/xim/xim.factor
Specialized array overhaul
[factor.git] / basis / x11 / xim / xim.factor
1 ! Copyright (C) 2007, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.strings arrays byte-arrays
4 hashtables io io.encodings.string kernel math namespaces
5 sequences strings continuations x11 x11.xlib
6 specialized-arrays accessors io.encodings.utf16n ;
7 SPECIALIZED-ARRAY: uint
8 IN: x11.xim
9
10 SYMBOL: xim
11
12 : (init-xim) ( classname medifier -- im )
13     XSetLocaleModifiers [ "XSetLocaleModifiers() failed" throw ] unless
14     [ dpy get f ] dip dup XOpenIM ;
15
16 : init-xim ( classname -- )
17     dup "" (init-xim)
18     [ nip ]
19     [ "@im=none" (init-xim) [ "XOpenIM() failed" throw ] unless* ] if*
20     xim set-global ;
21
22 : close-xim ( -- )
23     xim get-global XCloseIM drop f xim set-global ;
24
25 : with-xim ( quot -- )
26     [ "Factor" init-xim ] dip [ close-xim ] [ ] cleanup ; inline
27
28 : create-xic ( window classname -- xic )
29     [
30         [ xim get-global XNClientWindow ] dip
31         XNFocusWindow over
32         XNInputStyle XIMPreeditNothing XIMStatusNothing bitor
33         XNResourceName
34     ] dip
35     XNResourceClass over 0 XCreateIC
36     [ "XCreateIC() failed" throw ] unless* ;
37
38 CONSTANT: buf-size 100
39
40 SYMBOL: keybuf
41 SYMBOL: keysym
42
43 : prepare-lookup ( -- )
44     buf-size <uint-array> keybuf set
45     0 <KeySym> keysym set ;
46
47 : finish-lookup ( len -- string keysym )
48     keybuf get swap 2 * head utf16n decode
49     keysym get *KeySym ;
50
51 : lookup-string ( event xic -- string keysym )
52     [
53         prepare-lookup
54         swap keybuf get buf-size keysym get 0 <int>
55         XwcLookupString
56         finish-lookup
57     ] with-scope ;