1 /* This module implements and interface to the Newt library */
2 #include <stdio.h>
3 #include <slang.h>
4 #include <newt.h>
5 
6 SLANG_MODULE(newt);
7 
8 static int Ok_To_Draw;
9 
init(void)10 static void init (void)
11 {
12    newtInit ();
13    Ok_To_Draw = 1;
14 }
15 
cls(void)16 static void cls (void)
17 {
18    if (Ok_To_Draw)
19      newtCls ();
20 }
21 
draw_root_text(int * c,int * r,char * s)22 static void draw_root_text (int *c, int *r, char *s)
23 {
24    if (Ok_To_Draw)
25      newtDrawRootText (*c, *r, s);
26 }
27 
open_window(int * c,int * r,int * dc,int * dr,char * title)28 static void open_window (int *c, int *r, int *dc, int *dr, char *title)
29 {
30    if (Ok_To_Draw)
31      newtOpenWindow (*c, *r, *dc, *dr, title);
32 }
33 
refresh(void)34 static void refresh (void)
35 {
36    if (Ok_To_Draw)
37      newtRefresh ();
38 }
39 
finished(void)40 static void finished (void)
41 {
42    if (Ok_To_Draw)
43      newtFinished ();
44    Ok_To_Draw = 0;
45 }
46 
47 #define I SLANG_INT_TYPE
48 #define S SLANG_STRING_TYPE
49 
50 static SLang_Intrin_Fun_Type Module_Funs [] =
51 {
52    MAKE_INTRINSIC_0("newtInit", init, SLANG_VOID_TYPE),
53    MAKE_INTRINSIC_0("newtCls", cls, SLANG_VOID_TYPE),
54    MAKE_INTRINSIC_IIS("newtDrawRootText", draw_root_text, SLANG_VOID_TYPE),
55    MAKE_INTRINSIC_5("newtOpenWindow", open_window, SLANG_VOID_TYPE, I,I,I,I,S),
56    MAKE_INTRINSIC_0("newtRefresh", refresh, SLANG_VOID_TYPE),
57    MAKE_INTRINSIC_0("NewtFinished", finished, SLANG_VOID_TYPE),
58 
59    SLANG_END_TABLE
60 };
61 
62 static SLang_Intrin_Var_Type Module_Variables [] =
63 {
64    SLANG_END_TABLE
65 };
66 
67 static SLang_IConstant_Type Module_Constants [] =
68 {
69    SLANG_END_TABLE
70 };
71 
init_newt_module_ns(char * ns)72 int init_newt_module_ns (char *ns)
73 {
74    if ((-1 == SLns_add_intrin_fun_table (ns, Module_Funs, "__NEWT__"))
75        || (-1 == SLns_add_intrin_var_table (ns, Module_Variables, NULL))
76        || (-1 == SLns_add_iconstant_table (ns, Module_Constants, NULL)))
77      return -1;
78 
79    Ok_To_Draw = 0;
80 
81    (void) SLang_add_cleanup_function (finished);
82 
83    return 0;
84 }
85 
86 /* This function is optional */
deinit_newt_module(void)87 void deinit_newt_module (void)
88 {
89    finished ();
90 }
91