Merge branch 'master' into framebuffer

This commit is contained in:
Łukasz Magiera 2016-02-28 12:56:28 +01:00
commit f58770449a
18 changed files with 398 additions and 158 deletions

View File

@ -13,7 +13,7 @@ SOURCE = src/c
CORELUA = src/lua/core CORELUA = src/lua/core
RESOURCES = resources RESOURCES = resources
LIBS=-lm -lssl -lcrypto LIBS=-lm -lssl -lcrypto -levent_core
INCLUDES=-I$(SOURCE) -Isrc/c/lib/lua -Iinclude -Idependencies/include -Idependencies/include-$(PREFIX) INCLUDES=-I$(SOURCE) -Isrc/c/lib/lua -Iinclude -Idependencies/include -Idependencies/include-$(PREFIX)
@ -27,20 +27,35 @@ BUILDDIRECTORIES = $(patsubst $(SOURCE)/%, $(BUILD)%, $(SRCDIRECTORIES))
CFILES = $(shell find $(SOURCE) -type f -name '*.c') CFILES = $(shell find $(SOURCE) -type f -name '*.c')
OBJECTS := $(patsubst $(SOURCE)/%.c, $(BUILD)%.c.o, $(CFILES)) OBJECTS := $(patsubst $(SOURCE)/%.c, $(BUILD)%.c.o, $(CFILES))
OUTNAME = lupi
# Targets # Targets
# Pseudo Targets # Pseudo Targets
debug: CFLAGS+= -g -DLOGGING -DDEBUG debug: CFLAGS+= -g -DLOGGING -DDEBUG
debug: build debug: build
winexe: $(BUILD)$(OUTNAME)
cp $(BUILD)$(OUTNAME) $(BUILD)$(OUTNAME).exe
win: LIBS+= -lws2_32
win: all winexe
win-build: LIBS+= -lws2_32
win-build: build winexe
win-debug: LIBS+= -lws2_32
win-debug: debug winexe
$(BUILDDIRECTORIES): $(BUILDDIRECTORIES):
mkdir -p $@ mkdir -p $@
build: smallclean $(BUILDDIRECTORIES) resources $(BUILD)lupi build: smallclean $(BUILDDIRECTORIES) resources $(BUILD)$(OUTNAME)
all: clean build all: clean build
$(BUILD)lupi: $(OBJECTS) $(BUILD)$(OUTNAME): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBS)
$(BUILD)%.c.o: $(SOURCE)/%.c $(BUILD)%.c.o: $(SOURCE)/%.c

View File

@ -4,7 +4,6 @@
#ifndef LUPI_H #ifndef LUPI_H
#define LUPI_H #define LUPI_H
/* #define LOGGING */
#ifdef LOGGING #ifdef LOGGING
void logn(const char *message); void logn(const char *message);
void logi(int message); void logi(int message);
@ -28,14 +27,7 @@ void fb_start(lua_State *L);
void setup_modules(lua_State *L); void setup_modules(lua_State *L);
void termutils_start(lua_State *L); void termutils_start(lua_State *L);
void internet_start(lua_State *L); void internet_start(lua_State *L);
void epoll_prepare(); void event_prepare();
int epoll_pull(int timeout); int event_pull(int timeout);
struct lupi_event_handler {
int (*handler)(int, void*); /* FD, data, return number of pushed events */
/* TODO: doc? */
int fd;
void* data;
};
#endif #endif

View File

@ -7,33 +7,37 @@ OUT=$TOOL
if [ $# -lt 1 ] if [ $# -lt 1 ]
then then
echo "Usage : $0 [all|arm32|x86_64|i486]" echo "Usage : $0 [all|arm32|x86_64|i486|x86_64-win|i686-win] <libevent|libressl>"
exit exit
fi fi
case "$1" in case "$1" in
all ) all )
TARGETS=(arm32 i486 x86_64) TARGETS=(arm32 i486 x86_64 x86_64-win i686-win)
for i in ${TARGETS[@]}; do for i in ${TARGETS[@]}; do
./$0 $i ./$0 $i $2
done done
;; ;;
arm32 ) arm32 )
TOOL=arm-linux-musleabihf TOOL=arm-linux-musleabihf
OUT=$TOOL OUT=$TOOL
OPENSSL_TARGET=linux-generic32
;; ;;
i486 ) i486 )
TOOL=i486-linux-musl TOOL=i486-linux-musl
OUT=$TOOL OUT=$TOOL
OPENSSL_TARGET=linux-generic32
;; ;;
x86_64 ) x86_64 )
TOOL=x86_64-linux-musl TOOL=x86_64-linux-musl
OUT=$TOOL OUT=$TOOL
OPENSSL_TARGET=linux-generic64
;; ;;
x86_64-win )
TOOL=x86_64-w64-mingw32
OUT=$TOOL
;;
i686-win )
TOOL=i686-w64-mingw32
OUT=$TOOL
;;
*) echo "Invalid target!" ; exit 1 *) echo "Invalid target!" ; exit 1
;; ;;
esac esac
@ -46,15 +50,52 @@ rm -rf dependencies/lib-$OUT
mkdir -p dependencies/lib-$OUT mkdir -p dependencies/lib-$OUT
cd dependencies cd dependencies
git clone https://github.com/libressl-portable/portable.git libressl
cd libressl
./autogen.sh
./configure --host=$TOOL
make clean
make -j8
mkdir -p ../include/openssl #################
mkdir -p ../include-$OUT/openssl # LibreSSL
cp -rfv crypto/.libs/libcrypto.a ../lib-$OUT
cp -rfv ssl/.libs/libssl.a ../lib-$OUT if [ $2 = "libressl" ] || [ $# -lt 2 ]; then
cp -rfv include/openssl/* ../include-$OUT/openssl
git clone https://github.com/libressl-portable/portable.git libressl
cd libressl
./autogen.sh
./configure --host=$TOOL
make clean
make -j8
mkdir -p ../include/openssl
mkdir -p ../include-$OUT/openssl
cp -rfv crypto/.libs/libcrypto.a ../lib-$OUT
cp -rfv ssl/.libs/libssl.a ../lib-$OUT
cp -rfv include/openssl/* ../include-$OUT/openssl
cd ..
fi
#################
# LibEvent
if [ $2 = "libevent" ] || [ $# -lt 2 ]; then
if [ ! -f "libevent-2.0.22-stable.tar.gz" ]; then
wget "https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz"
tar xzvf "libevent-2.0.22-stable.tar.gz"
fi
cd libevent-2.0.22-stable
./autogen.sh
./configure --host=$TOOL
make clean
make -j8
mkdir -p ../include/event2
mkdir -p ../include-$OUT/event2
cp -rfv include/event2/* ../include-$OUT/event2
cp -rfv .libs/libevent.a ../lib-$OUT
cp -rfv .libs/libevent_core.a ../lib-$OUT
cp -rfv .libs/libevent_extra.a ../lib-$OUT
cp -rfv .libs/libevent_pthreads.a ../lib-$OUT
fi

View File

@ -23,9 +23,11 @@ generate() {
filename="$(basename "$file")" filename="$(basename "$file")"
if [ -d "$file" ] if [ -d "$file" ]
then then
echo "Enter directory $file"
generate $file $2 $3 ${PREFIX}${filename}_ generate $file $2 $3 ${PREFIX}${filename}_
else else
filename="${filename%.*}" filename="${filename%.*}"
echo "Generate $PREFIX$filename"
echo "extern char $PREFIX$filename[];" >> "$OUTPUTH" echo "extern char $PREFIX$filename[];" >> "$OUTPUTH"

View File

@ -1,70 +0,0 @@
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <unistd.h>
static int epollfd;
static int handleStdin(int fd, void* data) {
char buf;
int r = read(fd, &buf, 1); /* TODO: Wide chars? */
if(r > 0) {
/* if(buf == 10) buf = 13; */
lua_State* L = getL();
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_down");
lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "user");
lua_call(L, 5, 0);
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_up");
lua_pushstring(L, "TODO:SetThisUuid");
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "user");
lua_call(L, 5, 0);
return 2;
}
return 0;
}
void epoll_prepare() {
if ((epollfd = epoll_create1(0)) < 0) {
perror("epoll_create");
exit(EXIT_FAILURE);
}
struct lupi_event_handler* stdin_handler = malloc(sizeof(struct lupi_event_handler));
stdin_handler->data = NULL;
stdin_handler->handler = handleStdin;
stdin_handler->fd = STDIN_FILENO;
struct epoll_event stdinEvent;
stdinEvent.events = EPOLLIN | EPOLLPRI;
stdinEvent.data.ptr = stdin_handler;
if ((epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &stdinEvent) < 0)) {
perror("epoll_ctl");
exit(EXIT_FAILURE);
}
}
int epoll_pull(int timeout) {
struct epoll_event evBuffer;
int pushed = 0;
int eres = epoll_wait(epollfd, &evBuffer, 1, timeout);
if(eres > 0) {
struct lupi_event_handler* handler = (struct lupi_event_handler*)evBuffer.data.ptr;
pushed = handler->handler(handler->fd, handler->data);
}
return pushed;
}

76
src/c/event.c Normal file
View File

@ -0,0 +1,76 @@
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef _WIN32
#define WIN32
#endif
#include <event2/event.h>
#include <event2/event_struct.h>
struct event_base *base;
struct event stdinEvent;
int nevt = 0;
static void handleStdin(evutil_socket_t fd, short what, void *ptr) {
char buf;
int r = read(fd, &buf, 1); /* TODO: Wide chars? */
if(r > 0) {
lua_State* L = getL();
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_down");
lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_up");
lua_pushstring(L, "TODO:SetThisUuid");
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
nevt += 2;
}
}
void event_prepare() {
struct event_config* cfg = event_config_new();
event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
base = event_base_new_with_config(cfg);
evutil_make_socket_nonblocking(STDIN_FILENO);
event_assign(&stdinEvent, base, STDIN_FILENO, EV_READ, handleStdin, NULL);
}
static void add_events(struct timeval* timeout) {
event_add(&stdinEvent, timeout);
}
int event_pull(int _timeout) {
if(_timeout > 0) { /* wait max this much time for event */
struct timeval timeout = {_timeout / 1000, (_timeout % 1000) * 1000};
add_events(&timeout);
event_base_loop(base, EVLOOP_ONCE);
} else if(_timeout == 0) { /* Get event without blocking */
add_events(NULL);
event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
} else { /* wait for event to appear */
add_events(NULL);
event_base_loop(base, EVLOOP_ONCE);
}
int n = nevt;
nevt = 0;
return n;
}

View File

@ -1,4 +1,6 @@
#include "lupi.h" #include "lupi.h"
#ifndef _WIN32
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -9,3 +11,8 @@ void lupi_init() {
mount(NULL, "/proc", "procfs", 0, NULL); mount(NULL, "/proc", "procfs", 0, NULL);
} }
} }
#else
void lupi_init() {
}
#endif

View File

@ -9,9 +9,6 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h> #include <strings.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
@ -19,6 +16,15 @@
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/conf.h> #include <openssl/conf.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
SSL_CTX* ctx = NULL; SSL_CTX* ctx = NULL;
static int l_open(lua_State *L) { /* TODO: Any mem leaks? */ static int l_open(lua_State *L) { /* TODO: Any mem leaks? */
@ -54,8 +60,12 @@ static int l_open(lua_State *L) { /* TODO: Any mem leaks? */
close(sockfd); close(sockfd);
continue; continue;
} }
#ifdef _WIN32
u_long blockmode = 1;
ioctlsocket(sockfd,FIONBIO,&blockmode);
#else
fcntl(sockfd, F_SETFL, O_NONBLOCK); fcntl(sockfd, F_SETFL, O_NONBLOCK);
#endif
break; break;
} }
@ -197,8 +207,15 @@ static void ssl_init() {
void internet_start(lua_State *L) { void internet_start(lua_State *L) {
ssl_init(); ssl_init();
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#else
WSADATA wd;
if(WSAStartup(MAKEWORD(2,2), &wd)!=0) {
WSACleanup();
exit(1);
}
#endif
struct luaL_Reg netlib[] = { struct luaL_Reg netlib[] = {
{"open", l_open}, {"open", l_open},
{"write", l_write}, {"write", l_write},

119
src/c/lib/wcwidth.c Normal file
View File

@ -0,0 +1,119 @@
/*
* This is an implementation of wcwidth() and wcswidth() as defined in
* "The Single UNIX Specification, Version 2, The Open Group, 1997"
* <http://www.UNIX-systems.org/online.html>
*
* Markus Kuhn -- 2001-01-12 -- public domain
* ftp://ftp.maths.tcd.ie/src/windows/putty/wcwidth.c
*/
#include <wchar.h>
struct interval {
unsigned short first;
unsigned short last;
};
/* auxiliary function for binary search in interval table */
static int bisearch(wchar_t ucs, const struct interval *table, int max) {
int min = 0;
int mid;
if (ucs < table[0].first || ucs > table[max].last)
return 0;
while (max >= min) {
mid = (min + max) / 2;
if (ucs > table[mid].last)
min = mid + 1;
else if (ucs < table[mid].first)
max = mid - 1;
else
return 1;
}
return 0;
}
#ifdef _WIN32
int wcwidth(wchar_t ucs)
{
/* sorted list of non-overlapping intervals of non-spacing characters */
static const struct interval combining[] = {
{ 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
{ 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
{ 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
{ 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
{ 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
{ 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
{ 0x07A6, 0x07B0 }, { 0x0901, 0x0902 }, { 0x093C, 0x093C },
{ 0x0941, 0x0948 }, { 0x094D, 0x094D }, { 0x0951, 0x0954 },
{ 0x0962, 0x0963 }, { 0x0981, 0x0981 }, { 0x09BC, 0x09BC },
{ 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 },
{ 0x0A02, 0x0A02 }, { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 },
{ 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 },
{ 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 },
{ 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 },
{ 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
{ 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
{ 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
{ 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
{ 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
{ 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA },
{ 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 },
{ 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 },
{ 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD },
{ 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 },
{ 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 },
{ 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC },
{ 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, { 0x1032, 0x1032 },
{ 0x1036, 0x1037 }, { 0x1039, 0x1039 }, { 0x1058, 0x1059 },
{ 0x1160, 0x11FF }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
{ 0x17C9, 0x17D3 }, { 0x180B, 0x180E }, { 0x18A9, 0x18A9 },
{ 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x206A, 0x206F },
{ 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
{ 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF },
{ 0xFFF9, 0xFFFB }
};
/* test for 8-bit control characters */
if (ucs == 0)
return 0;
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
return -1;
/* binary search in table of non-spacing characters */
if (bisearch(ucs, combining,
sizeof(combining) / sizeof(struct interval) - 1))
return 0;
/* if we arrive here, ucs is not a combining or C0/C1 control character */
return 1 +
(ucs >= 0x1100 &&
(ucs <= 0x115f || /* Hangul Jamo init. consonants */
(ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
ucs != 0x303f) || /* CJK ... Yi */
(ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
(ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
(ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
(ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
(ucs >= 0x20000 && ucs <= 0x2ffff)));
}
int wcswidth(const wchar_t *pwcs, size_t n)
{
int w, width = 0;
for (;*pwcs && n-- > 0; pwcs++)
if ((w = wcwidth(*pwcs)) < 0)
return -1;
else
width += w;
return width;
}
#endif

View File

@ -4,10 +4,14 @@
#include <lua.h> #include <lua.h>
#include <lualib.h> #include <lualib.h>
#include <lauxlib.h> #include <lauxlib.h>
#ifndef _WIN32
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/statvfs.h>
#else
#include <windows.h>
#endif
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/time.h> #include <sys/time.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -99,7 +103,11 @@ static int l_fs_exists (lua_State *L) {
static int l_fs_mkdir (lua_State *L) { static int l_fs_mkdir (lua_State *L) {
const char* fname = lua_tostring(L, 1); const char* fname = lua_tostring(L, 1);
#ifndef _WIN32
if( mkdir( fname, 0755 ) != -1 ) { if( mkdir( fname, 0755 ) != -1 ) {
#else
if( mkdir( fname ) != -1 ) {
#endif
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
} else { } else {
lua_pushboolean(L, 0); lua_pushboolean(L, 0);
@ -125,12 +133,16 @@ static int l_fs_isdir (lua_State *L) {
static int l_fs_spaceUsed (lua_State *L) { static int l_fs_spaceUsed (lua_State *L) {
const char* fname = lua_tostring(L, 1); const char* fname = lua_tostring(L, 1);
#ifndef _WIN32
struct statvfs s; struct statvfs s;
if( statvfs(fname, &s) != -1 ) { if( statvfs(fname, &s) != -1 ) {
lua_pushnumber(L, s.f_bsize * s.f_bfree); lua_pushnumber(L, s.f_bsize * s.f_bfree);
} else { } else {
lua_pushnumber(L, -1); lua_pushnumber(L, -1);
} }
#else
lua_pushnumber(L, -1);
#endif
return 1; return 1;
} }
@ -186,12 +198,16 @@ static int l_fs_write (lua_State *L) {
static int l_fs_spaceTotal (lua_State *L) { static int l_fs_spaceTotal (lua_State *L) {
const char* fname = lua_tostring(L, 1); const char* fname = lua_tostring(L, 1);
#ifndef _WIN32
struct statvfs s; struct statvfs s;
if( statvfs(fname, &s) != -1 ) { if( statvfs(fname, &s) != -1 ) {
lua_pushnumber(L, s.f_frsize * s.f_blocks); lua_pushnumber(L, s.f_frsize * s.f_blocks);
} else { } else {
lua_pushnumber(L, -1); lua_pushnumber(L, -1);
} }
#else
lua_pushnumber(L, -1);
#endif
return 1; return 1;
} }
@ -319,6 +335,7 @@ static int l_fs_read (lua_State *L) {
static int l_beep (lua_State *L) { static int l_beep (lua_State *L) {
int freq = lua_tonumber(L, 1); int freq = lua_tonumber(L, 1);
int btime = lua_tonumber(L, 2); int btime = lua_tonumber(L, 2);
#ifndef _WIN32
int console_fd = -1; int console_fd = -1;
if((console_fd = open("/dev/console", O_WRONLY)) == -1) { if((console_fd = open("/dev/console", O_WRONLY)) == -1) {
@ -334,6 +351,7 @@ static int l_beep (lua_State *L) {
usleep(1000 * btime); usleep(1000 * btime);
ioctl(console_fd, KIOCSOUND, 0); ioctl(console_fd, KIOCSOUND, 0);
close(console_fd); close(console_fd);
#endif /* TODO win32 implementation */
return 0; return 0;
} }
@ -345,21 +363,45 @@ static int l_uptime (lua_State *L) { /* Return ms */
} }
static int l_totalMemory (lua_State *L) { static int l_totalMemory (lua_State *L) {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status );
lua_pushnumber(L, status.dwTotalPhys);
#elif defined(_WIN32)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx( &status );
lua_pushnumber(L, (size_t)status.ullTotalPhys);
#else
long pages = sysconf(_SC_PHYS_PAGES); long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE); long page_size = sysconf(_SC_PAGE_SIZE);
lua_pushnumber(L, pages * page_size); lua_pushnumber(L, pages * page_size);
#endif
return 1; return 1;
} }
static int l_freeMemory (lua_State *L) { static int l_freeMemory (lua_State *L) {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status );
lua_pushnumber(L, status.dwAvailPhys);
#elif defined(_WIN32)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx( &status );
lua_pushnumber(L, (size_t)status.ullAvailPhys);
#else
long pages = sysconf(_SC_AVPHYS_PAGES); long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE); long page_size = sysconf(_SC_PAGE_SIZE);
lua_pushnumber(L, pages * page_size); lua_pushnumber(L, pages * page_size);
#endif
return 1; return 1;
} }
static int l_pull (lua_State *L) { static int l_pull (lua_State *L) {
lua_pushnumber(L, epoll_pull(lua_tonumber(L, 1))); lua_pushnumber(L, event_pull(lua_tonumber(L, 1)));
return 1; return 1;
} }
@ -378,6 +420,15 @@ static int l_towlower (lua_State *L) {
return 1; return 1;
} }
static int l_platform (lua_State *L) { /* returns platform identifiers separated by | */
#ifndef _WIN32
lua_pushstring(L, "unix|linux|other");
#else
lua_pushstring(L, "windows");
#endif
return 1;
}
#ifdef DEBUG #ifdef DEBUG
static int l_debug (lua_State *L) { static int l_debug (lua_State *L) {
return 0; return 0;
@ -385,42 +436,6 @@ static int l_debug (lua_State *L) {
#endif #endif
void luanative_start(lua_State *L) { void luanative_start(lua_State *L) {
/*lua_createtable (L, 0, 1);
pushctuple(L, "sleep", l_sleep);
pushctuple(L, "log", l_log);
pushctuple(L, "fs_exists", l_fs_exists);
pushctuple(L, "fs_mkdir", l_fs_mkdir);
pushctuple(L, "fs_isdir", l_fs_isdir);
pushctuple(L, "fs_spaceUsed", l_fs_spaceUsed);
pushctuple(L, "fs_open", l_fs_open);
pushctuple(L, "fs_seek", l_fs_seek);
pushctuple(L, "fs_write", l_fs_write);
pushctuple(L, "fs_spaceTotal", l_fs_spaceTotal);
pushctuple(L, "fs_rename", l_fs_rename);
pushctuple(L, "fs_list", l_fs_list);
pushctuple(L, "fs_lastModified", l_fs_lastModified);
pushctuple(L, "fs_remove", l_fs_remove);
pushctuple(L, "fs_close", l_fs_close);
pushctuple(L, "fs_size", l_fs_size);
pushctuple(L, "fs_read", l_fs_read);
pushctuple(L, "wcwidth", l_wcwidth);
pushctuple(L, "towlower", l_towlower);
pushctuple(L, "towupper", l_towupper);
pushctuple(L, "beep", l_beep);
pushctuple(L, "uptime", l_uptime);
pushctuple(L, "totalMemory", l_totalMemory);
pushctuple(L, "freeMemory", l_freeMemory);
pushctuple(L, "pull", l_pull);
#ifdef DEBUG
lua_pushstring(L, "debug");
lua_pushboolean(L, 1);
lua_settable(L, -3);
#endif*/
struct luaL_Reg nativelib[] = { struct luaL_Reg nativelib[] = {
{"sleep", l_sleep}, {"sleep", l_sleep},
@ -448,6 +463,7 @@ void luanative_start(lua_State *L) {
{"totalMemory", l_totalMemory}, {"totalMemory", l_totalMemory},
{"freeMemory", l_freeMemory}, {"freeMemory", l_freeMemory},
{"pull", l_pull}, {"pull", l_pull},
{"platform", l_platform},
#ifdef DEBUG #ifdef DEBUG
{"debug", l_debug}, {"debug", l_debug},
#endif #endif

View File

@ -3,6 +3,7 @@
int main (void) { int main (void) {
puts("LuPI L0 INIT"); puts("LuPI L0 INIT");
lupi_init();
run_init(); run_init();
return 0; return 0;
} }

View File

@ -8,7 +8,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statvfs.h> //#include <sys/statvfs.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -21,7 +21,6 @@ lua_State* getL() {
} }
void run_init() { void run_init() {
lupi_init();
L = luaL_newstate(); L = luaL_newstate();
luaL_openlibs (L); luaL_openlibs (L);
@ -30,9 +29,8 @@ void run_init() {
internet_start (L); internet_start (L);
fb_start (L); fb_start (L);
termutils_start (L); termutils_start (L);
epoll_prepare(); event_prepare();
/* int status = luaL_loadstring(L, lua_init); */
int status = luaL_loadbuffer(L, lua_init, strlen(lua_init), "=INIT"); int status = luaL_loadbuffer(L, lua_init, strlen(lua_init), "=INIT");
if (status) { if (status) {
fprintf(stderr, "Couldn't load init: %s\n", lua_tostring(L, -1)); fprintf(stderr, "Couldn't load init: %s\n", lua_tostring(L, -1));

View File

@ -3,9 +3,11 @@
#include <lualib.h> #include <lualib.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#ifndef _WIN32
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <termios.h> #include <termios.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -31,12 +33,23 @@ static int l_term_restore (lua_State *L) {
return 0; return 0;
} }
#else
static int l_get_term_sz (lua_State *L) { return 0; }
static int l_term_restore (lua_State *L) { return 0; }
#endif
static int l_term_init (lua_State *L) { static int l_term_init (lua_State *L) {
#ifndef _WIN32
tcsetattr (STDOUT_FILENO, TCSAFLUSH, &new); tcsetattr (STDOUT_FILENO, TCSAFLUSH, &new);
return 0; lua_pushboolean(L, 1);
#else
lua_pushboolean(L, 0);
#endif
return 1;
} }
void termutils_start(lua_State *L) { void termutils_start(lua_State *L) {
#ifndef _WIN32
signal(SIGWINCH, handle_winch); signal(SIGWINCH, handle_winch);
if (tcgetattr (STDOUT_FILENO, &old) != 0) if (tcgetattr (STDOUT_FILENO, &old) != 0)
@ -49,6 +62,7 @@ void termutils_start(lua_State *L) {
new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag &= ~(CSIZE | PARENB); new.c_cflag &= ~(CSIZE | PARENB);
new.c_cflag |= CS8; new.c_cflag |= CS8;
#endif
struct luaL_Reg termlib[] = { struct luaL_Reg termlib[] = {
{"getSize", l_get_term_sz}, {"getSize", l_get_term_sz},

View File

@ -2,6 +2,11 @@ local boot = {}
function boot.boot() function boot.boot()
local gpu = modules.component.api.proxy(modules.component.api.list("gpu", true)()) local gpu = modules.component.api.proxy(modules.component.api.list("gpu", true)())
if not gpu then
gpu = setmetatable({}, {__index = function()
return function() return 0, 0 end
end})
end
local w, h = gpu.getResolution() local w, h = gpu.getResolution()
local function bsod(...) local function bsod(...)

View File

@ -135,7 +135,7 @@ function api.pullSignal(timeout)
local nevts = 0 local nevts = 0
repeat repeat
nevts = native.pull(timeout) nevts = native.pull(timeout)
until nevts > 0 or native.uptime() > timeoutuptime until nevts > 0 or native.uptime() >= timeoutuptime
if signalQueue[1] then if signalQueue[1] then
native.log("pullSignal native: " .. signalQueue[1][1]) native.log("pullSignal native: " .. signalQueue[1][1])
return table.unpack(table.remove(signalQueue, 1)) return table.unpack(table.remove(signalQueue, 1))

View File

@ -57,7 +57,7 @@ function filesystem.register(basePath, uuid)
native.fs_mkdir(basePath) native.fs_mkdir(basePath)
end end
if not native.fs_isdir(basePath) then if not native.fs_isdir(basePath) then
error("Filesystem root is not a directory!") error("Filesystem root is not a directory! (basePath=".. tostring(basePath) ..")")
end end
local function realpath(path) local function realpath(path)
checkArg(1, path, "string") checkArg(1, path, "string")
@ -130,7 +130,7 @@ function filesystem.register(basePath, uuid)
return native.fs_lastModified(realpath(path)) return native.fs_lastModified(realpath(path))
end end
function fs.getLabel() function fs.getLabel()
return path --TODO: Implement, use real labels return basePath --TODO: Implement, use real labels
end end
function fs.remove(path) --TODO: TEST!! function fs.remove(path) --TODO: TEST!!
checkArg(1, path, "string") checkArg(1, path, "string")

View File

@ -84,7 +84,7 @@ function main()
modules.gpio.register() modules.gpio.register()
modules.internet.start() modules.internet.start()
modules.filesystem.register("root") modules.filesystem.register("root")
if native.debug then if native.debug and native.platform():match("unix") then
modules.filesystem.register("/", "11111111-1111-1111-1111-111111111111") modules.filesystem.register("/", "11111111-1111-1111-1111-111111111111")
end end
modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid()) modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid())
@ -92,7 +92,10 @@ function main()
if framebuffer.isReady() then if framebuffer.isReady() then
modules.fbgpu.start() modules.fbgpu.start()
else else
modules.textgpu.start() local textgpuAddr, tgfail = modules.textgpu.start()
if not textgpuAddr then
lprint("Couldn't initialize text gpu: " .. tostring(tgfail))
end
end end
if native.debug then if native.debug then

View File

@ -245,7 +245,9 @@ function textgpu.start()
return screenAddr return screenAddr
end end
termutils.init() if not termutils.init() then
return nil, "Cannot initialize terminal based gpu"
end
write("\x1b[?25l") --Disable cursor write("\x1b[?25l") --Disable cursor
local w, h = gpu.getResolution() local w, h = gpu.getResolution()
_height = h _height = h
@ -253,7 +255,7 @@ function textgpu.start()
gpu.setForeground(0xFFFFFF) gpu.setForeground(0xFFFFFF)
gpu.setBackground(0x000000) gpu.setBackground(0x000000)
modules.component.api.register(nil, "gpu", gpu) local gpuaddr = modules.component.api.register(nil, "gpu", gpu)
screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c
modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) modules.component.api.register("TODO:SetThisUuid", "keyboard", {})
@ -262,6 +264,8 @@ function textgpu.start()
io.flush() io.flush()
termutils.restore() termutils.restore()
end end
return gpuaddr
end end
return textgpu return textgpu