ScummVM API documentation
android.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef _ANDROID_H_
23 #define _ANDROID_H_
24 
25 #include "backends/platform/android/portdefs.h"
26 #include "common/fs.h"
27 #include "common/archive.h"
28 #include "common/mutex.h"
29 #include "common/ustr.h"
30 #include "audio/mixer_intern.h"
31 #include "backends/modular-backend.h"
32 #include "backends/plugins/posix/posix-provider.h"
33 #include "backends/fs/posix/posix-fs-factory.h"
34 #include "backends/fs/posix/posix-fs-factory.h"
35 #include "backends/log/log.h"
36 #include "backends/platform/android/touchcontrols.h"
37 
38 #include <pthread.h>
39 
40 #include <android/log.h>
41 
42 // toggles start
43 //#define ANDROID_DEBUG_ENTER
44 //#define ANDROID_DEBUG_GL
45 //#define ANDROID_DEBUG_GL_CALLS
46 // toggles end
47 
48 extern const char *android_log_tag;
49 
50 #define _ANDROID_LOG(prio, fmt, args...) __android_log_print(prio, android_log_tag, fmt, ## args)
51 #define LOGD(fmt, args...) _ANDROID_LOG(ANDROID_LOG_DEBUG, fmt, ##args)
52 #define LOGI(fmt, args...) _ANDROID_LOG(ANDROID_LOG_INFO, fmt, ##args)
53 #define LOGW(fmt, args...) _ANDROID_LOG(ANDROID_LOG_WARN, fmt, ##args)
54 #define LOGE(fmt, args...) _ANDROID_LOG(ANDROID_LOG_ERROR, fmt, ##args)
55 
56 #define MAX_ANDROID_SCUMMVM_LOG_FILESIZE_IN_BYTES (100*1024)
57 
58 #ifdef ANDROID_DEBUG_ENTER
59 #define ENTER(fmt, args...) LOGD("%s(" fmt ")", __FUNCTION__, ##args)
60 #else
61 #define ENTER(fmt, args...) do { } while (false)
62 #endif
63 
64 #ifdef ANDROID_DEBUG_GL
65 extern void checkGlError(const char *expr, const char *file, int line);
66 
67 #ifdef ANDROID_DEBUG_GL_CALLS
68 #define GLCALLLOG(x, before) \
69  do { \
70  if (before) \
71  LOGD("calling '%s' (%s:%d)", x, __FILE__, __LINE__); \
72  else \
73  LOGD("returned from '%s' (%s:%d)", x, __FILE__, __LINE__); \
74  } while (false)
75 #else
76 #define GLCALLLOG(x, before) do { } while (false)
77 #endif
78 
79 #define GLCALL(x) \
80  do { \
81  GLCALLLOG(#x, true); \
82  (x); \
83  GLCALLLOG(#x, false); \
84  checkGlError(#x, __FILE__, __LINE__); \
85  } while (false)
86 
87 #define GLTHREADCHECK \
88  do { \
89  assert(dynamic_cast<OSystem_Android *>(g_system)->isRunningInMainThread()); \
90  } while (false)
91 
92 #else
93 #define GLCALL(x) do { (x); } while (false)
94 #define GLTHREADCHECK do { } while (false)
95 #endif
96 
97 void *androidGLgetProcAddress(const char *name);
98 
100 private:
101  static const int kQueuedInputEventDelay = 50;
102 
103  struct EventWithDelay : public Common::Event {
105  uint32 originTimeMillis;
106 
108  uint32 referTimeMillis;
109 
111  uint32 delayMillis;
112 
114  Common::EventType connectedType;
115 
117  bool connectedTypeExecuted;
118 
119  EventWithDelay() : originTimeMillis(0), referTimeMillis(0), delayMillis(0), connectedType(Common::EVENT_INVALID), connectedTypeExecuted(false) {
120  }
121 
122  void reset() {
123  originTimeMillis = 0;
124  referTimeMillis = 0;
125  delayMillis = 0;
126  connectedType = Common::EVENT_INVALID;
127  connectedTypeExecuted = false;
128  }
129  };
130 
131  // passed from the dark side
132  int _audio_sample_rate;
133  int _audio_buffer_size;
134 
135  int _screen_changeid;
136 
137  pthread_t _main_thread;
138 
139  bool _timer_thread_exit;
140  pthread_t _timer_thread;
141 
142  bool _audio_thread_exit;
143  pthread_t _audio_thread;
144 
145  bool _virtkeybd_on;
146 
147  Audio::MixerImpl *_mixer;
148  timeval _startTime;
149 
150  Common::Queue<Common::Event> _event_queue;
151  EventWithDelay _delayedMouseBtnUpEvent;
152  EventWithDelay _delayedMouseBtnDownEvent;
153  Common::Mutex *_event_queue_lock;
154 
155  Common::Point _touch_pt_down, _touch_pt_scroll, _touch_pt_dt, _touch_pt_multi;
156  int _eventScaleX;
157  int _eventScaleY;
158  int _touch_mode;
159  int _touchpad_scale; // Used in events.cpp
160  int _trackball_scale; // Used in events.cpp
161  int _dpad_scale; // Used in events.cpp
162  int _joystick_scale; // TODO This seems currently unused. Is it needed?
163 // int _fingersDown;
164  int _firstPointerId;
165  int _secondPointerId;
166  int _thirdPointerId;
167 
168  TouchControls _touchControls;
169 
170  bool _engineRunning;
171 
172  Common::Path _defaultConfigFileName;
173  Common::Path _defaultLogFileName;
174  Common::String _systemPropertiesSummaryStr;
175  Common::String _systemSDKdetectedStr;
176 
177  Backends::Log::Log *_logger;
178 
179 #if defined(USE_OPENGL) && defined(USE_GLAD)
180  // Cached dlopen object
181  mutable void *_gles2DL;
182 #endif
183 
184  static void *timerThreadFunc(void *arg);
185  static void *audioThreadFunc(void *arg);
186  Common::String getSystemProperty(const char *name) const;
187 
188  Common::WriteStream *createLogFileForAppending();
189 
190 public:
191  enum {
192  TOUCH_MODE_TOUCHPAD = 0,
193  TOUCH_MODE_MOUSE = 1,
194  TOUCH_MODE_GAMEPAD = 2,
195  TOUCH_MODE_MAX = 3
196  };
197 
198  enum {
199  SCREEN_ORIENTATION_UNSPECIFIED = 0xffffffff,
200  SCREEN_ORIENTATION_LANDSCAPE = 0,
201  SCREEN_ORIENTATION_PORTRAIT = 1
202  };
203 
204  enum {
205  SHOW_ON_SCREEN_NONE = 0,
206  SHOW_ON_SCREEN_MENU = 1,
207  SHOW_ON_SCREEN_INPUT_MODE = 2,
208  SHOW_ON_SCREEN_ALL = 0xffffffff,
209  };
210 
211  OSystem_Android(int audio_sample_rate, int audio_buffer_size);
212  virtual ~OSystem_Android();
213 
214  void initBackend() override;
215  void engineInit() override;
216  void engineDone() override;
217 
218  void updateStartSettings(const Common::String &executable, Common::String &command, Common::StringMap &startSettings, Common::StringArray& additionalArgs) override;
219 
220  bool hasFeature(OSystem::Feature f) override;
221  void setFeatureState(OSystem::Feature f, bool enable) override;
222  bool getFeatureState(OSystem::Feature f) override;
223 
224  void pushEvent(int type, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);
225  void pushEvent(const Common::Event &event);
226  void pushEvent(const Common::Event &event1, const Common::Event &event2);
227  void pushDelayedTouchMouseBtnEvents();
228 
229  TouchControls &getTouchControls() { return _touchControls; }
230  void applyTouchSettings(bool _3dMode, bool overlayShown);
231  void setupTouchMode(int oldValue, int newValue);
232 
233  void applyOrientationSettings();
234 
235  void updateOnScreenControls();
236 
237  bool pollEvent(Common::Event &event) override;
241 
244 
245  void registerDefaultSettings(const Common::String &target) const override;
246  GUI::OptionsContainerWidget *buildBackendOptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const override;
247  void applyBackendSettings() override;
248 
249  uint32 getMillis(bool skipRecord = false) override;
250  void delayMillis(uint msecs) override;
252 
253  void quit() override;
254 
255  void setWindowCaption(const Common::U32String &caption) override;
256 
257  Audio::Mixer *getMixer() override;
258  void getTimeAndDate(TimeDate &td, bool skipRecord = false) const override;
259  void logMessage(LogMessageType::Type type, const char *message) override;
260  void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0) override;
261  bool openUrl(const Common::String &url) override;
262  bool hasTextInClipboard() override;
264  bool setTextInClipboard(const Common::U32String &text) override;
265  bool isConnectionLimited() override;
266  Common::String getSystemLanguage() const override;
267 
268  const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
269  int getDefaultGraphicsMode() const override;
270  bool setGraphicsMode(int mode, uint flags) override;
271  int getGraphicsMode() const override;
272 
273  OpenGL::ContextType getOpenGLType() const override { return OpenGL::kContextGLES2; }
274 #if defined(USE_OPENGL) && defined(USE_GLAD)
275  void *getOpenGLProcAddress(const char *name) const override;
276 #endif
277 
278 #ifdef ANDROID_DEBUG_GL
279  bool isRunningInMainThread() { return pthread_self() == _main_thread; }
280 #endif
281 
282  virtual const char * const *buildHelpDialogData() override;
283 };
284 
285 #endif
bool setGraphicsMode(int mode, uint flags) override
void delayMillis(uint msecs) override
Definition: modular-backend.h:48
bool hasFeature(OSystem::Feature f) override
Definition: system.h:106
Definition: str.h:59
int getDefaultGraphicsMode() const override
Common::KeymapperDefaultBindings * getKeymapperDefaultBindings() override
Common::Path getDefaultLogFileName() override
bool getFeatureState(OSystem::Feature f) override
void setWindowCaption(const Common::U32String &caption) override
Definition: stream.h:77
Common::String getSystemLanguage() const override
Definition: log.h:41
bool openUrl(const Common::String &url) override
Definition: system.h:730
EventType
Definition: events.h:49
uint32 getMillis(bool skipRecord=false) override
Feature
Definition: system.h:403
bool isConnectionLimited() override
void logMessage(LogMessageType::Type type, const char *message) override
Definition: path.h:52
void engineInit() override
Common::Path getDefaultConfigFileName() override
void getTimeAndDate(TimeDate &td, bool skipRecord=false) const override
void applyBackendSettings() override
Definition: object.h:60
void setFeatureState(OSystem::Feature f, bool enable) override
Definition: widget.h:532
void registerDefaultSettings(const Common::String &target) const override
Type
Definition: log.h:33
void engineDone() override
void updateStartSettings(const Common::String &executable, Common::String &command, Common::StringMap &startSettings, Common::StringArray &additionalArgs) override
Common::HardwareInputSet * getHardwareInputSet() override
Definition: mixer_intern.h:58
void initBackend() override
Definition: mixer.h:59
Definition: ustr.h:57
Common::KeymapArray getGlobalKeymaps() override
OpenGL::ContextType getOpenGLType() const override
Definition: android.h:273
Definition: archive.h:312
Common::U32String getTextFromClipboard() override
int getGraphicsMode() const override
virtual const char *const * buildHelpDialogData() override
void quit() override
Definition: events.h:199
Definition: hardware-input.h:199
Definition: mutex.h:67
Definition: rect.h:45
const OSystem::GraphicsMode * getSupportedGraphicsModes() const override
bool setTextInClipboard(const Common::U32String &text) override
void addSysArchivesToSearchSet(Common::SearchSet &s, int priority=0) override
Definition: keymapper-defaults.h:32
bool pollEvent(Common::Event &event) override
Audio::Mixer * getMixer() override
Definition: touchcontrols.h:41
Common::MutexInternal * createMutex() override
Definition: android.h:99
bool hasTextInClipboard() override
Definition: events.h:259
GUI::OptionsContainerWidget * buildBackendOptionsWidget(GUI::GuiObject *boss, const Common::String &name, const Common::String &target) const override
Definition: mutex.h:40