Mercurial > projects > mde
annotate mde/input/joystick.d @ 89:97e6dce08037
Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Mon, 29 Sep 2008 18:27:17 +0100 |
parents | 79d816b3e2d2 |
children | 2a364c7d82c9 |
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 |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify it under the terms |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
6 of the GNU General Public License as published by the Free Software Foundation, either |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
7 version 2 of the License, or (at your option) any later version. |
17
5f90774ea1ef
Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
10
diff
changeset
|
8 |
5f90774ea1ef
Applied the GNU GPL v2 to mde.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
10
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
13 You should have received a copy of the GNU General Public License |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
17
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 |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
67
diff
changeset
|
21 import mde.setup.exception; // InitStage |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
22 import tango.util.log.Log : Log, Logger; |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
23 |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
24 import derelict.sdl.joystick; |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
25 |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
26 private Logger logger; |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
27 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
|
28 logger = Log.getLogger ("mde.input.joystick"); |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
29 } |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
30 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
|
31 |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
32 /** Open joysticks ready for use. |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
33 * |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
34 * 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
|
35 * else, except for the controller not working. |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
36 * |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
37 * closeJoysticks must be run to cleanup afterwards. |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
38 */ |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
67
diff
changeset
|
39 StageState openJoysticks () { |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
40 joysticks = new SDL_Joystick*[SDL_NumJoysticks ()]; |
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 |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
31
diff
changeset
|
44 logger.error ("Unable to open joystick {} via SDL", i); |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
67
diff
changeset
|
45 return StageState.ERROR; // prevent closeJoysticks running, but don't halt program |
9
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 |
67
108d123238c0
Changes to work with tango r3700 (post 0.99.6).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
31
diff
changeset
|
49 logger.info ("Opened {} joysticks via SDL, succesfully unless preceding errors say otherwise.", joysticks.length); |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
67
diff
changeset
|
50 return StageState.ACTIVE; |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
51 } |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
52 |
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
53 /// Cleanup fct. |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
67
diff
changeset
|
54 StageState closeJoysticks () { |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
55 foreach (js; joysticks) { |
89
97e6dce08037
Solved some/removed some obsolete jobs/FIXMEs (excluding from gui code). General cleanup.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
86
diff
changeset
|
56 // NOTE: This sometimes causes a SIGSEGV (Address boundary error) when init fails. |
29
f985c28c0ec9
A new GUI widget plus changes to the init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
26
diff
changeset
|
57 if(js !is null) SDL_JoystickClose(js); // only close if successfully opened |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
58 } |
85
56c0ddd90193
Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
67
diff
changeset
|
59 return StageState.INACTIVE; |
9
1885a9080f2a
Joystick button input now works with config.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
60 } |