/usr/share/games/freevial/src/common/events.py is in freevial 1.3-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 | # -*- coding: utf-8 -*-
#
# Freevial
# Common event-related classes and functions
#
# Copyright (C) 2007, 2008 The Freevial Team
#
# By Carles Oriol i Margarit <carles@kumbaworld.com>
# By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
#
# 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 3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import re
import pygame
from freevialglob import screenshot, if2
mouseButtons = {
'primary': 1,
'secondary': 2,
'middle': 3,
}
# Aliases for PS2 remotes
joystick_aliases = {
0: pygame.K_RETURN,
1: pygame.K_ESCAPE,
2: pygame.K_RETURN,
3: pygame.K_s,
4: pygame.K_F2,
5: pygame.K_a,
6: pygame.K_F1,
7: pygame.K_F3,
8: pygame.K_SPACE,
9: pygame.K_ESCAPE,
12: pygame.K_UP,
13: pygame.K_RIGHT,
14: pygame.K_DOWN,
15: pygame.K_LEFT,
}
class EventHandle:
"""
This class takes a pygame event and creates an object with convenient
methods to identify it.
"""
global mouseButtons
def __init__(self, event, do_base_actions = True):
if event.type == pygame.JOYBUTTONDOWN:
event = self._convert_joystick_event(event)
self.event = event
# The following line should be deprecated when eventLoop improves.
self.type = event.type
self.handled = False
if do_base_actions and self.base_actions():
self.handled = True
def _convert_joystick_event(self, event):
if joystick_aliases.get(event.button):
return pygame.event.Event(pygame.KEYUP, { 'key': joystick_aliases[ event.button ], 'unicode': u's', 'mod': 0 })
def _getKey(self, key):
if type(key) is str:
if key[:2] != 'K_':
key = 'K_' + key
key = getattr(pygame, key)
return key
def _isKeyEvent(self):
return hasattr(self.event, 'key')
def _isStateEvent(self):
return hasattr(self.event, 'state')
def _hasKey(self, keynames):
if not self._isKeyEvent():
return False
if len(keynames) == 1 and type(keynames[0]) is tuple:
keynames = keynames[0]
for key in keynames:
if self.event.key == self._getKey(key):
return True
return False
def isKey(self, *keynames):
return self._hasKey(keynames)
def isUp(self):
return self.event.type == pygame.KEYUP
def isDown(self):
return self.event.type == pygame.KEYDOWN
def isClick(self, request = 0):
if type(request) is not int:
request = mouseButtons[ request ]
return self.event.type == pygame.MOUSEBUTTONDOWN and (self.event.button == request or request == 0)
def isRelease(self, request = 0):
if type(request) is not int:
request = mouseButtons[ request ]
return self.event.type == pygame.MOUSEBUTTONUP and (self.event.button == request or request == 0)
def keyUp(self, *keynames):
if not self.isUp():
return False
if len(keynames) == 0:
return True
return self.isKey(*keynames)
def keyDown(self, *keynames):
if not self.isDown():
return False
if len(keynames) == 0:
return True
return self.isKey(keynames)
def isWindowMinimize(self):
return self._isStateEvent() and self.event.state == 6 and self.event.gain == 0
def isWindowRestore(self):
return self._isStateEvent() and self.event.state == 4 and self.event.gain == 1
def isWindowFocusLose(self):
return self._isStateEvent() and self.event.state == 1 and self.event.gain == 0
def isWindowFocusGain(self):
return self._isStateEvent() and self.event.state == 1 and self.event.gain == 1
def isQuit(self):
return self.event.type == pygame.QUIT
def str(self):
return if2(self._isKeyEvent(), printKey(self.event.key), '')
def is_user_action(self):
return self.event.type in (pygame.KEYUP, pygame.KEYDOWN,
pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN, pygame.JOYBUTTONDOWN)
def base_actions(self):
if self.isQuit():
sys.exit()
elif self.keyDown('PRINT'):
screenshot(pygame.display.get_surface())
return True
elif self.isWindowFocusLose() or self.isWindowFocusGain():
# Those aren't interesting, skip them.
# We could also do some CPU saving here, but this would produce
# bad synchronization between the music and the images.
return True
elif self.keyUp('F11'):
pygame.display.toggle_fullscreen()
return True
elif self.isWindowMinimize():
pauseGameUntilRestore()
return True
else:
return False
def eventLoop():
"""
Generator which runs through the event loop, takes care of global
events and yields the unhandled event objects.
This function may be expanded in the future to add support for
external events (which could come from DBUS or other sources).
"""
for event in pygame.event.get():
eventhandle = EventHandle(event)
if eventhandle.handled: continue
yield eventhandle
def waitForMouseRelease():
while pygame.mouse.get_pressed()[0] + pygame.mouse.get_pressed()[1] + pygame.mouse.get_pressed()[2] != 0:
pygame.event.wait()
def pauseGameUntilRestore():
while True:
for event in pygame.event.get():
if EventHandle(event).isWindowRestore():
return False
# Sleep for 10 milliseconds.
# This has no visible effect but will drastically reduce CPU usage.
pygame.time.wait(10)
aobert = atancat = adieresi = acirc = False
accents = [u"aeiou", u"àèìòù", u"áéíóú", u"äëïöü", u"âêîôû" ]
def printKey(tecla):
""" Translates a pygame Key object for on-game printing of it's value. """
global aobert, atancat, adieresi, acirc, accents
keyname = pygame.key.name(tecla)
if keyname == 'space':
return ' '
if keyname == 'world 71':
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
return u'Ç'
else:
return u'ç'
if keyname == 'tab':
return ' '
if len(keyname) == 3 and keyname[:1] == '[' and keyname[2:] == ']':
keyname = keyname[1:2]
pos = accents[0].find(keyname)
if pos != -1:
if aobert: keyname = accents[1][pos]
if atancat: keyname = accents[2][pos]
if adieresi: keyname = accents[3][pos]
if acirc: keyname = accents[4][pos]
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
keyname = keyname.upper()
if tecla == 314:
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
atancat = True
elif pygame.key.get_mods() & pygame.KMOD_CTRL:
adieresi = True
elif pygame.key.get_mods() & pygame.KMOD_ALT:
acirc = True
else:
aobert = True
else:
aobert = atancat = adieresi = acirc = False
if not re.search(u"^[a-zA-Z0-9,.+'-/*àèìòùáéíóúäëïöüâêîôû ]$", keyname):
return ''
return keyname
|