Created
January 13, 2013 01:24
-
-
Save akbsteam/4521648 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <config> | |
| <joystick id="0"> | |
| <axis number="0" low="Left" high="Right" /> | |
| <axis number="1" low="Up" high="Down" /> | |
| <button number="0" key="SN2" /> | |
| <button number="1" key="SN1" /> | |
| <button number="2" key="HC1" /> | |
| <button number="3" key="HC2" /> | |
| <button number="4" key="HC3" /> | |
| <button number="5" key="HHC1" /> | |
| <button number="6" key="HHC2" /> | |
| <button number="7" key="HHO1" /> | |
| <button number="9" key="HHO2" /> | |
| <button number="10" key="SN3" /> | |
| </joystick> | |
| </config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from xml.dom.minidom import * | |
| from threads import threaded | |
| import os | |
| os.environ["SDL_VIDEODRIVER"] = "dummy" | |
| class JoyStickAxis(): | |
| def __init__(self, lowKey, highKey): | |
| self.low = lowKey | |
| self.high = highKey | |
| self.low_active = False | |
| self.high_active = False | |
| class JoyStick(): | |
| def __init__(self): | |
| self.axis = { } | |
| self.buttons = { } | |
| def print_info(self): | |
| print "Joystick mappings found : " | |
| for axis in self.axis.keys(): | |
| print " - axis %s direction low mapped to key %s" % (axis, self.axis[axis].low) | |
| print " - axis %s direction high mapped to key %s" % (axis, self.axis[axis].high) | |
| for key in self.buttons: | |
| print " - button %s mapped to key %s" % (key, self.buttons[key]) | |
| class JKeys: | |
| def __init__(self, server, document): | |
| self.server = server | |
| self.joysticks = {} | |
| for joy_node in document.getElementsByTagName("joystick"): | |
| joy = JoyStick() | |
| for axis_node in joy_node.getElementsByTagName("axis"): | |
| axis = JoyStickAxis( axis_node.getAttribute("low"), axis_node.getAttribute("high")) | |
| joy.axis[ axis_node.getAttribute("number") ] = axis | |
| for button_node in joy_node.getElementsByTagName("button"): | |
| joy.buttons[button_node.getAttribute("number")] = button_node.getAttribute("key") | |
| self.joysticks[int(joy_node.getAttribute("id"))] = joy | |
| joy.print_info() | |
| try: | |
| import pygame | |
| except: | |
| print "You need to install pygame to capture joystick controls" | |
| pygame.init() | |
| self.nbJoy = pygame.joystick.get_count() | |
| for i in range(self.nbJoy): | |
| pygame.joystick.Joystick(i).init() | |
| if self.nbJoy == 0: | |
| print "Sorry no joysticks found!!" | |
| elif self.nbJoy != len(self.joysticks): | |
| print "%d joysticks configured, only %d joysticks found" % (len(self.joysticks), self.nbJoy) | |
| self.debug = False | |
| @threaded | |
| def run(self): | |
| if self.nbJoy != 0: | |
| import pygame | |
| pygame.display.init() | |
| pygame.time.set_timer(1, 100) | |
| capture_events = [pygame.JOYBUTTONDOWN, pygame.JOYAXISMOTION, pygame.JOYBUTTONUP, pygame.JOYHATMOTION] | |
| # clear out all pre existing pygame events else we get 1 of each | |
| pygame.event.clear() | |
| def key_press( key ): | |
| keyInfo = key + ":1" | |
| self.server.send_to_all(keyInfo) | |
| def key_release( key ): | |
| keyInfo = key + ":0" | |
| self.server.send_to_all(keyInfo) | |
| while True: | |
| pygame.event.pump() | |
| ev = pygame.event.wait() | |
| if ev.type in capture_events: | |
| joy = self.joysticks[ ev.joy ] | |
| if ev.type == pygame.JOYHATMOTION: | |
| res = gethatcode(ev) | |
| if self.debug: print "UNHANDLED JOY HAT code: ", res | |
| elif ev.type == pygame.JOYBUTTONDOWN: | |
| button = str(ev.button) | |
| if joy.buttons.has_key(button): | |
| key_code = joy.buttons[button] | |
| if self.debug: print "Button down : " + key_code | |
| key_press( key_code ) | |
| elif ev.type == pygame.JOYBUTTONUP: | |
| button = str(ev.button) | |
| if joy.buttons.has_key(button): | |
| key_code = joy.buttons[button] | |
| if self.debug: print "Button up : " + key_code | |
| key_release( key_code ) | |
| elif ev.type == pygame.JOYAXISMOTION: | |
| axis_number = str(ev.axis) | |
| if joy.axis.has_key(axis_number): | |
| axis = joy.axis[axis_number] | |
| if ev.value < 0: | |
| if self.debug: print "Button down : " + axis.low | |
| key_press( axis.low ) | |
| axis.low_active = True | |
| elif ev.value > 0: | |
| if self.debug: print "Button down : " + axis.high | |
| key_press( axis.high ) | |
| axis.high_active = True | |
| else: | |
| if axis.low_active: | |
| if self.debug: print "Button up : " + axis.low | |
| key_release( axis.low ) | |
| axis.low_active = False | |
| if axis.high_active: | |
| if self.debug: print "Button up : " + axis.high | |
| key_release( axis.high ) | |
| axis.high_active = False | |
| pygame.display.quit() | |
| return res | |
| def __del__(self): | |
| import pygame | |
| pygame.quit() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import threading | |
| import time | |
| def threaded(f): | |
| def wrapper(*args): | |
| t = threading.Thread(target=f, args=args) | |
| t.setDaemon(True) # wont keep app alive | |
| t.start() | |
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| from xml.dom.minidom import * | |
| import asyncore | |
| import asynchat | |
| import socket | |
| class Lobby(object): | |
| def __init__(self): | |
| self.clients = set() | |
| def leave(self, client): | |
| self.clients.remove(client) | |
| def join(self, client): | |
| self.clients.add(client) | |
| def send_to_all(self, data): | |
| for client in self.clients: | |
| client.push(data) | |
| class Client(asynchat.async_chat): | |
| def __init__(self, conn, lobby): | |
| asynchat.async_chat.__init__(self, sock=conn) | |
| self.in_buffer = "" | |
| self.set_terminator("n") | |
| self.lobby = lobby | |
| self.lobby.join(self) | |
| def collect_incoming_data(self, data): | |
| self.in_buffer += data | |
| def found_terminator(self): | |
| if self.in_buffer.rstrip() == "QUIT": | |
| self.lobby.leave(self) | |
| self.close_when_done() | |
| else: | |
| self.lobby.send_to_all(self.in_buffer + self.terminator) | |
| self.in_buffer = "" | |
| class Server(asynchat.async_chat): | |
| def __init__(self): | |
| asynchat.async_chat.__init__(self) | |
| self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | |
| self.set_reuse_addr() | |
| self.bind(('', 12345)) | |
| self.listen(5) | |
| self.lobby = None | |
| def set_lobby(self, lobby): | |
| self.lobby = lobby | |
| def handle_accept(self): | |
| sock, addr = self.accept() | |
| client = Client(sock, self.lobby) | |
| def send_to_all(self, msg): | |
| self.lobby.send_to_all(msg.encode("utf-8")+"\n") | |
| lobby = Lobby() | |
| server = Server() | |
| server.set_lobby(lobby) | |
| f = open("config.joy", "r") | |
| xml = f.read() | |
| f.close() | |
| document = parseString(xml) | |
| # Start joystick keys | |
| from joystick import JKeys | |
| j = JKeys(server, document) | |
| # j.debug = True | |
| j.run() | |
| asyncore.loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment