-
Notifications
You must be signed in to change notification settings - Fork 0
/
apps.c
275 lines (236 loc) · 6.86 KB
/
apps.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
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
/**
* Copyright Alexandru Olaru
* See LICENSE file for copyright and license details
*/
#define _GNU_SOURCE // for DT_*
#include "apps.h"
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include "item.h"
static const char *g_syswide_apps_path = "/usr/share/applications";
static struct item *g_apps;
static size_t g_app_count;
static bool str_endswith(const char *str, const char *end)
{
char *f = strstr(str, end);
if (!f)
return NULL;
return strlen(f) == strlen(end);
}
static int parse_desktop_file_value(const char *line, const char *name, char **out_value)
{
/* Omit Allocation for null terminator since these lines should end with \n, and if not they'll have a letter cropped out */
size_t value_len = strlen(line) - strlen(name);
if (!value_len)
return -1;
*out_value = malloc(value_len * sizeof(char));
if (!(*out_value))
return -1;
snprintf(*out_value, value_len, "%s", line + strlen(name));
return 0;
}
static int find_icon_path_by_name(const char *name, char *out_path)
{
char tmppath[PATH_MAX];
snprintf(tmppath, PATH_MAX, "/usr/share/icons/hicolor/256x256/apps/%s.png", name);
if (access(tmppath, F_OK) == 0)
{
snprintf(out_path, PATH_MAX, "%s", tmppath);
return 0;
}
snprintf(tmppath, PATH_MAX, "/usr/share/icons/%s.png", name);
if (access(tmppath, F_OK) == 0)
{
snprintf(out_path, PATH_MAX, "%s", tmppath);
return 0;
}
snprintf(tmppath, PATH_MAX, "/usr/share/icons/hicolor/128x128/apps/%s.png", name);
if (access(tmppath, F_OK) == 0)
{
snprintf(out_path, PATH_MAX, "%s", tmppath);
return 0;
}
snprintf(tmppath, PATH_MAX, "/usr/share/icons/hicolor/64x64/apps/%s.png", name);
if (access(tmppath, F_OK) == 0)
{
snprintf(out_path, PATH_MAX, "%s", tmppath);
return 0;
}
snprintf(tmppath, PATH_MAX, "/usr/share/pixmaps/%s.png", name);
if (access(tmppath, F_OK) == 0)
{
snprintf(out_path, PATH_MAX, "%s", tmppath);
return 0;
}
return -1;
}
#define TEARDOWN_APP(_app) \
{ \
if (_app.text) \
free(_app.text); \
if (_app.icon_path) \
free(_app.icon_path); \
if (_app.exec_cmd) \
free(_app.exec_cmd); \
if (_app.img) \
drw_img_free(_app.img); \
}
static int parse_desktop_file(const char *path, Drw *drw, size_t icon_wh)
{
FILE *f = fopen(path, "r");
if (!f)
return -1;
bool parse_section = false;
struct item app = {0};
/* How did I get this number? I read the biggest line in my /usr/share/applications/gimp.desktop */
char line[1024];
while (fgets(line, 1024, f))
{
if (!parse_section)
{
if (strcmp(line, "[Desktop Entry]\n") == 0)
parse_section = true;
continue;
}
else if (strstr(line, "[Desktop"))
{
break;
}
if (strstr(line, "Name=") == line) // FIXME: locales?
{
if (parse_desktop_file_value(line, "Name=", &app.text) < 0)
{
TEARDOWN_APP(app);
fclose(f);
return -1;
}
}
else if (strstr(line, "Icon=") == line)
{
if (parse_desktop_file_value(line, "Icon=", &app.icon_path) < 0)
{
TEARDOWN_APP(app);
fclose(f);
return -1;
}
}
else if (strstr(line, "Exec=") == line)
{
if (parse_desktop_file_value(line, "Exec=", &app.exec_cmd) < 0)
{
TEARDOWN_APP(app);
fclose(f);
return -1;
}
/* Strip out %F %U, am I doing this right? */
char *c = strchr(app.exec_cmd, '%');
if (c)
*c = '\0';
}
else if (strstr(line, "NoDisplay=true") == line)
{
TEARDOWN_APP(app);
fclose(f);
return 0;
}
}
fclose(f);
if (!app.exec_cmd || !app.text)
{
TEARDOWN_APP(app);
return -1;
}
if (app.icon_path)
{
char imgpath[PATH_MAX];
if (find_icon_path_by_name(app.icon_path, imgpath) < 0)
goto done;
app.img = drw_img_load(drw, imgpath, icon_wh, icon_wh);
if (!app.img)
{
TEARDOWN_APP(app);
return -1;
}
}
done:
/* I think I just found my very first compiler bug in "clang version 16.0.6". If i put a label before any
type of variable declaration it errors out with "error: expected expression" for that declaration.
If i slap some other type of instrution like ';' (a noop) it works. This will need some further investigation
*/
;
struct item *tmp = realloc(g_apps, (g_app_count + 1) * sizeof(struct item));
if (!tmp)
{
TEARDOWN_APP(app);
return -1;
}
g_apps = tmp;
++g_app_count;
g_apps[g_app_count - 1] = app;
return 0;
}
static int enumerate_desktop_apps_in_dirpath(const char *dirpath, Drw *drw, size_t icon_wh)
{
DIR *d = opendir(dirpath);
if (!d)
return -1;
struct dirent *dirent;
while ((dirent = readdir(d)) != NULL)
{
if (dirent->d_type != DT_REG && dirent->d_type != DT_LNK)
continue;
if (!str_endswith(dirent->d_name, ".desktop"))
continue;
char fullpath[PATH_MAX];
snprintf(fullpath, PATH_MAX, "%s/%s", dirpath, dirent->d_name);
parse_desktop_file(fullpath, drw, icon_wh);
}
closedir(d);
return 0;
}
int dmenu_apps_parse(Drw *drw, size_t icon_wh)
{
enumerate_desktop_apps_in_dirpath(g_syswide_apps_path, drw, icon_wh);
char *user = getlogin();
if (!user)
return -1;
if (user)
{
char local_apps_path[PATH_MAX];
snprintf(local_apps_path, PATH_MAX, "/home/%s/.local/share/applications", user);
enumerate_desktop_apps_in_dirpath(local_apps_path, drw, icon_wh);
}
/* Mark the end of the list by a NULL text, as per whoever wrote the item matching code's (goofy) decision */
struct item *tmp = realloc(g_apps, (g_app_count + 1) * sizeof(struct item));
if (!tmp)
return -1;
g_apps = tmp;
++g_app_count;
g_apps[g_app_count - 1].text = NULL;
return 0;
}
void dmenu_apps_cleanup()
{
if (g_apps)
{
for (struct item *it = g_apps; it->text; ++it)
{
free(it->text);
free(it->icon_path);
free(it->exec_cmd);
}
free(g_apps);
g_apps = NULL;
g_app_count = 0;
}
}
struct item *dmenu_get_app_items()
{
return g_apps;
}