Restore pynput solution. Thuis requires an x-server while running

This commit is contained in:
Leone, Mark A [LGS] 2025-07-04 22:33:48 -04:00
parent 853be109e7
commit ce88168e2e

View file

@ -15,6 +15,7 @@ from monitors import CPUMonitor, MemoryMonitor, BatteryMonitor, DiskMonitor, Net
# External Dependencies
import numpy as np
from pynput.keyboard import Key, Listener
import evdev
from serial.tools import list_ports
@ -112,6 +113,27 @@ def main(args):
"snap": draw_snap,
"none": lambda *x: x # noop
}
def on_press(key):
global alt_pressed
global i_pressed
if type(key).__name__ == 'KeyCode':
if key.char == 'i':
i_pressed = True
elif key == Key.alt:
alt_pressed = True
def on_release(key):
global alt_pressed
global i_pressed
if type(key).__name__ == 'KeyCode':
if key.char == 'i':
i_pressed = False
elif key == Key.alt:
alt_pressed = False
if key == Key.esc:
# Stop listener
return False
#################################################
### Load app functions from plugins ###
@ -133,78 +155,79 @@ def main(args):
if args.snapshot_duration > args.snapshot_interval:
print("Snapshot duration must be less than snapshot interval. Exiting...")
sys.exit(0)
start_time = time.time()
while True:
elapsed_time = time.time()
show_snapshot = True if args.snapshot_interval == 0 or elapsed_time % args.snapshot_interval <= args.snapshot_duration else False
try:
screen_brightness = get_monitor_brightness()
background_value = int(screen_brightness * (max_background_brightness - min_background_brightness) + min_background_brightness)
foreground_value = int(screen_brightness * (max_foreground_brightness - min_foreground_brightness) + min_foreground_brightness)
grid = np.zeros((9,34), dtype = int)
active_keys = device.active_keys(verbose=True)
if (MODIFIER_KEYS[0] in active_keys or MODIFIER_KEYS[1] in active_keys) and KEY_I in active_keys and not args.no_key_listener:
if args.left_snap and show_snapshot:
draw_id(grid, "snap", foreground_value)
else:
draw_outline_border(grid, background_value)
draw_ids(grid, args.top_left, args.bottom_left, foreground_value)
left_drawing_queue.put(grid)
with Listener(
on_press=on_press,
on_release=on_release):
while True:
elapsed_time = time.time()
show_snapshot = True if args.snapshot_interval == 0 or elapsed_time % args.snapshot_interval <= args.snapshot_duration else False
try:
screen_brightness = get_monitor_brightness()
background_value = int(screen_brightness * (max_background_brightness - min_background_brightness) + min_background_brightness)
foreground_value = int(screen_brightness * (max_foreground_brightness - min_foreground_brightness) + min_foreground_brightness)
grid = np.zeros((9,34), dtype = int)
if args.right_snap and show_snapshot:
draw_id(grid, "snap", foreground_value)
else:
draw_outline_border(grid, background_value)
draw_ids(grid, args.top_right, args.bottom_right, foreground_value)
right_drawing_queue.put(grid)
grid = np.zeros((9,34), dtype = int)
time.sleep(0.1)
continue
if i_pressed and alt_pressed and not args.no_key_listener:
if args.left_snap and show_snapshot:
draw_id(grid, "snap", foreground_value)
else:
draw_outline_border(grid, background_value)
draw_ids(grid, args.top_left, args.bottom_left, foreground_value)
left_drawing_queue.put(grid)
grid = np.zeros((9,34), dtype = int)
if args.right_snap and show_snapshot:
draw_id(grid, "snap", foreground_value)
else:
draw_outline_border(grid, background_value)
draw_ids(grid, args.top_right, args.bottom_right, foreground_value)
right_drawing_queue.put(grid)
grid = np.zeros((9,34), dtype = int)
time.sleep(0.1)
continue
# Draw by half or whole panel, depending on program args
for i, draw_queue in enumerate(drawing_queues):
grid = np.zeros((9,34), dtype = int)
if i == 0:
panel = 'left'
if args.left_snap is not None and show_snapshot:
app_functions["snap"](grid, foreground_value, args.left_snap, args.snapshot_path, 'left')
draw_queue.put(grid)
continue
# Draw by half or whole panel, depending on program args
for i, draw_queue in enumerate(drawing_queues):
grid = np.zeros((9,34), dtype = int)
if i == 0:
panel = 'left'
if args.left_snap is not None and show_snapshot:
app_functions["snap"](grid, foreground_value, args.left_snap, args.snapshot_path, 'left')
draw_queue.put(grid)
continue
else:
_args = [args.top_left, args.bottom_left]
else:
_args = [args.top_left, args.bottom_left]
else:
panel = 'right'
if args.right_snap is not None and show_snapshot:
app_functions["snap"](grid, foreground_value, args.right_snap, args.snapshot_path, 'right')
draw_queue.put(grid)
continue
_args = [args.top_right, args.bottom_right]
for j, arg in enumerate(_args):
if j == 0:
idx = 0
loc = 'top'
else:
idx = 16
loc = 'bottom'
try:
func = app_functions[arg]
func(arg, grid, foreground_value, idx)
except KeyError:
print(f"Unrecognized display option {arg} for {loc} {panel}")
if arg == 'mem-bat': arg = 'mem' # Single border draw for mem and bat together
draw_app_border(arg, grid, background_value, idx)
draw_queue.put(grid)
except KeyboardInterrupt:
break
except Exception as e:
import traceback
print(f"Error in main loop: {e}")
traceback.print_exc()
time.sleep(1.0)
time.sleep(0.1)
print("Exiting")
panel = 'right'
if args.right_snap is not None and show_snapshot:
app_functions["snap"](grid, foreground_value, args.right_snap, args.snapshot_path, 'right')
draw_queue.put(grid)
continue
_args = [args.top_right, args.bottom_right]
for j, arg in enumerate(_args):
if j == 0:
idx = 0
loc = 'top'
else:
idx = 16
loc = 'bottom'
try:
func = app_functions[arg]
func(arg, grid, foreground_value, idx)
except KeyError:
print(f"Unrecognized display option {arg} for {loc} {panel}")
if arg == 'mem-bat': arg = 'mem' # Single border draw for mem and bat together
draw_app_border(arg, grid, background_value, idx)
draw_queue.put(grid)
except KeyboardInterrupt:
break
except Exception as e:
import traceback
print(f"Error in main loop: {e}")
traceback.print_exc()
time.sleep(1.0)
time.sleep(0.1)
print("Exiting")
if __name__ == "__main__":
app_names = ["cpu", "net", "disk", "mem-bat", "none"]