30 #define G_LOG_DOMAIN "Rofi"
43 #include <sys/types.h>
46 #include <glib-unix.h>
48 #include <libgwater-xcb.h>
50 #ifdef USE_NK_GIT_VERSION
51 #include "nkutils-git-version.h"
53 #define GIT_VERSION NK_GIT_VERSION
57 #include "resources.h"
150 for (
unsigned int i = 0; i <
num_modi; i++ ) {
163 g_debug (
"Teardown" );
177 for (
unsigned int i = 0; i <
num_modi; i++ ) {
179 GString *str = g_string_new (
"Failed to initialize the mode: " );
180 g_string_append ( str,
modi[i]->name );
181 g_string_append ( str,
"\n" );
184 g_string_free ( str, FALSE );
196 if (
find_arg (
"-selected-row" ) >= 0 ) {
263 gboolean active = FALSE;
264 for (
unsigned int j = 0; j <
num_modi; j++ ) {
270 printf (
" * %s%s%s%s\n",
280 print_help_msg (
"-no-config",
"",
"Do not load configuration, use default values.", NULL, is_term );
281 print_help_msg (
"-v,-version",
"",
"Print the version number and exit.", NULL, is_term );
282 print_help_msg (
"-dmenu",
"",
"Start in dmenu mode.", NULL, is_term );
283 print_help_msg (
"-display",
"[string]",
"X server to contact.",
"${DISPLAY}", is_term );
284 print_help_msg (
"-h,-help",
"",
"This help message.", NULL, is_term );
285 print_help_msg (
"-dump-xresources",
"",
"Dump the current configuration in Xresources format and exit.", NULL, is_term );
286 print_help_msg (
"-e",
"[string]",
"Show a dialog displaying the passed message and exit.", NULL, is_term );
287 print_help_msg (
"-markup",
"",
"Enable pango markup where possible.", NULL, is_term );
288 print_help_msg (
"-normal-window",
"",
"Behave as a normal window. (experimental)", NULL, is_term );
289 print_help_msg (
"-show",
"[mode]",
"Show the mode 'mode' and exit. The mode has to be enabled.", NULL, is_term );
290 print_help_msg (
"-no-lazy-grab",
"",
"Disable lazy grab that, when fail to grab keyboard, does not block but retry later.", NULL, is_term );
291 print_help_msg (
"-no-plugins",
"",
"Disable loading of external plugins.", NULL, is_term );
292 print_help_msg (
"-plugin-path",
"",
"Directory used to search for rofi plugins. *DEPRECATED*", NULL, is_term );
293 print_help_msg (
"-dump-config",
"",
"Dump the current configuration in rasi format and exit.", NULL, is_term );
294 print_help_msg (
"-upgrade-config",
"",
"Upgrade the old-style configuration fiel in the new rasi format and exit.", NULL, is_term );
295 print_help_msg (
"-dump-theme",
"",
"Dump the current theme in rasi format and exit.", NULL, is_term );
297 static void help ( G_GNUC_UNUSED
int argc,
char **argv )
299 int is_term = isatty ( fileno ( stdout ) );
300 printf (
"%s usage:\n", argv[0] );
301 printf (
"\t%s [-options ...]\n\n", argv[0] );
302 printf (
"Command line only options:\n" );
304 printf (
"DMENU command line options:\n" );
306 printf (
"Global options:\n" );
311 printf (
"Detected modi:\n" );
314 printf (
"Compile time options:\n" );
336 printf (
"For more information see: %sman rofi%s\n", is_term ?
color_bold :
"", is_term ?
color_reset :
"" );
342 printf (
" Bugreports: %s"PACKAGE_BUGREPORT
"%s\n", is_term ?
color_bold :
"", is_term ?
color_reset :
"" );
345 if (
find_arg (
"-no-config" ) < 0 ) {
354 printf (
" Configuration file: %sDisabled%s\n", is_term ?
color_bold :
"", is_term ?
color_reset :
"" );
360 int is_term = isatty ( fileno ( stdout ) );
363 fprintf ( stderr,
"Mode %s%s%s is not enabled. I have enabled it for now.\n",
365 fprintf ( stderr,
"Please consider adding %s%s%s to the list of enabled modi: %smodi: %s%s%s,%s%s.\n",
374 GString *str = g_string_new (
"" );
375 g_string_printf ( str,
"Mode %s is not found.\nThe following modi are known:\n", mode );
377 gboolean active = FALSE;
378 for (
unsigned int j = 0; j <
num_modi; j++ ) {
384 g_string_append_printf ( str,
" * %s%s\n",
393 int is_term = isatty ( fileno ( stdout ) );
395 fprintf ( stderr,
"Rofi is unsure what to show.\n" );
396 fprintf ( stderr,
"Please specify the mode you want to show.\n\n" );
397 fprintf ( stderr,
" %srofi%s -show %s{mode}%s\n\n",
400 fprintf ( stderr,
"The following modi are enabled:\n" );
401 for (
unsigned int j = 0; j <
num_modi; j++ ) {
402 fprintf ( stderr,
" * %s%s%s\n",
407 fprintf ( stderr,
"\nThe following can be enabled:\n" );
409 gboolean active = FALSE;
410 for (
unsigned int j = 0; j <
num_modi; j++ ) {
417 fprintf ( stderr,
" * %s%s%s\n",
423 fprintf ( stderr,
"\nTo activate a mode, add it to the list of modi in the %smodi%s setting.\n",
432 for (
unsigned int i = 0; i <
num_modi; i++ ) {
454 iter != NULL; iter = g_list_next ( iter ) ) {
455 g_string_free ( (GString *) iter->data, TRUE );
507 g_debug (
"Looking into: %s for plugins", base_dir );
508 GDir *dir = g_dir_open ( base_dir, 0, NULL );
510 const char *dn = NULL;
511 while ( ( dn = g_dir_read_name ( dir ) ) ) {
512 if ( !g_str_has_suffix ( dn, G_MODULE_SUFFIX ) ) {
515 char *fn = g_build_filename ( base_dir, dn, NULL );
516 g_debug (
"Trying to open: %s plugin", fn );
517 GModule *mod = g_module_open ( fn, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL );
520 if ( g_module_symbol ( mod,
"mode", (gpointer *) &m ) ) {
522 g_warning (
"ABI version of plugin: '%s' does not match: %08X expecting: %08X", dn, m->abi_version,
ABI_VERSION );
523 g_module_close ( mod );
528 g_module_close ( mod );
533 g_warning (
"Symbol 'mode' not found in module: %s", dn );
534 g_module_close ( mod );
538 g_warning (
"Failed to open 'mode' plugin: '%s', error: %s", dn, g_module_error () );
563 if (
find_arg (
"-no-plugins" ) < 0 ) {
568 const char *path = g_getenv (
"ROFI_PLUGIN_PATH" );
569 if ( path != NULL ) {
570 gchar ** paths = g_strsplit ( path,
":", -1 );
571 for (
unsigned int i = 0; paths[i]; i++ ) {
574 g_strfreev ( paths );
594 g_module_close ( mod );
633 return ( index ==
num_modi ) ? -1 : (int) index;
637 const char *
const sep =
",#";
640 char *switcher_str = g_strdup (
config.
modi );
642 for (
char *token = strtok_r ( switcher_str, sep, &savept ); token != NULL; token = strtok_r ( NULL, sep, &savept ) ) {
648 g_free ( switcher_str );
665 return G_SOURCE_CONTINUE;
669 GString *emesg = g_string_new (
"The following errors were detected when starting rofi:\n" );
672 for (; iter != NULL && index < 2; iter = g_list_next ( iter ) ) {
673 GString *msg = (GString *) ( iter->data );
674 g_string_append ( emesg,
"\n\n" );
675 g_string_append ( emesg, msg->str );
678 if ( g_list_length ( iter ) > 1 ) {
679 g_string_append_printf ( emesg,
"\nThere are <b>%d</b> more errors.", g_list_length ( iter ) - 1 );
682 g_string_free ( emesg, TRUE );
686 static gboolean
startup ( G_GNUC_UNUSED gpointer data )
694 if (
find_arg (
"-normal-window" ) >= 0 ) {
697 TICK_N (
"Grab keyboard" );
699 TICK_N (
"Create Window" );
704 TICK_N (
"Config sanity check" );
708 return G_SOURCE_REMOVE;
723 if (
find_arg (
"-markup" ) >= 0 ) {
747 return G_SOURCE_REMOVE;
759 return G_SOURCE_REMOVE;
762 static gboolean
record ( G_GNUC_UNUSED
void *data )
765 return G_SOURCE_CONTINUE;
775 int main (
int argc,
char *argv[] )
784 g_print (
"Version: "GIT_VERSION
"\n" );
786 g_print (
"Version: "VERSION
"\n" );
792 const char *ro_pid = g_getenv (
"ROFI_OUTSIDE" );
793 if ( ro_pid != NULL ) {
794 int ro_pidi = g_ascii_strtoll ( ro_pid, NULL, 0 );
795 if ( kill ( ro_pidi, 0 ) == 0 ) {
796 printf (
"Do not launch rofi from inside rofi.\r\n" );
811 char *base_name = g_path_get_basename ( argv[0] );
812 const char *
const dmenu_str =
"dmenu";
813 dmenu_mode = ( strcmp ( base_name, dmenu_str ) == 0 );
815 g_free ( base_name );
820 const char *path = g_get_user_runtime_dir ();
822 if ( g_mkdir_with_parents ( path, 0700 ) < 0 ) {
823 g_warning (
"Failed to create user runtime directory: %s with error: %s", path, g_strerror ( errno ) );
824 pidfile = g_build_filename ( g_get_home_dir (),
".rofi.pid", NULL );
827 pidfile = g_build_filename ( path,
"rofi.pid", NULL );
833 const char *cpath = g_get_user_config_dir ();
835 config_path = g_build_filename ( cpath,
"rofi",
"config", NULL );
842 if ( g_str_has_suffix ( c,
".rasi" ) ) {
851 if ( setlocale ( LC_ALL,
"" ) == NULL ) {
852 g_warning (
"Failed to set locale." );
857 TICK_N (
"Setup Locale" );
859 TICK_N (
"Collect MODI" );
863 main_loop = g_main_loop_new ( NULL, FALSE );
865 TICK_N (
"Setup mainloop" );
871 g_warning (
"Connection has error" );
875 TICK_N (
"Setup Display" );
881 if (
find_arg (
"-no-config" ) < 0 ) {
883 gboolean found_system = FALSE;
884 const char *
const * dirs = g_get_system_config_dirs ();
886 for (
unsigned int i = 0; !found_system && dirs[i]; i++ ) {
888 gchar *etc = g_build_filename ( dirs[i],
"rofi.rasi", NULL );
889 g_debug (
"Look for default config file: %s", etc );
890 if ( g_file_test ( etc, G_FILE_TEST_IS_REGULAR ) ) {
891 g_debug (
"Parsing: %s", etc );
897 gchar *xetc = g_build_filename ( dirs[i],
"rofi.conf", NULL );
898 g_debug (
"Look for default config file: %s", xetc );
899 if ( g_file_test ( xetc, G_FILE_TEST_IS_REGULAR ) ) {
909 if ( !found_system ) {
911 gchar *etc = g_build_filename ( SYSCONFDIR,
"rofi.rasi", NULL );
912 g_debug (
"Look for default config file: %s", etc );
913 if ( g_file_test ( etc, G_FILE_TEST_IS_REGULAR ) ) {
914 g_debug (
"Look for default config file: %s", etc );
919 gchar *xetc = g_build_filename ( SYSCONFDIR,
"rofi.conf", NULL );
920 g_debug (
"Look for default config file: %s", xetc );
921 if ( g_file_test ( xetc, G_FILE_TEST_IS_REGULAR ) ) {
941 if ( g_file_test (
config_path, G_FILE_TEST_IS_REGULAR ) ) {
955 TICK_N (
"Parsed theme" );
959 TICK_N (
"Load cmd config " );
962 g_warning (
"The old Xresources based configuration format is deprecated." );
963 g_warning (
"Please upgrade: rofi -upgrade-config." );
974 if ( g_mkdir_with_parents (
cache_dir, 0700 ) < 0 ) {
975 g_warning (
"Failed to create cache directory: %s", g_strerror ( errno ) );
980 char *windowid = NULL;
997 GBytes *theme_data = g_resource_lookup_data (
998 resources_get_resource (),
999 "/org/qtools/rofi/default_theme.rasi",
1000 G_RESOURCE_LOOKUP_FLAGS_NONE,
1003 const char *theme = g_bytes_get_data ( theme_data, NULL );
1005 g_warning (
"Failed to parse default theme. Giving up.." );
1008 iter != NULL; iter = g_list_next ( iter ) ) {
1009 g_warning (
"Error: %s%s%s",
1015 return EXIT_FAILURE;
1017 g_bytes_unref ( theme_data );
1019 rofi_theme_convert_old ();
1027 for (
int index = 0; theme_str && theme_str[index]; index++ ) {
1033 g_free ( theme_str );
1036 if (
find_arg (
"-dump-theme" ) >= 0 ) {
1039 return EXIT_SUCCESS;
1041 if (
find_arg (
"-upgrade-config" ) >= 0 ) {
1044 for (
unsigned int i = 0; i <
num_modi; i++ ) {
1048 const char *cpath = g_get_user_config_dir ();
1050 char *fcpath = g_build_filename ( cpath,
"rofi", NULL );
1051 if ( !g_file_test ( fcpath, G_FILE_TEST_IS_DIR ) && g_mkdir_with_parents ( fcpath, 0700 ) < 0 ) {
1052 g_warning (
"Failed to create rofi configuration directory: %s", fcpath );
1055 return EXIT_FAILURE;
1058 fcpath = g_build_filename ( cpath,
"rofi",
"config.rasi", NULL );
1059 if ( g_file_test ( fcpath, G_FILE_TEST_IS_REGULAR ) ) {
1060 g_warning (
"New configuration file already exists: %s", fcpath );
1063 return EXIT_FAILURE;
1065 FILE *fd = fopen ( fcpath,
"w" );
1067 g_warning (
"Failed to open new rofi configuration file: %s: %s", fcpath, strerror ( errno ) );
1070 return EXIT_FAILURE;
1073 fprintf ( stdout,
"\n***** Generated configuration file in: %s *****\n", fcpath );
1080 g_warning (
"Failed to get user configuration directory." );
1082 return EXIT_FAILURE;
1085 return EXIT_SUCCESS;
1087 if (
find_arg (
"-dump-config" ) >= 0 ) {
1090 return EXIT_SUCCESS;
1095 help ( argc, argv );
1097 return EXIT_SUCCESS;
1099 if (
find_arg (
"-dump-xresources" ) >= 0 ) {
1102 return EXIT_SUCCESS;
1105 unsigned int interval = 1;
1107 g_timeout_add ( 1000 / (
double) interval,
record, NULL );
1109 if (
find_arg (
"-benchmark-ui" ) >= 0 ) {
1114 TICK_N (
"Workers initialize" );
1116 TICK_N (
"Icon fetcher initialize" );
1120 TICK_N (
"Pid file created" );
1123 return EXIT_FAILURE;
1126 TICK_N (
"Text box setup" );
1129 g_warning (
"Failed to properly finish display setup" );
1131 return EXIT_FAILURE;
1133 TICK_N (
"Setup late Display" );
1136 TICK_N (
"Theme setup" );
1150 g_free ( windowid );