-
Notifications
You must be signed in to change notification settings - Fork 7
/
core.py
executable file
·163 lines (134 loc) · 5.32 KB
/
core.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
#!/usr/bin/env python3
import os
import time
import subprocess
import json
# Change the PWD to this locationself.wfile.write(
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
# Variables (Constants)
DEFAULT_VIDEO = 'totem'
DEFAULT_MUSIC = 'rhythmbox'
APPS = ('totem', 'vlc', 'xbmc', 'rhythmbox')
def is_json(myjson):
try:
json_object = json.loads(myjson)
except ValueError as e:
return False
return True
# Kill all running aps
def killApps():
# Loop through them and kill them as we go along
for APP in APPS:
getPid = subprocess.getoutput("ps -e | grep " + APP + " | awk {'print $1'}")
#print "ps -e | grep " + APP + " | awk {'print $1'} , PID: " + getPid
# If there is a PID, then the application is running and we can kill it
if getPid != '':
subprocess.getoutput('kill ' + getPid)
print('Killing ' + APP + ", VIA: " + 'kill ' + getPid)
# This function will be used to confirm whether an application is running
def appRunning(APP):
# Get the PID, if it exists then the app is running
getPid = subprocess.getoutput("ps -e | grep " + APP + " | awk {'print $1'}")
# If there is a PID, then the application is indeed running
if getPid != '':
return False
else:
return True
# This will perform the actions
def commandKeys(pressed):
# Debound multiple movement command from the server
multiple_commands = pressed.find("}{")
if multiple_commands != -1:
print('[MULTIPLE COMMANDS] This may work, this may not work\r\n')
for command in pressed.replace('}{', '}\n{').split("\n"):
commandKeys(command)
return
# The dictionary with the commands and switches to be run
defaults = {
'up': ['xdotool', 'key', "Up"],
'down': ['xdotool', 'key', "Down"],
'left': ['xdotool', 'key', "Left"],
'right': ['xdotool', 'key', "Right"],
'ok': ['xdotool', 'key', "KP_Enter"],
'play': ['xdotool', 'key', "XF86AudioPlay"],
'info': ['xdotool', 'key', 'Menu'],
'back': ['xdotool', 'key', "Escape"],
'next': ['xdotool', 'key', "XF86AudioNext"],
'prev': ['xdotool', 'key', "XF86AudioPrev"],
'video': [DEFAULT_VIDEO],
'stop': ['xdotool', 'key', "XF86AudioStop"],
'music': [DEFAULT_MUSIC],
'vol-up': ['xdotool', 'key', "XF86AudioRaiseVolume"],
'vol-down': ['xdotool', 'key', "XF86AudioLowerVolume"],
'click' : ['xdotool', 'click', "1"],
'rclick' : ['xdotool', 'click', "3"],
'clickclick' : ['xdotool', 'click', "1"],
}
# Did we receive a JSON string?
if is_json(pressed):
JSON = json.loads(pressed)
# Currently we are only concerned with moving the mouse
if JSON['action'] == "mouse-move":
# If the positions is None in the case of duplicate commands, just exit early
if not JSON['x']:
return
if not JSON['y']:
return
# If the positions is null in the case of duplicate commands, just exit early
if JSON['x'] == "null":
return
if JSON['y'] == "null":
return
# Handle negative values
if JSON['x'] < 0 or JSON['y'] < 0:
mouseCommand = [
'xdotool',
'mousemove_relative',
'--',
str(float(JSON['x'])),
str(float(JSON['y']))
]
else:
mouseCommand = [
'xdotool',
'mousemove_relative',
str(float(JSON['x'])),
str(float(JSON['y']))
]
# Dispatch the mouse move event
xdotool(mouseCommand)
# Was the keypress valid? If so, run the relevant command
elif pressed == "power":
killApps()
elif pressed == "fullscreen":
xdotool(defaults['click'], True)
xdotool(defaults['click'], True)
# The media launchers are a bit different, we need to check that the application is not already fired up
elif pressed == "video" or pressed == "music":
# Check if it's already running, if not kill all other players listed in apps and open this one
if appRunning(str(defaults[pressed])) == False:
#killApps()
appLaunch(defaults[pressed], True)
print("Launching " + str(defaults[pressed]))
else:
print(str(defaults[pressed]) + " is already running")
# Othewise lets check the action to be run, based on the key pressed
elif pressed in defaults:
print(defaults[pressed])
xdotool(defaults[pressed])
# Command not found sadly
else:
print(pressed, "command not found, or is missing in core")
# This little baby is what sends the command from the dictionary to the kernel to be processed.
def xdotool(action, surpress=False):
ps = subprocess.Popen(action)
if surpress == False:
print(action)
# Applications should be done a bit differently
def appLaunch(action, surpress=False):
FNULL = open('/dev/null', 'w')
ps = subprocess.Popen(action, shell=True, stderr=FNULL)
if surpress == False:
print(action)