Add luacurses to contrib
This commit is contained in:
parent
f28422324d
commit
36c7609063
14 changed files with 4083 additions and 0 deletions
8
contrib/luacurses/Makefile
Normal file
8
contrib/luacurses/Makefile
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
include ../../build/module.mk
|
||||||
|
include ../../build/config.mk
|
||||||
|
include ../../build/gccconfig.mk
|
||||||
|
|
||||||
|
compile:
|
||||||
|
mkdir -p dist$(LUA_LIBRARYDIR)
|
||||||
|
$(CC) $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(SHLIB_FLAGS) -pedantic \
|
||||||
|
-Wall $(FPIC) $(LUA_CFLAGS) -o dist$(LUA_LIBRARYDIR)/curses.so src/src/curses.c src/src/luacurses.c
|
15
contrib/luacurses/src/Makefile
Normal file
15
contrib/luacurses/src/Makefile
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
|
||||||
|
SRC=src/curses.c src/luacurses.c
|
||||||
|
INCLUDE=/usr/include/lua5.1
|
||||||
|
CFLAGS=-shared -ansi -Wall -O2 -I$(INCLUDE) -fpic
|
||||||
|
|
||||||
|
all: lib/curses.so
|
||||||
|
|
||||||
|
lib/curses.so: $(SRC)
|
||||||
|
if ! [ -d lib ]; then mkdir lib; fi
|
||||||
|
gcc $(CFLAGS) -o lib/curses.so $(SRC)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -Rf lib
|
||||||
|
|
||||||
|
|
BIN
contrib/luacurses/src/doc/curses.pdf
Normal file
BIN
contrib/luacurses/src/doc/curses.pdf
Normal file
Binary file not shown.
BIN
contrib/luacurses/src/doc/curses.ps.gz
Normal file
BIN
contrib/luacurses/src/doc/curses.ps.gz
Normal file
Binary file not shown.
3630
contrib/luacurses/src/src/curses.c
Normal file
3630
contrib/luacurses/src/src/curses.c
Normal file
File diff suppressed because it is too large
Load diff
137
contrib/luacurses/src/src/luacurses.c
Normal file
137
contrib/luacurses/src/src/luacurses.c
Normal file
|
@ -0,0 +1,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);
|
||||||
|
}
|
||||||
|
|
38
contrib/luacurses/src/src/luacurses.h
Normal file
38
contrib/luacurses/src/src/luacurses.h
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
|
||||||
|
#include <curses.h>
|
||||||
|
|
||||||
|
#define MKLUALIB_META_CURSES_SCREEN "SCREEN*"
|
||||||
|
|
||||||
|
SCREEN* luacurses_toscreen(lua_State* L, int index);
|
||||||
|
SCREEN** luacurses_newscreen(lua_State* L);
|
||||||
|
void luacurses_regscreen(lua_State* L, const char* name, SCREEN* userdata);
|
||||||
|
|
||||||
|
#define MKLUALIB_META_CURSES_WINDOW "WINDOW*"
|
||||||
|
|
||||||
|
WINDOW* luacurses_towindow(lua_State* L, int index);
|
||||||
|
WINDOW** luacurses_newwindow(lua_State* L);
|
||||||
|
void luacurses_regwindow(lua_State* L, const char* name, WINDOW* userdata);
|
||||||
|
|
||||||
|
#define MKLUALIB_META_CURSES_FILE "FILE*"
|
||||||
|
|
||||||
|
FILE* tofile(lua_State* L, int index);
|
||||||
|
FILE** newfile(lua_State* L);
|
||||||
|
void luacurses_regfile(lua_State* L, const char* name, FILE* f);
|
||||||
|
|
||||||
|
char* luacurses_wgetnstr(WINDOW* w, int n);
|
||||||
|
char* luacurses_wgetstr(WINDOW* w);
|
||||||
|
|
||||||
|
#define luacurses_mvwgetnstr(w, y, x, n) (wmove(w, y, x) == ERR ? 0 : luacurses_wgetnstr(w, n))
|
||||||
|
#define luacurses_getnstr(n) luacurses_wgetnstr(stdscr, n)
|
||||||
|
#define luacurses_mvgetnstr(y, x, n) luacurses_mvwgetnstr(stdscr, y, x, n)
|
||||||
|
|
||||||
|
char* luacurses_window_tostring(WINDOW* w);
|
||||||
|
char* luacurses_screen_tostring(SCREEN* s);
|
||||||
|
|
||||||
|
#define luacurses_window_free(w) {delwin(w); w = 0;}
|
||||||
|
#define luacurses_screen_free(s) {delscreen(s); s = 0;}
|
||||||
|
|
||||||
|
bool luacurses_getmouse(short* id, int* x, int* y, int* z, mmask_t* bstate);
|
||||||
|
bool luacurses_ungetmouse (short id, int x, int y, int z, mmask_t bstate);
|
||||||
|
mmask_t luacurses_addmousemask(mmask_t m);
|
||||||
|
|
49
contrib/luacurses/src/test/filter.lua
Normal file
49
contrib/luacurses/src/test/filter.lua
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
function read_cmd()
|
||||||
|
curses.attron(curses.A_BOLD);
|
||||||
|
curses.addstr("Command: ");
|
||||||
|
curses.attron(underline);
|
||||||
|
local s = "";
|
||||||
|
while (true) do
|
||||||
|
local c = string.char(curses.getch());
|
||||||
|
if (c == '\n') then break; end
|
||||||
|
s = s .. c;
|
||||||
|
end
|
||||||
|
curses.attroff(underline);
|
||||||
|
curses.attroff(curses.A_BOLD);
|
||||||
|
curses.addch("\n");
|
||||||
|
|
||||||
|
return s;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
curses.filter();
|
||||||
|
curses.initscr();
|
||||||
|
curses.cbreak();
|
||||||
|
curses.keypad(curses.stdscr(), TRUE);
|
||||||
|
|
||||||
|
if (curses.has_colors()) then
|
||||||
|
curses.start_color();
|
||||||
|
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK);
|
||||||
|
underline = curses.COLOR_PAIR(1);
|
||||||
|
else
|
||||||
|
underline = curses.A_UNDERLINE;
|
||||||
|
end
|
||||||
|
|
||||||
|
while (true) do
|
||||||
|
local s = read_cmd();
|
||||||
|
if (s == "exit") then break; end
|
||||||
|
curses.reset_shell_mode();
|
||||||
|
io.write("\n");
|
||||||
|
io.flush(io.stdout);
|
||||||
|
os.execute(s);
|
||||||
|
curses.reset_prog_mode();
|
||||||
|
curses.touchwin(curses.stdscr());
|
||||||
|
curses.erase();
|
||||||
|
curses.refresh();
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.endwin();
|
||||||
|
|
12
contrib/luacurses/src/test/getnstr.lua
Normal file
12
contrib/luacurses/src/test/getnstr.lua
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
curses.initscr();
|
||||||
|
|
||||||
|
curses.keypad(curses.stdscr(), true);
|
||||||
|
s = curses.mvgetnstr(10, 10, 10);
|
||||||
|
curses.addstr(s);
|
||||||
|
curses.getch();
|
||||||
|
|
||||||
|
curses.endwin();
|
||||||
|
|
13
contrib/luacurses/src/test/getyx.lua
Normal file
13
contrib/luacurses/src/test/getyx.lua
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
curses.initscr();
|
||||||
|
while (true) do
|
||||||
|
local s = curses.getnstr(1000);
|
||||||
|
curses.addstr(s);
|
||||||
|
curses.addstr(":" .. table.concat({curses.getyx(curses.stdscr())}, ' ') .. "\n");
|
||||||
|
if (s == "exit") then break; end
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.endwin();
|
||||||
|
|
20
contrib/luacurses/src/test/hello.lua
Normal file
20
contrib/luacurses/src/test/hello.lua
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
function show_message(message)
|
||||||
|
local width = string.len(message) + 6;
|
||||||
|
win = curses.newwin(5, width, (curses.LINES() - 5) / 2, (curses.COLS() - width) / 2);
|
||||||
|
win:box('|', '-');
|
||||||
|
win:mvaddstr(2, 3, message);
|
||||||
|
win:getch();
|
||||||
|
win:delwin();
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.initscr();
|
||||||
|
curses.cbreak();
|
||||||
|
curses.mvaddstr((curses.LINES() - 5) / 2, (curses.COLS() - 10) / 2, "Hit any key");
|
||||||
|
curses.getch();
|
||||||
|
show_message("Hello, World!")
|
||||||
|
|
||||||
|
curses.endwin();
|
||||||
|
|
54
contrib/luacurses/src/test/mouse.lua
Normal file
54
contrib/luacurses/src/test/mouse.lua
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
function show_message(m)
|
||||||
|
local width = string.len(m) + 6;
|
||||||
|
local win = curses.newwin(5, width, (lines - 5) / 2, (cols - width) / 2);
|
||||||
|
win:keypad(true);
|
||||||
|
win:attron(curses.COLOR_PAIR(curses.COLOR_RED));
|
||||||
|
win:box('|', '-', '+');
|
||||||
|
win:mvaddstr(2, 3, m);
|
||||||
|
win:refresh();
|
||||||
|
win:getch();
|
||||||
|
win:delwin();
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.initscr();
|
||||||
|
curses.start_color();
|
||||||
|
curses.init_pair(curses.COLOR_BLUE, curses.COLOR_BLUE, curses.COLOR_WHITE);
|
||||||
|
curses.init_pair(curses.COLOR_RED, curses.COLOR_RED, curses.COLOR_WHITE);
|
||||||
|
curses.cbreak();
|
||||||
|
curses.noecho();
|
||||||
|
curses.keypad(curses.stdscr(), true);
|
||||||
|
|
||||||
|
lines = curses.LINES();
|
||||||
|
cols = curses.COLS();
|
||||||
|
|
||||||
|
mmasks =
|
||||||
|
{
|
||||||
|
curses.BUTTON1_CLICKED,
|
||||||
|
curses.BUTTON2_CLICKED,
|
||||||
|
curses.BUTTON3_CLICKED,
|
||||||
|
curses.BUTTON4_CLICKED
|
||||||
|
};
|
||||||
|
|
||||||
|
table.foreachi(mmasks, function(_i, _m) curses.addmousemask(_m) end);
|
||||||
|
curses.attron(curses.COLOR_PAIR(curses.COLOR_BLUE));
|
||||||
|
curses.attron(curses.A_BOLD);
|
||||||
|
curses.mvaddstr((lines - 5) / 2, (cols - 10) / 2, "click");
|
||||||
|
|
||||||
|
curses.refresh();
|
||||||
|
while(true) do
|
||||||
|
local c = curses.getch();
|
||||||
|
if (c == curses.KEY_MOUSE) then
|
||||||
|
local r, id, x, y, z, bstate = curses.getmouse();
|
||||||
|
if (r) then
|
||||||
|
show_message("id = " .. id .. ", x = " .. x .. ", y = " .. y .. ", z = " .. z .. ", bstate = " ..
|
||||||
|
string.format("0x%x", bstate));
|
||||||
|
end
|
||||||
|
break;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.endwin();
|
||||||
|
|
18
contrib/luacurses/src/test/pair.lua
Normal file
18
contrib/luacurses/src/test/pair.lua
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
curses.initscr();
|
||||||
|
|
||||||
|
curses.start_color();
|
||||||
|
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_YELLOW);
|
||||||
|
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_RED);
|
||||||
|
|
||||||
|
for i = 1, 2 do
|
||||||
|
local r, f, b = curses.pair_content(i);
|
||||||
|
curses.attrset(curses.COLOR_PAIR(i));
|
||||||
|
curses.addstr(f .. ", " .. b .. "\n");
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.getch();
|
||||||
|
curses.endwin();
|
||||||
|
|
89
contrib/luacurses/src/test/rain.lua
Normal file
89
contrib/luacurses/src/test/rain.lua
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
|
||||||
|
require("curses");
|
||||||
|
|
||||||
|
curses.initscr();
|
||||||
|
curses.nl();
|
||||||
|
curses.noecho();
|
||||||
|
|
||||||
|
|
||||||
|
if (curses.has_colors()) then
|
||||||
|
curses.start_color();
|
||||||
|
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK);
|
||||||
|
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK);
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.curs_set(0);
|
||||||
|
curses.timeout(0);
|
||||||
|
|
||||||
|
math.randomseed(os.time());
|
||||||
|
|
||||||
|
lines = curses.LINES();
|
||||||
|
cols = curses.COLS();
|
||||||
|
|
||||||
|
xpos = {};
|
||||||
|
ypos = {};
|
||||||
|
r = lines - 4;
|
||||||
|
c = cols - 4;
|
||||||
|
for i = 0, 4 do
|
||||||
|
xpos[i] = c * math.random() + 2;
|
||||||
|
ypos[i] = r * math.random() + 2;
|
||||||
|
end
|
||||||
|
|
||||||
|
function dec(i, max)
|
||||||
|
if (curses.has_colors()) then
|
||||||
|
local z = 3 * math.random();
|
||||||
|
local c = curses.COLOR_PAIR(z);
|
||||||
|
curses.attrset(c);
|
||||||
|
if (math.floor(z) > 0) then
|
||||||
|
curses.attron(curses.A_BOLD);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if (i > 0) then return i - 1;
|
||||||
|
else return max;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(true) do
|
||||||
|
x = c * math.random() + 2;
|
||||||
|
y = r * math.random() + 2;
|
||||||
|
|
||||||
|
curses.mvaddstr(y, x, ".");
|
||||||
|
|
||||||
|
curses.mvaddstr(ypos[i], xpos[i], "o");
|
||||||
|
|
||||||
|
i = dec(i, 4);
|
||||||
|
curses.mvaddstr(ypos[i], xpos[i], "O");
|
||||||
|
|
||||||
|
i = dec(i, 4);
|
||||||
|
curses.mvaddstr(ypos[i] - 1, xpos[i], "-");
|
||||||
|
curses.mvaddstr(ypos[i], xpos[i] - 1, "|.|");
|
||||||
|
curses.mvaddstr(ypos[i] + 1, xpos[i], "-");
|
||||||
|
|
||||||
|
i = dec(i, 4);
|
||||||
|
curses.mvaddstr(ypos[i] - 2, xpos[i], "-");
|
||||||
|
curses.mvaddstr(ypos[i] - 1, xpos[i] - 1, "/ \\");
|
||||||
|
curses.mvaddstr(ypos[i], xpos[i] - 2, "| O |");
|
||||||
|
curses.mvaddstr(ypos[i] + 1, xpos[i] - 1, "\\ /");
|
||||||
|
curses.mvaddstr(ypos[i] + 2, xpos[i], "-");
|
||||||
|
|
||||||
|
i = dec(i, 4);
|
||||||
|
curses.mvaddstr(ypos[i] - 2, xpos[i], " ");
|
||||||
|
curses.mvaddstr(ypos[i] - 1, xpos[i] - 1, " ");
|
||||||
|
curses.mvaddstr(ypos[i], xpos[i] - 2, " ");
|
||||||
|
curses.mvaddstr(ypos[i] + 1, xpos[i] - 1, " ");
|
||||||
|
curses.mvaddstr(ypos[i] + 2, xpos[i], " ");
|
||||||
|
|
||||||
|
|
||||||
|
xpos[i] = x;
|
||||||
|
ypos[i] = y;
|
||||||
|
|
||||||
|
local ch = curses.getch();
|
||||||
|
if (ch == string.byte('q', 1)) or (ch == string.byte('Q', 1)) then break; end
|
||||||
|
curses.refresh();
|
||||||
|
curses.napms(50);
|
||||||
|
end
|
||||||
|
|
||||||
|
curses.endwin();
|
||||||
|
|
Loading…
Reference in a new issue