-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
182 lines (160 loc) · 6.5 KB
/
menu.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import bakebit_128_64_oled as oled
from PIL import Image, ImageFont, ImageDraw
import time
import signal
import os
import subprocess
# Définition des dimensions de l'écran
width = 128
height = 64
bash_path = f'{os.getcwd()}/'
#f"{bash_path}"
# Initialise un nouveau groupe de processus
os.setpgrp()
# Initialisation de l'écran OLED
oled.init()
oled.setNormalDisplay()
oled.setHorizontalMode()
font14 = ImageFont.truetype('DejaVuSansMono.ttf', 14)
# Création de l'image et du contexte de dessin
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
# Définition des options avec leurs images et commandes associées
options = [
{"image": f"{bash_path}menu_palera1n.png", "command": f"sudo python3 {bash_path}palera1n.py"},
{"image": f"{bash_path}menu_checkra1n.png", "command": f"sudo python3 {bash_path}checkra1n.py"},
{"image": f"{bash_path}menu_reboot.png", "command": "sudo shutdown -r now"}
]
current_option_index = 0
in_reboot_confirmation = False
reboot_confirmation_option = "YES" # Commence par "YES"
startTime = time.time()
outTime = 30
def display_image():
image_path = options[current_option_index]['image']
image = Image.open(image_path).convert('1')
if current_option_index==len(options) - 1:
draw=ImageDraw.Draw(image)
tempI = int(open('/sys/class/thermal/thermal_zone0/temp').read())
if tempI > 1000:
tempI = tempI / 1000
tempVal = str(round(tempI, 1)) + '°C'
arch = subprocess.check_output('arch', shell=True)
arch = str(arch.decode('UTF-8'))
draw.text((76, 0), arch, font=font14, fill=255)
draw.text((76, 21), tempVal, font=font14, fill=255)
oled.drawImage(image)
def display_reboot_confirmation():
global reboot_confirmation_option
clear_screen() # Fonction pour effacer l'écran
draw.text((10, 0), "Reboot?", font=font14, fill=255)
# Affiche les options avec un curseur
if reboot_confirmation_option == "YES":
draw.rectangle((10, 20, 50, 35), outline=255, fill=255) # Rectangle blanc pour "YES"
draw.text((10, 20), "YES", font=font14, fill=0) # Texte en noir
draw.text((60, 20), "NO", font=font14, fill=255) # Texte en blanc pour "NO"
else:
draw.rectangle((60, 20, 100, 35), outline=255, fill=255) # Rectangle blanc pour "NO"
draw.text((10, 20), "YES", font=font14, fill=255) # Texte en blanc pour "YES"
draw.text((60, 20), "NO", font=font14, fill=0) # Texte en noir
cmd = "hostname -I | cut -d\' \' -f1"
IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
if len(IP) < 2:
IP = ' no network'
else:
IP="IP:" + IP
draw.text((0, 42), IP, font=font14, fill=255) # Texte en blanc pour "YES"
oled.drawImage(image)
def execute_option():
global in_reboot_confirmation, reboot_confirmation_option
command = options[current_option_index]['command']
if current_option_index == len(options) - 1: # L'index de l'option "Reboot"
in_reboot_confirmation = True
reboot_confirmation_option = "YES"
display_reboot_confirmation()
elif command:
subprocess.Popen(command, shell=True, preexec_fn=os.setsid)
os.killpg(0, signal.SIGTERM) # Tue tous les processus dans le groupe actuel
def navigate_options(signum, stack):
global current_option_index,startTime
if signum == signal.SIGUSR1: # Bouton 1: navigation vers le haut
if (time.time() - startTime) > outTime:
startTime = time.time()
else:
startTime = time.time()
current_option_index = (current_option_index - 1) % len(options)
elif signum == signal.SIGUSR2: # Bouton 2: navigation vers le bas
if (time.time() - startTime) > outTime:
startTime = time.time()
else:
startTime = time.time()
current_option_index = (current_option_index + 1) % len(options)
display_image()
def validate_option(signum, stack):
global startTime
if time.time()-startTime > outTime:
startTime = time.time()
display_image()
else:
startTime = time.time()
execute_option()
def navigate_reboot_confirmation(signum, stack):
global reboot_confirmation_option
if signum == signal.SIGUSR1: # Bouton 1: sélectionner "YES"
reboot_confirmation_option = "YES"
elif signum == signal.SIGUSR2: # Bouton 2: sélectionner "NO"
reboot_confirmation_option = "NO"
display_reboot_confirmation()
def validate_reboot_confirmation(signum, stack):
global in_reboot_confirmation
if reboot_confirmation_option == "YES":
clear_screen()
# Centre le message "Rebooting !" sur l'écran
text = "Rebooting !"
w, h = draw.textsize(text, font=font14)
x = (width - w) // 2
y = (height - h) // 2
draw.text((x, y), text, font=font14, fill=255)
oled.drawImage(image)
time.sleep(3) # Affiche le message pendant 3 secondes
subprocess.Popen("sudo shutdown -r now", shell=True)
else:
in_reboot_confirmation = False
display_image() # Revenir à l'affichage normal
def clear_screen():
global draw, image
draw.rectangle((0, 0, width, height), outline=0, fill=0)
oled.drawImage(image)
def get_device_state():
appledevice = subprocess.check_output('lsusb | grep "Apple"; exit 0', shell=True)
if not appledevice:
return False
else:
return True
if __name__ == "__main__":
signal.signal(signal.SIGUSR1, navigate_options)
signal.signal(signal.SIGUSR2, navigate_options)
signal.signal(signal.SIGALRM, validate_option)
display_image() # Affiche l'image de bienvenue au démarrage
startTime = time.time()
Close_EN=True
while True:
if (time.time() - startTime) > outTime:
if get_device_state():
print('iphone')
startTime = time.time()
display_image()
elif Close_EN:
oled.clearDisplay()
Close_EN=False
elif in_reboot_confirmation:
signal.signal(signal.SIGUSR1, navigate_reboot_confirmation)
signal.signal(signal.SIGUSR2, navigate_reboot_confirmation)
signal.signal(signal.SIGALRM, validate_reboot_confirmation)
Close_EN=True
else:
signal.signal(signal.SIGUSR1, navigate_options)
signal.signal(signal.SIGUSR2, navigate_options)
signal.signal(signal.SIGALRM, validate_option)
Close_EN=True
#time.sleep(0.2)