|
import sys |
|
import json |
|
paths = [ |
|
'', |
|
'/usr/lib/python2.7', |
|
'/usr/lib/python2.7/plat-x86_64-linux-gnu', |
|
'/usr/lib/python2.7/lib-tk', |
|
'/usr/lib/python2.7/lib-old', |
|
'/usr/lib/python2.7/lib-dynload', |
|
'/home/zezic/.local/lib/python2.7/site-packages', |
|
'/usr/local/lib/python2.7/dist-packages', |
|
'/usr/lib/python2.7/dist-packages', |
|
'/usr/lib/python2.7/dist-packages/gtk-2.0' |
|
] |
|
for path in paths: |
|
sys.path.insert(0, path) |
|
|
|
import vjoy |
|
import pyautogui |
|
from threading import Thread |
|
from pymouse import PyMouseEvent |
|
from pykeyboard import PyKeyboardEvent |
|
from Xlib import X |
|
from Xlib.protocol import rq |
|
import zmq |
|
|
|
|
|
def getVJoyInfo(): |
|
return { |
|
'name': 'PSX DualShock', |
|
'relaxis': [], |
|
'absaxis': [vjoy.ABS_X, vjoy.ABS_Y, vjoy.ABS_RX, vjoy.ABS_RY], |
|
'feedback': [], |
|
'buttons': [vjoy.KEY_LEFT, vjoy.KEY_RIGHT, vjoy.KEY_UP, |
|
vjoy.KEY_DOWN, vjoy.BTN_A, vjoy.BTN_B, |
|
vjoy.BTN_X, vjoy.BTN_Y, vjoy.BTN_TL, |
|
vjoy.BTN_TR, vjoy.BTN_TL2, vjoy.BTN_TR2, |
|
vjoy.BTN_SELECT, vjoy.BTN_START, vjoy.BTN_THUMBL, |
|
vjoy.BTN_THUMBR], |
|
'maxeffects': 4 |
|
} |
|
|
|
|
|
class Keyboard(PyKeyboardEvent): |
|
def __init__(self): |
|
PyKeyboardEvent.__init__(self) |
|
self.KEY_1 = False |
|
self.KEY_2 = False |
|
self.KEY_3 = False |
|
self.KEY_4 = False |
|
|
|
def handler(self, reply): |
|
data = reply.data |
|
while len(data): |
|
event, data = rq.EventField(None).parse_binary_value(data, self.display.display, None, None) |
|
if event.detail == self.lookup_character_keycode('1'): |
|
if event.type == X.KeyPress: |
|
self.KEY_1 = True |
|
if event.type == X.KeyRelease: |
|
self.KEY_1 = False |
|
if event.detail == self.lookup_character_keycode('2'): |
|
if event.type == X.KeyPress: |
|
self.KEY_2 = True |
|
if event.type == X.KeyRelease: |
|
self.KEY_2 = False |
|
if event.detail == self.lookup_character_keycode('3'): |
|
if event.type == X.KeyPress: |
|
self.KEY_3 = True |
|
if event.type == X.KeyRelease: |
|
self.KEY_3 = False |
|
if event.detail == self.lookup_character_keycode('4'): |
|
if event.type == X.KeyPress: |
|
self.KEY_4 = True |
|
if event.type == X.KeyRelease: |
|
self.KEY_4 = False |
|
|
|
|
|
class Mouse(PyMouseEvent): |
|
def __init__(self): |
|
PyMouseEvent.__init__(self) |
|
self.left = False |
|
self.right = False |
|
self.mid = False |
|
|
|
def click(self, x, y, button, press): |
|
'''Print Fibonacci numbers when the left click is pressed.''' |
|
if button == 1: |
|
self.left = press |
|
if button == 2: |
|
self.right = press |
|
if button == 3: |
|
self.mid = press |
|
|
|
|
|
m = Mouse() |
|
k = Keyboard() |
|
|
|
width = pyautogui.size()[0] |
|
|
|
mt = Thread(target=m.run) |
|
mt.start() |
|
kt = Thread(target=k.run) |
|
kt.start() |
|
|
|
left = 0 |
|
right = 0 |
|
|
|
context = zmq.Context() |
|
socket = context.socket(zmq.PAIR) |
|
socket.bind('tcp://*:5556') |
|
|
|
def doVJoyThink(): |
|
global left |
|
global right |
|
speed = 2048 |
|
|
|
mouse_x = pyautogui.position()[0] |
|
mul = (mouse_x - (width / 4)) / float(width / 2) |
|
x = round(65536 * mul) - 32768 |
|
|
|
if m.left: |
|
if left < 32768: |
|
left += speed |
|
else: |
|
if left > 0: |
|
left -= speed |
|
|
|
if m.right: |
|
if right < 32768: |
|
right += speed |
|
else: |
|
if right > 0: |
|
right -= speed |
|
|
|
events = [ |
|
[vjoy.EV_ABS, vjoy.ABS_X, x], |
|
[vjoy.EV_ABS, vjoy.ABS_RX, left], |
|
[vjoy.EV_ABS, vjoy.ABS_RY, right], |
|
[vjoy.EV_KEY, vjoy.BTN_A, k.KEY_1], |
|
[vjoy.EV_KEY, vjoy.BTN_B, k.KEY_2], |
|
[vjoy.EV_KEY, vjoy.BTN_X, k.KEY_3], |
|
[vjoy.EV_KEY, vjoy.BTN_Y, k.KEY_4] |
|
] |
|
socket.send(json.dumps({ |
|
'x': mul |
|
})) |
|
return events |