Debian Codegraph @

src/ANSITerminal_win_stubs.c

raw symbols SCIP
1 /* ANSITerminal; windows API calls
2
3 Allow colors, cursor movements, erasing,... under Unix and DOS shells.
4 *********************************************************************
5
6 Copyright 2010 by Christophe Troestler <[email protected]>
7 http://math.umons.ac.be/an/software/
8
9 Copyright 2010 by Vincent Hugot
10 [email protected]
11 www.vincent-hugot.com
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public License
15 version 2.1 as published by the Free Software Foundation, with the
16 special exception on linking described in file LICENSE.
17
18 This library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
21 LICENSE for more details.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #define CAML_INTERNALS
27 #include <caml/io.h>
28 #include <caml/mlvalues.h>
29 #include <caml/alloc.h>
30 #include <caml/memory.h>
31 #include <caml/callback.h>
32 #include <windows.h>
33
34 /* From otherlibs/win32unix/channels.c */
35 extern long _get_osfhandle(int);
36 #define HANDLE_OF_CHAN(vchan) ((HANDLE) _get_osfhandle(Channel(vchan)->fd))
37
38 static HANDLE hStdout;
39 static CONSOLE_SCREEN_BUFFER_INFO *csbiInfo = NULL;
40
41 void raise_error(char *fname, char *msg)
42 {
43 CAMLparam0();
44 CAMLlocal2(vfname, vmsg);
45 static value *exn = NULL;
46 value args[2];
47
48 if (exn == NULL) {
49 /* First time around, look up by name */
50 exn = caml_named_value("ANSITerminal.Error");
51 }
52 vfname = caml_copy_string(fname);
53 vmsg = caml_copy_string(msg);
54 args[0] = vfname;
55 args[1] = vmsg;
56 caml_raise_with_args(*exn, 2, args);
57 CAMLnoreturn;
58 }
59
60 void exn_of_error(char *fname, BOOL cond)
61 {
62 CAMLparam0();
63 CAMLlocal2(vfname, vmsg);
64 char *msg, *p;
65 LPVOID lpMsgBuf;
66 static value *exn = NULL;
67 value args[2];
68
69 if (cond) {
70 if (exn == NULL) {
71 /* First time around, look up by name */
72 exn = caml_named_value("ANSITerminal.Error");
73 }
74 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
75 FORMAT_MESSAGE_FROM_SYSTEM |
76 FORMAT_MESSAGE_IGNORE_INSERTS,
77 NULL,
78 GetLastError(),
79 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
80 (LPTSTR) &lpMsgBuf,
81 0, NULL);
82 vfname = caml_copy_string(fname);
83 msg = (char *) lpMsgBuf;
84 p = msg + strlen(msg) - 1;
85 while(*p == '\n' || *p == '\r') {
86 *p = '\0';
87 p--;
88 }
89 vmsg = caml_copy_string(msg);
90 LocalFree(lpMsgBuf);
91 args[0] = vfname;
92 args[1] = vmsg;
93 caml_raise_with_args(*exn, 2, args);
94 }
95 CAMLreturn0;
96 }
97
98
99
100 #define SET_CSBI(fname) \
101 if (! csbiInfo) { \
102 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); \
103 if (hStdout == INVALID_HANDLE_VALUE) { \
104 raise_error(fname, "Invalid stdout handle"); \
105 } \
106 exn_of_error(fname, \
107 ! GetConsoleScreenBufferInfo(hStdout, csbiInfo)); \
108 }
109
110
111 #define SET_STYLE(fname) \
112 CAMLexport \
113 value ANSITerminal_ ## fname(value vchan, value vcode) \
114 { \
115 HANDLE h = HANDLE_OF_CHAN(vchan); \
116 int code = Int_val(vcode); \
117 \
118 exn_of_error("ANSITerminal." #fname, \
119 ! SetConsoleTextAttribute(h, code)); \
120 return Val_unit; \
121 }
122
123 SET_STYLE(set_style)
124 SET_STYLE(unset_style)
125
126 CAMLexport
127 value ANSITerminal_get_style(value vchan)
128 {
129 HANDLE h = HANDLE_OF_CHAN(vchan);
130 CONSOLE_SCREEN_BUFFER_INFO info;
131
132 /* To save the previous info when setting style */
133 exn_of_error("ANSITerminal.set_style",
134 ! GetConsoleScreenBufferInfo(h, &info));
135
136 return(Val_int(info.wAttributes));
137 }
138
139
140 CAMLexport
141 value ANSITerminal_pos(value vunit)
142 {
143 CAMLparam1(vunit);
144 CAMLlocal1(vpos);
145 SMALL_RECT w;
146 SHORT x, y;
147
148 exn_of_error("ANSITerminal.pos_cursor",
149 ! GetConsoleScreenBufferInfo(hStdout, csbiInfo));
150 w = csbiInfo->srWindow;
151 /* The topmost left character has pos (1,1) */
152 x = csbiInfo->dwCursorPosition.X - w.Left + 1;
153 y = csbiInfo->dwCursorPosition.Y - w.Top + 1;
154
155 vpos = caml_alloc_tuple(2);
156 Store_field(vpos, 0, Val_int(x));
157 Store_field(vpos, 1, Val_int(y));
158 CAMLreturn(vpos);
159 }
160
161 CAMLexport
162 value ANSITerminal_size(value vunit)
163 {
164 CAMLparam1(vunit);
165 CAMLlocal1(vsize);
166 SMALL_RECT w;
167
168 /* Update the global var as the terminal may have been be resized */
169 SET_CSBI("ANSITerminal.size");
170 exn_of_error("ANSITerminal.size",
171 ! GetConsoleScreenBufferInfo(hStdout, csbiInfo));
172 w = csbiInfo->srWindow;
173
174 vsize = caml_alloc_tuple(2);
175 Store_field(vsize, 0, Val_int(w.Right - w.Left + 1));
176 Store_field(vsize, 1, Val_int(w.Bottom - w.Top + 1));
177
178 CAMLreturn(vsize);
179 }
180
181 CAMLexport
182 value ANSITerminal_resize(value vx, value vy)
183 {
184 /* noalloc */
185 COORD dwSize;
186 dwSize.X = Int_val(vx);
187 dwSize.Y = Int_val(vy);
188 exn_of_error("ANSITerminal.resize",
189 ! SetConsoleScreenBufferSize(hStdout, dwSize));
190 return Val_unit;
191 }
192
193
194 CAMLexport
195 value ANSITerminal_SetCursorPosition(value vx, value vy)
196 {
197 COORD c;
198 SMALL_RECT w;
199
200 SET_CSBI("ANSITerminal.set_cursor");
201 exn_of_error("ANSITerminal.set_cursor",
202 ! GetConsoleScreenBufferInfo(hStdout, csbiInfo));
203 /* The top lefmost coordinate is (1,1) for ANSITerminal while it is
204 * (0,0) for windows. */
205 w = csbiInfo->srWindow;
206 c.X = Int_val(vx) - 1 + w.Left;
207 c.Y = Int_val(vy) - 1 + w.Top;
208
209 // very subtle debugging method...
210 fprintf(stderr,"vx,vy = %d,%d --> [c.X,Y = %d,%d ; L %d R %d T %d B %d]\n",
211 Int_val(vx),Int_val(vy), c.X, c.Y, w.Left, w.Right, w.Top, w.Bottom);
212
213 if (c.X > w.Right) c.X = w.Right;
214 if (c.Y > w.Bottom) c.Y = w.Bottom;
215 exn_of_error("ANSITerminal.set_cursor",
216 ! SetConsoleCursorPosition(hStdout, c));
217 return Val_unit;
218 }
219
220 CAMLexport
221 value ANSITerminal_FillConsoleOutputCharacter(
222 value vchan, value vc, value vlen, value vx, value vy)
223 {
224 CAMLparam1(vchan);
225 HANDLE h = HANDLE_OF_CHAN(vchan);
226 int NumberOfCharsWritten;
227 COORD dwWriteCoord;
228
229 SET_CSBI("ANSITerminal.erase");
230 exn_of_error("ANSITerminal.erase",
231 ! GetConsoleScreenBufferInfo(hStdout, csbiInfo));
232 dwWriteCoord.X = Int_val(vx) - 1 + csbiInfo->srWindow.Left;
233 dwWriteCoord.Y = Int_val(vy) - 1 + csbiInfo->srWindow.Top;
234 exn_of_error("ANSITerminal.erase",
235 !FillConsoleOutputCharacter(h, Int_val(vc), Int_val(vlen),
236 dwWriteCoord,
237 &NumberOfCharsWritten));
238 CAMLreturn(Val_int(NumberOfCharsWritten));
239 }
240
241
242 CAMLexport
243 value ANSITerminal_Scroll(value vx)
244 {
245 /* noalloc */
246 INT x = Int_val(vx);
247 SMALL_RECT srctScrollRect, srctClipRect;
248 CHAR_INFO chiFill;
249 COORD coordDest;
250
251 SET_CSBI("ANSITerminal.scroll");
252 srctScrollRect.Left = 0;
253 srctScrollRect.Top = 1;
254 srctScrollRect.Right = csbiInfo->dwSize.X - x;
255 srctScrollRect.Bottom = csbiInfo->dwSize.Y - x;
256
257 // The destination for the scroll rectangle is one row up.
258 coordDest.X = 0;
259 coordDest.Y = 0;
260
261 // The clipping rectangle is the same as the scrolling rectangle.
262 // The destination row is left unchanged.
263 srctClipRect = srctScrollRect;
264
265 // Set the fill character and attributes.
266 chiFill.Attributes = FOREGROUND_RED|FOREGROUND_INTENSITY;
267 chiFill.Char.AsciiChar = (char) ' ';
268
269 exn_of_error("ANSITerminal.scroll",
270 ! ScrollConsoleScreenBuffer(
271 hStdout, // screen buffer handle
272 &srctScrollRect, // scrolling rectangle
273 &srctClipRect, // clipping rectangle
274 coordDest, // top left destination cell
275 &chiFill)); // fill character and color
276 return Val_unit;
277 }