rofi  1.5.1
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-2017 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 
29 #define G_LOG_DOMAIN "Rofi"
30 
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <time.h>
39 #include <locale.h>
40 #include <gmodule.h>
41 #include <xcb/xcb.h>
42 #include <sys/types.h>
43 
44 #include <glib-unix.h>
45 
46 #include <libgwater-xcb.h>
47 
48 #ifdef USE_NK_GIT_VERSION
49 #include "nkutils-git-version.h"
50 #ifdef NK_GIT_VERSION
51 #define GIT_VERSION NK_GIT_VERSION
52 #endif
53 #endif
54 
55 #include "resources.h"
56 
57 #include "rofi.h"
58 #include "display.h"
59 
60 #include "settings.h"
61 #include "mode.h"
62 #include "helper.h"
63 #include "widgets/textbox.h"
64 #include "xrmoptions.h"
65 #include "dialogs/dialogs.h"
66 
67 #include "view.h"
68 #include "view-internal.h"
69 
70 #include "theme.h"
71 
72 #include "timings.h"
73 
74 // Plugin abi version.
75 // TODO: move this check to mode.c
76 #include "mode-private.h"
77 
78 // Pidfile.
79 char *pidfile = NULL;
80 const char *cache_dir = NULL;
81 
83 GList *list_of_error_msgs = NULL;
84 
85 static void rofi_collect_modi_destroy ( void );
86 void rofi_add_error_message ( GString *str )
87 {
88  list_of_error_msgs = g_list_append ( list_of_error_msgs, str );
89 }
90 
92 G_MODULE_EXPORT char *config_path = NULL;
93 G_MODULE_EXPORT char *config_path_new = NULL;
95 Mode **modi = NULL;
96 
98 Mode ** available_modi = NULL;
100 unsigned int num_available_modi = 0;
102 unsigned int num_modi = 0;
104 unsigned int curr_switcher = 0;
105 
106 NkBindings *bindings = NULL;
107 
109 GMainLoop *main_loop = NULL;
110 
112 static int dmenu_mode = FALSE;
114 int return_code = EXIT_SUCCESS;
115 
116 void process_result ( RofiViewState *state );
117 
118 void rofi_set_return_code ( int code )
119 {
120  return_code = code;
121 }
122 
123 unsigned int rofi_get_num_enabled_modi ( void )
124 {
125  return num_modi;
126 }
127 
128 const Mode * rofi_get_mode ( unsigned int index )
129 {
130  return modi[index];
131 }
132 
140 static int switcher_get ( const char *name )
141 {
142  for ( unsigned int i = 0; i < num_modi; i++ ) {
143  if ( strcmp ( mode_get_name ( modi[i] ), name ) == 0 ) {
144  return i;
145  }
146  }
147  return -1;
148 }
149 
153 static void teardown ( int pfd )
154 {
155  g_debug ( "Teardown" );
156  // Cleanup font setup.
157  textbox_cleanup ( );
158 
160 
161  // Cleanup view
163  // Cleanup pid file.
164  remove_pid_file ( pfd );
165 }
166 static void run_switcher ( ModeMode mode )
167 {
168  // Otherwise check if requested mode is enabled.
169  for ( unsigned int i = 0; i < num_modi; i++ ) {
170  if ( !mode_init ( modi[i] ) ) {
171  GString *str = g_string_new ( "Failed to initialize the mode: " );
172  g_string_append ( str, modi[i]->name );
173  g_string_append ( str, "\n" );
174 
176  g_string_free ( str, FALSE );
177  break;
178  }
179  }
180  // Error dialog must have been created.
181  if ( rofi_view_get_active () != NULL ) {
182  return;
183  }
184  curr_switcher = mode;
186  if ( state ) {
187  rofi_view_set_active ( state );
188  }
189  if ( rofi_view_get_active () == NULL ) {
190  g_main_loop_quit ( main_loop );
191  }
192 }
194 {
195  Mode *sw = state->sw;
196  rofi_view_set_active ( NULL );
197  if ( sw != NULL ) {
198  unsigned int selected_line = rofi_view_get_selected_line ( state );;
199  MenuReturn mretv = rofi_view_get_return_value ( state );
200  char *input = g_strdup ( rofi_view_get_user_input ( state ) );
201  ModeMode retv = mode_result ( sw, mretv, &input, selected_line );
202  g_free ( input );
203 
204  ModeMode mode = curr_switcher;
205  // Find next enabled
206  if ( retv == NEXT_DIALOG ) {
207  mode = ( mode + 1 ) % num_modi;
208  }
209  else if ( retv == PREVIOUS_DIALOG ) {
210  if ( mode == 0 ) {
211  mode = num_modi - 1;
212  }
213  else {
214  mode = ( mode - 1 ) % num_modi;
215  }
216  }
217  else if ( retv == RELOAD_DIALOG ) {
218  // do nothing.
219  }
220  else if ( retv == RESET_DIALOG ) {
221  rofi_view_clear_input ( state );
222  }
223  else if ( retv < MODE_EXIT ) {
224  mode = ( retv ) % num_modi;
225  }
226  else {
227  mode = retv;
228  }
229  if ( mode != MODE_EXIT ) {
233  rofi_view_switch_mode ( state, modi[mode] );
234  rofi_view_set_active ( state );
235  curr_switcher = mode;
236  return;
237  }
238  }
239  rofi_view_free ( state );
240 }
241 
245 static void print_list_of_modi ( int is_term )
246 {
247  for ( unsigned int i = 0; i < num_available_modi; i++ ) {
248  gboolean active = FALSE;
249  for ( unsigned int j = 0; j < num_modi; j++ ) {
250  if ( modi[j] == available_modi[i] ) {
251  active = TRUE;
252  break;
253  }
254  }
255  printf ( " * %s%s%s%s\n",
256  active ? "+" : "",
257  is_term ? ( active ? color_green : color_red ) : "",
258  available_modi[i]->name,
259  is_term ? color_reset : ""
260  );
261  }
262 }
263 static void print_main_application_options ( int is_term )
264 {
265  print_help_msg ( "-no-config", "", "Do not load configuration, use default values.", NULL, is_term );
266  print_help_msg ( "-v,-version", "", "Print the version number and exit.", NULL, is_term );
267  print_help_msg ( "-dmenu", "", "Start in dmenu mode.", NULL, is_term );
268  print_help_msg ( "-display", "[string]", "X server to contact.", "${DISPLAY}", is_term );
269  print_help_msg ( "-h,-help", "", "This help message.", NULL, is_term );
270  print_help_msg ( "-dump-xresources", "", "Dump the current configuration in Xresources format and exit.", NULL, is_term );
271  print_help_msg ( "-e", "[string]", "Show a dialog displaying the passed message and exit.", NULL, is_term );
272  print_help_msg ( "-markup", "", "Enable pango markup where possible.", NULL, is_term );
273  print_help_msg ( "-normal-window", "", "In dmenu mode, behave as a normal window. (experimental)", NULL, is_term );
274  print_help_msg ( "-show", "[mode]", "Show the mode 'mode' and exit. The mode has to be enabled.", NULL, is_term );
275  print_help_msg ( "-no-lazy-grab", "", "Disable lazy grab that, when fail to grab keyboard, does not block but retry later.", NULL, is_term );
276  print_help_msg ( "-no-plugins", "", "Disable loading of external plugins.", NULL, is_term );
277  print_help_msg ( "-plugin-path", "", "Directory used to search for rofi plugins.", NULL, is_term );
278  print_help_msg ( "-dump-config", "", "Dump the current configuration in rasi format and exit.", NULL, is_term );
279  print_help_msg ( "-dump-theme", "", "Dump the current theme in rasi format and exit.", NULL, is_term );
280 }
281 static void help ( G_GNUC_UNUSED int argc, char **argv )
282 {
283  int is_term = isatty ( fileno ( stdout ) );
284  printf ( "%s usage:\n", argv[0] );
285  printf ( "\t%s [-options ...]\n\n", argv[0] );
286  printf ( "Command line only options:\n" );
287  print_main_application_options ( is_term );
288  printf ( "DMENU command line options:\n" );
290  printf ( "Global options:\n" );
291  print_options ();
292  printf ( "\n" );
294  printf ( "\n" );
295  printf ( "Detected modi:\n" );
296  print_list_of_modi ( is_term );
297  printf ( "\n" );
298  printf ( "Compile time options:\n" );
299 #ifdef WINDOW_MODE
300  printf ( "\t* window %senabled%s\n", is_term ? color_green : "", is_term ? color_reset : "" );
301 #else
302  printf ( "\t* window %sdisabled%s\n", is_term ? color_red : "", is_term ? color_reset : "" );
303 #endif
304 #ifdef ENABLE_DRUN
305  printf ( "\t* drun %senabled%s\n", is_term ? color_green : "", is_term ? color_reset : "" );
306 #else
307  printf ( "\t* drun %sdisabled%s\n", is_term ? color_red : "", is_term ? color_reset : "" );
308 #endif
309 #ifdef TIMINGS
310  printf ( "\t* timings %senabled%s\n", is_term ? color_green : "", is_term ? color_reset : "" );
311 #else
312  printf ( "\t* timings %sdisabled%s\n", is_term ? color_red : "", is_term ? color_reset : "" );
313 #endif
314 #ifdef ENABLE_GCOV
315  printf ( "\t* gcov %senabled%s\n", is_term ? color_green : "", is_term ? color_reset : "" );
316 #else
317  printf ( "\t* gcov %sdisabled%s\n", is_term ? color_red : "", is_term ? color_reset : "" );
318 #endif
319 #ifdef ENABLE_ASAN
320  printf ( "\t* asan %senabled%s\n", is_term ? color_green : "", is_term ? color_reset : "" );
321 #else
322  printf ( "\t* asan %sdisabled%s\n", is_term ? color_red : "", is_term ? color_reset : "" );
323 #endif
324  printf ( "\n" );
325  printf ( "For more information see: %sman rofi%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
326 #ifdef GIT_VERSION
327  printf ( " Version: %s"GIT_VERSION "%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
328 #else
329  printf ( " Version: %s"VERSION "%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
330 #endif
331  printf ( " Bugreports: %s"PACKAGE_BUGREPORT "%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
332  printf ( " Support: %s"PACKAGE_URL "%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
333  printf ( " %s#rofi @ freenode.net%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
334  if ( find_arg ( "-no-config" ) < 0 ) {
335  if ( config_path_new ) {
336  printf ( " Configuration file: %s%s%s\n", is_term ? color_bold : "", config_path_new, is_term ? color_reset : "" );
337  }
338  else {
339  printf ( " Configuration file: %s%s%s\n", is_term ? color_bold : "", config_path, is_term ? color_reset : "" );
340  }
341  }
342  else {
343  printf ( " Configuration file: %sDisabled%s\n", is_term ? color_bold : "", is_term ? color_reset : "" );
344  }
345 }
346 
347 static void help_print_disabled_mode ( const char *mode )
348 {
349  int is_term = isatty ( fileno ( stdout ) );
350  // Only output to terminal
351  if ( is_term ) {
352  fprintf ( stderr, "Mode %s%s%s is not enabled. I have enabled it for now.\n",
353  color_red, mode, color_reset );
354  fprintf ( stderr, "Please consider adding %s%s%s to the list of enabled modi: %smodi: %s%s%s,%s%s.\n",
355  color_red, mode, color_reset,
357  color_red, mode, color_reset
358  );
359  }
360 }
361 static void help_print_mode_not_found ( const char *mode )
362 {
363  int is_term = isatty ( fileno ( stdout ) );
364  fprintf ( stderr, "Mode %s%s%s is not found.\n",
365  is_term ? color_red : "", mode, is_term ? color_reset : "" );
366  fprintf ( stderr, "The following modi are known:\n" );
367  print_list_of_modi ( is_term );
368  printf ( "\n" );
369 }
370 static void help_print_no_arguments ( void )
371 {
372  int is_term = isatty ( fileno ( stdout ) );
373  // Daemon mode
374  fprintf ( stderr, "Rofi is unsure what to show.\n" );
375  fprintf ( stderr, "Please specify the mode you want to show.\n\n" );
376  fprintf ( stderr, " %srofi%s -show %s{mode}%s\n\n",
377  is_term ? color_bold : "", is_term ? color_reset : "",
378  is_term ? color_green : "", is_term ? color_reset : "" );
379  fprintf ( stderr, "The following modi are enabled:\n" );
380  for ( unsigned int j = 0; j < num_modi; j++ ) {
381  fprintf ( stderr, " * %s%s%s\n",
382  is_term ? color_green : "",
383  modi[j]->name,
384  is_term ? color_reset : "" );
385  }
386  fprintf ( stderr, "\nThe following can be enabled:\n" );
387  for ( unsigned int i = 0; i < num_available_modi; i++ ) {
388  gboolean active = FALSE;
389  for ( unsigned int j = 0; j < num_modi; j++ ) {
390  if ( modi[j] == available_modi[i] ) {
391  active = TRUE;
392  break;
393  }
394  }
395  if ( !active ) {
396  fprintf ( stderr, " * %s%s%s\n",
397  is_term ? color_red : "",
398  available_modi[i]->name,
399  is_term ? color_reset : "" );
400  }
401  }
402  fprintf ( stderr, "\nTo activate a mode, add it to the list of modi in the %smodi%s setting.\n",
403  is_term ? color_green : "", is_term ? color_reset : "" );
404 }
405 
409 static void cleanup ()
410 {
411  for ( unsigned int i = 0; i < num_modi; i++ ) {
412  mode_destroy ( modi[i] );
413  }
415  if ( main_loop != NULL ) {
416  g_main_loop_unref ( main_loop );
417  main_loop = NULL;
418  }
419  // Cleanup
420  display_cleanup ();
421 
422  nk_bindings_free ( bindings );
423 
424  // Cleaning up memory allocated by the Xresources file.
426  g_free ( modi );
427 
428  g_free ( config_path );
429  g_free ( config_path_new );
430 
431  if ( list_of_error_msgs ) {
432  for ( GList *iter = g_list_first ( list_of_error_msgs );
433  iter != NULL; iter = g_list_next ( iter ) ) {
434  g_string_free ( (GString *) iter->data, TRUE );
435  }
436  g_list_free ( list_of_error_msgs );
437  }
438 
439  if ( rofi_theme ) {
441  rofi_theme = NULL;
442  }
443  TIMINGS_STOP ();
445 }
446 
456 Mode * rofi_collect_modi_search ( const char *name )
457 {
458  for ( unsigned int i = 0; i < num_available_modi; i++ ) {
459  if ( g_strcmp0 ( name, available_modi[i]->name ) == 0 ) {
460  return available_modi[i];
461  }
462  }
463  return NULL;
464 }
470 static gboolean rofi_collect_modi_add ( Mode *mode )
471 {
472  Mode *m = rofi_collect_modi_search ( mode->name );
473  if ( m == NULL ) {
474  available_modi = g_realloc ( available_modi, sizeof ( Mode * ) * ( num_available_modi + 1 ) );
475  // Set mode.
478  return TRUE;
479  }
480  return FALSE;
481 }
482 
483 static void rofi_collect_modi_dir ( const char *base_dir )
484 {
485  GDir *dir = g_dir_open ( base_dir, 0, NULL );
486  if ( dir ) {
487  const char *dn = NULL;
488  while ( ( dn = g_dir_read_name ( dir ) ) ) {
489  if ( !g_str_has_suffix ( dn, G_MODULE_SUFFIX ) ) {
490  continue;
491  }
492  char *fn = g_build_filename ( base_dir, dn, NULL );
493  GModule *mod = g_module_open ( fn, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL );
494  if ( mod ) {
495  Mode *m = NULL;
496  if ( g_module_symbol ( mod, "mode", (gpointer *) &m ) ) {
497  if ( m->abi_version != ABI_VERSION ) {
498  g_warning ( "ABI version of plugin: '%s' does not match: %08X expecting: %08X", dn, m->abi_version, ABI_VERSION );
499  g_module_close ( mod );
500  }
501  else {
502  m->module = mod;
503  if ( !rofi_collect_modi_add ( m ) ) {
504  g_module_close ( mod );
505  }
506  }
507  }
508  else {
509  g_warning ( "Symbol 'mode' not found in module: %s", dn );
510  g_module_close ( mod );
511  }
512  }
513  else {
514  g_warning ( "Failed to open 'mode' plugin: '%s', error: %s", dn, g_module_error () );
515  }
516  g_free ( fn );
517  }
518  g_dir_close ( dir );
519  }
520 }
521 
525 static void rofi_collect_modi ( void )
526 {
527 #ifdef WINDOW_MODE
528  rofi_collect_modi_add ( &window_mode );
529  rofi_collect_modi_add ( &window_mode_cd );
530 #endif
533 #ifdef ENABLE_DRUN
534  rofi_collect_modi_add ( &drun_mode );
535 #endif
538 
539  if ( find_arg ( "-no-plugins" ) < 0 ) {
540  find_arg_str ( "-plugin-path", &( config.plugin_path ) );
541  g_debug ( "Parse plugin path: %s", config.plugin_path );
543  }
544 }
545 
549 static void rofi_collect_modi_setup ( void )
550 {
551  for ( unsigned int i = 0; i < num_available_modi; i++ ) {
553  }
554 }
555 static void rofi_collect_modi_destroy ( void )
556 {
557  for ( unsigned int i = 0; i < num_available_modi; i++ ) {
558  if ( available_modi[i]->module ) {
559  GModule *mod = available_modi[i]->module;
560  available_modi[i] = NULL;
561  g_module_close ( mod );
562  }
563  if ( available_modi[i] ) {
564  mode_free ( &( available_modi[i] ) );
565  }
566  }
567  g_free ( available_modi );
568  available_modi = NULL;
569  num_available_modi = 0;
570 }
571 
579 static int add_mode ( const char * token )
580 {
581  unsigned int index = num_modi;
582  // Resize and add entry.
583  modi = (Mode * *) g_realloc ( modi, sizeof ( Mode* ) * ( num_modi + 1 ) );
584 
585  Mode *mode = rofi_collect_modi_search ( token );
586  if ( mode ) {
587  modi[num_modi] = mode;
588  num_modi++;
589  }
590  else if ( script_switcher_is_valid ( token ) ) {
591  // If not build in, use custom modi.
592  Mode *sw = script_switcher_parse_setup ( token );
593  if ( sw != NULL ) {
594  // Add to available list, so combi can find it.
595  rofi_collect_modi_add ( sw );
596  modi[num_modi] = sw;
597  num_modi++;
598  }
599  }
600  return ( index == num_modi ) ? -1 : (int) index;
601 }
602 static gboolean setup_modi ( void )
603 {
604  const char *const sep = ",#";
605  char *savept = NULL;
606  // Make a copy, as strtok will modify it.
607  char *switcher_str = g_strdup ( config.modi );
608  // Split token on ','. This modifies switcher_str.
609  for ( char *token = strtok_r ( switcher_str, sep, &savept ); token != NULL; token = strtok_r ( NULL, sep, &savept ) ) {
610  if ( add_mode ( token ) == -1 ) {
611  help_print_mode_not_found ( token );
612  g_free ( switcher_str );
613  return TRUE;
614  }
615  }
616  // Free string that was modified by strtok_r
617  g_free ( switcher_str );
618  return FALSE;
619 }
620 
621 void rofi_quit_main_loop ( void )
622 {
623  g_main_loop_quit ( main_loop );
624 }
625 
626 static gboolean main_loop_signal_handler_int ( G_GNUC_UNUSED gpointer data )
627 {
628  // Break out of loop.
629  g_main_loop_quit ( main_loop );
630  return G_SOURCE_CONTINUE;
631 }
632 
633 static gboolean startup ( G_GNUC_UNUSED gpointer data )
634 {
635  TICK_N ( "Startup" );
636  // flags to run immediately and exit
637  char *sname = NULL;
638  char *msg = NULL;
639  MenuFlags window_flags = MENU_NORMAL;
640 
641  if ( find_arg ( "-normal-window" ) >= 0 ) {
642  window_flags |= MENU_NORMAL_WINDOW;
643  }
644  TICK_N ( "Grab keyboard" );
645  __create_window ( window_flags );
646  TICK_N ( "Create Window" );
647  // Parse the keybindings.
648  TICK_N ( "Parse ABE" );
649  // Sanity check
651  TICK_N ( "Config sanity check" );
652 
653  if ( list_of_error_msgs != NULL ) {
654  GString *emesg = g_string_new ( "The following errors were detected when starting rofi:\n" );
655  GList *iter = g_list_first ( list_of_error_msgs );
656  int index = 0;
657  for (; iter != NULL && index < 2; iter = g_list_next ( iter ) ) {
658  GString *msg = (GString *) ( iter->data );
659  g_string_append ( emesg, "\n\n" );
660  g_string_append ( emesg, msg->str );
661  index++;
662  }
663  if ( g_list_length ( iter ) > 1 ) {
664  g_string_append_printf ( emesg, "\nThere are <b>%d</b> more errors.", g_list_length ( iter ) - 1 );
665  }
667  g_string_free ( emesg, TRUE );
668  return G_SOURCE_REMOVE;
669  }
670  // Dmenu mode.
671  if ( dmenu_mode == TRUE ) {
672  // force off sidebar mode:
673  config.sidebar_mode = FALSE;
674  int retv = dmenu_switcher_dialog ();
675  if ( retv ) {
676  rofi_set_return_code ( EXIT_SUCCESS );
677  // Directly exit.
678  g_main_loop_quit ( main_loop );
679  }
680  }
681  else if ( find_arg_str ( "-e", &( msg ) ) ) {
682  int markup = FALSE;
683  if ( find_arg ( "-markup" ) >= 0 ) {
684  markup = TRUE;
685  }
686  if ( !rofi_view_error_dialog ( msg, markup ) ) {
687  g_main_loop_quit ( main_loop );
688  }
689  }
690  else if ( find_arg_str ( "-show", &sname ) == TRUE ) {
691  int index = switcher_get ( sname );
692  if ( index < 0 ) {
693  // Add it to the list
694  index = add_mode ( sname );
695  // Complain
696  if ( index >= 0 ) {
697  help_print_disabled_mode ( sname );
698  }
699  // Run it anyway if found.
700  }
701  if ( index >= 0 ) {
702  run_switcher ( index );
703  }
704  else {
705  help_print_mode_not_found ( sname );
706  g_main_loop_quit ( main_loop );
707  return G_SOURCE_REMOVE;
708  }
709  }
710  else if ( find_arg ( "-show" ) >= 0 && num_modi > 0 ) {
711  run_switcher ( 0 );
712  }
713  else{
715 
716  g_main_loop_quit ( main_loop );
717  }
718 
719  return G_SOURCE_REMOVE;
720 }
721 
722 static gboolean record ( G_GNUC_UNUSED void *data )
723 {
725  return G_SOURCE_CONTINUE;
726 }
735 int main ( int argc, char *argv[] )
736 {
737  TIMINGS_START ();
738 
739  cmd_set_arguments ( argc, argv );
740 
741  // Version
742  if ( find_arg ( "-v" ) >= 0 || find_arg ( "-version" ) >= 0 ) {
743 #ifdef GIT_VERSION
744  g_print ( "Version: "GIT_VERSION "\n" );
745 #else
746  g_print ( "Version: "VERSION "\n" );
747 #endif
748  return EXIT_SUCCESS;
749  }
750 
751  // Detect if we are in dmenu mode.
752  // This has two possible causes.
753  // 1 the user specifies it on the command-line.
754  if ( find_arg ( "-dmenu" ) >= 0 ) {
755  dmenu_mode = TRUE;
756  }
757  // 2 the binary that executed is called dmenu (e.g. symlink to rofi)
758  else{
759  // Get the base name of the executable called.
760  char *base_name = g_path_get_basename ( argv[0] );
761  const char * const dmenu_str = "dmenu";
762  dmenu_mode = ( strcmp ( base_name, dmenu_str ) == 0 );
763  // Free the basename for dmenu detection.
764  g_free ( base_name );
765  }
766  TICK ();
767  // Get the path to the cache dir.
768  cache_dir = g_get_user_cache_dir ();
769 
770  if ( g_mkdir_with_parents ( cache_dir, 0700 ) < 0 ) {
771  g_warning ( "Failed to create cache directory: %s", g_strerror ( errno ) );
772  return EXIT_FAILURE;
773  }
774 
775  // Create pid file path.
776  const char *path = g_get_user_runtime_dir ();
777  if ( path ) {
778  if ( g_mkdir_with_parents ( path, 0700 ) < 0 ) {
779  g_warning ( "Failed to create user runtime directory: %s with error: %s", path, g_strerror ( errno ) );
780  pidfile = g_build_filename ( g_get_home_dir (), ".rofi.pid", NULL );
781  }
782  else {
783  pidfile = g_build_filename ( path, "rofi.pid", NULL );
784  }
785  }
786  config_parser_add_option ( xrm_String, "pid", (void * *) &pidfile, "Pidfile location" );
787 
788  if ( find_arg ( "-config" ) < 0 ) {
789  const char *cpath = g_get_user_config_dir ();
790  if ( cpath ) {
791  config_path = g_build_filename ( cpath, "rofi", "config", NULL );
792  config_path_new = g_strconcat ( config_path, ".rasi", NULL );
793  }
794  }
795  else {
796  char *c = NULL;
797  find_arg_str ( "-config", &c );
798  if ( g_str_has_suffix ( c, ".rasi" ) ) {
800  }
801  else {
803  }
804  }
805 
806  TICK ();
807  if ( setlocale ( LC_ALL, "" ) == NULL ) {
808  g_warning ( "Failed to set locale." );
809  cleanup ();
810  return EXIT_FAILURE;
811  }
812 
813  TICK_N ( "Setup Locale" );
815  TICK_N ( "Collect MODI" );
817  TICK_N ( "Setup MODI" );
818 
819  main_loop = g_main_loop_new ( NULL, FALSE );
820 
821  TICK_N ( "Setup mainloop" );
822 
823  bindings = nk_bindings_new ( 0 );
824  TICK_N ( "NK Bindings" );
825 
826  if ( !display_setup ( main_loop, bindings ) ) {
827  g_warning ( "Connection has error" );
828  cleanup ();
829  return EXIT_FAILURE;
830  }
831  TICK_N ( "Setup Display" );
832 
833  // Setup keybinding
834  setup_abe ();
835  TICK_N ( "Setup abe" );
836 
837  if ( find_arg ( "-no-config" ) < 0 ) {
838  // Load distro default settings
839  gchar *etc = g_build_filename ( SYSCONFDIR, "rofi.conf", NULL );
840  if ( g_file_test ( etc, G_FILE_TEST_IS_REGULAR ) ) {
842  }
843  g_free ( etc );
844  // Load in config from X resources.
846  if ( config_path_new && g_file_test ( config_path_new, G_FILE_TEST_IS_REGULAR ) ) {
849  rofi_theme = NULL;
850  }
851  } else {
852  g_free ( config_path_new );
853  config_path_new = NULL;
855  }
856  }
857  find_arg_str ( "-theme", &( config.theme ) );
858  if ( config.theme ) {
859  TICK_N ( "Parse theme" );
860  if ( rofi_theme_parse_file ( config.theme ) ) {
861  // TODO: instantiate fallback theme.?
863  rofi_theme = NULL;
864  }
865  TICK_N ( "Parsed theme" );
866  }
867  // Parse command line for settings, independent of other -no-config.
869  TICK_N ( "Load cmd config " );
870 
872 
874  char *windowid = NULL;
875  if ( !dmenu_mode ) {
876  // setup_modi
877  if ( setup_modi () ) {
878  cleanup ();
879  return EXIT_FAILURE;
880  }
881  TICK_N ( "Setup Modi" );
882  }
883  else {
884  // Hack for dmenu compatibility.
885  if ( find_arg_str ( "-w", &windowid ) == TRUE ) {
886  config.monitor = g_strdup_printf ( "wid:%s", windowid );
887  windowid = config.monitor;
888  }
889  }
890  if ( rofi_theme_is_empty ( ) ) {
891  GBytes *theme_data = g_resource_lookup_data (
892  resources_get_resource (),
893  "/org/qtools/rofi/default_theme.rasi",
894  G_RESOURCE_LOOKUP_FLAGS_NONE,
895  NULL );
896  if ( theme_data ) {
897  const char *theme = g_bytes_get_data ( theme_data, NULL );
898  if ( rofi_theme_parse_string ( (const char *) theme ) ) {
899  g_warning ( "Failed to parse default theme. Giving up.." );
900  if ( list_of_error_msgs ) {
901  for ( GList *iter = g_list_first ( list_of_error_msgs );
902  iter != NULL; iter = g_list_next ( iter ) ) {
903  g_warning ( "Error: %s%s%s",
904  color_bold, ( (GString *) iter->data )->str, color_reset );
905  }
906  }
907  rofi_theme = NULL;
908  cleanup ();
909  return EXIT_FAILURE;
910  }
911  g_bytes_unref ( theme_data );
912  }
913  rofi_theme_convert_old ();
914  }
915 
919  const char ** theme_str = find_arg_strv ( "-theme-str" );
920  if ( theme_str ) {
921  for ( int index = 0; theme_str && theme_str[index]; index++ ) {
922  if ( rofi_theme_parse_string ( theme_str[index] ) ) {
924  rofi_theme = NULL;
925  }
926  }
927  g_free ( theme_str );
928  }
929 
930  if ( find_arg ( "-dump-theme" ) >= 0 ) {
932  cleanup ();
933  return EXIT_SUCCESS;
934  }
935  if ( find_arg ( "-dump-config" ) >= 0 ) {
937  cleanup ();
938  return EXIT_SUCCESS;
939  }
940  // Dump.
941  // catch help request
942  if ( find_arg ( "-h" ) >= 0 || find_arg ( "-help" ) >= 0 || find_arg ( "--help" ) >= 0 ) {
943  help ( argc, argv );
944  cleanup ();
945  return EXIT_SUCCESS;
946  }
947  if ( find_arg ( "-dump-xresources" ) >= 0 ) {
949  cleanup ();
950  return EXIT_SUCCESS;
951  }
952 
953  unsigned int interval = 1;
954  if ( find_arg_uint ( "-record-screenshots", &interval ) ) {
955  g_timeout_add ( 1000 / (double) interval, record, NULL );
956  }
957 
959 
960  // Create pid file
961  int pfd = create_pid_file ( pidfile );
962  if ( pfd < 0 ) {
963  cleanup ();
964  return EXIT_FAILURE;
965  }
966  textbox_setup ();
967 
968  if ( !display_late_setup () ) {
969  g_warning ( "Failed to properly finish display setup" );
970  cleanup ();
971  return EXIT_FAILURE;
972  }
973  TICK_N ( "Setup late Display" );
974 
975  // Setup signal handling sources.
976  // SIGINT
977  g_unix_signal_add ( SIGINT, main_loop_signal_handler_int, NULL );
978 
979  g_idle_add ( startup, NULL );
980 
981  // Start mainloop.
982  g_main_loop_run ( main_loop );
983  teardown ( pfd );
984  cleanup ();
985 
986  /* dirty hack */
987  g_free ( windowid );
988  return return_code;
989 }
void rofi_view_cleanup()
Definition: view.c:1777
void rofi_theme_free(ThemeWidget *widget)
Definition: theme.c:122
const char * cache_dir
Definition: rofi.c:80
GMainLoop * main_loop
Definition: rofi.c:109
MenuReturn
Definition: mode.h:66
NkBindings * bindings
Definition: rofi.c:106
unsigned int num_modi
Definition: rofi.c:102
void rofi_quit_main_loop(void)
Definition: rofi.c:621
void textbox_cleanup(void)
Definition: textbox.c:835
Mode run_mode
Definition: run.c:410
char * monitor
Definition: settings.h:133
void display_early_cleanup(void)
Definition: xcb.c:1315
void rofi_set_return_code(int code)
Definition: rofi.c:118
void rofi_view_clear_input(RofiViewState *state)
Definition: view.c:1876
void textbox_setup(void)
Definition: textbox.c:817
unsigned int sidebar_mode
Definition: settings.h:118
static void rofi_collect_modi_destroy(void)
Definition: rofi.c:555
void rofi_view_workers_finalize(void)
Definition: view.c:1842
static gboolean startup(G_GNUC_UNUSED gpointer data)
Definition: rofi.c:633
xcb_stuff * xcb
Definition: xcb.c:85
const char * rofi_view_get_user_input(const RofiViewState *state)
Definition: view.c:537
void __create_window(MenuFlags menu_flags)
Definition: view.c:660
void config_parse_xresource_dump(void)
Definition: xrmoptions.c:545
static void rofi_collect_modi(void)
Definition: rofi.c:525
int dmenu_switcher_dialog(void)
Definition: dmenu.c:557
static void print_list_of_modi(int is_term)
Definition: rofi.c:245
int config_sanity_check(void)
Definition: helper.c:574
#define TIMINGS_START()
Definition: timings.h:81
#define color_green
Definition: rofi.h:96
void mode_free(Mode **mode)
Definition: mode.c:118
void print_dmenu_options(void)
Definition: dmenu.c:648
static void teardown(int pfd)
Definition: rofi.c:153
static void rofi_collect_modi_setup(void)
Definition: rofi.c:549
RofiViewState * rofi_view_get_active(void)
Definition: view.c:446
int find_arg_uint(const char *const key, unsigned int *val)
Definition: helper.c:347
Mode ** modi
Definition: rofi.c:95
unsigned int rofi_get_num_enabled_modi(void)
Definition: rofi.c:123
char * modi
Definition: settings.h:53
MenuReturn rofi_view_get_return_value(const RofiViewState *state)
Definition: view.c:512
ModeMode
Definition: mode.h:49
int find_arg_str(const char *const key, char **val)
Definition: helper.c:305
const char ** find_arg_strv(const char *const key)
Definition: helper.c:316
void config_parse_xresource_options_file(const char *filename)
Definition: xrmoptions.c:324
unsigned int num_available_modi
Definition: rofi.c:100
void rofi_view_workers_initialize(void)
Definition: view.c:1815
G_MODULE_EXPORT char * config_path_new
Definition: rofi.c:93
Definition: mode.h:52
void rofi_view_free(RofiViewState *state)
Definition: view.c:490
#define TIMINGS_STOP()
Definition: timings.h:85
void display_cleanup(void)
Definition: xcb.c:1322
gboolean display_setup(GMainLoop *main_loop, NkBindings *bindings)
Definition: xcb.c:1082
void print_help_msg(const char *option, const char *type, const char *text, const char *def, int isatty)
Definition: xrmoptions.c:744
void config_parse_xresource_options(xcb_stuff *xcb)
Definition: xrmoptions.c:315
void mode_set_config(Mode *mode)
Definition: mode.c:151
Mode * rofi_collect_modi_search(const char *name)
Definition: rofi.c:456
void remove_pid_file(int fd)
Definition: helper.c:547
static int switcher_get(const char *name)
Definition: rofi.c:140
void mode_destroy(Mode *mode)
Definition: mode.c:49
gboolean rofi_theme_parse_file(const char *file)
#define ABI_VERSION
Definition: mode-private.h:34
void config_xresource_free(void)
Definition: xrmoptions.c:487
static void help_print_disabled_mode(const char *mode)
Definition: rofi.c:347
static void help_print_mode_not_found(const char *mode)
Definition: rofi.c:361
void rofi_view_set_active(RofiViewState *state)
Definition: view.c:451
static int add_mode(const char *token)
Definition: rofi.c:579
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:1726
#define TICK()
Definition: timings.h:89
G_MODULE_EXPORT char * config_path
Definition: rofi.c:92
int return_code
Definition: rofi.c:114
int main(int argc, char *argv[])
Definition: rofi.c:735
void rofi_add_error_message(GString *str)
Definition: rofi.c:86
void cmd_set_arguments(int argc, char **argv)
Definition: helper.c:73
gboolean script_switcher_is_valid(const char *token)
Definition: script.c:319
char * rofi_expand_path(const char *input)
Definition: helper.c:669
void rofi_theme_print(ThemeWidget *widget)
Definition: theme.c:329
void display_dump_monitor_layout(void)
Definition: xcb.c:425
static void print_main_application_options(int is_term)
Definition: rofi.c:263
void rofi_view_switch_mode(RofiViewState *state, Mode *mode)
Definition: view.c:1884
int create_pid_file(const char *pidfile)
Definition: helper.c:508
GList * list_of_error_msgs
Definition: rofi.c:83
char * filter
Definition: settings.h:138
unsigned int curr_switcher
Definition: rofi.c:104
const char * mode_get_name(const Mode *mode)
Definition: mode.c:112
#define ERROR_MSG_MARKUP
Definition: rofi.h:110
Mode * script_switcher_parse_setup(const char *str)
Definition: script.c:283
void process_result(RofiViewState *state)
Definition: rofi.c:193
ModeMode mode_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line)
Definition: mode.c:97
ThemeWidget * rofi_theme
static int dmenu_mode
Definition: rofi.c:112
GModule * module
Definition: mode-private.h:197
void config_parse_dump_config_rasi_format(gboolean changes)
Dump configuration in rasi format.
Definition: xrmoptions.c:607
static void help(G_GNUC_UNUSED int argc, char **argv)
Definition: rofi.c:281
void print_options(void)
Definition: xrmoptions.c:726
Mode combi_mode
Definition: combi.c:299
const Mode * rofi_get_mode(unsigned int index)
Definition: rofi.c:128
static gboolean main_loop_signal_handler_int(G_GNUC_UNUSED gpointer data)
Definition: rofi.c:626
gboolean parse_keys_abe(NkBindings *bindings)
Definition: keyb.c:146
void config_parse_cmd_options(void)
Definition: xrmoptions.c:392
void setup_abe(void)
Definition: keyb.c:132
unsigned int rofi_view_get_selected_line(const RofiViewState *state)
Definition: view.c:517
Settings config
char * plugin_path
Definition: settings.h:163
int find_arg(const char *const key)
Definition: helper.c:295
static void help_print_no_arguments(void)
Definition: rofi.c:370
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition: xrmoptions.c:220
char * pidfile
Definition: rofi.c:79
static gboolean record(G_GNUC_UNUSED void *data)
Definition: rofi.c:722
#define color_bold
Definition: rofi.h:92
Mode help_keys_mode
Definition: help-keys.c:122
Mode ** available_modi
Definition: rofi.c:98
#define TICK_N(a)
Definition: timings.h:94
static void rofi_collect_modi_dir(const char *base_dir)
Definition: rofi.c:483
static void cleanup()
Definition: rofi.c:409
Mode ssh_mode
Definition: ssh.c:524
#define color_reset
Definition: rofi.h:90
static gboolean rofi_collect_modi_add(Mode *mode)
Definition: rofi.c:470
gboolean rofi_theme_parse_string(const char *string)
char * theme
Definition: settings.h:161
void rofi_capture_screenshot(void)
Definition: view.c:176
gboolean rofi_theme_is_empty(void)
Definition: theme.c:769
char * name
Definition: mode-private.h:156
RofiViewState * rofi_view_create(Mode *sw, const char *input, MenuFlags menu_flags, void(*finalize)(RofiViewState *))
Definition: view.c:1654
MenuFlags
Definition: view.h:43
static void run_switcher(ModeMode mode)
Definition: rofi.c:166
static gboolean setup_modi(void)
Definition: rofi.c:602
#define color_red
Definition: rofi.h:98
gboolean display_late_setup(void)
Definition: xcb.c:1277
int mode_init(Mode *mode)
Definition: mode.c:42