POSIX: Implement displayLogFile in the posix backend

Tested only on Linux, but hopefully this is sufficiently
portable to support the other POSIX platforms.

Using fork/exec instead of the simpler 'system' to avoid quoting issues
and depending on specific shell features to handle a missing xdg-open
gracefully.
This commit is contained in:
Willem Jan Palenstijn 2011-06-04 15:07:12 +02:00
parent eb89240370
commit 656c252636
2 changed files with 59 additions and 0 deletions

View File

@ -22,6 +22,8 @@
#define FORBIDDEN_SYMBOL_EXCEPTION_getenv
#define FORBIDDEN_SYMBOL_EXCEPTION_mkdir
#define FORBIDDEN_SYMBOL_EXCEPTION_exit
#define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h //On IRIX, sys/stat.h includes sys/time.h
#include "common/scummsys.h"
@ -34,6 +36,8 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
OSystem_POSIX::OSystem_POSIX(Common::String baseConfigName)
@ -58,6 +62,12 @@ void OSystem_POSIX::initBackend() {
OSystem_SDL::initBackend();
}
bool OSystem_POSIX::hasFeature(Feature f) {
if (f == kFeatureDisplayLogFile)
return true;
return OSystem_SDL::hasFeature(f);
}
Common::String OSystem_POSIX::getDefaultConfigFileName() {
char configFile[MAXPATHLEN];
@ -138,4 +148,49 @@ Common::WriteStream *OSystem_POSIX::createLogFile() {
return stream;
}
bool OSystem_POSIX::displayLogFile() {
if (_logFilePath.empty())
return false;
// FIXME: This may not work perfectly when in fullscreen mode.
// On my system it drops from fullscreen without ScummVM noticing,
// so the next Alt-Enter does nothing, going from windowed to windowed.
// (wjp, 20110604)
pid_t pid = fork();
if (pid < 0) {
// failed to fork
return false;
} else if (pid == 0) {
// Try xdg-open first
execlp("xdg-open", "xdg-open", _logFilePath.c_str(), (char*)0);
// If we're here, that clearly failed.
// Try xterm+less next
execlp("xterm", "xterm", "-e", "less", _logFilePath.c_str(), (char*)0);
// TODO: If less does not exist we could fall back to 'more'.
// However, we'll have to use 'xterm -hold' for that to prevent the
// terminal from closing immediately (for short log files) or
// unexpectedly.
exit(127);
}
int status;
// Wait for viewer to close.
// (But note that xdg-open may have spawned a viewer in the background.)
pid = waitpid(pid, &status, 0);
if (pid < 0) {
// Probably nothing sensible to do in this error situation
return false;
}
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
}
#endif

View File

@ -31,6 +31,10 @@ public:
OSystem_POSIX(Common::String baseConfigName = ".scummvmrc");
virtual ~OSystem_POSIX() {}
virtual bool hasFeature(Feature f);
virtual bool displayLogFile();
virtual void init();
virtual void initBackend();