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
|
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fts.h>
#include <glib.h>
#include <gio/gdesktopappinfo.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "drun.h"
#include "history.h"
#include "log.h"
#include "mkdirp.h"
#include "string_vec.h"
#include "xmalloc.h"
static const char *default_data_dir = ".local/share/";
static const char *default_cache_dir = ".cache/";
static const char *cache_basename = "tofi-drun";
[[nodiscard("memory leaked")]]
static char *get_cache_path() {
char *cache_name = NULL;
const char *state_path = getenv("XDG_CACHE_HOME");
if (state_path == NULL) {
const char *home = getenv("HOME");
if (home == NULL) {
log_error("Couldn't retrieve HOME from environment.\n");
return NULL;
}
size_t len = strlen(home) + 1
+ strlen(default_cache_dir) + 1
+ strlen(cache_basename) + 1;
cache_name = xmalloc(len);
snprintf(
cache_name,
len,
"%s/%s/%s",
home,
default_cache_dir,
cache_basename);
} else {
size_t len = strlen(state_path) + 1
+ strlen(cache_basename) + 1;
cache_name = xmalloc(len);
snprintf(
cache_name,
len,
"%s/%s",
state_path,
cache_basename);
}
return cache_name;
}
[[nodiscard("memory leaked")]]
static struct string_vec get_application_paths() {
char *base_paths = NULL;
const char *xdg_data_dirs = getenv("XDG_DATA_DIRS");
if (xdg_data_dirs == NULL) {
xdg_data_dirs = "/usr/local/share/:/usr/share/";
}
const char *xdg_data_home = getenv("XDG_DATA_HOME");
if (xdg_data_home == NULL) {
const char *home = getenv("HOME");
if (home == NULL) {
log_error("Couldn't retrieve HOME from environment.\n");
exit(EXIT_FAILURE);
}
size_t len = strlen(home) + 1
+ strlen(default_data_dir) + 1
+ strlen(xdg_data_dirs) + 1;
base_paths = xmalloc(len);
snprintf(
base_paths,
len,
"%s/%s:%s",
home,
default_data_dir,
xdg_data_dirs);
} else {
size_t len = strlen(xdg_data_home) + 1
+ strlen(xdg_data_dirs) + 1;
base_paths = xmalloc(len);
snprintf(
base_paths,
len,
"%s:%s",
xdg_data_home,
xdg_data_dirs);
}
/* Append /applications/ to each entry. */
struct string_vec paths = string_vec_create();
char *saveptr = NULL;
char *path_entry = strtok_r(base_paths, ":", &saveptr);
while (path_entry != NULL) {
const char *subdir = "applications/";
size_t len = strlen(path_entry) + 1 + strlen(subdir) + 1;
char *apps = xmalloc(len);
snprintf(apps, len, "%s/%s", path_entry, subdir);
string_vec_add(&paths, apps);
free(apps);
path_entry = strtok_r(NULL, ":", &saveptr);
}
free(base_paths);
return paths;
}
static void parse_desktop_file(gpointer key, gpointer value, void *data)
{
const char *id = key;
const char *path = value;
struct desktop_vec *apps = data;
desktop_vec_add_file(apps, id, path);
}
struct desktop_vec drun_generate(void)
{
/*
* Note for the future: this custom logic could be replaced with
* g_app_info_get_all(), but that's slower. Worth remembering
* though if this runs into issues.
*/
log_debug("Retrieving application dirs.\n");
struct string_vec paths = get_application_paths();
struct string_vec desktop_files = string_vec_create();
log_debug("Scanning for .desktop files.\n");
for (size_t i = 0; i < paths.count; i++) {
const char *path_entry = paths.buf[i].string;
DIR *dir = opendir(path_entry);
if (dir != NULL) {
struct dirent *d;
while ((d = readdir(dir)) != NULL) {
const char *extension = strrchr(d->d_name, '.');
if (extension == NULL) {
continue;
}
if (strcmp(extension, ".desktop")) {
continue;
}
string_vec_add(&desktop_files, d->d_name);
}
closedir(dir);
}
}
log_debug("Found %zu files.\n", desktop_files.count);
log_debug("Parsing .desktop files.\n");
/*
* The Desktop Entry Specification says that only the highest
* precedence application file with a given ID should be used, so store
* the id / path pairs into a hash table to enforce uniqueness.
*/
GHashTable *id_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
struct desktop_vec apps = desktop_vec_create();
for (size_t i = 0; i < paths.count; i++) {
char *path_entry = paths.buf[i].string;
char *tree[2] = { path_entry, NULL };
size_t prefix_len = strlen(path_entry);
FTS *fts = fts_open(tree, FTS_LOGICAL, NULL);
FTSENT *entry = fts_read(fts);
for (; entry != NULL; entry = fts_read(fts)) {
const char *extension = strrchr(entry->fts_name, '.');
if (extension == NULL) {
continue;
}
if (strcmp(extension, ".desktop")) {
continue;
}
char *id = xstrdup(&entry->fts_path[prefix_len]);
char *slash = strchr(id, '/');
while (slash != NULL) {
*slash = '-';
slash = strchr(slash, '/');
}
/*
* We're iterating from highest to lowest precedence,
* so only the first file with a given ID should be
* stored.
*/
if (!g_hash_table_contains(id_hash, id)) {
char *path = xstrdup(entry->fts_path);
g_hash_table_insert(id_hash, id, path);
} else {
free(id);
}
}
fts_close(fts);
}
/* Parse the remaining files into our desktop_vec. */
g_hash_table_foreach(id_hash, parse_desktop_file, &apps);
g_hash_table_unref(id_hash);
log_debug("Found %zu apps.\n", apps.count);
/*
* It's now safe to sort the desktop file vector, as the rules about
* file precedence have been taken care of.
*/
log_debug("Sorting results.\n");
desktop_vec_sort(&apps);
string_vec_destroy(&desktop_files);
string_vec_destroy(&paths);
return apps;
}
struct desktop_vec drun_generate_cached()
{
log_debug("Retrieving cache location.\n");
char *cache_path = get_cache_path();
struct stat sb;
if (cache_path == NULL) {
return drun_generate();
}
/* If the cache doesn't exist, create it and return */
errno = 0;
if (stat(cache_path, &sb) == -1) {
if (errno == ENOENT) {
struct desktop_vec apps = drun_generate();
if (!mkdirp(cache_path)) {
free(cache_path);
return apps;
}
FILE *cache = fopen(cache_path, "wb");
desktop_vec_save(&apps, cache);
fclose(cache);
free(cache_path);
return apps;
}
free(cache_path);
return drun_generate();
}
log_debug("Retrieving application dirs.\n");
struct string_vec application_path = get_application_paths();;
/* The cache exists, so check if it's still in date */
bool out_of_date = false;
for (size_t i = 0; i < application_path.count; i++) {
struct stat path_sb;
if (stat(application_path.buf[i].string, &path_sb) == 0) {
if (path_sb.st_mtim.tv_sec > sb.st_mtim.tv_sec) {
out_of_date = true;
break;
}
}
}
string_vec_destroy(&application_path);
struct desktop_vec apps;
if (out_of_date) {
log_debug("Cache out of date, updating.\n");
log_indent();
apps = drun_generate();
log_unindent();
FILE *cache = fopen(cache_path, "wb");
desktop_vec_save(&apps, cache);
fclose(cache);
} else {
log_debug("Cache up to date, loading.\n");
FILE *cache = fopen(cache_path, "rb");
apps = desktop_vec_load(cache);
fclose(cache);
}
free(cache_path);
return apps;
}
void drun_launch(const char *filename)
{
GDesktopAppInfo *info = g_desktop_app_info_new_from_filename(filename);
GAppLaunchContext *context = g_app_launch_context_new();
if (!g_app_info_launch((GAppInfo *)info, NULL, context, NULL)) {
log_error("Failed to launch %s.\n", filename);
}
g_object_unref(context);
g_object_unref(info);
}
static int cmpscorep(const void *restrict a, const void *restrict b)
{
struct desktop_entry *restrict app1 = (struct desktop_entry *)a;
struct desktop_entry *restrict app2 = (struct desktop_entry *)b;
return app2->history_score - app1->history_score;
}
void drun_history_sort(struct desktop_vec *apps, struct history *history)
{
log_debug("Moving already known apps to the front.\n");
for (size_t i = 0; i < history->count; i++) {
struct desktop_entry *res = desktop_vec_find(apps, history->buf[i].name);
if (res == NULL) {
continue;
}
res->history_score = history->buf[i].run_count;
}
qsort(apps->buf, apps->count, sizeof(apps->buf[0]), cmpscorep);
}
|