-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.py
More file actions
136 lines (122 loc) · 4.24 KB
/
Copy pathbackground.py
File metadata and controls
136 lines (122 loc) · 4.24 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
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
# Copyright (c) 2025 ComputerL (@1ComputerL)
# Licensed under the MIT License. See LICENSE file in the project root for full license information.
# This program is the starting point of the MusicPi system
# It should be run at startup
# It runs in the background and listens for a specific key press to start the music_system.py program
# Also, it starts a program to turn output 'DSI-1' on and off based on other specific key presses
# Created by ComputerL
### IMPORTS ###
import sys
import os
import subprocess
import logging
import evdevlib
import time
import signal
import os
### PATH SETUP ###
# get the parent directory of this file
parentdir = os.path.dirname(os.path.abspath(__file__))
# add the MusicPi directory to the system path
sys.path.append(parentdir)
# delay
time.sleep(3)
### LOGGING SETUP ###
# create new logger
log = logging.getLogger('my_logger')
# configure the logger
logging.basicConfig(filename=parentdir+'/musicpilog.log', level=logging.INFO, format='%(asctime)s - %(message)s')
### INITIATE THINGS ###
# global variables to track state of power button
power_button_state = False
prev_power_button_state = False
# set up the systemprogram variable
systemprogram = None
# get the state of the screen
result=os.system(" xrandr | grep -q \" connected [0-9]\"")
# if the result is 0, the screen is (open)
if result == 0:
# set the screen open state to True
screenOpenState = True
else:
# set the screen open state to False (closed)
screenOpenState = False
# set the previous screen open state to the current state
screenOpenState_prev = screenOpenState
# function to handle stop signals
def handle_stop_signal(signum, frame):
# terminate subprocess
try:
systemprogram.terminate()
except:
pass
# logging
log.info("BackgroundProgram: received stop signal; terminated subprocesses and now exiting;")
# exit the program
sys.exit(0)
# register the signal handler for SIGTERM
signal.signal(signal.SIGTERM, handle_stop_signal)
# register the signal handler for SIGTSTP
signal.signal(signal.SIGTSTP, handle_stop_signal)
# function to handle f9 key press events
def on_press(key):
# sanitize the key input
key=str(key)
# enable editing of these global variables
global power_button_state, screenOpenState
# if the key pressed is f9
if key == "Key.f9":
# toggle the power button state
power_button_state = not power_button_state
# if the key pressed is f7
elif key == "Key.f12":
# toggle the screen open state
screenOpenState = not screenOpenState
# create listener to monitor key presses
key_listener = evdevlib.Listener(on_press=on_press)
# start the listener
key_listener.start()
# logging
log.info("BackgroundProgram: background.py started; initiated things (log, stop signal handler, key listener);")
### MAIN LOOP ###
while True:
# if the power button was just now turned off
if power_button_state == False and prev_power_button_state == True:
# teminate the system.py main program
if systemprogram is not None:
try:
systemprogram.terminate()
except:
pass
# update the variable
prev_power_button_state = False
# logging
log.info('BackgroundProgram: terminated music_system.py main program;')
# if the screen was just now opened
if screenOpenState == False and screenOpenState_prev == True:
# turn on the screen
os.system("wlr-randr --output DSI-1 --on")
# update the variable
screenOpenState_prev = False
# if the power button was just now turned on
if power_button_state == True and prev_power_button_state == False:
# open system.py main program as subprocess
systemprogram = subprocess.Popen(
['/bin/python', parentdir+ '/music_system.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# update the variable
prev_power_button_state = True
# logging
log.info('BackgroundProgram: opened music_system.py main program as subprocess;')
# if the screen was just now closed
if screenOpenState == True and screenOpenState_prev == False:
# turn off the screen
os.system("wlr-randr --output DSI-1 --off")
# update the variable
screenOpenState_prev = True
# delay
time.sleep(.05)