X11 sample rewritten; now it works again
[k8lst.git] / modules / slang.st
blobd539f9c3ae4b2dc60bb9566b05f7b8079d7ef80e
2  Little Smalltalk, Version 5
4  Copyright (C) 1987-2005 by Timothy A. Budd
5  Copyright (C) 2007 by Charles R. Childers
6  Copyright (C) 2005-2007 by Danny Reinhold
7  Copyright (C) 2010 by Ketmar // Vampire Avalon
9  ============================================================================
10  This license applies to the virtual machine and to the initial image of
11  the Little Smalltalk system and to all files in the Little Smalltalk
12  packages except the files explicitly licensed with another license(s).
13  ============================================================================
14  Permission is hereby granted, free of charge, to any person obtaining a copy
15  of this software and associated documentation files (the 'Software'), to deal
16  in the Software without restriction, including without limitation the rights
17  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  copies of the Software, and to permit persons to whom the Software is
19  furnished to do so, subject to the following conditions:
21  The above copyright notice and this permission notice shall be included in
22  all copies or substantial portions of the Software.
24  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  DEALINGS IN THE SOFTWARE.
32 Requires [
33   ffi
36 Package [
37   SLang
41 class: SLang
42   | lib
43     fnInitTty
44     fnResetTty
45     fnGetTermInfo
46     fnKpInit
47     fnSmgInit
48     fnSmgReset
49     fnSetCursorVisibility
50     fnScreenCols
51     fnScreenRows
52     fnRefresh
53     fnGotoXY
54     fnGetKey
55     fnWriteChar
56     fnWriteString
57     fnCls
58     fnInputPending
59     fnCharSet
60     fnDrawBox
61   |
63   ^initialize [
64     lib ifNil: [ lib := FfiLibrary new: 'libslang' ].
65   ]
67   ^slInitTty [
68     (fnInitTty ifNil: [ fnInitTty := FfiFunction new: lib name: 'SLang_init_tty' ])
69       retType: nil with: -1 with: 0 with: 0.  "abortChar, flowCtrl, opost"
70   ]
72   ^slResetTty [
73     (fnResetTty ifNil: [ fnResetTty := FfiFunction new: lib name: 'SLang_reset_tty' ])
74       call.
75   ]
77   ^slGetTermInfo [
78     (fnGetTermInfo ifNil: [ fnGetTermInfo := FfiFunction new: lib name: 'SLtt_get_terminfo' ])
79       call.
80   ]
82   ^slKpInit [
83     (fnKpInit ifNil: [ fnKpInit := FfiFunction new: lib name: 'SLkp_init' ])
84       call.
85   ]
87   ^slSmgInit [
88     (fnSmgInit ifNil: [ fnSmgInit := FfiFunction new: lib name: 'SLsmg_init_smg' ])
89       call.
90   ]
92   ^slSmgReset [
93     (fnSmgReset ifNil: [ fnSmgReset := FfiFunction new: lib name: 'SLsmg_reset_smg' ])
94       call.
95   ]
97   ^init [
98     self;
99       initialize;
100       slInitTty;
101       slGetTermInfo;
102       slKpInit;
103       slSmgInit;
104       cursor: false.
105   ]
107   ^reset [
108     self;
109       cursor: true;
110       normalCharSet;
111       slSmgReset;
112       slResetTty.
113   ]
115   ^screenCols [
116     ^(fnScreenCols ifNil: [ fnScreenCols := FfiValue new: lib name: 'SLtt_Screen_Cols' ])
117       as: SmallInt.
118   ]
120   ^screenRows [
121     ^(fnScreenRows ifNil: [ fnScreenRows := FfiValue new: lib name: 'SLtt_Screen_Rows' ])
122       as: SmallInt.
123   ]
125   ^cursor: aVisFlag [
126     (fnSetCursorVisibility ifNil: [ fnSetCursorVisibility := FfiFunction new: lib name: 'SLtt_set_cursor_visibility' ])
127       retType: nil with: aVisFlag.
128   ]
130   ^refresh [
131     (fnRefresh ifNil: [ fnRefresh := FfiFunction new: lib name: 'SLsmg_refresh' ])
132       retType: nil
133   ]
135   ^cls [
136     (fnCls ifNil: [ fnCls := FfiFunction new: lib name: 'SLsmg_cls' ])
137       retType: nil
138   ]
140   ^gotoX: x y: y [
141     (fnGotoXY ifNil: [ fnGotoXY := FfiFunction new: lib name: 'SLsmg_gotorc' ])
142       retType: nil with: y with: x
143   ]
145   ^writeChar: s [
146     (fnWriteChar ifNil: [ fnWriteChar := FfiFunction new: lib name: 'SLsmg_write_char' ])
147       retType: nil with: s
148   ]
150   ^writeString: s [
151     (s class == Char) ifTrue: [ ^self writeChar: s ].
152     (fnWriteString ifNil: [ fnWriteString := FfiFunction new: lib name: 'SLsmg_write_string' ])
153       retType: nil with: s
154   ]
156   ^getKey [
157     ^(fnGetKey ifNil: [ fnGetKey := FfiFunction new: lib name: 'SLkp_getkey' ])
158       retType: SmallInt
159   ]
161   ^inputPending: aWaitSec [
162     "aWaitSec either float or int or nil"
163     aWaitSec ifNil: [ aWaitSec := 0 ]
164       ifNotNil: [
165         aWaitSec isFloat
166           ifTrue: [ aWaitSec := (aWaitSec * 10) asSmallInt ]
167           ifFalse: [ aWaitSec := aWaitSec * 10 ]].
168     ^(fnInputPending ifNil: [ fnInputPending := FfiFunction new: lib name: 'SLang_input_pending' ])
169       retType: SmallInt with: aWaitSec
170   ]
172   ^inputPending [
173     ^self inputPending: nil
174   ]
176   ^charSet: idx [
177     (fnCharSet ifNil: [ fnCharSet := FfiFunction new: lib name: 'SLsmg_set_char_set' ])
178       retType: nil with: idx
179   ]
181   ^normalCharSet [
182     ^self charSet: 0
183   ]
185   ^lineCharSet [
186     ^self charSet: 1
187   ]
189   ^drawBoxX0: x0 y0: y0 x1: x1 y1: y1 [
190     (fnDrawBox ifNil: [ fnDrawBox := FfiFunction new: lib name: 'SLsmg_draw_box' ])
191       retType: nil with: x0 with: y0 with: x1 with: y1
192   ]
194   ^writeStringX: x y: y text: text [
195     ^self; gotoX: x y: y; writeString: text
196   ]
198   ^writeCharX: x y: y char: char [
199     ^self; gotoX: x y: y; writeChar: char
200   ]
205 flushInput [
206   [(self pendingInput: 0) ~= 0] whileTrue: [self getKey].
213   | c |
214   SLang init.
215   SLang cls.
216   SLang drawBoxX0: 7 y0: 5 x1: 10 y1: 15.
217   SLang gotoX: 2 y: 4.
218   SLang writeString: 'test!'.
219   SLang refresh.
220   c := SLang getKey.
221   SLang reset.
222   SLang screenCols print. 'x' print. SLang screenRows printNl.
223   c printNl.