~gotwig/notify-osd/my-notify-osd

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
/*******************************************************************************
 **3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
 **      10        20        30        40        50        60        70        80
 **
 ** notify-osd
 **
 ** util.c - all sorts of helper functions
 **
 ** Copyright 2009 Canonical Ltd.
 **
 ** Authors:
 **    Cody Russell <cody.russell@canonical.com>
 **    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
 **
 ** This program is free software: you can redistribute it and/or modify it
 ** under the terms of the GNU General Public License version 3, as published
 ** by the Free Software Foundation.
 **
 ** This program is distributed in the hope that it will be useful, but
 ** WITHOUT ANY WARRANTY; without even the implied warranties of
 ** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 ** PURPOSE.  See the GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License along
 ** with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 *******************************************************************************/

#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <pango/pango.h>
#include <cairo.h>

#define CHARACTER_LT_REGEX            "&(lt;|#60;|#x3c;)"
#define CHARACTER_GT_REGEX            "&(gt;|#62;|#x3e;)"
#define CHARACTER_AMP_REGEX           "&(amp;|#38;|#x26;)"
#define CHARACTER_APOS_REGEX          "&apos;"
#define CHARACTER_QUOT_REGEX          "&quot;"
#define CHARACTER_NEWLINE_REGEX       " *((<br[^/>]*/?>|\r|\n)+ *)+"

#define TAG_MATCH_REGEX     "<(b|i|u|big|a|img|span|s|sub|small|tt|html|qt)\\b[^>]*>(.*?)</\\1>|<(img|span|a)[^>]/>|<(img)[^>]*>"
#define TAG_REPLACE_REGEX   "<(b|i|u|big|a|img|span|s|sub|small|tt|html|qt)\\b[^>]*>|</(b|i|u|big|a|img|span|s|sub|small|tt|html|qt)>"

struct _ReplaceMarkupData
{
	gchar* regex;
	gchar* replacement;
};

typedef struct _ReplaceMarkupData ReplaceMarkupData;

static gchar*
strip_html (const gchar *text, const gchar *match_regex, const gchar* replace_regex)
{
	GRegex   *regex;
	gchar    *ret;
	gboolean  match = FALSE;
	GMatchInfo *info = NULL;

	regex = g_regex_new (match_regex, G_REGEX_DOTALL | G_REGEX_OPTIMIZE, 0, NULL);
	match = g_regex_match (regex, text, 0, &info);
	g_regex_unref (regex);

	if (match) {
		regex = g_regex_new (replace_regex, G_REGEX_DOTALL | G_REGEX_OPTIMIZE, 0, NULL);
		ret = g_regex_replace (regex, text, -1, 0, "", 0, NULL);
		g_regex_unref (regex);
	} else {
		ret = g_strdup (text);
	}

	if (info)
		g_match_info_free (info);

	return ret;
}

static gchar*
replace_markup (const gchar *text, const gchar *match_regex, const gchar *replace_text)
{
	GRegex *regex;
	gchar  *ret;

	regex = g_regex_new (match_regex, G_REGEX_DOTALL | G_REGEX_OPTIMIZE, 0, NULL);
	ret = g_regex_replace (regex, text, -1, 0, replace_text, 0, NULL);
	g_regex_unref (regex);

	return ret;
}

gchar*
filter_text (const gchar *text)
{
	gchar *text1;

	text1 = strip_html (text, TAG_MATCH_REGEX, TAG_REPLACE_REGEX);

	static ReplaceMarkupData data[] = {
		{ CHARACTER_AMP_REGEX, "&" },
		{ CHARACTER_LT_REGEX, "<" },
		{ CHARACTER_GT_REGEX, ">" },
		{ CHARACTER_APOS_REGEX, "'" },
		{ CHARACTER_QUOT_REGEX, "\"" },
		{ CHARACTER_NEWLINE_REGEX, "\n" }
		};

	ReplaceMarkupData* ptr = data;
	ReplaceMarkupData* end = data + sizeof(data) / sizeof(ReplaceMarkupData);
	for (; ptr != end; ++ptr) {
		gchar* tmp = replace_markup (text1, ptr->regex, ptr->replacement);
		g_free (text1);
		text1 = tmp;
	}

	return text1;
}

gchar*
newline_to_space (const gchar *text)
{
	gchar *text1;

	text1 = strip_html (text, TAG_MATCH_REGEX, TAG_REPLACE_REGEX);

	static ReplaceMarkupData data[] = {
		{ CHARACTER_NEWLINE_REGEX, " " }
		};

	ReplaceMarkupData* ptr = data;
	ReplaceMarkupData* end = data + sizeof(data) / sizeof(ReplaceMarkupData);
	for (; ptr != end; ++ptr) {
		gchar* tmp = replace_markup (text1, ptr->regex, ptr->replacement);
		g_free (text1);
		text1 = tmp;
	}

	return text1;
}

gboolean
destroy_cloned_surface (cairo_surface_t* surface)
{
	gboolean finalref = FALSE;
	g_return_val_if_fail (surface, FALSE);

	if (cairo_surface_get_reference_count  (surface) == 1) {
		g_free (cairo_image_surface_get_data (surface));
		finalref = TRUE;
	}
	cairo_surface_destroy (surface);
	return finalref;
}

cairo_surface_t*
copy_surface (cairo_surface_t* orig)
{
	cairo_surface_t* copy       = NULL;
	guchar*          pixels_src = NULL;
	guchar*          pixels_cpy = NULL;
	cairo_format_t   format;
	gint             width;
	gint             height;
	gint             stride;

	pixels_src = cairo_image_surface_get_data (orig);
	if (!pixels_src)
		return NULL;

	format = cairo_image_surface_get_format (orig);
	width  = cairo_image_surface_get_width (orig);
	height = cairo_image_surface_get_height (orig);
	stride = cairo_image_surface_get_stride (orig);

	pixels_cpy = g_malloc0 (stride * height);
	if (!pixels_cpy)
		return NULL;

	memcpy ((void*) pixels_cpy, (void*) pixels_src, height * stride);

	copy = cairo_image_surface_create_for_data (pixels_cpy,
						    format,
						    width,
						    height,
						    stride);

	return copy;
}

// code of get_wm_name() based in large chunks on www.amsn-project.net
gchar*
get_wm_name (Display* dpy)
{
	int            screen;
	Atom           type;
	int            format;
	unsigned long  bytes_returned;
	unsigned long  n_returned;
	unsigned char* buffer;
        Window*        child;
        Window         root;
	Atom           supwmcheck;
	Atom           wmname;

	if (!dpy)
		return NULL;

	screen = DefaultScreen (dpy);
	root = RootWindow (dpy, screen);
	supwmcheck = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
	wmname = XInternAtom (dpy, "_NET_WM_NAME", False);

	XGetWindowProperty (dpy,
			    root,
			    supwmcheck,
			    0,
			    8,
			    False,
			    AnyPropertyType,
			    &type,
			    &format,
			    &n_returned,
			    &bytes_returned,
			    &buffer);

	child = (Window*) buffer;

	if (n_returned != 1)
		return NULL;

        XGetWindowProperty (dpy,
			    *child,
			    wmname,
			    0,
			    128,
			    False,
			    AnyPropertyType,
			    &type,
			    &format,
			    &n_returned,
			    &bytes_returned,
			    &buffer);

	if (n_returned == 0)
		return NULL;

	XFree (child);

	// example wm-names as reported by get_wm_name()
	//
	//  compiz
	//  Metacity
	//  Xfwm4
	//  KWin
	//  xmonad

	return (gchar*) buffer;
}

GString*
extract_font_face (const gchar* string)
{
	GRegex*     regex      = NULL;
	GMatchInfo* match_info = NULL;
	GString*    font_face  = NULL;

	// sanity check
	if (!string)
		return NULL;

	// extract font-face-name/style
	font_face = g_string_new ("");
	if (!font_face)
		return NULL;

	// setup regular expression to extract leading text before trailing int
	regex = g_regex_new ("([A-Z a-z])+", 0, 0, NULL);

	// walk the string
	g_regex_match (regex, string, 0, &match_info);
	while (g_match_info_matches (match_info))
	{
		gchar* word = NULL;

		word = g_match_info_fetch (match_info, 0);
		if (word)
		{
			g_string_append (font_face, word);
			g_free (word);
		}

		g_match_info_next (match_info, NULL);
	}

	// clean up
	g_match_info_free (match_info);
	g_regex_unref (regex);

	return font_face;
}