From f19d111681d20a88fd31ea87fecbdcc39cc1b707 Mon Sep 17 00:00:00 2001 From: pixel Date: Fri, 23 Jul 2004 23:20:11 +0000 Subject: Some new features in --- Dalos/Dalos.cc | 135 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 107 insertions(+), 28 deletions(-) diff --git a/Dalos/Dalos.cc b/Dalos/Dalos.cc index 07bcbcd..b3158c0 100644 --- a/Dalos/Dalos.cc +++ b/Dalos/Dalos.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Dalos.cc,v 1.2 2004-07-23 01:11:34 pixel Exp $ */ +/* $Id: Dalos.cc,v 1.3 2004-07-23 23:20:11 pixel Exp $ */ #include #include @@ -38,13 +38,18 @@ #include #include +#include +#include + +#include "cd-tool-hc.h" + #ifdef __MINGW32__ #define main SDL_main #endif bool auto_exec = true; -bool started = false; -bool lua_break = false; +bool lua_started = false; +bool do_lua_break = false; String prompt = "> "; @@ -57,6 +62,7 @@ SDL_sem * key_vect_size; std::vector key_vect; CODE_BEGINS + class console; console * Console; @@ -65,6 +71,33 @@ mogltk::widgets::Root * Root; mogltk::widget * Frame; mogltk::widgets::Menu * MainMenu; +class threaded_Lua : public Lua { + public: + threaded_Lua() : mutex(SDL_CreateMutex()) { } + virtual ~threaded_Lua() { SDL_DestroyMutex(mutex); } + virtual void lock() { + SDL_mutexP(mutex); + } + virtual void unlock() { + SDL_mutexV(mutex); + } + private: + SDL_mutex * mutex; +}; + +class threaded_locker : public locker_t { + public: + threaded_locker() : mutex(SDL_CreateMutex()) { } + virtual ~threaded_locker() { SDL_DestroyMutex(mutex); } + virtual void lock() { + SDL_mutexP(mutex); + } + virtual void unlock() { + SDL_mutexV(mutex); + } + private: + SDL_mutex * mutex; +}; static int lua_print(lua_State * _L) { Lua * L = Lua::find(_L); @@ -79,13 +112,13 @@ static int lua_print(lua_State * _L) { } static void lua_hook(lua_State * _L, lua_Debug * ar) { - if (!started) + if (!lua_started) return; Lua * L = Lua::find(_L); - if (lua_break) { - L->error("Breaking"); - lua_break = false; + if (do_lua_break) { + L->do_break(); + do_lua_break = false; } } @@ -114,7 +147,6 @@ static int readline_thread(void * d) { String line, endline; bool runit; int pos; - rl_getc_function = rl_getc_func; rl_redisplay_function = rl_redisplay_func; @@ -151,6 +183,7 @@ static int readline_thread(void * d) { } else if (auto_exec) { runit = true; } + SDL_mutexP(lua_command_lock); lua_command << line; SDL_mutexV(lua_command_lock); @@ -171,7 +204,10 @@ static int readline_thread(void * d) { } void start_lua() { - Lua * L = new Lua(); + Lua * L = new threaded_Lua(); + Buffer built; + bool use_builtin_cdtool = false; + int i; L->open_base(); L->open_math(); @@ -182,8 +218,8 @@ void start_lua() { LuaOutput::pushconstruct(L); LuaBuffer::pushconstruct(L); - // CD_PUSHSTATICS(L); - // Luapsx::pushstatics(L); + CD_PUSHSTATICS(L); + Luapsx::pushstatics(L); L->push("print"); L->push(lua_print); @@ -194,6 +230,25 @@ void start_lua() { lua_command_lock = SDL_CreateMutex(); lua_command_sem = SDL_CreateSemaphore(0); + try { + L->load(&Input("cd-tool.lua")); + } catch (GeneralException e) { + printm(M_WARNING, "There was an error loading cd-tool.lua: %s, using built-in.\n", e.GetMsg()); + use_builtin_cdtool = true; + } + + if (use_builtin_cdtool) { + for (i = 0; i < cd_tool_lua_size; i++) { + built.writeU8(cd_tool_lua[i]); + } + try { + L->load(&built); + } + catch (GeneralException e) { + printm(M_WARNING, "There was an error loading built-in cd-tool.lua: %s\n", e.GetMsg()); + } + } + SDL_CreateThread(LuaThread, L); } @@ -201,7 +256,7 @@ static int LuaThread(void * d) { Lua * L = (Lua *) d; while (true) { SDL_SemWait(lua_command_sem); - started = true; + lua_started = true; SDL_mutexP(lua_command_lock); try { L->load(&lua_command); @@ -218,7 +273,7 @@ static int LuaThread(void * d) { printm(M_ERROR, "Aborted. LUA caused the following exception: %s\n", e.GetMsg()); } SDL_mutexV(lua_command_lock); - started = false; + lua_started = false; } } @@ -263,14 +318,23 @@ class console_keyevent : public mogltk::engine::keyevent { return; case SDLK_c: if (k.mod & KMOD_CTRL) { - if (started) - lua_break = true; + if (lua_started) + do_lua_break = true; SDL_mutexV(key_vect_mutex); return; } default: if (k.unicode) { - key_vect.push_back(k.unicode); + switch (k.unicode) { + case 0xb4: + key_vect.push_back('\''); + break; + case 0xa8: + key_vect.push_back('"'); + break; + default: + key_vect.push_back(k.unicode); + } } else { SDL_mutexV(key_vect_mutex); if (old_handler) @@ -278,8 +342,12 @@ class console_keyevent : public mogltk::engine::keyevent { return; } } + // hack... + if (lua_started) + key_vect.pop_back(); SDL_mutexV(key_vect_mutex); - SDL_SemPost(key_vect_size); + if (!lua_started) + SDL_SemPost(key_vect_size); } else { if (k.sym == SDLK_ESCAPE) { Application->Console->SetVisible(true); @@ -339,12 +407,12 @@ class console : public mogltk::widget { page = 0; } void page_up() { - page += nlines; + page += nlines - 1; if (page > (lines.size() - nlines)) page = lines.size() - nlines; } void page_down() { - page -= nlines; + page -= nlines - 1; if (page < 0) page = 0; } @@ -388,12 +456,17 @@ class console : public mogltk::widget { } mogltk::ColorP::Max.A = 200; - Shaper()->box(GetAX() + 6 * (cursor_pos + 2), GetAY() + (nlines - 1) * 13, GetAX() + 6 * (cursor_pos + 3) - 1, GetAY() + nlines * 13, GREEN); - mogltk::ColorP::Max.A = 255; - mogltk::FixedFont->putcursor(GetAX(), GetAY() + (nlines - 1) * 13); - mogltk::FixedFont->printf(prompt); - mogltk::FixedFont->printf("%s", rl_line_buffer + start); + if (lua_started) { + Shaper()->box(GetAX(), GetAY() + (nlines - 1) * 13, GetAX() + 6, GetAY() + nlines * 13, GREEN); + } else { + Shaper()->box(GetAX() + 6 * (cursor_pos + 2), GetAY() + (nlines - 1) * 13, GetAX() + 6 * (cursor_pos + 3) - 1, GetAY() + nlines * 13, GREEN); + + mogltk::ColorP::Max.A = 255; + mogltk::FixedFont->putcursor(GetAX(), GetAY() + (nlines - 1) * 13); + mogltk::FixedFont->printf(prompt); + mogltk::FixedFont->printf("%s", rl_line_buffer + start); + } } virtual bool process_event(int mx, int my, mogltk::event_t event) { } @@ -474,8 +547,13 @@ class about : public mogltk::widgets::action { } about_dialog; virtual int startup() throw (GeneralException) { - verbosity = M_INFO; - new Archive(argv[0], ARCHIVE_EXECUTABLE); + verbosity = M_WARNING; + try { + new Archive(argv[0], ARCHIVE_EXECUTABLE); + } + catch (...) { + new Archive("Dalos.paq"); + } mogltk::widgets::ContextMenu * c; mogltk::base * gl = new mogltk::glbase(); @@ -496,10 +574,11 @@ virtual int startup() throw (GeneralException) { Console->move(0, Frame->GetH() - Console->GetH()); Console->add_line("Dalos v0.1 - LUA console"); - start_lua(); - printer = new myprinter(); + locker = new threaded_locker(); + start_lua(); + SDL_CreateThread(readline_thread, 0); MainMenu->addnode("/", 0); -- cgit v1.2.3