rofi  1.7.0
rofi.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2012 Sean Pringle <sean.pringle@gmail.com>
6  * Copyright © 2013-2021 Qball Cow <qball@gmpclient.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28 
30 #define G_LOG_DOMAIN "Rofi"
31 
32 #include "config.h"
33 #include <errno.h>
34 #include <gmodule.h>
35 #include <locale.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sysexits.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <xcb/xcb.h>
44 
45 #include <glib-unix.h>
46 
47 #include <libgwater-xcb.h>
48 
49 #ifdef USE_NK_GIT_VERSION
50 #include "nkutils-git-version.h"
51 #ifdef NK_GIT_VERSION
52 #define GIT_VERSION NK_GIT_VERSION
53 #endif
54 #endif
55 
56 #include "resources.h"
57 
58 #include "display.h"
59 #include "rofi.h"
60 #include "settings.h"
61 
62 #include "dialogs/dialogs.h"
63 #include "helper.h"
64 #include "mode.h"
65 #include "widgets/textbox.h"
66 #include "xrmoptions.h"
67 
68 #include "view-internal.h"
69 #include "view.h"
70 
71 #include "rofi-icon-fetcher.h"
72 #include "theme.h"
73 
74 #include "timings.h"
75 
76 // Plugin abi version.
77 // TODO: move this check to mode.c
78 #include "mode-private.h"
79 
81 char *pidfile = NULL;
83 const char *cache_dir = NULL;
84 
86 GList *list_of_error_msgs = NULL;
87 
88 static void rofi_collect_modi_destroy(void);
89 void rofi_add_error_message(GString *str) {
90  list_of_error_msgs = g_list_append(list_of_error_msgs, str);
91 }
93  if (list_of_error_msgs) {
94  for (GList *iter = g_list_first(list_of_error_msgs); iter != NULL;
95  iter = g_list_next(iter)) {
96  g_string_free((GString *)iter->data, TRUE);
97  }
98  g_list_free(list_of_error_msgs);
99  list_of_error_msgs = NULL;
100  }
101 }
102 
104 G_MODULE_EXPORT char *config_path = NULL;
107 Mode **modi = NULL;
108 
112 unsigned int num_available_modi = 0;
114 unsigned int num_modi = 0;
116 unsigned int curr_switcher = 0;
117 
119 NkBindings *bindings = NULL;
120 
122 GMainLoop *main_loop = NULL;
123 
125 static int dmenu_mode = FALSE;
127 int return_code = EXIT_SUCCESS;
128 
129 void process_result(RofiViewState *state);
130 
131 void rofi_set_return_code(int code) { return_code = code; }
132 
133 unsigned int rofi_get_num_enabled_modi(void) { return num_modi; }
134 
135 const Mode *rofi_get_mode(unsigned int index) { return modi[index]; }
136 
144 static int switcher_get(const char *name) {
145  for (unsigned int i = 0; i < num_modi; i++) {
146  if (strcmp(mode_get_name(modi[i]), name) == 0) {
147  return i;
148  }
149  }
150  return -1;
151 }
152 
156 static void teardown(int pfd) {
157  g_debug("Teardown");
158  // Cleanup font setup.
159  textbox_cleanup();
160 
162 
163  // Cleanup view
165  // Cleanup pid file.
166  remove_pid_file(pfd);
167 }
168 static void run_switcher(ModeMode mode) {
169  // Otherwise check if requested mode is enabled.
170  for (unsigned int i = 0; i < num_modi; i++) {
171  if (!mode_init(modi[i])) {
172  GString *str = g_string_new("Failed to initialize the mode: ");
173  g_string_append(str, modi[i]->name);
174  g_string_append(str, "\n");
175 
177  g_string_free(str, FALSE);
178  break;
179  }
180  }
181  // Error dialog must have been created.
182  if (rofi_view_get_active() != NULL) {
183  return;
184  }
185  curr_switcher = mode;
186  RofiViewState *state =
188 
189  // User can pre-select a row.
190  if (find_arg("-selected-row") >= 0) {
191  unsigned int sr = 0;
192  find_arg_uint("-selected-row", &(sr));
193  rofi_view_set_selected_line(state, sr);
194  }
195  if (state) {
196  rofi_view_set_active(state);
197  }
198  if (rofi_view_get_active() == NULL) {
199  g_main_loop_quit(main_loop);
200  }
201 }
203  Mode *sw = state->sw;
204  // rofi_view_set_active ( NULL );
205  if (sw != NULL) {
206  unsigned int selected_line = rofi_view_get_selected_line(state);
207  ;
208  MenuReturn mretv = rofi_view_get_return_value(state);
209  char *input = g_strdup(rofi_view_get_user_input(state));
210  ModeMode retv = mode_result(sw, mretv, &input, selected_line);
211  {
212  if (state->text) {
213  if (input == NULL) {
214  textbox_text(state->text, "");
215  } else if (strcmp(rofi_view_get_user_input(state), input) != 0) {
216  textbox_text(state->text, input);
217  textbox_cursor_end(state->text);
218  }
219  }
220  }
221  g_free(input);
222 
223  ModeMode mode = curr_switcher;
224  // Find next enabled
225  if (retv == NEXT_DIALOG) {
226  mode = (mode + 1) % num_modi;
227  } else if (retv == PREVIOUS_DIALOG) {
228  if (mode == 0) {
229  mode = num_modi - 1;
230  } else {
231  mode = (mode - 1) % num_modi;
232  }
233  } else if (retv == RELOAD_DIALOG) {
234  // do nothing.
235  } else if (retv == RESET_DIALOG) {
236  rofi_view_clear_input(state);
237  } else if (retv < MODE_EXIT) {
238  mode = (retv) % num_modi;
239  } else {
240  mode = retv;
241  }
242  if (mode != MODE_EXIT) {
246  rofi_view_switch_mode(state, modi[mode]);
247  curr_switcher = mode;
248  return;
249  }
250  // On exit, free current view, and pop to one above.
252  rofi_view_free(state);
253  return;
254  }
255  // rofi_view_set_active ( NULL );
257  rofi_view_free(state);
258 }
259 
263 static void print_list_of_modi(int is_term) {
264  for (unsigned int i = 0; i < num_available_modi; i++) {
265  gboolean active = FALSE;
266  for (unsigned int j = 0; j < num_modi; j++) {
267  if (modi[j] == available_modi[i]) {
268  active = TRUE;
269  break;
270  }
271  }
272  printf(" * %s%s%s%s\n", active ? "+" : "",
273  is_term ? (active ? color_green : color_red) : "",
274  available_modi[i]->name, is_term ? color_reset : "");
275  }
276 }
277 static void print_main_application_options(int is_term) {
278  print_help_msg("-no-config", "",
279  "Do not load configuration, use default values.", NULL,
280  is_term);
281  print_help_msg("-v,-version", "", "Print the version number and exit.", NULL,
282  is_term);
283  print_help_msg("-dmenu", "", "Start in dmenu mode.", NULL, is_term);
284  print_help_msg("-display", "[string]", "X server to contact.", "${DISPLAY}",
285  is_term);
286  print_help_msg("-h,-help", "", "This help message.", NULL, is_term);
287  print_help_msg("-e", "[string]",
288  "Show a dialog displaying the passed message and exit.", NULL,
289  is_term);
290  print_help_msg("-markup", "", "Enable pango markup where possible.", NULL,
291  is_term);
292  print_help_msg("-normal-window", "",
293  "Behave as a normal window. (experimental)", NULL, is_term);
294  print_help_msg("-show", "[mode]",
295  "Show the mode 'mode' and exit. The mode has to be enabled.",
296  NULL, is_term);
297  print_help_msg("-no-lazy-grab", "",
298  "Disable lazy grab that, when fail to grab keyboard, does not "
299  "block but retry later.",
300  NULL, is_term);
301  print_help_msg("-no-plugins", "", "Disable loading of external plugins.",
302  NULL, is_term);
303  print_help_msg("-plugin-path", "",
304  "Directory used to search for rofi plugins. *DEPRECATED*",
305  NULL, is_term);
306  print_help_msg("-dump-config", "",
307  "Dump the current configuration in rasi format and exit.",
308  NULL, is_term);
309  print_help_msg("-dump-theme", "",
310  "Dump the current theme in rasi format and exit.", NULL,
311  is_term);
312 }
313 static void help(G_GNUC_UNUSED int argc, char **argv) {
314  int is_term = isatty(fileno(stdout));
315  printf("%s usage:\n", argv[0]);
316  printf("\t%s [-options ...]\n\n", argv[0]);
317  printf("Command line only options:\n");
319  printf("DMENU command line options:\n");
321  printf("Global options:\n");
322  print_options();
323  printf("\n");
325  printf("\n");
326  printf("Detected modi:\n");
327  print_list_of_modi(is_term);
328  printf("\n");
329  printf("Compile time options:\n");
330 #ifdef WINDOW_MODE
331  printf("\t* window %senabled%s\n", is_term ? color_green : "",
332  is_term ? color_reset : "");
333 #else
334  printf("\t* window %sdisabled%s\n", is_term ? color_red : "",
335  is_term ? color_reset : "");
336 #endif
337 #ifdef ENABLE_DRUN
338  printf("\t* drun %senabled%s\n", is_term ? color_green : "",
339  is_term ? color_reset : "");
340 #else
341  printf("\t* drun %sdisabled%s\n", is_term ? color_red : "",
342  is_term ? color_reset : "");
343 #endif
344 #ifdef ENABLE_GCOV
345  printf("\t* gcov %senabled%s\n", is_term ? color_green : "",
346  is_term ? color_reset : "");
347 #else
348  printf("\t* gcov %sdisabled%s\n", is_term ? color_red : "",
349  is_term ? color_reset : "");
350 #endif
351 #ifdef ENABLE_ASAN
352  printf("\t* asan %senabled%s\n", is_term ? color_green : "",
353  is_term ? color_reset : "");
354 #else
355  printf("\t* asan %sdisabled%s\n", is_term ? color_red : "",
356  is_term ? color_reset : "");
357 #endif
358  printf("\n");
359  printf("For more information see: %sman rofi%s\n", is_term ? color_bold : "",
360  is_term ? color_reset : "");
361 #ifdef GIT_VERSION
362  printf(" Version: %s" GIT_VERSION "%s\n",
363  is_term ? color_bold : "", is_term ? color_reset : "");
364 #else
365  printf(" Version: %s" VERSION "%s\n",
366  is_term ? color_bold : "", is_term ? color_reset : "");
367 #endif
368  printf(" Bugreports: %s" PACKAGE_BUGREPORT "%s\n",
369  is_term ? color_bold : "", is_term ? color_reset : "");
370  printf(" Support: %s" PACKAGE_URL "%s\n",
371  is_term ? color_bold : "", is_term ? color_reset : "");
372  printf(" %s#rofi @ libera.chat%s\n",
373  is_term ? color_bold : "", is_term ? color_reset : "");
374  if (find_arg("-no-config") < 0) {
375  if (config_path) {
376  printf(" Configuration file: %s%s%s\n", is_term ? color_bold : "",
377  config_path, is_term ? color_reset : "");
378  }
379  } else {
380  printf(" Configuration file: %sDisabled%s\n",
381  is_term ? color_bold : "", is_term ? color_reset : "");
382  }
383 }
384 
385 static void help_print_disabled_mode(const char *mode) {
386  int is_term = isatty(fileno(stdout));
387  // Only output to terminal
388  if (is_term) {
389  fprintf(stderr, "Mode %s%s%s is not enabled. I have enabled it for now.\n",
390  color_red, mode, color_reset);
391  fprintf(stderr,
392  "Please consider adding %s%s%s to the list of enabled modi: "
393  "%smodi: %s%s%s,%s%s.\n",
395  color_red, mode, color_reset);
396  }
397 }
398 static void help_print_mode_not_found(const char *mode) {
399  GString *str = g_string_new("");
400  g_string_printf(str, "Mode %s is not found.\nThe following modi are known:\n",
401  mode);
402  for (unsigned int i = 0; i < num_available_modi; i++) {
403  gboolean active = FALSE;
404  for (unsigned int j = 0; j < num_modi; j++) {
405  if (modi[j] == available_modi[i]) {
406  active = TRUE;
407  break;
408  }
409  }
410  g_string_append_printf(str, " * %s%s\n", active ? "+" : "",
411  available_modi[i]->name);
412  }
414 }
415 static void help_print_no_arguments(void) {
416  int is_term = isatty(fileno(stdout));
417  // Daemon mode
418  fprintf(stderr, "Rofi is unsure what to show.\n");
419  fprintf(stderr, "Please specify the mode you want to show.\n\n");
420  fprintf(stderr, " %srofi%s -show %s{mode}%s\n\n",
421  is_term ? color_bold : "", is_term ? color_reset : "",
422  is_term ? color_green : "", is_term ? color_reset : "");
423  fprintf(stderr, "The following modi are enabled:\n");
424  for (unsigned int j = 0; j < num_modi; j++) {
425  fprintf(stderr, " * %s%s%s\n", is_term ? color_green : "", modi[j]->name,
426  is_term ? color_reset : "");
427  }
428  fprintf(stderr, "\nThe following can be enabled:\n");
429  for (unsigned int i = 0; i < num_available_modi; i++) {
430  gboolean active = FALSE;
431  for (unsigned int j = 0; j < num_modi; j++) {
432  if (modi[j] == available_modi[i]) {
433  active = TRUE;
434  break;
435  }
436  }
437  if (!active) {
438  fprintf(stderr, " * %s%s%s\n", is_term ? color_red : "",
439  available_modi[i]->name, is_term ? color_reset : "");
440  }
441  }
442  fprintf(stderr,
443  "\nTo activate a mode, add it to the list of modi in the %smodi%s "
444  "setting.\n",
445  is_term ? color_green : "", is_term ? color_reset : "");
446 }
447 
451 static void cleanup() {
452  for (unsigned int i = 0; i < num_modi; i++) {
453  mode_destroy(modi[i]);
454  }
456  if (main_loop != NULL) {
457  g_main_loop_unref(main_loop);
458  main_loop = NULL;
459  }
460  // Cleanup
461  display_cleanup();
462 
463  nk_bindings_free(bindings);
464 
465  // Cleaning up memory allocated by the Xresources file.
467  g_free(modi);
468 
469  g_free(config_path);
470 
472 
473  if (rofi_theme) {
475  rofi_theme = NULL;
476  }
477  TIMINGS_STOP();
480 
481  if (rofi_configuration) {
483  rofi_configuration = NULL;
484  }
485 }
486 
491 Mode *rofi_collect_modi_search(const char *name) {
492  for (unsigned int i = 0; i < num_available_modi; i++) {
493  if (g_strcmp0(name, available_modi[i]->name) == 0) {
494  return available_modi[i];
495  }
496  }
497  return NULL;
498 }
504 static gboolean rofi_collect_modi_add(Mode *mode) {
505  Mode *m = rofi_collect_modi_search(mode->name);
506  if (m == NULL) {
508  g_realloc(available_modi, sizeof(Mode *) * (num_available_modi + 1));
509  // Set mode.
512  return TRUE;
513  }
514  return FALSE;
515 }
516 
517 static void rofi_collect_modi_dir(const char *base_dir) {
518  g_debug("Looking into: %s for plugins", base_dir);
519  GDir *dir = g_dir_open(base_dir, 0, NULL);
520  if (dir) {
521  const char *dn = NULL;
522  while ((dn = g_dir_read_name(dir))) {
523  if (!g_str_has_suffix(dn, G_MODULE_SUFFIX)) {
524  continue;
525  }
526  char *fn = g_build_filename(base_dir, dn, NULL);
527  g_debug("Trying to open: %s plugin", fn);
528  GModule *mod =
529  g_module_open(fn, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
530  if (mod) {
531  Mode *m = NULL;
532  if (g_module_symbol(mod, "mode", (gpointer *)&m)) {
533  if (m->abi_version != ABI_VERSION) {
534  g_warning("ABI version of plugin: '%s' does not match: %08X "
535  "expecting: %08X",
536  dn, m->abi_version, ABI_VERSION);
537  g_module_close(mod);
538  } else {
539  m->module = mod;
540  if (!rofi_collect_modi_add(m)) {
541  g_module_close(mod);
542  }
543  }
544  } else {
545  g_warning("Symbol 'mode' not found in module: %s", dn);
546  g_module_close(mod);
547  }
548  } else {
549  g_warning("Failed to open 'mode' plugin: '%s', error: %s", dn,
550  g_module_error());
551  }
552  g_free(fn);
553  }
554  g_dir_close(dir);
555  }
556 }
557 
561 static void rofi_collect_modi(void) {
562 #ifdef WINDOW_MODE
563  rofi_collect_modi_add(&window_mode);
564  rofi_collect_modi_add(&window_mode_cd);
565 #endif
568 #ifdef ENABLE_DRUN
570 #endif
574 
575  if (find_arg("-no-plugins") < 0) {
576  find_arg_str("-plugin-path", &(config.plugin_path));
577  g_debug("Parse plugin path: %s", config.plugin_path);
579  /* ROFI_PLUGIN_PATH */
580  const char *path = g_getenv("ROFI_PLUGIN_PATH");
581  if (path != NULL) {
582  gchar **paths = g_strsplit(path, ":", -1);
583  for (unsigned int i = 0; paths[i]; i++) {
584  rofi_collect_modi_dir(paths[i]);
585  }
586  g_strfreev(paths);
587  }
588  }
589 }
590 
594 static void rofi_collect_modi_setup(void) {
595  for (unsigned int i = 0; i < num_available_modi; i++) {
597  }
598 }
599 static void rofi_collect_modi_destroy(void) {
600  for (unsigned int i = 0; i < num_available_modi; i++) {
601  if (available_modi[i]->module) {
602  GModule *mod = available_modi[i]->module;
603  available_modi[i] = NULL;
604  g_module_close(mod);
605  }
606  if (available_modi[i]) {
607  mode_free(&(available_modi[i]));
608  }
609  }
610  g_free(available_modi);
611  available_modi = NULL;
612  num_available_modi = 0;
613 }
614 
622 static int add_mode(const char *token) {
623  unsigned int index = num_modi;
624  // Resize and add entry.
625  modi = (Mode **)g_realloc(modi, sizeof(Mode *) * (num_modi + 1));
626 
627  Mode *mode = rofi_collect_modi_search(token);
628  if (mode) {
629  modi[num_modi] = mode;
630  num_modi++;
631  } else if (script_switcher_is_valid(token)) {
632  // If not build in, use custom modi.
633  Mode *sw = script_switcher_parse_setup(token);
634  if (sw != NULL) {
635  // Add to available list, so combi can find it.
637  mode_set_config(sw);
638  modi[num_modi] = sw;
639  num_modi++;
640  }
641  }
642  return (index == num_modi) ? -1 : (int)index;
643 }
644 static gboolean setup_modi(void) {
645  const char *const sep = ",#";
646  char *savept = NULL;
647  // Make a copy, as strtok will modify it.
648  char *switcher_str = g_strdup(config.modi);
649  // Split token on ','. This modifies switcher_str.
650  for (char *token = strtok_r(switcher_str, sep, &savept); token != NULL;
651  token = strtok_r(NULL, sep, &savept)) {
652  if (add_mode(token) == -1) {
654  }
655  }
656  // Free string that was modified by strtok_r
657  g_free(switcher_str);
658  return FALSE;
659 }
660 
665 void rofi_quit_main_loop(void) { g_main_loop_quit(main_loop); }
666 
667 static gboolean main_loop_signal_handler_int(G_GNUC_UNUSED gpointer data) {
668  // Break out of loop.
669  g_main_loop_quit(main_loop);
670  return G_SOURCE_CONTINUE;
671 }
672 static void show_error_dialog() {
673  GString *emesg =
674  g_string_new("The following errors were detected when starting rofi:\n");
675  GList *iter = g_list_first(list_of_error_msgs);
676  int index = 0;
677  for (; iter != NULL && index < 2; iter = g_list_next(iter)) {
678  GString *msg = (GString *)(iter->data);
679  g_string_append(emesg, "\n\n");
680  g_string_append(emesg, msg->str);
681  index++;
682  }
683  if (g_list_length(iter) > 1) {
684  g_string_append_printf(emesg, "\nThere are <b>%d</b> more errors.",
685  g_list_length(iter) - 1);
686  }
688  g_string_free(emesg, TRUE);
689  rofi_set_return_code(EX_DATAERR);
690 }
691 
692 static gboolean startup(G_GNUC_UNUSED gpointer data) {
693  TICK_N("Startup");
694  // flags to run immediately and exit
695  char *sname = NULL;
696  char *msg = NULL;
697  MenuFlags window_flags = MENU_NORMAL;
698 
699  if (find_arg("-normal-window") >= 0) {
700  window_flags |= MENU_NORMAL_WINDOW;
701  }
702  TICK_N("Grab keyboard");
703  __create_window(window_flags);
704  TICK_N("Create Window");
705  // Parse the keybindings.
706  TICK_N("Parse ABE");
707  // Sanity check
709  TICK_N("Config sanity check");
710 
711  if (list_of_error_msgs != NULL) {
713  return G_SOURCE_REMOVE;
714  }
715  // Dmenu mode.
716  if (dmenu_mode == TRUE) {
717  // force off sidebar mode:
718  config.sidebar_mode = FALSE;
719  int retv = dmenu_switcher_dialog();
720  if (retv) {
721  rofi_set_return_code(EXIT_SUCCESS);
722  // Directly exit.
723  g_main_loop_quit(main_loop);
724  }
725  } else if (find_arg_str("-e", &(msg))) {
726  int markup = FALSE;
727  if (find_arg("-markup") >= 0) {
728  markup = TRUE;
729  }
730  if (!rofi_view_error_dialog(msg, markup)) {
731  g_main_loop_quit(main_loop);
732  }
733  } else if (find_arg_str("-show", &sname) == TRUE) {
734  int index = switcher_get(sname);
735  if (index < 0) {
736  // Add it to the list
737  index = add_mode(sname);
738  // Complain
739  if (index >= 0) {
741  }
742  // Run it anyway if found.
743  }
744  if (index >= 0) {
745  run_switcher(index);
746  } else {
749  return G_SOURCE_REMOVE;
750  }
751  } else if (find_arg("-show") >= 0 && num_modi > 0) {
752  run_switcher(0);
753  } else {
755 
756  g_main_loop_quit(main_loop);
757  }
758 
759  return G_SOURCE_REMOVE;
760 }
761 
762 static gboolean record(G_GNUC_UNUSED void *data) {
764  return G_SOURCE_CONTINUE;
765 }
774 int main(int argc, char *argv[]) {
775  TIMINGS_START();
776 
777  cmd_set_arguments(argc, argv);
778 
779  // Version
780  if (find_arg("-v") >= 0 || find_arg("-version") >= 0) {
781 #ifdef GIT_VERSION
782  g_print("Version: " GIT_VERSION "\n");
783 #else
784  g_print("Version: " VERSION "\n");
785 #endif
786  return EXIT_SUCCESS;
787  }
788 
789  if (find_arg("-rasi-validate") >= 0) {
790  char *str = NULL;
791  find_arg_str("-rasi-validate", &str);
792  if (str != NULL) {
793  int retv = rofi_theme_rasi_validate(str);
794  cleanup();
795  return retv;
796  }
797  fprintf(stderr, "Usage: %s -rasi-validate my-theme.rasi", argv[0]);
798  return EXIT_FAILURE;
799  }
800 
801  {
802  const char *ro_pid = g_getenv("ROFI_OUTSIDE");
803  if (ro_pid != NULL) {
804  int ro_pidi = g_ascii_strtoll(ro_pid, NULL, 0);
805  if (kill(ro_pidi, 0) == 0) {
806  printf("Do not launch rofi from inside rofi.\r\n");
807  return EXIT_FAILURE;
808  }
809  }
810  }
811 
812  // Detect if we are in dmenu mode.
813  // This has two possible causes.
814  // 1 the user specifies it on the command-line.
815  if (find_arg("-dmenu") >= 0) {
816  dmenu_mode = TRUE;
817  }
818  // 2 the binary that executed is called dmenu (e.g. symlink to rofi)
819  else {
820  // Get the base name of the executable called.
821  char *base_name = g_path_get_basename(argv[0]);
822  const char *const dmenu_str = "dmenu";
823  dmenu_mode = (strcmp(base_name, dmenu_str) == 0);
824  // Free the basename for dmenu detection.
825  g_free(base_name);
826  }
827  TICK();
828 
829  // Create pid file path.
830  const char *path = g_get_user_runtime_dir();
831  if (path) {
832  if (g_mkdir_with_parents(path, 0700) < 0) {
833  g_warning("Failed to create user runtime directory: %s with error: %s",
834  path, g_strerror(errno));
835  pidfile = g_build_filename(g_get_home_dir(), ".rofi.pid", NULL);
836  } else {
837  pidfile = g_build_filename(path, "rofi.pid", NULL);
838  }
839  }
840  config_parser_add_option(xrm_String, "pid", (void **)&pidfile,
841  "Pidfile location");
842 
844  if (find_arg("-no-default-config") < 0) {
845  GBytes *theme_data = g_resource_lookup_data(
846  resources_get_resource(), "/org/qtools/rofi/default_configuration.rasi",
847  G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
848  if (theme_data) {
849  const char *theme = g_bytes_get_data(theme_data, NULL);
850  if (rofi_theme_parse_string((const char *)theme)) {
851  g_warning("Failed to parse default configuration. Giving up..");
852  if (list_of_error_msgs) {
853  for (GList *iter = g_list_first(list_of_error_msgs); iter != NULL;
854  iter = g_list_next(iter)) {
855  g_warning("Error: %s%s%s", color_bold, ((GString *)iter->data)->str,
856  color_reset);
857  }
858  }
859  rofi_configuration = NULL;
860  cleanup();
861  return EXIT_FAILURE;
862  }
863  g_bytes_unref(theme_data);
864  }
865  }
866 
867  if (find_arg("-config") < 0) {
868  const char *cpath = g_get_user_config_dir();
869  if (cpath) {
870  config_path = g_build_filename(cpath, "rofi", "config.rasi", NULL);
871  }
872  } else {
873  char *c = NULL;
874  find_arg_str("-config", &c);
876  }
877 
878  TICK();
879  if (setlocale(LC_ALL, "") == NULL) {
880  g_warning("Failed to set locale.");
881  cleanup();
882  return EXIT_FAILURE;
883  }
884 
885  TICK_N("Setup Locale");
887  TICK_N("Collect MODI");
889  TICK_N("Setup MODI");
890 
891  main_loop = g_main_loop_new(NULL, FALSE);
892 
893  TICK_N("Setup mainloop");
894 
895  bindings = nk_bindings_new(0);
896  TICK_N("NK Bindings");
897 
899  g_warning("Connection has error");
900  cleanup();
901  return EXIT_FAILURE;
902  }
903  TICK_N("Setup Display");
904 
905  // Setup keybinding
906  setup_abe();
907  TICK_N("Setup abe");
908 
909  if (find_arg("-no-config") < 0) {
910  // Load distro default settings
911  gboolean found_system = FALSE;
912  const char *const *dirs = g_get_system_config_dirs();
913  if (dirs) {
914  for (unsigned int i = 0; !found_system && dirs[i]; i++) {
916  gchar *etc = g_build_filename(dirs[i], "rofi.rasi", NULL);
917  g_debug("Look for default config file: %s", etc);
918  if (g_file_test(etc, G_FILE_TEST_IS_REGULAR)) {
919  g_debug("Parsing: %s", etc);
921  found_system = TRUE;
922  }
923  g_free(etc);
924  }
925  }
926  if (!found_system) {
928  gchar *etc = g_build_filename(SYSCONFDIR, "rofi.rasi", NULL);
929  g_debug("Look for default config file: %s", etc);
930  if (g_file_test(etc, G_FILE_TEST_IS_REGULAR)) {
931  g_debug("Look for default config file: %s", etc);
933  }
934  g_free(etc);
935  }
936 
937  if (config_path && g_file_test(config_path, G_FILE_TEST_IS_REGULAR)) {
940  rofi_theme = NULL;
941  }
942  }
943  }
944  find_arg_str("-theme", &(config.theme));
945  if (config.theme) {
946  TICK_N("Parse theme");
949  g_warning("Failed to parse theme: \"%s\"", config.theme);
950  // TODO: instantiate fallback theme.?
952  rofi_theme = NULL;
953  }
954  TICK_N("Parsed theme");
955  }
956  // Parse command line for settings, independent of other -no-config.
958  TICK_N("Load cmd config ");
959 
961 
962  // Get the path to the cache dir.
963  cache_dir = g_get_user_cache_dir();
964 
965  if (config.cache_dir != NULL) {
967  }
968 
969  if (g_mkdir_with_parents(cache_dir, 0700) < 0) {
970  g_warning("Failed to create cache directory: %s", g_strerror(errno));
971  return EXIT_FAILURE;
972  }
973 
975  char *windowid = NULL;
976  if (!dmenu_mode) {
977  // setup_modi
978  if (setup_modi()) {
979  cleanup();
980  return EXIT_FAILURE;
981  }
982  TICK_N("Setup Modi");
983  } else {
984  // Hack for dmenu compatibility.
985  if (find_arg_str("-w", &windowid) == TRUE) {
986  config.monitor = g_strdup_printf("wid:%s", windowid);
987  windowid = config.monitor;
988  }
989  }
990 
994  const char **theme_str = find_arg_strv("-theme-str");
995  if (theme_str) {
996  for (int index = 0; theme_str && theme_str[index]; index++) {
997  if (rofi_theme_parse_string(theme_str[index])) {
998  g_warning("Failed to parse -theme-str option: \"%s\"",
999  theme_str[index]);
1001  rofi_theme = NULL;
1002  }
1003  }
1004  g_free(theme_str);
1005  }
1006 
1007  if (find_arg("-dump-theme") >= 0) {
1009  cleanup();
1010  return EXIT_SUCCESS;
1011  }
1012  if (find_arg("-dump-config") >= 0) {
1013  config_parse_dump_config_rasi_format(stdout, FALSE);
1014  cleanup();
1015  return EXIT_SUCCESS;
1016  }
1017  // Dump.
1018  // catch help request
1019  if (find_arg("-h") >= 0 || find_arg("-help") >= 0 ||
1020  find_arg("--help") >= 0) {
1021  help(argc, argv);
1022  cleanup();
1023  return EXIT_SUCCESS;
1024  }
1025 
1026  unsigned int interval = 1;
1027  if (find_arg_uint("-record-screenshots", &interval)) {
1028  g_timeout_add(1000 / (double)interval, record, NULL);
1029  }
1030  if (find_arg("-benchmark-ui") >= 0) {
1031  config.benchmark_ui = TRUE;
1032  }
1033 
1035  TICK_N("Workers initialize");
1037  TICK_N("Icon fetcher initialize");
1038 
1039  // Create pid file
1040  int pfd = create_pid_file(pidfile);
1041  TICK_N("Pid file created");
1042  if (pfd < 0) {
1043  cleanup();
1044  return EXIT_FAILURE;
1045  }
1046  textbox_setup();
1047  TICK_N("Text box setup");
1048 
1049  if (!display_late_setup()) {
1050  g_warning("Failed to properly finish display setup");
1051  cleanup();
1052  return EXIT_FAILURE;
1053  }
1054  TICK_N("Setup late Display");
1055 
1057  TICK_N("Theme setup");
1058 
1059  // Setup signal handling sources.
1060  // SIGINT
1061  g_unix_signal_add(SIGINT, main_loop_signal_handler_int, NULL);
1062 
1063  g_idle_add(startup, NULL);
1064 
1065  // Start mainloop.
1066  g_main_loop_run(main_loop);
1067  teardown(pfd);
1068  cleanup();
1069 
1070  /* dirty hack */
1071  g_free(windowid);
1072  return return_code;
1073 }
1074 
1076 extern GList *list_of_error_msgs;
1077 int rofi_theme_rasi_validate(const char *filename) {
1078  rofi_theme_parse_file(filename);
1079  if (list_of_error_msgs == NULL) {
1080  return EXIT_SUCCESS;
1081  }
1082 
1083  for (GList *iter = g_list_first(list_of_error_msgs); iter != NULL;
1084  iter = g_list_next(iter)) {
1085  fputs(((GString *)iter->data)->str, stderr);
1086  }
1087 
1088  return EXIT_FAILURE;
1089 }
Mode drun_mode
Definition: drun.c:1471
Mode combi_mode
Definition: combi.c:309
void config_parse_cmd_options(void)
Definition: xrmoptions.c:510
void print_options(void)
Definition: xrmoptions.c:861
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition: xrmoptions.c:424
void print_help_msg(const char *option, const char *type, const char *text, const char *def, int isatty)
Definition: xrmoptions.c:878
void config_parse_dump_config_rasi_format(FILE *out, gboolean changes)
Dump configuration in rasi format.
Definition: xrmoptions.c:737
@ xrm_String
Definition: xrmoptions.h:74
void config_xresource_free(void)
Definition: xrmoptions.c:675
int dmenu_switcher_dialog(void)
Definition: dmenu.c:676
void print_dmenu_options(void)
Definition: dmenu.c:774
Mode file_browser_mode
Definition: filebrowser.c:628
void cmd_set_arguments(int argc, char **argv)
Definition: helper.c:70
int create_pid_file(const char *pidfile)
Definition: helper.c:538
const char ** find_arg_strv(const char *const key)
Definition: helper.c:320
void remove_pid_file(int fd)
Definition: helper.c:576
char * rofi_expand_path(const char *input)
Definition: helper.c:713
int find_arg_str(const char *const key, char **val)
Definition: helper.c:310
int find_arg_uint(const char *const key, unsigned int *val)
Definition: helper.c:349
int find_arg(const char *const key)
Definition: helper.c:301
int config_sanity_check(void)
Definition: helper.c:603
Mode help_keys_mode
Definition: help-keys.c:111
void rofi_icon_fetcher_destroy(void)
void rofi_icon_fetcher_init(void)
gboolean parse_keys_abe(NkBindings *bindings)
Definition: keyb.c:162
void setup_abe(void)
Definition: keyb.c:136
void mode_destroy(Mode *mode)
Definition: mode.c:48
int mode_init(Mode *mode)
Definition: mode.c:42
void mode_free(Mode **mode)
Definition: mode.c:122
ModeMode mode_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line)
Definition: mode.c:91
MenuReturn
Definition: mode.h:65
ModeMode
Definition: mode.h:49
const char * mode_get_name(const Mode *mode)
Definition: mode.c:117
void mode_set_config(Mode *mode)
Definition: mode.c:160
@ MODE_EXIT
Definition: mode.h:51
@ NEXT_DIALOG
Definition: mode.h:53
@ RELOAD_DIALOG
Definition: mode.h:55
@ PREVIOUS_DIALOG
Definition: mode.h:57
@ RESET_DIALOG
Definition: mode.h:59
void rofi_quit_main_loop(void)
Definition: rofi.c:665
#define color_reset
Definition: rofi.h:95
#define color_bold
Definition: rofi.h:97
Mode * rofi_collect_modi_search(const char *name)
Definition: rofi.c:491
void rofi_clear_error_messages(void)
Definition: rofi.c:92
#define color_red
Definition: rofi.h:103
#define ERROR_MSG_MARKUP
Definition: rofi.h:116
void rofi_set_return_code(int code)
Definition: rofi.c:131
unsigned int rofi_get_num_enabled_modi(void)
Definition: rofi.c:133
const Mode * rofi_get_mode(unsigned int index)
Definition: rofi.c:135
const char * cache_dir
Definition: rofi.c:83
void rofi_add_error_message(GString *str)
Definition: rofi.c:89
#define color_green
Definition: rofi.h:101
Mode run_mode
Definition: run.c:571
Mode * script_switcher_parse_setup(const char *str)
Definition: script.c:422
gboolean script_switcher_is_valid(const char *token)
Definition: script.c:460
Mode ssh_mode
Definition: ssh.c:637
#define TICK()
Definition: timings.h:64
#define TIMINGS_START()
Definition: timings.h:60
#define TIMINGS_STOP()
Definition: timings.h:73
#define TICK_N(a)
Definition: timings.h:69
void textbox_cleanup(void)
Definition: textbox.c:832
void textbox_setup(void)
Definition: textbox.c:806
void textbox_cursor_end(textbox *tb)
Definition: textbox.c:585
void textbox_text(textbox *tb, const char *text)
Definition: textbox.c:311
void rofi_view_cleanup()
Definition: view.c:2071
void __create_window(MenuFlags menu_flags)
Definition: view.c:751
void rofi_view_clear_input(RofiViewState *state)
Definition: view.c:2162
void rofi_view_switch_mode(RofiViewState *state, Mode *mode)
Definition: view.c:2173
void rofi_view_remove_active(RofiViewState *state)
Definition: view.c:520
RofiViewState * rofi_view_get_active(void)
Definition: view.c:518
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:2018
void rofi_view_set_active(RofiViewState *state)
Definition: view.c:527
MenuFlags
Definition: view.h:48
MenuReturn rofi_view_get_return_value(const RofiViewState *state)
Definition: view.c:586
void rofi_view_set_selected_line(RofiViewState *state, unsigned int selected_line)
Definition: view.c:550
RofiViewState * rofi_view_create(Mode *sw, const char *input, MenuFlags menu_flags, void(*finalize)(RofiViewState *))
Definition: view.c:1932
void rofi_view_free(RofiViewState *state)
Definition: view.c:568
const char * rofi_view_get_user_input(const RofiViewState *state)
Definition: view.c:607
unsigned int rofi_view_get_selected_line(const RofiViewState *state)
Definition: view.c:590
@ MENU_NORMAL_WINDOW
Definition: view.h:54
@ MENU_NORMAL
Definition: view.h:50
void rofi_capture_screenshot(void)
Definition: view.c:174
void rofi_view_workers_initialize(void)
Definition: view.c:2113
void rofi_view_workers_finalize(void)
Definition: view.c:2140
#define ABI_VERSION
Definition: mode-private.h:34
static void help(G_GNUC_UNUSED int argc, char **argv)
Definition: rofi.c:313
static void rofi_collect_modi_setup(void)
Definition: rofi.c:594
static void rofi_collect_modi_destroy(void)
Definition: rofi.c:599
int main(int argc, char *argv[])
Definition: rofi.c:774
GList * list_of_error_msgs
Definition: rofi.c:86
static int switcher_get(const char *name)
Definition: rofi.c:144
static gboolean record(G_GNUC_UNUSED void *data)
Definition: rofi.c:762
unsigned int num_modi
Definition: rofi.c:114
G_MODULE_EXPORT char * config_path
Definition: rofi.c:104
NkBindings * bindings
Definition: rofi.c:119
Mode ** available_modi
Definition: rofi.c:110
static void print_main_application_options(int is_term)
Definition: rofi.c:277
static void run_switcher(ModeMode mode)
Definition: rofi.c:168
static void cleanup()
Definition: rofi.c:451
static void help_print_mode_not_found(const char *mode)
Definition: rofi.c:398
static void print_list_of_modi(int is_term)
Definition: rofi.c:263
static void rofi_collect_modi_dir(const char *base_dir)
Definition: rofi.c:517
static gboolean startup(G_GNUC_UNUSED gpointer data)
Definition: rofi.c:692
static void rofi_collect_modi(void)
Definition: rofi.c:561
int rofi_theme_rasi_validate(const char *filename)
Definition: rofi.c:1077
GMainLoop * main_loop
Definition: rofi.c:122
static void teardown(int pfd)
Definition: rofi.c:156
char * pidfile
Definition: rofi.c:81
int return_code
Definition: rofi.c:127
static void show_error_dialog()
Definition: rofi.c:672
void process_result(RofiViewState *state)
Definition: rofi.c:202
unsigned int num_available_modi
Definition: rofi.c:112
static int add_mode(const char *token)
Definition: rofi.c:622
static gboolean main_loop_signal_handler_int(G_GNUC_UNUSED gpointer data)
Definition: rofi.c:667
static gboolean setup_modi(void)
Definition: rofi.c:644
unsigned int curr_switcher
Definition: rofi.c:116
Mode ** modi
Definition: rofi.c:107
static void help_print_no_arguments(void)
Definition: rofi.c:415
static int dmenu_mode
Definition: rofi.c:125
static void help_print_disabled_mode(const char *mode)
Definition: rofi.c:385
static gboolean rofi_collect_modi_add(Mode *mode)
Definition: rofi.c:504
Settings config
textbox * text
Definition: view-internal.h:59
char * cache_dir
Definition: settings.h:161
char * theme
Definition: settings.h:150
char * plugin_path
Definition: settings.h:152
char * filter
Definition: settings.h:139
char * modi
Definition: settings.h:57
unsigned int sidebar_mode
Definition: settings.h:120
gboolean benchmark_ui
Definition: settings.h:171
char * monitor
Definition: settings.h:137
unsigned int abi_version
Definition: mode-private.h:161
GModule * module
Definition: mode-private.h:204
char * name
Definition: mode-private.h:163
void rofi_theme_parse_process_conditionals(void)
Definition: theme.c:1445
void rofi_theme_print(ThemeWidget *widget)
Definition: theme.c:539
void rofi_theme_reset(void)
Definition: theme.c:210
void rofi_theme_free(ThemeWidget *widget)
Definition: theme.c:216
gboolean rofi_theme_parse_string(const char *string)
gboolean rofi_theme_parse_file(const char *file)
ThemeWidget * rofi_theme
Definition: theme.h:90
gboolean display_late_setup(void)
Definition: xcb.c:1601
void display_early_cleanup(void)
Definition: xcb.c:1636
void display_cleanup(void)
Definition: xcb.c:1642
gboolean display_setup(GMainLoop *main_loop, NkBindings *bindings)
Definition: xcb.c:1383
void display_dump_monitor_layout(void)
Definition: xcb.c:663
ThemeWidget * rofi_configuration
Definition: xrmoptions.c:46