-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystick.py
213 lines (202 loc) · 8.98 KB
/
joystick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: UTF-8 -*-
#
###
# Application: wah!cade
# File: joystick.py
# Description: Wah!Cade Joystick Class
# Copyright (c) 2007,2010 zerodiv, sairuk
###
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
from constants import *
from wc_common import WahCade
wc = WahCade()
import gtk
_ = gettext.gettext
pygame_imported = False
try:
import pygame
pygame_imported = True
except ImportError:
print _('Warning: pygame module not found. Joysticks not supported')
class joystick:
"""pygame joystick class"""
def __init__(self, debug=False):
"""initialise"""
self.debug = False
self.state = {}
self.devices = {}
self.ctrls = {}
self.mw_key_events = []
if pygame_imported:
pygame.init()
pygame.mixer.quit()
self.prtjoy = 0
if debug:
wc.log_msg("[PYGAME] init ok",1)
# stop and start the joystick
def joy_count(self, type):
self.num_joysticks = pygame.joystick.get_count()
for dev_num in self.devices.iterkeys():
if dev_num < self.num_joysticks:
self.devices[dev_num] = pygame.joystick.Joystick(dev_num)
if type == 'start':
self.devices[dev_num].init()
if self.devices[dev_num].get_init:
wc.log_msg("[PYGAME] started joystick device number: " + str(dev_num))
else:
wc.log_msg("[PYGAME] Joystick device number: " + str(dev_num) + " failed to initialise correctly")
elif type == 'stop':
self.devices[dev_num].quit()
if self.devices[dev_num].get_init:
wc.log_msg("[PYGAME] Stopped joystick device number: " + str(dev_num))
else:
wc.log_msg("[PYGAME] Could not stop device number: " + str(dev_num))
else:
return 1
def use_ini_controls(self, ctrlr_ini):
"""read controller ini file"""
if not pygame_imported:
return
for mw_keys in ctrlr_ini.ini_dict.itervalues():
mw_keys = mw_keys.strip(' "').split(' | ')
for mw_key in mw_keys:
if mw_key[:3] == "JOY":
self.state[mw_key] = 0
(dev_num, control) = mw_key.split("_", 1)
dev_num = dev_num[3:]
self.devices[int(dev_num) - 1] = None
self.joy_count('start')
def use_all_controls(self):
"""which joysticks"""
if not pygame_imported:
return
self.joy_count('start')
num_buttons = self.devices[dev_num].get_numbuttons()
for button_num in range(num_buttons):
mw_key = "JOY%s_BUTTON%s" % (dev_num + 1, button_num)
ctrl_name = _('Joystick %s Button %s') % (
dev_num + 1, button_num)
self.state[mw_key] = 0
self.ctrls[ctrl_name] = mw_key
if self.devices[dev_num].get_numaxes() > 2:
mw_key = "JOY%s_" % (dev_num + 1)
ctrl_name = _('Joystick %s ') % (dev_num + 1)
self.state[mw_key + "UP"] = 0
self.state[mw_key + "DOWN"] = 0
self.state[mw_key + "LEFT"] = 0
self.state[mw_key + "RIGHT"] = 0
self.ctrls[ctrl_name + _('Up')] = mw_key + "UP"
self.ctrls[ctrl_name + _('Down')] = mw_key + "DOWN"
self.ctrls[ctrl_name + _('Left')] = mw_key + "LEFT"
self.ctrls[ctrl_name + _('Right')] = mw_key + "RIGHT"
# Get the number of joysticks attached to the system
# Log joystick names
# Log joystick device numbers
def joy_info(self):
"""gather joystick information"""
for i in range(0, self.num_joysticks):
wc.log_msg("[PYGAME] Joystick: " + str(pygame.joystick.Joystick(i).get_name()))
wc.log_msg("[PYGAME] Device ID: " + str(pygame.joystick.Joystick(i).get_id()))
wc.log_msg("[PYGAME] Number of axes: " + str(pygame.joystick.Joystick(i).get_numaxes()))
wc.log_msg("[PYGAME] Number of trackballs: " + str(pygame.joystick.Joystick(i).get_numballs()))
wc.log_msg("[PYGAME] Number of buttons: " + str(pygame.joystick.Joystick(i).get_numbuttons()))
wc.log_msg("[PYGAME] Number of hats: " + str(pygame.joystick.Joystick(i).get_numhats()))
def proc_keys(self, active_window, e, mw_key):
if active_window.window:
e.window = active_window.window
if self.debug:
wc.log_msg("[PYGAME] joystick, key-release: " + mw_key,1)
# poll joysticks
def poll(self, event_cb, initial_repeat_delay=10):
"""poll for joystick events"""
if not pygame_imported:
if self.debug:
wc.log_msg("[PYGAME] not imported, returning from poll function",1)
return 0
repeat_delay = 1
# give pygame a chance to do its magic with the joystick
pygame.event.pump()
# end polling if no joysticks are found
if self.num_joysticks == 0 and self.prtjoy == 0:
wc.log_msg("[PYGAME] No joysticks found")
self.prtjoy = 1
return 0,
else:
# get the focused window or return
active_window = None
windows = gtk.window_list_toplevels()
for window in windows:
if window.is_active():
active_window = window
if active_window == None:
return 1
# check if any of our defined controls were pressed
mw_key_events = []
for mw_key in self.state.iterkeys():
(dev_num, joy_type) = (mw_key.split("_", 1))
dev_num = int(dev_num[3:]) - 1
# if binding is for an unfound joystick, continue to next binding
if dev_num > self.num_joysticks - 1:
continue
if joy_type[:6] == "BUTTON":
button_num = joy_type[6:]
if self.devices[dev_num].get_button(int(button_num)):
self.state[mw_key] += 1
mw_key_events.append(mw_key)
elif self.state[mw_key] > 0:
self.state[mw_key] = 0
mw_key_events.append(mw_key)
elif joy_type in ["LEFT", "RIGHT", "UP", "DOWN"]:
if joy_type in ["LEFT", "RIGHT"]:
axis_num = 0
else:
axis_num = 1
axis_value = self.devices[dev_num].get_axis(axis_num)
if joy_type in ["UP", "LEFT"]:
if axis_value < -0.5:
self.state[mw_key] += 1
mw_key_events.append(mw_key)
elif self.state[mw_key] > 0:
self.state[mw_key] = 0
mw_key_events.append(mw_key)
else:
if axis_value > 0.5:
self.state[mw_key] += 1
mw_key_events.append(mw_key)
elif self.state[mw_key] > 0:
self.state[mw_key] = 0
mw_key_events.append(mw_key)
# send fake key-press events
if len(mw_key_events) > 0:
mw_key = mw_key_events[0]
if self.state[mw_key] == 0:
e = gtk.gdk.Event(gtk.gdk.KEY_RELEASE)
self.proc_keys(active_window, e, mw_key)
event_cb(active_window, e, "JOYSTICK", mw_key)
elif self.state[mw_key] == 1:
e = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
self.proc_keys(active_window, e, mw_key)
event_cb(active_window, e, "JOYSTICK", mw_key)
#try clearing event queue
try:
pygame.event.clear()
if self.debug:
wc.log_msg("[PYGAME] Cleared pygame events",1)
except:
if self.debug:
wc.log_msg("[PYGAME] Could not clear PyGame event queue",1)
return 1