annotate mde/input/joystick.d @ 17:5f90774ea1ef

Applied the GNU GPL v2 to mde. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 15 Mar 2008 15:14:25 +0000
parents 4c3575400769
children 32eff0e01c05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
1 /* LICENSE BLOCK
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
2 Part of mde: a Modular D game-oriented Engine
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
4
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
5 This program is free software; you can redistribute it and/or modify it under the terms of
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
6 the GNU General Public License, version 2, as published by the Free Software Foundation.
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
7
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
10 See the GNU General Public License for more details.
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
11
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
12 You should have received a copy of the GNU General Public License along
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
13 with this program; if not, write to the Free Software Foundation, Inc.,
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
5f90774ea1ef Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 10
diff changeset
15
9
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /** Opens SDL joysticks ready for use.
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 * May be extended later to include other input devices and remap devices as per config.
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 */
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 module mde.input.joystick;
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 import tango.util.log.Log : Log, Logger;
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23 import derelict.sdl.joystick;
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 private Logger logger;
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26 static this() {
10
4c3575400769 DefaultData largely rewritten with unittest, SDL input event handling completed with unittest, changes to Init threading.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 9
diff changeset
27 logger = Log.getLogger ("mde.input.joystick");
9
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 }
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 private SDL_Joystick*[] joysticks; // pointers to all joystick structs, whether successfully opened or not
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 /** Open joysticks ready for use.
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32 *
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 * This is simply required for SDL to handle joystick events. It can fail, but won't affect anything
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 * else, except for the controller not working.
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 *
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 * closeJoysticks must be run to cleanup afterwards.
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 */
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38 void openJoysticks () {
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 joysticks = new SDL_Joystick*[SDL_NumJoysticks ()];
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 char tmp[128] = void;
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 for (int i = 0; i < joysticks.length; ++i) {
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 if ((joysticks[i] = SDL_JoystickOpen (i)) is null) { // null on failure
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 logger.warn (logger.format (tmp, "Unable to open joystick {} via SDL", i));
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 }
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 }
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 logger.info (logger.format (tmp, "Opened {} joysticks via SDL, succesfully unless preceding warnings say otherwise.", joysticks.length));
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 }
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51 /// Cleanup fct.
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 void closeJoysticks () {
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 foreach (js; joysticks) {
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 if(js) SDL_JoystickClose(js); // only close if successfully opened
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 }
1885a9080f2a Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 }