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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 Red Hat, Inc.
* Copyright (C) 2012-2021 MATE Developers
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author: Behdad Esfahbod, Red Hat, Inc.
*/
#include "fontconfig-monitor.h"
#include <gio/gio.h>
#include <fontconfig/fontconfig.h>
#define TIMEOUT_SECONDS 2
static void
stuff_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer handle);
void
fontconfig_cache_init (void)
{
FcInit ();
}
gboolean
fontconfig_cache_update (void)
{
return !FcConfigUptoDate (NULL) && FcInitReinitialize ();
}
static void
monitor_files (GPtrArray *monitors,
FcStrList *list,
gpointer data)
{
const char *str;
while ((str = (const char *) FcStrListNext (list))) {
GFile *file;
GFileMonitor *monitor;
file = g_file_new_for_path (str);
monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL);
g_object_unref (file);
if (!monitor)
continue;
g_signal_connect (monitor, "changed", G_CALLBACK (stuff_changed), data);
g_ptr_array_add (monitors, monitor);
}
FcStrListDone (list);
}
struct _fontconfig_monitor_handle {
GPtrArray *monitors;
guint timeout;
GFunc notify_callback;
gpointer notify_data;
};
static GPtrArray *
monitors_create (gpointer data)
{
GPtrArray *monitors = g_ptr_array_new ();
monitor_files (monitors, FcConfigGetConfigFiles (NULL), data);
monitor_files (monitors, FcConfigGetFontDirs (NULL) , data);
return monitors;
}
static void
monitors_free (GPtrArray *monitors)
{
if (!monitors)
return;
g_ptr_array_foreach (monitors, (GFunc) g_object_unref, NULL);
g_ptr_array_free (monitors, TRUE);
}
static gboolean
update (gpointer data)
{
fontconfig_monitor_handle_t *handle = data;
gboolean notify = FALSE;
handle->timeout = 0;
if (fontconfig_cache_update ()) {
notify = TRUE;
monitors_free (handle->monitors);
handle->monitors = monitors_create (data);
}
/* we finish modifying handle before calling the notify callback,
* allowing the callback to free the monitor if it decides to. */
if (notify && handle->notify_callback)
handle->notify_callback (data, handle->notify_data);
return FALSE;
}
static void
stuff_changed (GFileMonitor *monitor G_GNUC_UNUSED,
GFile *file G_GNUC_UNUSED,
GFile *other_file G_GNUC_UNUSED,
GFileMonitorEvent event_type G_GNUC_UNUSED,
gpointer data)
{
fontconfig_monitor_handle_t *handle = data;
/* wait for quiescence */
if (handle->timeout)
g_source_remove (handle->timeout);
handle->timeout = g_timeout_add_seconds (TIMEOUT_SECONDS, update, data);
}
fontconfig_monitor_handle_t *
fontconfig_monitor_start (GFunc notify_callback,
gpointer notify_data)
{
fontconfig_monitor_handle_t *handle = g_slice_new0 (fontconfig_monitor_handle_t);
handle->notify_callback = notify_callback;
handle->notify_data = notify_data;
handle->monitors = monitors_create (handle);
return handle;
}
void
fontconfig_monitor_stop (fontconfig_monitor_handle_t *handle)
{
if (handle->timeout)
g_source_remove (handle->timeout);
handle->timeout = 0;
monitors_free (handle->monitors);
handle->monitors = NULL;
}
#ifdef FONTCONFIG_MONITOR_TEST
static void
yay (void)
{
g_message ("yay");
}
int
main (void)
{
GMainLoop *loop;
fontconfig_monitor_start ((GFunc) yay, NULL);
loop = g_main_loop_new (NULL, TRUE);
g_main_loop_run (loop);
return 0;
}
#endif
|