/usr/lib/games/solarwolf/input.py is in solarwolf 1.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | """Translate pygame events to controls, part of SOLARWOLF."""
# Portions Copyright (C) 2002 Aaron "APS" Schlaegel, LGPL, see lgpl.txt
import pygame.joystick
from pygame.locals import *
import time, sys, os, pickle, tempfile
import game, snd
ScreenshotNum = 1
# control constants
DEFAULT = 0
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
PRESS = 5
ABORT = 6
# axis norm constants, add 2 for every axis
AXISLESS = 0
AXISMORE = 1
# hat norm constants, add 4 for every hat
HATSTART = HATUP = 0
HATLEFT = 1
HATRIGHT = 2
HATDOWN = 3
FINISHMUSIC = USEREVENT+3
#translation tables
translations_default = {
KEYDOWN: {
K_UP: UP,
K_DOWN: DOWN,
K_LEFT: LEFT,
K_RIGHT: RIGHT,
K_RETURN: PRESS,
K_SPACE: PRESS,
K_KP8: UP,
K_KP2: DOWN,
K_KP4: LEFT,
K_KP6: RIGHT,
K_KP5: DOWN,
K_ESCAPE: ABORT,
K_DELETE: ABORT,
K_BREAK: ABORT,
#vi keys
K_h: LEFT,
K_j: DOWN,
K_k: UP,
K_l: RIGHT,
},
NOEVENT: {
# KEYDOWN: PRESS,
# JOYAXISMOTION: None,
# JOYBUTTONDOWN: PRESS,
# JOYHATMOTION: None
},
JOYAXISMOTION: {
AXISLESS + 0: LEFT,
AXISMORE + 0: RIGHT,
AXISLESS + 2: UP,
AXISMORE + 2: DOWN,
AXISLESS + 4: LEFT,
AXISMORE + 4: RIGHT,
AXISLESS + 6: UP,
AXISMORE + 6: DOWN,
},
JOYBUTTONDOWN: {
1: PRESS,
2: PRESS,
3: PRESS,
12: UP,
14: DOWN,
15: LEFT,
13: RIGHT,
},
JOYHATMOTION: {
HATUP + 0: UP,
HATDOWN + 0: DOWN,
HATLEFT + 0: LEFT,
HATRIGHT + 0: RIGHT,
}
}
hat_direction_text = {
HATUP: "Up", HATLEFT: "Left", HATRIGHT: "Right", HATDOWN: "Down"
}
actions_text = {
UP: "Up",
DOWN: "Down",
LEFT: "Left",
RIGHT: "Right",
PRESS: "Turbo",
ABORT: "Abort",
}
actions_order = [
UP, DOWN, LEFT, RIGHT, PRESS, ABORT
]
joystick = None
lastaxisvalue = []
translations = {}
joystick = None
lastaxisvalue = []
lasthatvalue = []
exclusivedict = {}
Cheatstring = ''
def init():
"init the joystick"
global joystick
global lastaxisvalue
global lasthatvalue
try:
num = pygame.joystick.get_count()
if num > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
lastaxisvalue = [None]*joystick.get_numaxes()
lasthatvalue = [[None, None]]*joystick.get_numhats()
except pygame.error:
joystick = None
def postactive():
keystate = pygame.key.get_pressed()
for key in range (len(keystate)):
if keystate[key]:
#I don't know how to get unicode
pygame.event.post(pygame.event.Event(KEYDOWN, {'key': key, 'mod': pygame.key.get_mods()}))
if joystick:
for button in range(joystick.get_numbuttons()):
if joystick.get_button(button):
pygame.event.post(pygame.event.Event(JOYBUTTONDOWN, {'joy': joystick.get_id(), 'button': button}))
for hat in range(joystick.get_numhats()):
value = joystick.get_hat(hat)
if 0 != value[0] or 0 != value[1]:
pygame.event.post(pygame.event.Event(JOYHATMOTION, {'joy': joystick.get_id(), 'hat': hat, 'value': value}))
for axis in range(joystick.get_numaxes()):
lastaxisvalue[axis] = None
value = joystick.get_axis(axis)
pygame.event.post(pygame.event.Event(JOYAXISMOTION, {'joy': joystick.get_id(), 'axis': axis, 'value': value}))
def resetexclusive():
global exclusivedict
exclusivedict.clear()
def exclusive(list, i):
global exclusivedict
thislist = str(list)
if i.translated in list:
if not exclusivedict.has_key(thislist):
exclusivedict[thislist] = {}
table = exclusivedict[thislist]
if i.release:
if not table.has_key(i.translated):
table[i.translated] = 0
else:
table[i.translated] -= 1
else:
if not table.has_key(i.translated):
table[i.translated] = 1
else:
table[i.translated] += 1
currentvalue = table[i.translated]
currentkey = i.translated
for key, value in table.items():
if value > currentvalue:
currentkey = key
currentvalue = value
if currentvalue > 0 and currentkey != i.translated:
newdict = i.dict
newdict['translated'] = currentkey
newdict['release'] = 0
return pygame.event.Event(i.type, newdict)
return i
def translate(event):
global lastaxisvalue, lasthatvalue, Cheatstring
normalized = None
translated = None
all = 0
release = 0
if event.type in (KEYDOWN, KEYUP):
# K_NUMLOCK and K_CAPSLOCK don't work right in pygame
if event.type == KEYDOWN:
if event.key == K_BACKSPACE:
Cheatstring = Cheatstring[:-1]
else:
try:
Cheatstring = Cheatstring[-4:]+event.unicode
except (TypeError, AttributeError):
Cheatstring = ''
if event.key == K_PRINT:
if event.type == KEYDOWN:
global ScreenshotNum
dir = os.environ.get('HOME', '.')
file = 'solarwolf%02d.bmp' % ScreenshotNum
fullname = os.path.join(dir, file)
print 'Screenshot:', fullname
try:
pygame.image.save(pygame.display.get_surface(), fullname)
except:
print ' Screenshot FAILED'
ScreenshotNum += 1
elif event.key not in (K_NUMLOCK, K_CAPSLOCK):
normalized = event.key
translated = translations[KEYDOWN].get(normalized, DEFAULT)
if translated == DEFAULT:
translated = translations[NOEVENT].get(KEYDOWN, None)
all = 1
if event.type == KEYUP:
release = 1
elif event.type == JOYAXISMOTION:
value = None
if event.value > .8:
value = AXISMORE
elif event.value < -.8:
value = AXISLESS
axis = event.axis
if value != lastaxisvalue[axis]:
if lastaxisvalue[axis] == None:
lastaxisvalue[axis] = value
if value != None:
normalized = axis * 2 + value
translated = translations[JOYAXISMOTION].get(normalized, DEFAULT)
if translated == DEFAULT:
translated = translations[NOEVENT].get(JOYAXISMOTION, None)
all = 1
else:
normalized = axis * 2 + lastaxisvalue[axis]
translated = translations[JOYAXISMOTION].get(normalized, DEFAULT)
if translated == DEFAULT:
translated = translations[NOEVENT].get(JOYAXISMOTION, None)
all = 1
if value != None:
pygame.event.post(event)
release = 1
lastaxisvalue[axis] = None
elif event.type == JOYHATMOTION:
hat = event.hat
value = [
(HATLEFT, None, HATRIGHT)[-1 * event.value[0] + 1],
(HATUP, None, HATDOWN)[-1 * event.value[1] + 1]
]
for axis in (0, 1):
if value[axis] != lasthatvalue[hat][axis]:
if lasthatvalue[hat][axis] == None:
lasthatvalue[hat][axis] = value[axis]
if value[axis] != None:
normalized = hat * 4 + value[axis]
translated = translations[JOYHATMOTION].get(normalized, DEFAULT)
if translated == DEFAULT:
translated = translations[NOEVENT].get(JOYHATMOTION, None)
all = 1
else:
normalized = hat * 4 + lasthatvalue[hat][axis]
translated = translations[JOYHATMOTION].get(normalized, DEFAULT)
if translated == DEFAULT:
translated = translations[NOEVENT].get(JOYHATMOTION, None)
all = 1
if value[axis] != None:
pygame.event.post(event)
release = 1
lasthatvalue[hat][axis] = None
elif event.type in (JOYBUTTONDOWN, JOYBUTTONUP):
normalized = event.button
translated = translations[JOYBUTTONDOWN].get(normalized, DEFAULT)
if translated == DEFAULT:
translated = translations[NOEVENT].get(JOYBUTTONDOWN, None)
all = 1
if event.type == JOYBUTTONUP:
release = 1
elif event.type == FINISHMUSIC:
snd.finish_playmusic()
newdict = event.dict
newdict['normalized'] = normalized
newdict['translated'] = translated
newdict['all'] = all
newdict['release'] = release
return pygame.event.Event(event.type, newdict)
def input_text(type, normalized):
global hat_direction_text
if type == KEYDOWN:
# K_NUMLOCK and K_CAPSLOCK don't work in pygame
if normalized not in (K_NUMLOCK, K_CAPSLOCK):
return 'Key' + pygame.key.name(normalized).title().replace(' ', '')
elif type == JOYBUTTONDOWN:
return 'Btn' + str(normalized)
elif type == JOYAXISMOTION:
if normalized % 2 == 0:
direction = "-"
value = AXISLESS
else:
direction = "+"
value = AXISMORE
axis = str( (normalized - value) / 2 )
return 'Axis' + axis + direction
elif type == JOYHATMOTION:
value = HATSTART + normalized % 4
direction = hat_direction_text[value]
hat = str( (normalized - value) / 4 )
return 'Hat' + hat + direction
elif type == NOEVENT:
return "*AllElse*"
def getdisplay():
global translations
display = {}
for type, table in translations.items():
for normalized, action in table.items():
if not display.has_key(action):
display[action] = []
if type != NOEVENT:
display[action].append((type, normalized))
else:
if normalized == KEYDOWN:
display[action].append((type, normalized))
for a in actions_order:
display[a].sort()
return display
def setdisplay(display):
global translations
translations.clear()
translations[NOEVENT] = {}
for a in actions_order:
for l in range(len(display[a])):
type = display[a][l][0]
normalized = display[a][l][1]
if not translations.has_key(type):
translations[type] = {}
if type != NOEVENT:
translations[type][normalized] = a
else:
translations[type][KEYDOWN] = a
translations[type][JOYBUTTONDOWN] = a
def load_translations():
global translations
global translations_default
translations = {}
#just sticking to the standard controls for now
#try:
# filename = game.make_dataname('input')
# translations = pickle.load(open(filename, 'rb'))
#except (IOError, OSError, KeyError):
#print 'ERROR OPENING CONTROL FILE, loading defaults'
if 1:
translations = translations_default
def save_translations():
global translations
#not gonna save for now
return
try:
filename = game.make_dataname('input')
f = open(filename, 'wb')
p = pickle.Pickler(f, 1)
p.dump(translations)
f.close()
except (IOError, OSError), msg:
import messagebox
messagebox.error("Error Saving Control Data",
"There was an error saving the control data.\nCurrent player controls have been lost.\n\n%s"%msg)
|