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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef BACKENDS_PLATFORM_IPHONE_OSYS_MAIN_H
24 #define BACKENDS_PLATFORM_IPHONE_OSYS_MAIN_H
25 
26 #include "graphics/surface.h"
27 #include "backends/platform/iphone/iphone_common.h"
28 #include "backends/base-backend.h"
29 #include "common/events.h"
30 #include "audio/mixer_intern.h"
31 #include "backends/fs/posix/posix-fs-factory.h"
32 #include "graphics/colormasks.h"
33 #include "graphics/palette.h"
34 
35 #include <AudioToolbox/AudioQueue.h>
36 
37 #define AUDIO_BUFFERS 3
38 #define WAVE_BUFFER_SIZE 2048
39 #define AUDIO_SAMPLE_RATE 44100
40 
41 #define SCUMMVM_ROOT_PATH "/var/mobile/Library/ScummVM"
42 #define SCUMMVM_SAVE_PATH SCUMMVM_ROOT_PATH "/Savegames"
43 #define SCUMMVM_PREFS_PATH SCUMMVM_ROOT_PATH "/Preferences"
44 
45 typedef void (*SoundProc)(void *param, byte *buf, int len);
46 typedef int (*TimerProc)(int interval);
47 
48 struct AQCallbackStruct {
49 	AudioQueueRef queue;
50 	uint32 frameCount;
51 	AudioQueueBufferRef buffers[AUDIO_BUFFERS];
52 	AudioStreamBasicDescription dataFormat;
53 };
54 
55 class OSystem_IPHONE : public EventsBaseBackend, public PaletteManager {
56 protected:
57 	static const OSystem::GraphicsMode s_supportedGraphicsModes[];
58 	static AQCallbackStruct s_AudioQueue;
59 	static SoundProc s_soundCallback;
60 	static void *s_soundParam;
61 
62 	Audio::MixerImpl *_mixer;
63 
64 	VideoContext *_videoContext;
65 
66 	Graphics::Surface _framebuffer;
67 
68 	// For signaling that screen format set up might have failed.
69 	TransactionError _gfxTransactionError;
70 
71 	// For use with the game texture
72 	uint16  _gamePalette[256];
73 	// For use with the mouse texture
74 	uint16  _gamePaletteRGBA5551[256];
75 
76 	struct timeval _startTime;
77 	uint32 _timeSuspended;
78 
79 	bool _mouseCursorPaletteEnabled;
80 	uint16 _mouseCursorPalette[256];
81 	Graphics::Surface _mouseBuffer;
82 	uint16 _mouseKeyColor;
83 	bool _mouseDirty;
84 	bool _mouseNeedTextureUpdate;
85 
86 	long _lastMouseDown;
87 	long _lastMouseTap;
88 	long _queuedEventTime;
89 	Common::Event _queuedInputEvent;
90 	bool _secondaryTapped;
91 	long _lastSecondaryDown;
92 	long _lastSecondaryTap;
93 	int _gestureStartX, _gestureStartY;
94 	bool _mouseClickAndDragEnabled;
95 	bool _touchpadModeEnabled;
96 	int _lastPadX;
97 	int _lastPadY;
98 	int _lastDragPosX;
99 	int _lastDragPosY;
100 
101 	int _timerCallbackNext;
102 	int _timerCallbackTimer;
103 	TimerProc _timerCallback;
104 
105 	Common::Array<Common::Rect> _dirtyRects;
106 	Common::Array<Common::Rect> _dirtyOverlayRects;
107 	ScreenOrientation _screenOrientation;
108 	bool _fullScreenIsDirty;
109 	bool _fullScreenOverlayIsDirty;
110 	int _screenChangeCount;
111 
112 public:
113 
114 	OSystem_IPHONE();
115 	virtual ~OSystem_IPHONE();
116 
117 	virtual void initBackend();
118 
119 	virtual bool hasFeature(Feature f);
120 	virtual void setFeatureState(Feature f, bool enable);
121 	virtual bool getFeatureState(Feature f);
122 	virtual const GraphicsMode *getSupportedGraphicsModes() const;
123 	virtual int getDefaultGraphicsMode() const;
124 	virtual bool setGraphicsMode(int mode);
125 	virtual int getGraphicsMode() const;
126 	virtual void initSize(uint width, uint height, const Graphics::PixelFormat *format);
127 
128 	virtual void beginGFXTransaction();
129 	virtual TransactionError endGFXTransaction();
130 
131 	virtual int16 getHeight();
132 	virtual int16 getWidth();
133 
134 #ifdef USE_RGB_COLOR
getScreenFormat()135 	virtual Graphics::PixelFormat getScreenFormat() const { return _framebuffer.format; }
136 	virtual Common::List<Graphics::PixelFormat> getSupportedFormats() const;
137 #endif
138 
getPaletteManager()139 	virtual PaletteManager *getPaletteManager() { return this; }
140 protected:
141 	// PaletteManager API
142 	virtual void setPalette(const byte *colors, uint start, uint num);
143 	virtual void grabPalette(byte *colors, uint start, uint num) const;
144 
145 public:
146 	virtual void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h);
147 	virtual void updateScreen();
148 	virtual Graphics::Surface *lockScreen();
149 	virtual void unlockScreen();
150 	virtual void setShakePos(int shakeXOffset, int shakeYOffset);
151 
152 	virtual void showOverlay();
153 	virtual void hideOverlay();
154 	virtual void clearOverlay();
155 	virtual void grabOverlay(void *buf, int pitch);
156 	virtual void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h);
157 	virtual int16 getOverlayHeight();
158 	virtual int16 getOverlayWidth();
getOverlayFormat()159 	virtual Graphics::PixelFormat getOverlayFormat() const { return Graphics::createPixelFormat<5551>(); }
160 
161 	virtual bool showMouse(bool visible);
162 
163 	virtual void warpMouse(int x, int y);
164 	virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor = 255, bool dontScale = false, const Graphics::PixelFormat *format = NULL);
165 	virtual void setCursorPalette(const byte *colors, uint start, uint num);
166 
167 	virtual bool pollEvent(Common::Event &event);
168 	virtual uint32 getMillis(bool skipRecord = false);
169 	virtual void delayMillis(uint msecs);
170 
171 	virtual MutexRef createMutex(void);
172 	virtual void lockMutex(MutexRef mutex);
173 	virtual void unlockMutex(MutexRef mutex);
174 	virtual void deleteMutex(MutexRef mutex);
175 
176 	static void mixCallback(void *sys, byte *samples, int len);
177 	virtual void setupMixer(void);
178 	virtual void setTimerCallback(TimerProc callback, int interval);
getScreenChangeID()179 	virtual int getScreenChangeID() const { return _screenChangeCount; }
180 	virtual void quit();
181 
182 	virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0);
183 	virtual void getTimeAndDate(TimeDate &t) const;
184 
185 	virtual Audio::Mixer *getMixer();
186 
187 	void startSoundsystem();
188 	void stopSoundsystem();
189 
190 	virtual Common::String getDefaultConfigFileName();
191 
192 	virtual void logMessage(LogMessageType::Type type, const char *message);
193 
194 protected:
195 	void initVideoContext();
196 	void updateOutputSurface();
197 
198 	void internUpdateScreen();
199 	void dirtyFullScreen();
200 	void dirtyFullOverlayScreen();
201 	void suspendLoop();
202 	void drawDirtyRect(const Common::Rect &dirtyRect);
203 	void updateMouseTexture();
204 	static void AQBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB);
205 	static int timerHandler(int t);
206 
207 	bool handleEvent_swipe(Common::Event &event, int direction);
208 	void handleEvent_keyPressed(Common::Event &event, int keyPressed);
209 	void handleEvent_orientationChanged(int orientation);
210 
211 	bool handleEvent_mouseDown(Common::Event &event, int x, int y);
212 	bool handleEvent_mouseUp(Common::Event &event, int x, int y);
213 
214 	bool handleEvent_secondMouseDown(Common::Event &event, int x, int y);
215 	bool handleEvent_secondMouseUp(Common::Event &event, int x, int y);
216 
217 	bool handleEvent_mouseDragged(Common::Event &event, int x, int y);
218 	bool handleEvent_mouseSecondDragged(Common::Event &event, int x, int y);
219 };
220 
221 #endif
222