1+ #include <stddef.h>
12#include <stdio.h>
23#include <stdlib.h>
34#include <stdarg.h>
@@ -124,6 +125,65 @@ void parse_config_file(const char *config_file_path)
124125 log_fatal ("Could not parse config file" );
125126}
126127
128+ #ifdef __unix__
129+ int desktop_config_handler (void * user , const char * section , const char * name , const char * value ){
130+ Entry * entry = (Entry * )(user );
131+ if (MATCH (section , "Desktop Entry" )){
132+ if (MATCH (name , "Icon" ))
133+ entry -> icon_path = strdup (value );
134+
135+ if (MATCH (name , "Exec" )){
136+ ssize_t end = strcspn (value , "%@" );
137+ char * cmd = malloc (end + 1 );
138+ cmd [end ] = '\0' ;
139+ memcpy (cmd , value , end );
140+ entry -> cmd = cmd ;
141+ log_error ("Parsed command '%s' from desktop file." , cmd );
142+ }
143+
144+ if (MATCH (name , "Name" ))
145+ entry -> title = strdup (value );
146+ return 0 ;
147+ }
148+ return 1 ;
149+ }
150+
151+
152+ Entry parse_possible_desktop_file (const char * name ){
153+ const char * xdg_dirs = getenv ("XDG_DATA_DIRS" );
154+ const char * app_path = "/applications/" ;
155+
156+ Entry result = {};
157+ if (xdg_dirs != NULL ){
158+ ssize_t last_delim = -1 ;
159+ ssize_t dirs_len = strlen (xdg_dirs );
160+ while (last_delim < dirs_len ){
161+ char xdg_path [4096 ] = {0 };
162+ ssize_t start_ind = last_delim ;
163+ if (start_ind < 0 )
164+ start_ind = 0 ;
165+
166+ ssize_t end = strcspn (& xdg_dirs [start_ind ], ":" );
167+ memcpy (& xdg_path , & xdg_dirs [start_ind ], end );
168+ memcpy (& xdg_path [end ], app_path , strlen (app_path ));
169+ memcpy (& xdg_path [end + strlen (app_path )], name , strlen (name ));
170+
171+ FILE * file = fopen (xdg_path , "r" );
172+ if (file != NULL ){
173+ int error = ini_parse_file (file , desktop_config_handler , & result );
174+ fclose (file );
175+ if (error == 0 )
176+ return result ;
177+ }
178+ last_delim += end + 1 ;
179+ }
180+ }
181+ return result ;
182+
183+ }
184+ #endif
185+
186+
127187// A function to handle config file parsing
128188int config_handler (void * user , const char * section , const char * name , const char * value )
129189{
@@ -445,8 +505,19 @@ int config_handler(void *user, const char *section, const char *name, const char
445505 // Store data in entry struct
446506 int i ;
447507 for (i = 0 ;i < 3 && token != NULL ; i ++ ) {
448- if (i == 0 )
508+ if (i == 0 ){
449509 entry -> title = strdup (token );
510+ #ifdef __unix__
511+ Entry ent = parse_possible_desktop_file (entry -> title );
512+ if (ent .cmd != NULL || ent .icon_path != NULL || ent .title != NULL ){
513+ entry -> cmd = ent .cmd ;
514+ entry -> icon_path = ent .icon_path ;
515+ entry -> title = ent .title ;
516+ i = 3 ;
517+ break ;
518+ }
519+ #endif
520+ }
450521 else if (i == 1 ) {
451522 entry -> icon_path = strdup (token );
452523 clean_path (entry -> icon_path );
@@ -1101,4 +1172,4 @@ void sprintf_alloc(char **buffer, const char *format, ...)
11011172 }
11021173 va_end (args1 );
11031174 va_end (args2 );
1104- }
1175+ }
0 commit comments