Embed default icon, in case it is not found on the system (e.g. tint2 not installed, just compiled)

This commit is contained in:
o9000 2017-12-29 16:10:58 +01:00
parent b4610fcb6e
commit dd1fd28114
7 changed files with 102 additions and 14 deletions

View file

@ -134,6 +134,7 @@ set( SOURCES src/config.c
src/util/tracing.c
src/mouse_actions.c
src/drag_and_drop.c
src/default_icon.c
src/clock/clock.c
src/systray/systraybar.c
src/launcher/launcher.c

5
src/default_icon.c Normal file

File diff suppressed because one or more lines are too long

10
src/default_icon.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef default_icon_h
#define default_icon_h
#include <Imlib2.h>
extern int default_icon_width;
extern int default_icon_height;
extern DATA32 default_icon_data[];
#endif

View file

@ -12,6 +12,7 @@
#include <X11/extensions/XShm.h>
#include "config.h"
#include "default_icon.h"
#include "drag_and_drop.h"
#include "fps_distribution.h"
#include "panel.h"
@ -55,6 +56,9 @@ void handle_cli_arguments(int argc, char **argv)
} else if (strcmp(argv[i], "--test-verbose") == 0) {
run_all_tests(true);
exit(0);
} else if (strcmp(argv[i], "--dump-image-data") == 0) {
dump_image_data(argv[i+1], argv[i+2]);
exit(0);
} else if (strcmp(argv[i], "-c") == 0) {
if (i + 1 < argc) {
i++;
@ -165,6 +169,22 @@ void create_default_elements()
default_panel();
}
void load_default_task_icon()
{
const gchar *const *data_dirs = g_get_system_data_dirs();
for (int i = 0; data_dirs[i] != NULL; i++) {
gchar *path = g_build_filename(data_dirs[i], "tint2", "default_icon.png", NULL);
if (g_file_test(path, G_FILE_TEST_EXISTS))
default_icon = load_image(path, TRUE);
g_free(path);
}
if (!default_icon) {
default_icon = imlib_create_image_using_data(default_icon_width,
default_icon_height,
default_icon_data);
}
}
void init_post_config()
{
server_init_visual();
@ -175,20 +195,7 @@ void init_post_config()
imlib_context_set_colormap(server.colormap);
init_signals_postconfig();
// load default icon
const gchar *const *data_dirs = g_get_system_data_dirs();
for (int i = 0; data_dirs[i] != NULL; i++) {
gchar *path = g_build_filename(data_dirs[i], "tint2", "default_icon.png", NULL);
if (g_file_test(path, G_FILE_TEST_EXISTS))
default_icon = load_image(path, TRUE);
g_free(path);
}
if (!default_icon) {
fprintf(stderr,
RED "Could not load default_icon.png. Please check that tint2 has been installed correctly!" RESET
"\n");
}
load_default_task_icon();
XSync(server.display, False);
}

View file

@ -1114,3 +1114,63 @@ void adjust_color(Color *color, int alpha, int saturation, int brightness)
color->rgb[1] = g / 255.;
color->rgb[2] = b / 255.;
}
void dump_image_data(const char *file_name, const char *name)
{
Imlib_Image image = load_image(file_name, false);
if (!image) {
fprintf(stderr, "tint2: Could not load image from file\n");
return;
}
gchar *header_name = g_strdup_printf("%s.h", name);
gchar *guard = g_strdup_printf("%s_h", name);
FILE *header = fopen(header_name, "wt");
fprintf(header,
"#ifndef %s\n"
"#define %s\n"
"\n"
"#include <Imlib2.h>\n"
"\n"
"extern int %s_width;\n"
"extern int %s_height;\n"
"extern DATA32 %s_data[];\n"
"\n"
"#endif\n",
guard,
guard,
name,
name,
name);
fclose(header);
g_free(guard);
g_free(header_name);
imlib_context_set_image(image);
gchar *source_name = g_strdup_printf("%s.c", name);
FILE *source = fopen(source_name, "wt");
fprintf(source,
"#include <%s.h>\n"
"\n"
"int %s_width = %d;\n"
"int %s_height = %d;\n"
"DATA32 %s_data[] = {",
name,
name,
imlib_image_get_width(),
name,
imlib_image_get_height(),
name);
size_t size = (size_t)imlib_image_get_width() * (size_t)imlib_image_get_height();
DATA32 *data = imlib_image_get_data_for_reading_only();
for (size_t i = 0; i < size; i++) {
fprintf(source, "%s%u", i == 0 ? "" : ", ", data[i]);
}
fprintf(source, "};\n");
fclose(source);
g_free(source_name);
imlib_free_image();
}

View file

@ -149,6 +149,8 @@ GString *tint2_g_string_replace(GString *s, const char *from, const char *to);
void get_image_mean_color(const Imlib_Image image, Color *mean_color);
void dump_image_data(const char *file_name, const char *name);
#define free_and_null(p) \
{ \
free(p); \

View file

@ -20,6 +20,9 @@ src/battery/battery.c
src/battery/battery.h
src/clock/clock.c
src/clock/clock.h
src/default_icon.c
src/default_icon.c
src/default_icon.h
src/execplugin/execplugin.c
src/execplugin/execplugin.h
src/launcher/launcher.c