Skip to content

Commit

Permalink
Handle input process closing without crashing (unplugged controller)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaburns committed Aug 15, 2021
1 parent dc8b2d0 commit 1ab102b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion input_reader_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def enqueue_output(exe_path):
proc = Popen([exe_path], shell=True, stdout=PIPE)
g_queue = Queue()
while not g_shouldStop:
g_queue.put(proc.stdout.readline().decode('utf-8'))
if proc.poll() is None:
g_queue.put(proc.stdout.readline().decode('utf-8'))
else:
g_shouldStop = True
proc.stdout.close()
proc.kill()
g_running = False
Expand Down Expand Up @@ -40,16 +43,25 @@ def sample_input_reader(mario_inputs):
has_line = True
line = g_queue.get()
if not has_line:
_sample_empty_inputs(mario_inputs)
return
vals = [int(x) for x in line.split()]
if len(vals) < 5:
_sample_empty_inputs(mario_inputs)
return
mario_inputs.stickX = _read_axis(float(vals[0]))
mario_inputs.stickY = _read_axis(float(vals[1]))
mario_inputs.buttonA = vals[2] != 0
mario_inputs.buttonB = vals[3] != 0
mario_inputs.buttonZ = vals[4] != 0

def _sample_empty_inputs(mario_inputs):
mario_inputs.stickX = 0.0
mario_inputs.stickY = 0.0
mario_inputs.buttonA = False
mario_inputs.buttonB = False
mario_inputs.buttonZ = False

def _read_axis(val):
val /= 32768.0
if val < 0.2 and val > -0.2:
Expand Down

0 comments on commit 1ab102b

Please sign in to comment.