-
Notifications
You must be signed in to change notification settings - Fork 0
/
tclpdc.c
491 lines (446 loc) · 12.2 KB
/
tclpdc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#define USE_TCL_STUBS
#include <tcl.h>
#define USE_TCLOO_STUBS
#include <tclOO.h>
#define PDC_WIDE
#include <sdl2/pdcsdl.h>
#include "keycodes.inc"
#if defined(_WIN32)
#define PUBLIC __declspec(dllexport)
#define UNUSED __pragma(warning(suppress: 4100 4101))
#elif defined(__GNUC__)
#define PUBLIC __attribute__ ((visibility ("default")))
#define UNUSED __attribute__ ((unused))
#endif
#define ASSERT(cond) do if (!(cond)) printf("Invalid assertion at " __FILE__ ":%d: " #cond "\n", __LINE__); while (0)
#define ASSERT_TCLOK(expr) do if ((expr) == TCL_ERROR) printf("TCL_ERROR at " __FILE__ ":%d: " #expr ": %s\n", __LINE__, Tcl_GetStringResult(interp)); while (0)
#define TCL_METHODTYPE(CLASS, METHOD) pdc_##CLASS##_##METHOD##_type
#define TCL_METHOD_DEF(CLASS, METHOD, ...) \
static int pdc_##CLASS##_##METHOD(ClientData, Tcl_Interp*, Tcl_ObjectContext, int, Tcl_Obj* const[]); \
static Tcl_MethodType TCL_METHODTYPE(CLASS, METHOD) = \
{ TCL_OO_METHOD_VERSION_CURRENT, "PDCurses " #CLASS " method: \"" #METHOD "\"", pdc_##CLASS##_##METHOD, NULL, NULL, }; \
static int pdc_##CLASS##_##METHOD(__VA_ARGS__)
static void pure_void(UNUSED ClientData window)
{
return;
}
static Tcl_ObjectMetadataType pdc_window_meta =
{
TCL_OO_METADATA_VERSION_CURRENT,
"PDcurses meta: \"window\"",
pure_void,
NULL,
};
static int get_vector2_from_obj(
Tcl_Interp* interp,
Tcl_Obj* const obj,
int vector[2],
const char y_name[],
const char x_name[])
{
int len;
Tcl_Obj** list;
if (Tcl_ListObjGetElements(interp, obj, &len, &list) == TCL_ERROR)
return TCL_ERROR;
if (len != 2)
{
if (y_name == NULL)
y_name = "y";
if (x_name == NULL)
x_name = "x";
Tcl_SetObjResult(interp, Tcl_ObjPrintf("invalid position: should be \"{%s %s}\"", y_name, x_name));
return TCL_ERROR;
}
for (int i = 0; i < len; ++i)
{
if (Tcl_GetIntFromObj(interp, list[i], vector + i) == TCL_ERROR)
{
return TCL_ERROR;
}
}
return TCL_OK;
}
TCL_METHOD_DEF(window, add,
UNUSED ClientData clientData,
Tcl_Interp* interp,
Tcl_ObjectContext context,
int objc,
Tcl_Obj* const objv[])
{
WINDOW* win = Tcl_ObjectGetMetadata(Tcl_ObjectContextObject(context), &pdc_window_meta);
bool moving = false;
int position[2];
int skipped_argc = Tcl_ObjectContextSkippedArgs(context);
int argc = objc - skipped_argc;
Tcl_Obj* const* argv = objv + skipped_argc;
if (argc == 2)
{
moving = true;
if (get_vector2_from_obj(interp, *argv, position, NULL, NULL) == TCL_ERROR)
return TCL_ERROR;
++argv;
}
else if (argc != 1)
{
Tcl_WrongNumArgs(interp, skipped_argc, objv, "?position? string");
return TCL_ERROR;
}
if (moving)
mvwaddstr(win, position[0], position[1], Tcl_GetString(*argv));
else
waddstr(win, Tcl_GetString(*argv));
return TCL_OK;
}
TCL_METHOD_DEF(window, getch,
UNUSED ClientData clientData,
Tcl_Interp* interp,
Tcl_ObjectContext context,
int objc,
Tcl_Obj* const objv[])
{
WINDOW* win = Tcl_ObjectGetMetadata(Tcl_ObjectContextObject(context), &pdc_window_meta);
int skipped_argc = Tcl_ObjectContextSkippedArgs(context);
if (objc - skipped_argc != 0)
{
Tcl_WrongNumArgs(interp, skipped_argc, objv, NULL);
return TCL_ERROR;
}
wint_t intchar;
int getret = wget_wch(win, &intchar);
ASSERT(intchar != WEOF);
if (getret == OK)
{
wchar_t character[2] = { intchar, L'\0' };
char utf8[TCL_UTF_MAX + 1];
PDC_wcstombs(utf8, character, TCL_UTF_MAX + 1);
Tcl_SetResult(interp, utf8, TCL_VOLATILE);
}
else if (getret == KEY_CODE_YES)
{
ASSERT(intchar - KEY_MIN < sizeof keycodes);
Tcl_SetObjResult(interp, Tcl_NewStringObj(keycodes[intchar - KEY_MIN], -1));
}
else
{
ASSERT(getret == ERR);
}
return TCL_OK;
}
TCL_METHOD_DEF(window, move,
UNUSED ClientData clientData,
Tcl_Interp* interp,
Tcl_ObjectContext context,
int objc,
Tcl_Obj* const objv[])
{
WINDOW* win = Tcl_ObjectGetMetadata(Tcl_ObjectContextObject(context), &pdc_window_meta);
int position[2];
int skipped_argc = Tcl_ObjectContextSkippedArgs(context);
int argc = objc - skipped_argc;
Tcl_Obj* const* argv = objv + skipped_argc;
if (argc != 1)
{
Tcl_WrongNumArgs(interp, skipped_argc, objv, "position");
return TCL_ERROR;
}
if (get_vector2_from_obj(interp, *argv, position, NULL, NULL) == TCL_ERROR)
return TCL_ERROR;
ASSERT(wmove(win, position[0], position[1]) == OK);
return TCL_OK;
}
TCL_METHOD_DEF(window, refresh,
UNUSED ClientData clientData,
Tcl_Interp* interp,
Tcl_ObjectContext context,
int objc,
Tcl_Obj* const objv[])
{
WINDOW* win = Tcl_ObjectGetMetadata(Tcl_ObjectContextObject(context), &pdc_window_meta);
bool nout = false;
int skipped_argc = Tcl_ObjectContextSkippedArgs(context);
int argc = objc - skipped_argc;
Tcl_Obj* const* argv = objv + skipped_argc;
if (argc == 1)
{
if (!Tcl_StringMatch(Tcl_GetString(*argv), "-nout"))
{
Tcl_SetObjResult(interp, Tcl_Format(interp, "bad option \"%s\": must be -nout", 1, argv));
return TCL_ERROR;
}
nout = true;
}
else if (argc > 1)
{
Tcl_WrongNumArgs(interp, skipped_argc, objv, "?-nout?");
return TCL_ERROR;
}
if (nout)
ASSERT(wnoutrefresh(win) == OK);
else
ASSERT(wrefresh(win) == OK);
return TCL_OK;
}
TCL_METHOD_DEF(window, getyx,
UNUSED ClientData clientData,
Tcl_Interp* interp,
Tcl_ObjectContext context,
int objc,
Tcl_Obj* const objv[])
{
WINDOW* win = Tcl_ObjectGetMetadata(Tcl_ObjectContextObject(context), &pdc_window_meta);
int position[2];
int skipped_argc = Tcl_ObjectContextSkippedArgs(context);
int argc = objc - skipped_argc;
Tcl_Obj* const* argv = objv + skipped_argc;
if (argc == 0)
getyx(win, position[0], position[1]);
else if (argc == 1)
{
const char* opt = Tcl_GetString(argv[0]);
if (Tcl_StringMatch(opt, "-par"))
getparyx(win, position[0], position[1]);
else if (Tcl_StringMatch(opt, "-beg"))
getbegyx(win, position[0], position[1]);
else if (Tcl_StringMatch(opt, "-max"))
getmaxyx(win, position[0], position[1]);
else
{
Tcl_SetObjResult(interp, Tcl_Format(interp, "bad option \"%s\": must be -par, -beg or -max", 1, argv));
return TCL_ERROR;
}
}
else if (argc > 1)
{
Tcl_WrongNumArgs(interp, skipped_argc, objv, "?option?");
return TCL_ERROR;
}
Tcl_Obj* position_objs[2];
for (int i = 0; i < 2; ++i)
{
position_objs[i] = Tcl_NewIntObj(position[i]);
}
Tcl_SetObjResult(interp, Tcl_NewListObj(2, position_objs));
return TCL_OK;
}
TCL_METHOD_DEF(window, opt,
UNUSED ClientData clientData,
Tcl_Interp* interp,
Tcl_ObjectContext context,
int objc,
Tcl_Obj* const objv[])
{
WINDOW* win = Tcl_ObjectGetMetadata(Tcl_ObjectContextObject(context), &pdc_window_meta);
int skipped_argc = Tcl_ObjectContextSkippedArgs(context);
int argc = objc - skipped_argc;
Tcl_Obj* const* argv = objv + skipped_argc;
if (argc != 2)
{
Tcl_WrongNumArgs(interp, skipped_argc, objv, "option value");
return TCL_ERROR;
}
Tcl_Obj* ret;
int value;
const char* option_str = Tcl_GetString(argv[0]);
if (Tcl_StringMatch(option_str, "keypad"))
{
if (Tcl_GetBooleanFromObj(interp, argv[1], &value) == TCL_ERROR)
return TCL_ERROR;
ASSERT(keypad(win, value) == OK);
}
else if (Tcl_StringMatch(option_str, "delay"))
{
if (Tcl_GetBooleanFromObj(interp, argv[1], &value) == TCL_ERROR)
return TCL_ERROR;
ASSERT(nodelay(win, !value) == OK);
}
else if (Tcl_StringMatch(option_str, "timeout"))
{
ASSERT_TCLOK(Tcl_GetIntFromObj(interp, argv[1], &value));
wtimeout(win, value);
}
else if (Tcl_StringMatch(option_str, "immedok"))
{
if (Tcl_GetBooleanFromObj(interp, argv[1], &value) == TCL_ERROR)
return TCL_ERROR;
immedok(win, value);
}
else if (Tcl_StringMatch(option_str, "leaveok"))
{
if (Tcl_GetBooleanFromObj(interp, argv[1], &value) == TCL_ERROR)
return TCL_ERROR;
ASSERT(leaveok(win, value) == OK);
}
else if (Tcl_StringMatch(option_str, "scrreg"))
{
int pos[2];
if (get_vector2_from_obj(interp, argv[1], pos, "top-margin", "bottom-margin") == TCL_ERROR)
return TCL_ERROR;
ASSERT(wsetscrreg(win, pos[0], pos[1]) == OK);
}
else if (Tcl_StringMatch(option_str, "scrollok"))
{
if (Tcl_GetBooleanFromObj(interp, argv[1], &value) == TCL_ERROR)
return TCL_ERROR;
ASSERT(scrollok(win, value) == OK);
}
else if (Tcl_StringMatch(option_str, "syncok"))
{
if (Tcl_GetBooleanFromObj(interp, argv[1], &value) == TCL_ERROR)
return TCL_ERROR;
ASSERT(syncok(win, value) == OK);
}
else
{
ret = Tcl_Format(interp, "unknown option %s: must be keypad, delay, timeout, immedok, leaveok, scrreg, scrollok, or syncok", 1, argv);
Tcl_SetObjResult(interp, ret);
return TCL_ERROR;
}
return TCL_OK;
}
static int pdc_beep(
UNUSED ClientData clientData,
Tcl_Interp* interp,
int objc,
Tcl_Obj* const objv[])
{
if (objc != 1)
{
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
beep();
return TCL_OK;
}
static int pdc_flash(
UNUSED ClientData clientData,
Tcl_Interp* interp,
int objc,
Tcl_Obj* const objv[])
{
if (objc != 1)
{
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
flash();
return TCL_OK;
}
static int pdc_doupdate(
UNUSED ClientData clientData,
Tcl_Interp* interp,
int objc,
Tcl_Obj* const objv[])
{
if (objc != 1)
{
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
ASSERT(doupdate() == OK);
return TCL_OK;
}
static int pdc_flushinp(
UNUSED ClientData clientData,
Tcl_Interp* interp,
int objc,
Tcl_Obj* const objv[])
{
if (objc != 1)
{
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
ASSERT(flushinp() == OK);
return TCL_OK;
}
static int pdc_opt(
UNUSED ClientData clientData,
Tcl_Interp* interp,
int objc,
Tcl_Obj* const objv[])
{
if (objc != 3)
{
Tcl_WrongNumArgs(interp, 1, objv, "option value");
return TCL_ERROR;
}
int value;
if (Tcl_GetBooleanFromObj(interp, objv[2], &value) == TCL_ERROR)
return TCL_ERROR;
const char* option_str = Tcl_GetString(objv[1]);
if (Tcl_StringMatch(option_str, "cbreak"))
{
if (value)
ASSERT(cbreak() == OK);
else
ASSERT(nocbreak() == OK);
}
else if (Tcl_StringMatch(option_str, "echo"))
{
if (value)
ASSERT(echo() == OK);
else
ASSERT(noecho() == OK);
}
else if (Tcl_StringMatch(option_str, "nl"))
{
if (value)
ASSERT(nl() == OK);
else
ASSERT(nonl() == OK);
}
else if (Tcl_StringMatch(option_str, "trace"))
{
if (value)
traceon();
else
traceoff();
}
else
{
Tcl_SetObjResult(interp, Tcl_Format(interp, "unknown option %s: must be cbreak, echo, nl, or trace", 1, objv + 1));
return TCL_ERROR;
}
return TCL_OK;
}
PUBLIC int Tclpdc_Init(Tcl_Interp *interp)
{
initscr();
if (has_colors())
{
if (start_color() != OK)
return TCL_ERROR;
}
printf("%s\n%s (%s)\n", curses_version(), termname(), longname());
if (Tcl_InitStubs(interp, "8.6", 0) == NULL || Tcl_OOInitStubs(interp) == NULL)
return TCL_ERROR;
Tcl_Class pdc_window;
{
Tcl_Class oo_class = Tcl_GetObjectAsClass(Tcl_GetObjectFromObj(interp, Tcl_NewStringObj("oo::class", -1)));
Tcl_Object pdc_window_object = Tcl_NewObjectInstance(
interp,
oo_class,
"pdc::window",
NULL,
0,
NULL,
0);
pdc_window = Tcl_GetObjectAsClass(pdc_window_object);
}
Tcl_NewMethod(interp, pdc_window, Tcl_NewStringObj("add", -1), 1, &TCL_METHODTYPE(window, add), NULL);
Tcl_NewMethod(interp, pdc_window, Tcl_NewStringObj("getch", -1), 1, &TCL_METHODTYPE(window, getch), NULL);
Tcl_NewMethod(interp, pdc_window, Tcl_NewStringObj("move", -1), 1, &TCL_METHODTYPE(window, move), NULL);
Tcl_NewMethod(interp, pdc_window, Tcl_NewStringObj("refresh", -1), 1, &TCL_METHODTYPE(window, refresh), NULL);
Tcl_NewMethod(interp, pdc_window, Tcl_NewStringObj("getyx", -1), 1, &TCL_METHODTYPE(window, getyx), NULL);
Tcl_NewMethod(interp, pdc_window, Tcl_NewStringObj("opt", -1), 1, &TCL_METHODTYPE(window, opt), NULL);
Tcl_CreateObjCommand(interp, "pdc::beep", pdc_beep, NULL, NULL);
Tcl_CreateObjCommand(interp, "pdc::flash", pdc_flash, NULL, NULL);
Tcl_CreateObjCommand(interp, "pdc::doupdate", pdc_doupdate, NULL, NULL);
Tcl_CreateObjCommand(interp, "pdc::flushinp", pdc_flushinp, NULL, NULL);
Tcl_CreateObjCommand(interp, "pdc::opt", pdc_opt, NULL, NULL);
Tcl_Object pdc_stdscr = Tcl_NewObjectInstance(interp, pdc_window, "pdc::stdscr", NULL, 0, NULL, 0);
Tcl_ObjectSetMetadata(pdc_stdscr, &pdc_window_meta, stdscr);
Tcl_Object pdc_curscr = Tcl_NewObjectInstance(interp, pdc_window, "pdc::curscr", NULL, 0, NULL, 0);
Tcl_ObjectSetMetadata(pdc_curscr, &pdc_window_meta, curscr);
return Tcl_PkgProvide(interp, "PDCurses", "1.0");
}