Added fake cin/cout/cerr to prevent application crashing on tablets

This commit is contained in:
pelya
2011-02-04 14:47:18 +00:00
parent 604c5e4b1d
commit e07c5cfd14
9 changed files with 111 additions and 30 deletions

View File

@@ -25,7 +25,7 @@ LOCAL_C_INCLUDES += $(foreach D, $(APP_SUBDIRS), $(LOCAL_PATH)/$(D)) \
$(LOCAL_PATH)/../sdl-$(SDL_VERSION)/include \
$(foreach L, $(COMPILED_LIBRARIES), $(LOCAL_PATH)/../$(L)/include)
LOCAL_CFLAGS += -D__sF=__SDL_fake_stdout
LOCAL_CFLAGS += -D__sF=__SDL_fake_stdout -Dcout=__SDL_fake_cout -Dcerr=__SDL_fake_cerr -Dclog=__SDL_fake_clog
LOCAL_CFLAGS += $(APPLICATION_ADDITIONAL_CFLAGS)

View File

@@ -47,7 +47,7 @@ CFLAGS="-I$NDK/build/platforms/$PLATFORMVER/arch-arm/usr/include \
-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID \
-Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -O2 \
-fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 \
-Wa,--noexecstack -DNDEBUG -g -D__sF=__SDL_fake_stdout \
-Wa,--noexecstack -DNDEBUG -g -D__sF=__SDL_fake_stdout -Dcout=__SDL_fake_cout -Dcerr=__SDL_fake_cerr -Dclog=__SDL_fake_clog \
-I$LOCAL_PATH/../sdl-1.2/include $STL_INCLUDE \
`echo $APP_MODULES | sed \"s@\([-a-zA-Z0-9_.]\+\)@-I$LOCAL_PATH/../\1/include@g\"`"

View File

@@ -50,7 +50,7 @@ CFLAGS="\
-fpic -ffunction-sections -funwind-tables -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi \
-march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 \
-I$NDK/platforms/$PLATFORMVER/arch-arm/usr/include -Wa,--noexecstack \
-DANDROID -D__sF=__SDL_fake_stdout -DNDEBUG -O2 -g \
-DANDROID -D__sF=__SDL_fake_stdout -Dcout=__SDL_fake_cout -Dcerr=__SDL_fake_cerr -Dclog=__SDL_fake_clog -DNDEBUG -O2 -g \
-I$NDK/sources/cxx-stl/gnu-libstdc++/include \
-I$NDK/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include \
-I$LOCAL_PATH/../sdl-1.2/include \

View File

@@ -1 +1 @@
openttd
ufoai

View File

@@ -1 +0,0 @@
../../../../sdl-1.3/src/video/android/SDL_fake_stdout.c

View File

@@ -0,0 +1 @@
../../../../sdl-1.3/src/video/android/SDL_fake_stdout.cpp

View File

@@ -57,8 +57,9 @@ static inline SDL_scancode TranslateKey(int scancode)
static int isTrackballUsed = 0;
static int isMouseUsed = 0;
enum { RIGHT_CLICK_NONE = 0, RIGHT_CLICK_WITH_MULTITOUCH = 1, RIGHT_CLICK_WITH_PRESSURE = 2, RIGHT_CLICK_WITH_KEY = 3 };
enum { LEFT_CLICK_NORMAL = 0, LEFT_CLICK_NEAR_CURSOR = 1, LEFT_CLICK_WITH_MULTITOUCH = 2, LEFT_CLICK_WITH_PRESSURE = 3, LEFT_CLICK_WITH_KEY = 4 };
enum { RIGHT_CLICK_NONE = 0, RIGHT_CLICK_WITH_MULTITOUCH = 1, RIGHT_CLICK_WITH_PRESSURE = 2, RIGHT_CLICK_WITH_KEY = 3, RIGHT_CLICK_WITH_TIMEOUT = 4 };
enum { LEFT_CLICK_NORMAL = 0, LEFT_CLICK_NEAR_CURSOR = 1, LEFT_CLICK_WITH_MULTITOUCH = 2, LEFT_CLICK_WITH_PRESSURE = 3,
LEFT_CLICK_WITH_KEY = 4, LEFT_CLICK_WITH_TIMEOUT = 5, LEFT_CLICK_WITH_TAP = 6, LEFT_CLICK_WITH_TAP_OR_TIMEOUT = 7 };
static int leftClickMethod = LEFT_CLICK_NORMAL;
static int rightClickMethod = RIGHT_CLICK_NONE;
static int leftClickKey = KEYCODE_DPAD_CENTER;

View File

@@ -1,23 +0,0 @@
/*
Writing to stdout will crash your program on some particular Android tablets.
Although this is the device bug I've put a workaround here,
it is heavily dependent on NDK internals and is not portable in any way.
*/
#ifndef _SDL_fake_stdout_h
#define _SDL_fake_stdout_h
#include <stdio.h>
extern FILE __SDL_fake_stdout[];
FILE __SDL_fake_stdout[3];
void SDL_ANDROID_initFakeStdout()
{
FILE * ff = NULL;
__SDL_fake_stdout[0] = * fopen("/dev/null", "r");
__SDL_fake_stdout[1] = * fopen("/dev/null", "w");
__SDL_fake_stdout[2] = * fopen("/dev/null", "w");
}
#endif

View File

@@ -0,0 +1,103 @@
/*
Writing to stdout will crash your program on some particular Android tablets.
Although this is the device bug I've put a workaround here,
it is heavily dependent on NDK internals and is not portable in any way.
*/
#ifndef _SDL_fake_stdout_h
#define _SDL_fake_stdout_h
#include <stdio.h>
#include <android/log.h>
extern "C" FILE __SDL_fake_stdout[];
FILE __SDL_fake_stdout[3];
extern "C" void SDL_ANDROID_initFakeStdout();
void SDL_ANDROID_initFakeStdout()
{
FILE * ff = NULL;
__SDL_fake_stdout[0] = * fopen("/dev/null", "r");
__SDL_fake_stdout[1] = * fopen("/dev/null", "w");
__SDL_fake_stdout[2] = * fopen("/dev/null", "w");
}
/* Outputting anything to cout/cerr WILL CRASH YOUR PROGRAM on specific devices -
x5a/x6d Android 2.1 tablet, and some other tablets,
however the same code runs on my HTC Evo without problem.
So I've just disabled cin/cout/cerr altogether.
*/
class _android_debugbuf: public streambuf
{
public:
_android_debugbuf()
{
pos = 0;
buf[0] = 0;
}
protected:
virtual int overflow(int c = EOF)
{
if (EOF == c)
{
return '\0'; // returning EOF indicates an error
}
else
{
outputchar(c);
return c;
}
};
// we dont do input so always return EOF
virtual int uflow() {return EOF;}
// we dont do input so always return 0 chars read
virtual int xsgetn(char *, int) {return 0;}
// Calls outputchar() for each character.
virtual int xsputn(const char *s, int n)
{
for (int i = 0; i < n; ++i)
{
outputchar(s[i]);
}
return n;// we always process all of the chars
};
private:
// the buffer
char buf[256];
int pos;
void outputchar(char c)
{
// TODO: mutex
if( pos >= sizeof(buf)-1 || c == '\n' || c == '\r' || c == 0 )
{
buf[pos] = 0;
__android_log_print(ANDROID_LOG_INFO, "libSDL", "%s", buf);
pos = 0;
buf[pos] = 0;
return;
};
buf[pos] = c;
pos++;
};
};
namespace std {
ostream __SDL_fake_cout(new _android_debugbuf());
ostream __SDL_fake_cerr(new _android_debugbuf());
ostream __SDL_fake_clog(new _android_debugbuf());
}
#endif