applications/luci-multiwan: use new uci disable option instead of disabling the init...
[project/luci.git] / contrib / luacurses / src / luacurses.c
1
2 #include <stdlib.h>
3
4 #include <lua.h>
5 #include <lualib.h>
6 #include <lauxlib.h>
7
8 #include <curses.h>
9 #include "luacurses.h"
10
11 SCREEN* luacurses_toscreen(lua_State* L, int index)
12 {
13     SCREEN** pscreen = (SCREEN**) luaL_checkudata(L, index, MKLUALIB_META_CURSES_SCREEN);
14     if (!pscreen) luaL_argerror(L, index, "bad screen");
15     if (!*pscreen) luaL_error(L, "attempt to use invalid screen");
16     return *pscreen;
17 }
18
19 SCREEN** luacurses_newscreen(lua_State* L)
20 {
21     SCREEN** pscreen = (SCREEN**) lua_newuserdata(L, sizeof(SCREEN*));
22     *pscreen = 0;
23     luaL_getmetatable(L, MKLUALIB_META_CURSES_SCREEN);
24     lua_setmetatable(L, -2);
25     return pscreen;
26 }
27
28 void luacurses_regscreen(lua_State* L, const char* name, SCREEN* userdata)
29 {
30     lua_pushstring(L, name);
31     SCREEN** pscreen = luacurses_newscreen(L);
32     *pscreen = userdata;
33     lua_settable(L, -3);
34 }
35
36 WINDOW* luacurses_towindow(lua_State* L, int index)
37 {
38     WINDOW** pwindow = (WINDOW**) luaL_checkudata(L, index, MKLUALIB_META_CURSES_WINDOW);
39     if (!pwindow) luaL_argerror(L, index, "bad window");
40     if (!*pwindow) luaL_error(L, "attempt to use invalid window");
41     return *pwindow;
42 }
43
44 WINDOW** luacurses_newwindow(lua_State* L)
45 {
46     WINDOW** pwindow = (WINDOW**) lua_newuserdata(L, sizeof(WINDOW*));
47     *pwindow = 0;
48     luaL_getmetatable(L, MKLUALIB_META_CURSES_WINDOW);
49     lua_setmetatable(L, -2);
50     return pwindow;
51 }
52
53 void luacurses_regwindow(lua_State* L, const char* name, WINDOW* userdata)
54 {
55     lua_pushstring(L, name);
56     WINDOW** pwindow = luacurses_newwindow(L);
57     *pwindow = userdata;
58     lua_settable(L, -3);
59 }
60
61 FILE* tofile(lua_State* L, int index)
62 {
63     FILE** pf = (FILE**) luaL_checkudata(L, index, MKLUALIB_META_CURSES_FILE);
64     if (!pf) luaL_argerror(L, index, "bad file");
65     if (!*pf) luaL_error(L, "attempt to use invalid file");
66     return *pf;
67 }
68
69 FILE** newfile(lua_State* L)
70 {
71     FILE** pf = (FILE**) lua_newuserdata(L, sizeof(FILE*));
72     *pf = 0;
73     luaL_getmetatable(L, MKLUALIB_META_CURSES_FILE);
74     lua_setmetatable(L, -2);
75     return pf;
76 }
77
78 void luacurses_regfile(lua_State* L, const char* name, FILE* f)
79 {
80     lua_pushstring(L, name);
81     FILE** pf = newfile(L);
82     *pf = f;
83     lua_settable(L, -3);
84 }
85
86 char* luacurses_wgetnstr(WINDOW* w, int n)
87 {
88     char* s = (char*) malloc(n + 1);
89     wgetnstr(w, s, n);
90     return s;
91 }
92
93 char* luacurses_window_tostring(WINDOW* w)
94 {
95     char* buf = (char*) malloc(64);
96     sprintf(buf, "window %p", w);
97     return buf;
98 }
99
100 char* luacurses_screen_tostring(SCREEN* s)
101 {
102     char* buf = (char*) malloc(64);
103     sprintf(buf, "screen %p", s);
104     return buf;  
105 }
106
107 bool luacurses_getmouse(short* id, int* x, int* y, int* z, mmask_t* bstate)
108 {
109     MEVENT e;
110     int res = getmouse(&e);
111
112     *id = e.id;
113     *x = e.x;
114     *y = e.y;
115     *z = e.z;
116     *bstate = e.bstate;
117     return (res == OK);
118 }
119
120 bool luacurses_ungetmouse (short id, int x, int y, int z, mmask_t bstate)
121 {
122     MEVENT e;
123     e.id = id;
124     e.x = x;
125     e.y = y;
126     e.z = z;
127     e.bstate = bstate;
128     return (ungetmouse(&e) == OK);
129 }
130
131 mmask_t luacurses_addmousemask(mmask_t m)
132 {
133     mmask_t old;
134     mousemask(m, &old);
135     return mousemask(old | m, 0);
136 }
137