static ModeMode drun_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line ) { DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw ); ModeMode retv = MODE_EXIT; gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION ); if ( mretv & MENU_NEXT ) { retv = NEXT_DIALOG; } else if ( mretv & MENU_PREVIOUS ) { retv = PREVIOUS_DIALOG; } else if ( mretv & MENU_QUICK_SWITCH ) { retv = ( mretv & MENU_LOWER_MASK ); } else if ( ( mretv & MENU_OK ) ) { exec_cmd_entry ( &( rmpd->entry_list[selected_line] ) ); } else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) { helper_execute_command ( NULL, *input, run_in_term ); } else if ( ( mretv & MENU_ENTRY_DELETE ) && selected_line < rmpd->cmd_list_length ) { if ( selected_line < rmpd->history_length ) { delete_entry_history ( &( rmpd->entry_list[selected_line] ) ); drun_entry_clear ( &( rmpd->entry_list[selected_line] ) ); memmove ( &( rmpd->entry_list[selected_line] ), &rmpd->entry_list[selected_line + 1], sizeof ( DRunModeEntry ) * ( rmpd->cmd_list_length - selected_line - 1 ) ); rmpd->cmd_list_length--; } retv = RELOAD_DIALOG; } return retv; }
static void exec_cmd_entry ( DRunModeEntry *e ) { GError *error = NULL; GRegex *reg = g_regex_new ( "%[a-zA-Z]", 0, 0, &error ); if ( error != NULL ) { fprintf ( stderr, "Internal error, failed to create regex: %s.\n", error->message ); g_error_free ( error ); return; } struct RegexEvalArg earg = { .e = e, .success = TRUE }; char *str = g_regex_replace_eval ( reg, e->exec, -1, 0, 0, drun_helper_eval_cb, &earg, &error ); if ( error != NULL ) { fprintf ( stderr, "Internal error, failed replace field codes: %s.\n", error->message ); g_error_free ( error ); return; } g_regex_unref ( reg ); if ( earg.success == FALSE ) { fprintf ( stderr, "Invalid field code in Exec line: %s.\n", e->exec );; return; } if ( str == NULL ) { fprintf ( stderr, "Nothing to execute after processing: %s.\n", e->exec );; return; } gchar *fp = rofi_expand_path ( g_strstrip ( str ) ); gchar *exec_path = g_key_file_get_string ( e->key_file, "Desktop Entry", "Path", NULL ); if ( exec_path != NULL && strlen ( exec_path ) == 0 ) { // If it is empty, ignore this property. (#529) g_free ( exec_path ); exec_path = NULL; } // Returns false if not found, if key not found, we don't want run in terminal. gboolean terminal = g_key_file_get_boolean ( e->key_file, "Desktop Entry", "Terminal", NULL ); if ( helper_execute_command ( exec_path, fp, terminal ) ) { char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL ); char *key = g_strdup_printf ( "%s:::%s", e->root, e->path ); history_set ( path, key ); g_free ( key ); g_free ( path ); } g_free ( exec_path ); g_free ( str ); g_free ( fp ); }
static ModeMode drun_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line ) { DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw ); ModeMode retv = MODE_EXIT; gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION ); if ( mretv & MENU_NEXT ) { retv = NEXT_DIALOG; } else if ( mretv & MENU_PREVIOUS ) { retv = PREVIOUS_DIALOG; } else if ( mretv & MENU_QUICK_SWITCH ) { retv = ( mretv & MENU_LOWER_MASK ); } else if ( ( mretv & MENU_OK ) ) { exec_cmd_entry ( &( rmpd->entry_list[selected_line] ) ); } else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) { RofiHelperExecuteContext context = { .name = NULL }; // FIXME: We assume startup notification in terminals, not in others helper_execute_command ( NULL, *input, run_in_term, run_in_term ? &context : NULL ); }
static void exec_cmd_entry ( DRunModeEntry *e ) { GError *error = NULL; GRegex *reg = g_regex_new ( "%[a-zA-Z]", 0, 0, &error ); if ( error != NULL ) { g_warning ( "Internal error, failed to create regex: %s.", error->message ); g_error_free ( error ); return; } struct RegexEvalArg earg = { .e = e, .success = TRUE }; char *str = g_regex_replace_eval ( reg, e->exec, -1, 0, 0, drun_helper_eval_cb, &earg, &error ); if ( error != NULL ) { g_warning ( "Internal error, failed replace field codes: %s.", error->message ); g_error_free ( error ); return; } g_regex_unref ( reg ); if ( earg.success == FALSE ) { g_warning ( "Invalid field code in Exec line: %s.", e->exec );; return; } if ( str == NULL ) { g_warning ( "Nothing to execute after processing: %s.", e->exec );; return; } const gchar *fp = g_strstrip ( str ); gchar *exec_path = g_key_file_get_string ( e->key_file, "Desktop Entry", "Path", NULL ); if ( exec_path != NULL && strlen ( exec_path ) == 0 ) { // If it is empty, ignore this property. (#529) g_free ( exec_path ); exec_path = NULL; } RofiHelperExecuteContext context = { .name = e->name, .icon = e->icon_name, .app_id = e->app_id, }; gboolean sn = g_key_file_get_boolean ( e->key_file, "Desktop Entry", "StartupNotify", NULL ); gchar *wmclass = NULL; if ( sn && g_key_file_has_key ( e->key_file, "Desktop Entry", "StartupWMClass", NULL ) ) { context.wmclass = wmclass = g_key_file_get_string ( e->key_file, "Desktop Entry", "StartupWMClass", NULL ); } // Returns false if not found, if key not found, we don't want run in terminal. gboolean terminal = g_key_file_get_boolean ( e->key_file, "Desktop Entry", "Terminal", NULL ); if ( helper_execute_command ( exec_path, fp, terminal, sn ? &context : NULL ) ) { char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL ); char *key = g_strdup_printf ( "%s:::%s", e->root, e->path ); history_set ( path, key ); g_free ( key ); g_free ( path ); } g_free ( wmclass ); g_free ( exec_path ); g_free ( str ); } /** * This function absorbs/freeś path, so this is no longer available afterwards. */ static gboolean read_desktop_file ( DRunModePrivateData *pd, const char *root, const char *path, const gchar *basename ) { // Create ID on stack. // We know strlen (path ) > strlen(root)+1 const ssize_t id_len = strlen ( path ) - strlen ( root ); char id[id_len]; g_strlcpy ( id, &( path[strlen ( root ) + 1] ), id_len ); for ( int index = 0; index < id_len; index++ ) { if ( id[index] == '/' ) { id[index] = '-'; } } // Check if item is on disabled list. if ( g_hash_table_contains ( pd->disabled_entries, id ) ) { g_debug ( "Skipping: %s, was previously seen.", id ); return TRUE; } GKeyFile *kf = g_key_file_new (); GError *error = NULL; g_key_file_load_from_file ( kf, path, 0, &error ); // If error, skip to next entry if ( error != NULL ) { g_debug ( "Failed to parse desktop file: %s because: %s", path, error->message ); g_error_free ( error ); g_key_file_free ( kf ); return FALSE; } // Skip non Application entries. gchar *key = g_key_file_get_string ( kf, "Desktop Entry", "Type", NULL ); if ( key == NULL ) { // No type? ignore. g_debug ( "Skipping desktop file: %s because: No type indicated", path ); g_key_file_free ( kf ); return FALSE; } if ( g_strcmp0 ( key, "Application" ) ) { g_debug ( "Skipping desktop file: %s because: Not of type application (%s)", path, key ); g_free ( key ); g_key_file_free ( kf ); return FALSE; } g_free ( key ); // Name key is required. if ( !g_key_file_has_key ( kf, "Desktop Entry", "Name", NULL ) ) { g_debug ( "Invalid DesktopFile: '%s', no 'Name' key present.", path ); g_key_file_free ( kf ); return FALSE; } // Skip hidden entries. if ( g_key_file_get_boolean ( kf, "Desktop Entry", "Hidden", NULL ) ) { g_debug ( "Adding desktop file: %s to disabled list because: Hdden", path ); g_key_file_free ( kf ); g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) ); return FALSE; } // Skip entries that have NoDisplay set. if ( g_key_file_get_boolean ( kf, "Desktop Entry", "NoDisplay", NULL ) ) { g_debug ( "Adding desktop file: %s to disabled list because: NoDisplay", path ); g_key_file_free ( kf ); g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) ); return FALSE; } // We need Exec, don't support DBusActivatable if ( !g_key_file_has_key ( kf, "Desktop Entry", "Exec", NULL ) ) { g_debug ( "Unsupported DesktopFile: '%s', no 'Exec' key present.", path ); g_key_file_free ( kf ); return FALSE; } if ( g_key_file_has_key ( kf, "Desktop Entry", "TryExec", NULL ) ) { char *te = g_key_file_get_string ( kf, "Desktop Entry", "TryExec", NULL ); if ( !g_path_is_absolute ( te ) ) { char *fp = g_find_program_in_path ( te ); if ( fp == NULL ) { g_free ( te ); g_key_file_free ( kf ); return FALSE; } g_free ( fp ); } else { if ( g_file_test ( te, G_FILE_TEST_IS_EXECUTABLE ) == FALSE ) { g_free ( te ); g_key_file_free ( kf ); return FALSE; } } g_free ( te ); } size_t nl = ( ( pd->cmd_list_length ) + 1 ); if ( nl >= pd->cmd_list_length_actual ) { pd->cmd_list_length_actual += 256; pd->entry_list = g_realloc ( pd->entry_list, pd->cmd_list_length_actual * sizeof ( *( pd->entry_list ) ) ); } pd->entry_list[pd->cmd_list_length].icon_size = 0; pd->entry_list[pd->cmd_list_length].root = g_strdup ( root ); pd->entry_list[pd->cmd_list_length].path = g_strdup ( path ); pd->entry_list[pd->cmd_list_length].app_id = g_strndup ( basename, strlen ( basename ) - strlen ( ".desktop" ) ); gchar *n = g_key_file_get_locale_string ( kf, "Desktop Entry", "Name", NULL, NULL ); pd->entry_list[pd->cmd_list_length].name = n; gchar *gn = g_key_file_get_locale_string ( kf, "Desktop Entry", "GenericName", NULL, NULL ); pd->entry_list[pd->cmd_list_length].generic_name = gn; pd->entry_list[pd->cmd_list_length].categories = g_key_file_get_locale_string_list ( kf, "Desktop Entry", "Categories", NULL, NULL, NULL ); pd->entry_list[pd->cmd_list_length].exec = g_key_file_get_string ( kf, "Desktop Entry", "Exec", NULL ); if ( config.show_icons ) { pd->entry_list[pd->cmd_list_length].icon_name = g_key_file_get_locale_string ( kf, "Desktop Entry", "Icon", NULL, NULL ); } else{ pd->entry_list[pd->cmd_list_length].icon_name = NULL; } pd->entry_list[pd->cmd_list_length].icon = NULL; // Keep keyfile around. pd->entry_list[pd->cmd_list_length].key_file = kf; // We don't want to parse items with this id anymore. g_hash_table_add ( pd->disabled_entries, g_strdup ( id ) ); ( pd->cmd_list_length )++; return TRUE; } /** * Internal spider used to get list of executables. */ static void walk_dir ( DRunModePrivateData *pd, const char *root, const char *dirname ) { DIR *dir; g_debug ( "Checking directory %s for desktop files.", dirname ); dir = opendir ( dirname ); if ( dir == NULL ) { return; } struct dirent *file; gchar *filename = NULL; struct stat st; while ( ( file = readdir ( dir ) ) != NULL ) { if ( file->d_name[0] == '.' ) { continue; } switch ( file->d_type ) { case DT_LNK: case DT_REG: case DT_DIR: case DT_UNKNOWN: filename = g_build_filename ( dirname, file->d_name, NULL ); break; default: continue; } // On a link, or if FS does not support providing this information // Fallback to stat method. if ( file->d_type == DT_LNK || file->d_type == DT_UNKNOWN ) { file->d_type = DT_UNKNOWN; if ( stat ( filename, &st ) == 0 ) { if ( S_ISDIR ( st.st_mode ) ) { file->d_type = DT_DIR; } else if ( S_ISREG ( st.st_mode ) ) { file->d_type = DT_REG; } } } switch ( file->d_type ) { case DT_REG: // Skip files not ending on .desktop. if ( g_str_has_suffix ( file->d_name, ".desktop" ) ) { read_desktop_file ( pd, root, filename, file->d_name ); } break; case DT_DIR: walk_dir ( pd, root, filename ); break; default: break; } g_free ( filename ); } closedir ( dir ); }