console mode: Move keyboard input to CStdInProc, make it compile again

Conflicts:

	src/editor/C4Console.cpp
	src/platform/StdMacApp.mm
Günther Brammer 2011-02-08 22:51:23 +01:00
parent 1ce90e97e5
commit 7dd368086e
8 changed files with 76 additions and 184 deletions

View File

@ -474,6 +474,7 @@ src/platform/StdSurface2.h \
src/platform/StdSurface8.cpp \
src/platform/StdSurface8.h \
src/platform/StdSync.h \
src/platform/StdTApp.cpp \
src/platform/StdVideo.cpp \
src/platform/StdVideo.h \
src/platform/StdWindow.h \
@ -539,7 +540,7 @@ else
if WIN32
clonk_SOURCES += src/platform/StdWindow.cpp
else
clonk_SOURCES += src/platform/StdXApp.cpp src/platform/StdXWindow.cpp src/platform/StdXPrivate.h src/platform/StdTApp.cpp
clonk_SOURCES += src/platform/StdXApp.cpp src/platform/StdXWindow.cpp src/platform/StdXPrivate.h
endif
endif
if DEVELOPER_MODE

View File

@ -70,6 +70,7 @@ public:
void QuitGame(); // quit game only, but restart application if in fullscreen startup menu mode
void Activate(); // activate app to gain full focus in OS
void SetNextMission(const char *szMissionFilename);
virtual void OnCommand(const char *szCmd);
const char *GetRevision() const { return Revision.getData(); }
@ -92,8 +93,6 @@ protected:
static bool ProcessCallback(const char *szMessage, int iProcess);
void ApplyResolutionConstraints();
virtual void OnCommand(const char *szCmd);
// set by ParseCommandLine, if neither scenario nor direct join adress has been specified
int UseStartupDialog;
// set by ParseCommandLine, for installing registration keys

View File

@ -54,7 +54,6 @@ bool CStdApp::Init(int argc, char * argv[])
// SDLmain.m copied the executable path into argv[0];
// just copy it (not sure if original buffer is guaranteed
// to be permanent).
this->argc=argc; this->argv=argv;
static char dir[PATH_MAX];
SCopy(argv[0], dir);
Location = dir;
@ -198,22 +197,3 @@ void CStdApp::MessageDialog(const char * message)
}
#endif
// Event-pipe-whatever stuff I do not understand.
bool CStdApp::ReadStdInCommand()
{
char c;
if (read(0, &c, 1) != 1)
return false;
if (c == '\n')
{
if (!CmdBuf.isNull())
{
OnCommand(CmdBuf.getData()); CmdBuf.Clear();
}
}
else if (isprint((unsigned char)c))
CmdBuf.AppendChar(c);
return true;
}

View File

@ -22,17 +22,7 @@
#include <C4Include.h>
#ifdef USE_CONSOLE
#include <StdWindow.h>
#include <StdGL.h>
#include <StdDDraw2.h>
#include <StdFile.h>
#include <StdBuf.h>
#include <string>
#include <sstream>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <C4Application.h>
#ifdef HAVE_LIBREADLINE
# if defined(HAVE_READLINE_READLINE_H)
@ -41,7 +31,6 @@
# include <readline.h>
# endif
static void readline_callback (char *);
static CStdApp * readline_callback_use_this_app = 0;
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
@ -62,10 +51,12 @@ CStdApp::CStdApp(): Active(false), fQuitMsgReceived(false),
#endif
fDspModeSet(false)
{
Add(&InProc);
}
CStdApp::~CStdApp()
{
Remove(&InProc);
}
bool CStdApp::Init(int argc, char * argv[])
@ -73,7 +64,6 @@ bool CStdApp::Init(int argc, char * argv[])
// Set locale
setlocale(LC_ALL,"");
// Try to figure out the location of the executable
this->argc=argc; this->argv=argv;
static char dir[PATH_MAX];
SCopy(argv[0], dir);
if (dir[0] != '/')
@ -87,35 +77,19 @@ bool CStdApp::Init(int argc, char * argv[])
Location = dir;
}
#if USE_CONSOLE && HAVE_LIBREADLINE
rl_callback_handler_install (">", readline_callback);
readline_callback_use_this_app = this;
#endif
// Custom initialization
return DoInit (argc, argv);
}
void CStdApp::Clear()
{
#if USE_CONSOLE && HAVE_LIBREADLINE
rl_callback_handler_remove();
#endif
}
void CStdApp::Quit()
{
fQuitMsgReceived = true;
}
/*
// handle commands
if(FD_ISSET(0, &rfds))
{
// Do not call OnStdInInput to be able to return
// HR_Failure when ReadStdInCommand returns false
if(!ReadStdInCommand())
return HR_Failure;
}
*/
bool CStdApp::GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, uint32_t iMonitor)
{
return false;
@ -128,6 +102,7 @@ bool CStdApp::SetVideoMode(unsigned int, unsigned int, unsigned int, unsigned in
// Copy the text to the clipboard or the primary selection
void CStdApp::Copy(const StdStrBuf & text, bool fClipboard)
{
return false;
}
// Paste the text from the clipboard or the primary selection
@ -144,35 +119,58 @@ bool CStdApp::IsClipboardFull(bool fClipboard)
void CStdApp::ClearClipboard(bool fClipboard)
{
}
/*
CStdWindow * CStdAppPrivate::GetWindow(unsigned long wnd) {
WindowListT::iterator i = WindowList.find(wnd);
if (i != WindowList.end()) return i->second;
return 0;
}
void CStdAppPrivate::SetWindow(unsigned long wnd, CStdWindow * pWindow) {
if (!pWindow) {
WindowList.erase(wnd);
} else {
WindowList[wnd] = pWindow;
}
}*/
bool CStdApp::ReadStdInCommand()
CStdInProc::CStdInProc()
{
#if HAVE_LIBREADLINE
#if USE_CONSOLE && HAVE_LIBREADLINE
rl_callback_handler_install (">", readline_callback);
#endif
}
CStdInProc::~CStdInProc()
{
#if USE_CONSOLE && HAVE_LIBREADLINE
rl_callback_handler_remove();
#endif
}
bool CStdInProc::Execute(int iTimeout, pollfd *)
{
#ifdef _WIN32
while (_kbhit())
{
// Surely not the most efficient way to do it, but we won't have to read much data anyway.
char c = getch();
if (c == '\r')
{
if (!CmdBuf.isNull())
{
OnCommand(CmdBuf.getData());
CmdBuf.Clear();
}
}
else if (isprint((unsigned char)c))
CmdBuf.AppendChar(c);
}
// FIXME: handle stdin-close
return true;
#elif defined(HAVE_LIBREADLINE)
rl_callback_read_char();
return true;
#else
// Surely not the most efficient way to do it, but we won't have to read much data anyway.
char c;
if (read(0, &c, 1) != 1)
{
Application.Quit();
return false;
}
if (c == '\n')
{
if (!CmdBuf.isNull())
{
OnCommand(CmdBuf.getData()); CmdBuf.Clear();
OnCommand(CmdBuf.getData());
CmdBuf.Clear();
}
}
else if (isprint((unsigned char)c))
@ -180,16 +178,17 @@ bool CStdApp::ReadStdInCommand()
return true;
#endif
}
#if HAVE_LIBREADLINE
static void readline_callback (char * line)
{
if (!line)
{
readline_callback_use_this_app->Quit();
Application.Quit();
}
else
{
readline_callback_use_this_app->OnCommand(line);
Application.OnCommand(line);
}
#if HAVE_READLINE_HISTORY
if (line && *line)
@ -206,17 +205,28 @@ bool CStdDDraw::SaveDefaultGammaRamp(CStdWindow * pWindow)
return true;
}
void CStdApp::OnStdInInput()
{
if (!ReadStdInCommand())
{
// TODO: This should only cause HandleMessage to return
// HR_Failure...
Quit();
}
}
void CStdApp::MessageDialog(const char * message)
{
}
bool CStdApp::FlushMessages()
{
// Always fail after quit message
if (fQuitMsgReceived)
return false;
return true;
}
void CStdWindow::Clear() {}
CStdWindow::CStdWindow() {}
CStdWindow::~CStdWindow() {}
void CStdWindow::EnumerateMultiSamples(std::vector<int, std::allocator<int> >&) const {}
void CStdWindow::FlashWindow() {}
bool CStdWindow::GetSize(RECT*) {return 0;}
CStdWindow* CStdWindow::Init(CStdWindow::WindowKind, CStdApp*, char const*, CStdWindow*, bool) {return this;}
bool CStdWindow::ReInit(CStdApp*) {return 0;}
bool CStdWindow::RestorePosition(char const*, char const*, bool) {return 0;}
void CStdWindow::SetSize(unsigned int, unsigned int) {}
void CStdWindow::SetTitle(char const*) {}
#endif // USE_CONSOLE

View File

@ -410,25 +410,6 @@ bool CStdApp::SetVideoMode(unsigned int iXRes, unsigned int iYRes, unsigned int
#endif
}
bool CStdApp::ReadStdInCommand()
{
while (_kbhit())
{
// Surely not the most efficient way to do it, but we won't have to read much data anyway.
char c = getch();
if (c == '\r')
{
if (!CmdBuf.isNull())
{
OnCommand(CmdBuf.getData()); CmdBuf.Clear();
}
}
else if (isprint((unsigned char)c))
CmdBuf.AppendChar(c);
}
return true;
}
void CStdApp::MessageDialog(const char * message)
{
MessageBox(0, message, C4ENGINECAPTION, MB_ICONERROR);

View File

@ -347,13 +347,8 @@ class CStdInProc : public StdSchedulerProc
{
public:
CStdInProc();
~CStdInProc() { }
~CStdInProc();
public:
void Notify();
bool Check();
bool CheckAndReset();
public:
// StdSchedulerProc override
virtual bool Execute(int iTimeout, pollfd *);
virtual void GetFDs(std::vector<struct pollfd> & checkfds)
@ -361,6 +356,9 @@ public:
pollfd pfd = { 0, POLLIN, 0 };
checkfds.push_back(pfd);
}
private:
// commands from stdin
StdCopyStrBuf CmdBuf;
};
#endif
@ -482,7 +480,6 @@ protected:
// the glib main loop that are in an anonymous namespace in
// StdXApp.cpp.
void OnXInput();
void OnStdInInput();
protected:
# ifdef USE_X11
class CStdAppPrivate * Priv;
@ -491,17 +488,12 @@ protected:
unsigned int KeyMask;
#endif
protected:
int argc; char ** argv;
#ifdef USE_CONSOLE
CStdInProc InProc;
#endif
StdStrBuf sLastError;
bool fDspModeSet; // true if display mode was changed
virtual bool DoInit(int argc, char * argv[]) = 0;
// commands from stdin (console only)
StdCopyStrBuf CmdBuf;
bool ReadStdInCommand();
virtual bool DoInit(int argc, char * argv[]) = 0;;
friend class CStdGL;
friend class CStdWindow;

View File

@ -44,24 +44,6 @@
#include <time.h>
#include <errno.h>
#ifdef HAVE_LIBREADLINE
# if defined(HAVE_READLINE_READLINE_H)
# include <readline/readline.h>
# elif defined(HAVE_READLINE_H)
# include <readline.h>
# endif
static void readline_callback (char *);
static CStdApp * readline_callback_use_this_app = 0;
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
# if defined(HAVE_READLINE_HISTORY_H)
# include <readline/history.h>
# elif defined(HAVE_HISTORY_H)
# include <history.h>
# endif
#endif /* HAVE_READLINE_HISTORY */
/* CStdApp */
#ifdef WITH_GLIB
@ -128,7 +110,6 @@ bool CStdApp::Init(int argc, char * argv[])
g_object_unref(icon);
#endif
// Try to figure out the location of the executable
Priv->argc=argc; Priv->argv=argv;
static char dir[PATH_MAX];
SCopy(argv[0], dir);
if (dir[0] != '/')
@ -651,49 +632,6 @@ void CStdAppPrivate::SetWindow(unsigned long wnd, CStdWindow * pWindow)
}
}
bool CStdApp::ReadStdInCommand()
{
#if HAVE_LIBREADLINE
rl_callback_read_char();
return true;
#else
// Surely not the most efficient way to do it, but we won't have to read much data anyway.
char c;
if (read(0, &c, 1) != 1)
return false;
if (c == '\n')
{
if (!CmdBuf.isNull())
{
OnCommand(CmdBuf.getData()); CmdBuf.Clear();
}
}
else if (isprint((unsigned char)c))
CmdBuf.AppendChar(c);
return true;
#endif
}
#if HAVE_LIBREADLINE
static void readline_callback (char * line)
{
if (!line)
{
readline_callback_use_this_app->Quit();
}
else
{
readline_callback_use_this_app->OnCommand(line);
}
#if HAVE_READLINE_HISTORY
if (line && *line)
{
add_history (line);
}
#endif
free(line);
}
#endif
void CStdApp::OnXInput()
{
while (XEventsQueued(dpy, QueuedAfterReading))
@ -714,14 +652,6 @@ void CStdApp::OnXInput()
XFlush(dpy);
}
void CStdApp::OnStdInInput()
{
if (!ReadStdInCommand())
{
Quit();
}
}
void CStdApp::MessageDialog(const char * message)
{
#ifdef WITH_DEVELOPER_MODE

View File

@ -170,7 +170,6 @@ public:
XIC xic;
Bool detectable_autorepeat_supported;
CX11Proc X11Proc;
int argc; char ** argv;
};
#endif // INC_STD_X_PRIVATE_H