-
Notifications
You must be signed in to change notification settings - Fork 0
/
splashscreen-util.c
355 lines (291 loc) · 9.02 KB
/
splashscreen-util.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* splashscreen-util.c
*
* Copyright (C) 2021 Ivaylo Dimitrov <[email protected]>
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <gconf/gconf-client.h>
#include <canberra.h>
#include <systemui.h>
#include <getopt.h>
#include <unistd.h>
#include <syslog.h>
#include "splashscreen-dbus-names.h"
static GtkWidget *window = NULL;
static GtkWidget *image = NULL;
static gchar *bootup_image_filename = NULL;
static gchar *shutdown_image_filename = NULL;
static gchar *shutdown_sound_filename = NULL;
static ca_context *c = NULL;
static GdkGeometry geometry = {};
#define CRITICAL(msg, ...) \
syslog(LOG_MAKEPRI(LOG_USER, LOG_CRIT), msg "\n", ##__VA_ARGS__)
#define WARN(msg, ...) \
syslog(LOG_MAKEPRI(LOG_USER, LOG_WARNING), msg "\n", ##__VA_ARGS__)
#define INFO(msg, ...) \
syslog(LOG_MAKEPRI(LOG_USER, LOG_INFO), msg "\n", ##__VA_ARGS__)
static GdkPixbuf *
_pixbuf_rotate_if_needed(GdkPixbuf *pixbuf)
{
GdkPixbuf *rotated;
if (!GDK_IS_PIXBUF(pixbuf))
{
WARN("not a valid pixbuf for rotation");
return pixbuf;
}
if (gdk_screen_width() >= gdk_screen_height())
return pixbuf;
rotated = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
g_object_unref(pixbuf);
return rotated;
}
static GdkPixbuf *
_load_image(const char *filename)
{
GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
GError *error = NULL;
GdkPixbuf *pixbuf = NULL;
g_assert(filename != NULL);
if (g_strrstr(filename, "/"))
{
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if (error)
{
CRITICAL("gdk_pixbuf_new_from_file failed %s", error->message);
g_error_free(error);
}
}
else
{
GtkIconInfo *info = gtk_icon_theme_lookup_icon(icon_theme, filename, 1,
GTK_ICON_LOOKUP_NO_SVG);
if (info)
{
gint size = gtk_icon_info_get_base_size(info);
if (size <= 0)
size = 38;
pixbuf = gtk_icon_theme_load_icon(icon_theme, filename, size,
GTK_ICON_LOOKUP_NO_SVG, &error);
gtk_icon_info_free(info);
if (error)
{
CRITICAL("failed to load icon %s", error->message);
g_error_free(error);
}
}
else
CRITICAL("failed to get icon info");
}
return pixbuf;
}
static void
_cleanup()
{
if (image)
{
gtk_widget_destroy(image);
image = NULL;
}
if (window)
{
gtk_widget_destroy(window);
window = NULL;
}
}
static void
_set_image(GtkWidget **image, const char *filename)
{
GdkPixbuf *pixbuf = _load_image(filename);
int h;
int w;
int width;
int height;
GdkPixbuf *scaled;
g_assert(pixbuf != NULL);
pixbuf = _pixbuf_rotate_if_needed(pixbuf);
width = gdk_pixbuf_get_width(pixbuf);
height = gdk_pixbuf_get_height(pixbuf);
w = geometry.max_width;
h = geometry.max_height;
if (width <= w && height <= h)
scaled = gdk_pixbuf_copy(pixbuf);
else
{
if (w * height > width * h)
w = width * h / height;
else
h = height * w / width;
scaled = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
}
if (!*image)
{
*image = gtk_image_new_from_pixbuf(scaled);
if (*image)
{
gtk_widget_show(*image);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(*image));
}
else
CRITICAL("gtk_image_new_from_pixbuf failed");
}
else
gtk_image_set_from_pixbuf(GTK_IMAGE(*image), scaled);
if (scaled)
g_object_unref(scaled);
if (pixbuf)
g_object_unref(pixbuf);
}
int
main(int argc, char **argv)
{
GConfClient *gc_client;
int option_index = 0;
static int sound = TRUE;
static int no_window = TRUE;
static int type = SPLASHSCREEN_ENABLE_SHUTDOWN;
static const struct option longopts[] =
{
{"sound", no_argument, &sound, TRUE},
{"no-sound", no_argument, &sound, FALSE},
{"shutdown", no_argument, &type, SPLASHSCREEN_ENABLE_SHUTDOWN},
{"bootup", no_argument, &type, SPLASHSCREEN_ENABLE_BOOTUP},
{"window", no_argument, &no_window, FALSE},
{"no-window", no_argument, &no_window, TRUE},
{0, 0, 0, 0}
};
openlog("splashscreen-util", LOG_PID | LOG_NDELAY , LOG_USER);
while (getopt_long(argc, argv, "", longopts, &option_index) != -1)
;
INFO("type '%d' (%s), sound '%d' (%s), logo type '%d' (%s)",
type, type != SPLASHSCREEN_ENABLE_SHUTDOWN ? "BOOTUP" : "SHUTDOWN",
sound, sound ? "YES" : "NO",
no_window, no_window ? "BACKGROUND" : "WINDOW");
gtk_init(&argc, &argv);
gc_client = gconf_client_get_default();
g_return_val_if_fail(gc_client != NULL, TRUE);
bootup_image_filename = gconf_client_get_string(
gc_client, "/system/systemui/splash/bootup_image", NULL);
shutdown_image_filename = gconf_client_get_string(
gc_client, "/system/systemui/splash/shutdown_image", NULL);
shutdown_sound_filename = gconf_client_get_string(
gc_client, "/system/systemui/splash/shutdown_soundfilename", NULL);
/* WTF?!? */
if (!bootup_image_filename)
bootup_image_filename = g_strdup("/tmp/foo.gif");
if (!shutdown_image_filename)
shutdown_image_filename = g_strdup("/tmp/bar.gif");
if (!shutdown_sound_filename)
shutdown_sound_filename = g_strdup("/usr/share/sounds/ui-shutdown.wav");
if (no_window)
{
GdkPixmap *drawable = NULL;
Display *dpy = GDK_DISPLAY();
Window root_window = GDK_ROOT_WINDOW();
GdkPixbuf *pixbuf;
INFO("setting root window background to Nokia logo");
pixbuf = _pixbuf_rotate_if_needed(_load_image(shutdown_image_filename));
if (pixbuf)
{
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &drawable, NULL, 255);
g_object_unref(pixbuf);
}
if (drawable)
{
gdk_error_trap_push();
XSetWindowBackgroundPixmap(dpy, root_window,
gdk_x11_drawable_get_xid(drawable));
XClearWindow(dpy, root_window);
XFlush(dpy);
if (gdk_error_trap_pop())
CRITICAL("failed to set root window logo background");
g_object_unref(drawable);
INFO("root window bg set to show Nokia logo");
}
else
CRITICAL("failed to create logo pixmap");
}
else
{
if (!window)
{
GdkColor color = {0, 0xFFFF, 0xFFFF, 0xFFFF};
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "splash");
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
gtk_window_fullscreen(GTK_WINDOW(window));
gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DND);
gtk_widget_add_events(GTK_WIDGET(window), GDK_EXPOSURE_MASK);
gtk_window_set_keep_above(GTK_WINDOW(window), TRUE);
gtk_widget_realize(window);
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
geometry.min_width = gdk_screen_get_width(
gdk_drawable_get_screen(GDK_DRAWABLE(GTK_WIDGET(window)->window)));
geometry.max_width = geometry.min_width;
geometry.min_height = gdk_screen_get_height(
gdk_drawable_get_screen(GDK_DRAWABLE(GTK_WIDGET(window)->window)));
geometry.max_height = geometry.min_height;
gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &geometry,
GDK_HINT_MAX_SIZE | GDK_HINT_MIN_SIZE);
if (type == SPLASHSCREEN_ENABLE_BOOTUP)
_set_image(&image, bootup_image_filename);
else if (type == SPLASHSCREEN_ENABLE_SHUTDOWN)
_set_image(&image, shutdown_image_filename);
else
{
CRITICAL("SPLASH: NOT BOOTUP; NOT SHUTDOWN; ERROR!");
_cleanup();
}
if (type == SPLASHSCREEN_ENABLE_BOOTUP ||
type == SPLASHSCREEN_ENABLE_SHUTDOWN)
{
gtk_widget_show_all(window);
while (gtk_events_pending())
gtk_main_iteration();
}
}
else
CRITICAL("already visible, ignore");
}
if (sound)
{
int res = ca_context_create(&c);
if (res)
CRITICAL("ca_context_create: %s", ca_strerror(res));
if (c)
{
res = ca_context_play(c, 0,
"media.filename", shutdown_sound_filename,
"media.name", "Shutdown notification",
NULL);
INFO("ca_context_play: %s", ca_strerror(res));
}
}
gtk_main();
CRITICAL("this line should never be reached");
_cleanup();
if (c)
{
ca_context_destroy(c);
c = NULL;
}
g_object_unref(gc_client);
g_free(bootup_image_filename);
g_free(shutdown_image_filename);
g_free(shutdown_sound_filename);
closelog();
return 0;
}