Move CStdInProc to its own source file

It should be usable without USE_CONSOLE, even though only the dedicated server
uses it at the moment.
stable-5.3
Günther Brammer 2012-11-30 18:30:00 +01:00
parent 21432d997b
commit acc37860ed
6 changed files with 156 additions and 123 deletions

View File

@ -501,6 +501,8 @@ set(OC_CLONK_SOURCES
src/platform/C4SoundLoaders.h
src/platform/C4SoundSystem.cpp
src/platform/C4SoundSystem.h
src/platform/C4StdInProc.cpp
src/platform/C4StdInProc.h
src/platform/C4Video.cpp
src/platform/C4Video.h
src/platform/C4Window.h

View File

@ -463,6 +463,8 @@ src/platform/C4SoundLoaders.cpp \
src/platform/C4SoundLoaders.h \
src/platform/C4SoundSystem.cpp \
src/platform/C4SoundSystem.h \
src/platform/C4StdInProc.cpp \
src/platform/C4StdInProc.h \
src/platform/C4Video.cpp \
src/platform/C4Video.h \
src/platform/C4Window.h \

View File

@ -23,6 +23,7 @@
#include <StdScheduler.h>
#include <StdSync.h>
#include <C4StdInProc.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
@ -76,31 +77,6 @@ public:
};
#endif
#ifdef USE_CONSOLE
// A simple alertable proc
class CStdInProc : public StdSchedulerProc
{
public:
CStdInProc();
~CStdInProc();
// StdSchedulerProc override
virtual bool Execute(int iTimeout, pollfd *);
#ifdef STDSCHEDULER_USE_EVENTS
virtual HANDLE GetEvent() { return GetStdHandle(STD_INPUT_HANDLE); }
#else
virtual void GetFDs(std::vector<struct pollfd> & checkfds)
{
pollfd pfd = { 0, POLLIN, 0 };
checkfds.push_back(pfd);
}
#endif
private:
// commands from stdin
StdCopyStrBuf CmdBuf;
};
#endif
class C4AbstractApp : public StdScheduler
{
public:
@ -188,7 +164,7 @@ public:
#elif defined(USE_CONSOLE)
protected:
CStdInProc InProc;
C4StdInProc InProc;
#endif
#ifdef USE_WIN32_WINDOWS

View File

@ -29,23 +29,6 @@
#include <C4Draw.h>
#include <C4Application.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 *);
#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 */
/* C4AbstractApp */
C4AbstractApp::C4AbstractApp(): Active(false), fQuitMsgReceived(false),
@ -110,86 +93,6 @@ bool C4AbstractApp::IsClipboardFull(bool fClipboard)
return false;
}
CStdInProc::CStdInProc()
{
#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 *)
{
#if 0 && defined(_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())
{
Application.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())
{
Application.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)
{
Application.Quit();
}
else
{
Application.OnCommand(line);
}
#if HAVE_READLINE_HISTORY
if (line && *line)
{
add_history (line);
}
#endif
free(line);
}
#endif
bool C4AbstractApp::ApplyGammaRamp(_D3DGAMMARAMP&, bool) { return true; }
bool C4AbstractApp::SaveDefaultGammaRamp(_D3DGAMMARAMP&) { return true; }
void C4AbstractApp::MessageDialog(const char * message) {}

View File

@ -0,0 +1,103 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2005 Peter Wortmann
* Copyright (c) 2005, 2011-2012 Günther Brammer
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <C4Include.h>
#include "C4StdInProc.h"
#include <C4Application.h>
#ifdef HAVE_LIBREADLINE
# if defined(HAVE_READLINE_READLINE_H)
# include <readline/readline.h>
# elif defined(HAVE_READLINE_H)
# include <readline.h>
# endif
# 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 */
static void readline_callback (char * line)
{
if (!line)
{
Application.Quit();
}
else
{
Application.OnCommand(line);
}
#if HAVE_READLINE_HISTORY
if (line && *line)
{
add_history (line);
}
#endif
free(line);
}
C4StdInProc::C4StdInProc()
{
rl_callback_handler_install (">", readline_callback);
}
C4StdInProc::~C4StdInProc()
{
rl_callback_handler_remove();
}
bool C4StdInProc::Execute(int iTimeout, pollfd *)
{
rl_callback_read_char();
return true;
}
#else
C4StdInProc::C4StdInProc() { }
C4StdInProc::~C4StdInProc() { }
bool C4StdInProc::Execute(int iTimeout, pollfd *)
{
// 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())
{
Application.OnCommand(CmdBuf.getData());
CmdBuf.Clear();
}
}
else if (isprint((unsigned char)c))
CmdBuf.AppendChar(c);
return true;
}
#endif /* HAVE_LIBREADLINE */

View File

@ -0,0 +1,47 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2010, 2012 Günther Brammer
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef INC_C4STDINPROC
#define INC_C4STDINPROC
#include <StdScheduler.h>
// A simple alertable proc
class C4StdInProc : public StdSchedulerProc
{
public:
C4StdInProc();
~C4StdInProc();
// StdSchedulerProc override
virtual bool Execute(int iTimeout, pollfd *);
#ifdef STDSCHEDULER_USE_EVENTS
virtual HANDLE GetEvent() { return GetStdHandle(STD_INPUT_HANDLE); }
#else
virtual void GetFDs(std::vector<struct pollfd> & checkfds)
{
pollfd pfd = { 0, POLLIN, 0 };
checkfds.push_back(pfd);
}
#endif
private:
// commands from stdin
StdCopyStrBuf CmdBuf;
};
#endif /* INC_C4STDINPROC */