/
icon.c
181 lines (147 loc) · 5.86 KB
/
icon.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
#include "icon.h"
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdbool.h>
#include <string.h>
#include "log.h"
#include "notification.h"
#include "settings.h"
static bool is_readable_file(const char *filename)
{
return (access(filename, R_OK) != -1);
}
const char *get_filename_ext(const char *filename)
{
const char *dot = strrchr(filename, '.');
if (!dot || dot == filename) return "";
return dot + 1;
}
static cairo_status_t read_from_buf(void *closure, unsigned char *data, unsigned int size)
{
GByteArray *buf = (GByteArray *)closure;
unsigned int cpy = MIN(size, buf->len);
memcpy(data, buf->data, cpy);
g_byte_array_remove_range(buf, 0, cpy);
return CAIRO_STATUS_SUCCESS;
}
cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf)
{
/*
* Export the gdk pixbuf into buffer as a png and import the png buffer
* via cairo again as a cairo_surface_t.
* It looks counterintuitive, as there is gdk_cairo_set_source_pixbuf,
* which does the job faster. But this would require gtk3 as a dependency
* for a single function call. See discussion in #334 and #376.
*/
cairo_surface_t *icon_surface = NULL;
GByteArray *buffer;
char *bufstr;
gsize buflen;
gdk_pixbuf_save_to_buffer(pixbuf, &bufstr, &buflen, "png", NULL, NULL);
buffer = g_byte_array_new_take((guint8*)bufstr, buflen);
icon_surface = cairo_image_surface_create_from_png_stream(read_from_buf, buffer);
g_byte_array_free(buffer, TRUE);
return icon_surface;
}
static GdkPixbuf *get_pixbuf_from_file(const char *filename)
{
GdkPixbuf *pixbuf = NULL;
if (is_readable_file(filename)) {
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if (!pixbuf) {
LOG_M("%s", error->message);
g_error_free(error);
}
}
return pixbuf;
}
GdkPixbuf *get_pixbuf_from_icon(const char *iconname)
{
if (!iconname || iconname[0] == '\0')
return NULL;
const char *suffixes[] = { ".svg", ".png", ".xpm", NULL };
GdkPixbuf *pixbuf = NULL;
gchar *uri_path = NULL;
if (g_str_has_prefix(iconname, "file://")) {
uri_path = g_filename_from_uri(iconname, NULL, NULL);
if (uri_path)
iconname = uri_path;
}
/* absolute path? */
if (iconname[0] == '/' || iconname[0] == '~') {
pixbuf = get_pixbuf_from_file(iconname);
}
/* search in icon_path */
if (!pixbuf) {
char *start = settings.icon_path,
*end, *current_folder, *maybe_icon_path;
do {
end = strchr(start, ':');
if (!end) end = strchr(settings.icon_path, '\0'); /* end = end of string */
current_folder = g_strndup(start, end - start);
for (const char **suf = suffixes; *suf; suf++) {
maybe_icon_path = g_strconcat(current_folder, "/", iconname, *suf, NULL);
pixbuf = get_pixbuf_from_file(maybe_icon_path);
g_free(maybe_icon_path);
if (pixbuf)
break;
}
g_free(current_folder);
if (pixbuf)
break;
start = end + 1;
} while (*(end) != '\0');
}
g_free(uri_path);
return pixbuf;
}
GdkPixbuf *get_pixbuf_from_raw_image(const RawImage *raw_image)
{
GdkPixbuf *pixbuf = NULL;
pixbuf = gdk_pixbuf_new_from_data(raw_image->data,
GDK_COLORSPACE_RGB,
raw_image->has_alpha,
raw_image->bits_per_sample,
raw_image->width,
raw_image->height,
raw_image->rowstride,
NULL,
NULL);
return pixbuf;
}
cairo_surface_t *icon_get_for_notification(const notification *n)
{
GdkPixbuf *pixbuf;
if (n->raw_icon)
pixbuf = get_pixbuf_from_raw_image(n->raw_icon);
else if (n->icon)
pixbuf = get_pixbuf_from_icon(n->icon);
else
return NULL;
if (!pixbuf)
return NULL;
int w = gdk_pixbuf_get_width(pixbuf);
int h = gdk_pixbuf_get_height(pixbuf);
int larger = w > h ? w : h;
if (settings.max_icon_size && larger > settings.max_icon_size) {
GdkPixbuf *scaled;
if (w >= h) {
scaled = gdk_pixbuf_scale_simple(pixbuf,
settings.max_icon_size,
(int) ((double) settings.max_icon_size / w * h),
GDK_INTERP_BILINEAR);
} else {
scaled = gdk_pixbuf_scale_simple(pixbuf,
(int) ((double) settings.max_icon_size / h * w),
settings.max_icon_size,
GDK_INTERP_BILINEAR);
}
g_object_unref(pixbuf);
pixbuf = scaled;
}
cairo_surface_t *ret = gdk_pixbuf_to_cairo_surface(pixbuf);
g_object_unref(pixbuf);
return ret;
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */