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 GOB_UTIL_H
24 #define GOB_UTIL_H
25 
26 #include "common/str.h"
27 #include "common/keyboard.h"
28 #include "common/events.h"
29 
30 namespace Common {
31 	class SeekableReadStream;
32 }
33 
34 namespace Gob {
35 
36 class GobEngine;
37 
38 #define KEYBUFSIZE 16
39 
40 enum MouseButtons {
41 	kMouseButtonsNone  = 0,
42 	kMouseButtonsLeft  = 1,
43 	kMouseButtonsRight = 2,
44 	kMouseButtonsBoth  = 3,
45 	kMouseButtonsAny   = 4
46 };
47 
48 enum Keys {
49 	kKeyNone      = 0x0000,
50 	kKeyBackspace = 0x0E08,
51 	kKeySpace     = 0x3920,
52 	kKeyReturn    = 0x1C0D,
53 	kKeyEscape    = 0x011B,
54 	kKeyDelete    = 0x5300,
55 	kKeyUp        = 0x4800,
56 	kKeyDown      = 0x5000,
57 	kKeyRight     = 0x4D00,
58 	kKeyLeft      = 0x4B00,
59 	kKeyF1        = 0x3B00,
60 	kKeyF2        = 0x3C00,
61 	kKeyF3        = 0x3D00,
62 	kKeyF4        = 0x3E00,
63 	kKeyF5        = 0x3F00,
64 	kKeyF6        = 0x4000,
65 	kKeyF7        = 0x4100,
66 	kKeyF8        = 0x4200,
67 	kKeyF9        = 0x4300,
68 	kKeyF10       = 0x4400
69 };
70 
71 enum ShortKey {
72 	kShortKeyUp        = 0x0B,
73 	kShortKeyDown      = 0x0A,
74 	kShortKeyRight     = 0x09,
75 	kShortKeyLeft      = 0x08,
76 	kShortKeyEscape    = 0x1B,
77 	kShortKeyBackspace = 0x19,
78 	kShortKeyDelete    = 0x1A
79 };
80 
81 class Util {
82 public:
83 	struct ListNode;
84 	struct ListNode {
85 		void *pData;
86 		struct ListNode *pNext;
87 		struct ListNode *pPrev;
ListNodeListNode88 		ListNode() : pData(0), pNext(0), pPrev(0) {}
89 	};
90 
91 	struct List {
92 		ListNode *pHead;
93 		ListNode *pTail;
ListList94 		List() : pHead(0), pTail(0) {}
95 	};
96 
97 	uint32 getTimeKey();
98 	int16 getRandom(int16 max);
99 	void beep(int16 freq);
100 
101 	void notifyPaused(uint32 duration);
102 
103 	void delay(uint16 msecs);
104 	void longDelay(uint16 msecs);
105 
106 	void initInput();
107 	void processInput(bool scroll = false);
108 	void clearKeyBuf();
109 	int16 getKey();
110 	int16 checkKey();
111 	bool checkKey(int16 &key);
112 	bool keyPressed();
113 
114 	uint32 getKeyState() const;
115 
116 	void getMouseState(int16 *pX, int16 *pY, MouseButtons *pButtons);
117 	void setMousePos(int16 x, int16 y);
118 	void waitMouseUp();
119 	void waitMouseDown();
120 	void waitMouseRelease(char drawMouse);
121 	void forceMouseUp(bool onlyWhenSynced = false);
122 
123 	void clearPalette();
124 	int16 getFrameRate();
125 	void setFrameRate(int16 rate);
126 	void notifyNewAnim();
127 	void waitEndFrame(bool handleInput = true);
128 	void setScrollOffset(int16 x = -1, int16 y = -1);
129 
130 	static void insertStr(const char *str1, char *str2, int16 pos);
131 	static void cutFromStr(char *str, int16 from, int16 cutlen);
132 	static void cleanupStr(char *str);
133 	static void replaceChar(char *str, char c1, char c2);
134 
135 	static void listInsertFront(List *list, void *data);
136 	static void listInsertBack(List *list, void *data);
137 	static void listDropFront(List *list);
138 	static void deleteList(List *list);
139 
140 	static char *setExtension(char *str, const char *ext);
141 	static Common::String setExtension(const Common::String &str, const Common::String &ext);
142 
143 	/** Read a constant-length string out of a stream. */
144 	static Common::String readString(Common::SeekableReadStream &stream, int n);
145 
146 	/** Convert a character in CP850 encoding to the equivalent lower case character. */
147 	static char toCP850Lower(char cp850);
148 	/** Convert a character in CP850 encoding to the equivalent upper case character. */
149 	static char toCP850Upper(char cp850);
150 
151 	Util(GobEngine *vm);
152 
153 protected:
154 	MouseButtons _mouseButtons;
155 
156 	Common::KeyState _keyBuffer[KEYBUFSIZE];
157 	int16 _keyBufferHead;
158 	int16 _keyBufferTail;
159 
160 	uint8 _fastMode;
161 
162 	int16 _frameRate;
163 	int16 _frameWaitTime;
164 	uint32 _startFrameTime;
165 
166 	uint32 _keyState;
167 
168 	GobEngine *_vm;
169 
170 	bool keyBufferEmpty();
171 	void addKeyToBuffer(const Common::KeyState &key);
172 	bool getKeyFromBuffer(Common::KeyState &key);
173 	int16 translateKey(const Common::KeyState &key);
174 	int16 toCP850(uint16 latin1);
175 	void checkJoystick();
176 
177 	void keyDown(const Common::Event &event);
178 	void keyUp(const Common::Event &event);
179 };
180 
181 } // End of namespace Gob
182 
183 #endif // GOB_UTIL_H
184