30 #define G_LOG_DOMAIN "Helper"
43 #include <glib/gstdio.h>
45 #include <pango/pango-fontmap.h>
46 #include <pango/pango.h>
47 #include <pango/pangocairo.h>
54 #include <sys/types.h>
61 "on focused monitor",
"on focused window",
"at mouse pointer",
62 "on monitor with focused window",
"on monitor that has mouse pointer"};
78 h = g_hash_table_new(g_str_hash, g_str_equal);
86 char *key = va_arg(ap,
char *);
87 if (key == (
char *)0) {
90 char *value = va_arg(ap,
char *);
91 if (value == (
char *)0) {
94 g_hash_table_insert(h, key, value);
100 g_hash_table_destroy(h);
102 if (g_shell_parse_argv(res, length, output, &error)) {
109 char *msg = g_strdup_printf(
"Failed to parse: '%s'\nError: '%s'",
string,
120 for (
size_t i = 0; tokens && tokens[i]; i++) {
121 g_regex_unref((GRegex *)tokens[i]->regex);
128 gchar *r = g_regex_escape_string(input, -1);
129 size_t str_l = strlen(r);
130 for (
size_t i = 0; i < str_l; i++) {
132 if (r[i + 1] ==
'*') {
134 }
else if (r[i + 1] ==
'?') {
143 GString *str = g_string_new(
"");
144 gchar *r = g_regex_escape_string(input, -1);
147 for (iter = r; iter && *iter !=
'\0'; iter = g_utf8_next_char(iter)) {
149 g_string_append(str,
"(");
151 g_string_append(str,
".*?(");
154 g_string_append_c(str,
'\\');
155 iter = g_utf8_next_char(iter);
157 if ((*iter) ==
'\0') {
161 g_string_append_unichar(str, g_utf8_get_char(iter));
162 g_string_append(str,
")");
166 char *retv = str->str;
167 g_string_free(str, FALSE);
172 gchar *r = g_regex_escape_string(input, -1);
173 char *retv = g_strconcat(
"\\b", r, NULL);
179 gunichar buf2[G_UNICHAR_MAX_DECOMPOSITION_LENGTH] = {
186 char *str = g_malloc0((g_utf8_strlen(s, 0) * 6 + 2));
188 for (
const char *iter = s; iter && *iter; iter = g_utf8_next_char(iter)) {
189 gunichar uc = g_utf8_get_char(iter);
191 gsize dl = g_unichar_fully_decompose(uc, FALSE, buf2,
192 G_UNICHAR_MAX_DECOMPOSITION_LENGTH);
194 l = g_unichar_to_utf8(buf2[0], buf);
196 l = g_unichar_to_utf8(uc, buf);
198 memcpy(striter, buf, l);
206 static inline GRegex *
R(
const char *s,
int case_sensitive) {
210 GRegex *r = g_regex_new(
211 str, G_REGEX_OPTIMIZE | ((case_sensitive) ? 0 : G_REGEX_CASELESS), 0,
218 s, G_REGEX_OPTIMIZE | ((case_sensitive) ? 0 : G_REGEX_CASELESS), 0, NULL);
232 retv =
R(r, case_sensitive);
236 retv =
R(input, case_sensitive);
238 r = g_regex_escape_string(input, -1);
239 retv =
R(r, case_sensitive);
245 retv =
R(r, case_sensitive);
250 retv =
R(r, case_sensitive);
254 r = g_regex_escape_string(input, -1);
255 retv =
R(r, case_sensitive);
266 size_t len = strlen(input);
271 char *saveptr = NULL, *token;
283 char *str = g_strdup(input);
287 const char *
const sep =
" ";
288 for (token = strtok_r(str, sep, &saveptr); token != NULL;
289 token = strtok_r(NULL, sep, &saveptr)) {
292 retv[num_tokens + 1] = NULL;
321 const char **retv = NULL;
329 retv = g_malloc0((length + 1) *
sizeof(
char *));
343 if (val != NULL && i > 0 && i < (
stored_argc - 1)) {
352 if (val != NULL && i > 0 && i < (
stored_argc - 1)) {
360 const size_t len = strlen(arg);
366 if (len == 2 && arg[0] ==
'\\') {
399 if (len > 2 && arg[0] ==
'\\' && arg[1] ==
'x') {
400 return (
char)strtol(&arg[2], NULL, 16);
402 g_warning(
"Failed to parse character string: \"%s\"", arg);
410 if (val != NULL && i > 0 && i < (
stored_argc - 1)) {
420 PangoAttrList *retv) {
427 for (
int j = 0; tokens[j]; j++) {
428 GMatchInfo *gmi = NULL;
429 if (tokens[j]->invert) {
432 g_regex_match(tokens[j]->regex, input, G_REGEX_MATCH_PARTIAL, &gmi);
433 while (g_match_info_matches(gmi)) {
434 int count = g_match_info_get_match_count(gmi);
435 for (
int index = (
count > 1) ? 1 : 0; index <
count; index++) {
437 g_match_info_fetch_pos(gmi, index, &start, &end);
439 PangoAttribute *pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
440 pa->start_index = start;
442 pango_attr_list_insert(retv, pa);
446 pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
447 pa->start_index = start;
449 pango_attr_list_insert(retv, pa);
452 PangoAttribute *pa = pango_attr_strikethrough_new(TRUE);
453 pa->start_index = start;
455 pango_attr_list_insert(retv, pa);
459 pango_attr_variant_new(PANGO_VARIANT_SMALL_CAPS);
460 pa->start_index = start;
462 pango_attr_list_insert(retv, pa);
465 PangoAttribute *pa = pango_attr_style_new(PANGO_STYLE_ITALIC);
466 pa->start_index = start;
468 pango_attr_list_insert(retv, pa);
471 PangoAttribute *pa = pango_attr_foreground_new(
474 pa->start_index = start;
476 pango_attr_list_insert(retv, pa);
479 pa = pango_attr_foreground_alpha_new(th.
color.
alpha * 65535);
480 pa->start_index = start;
482 pango_attr_list_insert(retv, pa);
486 g_match_info_next(gmi, NULL);
488 g_match_info_free(gmi);
500 for (
int j = 0; match && tokens[j]; j++) {
501 match = g_regex_match(tokens[j]->regex, r, 0, NULL);
502 match ^= tokens[j]->invert;
506 for (
int j = 0; match && tokens[j]; j++) {
507 match = g_regex_match(tokens[j]->regex, input, 0, NULL);
508 match ^= tokens[j]->invert;
521 GError *error = NULL;
522 g_spawn_async_with_pipes(NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
523 NULL, NULL, &fd, NULL, &error);
526 char *msg = g_strdup_printf(
"Failed to execute: '%s'\nError: '%s'", cmd,
543 int fd = g_open(
pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
545 g_warning(
"Failed to create pid file: '%s'.",
pidfile);
549 int flags = fcntl(fd, F_GETFD, NULL);
551 if (fcntl(fd, F_SETFD,
flags, NULL) < 0) {
552 g_warning(
"Failed to set CLOEXEC on pidfile.");
557 int retv = flock(fd, LOCK_EX | LOCK_NB);
559 g_warning(
"Failed to set lock on pidfile: Rofi already running?");
560 g_warning(
"Got error: %d %s", retv, g_strerror(errno));
564 if (ftruncate(fd, (off_t)0) == 0) {
567 int length = snprintf(buffer, 64,
"%i", getpid());
570 l += write(fd, &buffer[l], length - l);
579 g_warning(
"Failed to close pidfile: '%s'", g_strerror(errno));
585 const char *fam = pango_font_description_get_family(pfd);
586 int size = pango_font_description_get_size(pfd);
587 if (fam == NULL || size == 0) {
588 g_debug(
"Pango failed to parse font: '%s'", font);
589 g_debug(
"Got family: <b>%s</b> at size: <b>%d</b>", fam ? fam :
"{unknown}",
604 int found_error = FALSE;
606 g_string_new(
"<big><b>The configuration failed to validate:</b></big>\n");
616 g_string_append_printf(
618 "\t<b>config.sorting_method</b>=%s is not a valid sorting "
619 "strategy.\nValid options are: normal or fzf.\n",
638 g_string_append_printf(msg,
639 "\t<b>config.matching</b>=%s is not a valid "
640 "matching strategy.\nValid options are: glob, "
641 "regex, fuzzy, prefix or normal.\n",
648 g_string_append_printf(msg,
649 "\t<b>config.element_height</b>=%d is invalid. An "
650 "element needs to be at least 1 line high.\n",
656 g_string_append_printf(msg,
657 "\t<b>config.location</b>=%d is invalid. Value "
658 "should be between %d and %d.\n",
669 if (name && name[0] ==
'-') {
670 int index = name[1] -
'0';
671 if (index < 5 && index > 0) {
675 g_string_append_printf(
676 msg,
"\t<b>config.monitor</b>=%s Could not find monitor.\n", name);
682 PangoFontDescription *pfd =
684 const char *fam = pango_font_description_get_family(pfd);
685 int size = pango_font_description_get_size(pfd);
686 if (fam == NULL || size == 0) {
687 g_string_append_printf(msg,
"Pango failed to parse font: '%s'\n",
689 g_string_append_printf(msg,
690 "Got font family: <b>%s</b> at size <b>%d</b>\n",
691 fam ? fam :
"{unknown}", size);
695 pango_font_description_free(pfd);
704 g_string_append(msg,
"Please update your configuration.");
709 g_string_free(msg, TRUE);
714 char **str = g_strsplit(input, G_DIR_SEPARATOR_S, -1);
715 for (
unsigned int i = 0; str && str[i]; i++) {
717 if (str[i][0] ==
'~' && str[i][1] ==
'\0') {
719 str[i] = g_strdup(g_get_home_dir());
722 else if (str[i][0] ==
'~') {
723 struct passwd *p = getpwnam(&(str[i][1]));
726 str[i] = g_strdup(p->pw_dir);
730 if (input[0] == G_DIR_SEPARATOR) {
731 str[i] = g_strdup_printf(
"%s%s", G_DIR_SEPARATOR_S, s);
736 char *retv = g_build_filenamev(str);
742 #define MIN3(a, b, c) \
743 ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
745 unsigned int levenshtein(
const char *needle,
const glong needlelen,
746 const char *haystack,
const glong haystacklen) {
747 if (needlelen == G_MAXLONG) {
751 unsigned int column[needlelen + 1];
752 for (glong y = 0; y < needlelen; y++) {
757 column[needlelen] = needlelen;
758 for (glong x = 1; x <= haystacklen; x++) {
759 const char *needles = needle;
761 gunichar haystackc = g_utf8_get_char(haystack);
763 haystackc = g_unichar_tolower(haystackc);
765 for (glong y = 1, lastdiag = x - 1; y <= needlelen; y++) {
766 gunichar needlec = g_utf8_get_char(needles);
768 needlec = g_unichar_tolower(needlec);
770 unsigned int olddiag = column[y];
771 column[y] =
MIN3(column[y] + 1, column[y - 1] + 1,
772 lastdiag + (needlec == haystackc ? 0 : 1));
774 needles = g_utf8_next_char(needles);
776 haystack = g_utf8_next_char(haystack);
778 return column[needlelen];
783 return g_convert_with_fallback(input, length,
"UTF-8",
"latin1",
"\uFFFD",
784 NULL, &slength, NULL);
791 gchar *ret = g_markup_escape_text(text, -1);
803 if (g_utf8_validate(data, length, &end)) {
804 return g_memdup(data, length + 1);
806 string = g_string_sized_new(length + 16);
810 g_string_append_len(
string, data, end - data);
812 g_string_append(
string,
"\uFFFD");
813 length -= (end - data) + 1;
815 }
while (!g_utf8_validate(data, length, &end));
818 g_string_append_len(
string, data, length);
821 return g_string_free(
string, FALSE);
829 #define FUZZY_SCORER_MAX_LENGTH 256
831 #define MIN_SCORE (INT_MIN / 2)
833 #define LEADING_GAP_SCORE -4
837 #define WORD_START_SCORE 50
839 #define NON_WORD_SCORE 40
841 #define CAMEL_SCORE (WORD_START_SCORE + GAP_SCORE - 1)
843 #define CONSECUTIVE_SCORE (WORD_START_SCORE + GAP_SCORE)
845 #define PATTERN_NON_START_MULTIPLIER 1
847 #define PATTERN_START_MULTIPLIER 2
869 if (g_unichar_islower(c)) {
872 if (g_unichar_isupper(c)) {
875 if (g_unichar_isdigit(c)) {
909 gboolean pfirst = TRUE;
911 gboolean pstart = TRUE;
913 int *score = g_malloc_n(slen,
sizeof(
int));
915 int *dp = g_malloc_n(slen,
sizeof(
int));
918 int uleft = 0, ulefts = 0, left, lefts;
919 const gchar *pit = pattern, *sit;
921 for (si = 0, sit = str; si < slen; si++, sit = g_utf8_next_char(sit)) {
927 for (pi = 0; pi < plen; pi++, pit = g_utf8_next_char(pit)) {
928 gunichar pc = g_utf8_get_char(pit), sc;
929 if (g_unichar_isspace(pc)) {
934 for (si = 0, sit = str; si < slen; si++, sit = g_utf8_next_char(sit)) {
937 sc = g_utf8_get_char(sit);
940 : g_unichar_tolower(pc) == g_unichar_tolower(sc)) {
951 pfirst = pstart = FALSE;
954 for (si = 0; si < slen; si++) {
974 char *na = g_utf8_normalize(a, -1, G_NORMALIZE_ALL_COMPOSE);
975 char *nb = g_utf8_normalize(b, -1, G_NORMALIZE_ALL_COMPOSE);
976 *g_utf8_offset_to_pointer(na, n) =
'\0';
977 *g_utf8_offset_to_pointer(nb, n) =
'\0';
978 int r = g_utf8_collate(na, nb);
985 const char *error_cmd,
987 gboolean retv = TRUE;
988 GError *error = NULL;
990 GSpawnChildSetupFunc child_setup = NULL;
991 gpointer user_data = NULL;
995 g_spawn_async(wd, args, NULL, G_SPAWN_SEARCH_PATH, child_setup, user_data,
998 char *msg = g_strdup_printf(
"Failed to execute: '%s%s'\nError: '%s'",
999 error_precmd, error_cmd, error->message);
1003 g_error_free(error);
1013 gboolean run_in_term,
1030 if (context != NULL) {
1031 if (context->
name == NULL) {
1032 context->
name = args[0];
1034 if (context->
binary == NULL) {
1035 context->
binary = args[0];
1038 gsize l = strlen(
"Launching '' via rofi") + strlen(cmd) + 1;
1039 gchar *description = g_newa(gchar, l);
1041 g_snprintf(description, l,
"Launching '%s' via rofi", cmd);
1044 if (context->
command == NULL) {
1054 g_debug(
"Opening theme, testing: %s\n", filename);
1055 if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
1060 if (g_str_has_suffix(file, ext)) {
1061 filename = g_strdup(file);
1063 filename = g_strconcat(file, ext, NULL);
1066 const char *cpath = g_get_user_config_dir();
1068 char *themep = g_build_filename(cpath,
"rofi",
"themes", filename, NULL);
1069 g_debug(
"Opening theme, testing: %s\n", themep);
1070 if (themep && g_file_test(themep, G_FILE_TEST_EXISTS)) {
1078 char *themep = g_build_filename(cpath,
"rofi", filename, NULL);
1079 g_debug(
"Opening theme, testing: %s\n", themep);
1080 if (g_file_test(themep, G_FILE_TEST_EXISTS)) {
1086 const char *datadir = g_get_user_data_dir();
1089 g_build_filename(datadir,
"rofi",
"themes", filename, NULL);
1090 g_debug(
"Opening theme, testing: %s\n", theme_path);
1092 if (g_file_test(theme_path, G_FILE_TEST_EXISTS)) {
1100 char *theme_path = g_build_filename(THEME_DIR, filename, NULL);
1102 g_debug(
"Opening theme, testing: %s\n", theme_path);
1103 if (g_file_test(theme_path, G_FILE_TEST_EXISTS)) {
1114 while (input != NULL && isblank(*input)) {
1118 if (input == NULL) {
1122 const char *sep[] = {
"-",
":"};
1123 int pythonic = (strchr(input,
':') || input[0] ==
'-') ? 1 : 0;
1126 for (
char *token = strsep(&input, sep[pythonic]); token != NULL;
1127 token = strsep(&input, sep[pythonic])) {
1129 item->
start = item->
stop = (int)strtol(token, NULL, 10);
1134 if (token[0] ==
'\0') {
1139 item->
stop = (int)strtol(token, NULL, 10);
1148 if (input == NULL) {
1151 const char *
const sep =
",";
1152 for (
char *token = strtok_r(input, sep, &endp); token != NULL;
1153 token = strtok_r(NULL, sep, &endp)) {
1158 if (
parse_pair(token, &((*list)[*length]))) {
1164 int selected_line,
const char *filter) {
1165 for (
int i = 0; format && format[i]; i++) {
1166 if (format[i] ==
'i') {
1167 fprintf(stdout,
"%d", selected_line);
1168 }
else if (format[i] ==
'd') {
1169 fprintf(stdout,
"%d", (selected_line + 1));
1170 }
else if (format[i] ==
's') {
1171 fputs(
string, stdout);
1172 }
else if (format[i] ==
'p') {
1174 pango_parse_markup(
string, -1, 0, NULL, &esc, NULL, NULL);
1179 fputs(
"invalid string", stdout);
1181 }
else if (format[i] ==
'q') {
1182 char *quote = g_shell_quote(
string);
1183 fputs(quote, stdout);
1185 }
else if (format[i] ==
'f') {
1187 fputs(filter, stdout);
1189 }
else if (format[i] ==
'F') {
1191 char *quote = g_shell_quote(filter);
1192 fputs(quote, stdout);
1196 fputc(format[i], stdout);
1199 fputc(
'\n', stdout);
1207 int num_match = g_match_info_get_match_count(info);
1209 if (num_match == 5) {
1210 match = g_match_info_fetch(info, 4);
1211 if (match != NULL) {
1213 gchar *r = g_hash_table_lookup((GHashTable *)data, match);
1216 g_string_append(res, r);
1223 else if (num_match == 4) {
1224 match = g_match_info_fetch(info, 2);
1225 if (match != NULL) {
1227 gchar *r = g_hash_table_lookup((GHashTable *)data, match);
1230 gchar *prefix = g_match_info_fetch(info, 1);
1231 g_string_append(res, prefix);
1234 g_string_append(res, r);
1236 gchar *post = g_match_info_fetch(info, 3);
1237 g_string_append(res, post);
1251 h = g_hash_table_new(g_str_hash, g_str_equal);
1253 va_start(ap,
string);
1256 char *key = va_arg(ap,
char *);
1257 if (key == (
char *)0) {
1260 char *value = va_arg(ap,
char *);
1261 g_hash_table_insert(h, key, value);
1266 g_hash_table_destroy(h);
1285 GError *error = NULL;
1289 GRegex *reg = g_regex_new(
"\\[(.*)({[-\\w]+})(.*)\\]|({[\\w-]+})",
1290 G_REGEX_UNGREEDY, 0, &error);
1291 if (error == NULL) {
1293 g_regex_replace_eval(reg,
string, -1, 0, 0,
helper_eval_cb2, h, &error);
1298 if (error != NULL) {
1299 char *msg = g_strdup_printf(
"Failed to parse: '%s'\nError: '%s'",
string,
1304 g_error_free(error);
PangoAttrList * helper_token_match_get_pango_attr(RofiHighlightColorStyle th, rofi_int_matcher **tokens, const char *input, PangoAttrList *retv)
gboolean helper_validate_font(PangoFontDescription *pfd, const char *font)
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
void cmd_set_arguments(int argc, char **argv)
int find_arg_char(const char *const key, char *val)
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
void helper_tokenize_free(rofi_int_matcher **tokens)
char helper_parse_char(const char *arg)
void rofi_output_formatted_line(const char *format, const char *string, int selected_line, const char *filter)
gboolean helper_execute(const char *wd, char **args, const char *error_precmd, const char *error_cmd, RofiHelperExecuteContext *context)
unsigned int levenshtein(const char *needle, const glong needlelen, const char *haystack, const glong haystacklen)
char * rofi_latin_to_utf8_strdup(const char *input, gssize length)
int create_pid_file(const char *pidfile)
const char ** find_arg_strv(const char *const key)
int helper_parse_setup(char *string, char ***output, int *length,...)
int execute_generator(const char *cmd)
gchar * rofi_escape_markup(gchar *text)
int find_arg_int(const char *const key, int *val)
void remove_pid_file(int fd)
int rofi_scorer_fuzzy_evaluate(const char *pattern, glong plen, const char *str, glong slen)
char * rofi_expand_path(const char *input)
int find_arg_str(const char *const key, char **val)
rofi_int_matcher ** helper_tokenize(const char *input, int case_sensitive)
char * helper_get_theme_path(const char *file, const char *ext)
int find_arg_uint(const char *const key, unsigned int *val)
char * helper_string_replace_if_exists(char *string,...)
int find_arg(const char *const key)
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
int config_sanity_check(void)
char * rofi_force_utf8(const gchar *data, ssize_t length)
void rofi_add_error_message(GString *str)
int rofi_view_error_dialog(const char *msg, int markup)
#define CONSECUTIVE_SCORE
#define LEADING_GAP_SCORE
const char *const monitor_position_entries[]
static char * utf8_helper_simplify_string(const char *s)
static enum CharClass rofi_scorer_get_character_class(gunichar c)
int utf8_strncmp(const char *a, const char *b, size_t n)
static gchar * glob_to_regex(const char *input)
#define PATTERN_NON_START_MULTIPLIER
char * helper_string_replace_if_exists_v(char *string, GHashTable *h)
#define FUZZY_SCORER_MAX_LENGTH
static gboolean helper_eval_cb2(const GMatchInfo *info, GString *res, gpointer data)
#define PATTERN_START_MULTIPLIER
static rofi_int_matcher * create_regex(const char *input, int case_sensitive)
static gchar * prefix_regex(const char *input)
static GRegex * R(const char *s, int case_sensitive)
static gchar * fuzzy_to_regex(const char *input)
static gboolean parse_pair(char *input, rofi_range_pair *item)
static int rofi_scorer_get_score_for(enum CharClass prev, enum CharClass curr)
const gchar * description
MatchingMethod matching_method
unsigned int case_sensitive
char matching_negate_char
SortingMethod sorting_method_enum
int monitor_active(workarea *mon)
void display_startup_notification(RofiHelperExecuteContext *context, GSpawnChildSetupFunc *child_setup, gpointer *user_data)