diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2008-01-12 13:33:11 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2008-01-12 13:33:11 +0800 |
commit | a1cabe2b83732154321ed07a75b38f1d61807a82 (patch) | |
tree | b15d9c78180c15c2409106fe9dd6438cd4c09714 | |
parent | 89443698a9cf7c2b708b154f57449baa96e42a94 (diff) | |
download | pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.tar pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.tar.gz pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.tar.bz2 pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.tar.lz pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.tar.xz pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.tar.zst pttbbs-a1cabe2b83732154321ed07a75b38f1d61807a82.zip |
- bbslua/pfterm: Add Win32 porting
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3822 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | mbbsd/bbslua.c | 109 | ||||
-rw-r--r-- | mbbsd/pfterm.c | 55 |
2 files changed, 136 insertions, 28 deletions
diff --git a/mbbsd/bbslua.c b/mbbsd/bbslua.c index 9938cca8..fee3da02 100644 --- a/mbbsd/bbslua.c +++ b/mbbsd/bbslua.c @@ -39,7 +39,7 @@ // CONST DEFINITION ////////////////////////////////////////////////////////////////////////// -#define BBSLUA_INTERFACE_VER (0.115) +#define BBSLUA_INTERFACE_VER (0.116) #define BBSLUA_SIGNATURE "--#BBSLUA" // BBS-Lua script format: @@ -67,6 +67,14 @@ #define BLCONF_SLEEP_TMIN (BLCONF_PEEK_TIME) #define BLCONF_SLEEP_TMAX (BLCONF_KBHIT_TMAX) #define BLCONF_U_SECOND (1000000L) +#define BLCONF_MMAP_ATTACH +#define BLCONF_CURRENT_USERID cuser.userid + +#ifdef _WIN32 +# undef BLCONF_MMAP_ATTACH +# undef BLCONF_CURRENT_USERID +# define BLCONF_CURRENT_USERID "guest" +#endif ////////////////////////////////////////////////////////////////////////// // GLOBAL VARIABLES @@ -380,8 +388,6 @@ bl_sleep(lua_State *L) { int n = lua_gettop(L); double us = 0, nus = 0; - struct timeval tp, tdest; - struct timezone tz; if (n > 0) us = lua_tonumber(L, 1); @@ -392,31 +398,43 @@ bl_sleep(lua_State *L) nus = us; refresh(); - memset(&tz, 0, sizeof(tz)); - gettimeofday(&tp, &tz); - // nus is the destination time - nus = bl_tv2double(&tp) + us; - bl_double2tv(nus, &tdest); +#ifdef _WIN32 + + Sleep(us * 1000); - // use peek_input - while ( (tp.tv_sec < tdest.tv_sec) || - ((tp.tv_sec == tdest.tv_sec) && (tp.tv_usec < tdest.tv_usec))) +#else // !_WIN32 { - // calculate new peek time - us = nus - bl_tv2double(&tp); + struct timeval tp, tdest; + struct timezone tz; - // check if input key is system break key. - if (peek_input(us, BLCONF_BREAK_KEY)) + memset(&tz, 0, sizeof(tz)); + gettimeofday(&tp, &tz); + + // nus is the destination time + nus = bl_tv2double(&tp) + us; + bl_double2tv(nus, &tdest); + + // use peek_input + while ( (tp.tv_sec < tdest.tv_sec) || + ((tp.tv_sec == tdest.tv_sec) && (tp.tv_usec < tdest.tv_usec))) { - drop_input(); - abortBBSLua = 1; - return lua_yield(L, 0); - } + // calculate new peek time + us = nus - bl_tv2double(&tp); - // check time - gettimeofday(&tp, &tz); + // check if input key is system break key. + if (peek_input(us, BLCONF_BREAK_KEY)) + { + drop_input(); + abortBBSLua = 1; + return lua_yield(L, 0); + } + + // check time + gettimeofday(&tp, &tz); + } } +#endif // !_WIN32 return 0; } @@ -536,12 +554,24 @@ bl_ctime(lua_State *L) BLAPI_PROTO bl_clock(lua_State *L) { + double d = 0; + +#ifdef _WIN32 + + DWORD ms = timeGetTime() % 1000; + syncnow(); + d = now + ms / 1000.0f; + +#else // !_WIN32 + struct timeval tp; struct timezone tz; - double d = 0; memset(&tz, 0, sizeof(tz)); gettimeofday(&tp, &tz); d = bl_tv2double(&tp); + +#endif // !_WIN32 + lua_pushnumber(L, d); return 1; } @@ -549,7 +579,7 @@ bl_clock(lua_State *L) BLAPI_PROTO bl_userid(lua_State *L) { - lua_pushstring(L, cuser.userid); + lua_pushstring(L, BLCONF_CURRENT_USERID); return 1; } @@ -711,9 +741,11 @@ bbsluaHook(lua_State *L, lua_Debug* ar) static char * bbslua_attach(const char *fpath, int *plen) { + char *buf = NULL; + +#ifdef BLCONF_MMAP_ATTACH struct stat st; int fd = open(fpath, O_RDONLY, 0600); - char *buf = NULL; *plen = 0; @@ -733,15 +765,33 @@ bbslua_attach(const char *fpath, int *plen) *plen = 0; return NULL; } - madvise(buf, *plen, MADV_SEQUENTIAL); + +#else // !BLCONF_MMAP_ATTACH + + FILE *fp = fopen(fpath, "rt"); + *plen = 0; + if (!fp) + return NULL; + fseek(fp, 0, SEEK_END); + *plen = ftell(fp); + buf = (char*) malloc (*plen); + rewind(fp); + fread(buf, *plen, 1, fp); + +#endif // !BLCONF_MMAP_ATTACH + return buf; } static void bbslua_detach(char *p, int len) { +#ifdef BLCONF_MMAP_ATTACH munmap(p, len); +#else // !BLCONF_MMAP_ATTACH + free(p); +#endif // !BLCONF_MMAP_ATTACH } int @@ -1033,7 +1083,10 @@ bbslua(const char *fpath) char *bs, *ps, *pe; int sz = 0; int lineshift; + +#ifdef UMODE_BBSLUA unsigned int prevmode = getutmpmode(); +#endif // re-entrant not supported! if (runningBBSLua) @@ -1080,7 +1133,10 @@ bbslua(const char *fpath) return 0; } +#ifdef UMODE_BBSLUA setutmpmode(UMODE_BBSLUA); +#endif + bbslua_logo(L); vmsgf("提醒您執行中隨時可按 [Ctrl-C] 強制中斷 BBS-Lua 程式"); @@ -1112,7 +1168,10 @@ bbslua(const char *fpath) vmsgf("BBS-Lua 執行結束%s。", abortBBSLua ? " (使用者中斷)" : r ? " (程式錯誤)" : ""); clear(); + +#ifdef UMODE_BBSLUA setutmpmode(prevmode); +#endif return 0; } diff --git a/mbbsd/pfterm.c b/mbbsd/pfterm.c index d8e0d1ad..1288fecb 100644 --- a/mbbsd/pfterm.c +++ b/mbbsd/pfterm.c @@ -217,6 +217,14 @@ typedef struct static FlatTerm ft; +#ifdef _WIN32 + // sorry, we support only 80x24 on Windows now. + HANDLE hStdout; + COORD coordBufSize = {80, 24}, coordBufCoord = {0, 0}; + SMALL_RECT winrect = {0, 0, 79, 23}; + CHAR_INFO winbuf[80*24]; +#endif + ////////////////////////////////////////////////////////////////////////// // Flat Terminal Utility Macro ////////////////////////////////////////////////////////////////////////// @@ -392,6 +400,12 @@ int fterm_DBCS_Big5(unsigned char c1, unsigned char c2); void initscr(void) { +#ifdef _WIN32 + hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleScreenBufferSize(hStdout, coordBufSize); + SetConsoleCursorPosition(hStdout, coordBufCoord); +#endif + memset(&ft, sizeof(ft), 0); resizeterm(FTSZ_DEFAULT_ROW, FTSZ_DEFAULT_COL); ft.attr = ft.rattr = FTATTR_DEFAULT; @@ -709,6 +723,31 @@ doupdate(void) return; } +#ifdef _WIN32 + + assert(ft.rows == coordBufSize.Y); + assert(ft.cols == coordBufSize.X); + + for (y = 0; y < ft.rows; y++) + { + for (x = 0; x < ft.cols; x++) + { + WORD xAttr = FTAMAP[y][x], xxAttr; + // w32 attribute: bit swap (0,2) and (4, 6) + xxAttr = xAttr & 0xAA; + if (xAttr & 0x01) xxAttr |= 0x04; + if (xAttr & 0x04) xxAttr |= 0x01; + if (xAttr & 0x10) xxAttr |= 0x40; + if (xAttr & 0x40) xxAttr |= 0x10; + + winbuf[y*ft.cols + x].Attributes= xxAttr; + winbuf[y*ft.cols + x].Char.AsciiChar = FTCMAP[y][x]; + } + } + WriteConsoleOutputA(hStdout, winbuf, coordBufSize, coordBufCoord, &winrect); + +#else // !_WIN32 + // if scroll, do it first if (ft.scroll) fterm_rawscroll(ft.scroll); @@ -911,7 +950,9 @@ doupdate(void) } } - // doing rawcursor() earlier to enable max display time +#endif // !_WIN32 + + // doing fterm_rawcursor() earlier to enable max display time fterm_rawcursor(); fterm_dupe2bk(); ft.dirty = 0; @@ -1985,10 +2026,17 @@ fterm_rawmove_opt(int y, int x) void fterm_rawcursor(void) { +#ifdef _WIN32 + COORD cursor; + cursor.X = ft.x; + cursor.Y = ft.y; + SetConsoleCursorPosition(hStdout, cursor); +#else // fterm_rawattr(FTATTR_DEFAULT); fterm_rawattr(ft.attr); fterm_rawmove_opt(ft.y, ft.x); fterm_rawflush(); +#endif // !_WIN32 } void @@ -2145,11 +2193,12 @@ standend(void) } #ifndef _PFTERM_TEST_MAIN + void scr_dump(screen_backup_t *psb) { int y = 0; - void *p = NULL; + char *p = NULL; psb->row= ft.rows; psb->col= ft.cols; @@ -2171,7 +2220,7 @@ void scr_restore(const screen_backup_t *psb) { int y = 0; - void *p = NULL; + char *p = NULL; int c = ft.cols, r = ft.rows; if (!psb || !psb->raw_memory) return; |