-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackboard
executable file
·247 lines (194 loc) · 9.72 KB
/
backboard
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
# backboard
# Copyright (c) 2022 Nicholas Girga
# Creates a simple background theme for the Home page on the Steam Deck with CSS Loader
# The current version of this app.
APP_VERSION = "v1.0"
# Imports
import gi, os, shutil, subprocess, sys, webbrowser
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import Gdk, GdkPixbuf, Gio, Gtk
from pathlib import Path
# Define main class.
class Main:
# Allow usage of APP_VERSION variable
global APP_VERSION
# Initializes main class.
def __init__(self, **kwargs):
super().__init__(**kwargs)
# check for the needed dependencies
self.check_dependencies()
# create Gtk builder
self.builder = Gtk.Builder()
# define path to UI template
self.gladefile = "./backboard.ui"
# configure builder and connect signals
self.builder.add_from_file(self.gladefile)
self.builder.connect_signals(self)
# get main window object
self.window = self.builder.get_object("window")
# set the main window's icon
self.window.set_icon_from_file("./logo/backboard_256x.png")
# present main window
self.window.show_all()
# Called when the main window is destroyed.
def on_window_destroy(self, object, data = None):
Gtk.main_quit() # quit Gtk
# Returns true if a command is available on the system.
def command_exists(self, command: str) -> bool:
try:
result = not subprocess.run(["bash", "-c", "command -v " + command], capture_output=True).stdout.decode("UTF-8") == ""
except Exception as e:
print(str(e) + "\nERROR!: Bash could not determine if the specified command exists: \"" + command + "\".", ' ', '\n', sys.stderr)
sys.exit(-2) # could not determine if command exists; exit with code -2
return result
# Ensures all required dependencies are installed
def check_dependencies(self):
# check for base64
if not self.command_exists("base64"):
error_message = "Fatal Error: The \"base64\" command is not accessible! Cannot continue."
print(error_message, ' ', '\n', file = sys.stderr)
self.create_simple_message_dialog(error_message, "Backboard - Fatal Error")
sys.exit(1) # could not find base64 command; exit with code 1
# check for Decky
if not os.path.isdir(os.path.expanduser("~") + "/homebrew"):
print("Fatal Error: Decky does not appear to be installed! Please install it before continuing.", ' ', '\n', file = sys.stderr)
self.create_simple_message_dialog("Fatal Error: Decky does not appear to be installed! Please <a href=\"https://github.com/SteamDeckHomebrew/decky-loader#installation\">install it</a> before continuing.", "Backboard - Fatal Error", True)
sys.exit(2) # could not find Decky directory; exit with code 2
# check for CSS Loader
if not os.path.isdir(os.path.expanduser("~") + "/homebrew/plugins/SDH-CssLoader"):
print("Fatal Error: CSS Loader does not appear to be installed! Please install it before continuing.", ' ', '\n', file = sys.stderr)
self.create_simple_message_dialog("Fatal Error: CSS Loader does not appear to be installed! Please <a href=\"https://github.com/suchmememanyskill/SDH-CssLoader#installation\">install it</a> before continuing.", "Backboard - Fatal Error", True)
sys.exit(3) # could not find CSS Loader directory; exit with code 3
# Will create a simple message dialog.
def create_simple_message_dialog(self, text: str, title = "Backboard", markup = False, yesno = False, window = None):
# create dialog and set a few settings
message = Gtk.MessageDialog(parent = window, flags = 0, message_type = Gtk.MessageType.INFO, buttons = Gtk.ButtonsType.YES_NO if yesno else Gtk.ButtonsType.OK, text = text)
message.set_title(title)
if markup:
message.set_markup(text)
message.set_keep_above(True)
message.props.urgency_hint = True
# set window icon
message.set_icon_from_file("./logo/backboard_256x.png")
# run dialog, destory it when it exits, and return the response
response = message.run()
message.destroy()
return response
# Called when the about button is clicked
def on_about_button_clicked(self, object, data = None):
# create about dialog
about = Gtk.AboutDialog()
# set app information in about dialog
about.set_program_name("Backboard")
about.set_version(APP_VERSION)
about.set_property("copyright", "Copyright ©️ 2022 Nicholas Girga")
about.set_property("authors", ["Nicholas Girga"])
about.set_comments("Create basic background themes for the Steam Deck")
about.set_website("https://gitlab.com/nickgirga/steam-deck-backboard")
about.set_website_label("gitlab.com/nickgirga/steam-deck-backboard")
# set license in about dialog
about.set_license_type(Gtk.License.MIT_X11)
# set logo in about dialog
pixbuf = GdkPixbuf.Pixbuf.new_from_file("./logo/backboard_128x.png")
about.set_logo(pixbuf)
# set icon in about dialog
about.set_icon_from_file("./logo/backboard_256x.png")
# run and destroy about dialog
about.run()
about.destroy()
# Called when the help button is clicked.
def on_help_button_clicked(self, object, data = None):
webbrowser.open_new_tab("https://gitlab.com/nickgirga/steam-deck-backboard/-/wikis/Home") # open project Wiki in new tab of browser
# Called when the create button is clicked
def on_create_button_clicked(self, object, data = None):
# get theme data from widgets
theme_name = self.builder.get_object("theme_name").get_text()
author = self.builder.get_object("author").get_text()
version = self.builder.get_object("version").get_text()
image = self.builder.get_object("image").get_filename()
# fill theme_name if empty
if theme_name == "":
theme_name = "My Theme"
# fill author if empty
if author == "":
author = "Me"
# fill version if empty
if version == "":
version = "v1.0"
# warn user if image is empty
if image == "" or image == None:
warning_message = "You must select an image before creating the theme!"
print(warning_message)
self.create_simple_message_dialog(warning_message, "Backboard - Warning")
return
# create themes directory if needed
themes_directory = os.path.expanduser("~") + "/homebrew/themes"
Path(themes_directory).mkdir(exist_ok=True)
# check for duplicate theme names
if os.path.isdir(themes_directory + "/" + theme_name):
warning_message = "A theme with the chosen name already exists! Please choose another name or remove the existing theme."
print(warning_message)
self.create_simple_message_dialog(warning_message, "Backboard - Warning")
return
# copy template theme
new_theme = themes_directory + "/" + theme_name
shutil.copytree("./Steam-Deck-Themes/static-background", new_theme)
# encode image with base64
image_base64 = subprocess.run(["base64", "--wrap=0", image], capture_output=True).stdout.decode("UTF-8")
# update template theme image
css_file = new_theme + "/shared.css"
lines = []
with open(css_file, "r") as file:
lines = file.readlines()
with open(css_file, "w") as file:
line_count = 0
for line in lines:
if line_count == 1:
if image[-4:] == ".png":
# if PNG
file.write(" --static-background: url(data:image/png;base64" + image_base64 + ")\n")
else:
# if JPG/JPEG
file.write(" --static-background: url(data:image/jpeg;base64," + image_base64 + ")\n")
line_count += 1
continue
file.write(line)
line_count += 1
# update template theme data
theme_data_file = new_theme + "/theme.json"
lines = []
with open(theme_data_file, "r") as file:
lines = file.readlines()
with open(theme_data_file, "w") as file:
line_count = 0
for line in lines:
if line_count == 1:
file.write(" \"name\": \"" + theme_name + "\",\n")
line_count += 1
continue
if line_count == 2:
file.write(" \"version\": \"" + version + "\",\n")
line_count += 1
continue
if line_count == 3:
file.write(" \"author\": \"" + author + "\",\n")
line_count += 1
continue
file.write(line)
line_count += 1
# inform user of completion
self.create_simple_message_dialog("Your theme has been created! Go back into Gaming Mode and enable it through CSS Loader to use it! You may need to press \"Reload themes\" in order for it to show up.")
# Start.
if __name__ == "__main__":
try:
main = Main() # create new instance of main class
Gtk.main() # Initialize Gtk
except KeyboardInterrupt:
print("Quitting (user request)...")
sys.exit(0) # exit on KeyboardInterrupt
except Exception as e:
print(str(e))
sys.exit(-1) # main instance interrupted; exit with code -1