-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyProcesses.py
More file actions
110 lines (98 loc) · 4 KB
/
myProcesses.py
File metadata and controls
110 lines (98 loc) · 4 KB
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
import psutil, os, re, subprocess
## Kill tasks
def killTasks(procnames):
try:
for proc in psutil.process_iter():
if proc.name() in procnames:
pid = str(proc.as_dict(attrs=['pid'])['pid'])
name = proc.as_dict(attrs=['name'])['name']
subprocess.call(['sudo', 'kill', '-15', pid])
# Kodi needs SIGKILL -9 to close
kodiproc = ['kodi', 'kodi.bin']
for proc in psutil.process_iter():
if proc.name() in kodiproc:
pid = str(proc.as_dict(attrs=['pid'])['pid'])
name = proc.as_dict(attrs=['name'])['name']
subprocess.call(['sudo', 'kill', '-9', pid])
except:
pass
## Get emulator path
def getEmulatorPath(console):
path = '/opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ ' + console + ' '
return path
## Get game path
def getGamePath(console, game):
# Escape the spaces and brackets in game filenames
game = game.replace(" ", "\ ")
game = game.replace("(", "\(")
game = game.replace(")", "\)")
game = game.replace("'", "\\'")
gamePath = '/home/pi/RetroPie/roms/' + console + '/' + game
return gamePath
def process_exists(proc_name):
try:
ps = subprocess.Popen('ps ax -o pid= -o args= ', shell=True, stdout=subprocess.PIPE)
ps_pid = ps.pid
output = ps.stdout.read()
ps.stdout.close()
ps.wait()
for line in output.decode().split('\n'):
res = re.findall("(\d+) (.*)", line)
if res:
pid = int(res[0][0])
if proc_name in res[0][1] and pid != os.getpid() and pid != ps_pid:
return True
return False
except:
return False
def process_id(proc_name):
try:
ps = subprocess.Popen('ps ax -o pid= -o args= ', shell=True, stdout=subprocess.PIPE)
ps_pid = ps.pid
output = ps.stdout.read()
ps.stdout.close()
ps.wait()
for line in output.decode().split('\n'):
res = re.findall("(\d+) (.*)", line)
if res:
pid = int(res[0][0])
if proc_name in res[0][1] and pid != os.getpid() and pid != ps_pid:
return pid
return 0
except:
return 0
## Run game
def runGame(console, game, source):
try:
# Update status
f = open('/home/pi/scripts/myControl/myConfigs/status.conf', 'w')
f.seek(0)
f.truncate()
f.seek(0)
f.write(source)
f.close()
emulationstationRunning = process_exists('emulationstation')
procnames = ["retroarch", "ags", "uae4all2", "uae4arm", "capricerpi", "linapple", "hatari", "stella",
"atari800", "xroar", "vice", "daphne", "reicast", "pifba", "osmose", "gpsp", "jzintv",
"basiliskll", "mame", "advmame", "dgen", "openmsx", "mupen64plus", "gngeo", "dosbox", "ppsspp",
"simcoupe", "scummvm", "snes9x", "pisnes", "frotz", "fbzx", "fuse", "gemro", "cgenesis", "zdoom",
"eduke32", "lincity", "love", "alephone", "micropolis", "openbor", "openttd", "opentyrian",
"cannonball", "tyrquake", "ioquake3", "residualvm", "xrick", "sdlpop", "uqm", "stratagus",
"wolf4sdl", "solarus", "emulationstation"]
killTasks(procnames)
pid = os.fork()
if not pid:
try:
if ((emulationstationRunning == False and source == '') or console == ''):
subprocess.call('emulationstation', shell=True)
else:
subprocess.call(getEmulatorPath(console) + getGamePath(console,game), shell=True)
except:
pass
os._exit(0)
else:
response = {'type':'success', 'data':'', 'message':'Successfully started game.'}
return response
except:
print ('Error, failed to start game.')
return {'type':'error', 'data':'', 'message':'Failed to start game.'}