]> gitweb.factorcode.org Git - factor.git/blob - basis/x11/xim/xim.factor
54f20a28ddc70499a00afc6fb336db6e4879eddd
[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 specialized-arrays.uint
6 accessors io.encodings.utf16n ;
7 IN: x11.xim
8
9 SYMBOL: xim
10
11 : (init-xim) ( classname medifier -- im )
12     XSetLocaleModifiers [ "XSetLocaleModifiers() failed" throw ] unless
13     [ dpy get f ] dip dup XOpenIM ;
14
15 : init-xim ( classname -- )
16     dup "" (init-xim)
17     [ nip ]
18     [ "@im=none" (init-xim) [ "XOpenIM() failed" throw ] unless* ] if*
19     xim set-global ;
20
21 : close-xim ( -- )
22     xim get-global XCloseIM drop f xim set-global ;
23
24 : with-xim ( quot -- )
25     [ "Factor" init-xim ] dip [ close-xim ] [ ] cleanup ; inline
26
27 : create-xic ( window classname -- xic )
28     [
29         [ xim get-global XNClientWindow ] dip
30         XNFocusWindow over
31         XNInputStyle XIMPreeditNothing XIMStatusNothing bitor
32         XNResourceName
33     ] dip
34     XNResourceClass over 0 XCreateIC
35     [ "XCreateIC() failed" throw ] unless* ;
36
37 CONSTANT: buf-size 100
38
39 SYMBOL: keybuf
40 SYMBOL: keysym
41
42 : prepare-lookup ( -- )
43     buf-size <uint-array> keybuf set
44     0 <KeySym> keysym set ;
45
46 : finish-lookup ( len -- string keysym )
47     keybuf get swap 2 * head utf16n decode
48     keysym get *KeySym ;
49
50 : lookup-string ( event xic -- string keysym )
51     [
52         prepare-lookup
53         swap keybuf get buf-size keysym get 0 <int>
54         XwcLookupString
55         finish-lookup
56     ] with-scope ;