29#define G_LOG_DOMAIN "Modes.Script"
87 G_GNUC_UNUSED
size_t length) {
88 gchar **extras = g_strsplit(buffer,
"\x1f", -1);
89 gchar **extra = extras;
90 for (; *extra != NULL && *(extra + 1) != NULL; extra += 2) {
92 gchar *value = *(extra + 1);
96 if (strcasecmp(key,
"icon") == 0) {
98 }
else if (strcasecmp(key,
"display") == 0) {
100 }
else if (strcasecmp(key,
"meta") == 0) {
102 }
else if (strcasecmp(key,
"info") == 0) {
104 }
else if (strcasecmp(key,
"nonselectable") == 0) {
105 entry->
nonselectable = g_ascii_strcasecmp(value,
"true") == 0;
107 }
else if (strcasecmp(key,
"permanent") == 0) {
108 entry->
permanent = g_ascii_strcasecmp(value,
"true") == 0;
110 }
else if (strcasecmp(key,
"urgent") == 0) {
111 entry->
urgent = g_ascii_strcasecmp(value,
"true") == 0;
113 }
else if (strcasecmp(key,
"active") == 0) {
114 entry->
active = g_ascii_strcasecmp(value,
"true") == 0;
122 if (*extras != NULL) {
134 ssize_t length_key = 0;
135 while (length_key < length && line[length_key] !=
'\x1f') {
139 if ((length_key + 1) < length) {
140 line[length_key] =
'\0';
141 char *value = line + length_key + 1;
142 if (strcasecmp(line,
"message") == 0) {
144 pd->
message = strlen(value) ? g_strdup(value) : NULL;
145 }
else if (strcasecmp(line,
"prompt") == 0) {
147 pd->
prompt = g_strdup(value);
149 }
else if (strcasecmp(line,
"markup-rows") == 0) {
150 pd->
do_markup = (strcasecmp(value,
"true") == 0);
151 }
else if (strcasecmp(line,
"urgent") == 0) {
153 }
else if (strcasecmp(line,
"active") == 0) {
155 }
else if (strcasecmp(line,
"delim") == 0) {
157 }
else if (strcasecmp(line,
"no-custom") == 0) {
158 pd->
no_custom = (strcasecmp(value,
"true") == 0);
159 }
else if (strcasecmp(line,
"use-hot-keys") == 0) {
161 }
else if (strcasecmp(line,
"keep-selection") == 0) {
163 }
else if (strcasecmp(line,
"keep-filter") == 0) {
164 pd->
keep_filter = (strcasecmp(value,
"true") == 0);
165 }
else if (strcasecmp(line,
"new-selection") == 0) {
166 pd->
new_selection = (int64_t)g_ascii_strtoll(value, NULL, 0);
167 }
else if (strcasecmp(line,
"data") == 0) {
169 pd->
data = g_strdup(value);
170 }
else if (strcasecmp(line,
"theme") == 0) {
172 g_warning(
"Failed to parse: '%s'", value);
180 unsigned int *length,
int value,
184 GError *error = NULL;
194 char **env = g_get_environ();
196 char *str_value = g_strdup_printf(
"%d", value);
197 env = g_environ_setenv(env,
"ROFI_RETV", str_value, TRUE);
200 str_value = g_strdup_printf(
"%d", (
int)getpid());
201 env = g_environ_setenv(env,
"ROFI_OUTSIDE", str_value, TRUE);
204 if (entry && entry->
info) {
205 env = g_environ_setenv(env,
"ROFI_INFO", entry->
info, TRUE);
208 env = g_environ_setenv(env,
"ROFI_DATA", pd->
data, TRUE);
211 if (g_shell_parse_argv(sw->
ed, &argc, &argv, &error)) {
212 argv = g_realloc(argv, (argc + 2) *
sizeof(
char *));
213 argv[argc] = g_strdup(arg);
214 argv[argc + 1] = NULL;
215 g_spawn_async_with_pipes(NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL,
216 NULL, NULL, &fd, NULL, &error);
220 char *msg = g_strdup_printf(
"Failed to execute: '%s'\nError: '%s'",
221 (
char *)sw->
ed, error->message);
229 FILE *inp = fdopen(fd,
"r");
232 size_t buffer_length = 0;
233 ssize_t read_length = 0;
234 size_t actual_size = 0;
235 while ((read_length = getdelim(&buffer, &buffer_length, pd->
delim, inp)) >
238 if (buffer[read_length - 1] == pd->
delim) {
239 buffer[read_length - 1] =
'\0';
241 if (buffer[0] ==
'\0') {
244 if (actual_size < ((*length) + 2)) {
249 size_t buf_length = strlen(buffer) + 1;
250#if GLIB_CHECK_VERSION(2, 68, 0)
251 retv[(*length)].
entry = g_memdup2(buffer, buf_length);
253 retv[(*length)].
entry = g_memdup(buffer, buf_length);
256 retv[(*length)].
display = NULL;
257 retv[(*length)].
meta = NULL;
258 retv[(*length)].
info = NULL;
259 retv[(*length)].
active = FALSE;
260 retv[(*length)].
urgent = FALSE;
266 if (buf_length > 0 && (read_length > (ssize_t)buf_length)) {
269 read_length - buf_length);
279 if (fclose(inp) != 0) {
280 g_warning(
"Failed to close stdout off executor script: '%s'",
325 unsigned int selected_line) {
329 unsigned int new_length = 0;
337 if (selected_line != UINT32_MAX) {
356 3, &(rmpd->
cmd_list[selected_line]));
364 1, &(rmpd->
cmd_list[selected_line]));
375 if (new_list != NULL) {
387 if (keep_selection) {
398 if (keep_filter == FALSE) {
426static inline unsigned int get_index(
unsigned int length,
int index) {
430 if (((
unsigned int)-index) <= length) {
431 return length + index;
437 G_GNUC_UNUSED
int *state,
438 G_GNUC_UNUSED GList **list,
int get_entry) {
444 if (selected_line >=
start && selected_line <=
stop) {
452 if (selected_line >=
start && selected_line <=
stop) {
466 return get_entry ? g_strdup(pd->
cmd_list[selected_line].
display) : NULL;
468 return get_entry ? g_strdup(pd->
cmd_list[selected_line].
entry) : NULL;
473 unsigned int index) {
484 pango_parse_markup(rmpd->
cmd_list[index].
entry, -1, 0, NULL, &esc, NULL,
492 for (
int j = 0; match && tokens[j] != NULL; j++) {
496 if (test == tokens[j]->invert && rmpd->
cmd_list[index].
meta) {
517 unsigned int selected_line,
518 unsigned int height) {
522 g_return_val_if_fail(pd->
cmd_list != NULL, NULL);
562 const char *cpath = g_get_user_config_dir();
563 char *script_dir = g_build_filename(cpath,
"rofi",
"scripts", NULL);
564 if (g_file_test(script_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ==
569 GDir *sd = g_dir_open(script_dir, 0, NULL);
571 const char *file = NULL;
572 while ((file = g_dir_read_name(sd)) != NULL) {
573 char *sp = g_build_filename(cpath,
"rofi",
"scripts", file, NULL);
603 Mode *sw = g_malloc0(
sizeof(*sw));
619 Mode *sw = g_malloc0(
sizeof(*sw));
620 unsigned int index = 0;
621 const char *
const sep =
":";
622 char **tokens = g_strsplit(str, sep, 2);
624 index = g_strv_length(tokens);
625 sw->
name = g_strdup(tokens[0]);
646 "The script command '%s' has %u options, but needs 2: <name>:<script>.",
656 return strchr(token,
':') != NULL;
662 printf(
" • %s%s%s (%s)\n", is_term ? (
color_bold) :
"",
guint display_scale(void)
static char * _get_display_value(const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **attr_list, int get_entry)
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
char helper_parse_char(const char *arg)
char * rofi_expand_path(const char *input)
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void * mode_get_private_data(const Mode *mode)
void rofi_clear_error_messages(void)
void script_mode_gather_user_scripts(void)
gboolean script_mode_is_valid(const char *token)
void script_user_list(gboolean is_term)
void script_mode_cleanup(void)
Mode * script_mode_parse_setup(const char *str)
int rofi_view_error_dialog(const char *msg, int markup)
RofiViewState * rofi_view_get_active(void)
void rofi_view_set_selected_line(RofiViewState *state, unsigned int selected_line)
struct rofi_int_matcher_t rofi_int_matcher
static void script_mode_destroy(Mode *sw)
static void script_switcher_free(Mode *sw)
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, unsigned int height)
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, G_GNUC_UNUSED size_t length)
static unsigned int get_index(unsigned int length, int index)
static char * script_get_message(const Mode *sw)
ScriptUser * user_scripts
static int script_mode_has_user_script(char const *const user)
static void script_mode_reset_highlight(Mode *sw)
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)
static int script_mode_init(Mode *sw)
static DmenuScriptEntry * execute_executor(Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry)
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
static unsigned int script_mode_get_num_entries(const Mode *sw)
struct rofi_range_pair * urgent_list
struct rofi_range_pair * active_list
unsigned int num_urgent_list
unsigned int cmd_list_length
unsigned int num_active_list
DmenuScriptEntry * cmd_list
__mode_get_num_entries _get_num_entries
_mode_preprocess_input _preprocess_input
_mode_token_match _token_match
_mode_get_display_value _get_display_value
_mode_get_completion _get_completion
_mode_get_message _get_message
gboolean rofi_theme_parse_string(const char *string)