summaryrefslogtreecommitdiffhomepage
path: root/contrib/luacurses/src/luacurses.c
blob: 0cbd2fe6a487df559616c9377480d327d974be2f (plain)
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

#include <stdlib.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include <curses.h>
#include "luacurses.h"

SCREEN* luacurses_toscreen(lua_State* L, int index)
{
    SCREEN** pscreen = (SCREEN**) luaL_checkudata(L, index, MKLUALIB_META_CURSES_SCREEN);
    if (!pscreen) luaL_argerror(L, index, "bad screen");
    if (!*pscreen) luaL_error(L, "attempt to use invalid screen");
    return *pscreen;
}

SCREEN** luacurses_newscreen(lua_State* L)
{
    SCREEN** pscreen = (SCREEN**) lua_newuserdata(L, sizeof(SCREEN*));
    *pscreen = 0;
    luaL_getmetatable(L, MKLUALIB_META_CURSES_SCREEN);
    lua_setmetatable(L, -2);
    return pscreen;
}

void luacurses_regscreen(lua_State* L, const char* name, SCREEN* userdata)
{
    lua_pushstring(L, name);
    SCREEN** pscreen = luacurses_newscreen(L);
    *pscreen = userdata;
    lua_settable(L, -3);
}

WINDOW* luacurses_towindow(lua_State* L, int index)
{
    WINDOW** pwindow = (WINDOW**) luaL_checkudata(L, index, MKLUALIB_META_CURSES_WINDOW);
    if (!pwindow) luaL_argerror(L, index, "bad window");
    if (!*pwindow) luaL_error(L, "attempt to use invalid window");
    return *pwindow;
}

WINDOW** luacurses_newwindow(lua_State* L)
{
    WINDOW** pwindow = (WINDOW**) lua_newuserdata(L, sizeof(WINDOW*));
    *pwindow = 0;
    luaL_getmetatable(L, MKLUALIB_META_CURSES_WINDOW);
    lua_setmetatable(L, -2);
    return pwindow;
}

void luacurses_regwindow(lua_State* L, const char* name, WINDOW* userdata)
{
    lua_pushstring(L, name);
    WINDOW** pwindow = luacurses_newwindow(L);
    *pwindow = userdata;
    lua_settable(L, -3);
}

FILE* tofile(lua_State* L, int index)
{
    FILE** pf = (FILE**) luaL_checkudata(L, index, MKLUALIB_META_CURSES_FILE);
    if (!pf) luaL_argerror(L, index, "bad file");
    if (!*pf) luaL_error(L, "attempt to use invalid file");
    return *pf;
}

FILE** newfile(lua_State* L)
{
    FILE** pf = (FILE**) lua_newuserdata(L, sizeof(FILE*));
    *pf = 0;
    luaL_getmetatable(L, MKLUALIB_META_CURSES_FILE);
    lua_setmetatable(L, -2);
    return pf;
}

void luacurses_regfile(lua_State* L, const char* name, FILE* f)
{
    lua_pushstring(L, name);
    FILE** pf = newfile(L);
    *pf = f;
    lua_settable(L, -3);
}

char* luacurses_wgetnstr(WINDOW* w, int n)
{
    char* s = (char*) malloc(n + 1);
    wgetnstr(w, s, n);
    return s;
}

char* luacurses_window_tostring(WINDOW* w)
{
    char* buf = (char*) malloc(64);
    sprintf(buf, "window %p", w);
    return buf;
}

char* luacurses_screen_tostring(SCREEN* s)
{
    char* buf = (char*) malloc(64);
    sprintf(buf, "screen %p", s);
    return buf;  
}

bool luacurses_getmouse(short* id, int* x, int* y, int* z, mmask_t* bstate)
{
    MEVENT e;
    int res = getmouse(&e);

    *id = e.id;
    *x = e.x;
    *y = e.y;
    *z = e.z;
    *bstate = e.bstate;
    return (res == OK);
}

bool luacurses_ungetmouse (short id, int x, int y, int z, mmask_t bstate)
{
    MEVENT e;
    e.id = id;
    e.x = x;
    e.y = y;
    e.z = z;
    e.bstate = bstate;
    return (ungetmouse(&e) == OK);
}

mmask_t luacurses_addmousemask(mmask_t m)
{
    mmask_t old;
    mousemask(m, &old);
    return mousemask(old | m, 0);
}