rofi  1.5.4
run.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
34 #define G_LOG_DOMAIN "Dialogs.Run"
35 
36 #include <config.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 
40 #include <unistd.h>
41 #include <limits.h>
42 #include <signal.h>
43 #include <sys/types.h>
44 #include <dirent.h>
45 #include <strings.h>
46 #include <string.h>
47 #include <errno.h>
48 
49 #include "rofi.h"
50 #include "settings.h"
51 #include "helper.h"
52 #include "history.h"
53 #include "dialogs/run.h"
54 
55 #include "mode-private.h"
56 
57 #include "timings.h"
58 #include "rofi-icon-fetcher.h"
62 #define RUN_CACHE_FILE "rofi-3.runcache"
63 
67 typedef struct
68 {
70  char **cmd_list;
72  unsigned int cmd_list_length;
74 
81 static void exec_cmd ( const char *cmd, int run_in_term )
82 {
83  GError *error = NULL;
84  if ( !cmd || !cmd[0] ) {
85  return;
86  }
87  gsize lf_cmd_size = 0;
88  gchar *lf_cmd = g_locale_from_utf8 ( cmd, -1, NULL, &lf_cmd_size, &error );
89  if ( error != NULL ) {
90  g_warning ( "Failed to convert command to locale encoding: %s", error->message );
91  g_error_free ( error );
92  return;
93  }
94 
95  char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
96  RofiHelperExecuteContext context = { .name = NULL };
97  // FIXME: assume startup notification support for terminals
98  if ( helper_execute_command ( NULL, lf_cmd, run_in_term, run_in_term ? &context : NULL ) ) {
104  history_set ( path, cmd );
105  }
106  else {
107  history_remove ( path, cmd );
108  }
109  g_free ( path );
110  g_free ( lf_cmd );
111 }
112 
118 static void delete_entry ( const char *cmd )
119 {
120  char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
121 
122  history_remove ( path, cmd );
123 
124  g_free ( path );
125 }
126 
136 static int sort_func ( const void *a, const void *b, G_GNUC_UNUSED void *data )
137 {
138  const char *astr = *( const char * const * ) a;
139  const char *bstr = *( const char * const * ) b;
140 
141  if ( astr == NULL && bstr == NULL ) {
142  return 0;
143  }
144  else if ( astr == NULL ) {
145  return 1;
146  }
147  else if ( bstr == NULL ) {
148  return -1;
149  }
150  return g_strcmp0 ( astr, bstr );
151 }
152 
156 static char ** get_apps_external ( char **retv, unsigned int *length, unsigned int num_favorites )
157 {
159  if ( fd >= 0 ) {
160  FILE *inp = fdopen ( fd, "r" );
161  if ( inp ) {
162  char *buffer = NULL;
163  size_t buffer_length = 0;
164 
165  while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
166  int found = 0;
167  // Filter out line-end.
168  if ( buffer[strlen ( buffer ) - 1] == '\n' ) {
169  buffer[strlen ( buffer ) - 1] = '\0';
170  }
171 
172  // This is a nice little penalty, but doable? time will tell.
173  // given num_favorites is max 25.
174  for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
175  if ( strcasecmp ( buffer, retv[j] ) == 0 ) {
176  found = 1;
177  }
178  }
179 
180  if ( found == 1 ) {
181  continue;
182  }
183 
184  // No duplicate, add it.
185  retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
186  retv[( *length )] = g_strdup ( buffer );
187 
188  ( *length )++;
189  }
190  if ( buffer != NULL ) {
191  free ( buffer );
192  }
193  if ( fclose ( inp ) != 0 ) {
194  g_warning ( "Failed to close stdout off executor script: '%s'",
195  g_strerror ( errno ) );
196  }
197  }
198  }
199  retv[( *length ) ] = NULL;
200  return retv;
201 }
202 
206 static char ** get_apps ( unsigned int *length )
207 {
208  GError *error = NULL;
209  char **retv = NULL;
210  unsigned int num_favorites = 0;
211  char *path;
212 
213  if ( g_getenv ( "PATH" ) == NULL ) {
214  return NULL;
215  }
216  TICK_N ( "start" );
217  path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
218  retv = history_get_list ( path, length );
219  g_free ( path );
220  // Keep track of how many where loaded as favorite.
221  num_favorites = ( *length );
222 
223  path = g_strdup ( g_getenv ( "PATH" ) );
224 
225  gsize l = 0;
226  gchar *homedir = g_locale_to_utf8 ( g_get_home_dir (), -1, NULL, &l, &error );
227  if ( error != NULL ) {
228  g_debug ( "Failed to convert homedir to UTF-8: %s", error->message );
229  g_clear_error ( &error );
230  g_free ( homedir );
231  return NULL;
232  }
233 
234  const char *const sep = ":";
235  char *strtok_savepointer = NULL;
236  for ( const char *dirname = strtok_r ( path, sep, &strtok_savepointer ); dirname != NULL; dirname = strtok_r ( NULL, sep, &strtok_savepointer ) ) {
237  char *fpath = rofi_expand_path ( dirname );
238  DIR *dir = opendir ( fpath );
239  g_debug ( "Checking path %s for executable.", fpath );
240  g_free ( fpath );
241 
242  if ( dir != NULL ) {
243  struct dirent *dent;
244  gsize dirn_len = 0;
245  gchar *dirn = g_locale_to_utf8 ( dirname, -1, NULL, &dirn_len, &error );
246  if ( error != NULL ) {
247  g_debug ( "Failed to convert directory name to UTF-8: %s", error->message );
248  g_clear_error ( &error );
249  closedir ( dir );
250  continue;
251  }
252  gboolean is_homedir = g_str_has_prefix ( dirn, homedir );
253  g_free ( dirn );
254 
255  while ( ( dent = readdir ( dir ) ) != NULL ) {
256  if ( dent->d_type != DT_REG && dent->d_type != DT_LNK && dent->d_type != DT_UNKNOWN ) {
257  continue;
258  }
259  // Skip dot files.
260  if ( dent->d_name[0] == '.' ) {
261  continue;
262  }
263  if ( is_homedir ) {
264  gchar *fpath = g_build_filename ( dirname, dent->d_name, NULL );
265  gboolean b = g_file_test ( fpath, G_FILE_TEST_IS_EXECUTABLE );
266  g_free ( fpath );
267  if ( !b ) {
268  continue;
269  }
270  }
271 
272  gsize name_len;
273  gchar *name = g_filename_to_utf8 ( dent->d_name, -1, NULL, &name_len, &error );
274  if ( error != NULL ) {
275  g_debug ( "Failed to convert filename to UTF-8: %s", error->message );
276  g_clear_error ( &error );
277  g_free ( name );
278  continue;
279  }
280  // This is a nice little penalty, but doable? time will tell.
281  // given num_favorites is max 25.
282  int found = 0;
283  for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
284  if ( g_strcmp0 ( name, retv[j] ) == 0 ) {
285  found = 1;
286  }
287  }
288 
289  if ( found == 1 ) {
290  g_free ( name );
291  continue;
292  }
293 
294  retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
295  retv[( *length )] = name;
296  retv[( *length ) + 1] = NULL;
297  ( *length )++;
298  }
299 
300  closedir ( dir );
301  }
302  }
303  g_free ( homedir );
304 
305  // Get external apps.
306  if ( config.run_list_command != NULL && config.run_list_command[0] != '\0' ) {
307  retv = get_apps_external ( retv, length, num_favorites );
308  }
309  // No sorting needed.
310  if ( ( *length ) == 0 ) {
311  return retv;
312  }
313  // TODO: check this is still fast enough. (takes 1ms on laptop.)
314  if ( ( *length ) > num_favorites ) {
315  g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func, NULL );
316  }
317  g_free ( path );
318 
319  unsigned int removed = 0;
320  for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) {
321  if ( g_strcmp0 ( retv[index], retv[index + 1] ) == 0 ) {
322  g_free ( retv[index] );
323  retv[index] = NULL;
324  removed++;
325  }
326  }
327 
328  if ( ( *length ) > num_favorites ) {
329  g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ),
330  sort_func,
331  NULL );
332  }
333  // Reduce array length;
334  ( *length ) -= removed;
335 
336  TICK_N ( "stop" );
337  return retv;
338 }
339 
340 static int run_mode_init ( Mode *sw )
341 {
342  if ( sw->private_data == NULL ) {
343  RunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
344  sw->private_data = (void *) pd;
345  pd->cmd_list = get_apps ( &( pd->cmd_list_length ) );
346  }
347 
348  return TRUE;
349 }
350 static void run_mode_destroy ( Mode *sw )
351 {
353  if ( rmpd != NULL ) {
354  g_strfreev ( rmpd->cmd_list );
355  g_free ( rmpd );
356  sw->private_data = NULL;
357  }
358 }
359 
360 static unsigned int run_mode_get_num_entries ( const Mode *sw )
361 {
362  const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
363  return rmpd->cmd_list_length;
364 }
365 
366 static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
367 {
369  ModeMode retv = MODE_EXIT;
370 
371  gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
372 
373  if ( mretv & MENU_NEXT ) {
374  retv = NEXT_DIALOG;
375  }
376  else if ( mretv & MENU_PREVIOUS ) {
377  retv = PREVIOUS_DIALOG;
378  }
379  else if ( mretv & MENU_QUICK_SWITCH ) {
380  retv = ( mretv & MENU_LOWER_MASK );
381  }
382  else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
383  exec_cmd ( rmpd->cmd_list[selected_line], run_in_term );
384  }
385  else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
386  exec_cmd ( *input, run_in_term );
387  }
388  else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line] ) {
389  delete_entry ( rmpd->cmd_list[selected_line] );
390 
391  // Clear the list.
392  retv = RELOAD_DIALOG;
393  run_mode_destroy ( sw );
394  run_mode_init ( sw );
395  }
396  return retv;
397 }
398 
399 static char *_get_display_value ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry )
400 {
401  const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
402  return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
403 }
404 
405 static int run_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
406 {
407  const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
408  return helper_token_match ( tokens, rmpd->cmd_list[index] );
409 }
410 
411 #include "mode-private.h"
413 {
414  .name = "run",
415  .cfg_name_key = "display-run",
416  ._init = run_mode_init,
417  ._get_num_entries = run_mode_get_num_entries,
418  ._result = run_mode_result,
419  ._destroy = run_mode_destroy,
420  ._token_match = run_token_match,
421  ._get_display_value = _get_display_value,
422  ._get_icon = NULL,
423  ._get_completion = NULL,
424  ._preprocess_input = NULL,
425  .private_data = NULL,
426  .free = NULL
427 };
MENU_QUICK_SWITCH
@ MENU_QUICK_SWITCH
Definition: mode.h:79
delete_entry
static void delete_entry(const char *cmd)
Definition: run.c:118
history.h
cache_dir
const char * cache_dir
Definition: rofi.c:84
rofi_mode::name
char * name
Definition: mode-private.h:156
settings.h
RunModePrivateData::cmd_list
char ** cmd_list
Definition: run.c:70
get_apps_external
static char ** get_apps_external(char **retv, unsigned int *length, unsigned int num_favorites)
Definition: run.c:156
sort_func
static int sort_func(const void *a, const void *b, G_GNUC_UNUSED void *data)
Definition: run.c:136
run_mode_get_num_entries
static unsigned int run_mode_get_num_entries(const Mode *sw)
Definition: run.c:360
MENU_OK
@ MENU_OK
Definition: mode.h:69
rofi_int_matcher_t
Definition: rofi-types.h:235
RofiHelperExecuteContext
Definition: helper.h:267
PREVIOUS_DIALOG
@ PREVIOUS_DIALOG
Definition: mode.h:58
run_mode_destroy
static void run_mode_destroy(Mode *sw)
Definition: run.c:350
MENU_PREVIOUS
@ MENU_PREVIOUS
Definition: mode.h:81
run.h
NEXT_DIALOG
@ NEXT_DIALOG
Definition: mode.h:54
execute_generator
int execute_generator(const char *cmd)
Definition: helper.c:458
timings.h
RunModePrivateData
Definition: run.c:68
mode-private.h
Settings::run_list_command
char * run_list_command
Definition: settings.h:91
MODE_EXIT
@ MODE_EXIT
Definition: mode.h:52
MENU_ENTRY_DELETE
@ MENU_ENTRY_DELETE
Definition: mode.h:77
exec_cmd
static void exec_cmd(const char *cmd, int run_in_term)
Definition: run.c:81
TICK_N
#define TICK_N(a)
Definition: timings.h:68
rofi_expand_path
char * rofi_expand_path(const char *input)
Definition: helper.c:658
rofi_mode
Definition: mode-private.h:152
rofi.h
run_token_match
static int run_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition: run.c:405
rofi_mode::private_data
void * private_data
Definition: mode-private.h:185
history_set
void history_set(const char *filename, const char *entry)
Definition: history.c:178
history_get_list
char ** history_get_list(const char *filename, unsigned int *length)
Definition: history.c:325
MENU_CUSTOM_ACTION
@ MENU_CUSTOM_ACTION
Definition: mode.h:83
MENU_CUSTOM_INPUT
@ MENU_CUSTOM_INPUT
Definition: mode.h:75
helper_execute_command
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
Definition: helper.c:989
MENU_NEXT
@ MENU_NEXT
Definition: mode.h:73
RunModePrivateData::cmd_list_length
unsigned int cmd_list_length
Definition: run.c:72
run_mode_result
static ModeMode run_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition: run.c:366
run_mode
Mode run_mode
Definition: run.c:412
run_mode_init
static int run_mode_init(Mode *sw)
Definition: run.c:340
_get_display_value
static char * _get_display_value(const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry)
Definition: run.c:399
get_apps
static char ** get_apps(unsigned int *length)
Definition: run.c:206
helper_token_match
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:445
rofi-icon-fetcher.h
ModeMode
ModeMode
Definition: mode.h:50
RELOAD_DIALOG
@ RELOAD_DIALOG
Definition: mode.h:56
MENU_LOWER_MASK
@ MENU_LOWER_MASK
Definition: mode.h:85
helper.h
config
Settings config
history_remove
void history_remove(const char *filename, const char *entry)
Definition: history.c:259
RofiHelperExecuteContext::name
const gchar * name
Definition: helper.h:269
RUN_CACHE_FILE
#define RUN_CACHE_FILE
Definition: run.c:62