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 #if defined(MAEMO)
24 
25 #include "common/scummsys.h"
26 
27 #include "backends/events/maemosdl/maemosdl-events.h"
28 #include "backends/platform/maemo/maemo.h"
29 #include "common/translation.h"
30 
31 namespace Maemo {
32 
MaemoSdlEventSource()33 MaemoSdlEventSource::MaemoSdlEventSource() : SdlEventSource(), _clickEnabled(true) {
34 
35 }
36 
37 struct KeymapEntry {
38 	SDLKey sym;
39 	Common::KeyCode keycode;
40 	uint16 ascii;
41 };
42 
43 static const KeymapEntry keymapEntries[] = {
44 	{SDLK_F4, Common::KEYCODE_F11, 0},
45 	{SDLK_F5, Common::KEYCODE_F12, 0},
46 	{SDLK_F6, Common::KEYCODE_F13, 0},
47 	{SDLK_F7, Common::KEYCODE_F14, 0},
48 	{SDLK_F8, Common::KEYCODE_F15, 0},
49 	{SDLK_LAST, Common::KEYCODE_INVALID, 0}
50 };
51 
remapKey(SDL_Event & ev,Common::Event & event)52 bool MaemoSdlEventSource::remapKey(SDL_Event &ev, Common::Event &event) {
53 
54 	Model model = Model(((OSystem_SDL_Maemo *)g_system)->getModel());
55 	debug(10, "Model: %s %u %s %s", model.hwId, model.modelType, model.hwAlias, model.hasHwKeyboard ? "true" : "false");
56 
57 	// List of special N810 keys:
58 	// SDLK_F4 -> menu
59 	// SDLK_F5 -> home
60 	// SDLK_F6 -> fullscreen
61 	// SDLK_F7 -> zoom +
62 	// SDLK_F8 -> zoom -
63 
64 #ifdef ENABLE_KEYMAPPER
65 	if (ev.type == SDL_KEYDOWN || ev.type == SDL_KEYUP) {
66 		const KeymapEntry *entry;
67 		for (entry = keymapEntries; entry->sym != SDLK_LAST; ++entry) {
68 			if (ev.key.keysym.sym == entry->sym) {
69 				SDLModToOSystemKeyFlags(SDL_GetModState(), event);
70 				event.type = ev.type == SDL_KEYDOWN ? Common::EVENT_KEYDOWN : Common::EVENT_KEYUP;
71 				event.kbd.keycode = entry->keycode;
72 				event.kbd.ascii = entry->ascii;
73 				return true;
74 			}
75 		}
76 	}
77 #else
78 	switch (ev.type) {
79 		case SDL_KEYDOWN:{
80 			if (ev.key.keysym.sym == SDLK_F4
81 			    || (model.modelType == kModelTypeN900
82 			        && ev.key.keysym.sym == SDLK_m
83 			        && (ev.key.keysym.mod & KMOD_CTRL)
84 			        && (ev.key.keysym.mod & KMOD_SHIFT))) {
85 				event.type = Common::EVENT_MAINMENU;
86 				debug(9, "remapping to main menu");
87 				return true;
88 			} else if (ev.key.keysym.sym == SDLK_F6) {
89 				if (!model.hasHwKeyboard) {
90 #ifdef ENABLE_VKEYBD
91 					event.type = Common::EVENT_VIRTUAL_KEYBOARD;
92 					debug(9, "remapping to virtual keyboard trigger");
93 					return true;
94 #endif
95 				} else {
96 					// handled in keyup
97 				}
98 			} else if (ev.key.keysym.sym == SDLK_F7) {
99 				event.type = Common::EVENT_RBUTTONDOWN;
100 				processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
101 				 debug(9, "remapping to right click down");
102 				return true;
103 			} else if (ev.key.keysym.sym == SDLK_F8) {
104 				if (ev.key.keysym.mod & KMOD_CTRL) {
105 #ifdef ENABLE_VKEYBD
106 					event.type = Common::EVENT_VIRTUAL_KEYBOARD;
107 					debug(9, "remapping to virtual keyboard trigger");
108 					return true;
109 #endif
110 				} else {
111 					// handled in keyup
112 					return true;
113 				}
114 			}
115 			break;
116 		}
117 		case SDL_KEYUP: {
118 			if (ev.key.keysym.sym == SDLK_F4
119 			    || (model.modelType == kModelTypeN900
120 			        && ev.key.keysym.sym == SDLK_m
121 			        && (ev.key.keysym.mod & KMOD_CTRL)
122 			        && (ev.key.keysym.mod & KMOD_SHIFT))) {
123 				event.type = Common::EVENT_MAINMENU;
124 				return true;
125 			} else if (ev.key.keysym.sym == SDLK_F6) {
126 				if (!model.hasHwKeyboard) {
127 					// handled in keydown
128 				} else {
129 					bool currentState = ((OSystem_SDL *)g_system)->getGraphicsManager()->getFeatureState(OSystem::kFeatureFullscreenMode);
130 					g_system->beginGFXTransaction();
131 					((OSystem_SDL *)g_system)->getGraphicsManager()->setFeatureState(OSystem::kFeatureFullscreenMode, !currentState);
132 					g_system->endGFXTransaction();
133 					debug(9, "remapping to full screen toggle");
134 					return true;
135 				}
136 			} else if (ev.key.keysym.sym == SDLK_F7) {
137 				event.type = Common::EVENT_RBUTTONUP;
138 				processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
139 					debug(9, "remapping to right click up");
140 				return true;
141 			} else if (ev.key.keysym.sym == SDLK_F8) {
142 				if (ev.key.keysym.mod & KMOD_CTRL) {
143 					// handled in key down
144 				} else {
145 					toggleClickMode();
146 					debug(9, "remapping to click toggle");
147 					return true;
148 				}
149 			}
150 			break;
151 		}
152 	}
153 #endif
154 	// Invoke parent implementation of this method
155 	return SdlEventSource::remapKey(ev, event);
156 }
157 
handleMouseButtonDown(SDL_Event & ev,Common::Event & event)158 bool MaemoSdlEventSource::handleMouseButtonDown(SDL_Event &ev, Common::Event &event) {
159 
160 	if (ev.button.button == SDL_BUTTON_LEFT && !_clickEnabled) {
161 		return false;
162 	}
163 
164 	// Invoke parent implementation of this method
165 	return SdlEventSource::handleMouseButtonDown(ev, event);
166 }
167 
handleMouseButtonUp(SDL_Event & ev,Common::Event & event)168 bool MaemoSdlEventSource::handleMouseButtonUp(SDL_Event &ev, Common::Event &event) {
169 
170 	if (ev.button.button == SDL_BUTTON_LEFT && !_clickEnabled) {
171 		return false;
172 	}
173 
174 	// Invoke parent implementation of this method
175 	return SdlEventSource::handleMouseButtonUp(ev, event);
176 }
177 
toggleClickMode()178 bool MaemoSdlEventSource::toggleClickMode() {
179 	_clickEnabled = !_clickEnabled;
180 	_graphicsManager->displayMessageOnOSD(
181 	  _clickEnabled ? _("Clicking Enabled") : _("Clicking Disabled"));
182 
183 	return _clickEnabled;
184 }
185 
MaemoSdlEventObserver(MaemoSdlEventSource * eventSource)186 MaemoSdlEventObserver::MaemoSdlEventObserver(MaemoSdlEventSource *eventSource) {
187 	assert(eventSource);
188 	_eventSource = eventSource;
189 }
190 
notifyEvent(const Common::Event & event)191 bool MaemoSdlEventObserver::notifyEvent(const Common::Event &event) {
192 #ifdef ENABLE_KEYMAPPER
193 	if (event.type != Common::EVENT_CUSTOM_BACKEND_ACTION)
194 		return false;
195 	if (event.customType == kEventClickMode) {
196 		assert(_eventSource);
197 		_eventSource->toggleClickMode();
198 		return true;
199 	}
200 #endif
201 	return false;
202 }
203 
204 } // namespace Maemo
205 
206 #endif // if defined(MAEMO)
207