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 ssize_t str_size = (g_utf8_strlen(s, -1) * 6 + 2 + 1) *
sizeof(
char);
187 char *str = g_malloc0(str_size);
189 for (
const char *iter = s; iter && *iter; iter = g_utf8_next_char(iter)) {
190 gunichar uc = g_utf8_get_char(iter);
192 gsize dl = g_unichar_fully_decompose(uc, FALSE, buf2,
193 G_UNICHAR_MAX_DECOMPOSITION_LENGTH);
195 l = g_unichar_to_utf8(buf2[0], buf);
197 l = g_unichar_to_utf8(uc, buf);
199 memcpy(striter, buf, l);
207 static inline GRegex *
R(
const char *s,
int case_sensitive) {
211 GRegex *r = g_regex_new(
212 str, G_REGEX_OPTIMIZE | ((case_sensitive) ? 0 : G_REGEX_CASELESS), 0,
219 s, G_REGEX_OPTIMIZE | ((case_sensitive) ? 0 : G_REGEX_CASELESS), 0, NULL);
233 retv =
R(r, case_sensitive);
237 retv =
R(input, case_sensitive);
239 r = g_regex_escape_string(input, -1);
240 retv =
R(r, case_sensitive);
246 retv =
R(r, case_sensitive);
251 retv =
R(r, case_sensitive);
255 r = g_regex_escape_string(input, -1);
256 retv =
R(r, case_sensitive);
267 size_t len = strlen(input);
272 char *saveptr = NULL, *token;
284 char *str = g_strdup(input);
288 const char *
const sep =
" ";
289 for (token = strtok_r(str, sep, &saveptr); token != NULL;
290 token = strtok_r(NULL, sep, &saveptr)) {
293 retv[num_tokens + 1] = NULL;
322 const char **retv = NULL;
330 retv = g_malloc0((length + 1) *
sizeof(
char *));
344 if (val != NULL && i > 0 && i < (
stored_argc - 1)) {
353 if (val != NULL && i > 0 && i < (
stored_argc - 1)) {
361 const size_t len = strlen(arg);
367 if (len == 2 && arg[0] ==
'\\') {
400 if (len > 2 && arg[0] ==
'\\' && arg[1] ==
'x') {
401 return (
char)strtol(&arg[2], NULL, 16);
403 g_warning(
"Failed to parse character string: \"%s\"", arg);
411 if (val != NULL && i > 0 && i < (
stored_argc - 1)) {
421 PangoAttrList *retv) {
428 for (
int j = 0; tokens[j]; j++) {
429 GMatchInfo *gmi = NULL;
430 if (tokens[j]->invert) {
433 g_regex_match(tokens[j]->regex, input, G_REGEX_MATCH_PARTIAL, &gmi);
434 while (g_match_info_matches(gmi)) {
435 int count = g_match_info_get_match_count(gmi);
436 for (
int index = (
count > 1) ? 1 : 0; index <
count; index++) {
438 g_match_info_fetch_pos(gmi, index, &start, &end);
440 PangoAttribute *pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
441 pa->start_index = start;
443 pango_attr_list_insert(retv, pa);
447 pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
448 pa->start_index = start;
450 pango_attr_list_insert(retv, pa);
453 PangoAttribute *pa = pango_attr_strikethrough_new(TRUE);
454 pa->start_index = start;
456 pango_attr_list_insert(retv, pa);
460 pango_attr_variant_new(PANGO_VARIANT_SMALL_CAPS);
461 pa->start_index = start;
463 pango_attr_list_insert(retv, pa);
466 PangoAttribute *pa = pango_attr_style_new(PANGO_STYLE_ITALIC);
467 pa->start_index = start;
469 pango_attr_list_insert(retv, pa);
472 PangoAttribute *pa = pango_attr_foreground_new(
475 pa->start_index = start;
477 pango_attr_list_insert(retv, pa);
480 pa = pango_attr_foreground_alpha_new(th.
color.
alpha * 65535);
481 pa->start_index = start;
483 pango_attr_list_insert(retv, pa);
487 g_match_info_next(gmi, NULL);
489 g_match_info_free(gmi);
501 for (
int j = 0; match && tokens[j]; j++) {
502 match = g_regex_match(tokens[j]->regex, r, 0, NULL);
503 match ^= tokens[j]->invert;
507 for (
int j = 0; match && tokens[j]; j++) {
508 match = g_regex_match(tokens[j]->regex, input, 0, NULL);
509 match ^= tokens[j]->invert;
522 GError *error = NULL;
523 g_spawn_async_with_pipes(NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
524 NULL, NULL, &fd, NULL, &error);
527 char *msg = g_strdup_printf(
"Failed to execute: '%s'\nError: '%s'", cmd,
544 int fd = g_open(
pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
546 g_warning(
"Failed to create pid file: '%s'.",
pidfile);
550 int flags = fcntl(fd, F_GETFD, NULL);
552 if (fcntl(fd, F_SETFD,
flags, NULL) < 0) {
553 g_warning(
"Failed to set CLOEXEC on pidfile.");
558 int retv = flock(fd, LOCK_EX | LOCK_NB);
560 g_warning(
"Failed to set lock on pidfile: Rofi already running?");
561 g_warning(
"Got error: %d %s", retv, g_strerror(errno));
566 ssize_t l = read(fd, &buffer, 64);
568 pid_t pid = g_ascii_strtoll(buffer, NULL, 0);
571 retv = flock(fd, LOCK_EX | LOCK_NB);
585 if (ftruncate(fd, (off_t)0) == 0) {
588 int length = snprintf(buffer, 64,
"%i", getpid());
591 l += write(fd, &buffer[l], length - l);
600 g_warning(
"Failed to close pidfile: '%s'", g_strerror(errno));
606 const char *fam = pango_font_description_get_family(pfd);
607 int size = pango_font_description_get_size(pfd);
608 if (fam == NULL || size == 0) {
609 g_debug(
"Pango failed to parse font: '%s'", font);
610 g_debug(
"Got family: <b>%s</b> at size: <b>%d</b>", fam ? fam :
"{unknown}",
625 int found_error = FALSE;
627 g_string_new(
"<big><b>The configuration failed to validate:</b></big>\n");
637 g_string_append_printf(
639 "\t<b>config.sorting_method</b>=%s is not a valid sorting "
640 "strategy.\nValid options are: normal or fzf.\n",
659 g_string_append_printf(msg,
660 "\t<b>config.matching</b>=%s is not a valid "
661 "matching strategy.\nValid options are: glob, "
662 "regex, fuzzy, prefix or normal.\n",
669 g_string_append_printf(msg,
670 "\t<b>config.element_height</b>=%d is invalid. An "
671 "element needs to be at least 1 line high.\n",
677 g_string_append_printf(msg,
678 "\t<b>config.location</b>=%d is invalid. Value "
679 "should be between %d and %d.\n",
690 if (name && name[0] ==
'-') {
691 int index = name[1] -
'0';
692 if (index < 5 && index > 0) {
696 g_string_append_printf(
697 msg,
"\t<b>config.monitor</b>=%s Could not find monitor.\n", name);
708 g_string_append(msg,
"Please update your configuration.");
713 g_string_free(msg, TRUE);
718 char **str = g_strsplit(input, G_DIR_SEPARATOR_S, -1);
719 for (
unsigned int i = 0; str && str[i]; i++) {
721 if (str[i][0] ==
'~' && str[i][1] ==
'\0') {
723 str[i] = g_strdup(g_get_home_dir());
726 else if (str[i][0] ==
'~') {
727 struct passwd *p = getpwnam(&(str[i][1]));
730 str[i] = g_strdup(p->pw_dir);
734 if (input[0] == G_DIR_SEPARATOR) {
735 str[i] = g_strdup_printf(
"%s%s", G_DIR_SEPARATOR_S, s);
740 char *retv = g_build_filenamev(str);
746 #define MIN3(a, b, c) \
747 ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
749 unsigned int levenshtein(
const char *needle,
const glong needlelen,
750 const char *haystack,
const glong haystacklen) {
751 if (needlelen == G_MAXLONG) {
755 unsigned int column[needlelen + 1];
756 for (glong y = 0; y < needlelen; y++) {
761 column[needlelen] = needlelen;
762 for (glong x = 1; x <= haystacklen; x++) {
763 const char *needles = needle;
765 gunichar haystackc = g_utf8_get_char(haystack);
767 haystackc = g_unichar_tolower(haystackc);
769 for (glong y = 1, lastdiag = x - 1; y <= needlelen; y++) {
770 gunichar needlec = g_utf8_get_char(needles);
772 needlec = g_unichar_tolower(needlec);
774 unsigned int olddiag = column[y];
775 column[y] =
MIN3(column[y] + 1, column[y - 1] + 1,
776 lastdiag + (needlec == haystackc ? 0 : 1));
778 needles = g_utf8_next_char(needles);
780 haystack = g_utf8_next_char(haystack);
782 return column[needlelen];
787 return g_convert_with_fallback(input, length,
"UTF-8",
"latin1",
"\uFFFD",
788 NULL, &slength, NULL);
798 if (g_utf8_validate(data, length, &end)) {
799 return g_memdup(data, length + 1);
801 string = g_string_sized_new(length + 16);
805 g_string_append_len(
string, data, end - data);
807 g_string_append(
string,
"\uFFFD");
808 length -= (end - data) + 1;
810 }
while (!g_utf8_validate(data, length, &end));
813 g_string_append_len(
string, data, length);
816 return g_string_free(
string, FALSE);
824 #define FUZZY_SCORER_MAX_LENGTH 256
826 #define MIN_SCORE (INT_MIN / 2)
828 #define LEADING_GAP_SCORE -4
832 #define WORD_START_SCORE 50
834 #define NON_WORD_SCORE 40
836 #define CAMEL_SCORE (WORD_START_SCORE + GAP_SCORE - 1)
838 #define CONSECUTIVE_SCORE (WORD_START_SCORE + GAP_SCORE)
840 #define PATTERN_NON_START_MULTIPLIER 1
842 #define PATTERN_START_MULTIPLIER 2
864 if (g_unichar_islower(c)) {
867 if (g_unichar_isupper(c)) {
870 if (g_unichar_isdigit(c)) {
904 gboolean pfirst = TRUE;
906 gboolean pstart = TRUE;
908 int *score = g_malloc_n(slen,
sizeof(
int));
910 int *dp = g_malloc_n(slen,
sizeof(
int));
913 int uleft = 0, ulefts = 0, left, lefts;
914 const gchar *pit = pattern, *sit;
916 for (si = 0, sit = str; si < slen; si++, sit = g_utf8_next_char(sit)) {
922 for (pi = 0; pi < plen; pi++, pit = g_utf8_next_char(pit)) {
923 gunichar pc = g_utf8_get_char(pit), sc;
924 if (g_unichar_isspace(pc)) {
929 for (si = 0, sit = str; si < slen; si++, sit = g_utf8_next_char(sit)) {
932 sc = g_utf8_get_char(sit);
935 : g_unichar_tolower(pc) == g_unichar_tolower(sc)) {
946 pfirst = pstart = FALSE;
949 for (si = 0; si < slen; si++) {
969 char *na = g_utf8_normalize(a, -1, G_NORMALIZE_ALL_COMPOSE);
970 char *nb = g_utf8_normalize(b, -1, G_NORMALIZE_ALL_COMPOSE);
971 *g_utf8_offset_to_pointer(na, n) =
'\0';
972 *g_utf8_offset_to_pointer(nb, n) =
'\0';
973 int r = g_utf8_collate(na, nb);
980 const char *error_cmd,
982 gboolean retv = TRUE;
983 GError *error = NULL;
985 GSpawnChildSetupFunc child_setup = NULL;
986 gpointer user_data = NULL;
990 g_spawn_async(wd, args, NULL, G_SPAWN_SEARCH_PATH, child_setup, user_data,
993 char *msg = g_strdup_printf(
"Failed to execute: '%s%s'\nError: '%s'",
994 error_precmd, error_cmd, error->message);
1008 gboolean run_in_term,
1025 if (context != NULL) {
1026 if (context->
name == NULL) {
1027 context->
name = args[0];
1029 if (context->
binary == NULL) {
1030 context->
binary = args[0];
1033 gsize l = strlen(
"Launching '' via rofi") + strlen(cmd) + 1;
1034 gchar *description = g_newa(gchar, l);
1036 g_snprintf(description, l,
"Launching '%s' via rofi", cmd);
1039 if (context->
command == NULL) {
1049 g_debug(
"Opening theme, testing: %s\n", filename);
1050 if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
1055 if (g_str_has_suffix(file, ext)) {
1056 filename = g_strdup(file);
1058 filename = g_strconcat(file, ext, NULL);
1061 const char *cpath = g_get_user_config_dir();
1063 char *themep = g_build_filename(cpath,
"rofi",
"themes", filename, NULL);
1064 g_debug(
"Opening theme, testing: %s\n", themep);
1065 if (themep && g_file_test(themep, G_FILE_TEST_EXISTS)) {
1073 char *themep = g_build_filename(cpath,
"rofi", filename, NULL);
1074 g_debug(
"Opening theme, testing: %s\n", themep);
1075 if (g_file_test(themep, G_FILE_TEST_EXISTS)) {
1081 const char *datadir = g_get_user_data_dir();
1084 g_build_filename(datadir,
"rofi",
"themes", filename, NULL);
1085 g_debug(
"Opening theme, testing: %s\n", theme_path);
1087 if (g_file_test(theme_path, G_FILE_TEST_EXISTS)) {
1095 char *theme_path = g_build_filename(THEME_DIR, filename, NULL);
1097 g_debug(
"Opening theme, testing: %s\n", theme_path);
1098 if (g_file_test(theme_path, G_FILE_TEST_EXISTS)) {
1109 while (input != NULL && isblank(*input)) {
1113 if (input == NULL) {
1117 const char *sep[] = {
"-",
":"};
1118 int pythonic = (strchr(input,
':') || input[0] ==
'-') ? 1 : 0;
1121 for (
char *token = strsep(&input, sep[pythonic]); token != NULL;
1122 token = strsep(&input, sep[pythonic])) {
1124 item->
start = item->
stop = (int)strtol(token, NULL, 10);
1129 if (token[0] ==
'\0') {
1134 item->
stop = (int)strtol(token, NULL, 10);
1143 if (input == NULL) {
1146 const char *
const sep =
",";
1147 for (
char *token = strtok_r(input, sep, &endp); token != NULL;
1148 token = strtok_r(NULL, sep, &endp)) {
1153 if (
parse_pair(token, &((*list)[*length]))) {
1159 int selected_line,
const char *filter) {
1160 for (
int i = 0; format && format[i]; i++) {
1161 if (format[i] ==
'i') {
1162 fprintf(stdout,
"%d", selected_line);
1163 }
else if (format[i] ==
'd') {
1164 fprintf(stdout,
"%d", (selected_line + 1));
1165 }
else if (format[i] ==
's') {
1166 fputs(
string, stdout);
1167 }
else if (format[i] ==
'p') {
1169 pango_parse_markup(
string, -1, 0, NULL, &esc, NULL, NULL);
1174 fputs(
"invalid string", stdout);
1176 }
else if (format[i] ==
'q') {
1177 char *quote = g_shell_quote(
string);
1178 fputs(quote, stdout);
1180 }
else if (format[i] ==
'f') {
1182 fputs(filter, stdout);
1184 }
else if (format[i] ==
'F') {
1186 char *quote = g_shell_quote(filter);
1187 fputs(quote, stdout);
1191 fputc(format[i], stdout);
1194 fputc(
'\n', stdout);
1202 int num_match = g_match_info_get_match_count(info);
1204 if (num_match == 5) {
1205 match = g_match_info_fetch(info, 4);
1206 if (match != NULL) {
1208 gchar *r = g_hash_table_lookup((GHashTable *)data, match);
1211 g_string_append(res, r);
1218 else if (num_match == 4) {
1219 match = g_match_info_fetch(info, 2);
1220 if (match != NULL) {
1222 gchar *r = g_hash_table_lookup((GHashTable *)data, match);
1225 gchar *prefix = g_match_info_fetch(info, 1);
1226 g_string_append(res, prefix);
1229 g_string_append(res, r);
1231 gchar *post = g_match_info_fetch(info, 3);
1232 g_string_append(res, post);
1246 h = g_hash_table_new(g_str_hash, g_str_equal);
1248 va_start(ap,
string);
1251 char *key = va_arg(ap,
char *);
1252 if (key == (
char *)0) {
1255 char *value = va_arg(ap,
char *);
1256 g_hash_table_insert(h, key, value);
1261 g_hash_table_destroy(h);
1280 GError *error = NULL;
1284 GRegex *reg = g_regex_new(
"\\[(.*)({[-\\w]+})(.*)\\]|({[\\w-]+})",
1285 G_REGEX_UNGREEDY, 0, &error);
1286 if (error == NULL) {
1288 g_regex_replace_eval(reg,
string, -1, 0, 0,
helper_eval_cb2, h, &error);
1293 if (error != NULL) {
1294 char *msg = g_strdup_printf(
"Failed to parse: '%s'\nError: '%s'",
string,
1299 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)
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)
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)
int create_pid_file(const char *pidfile, gboolean kill_running)
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)