1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-03 04:23:42 -05:00
rofi/source/rofi.c

788 lines
24 KiB
C
Raw Normal View History

/**
2014-03-01 11:27:52 -05:00
* rofi
2014-01-10 04:35:38 -05:00
*
* MIT/X11 License
* Copyright (c) 2012 Sean Pringle <sean.pringle@gmail.com>
2015-12-31 18:27:00 -05:00
* Modified 2013-2016 Qball Cow <qball@gmpclient.org>
2014-01-10 04:35:38 -05:00
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
2014-01-10 04:35:38 -05:00
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
2014-01-10 04:35:38 -05:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <config.h>
2012-06-29 13:10:13 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2014-06-05 11:48:25 -04:00
#include <stdint.h>
2014-01-21 08:56:25 -05:00
#include <errno.h>
2014-03-16 08:47:44 -04:00
#include <time.h>
#include <locale.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xkb.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <xkbcommon/xkbcommon-x11.h>
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <sys/types.h>
2014-03-18 05:55:25 -04:00
2016-01-18 14:51:35 -05:00
#include <glib-unix.h>
#include <libgwater-xcb.h>
2015-09-13 11:29:50 -04:00
#define SN_API_NOT_YET_FROZEN
#include <libsn/sn.h>
2016-01-07 10:01:56 -05:00
#include "settings.h"
#include "mode.h"
2015-02-14 13:42:04 -05:00
#include "rofi.h"
2015-01-30 15:55:31 -05:00
#include "helper.h"
2015-02-14 13:42:04 -05:00
#include "textbox.h"
2015-02-09 13:35:51 -05:00
#include "x11-helper.h"
#include "xrmoptions.h"
2015-08-31 01:57:29 -04:00
#include "dialogs/dialogs.h"
2015-09-19 06:21:30 -04:00
2016-02-07 06:31:17 -05:00
#include "view.h"
#include "view-internal.h"
#include "xkb-internal.h"
2016-02-07 06:31:17 -05:00
// Pidfile.
char *pidfile = NULL;
const char *cache_dir = NULL;
SnDisplay *sndisplay = NULL;
SnLauncheeContext *sncontext = NULL;
xcb_connection_t *xcb_connection = NULL;
xcb_screen_t *xcb_screen = NULL;
struct xkb_stuff xkb = { NULL };
Display *display = NULL;
char *display_str = NULL;
char *config_path = NULL;
// Array of modi.
2016-02-06 08:27:36 -05:00
Mode **modi = NULL;
unsigned int num_modi = 0;
2015-01-17 12:27:41 -05:00
// Current selected switcher.
2016-02-06 08:27:36 -05:00
unsigned int curr_switcher = 0;
2015-09-26 14:34:34 -04:00
2016-02-08 12:16:16 -05:00
GMainLoop *main_loop = NULL;
GWaterXcbSource *main_loop_source = NULL;
2016-02-08 12:16:16 -05:00
gboolean quiet = FALSE;
2016-02-19 13:29:06 -05:00
static int dmenu_mode = FALSE;
int return_code = EXIT_SUCCESS;
void process_result ( RofiViewState *state );
void process_result_error ( RofiViewState *state );
2016-02-19 13:29:06 -05:00
void rofi_set_return_code ( int code )
{
return_code = code;
}
unsigned int rofi_get_num_enabled_modi ( void )
{
return num_modi;
}
const Mode * rofi_get_mode ( unsigned int index )
{
return modi[index];
}
/**
* @param name Name of the switcher to lookup.
*
* Find the index of the switcher with name.
*
* @returns index of the switcher in modi, -1 if not found.
*/
static int switcher_get ( const char *name )
{
for ( unsigned int i = 0; i < num_modi; i++ ) {
2016-02-06 07:40:25 -05:00
if ( strcmp ( mode_get_name ( modi[i] ), name ) == 0 ) {
return i;
}
}
return -1;
}
2015-01-08 07:16:44 -05:00
/**
* Do needed steps to start showing the gui
2015-01-08 07:16:44 -05:00
*/
static int setup ()
{
// Create pid file
int pfd = create_pid_file ( pidfile );
if ( pfd >= 0 ) {
// Request truecolor visual.
create_visual_and_colormap ( display );
2015-09-28 15:41:58 -04:00
textbox_setup ( display );
}
return pfd;
}
2016-02-06 08:27:36 -05:00
/**
* Teardown the gui.
*/
static void teardown ( int pfd )
{
// Cleanup font setup.
2015-01-21 04:04:15 -05:00
textbox_cleanup ( );
2015-07-28 16:22:18 -04:00
// Release the window.
release_keyboard ( display );
2016-02-06 08:27:36 -05:00
// Cleanup view
rofi_view_cleanup ();
2015-07-28 16:22:18 -04:00
// Cleanup pid file.
remove_pid_file ( pfd );
}
2016-02-19 13:29:06 -05:00
static int pfd = -1;
/**
* Start dmenu mode.
*/
static int run_dmenu ()
{
int ret_state = EXIT_FAILURE;
2016-02-19 13:29:06 -05:00
pfd = setup ();
if ( pfd < 0 ) {
return ret_state;
2015-07-28 16:22:18 -04:00
}
// Dmenu modi has a return state.
ret_state = dmenu_switcher_dialog ();
return ret_state;
}
2016-02-07 06:31:17 -05:00
static void __run_switcher_internal ( ModeMode mode, char *input )
{
char *prompt = g_strdup_printf ( "%s:", mode_get_name ( modi[mode] ) );
curr_switcher = mode;
2016-02-19 13:29:06 -05:00
RofiViewState * state = rofi_view_create ( modi[mode], input, prompt, NULL, MENU_NORMAL, process_result );
2016-02-07 06:31:17 -05:00
rofi_view_set_active ( state );
g_free ( prompt );
}
static void run_switcher ( ModeMode mode )
2014-01-20 16:58:10 -05:00
{
pfd = setup ();
if ( pfd < 0 ) {
return;
}
// Otherwise check if requested mode is enabled.
for ( unsigned int i = 0; i < num_modi; i++ ) {
2016-02-06 07:40:25 -05:00
if ( !mode_init ( modi[i] ) ) {
2016-02-08 03:03:11 -05:00
rofi_view_error_dialog ( ERROR_MSG ( "Failed to initialize all the modi." ), ERROR_MSG_MARKUP );
teardown ( pfd );
return;
}
2015-03-27 15:28:53 -04:00
}
2016-02-07 06:31:17 -05:00
char *input = g_strdup ( config.filter );
__run_switcher_internal ( mode, input );
g_free ( input );
}
2016-02-19 13:29:06 -05:00
void process_result ( RofiViewState *state )
{
2016-02-19 13:29:06 -05:00
Mode *sw = state->sw;
if ( sw != NULL ) {
unsigned int selected_line = rofi_view_get_selected_line ( state );;
MenuReturn mretv = rofi_view_get_return_value ( state );
char *input = g_strdup ( rofi_view_get_user_input ( state ) );
rofi_view_set_active ( NULL );
rofi_view_free ( state );
ModeMode retv = mode_result ( sw, mretv, &input, selected_line );
ModeMode mode = curr_switcher;
// Find next enabled
if ( retv == NEXT_DIALOG ) {
mode = ( mode + 1 ) % num_modi;
}
else if ( retv == PREVIOUS_DIALOG ) {
if ( mode == 0 ) {
mode = num_modi - 1;
}
else {
mode = ( mode - 1 ) % num_modi;
}
}
else if ( retv == RELOAD_DIALOG ) {
// do nothing.
}
else if ( retv < MODE_EXIT ) {
mode = ( retv ) % num_modi;
2015-03-27 15:28:53 -04:00
}
else {
2016-02-19 13:29:06 -05:00
mode = retv;
2015-03-27 15:28:53 -04:00
}
2016-02-19 13:29:06 -05:00
if ( mode != MODE_EXIT ) {
/**
* Load in the new mode.
*/
__run_switcher_internal ( mode, input );
g_free ( input );
return;
}
// Cleanup
g_free ( input );
2016-02-19 13:29:06 -05:00
for ( unsigned int i = 0; i < num_modi; i++ ) {
mode_destroy ( modi[i] );
}
}
2016-02-19 13:29:06 -05:00
}
void process_result_error ( RofiViewState *state )
{
rofi_view_set_active ( NULL );
rofi_view_free ( state );
teardown ( pfd );
2016-02-19 13:29:06 -05:00
g_main_loop_quit ( main_loop );
}
2014-01-16 12:47:19 -05:00
int show_error_message ( const char *msg, int markup )
{
int pfd = setup ();
if ( pfd < 0 ) {
return EXIT_FAILURE;
2014-01-20 16:58:10 -05:00
}
2016-02-08 03:03:11 -05:00
rofi_view_error_dialog ( msg, markup );
return EXIT_SUCCESS;
2012-06-29 13:10:13 -04:00
}
/**
2015-10-21 03:42:55 -04:00
* Help function.
*/
2015-10-21 03:42:55 -04:00
static void print_main_application_options ( void )
{
int is_term = isatty ( fileno ( stdout ) );
print_help_msg ( "-no-config", "", "Do not load configuration, use default values.", NULL, is_term );
print_help_msg ( "-quiet", "", "Suppress information messages.", NULL, is_term );
print_help_msg ( "-v,-version", "", "Print the version number and exit.", NULL, is_term );
print_help_msg ( "-dmenu", "", "Start in dmenu mode.", NULL, is_term );
print_help_msg ( "-display", "[string]", "X server to contact.", "${DISPLAY}", is_term );
print_help_msg ( "-h,-help", "", "This help message.", NULL, is_term );
print_help_msg ( "-dump-xresources", "", "Dump the current configuration in Xresources format and exit.", NULL, is_term );
2015-12-10 12:20:04 -05:00
print_help_msg ( "-dump-xresources-theme", "", "Dump the current color scheme in Xresources format and exit.", NULL, is_term );
print_help_msg ( "-e", "[string]", "Show a dialog displaying the passed message and exit.", NULL, is_term );
print_help_msg ( "-markup", "", "Enable pango markup where possible.", NULL, is_term );
print_help_msg ( "-normal-window", "", "In dmenu mode, behave as a normal window. (experimental)", NULL, is_term );
2015-11-14 13:59:56 -05:00
print_help_msg ( "-show", "[mode]", "Show the mode 'mode' and exit. The mode has to be enabled.", NULL, is_term );
2015-10-21 03:42:55 -04:00
}
static void help ( G_GNUC_UNUSED int argc, char **argv )
{
printf ( "%s usage:\n", argv[0] );
printf ( "\t%s [-options ...]\n\n", argv[0] );
2015-10-21 12:58:01 -04:00
printf ( "Command line only options:\n" );
2015-10-21 03:42:55 -04:00
print_main_application_options ();
2015-10-21 12:58:01 -04:00
printf ( "DMENU command line options:\n" );
print_dmenu_options ();
printf ( "Global options:\n" );
print_options ();
printf ( "\n" );
printf ( "For more information see: man rofi\n" );
2015-10-16 02:42:01 -04:00
printf ( "Version: "VERSION "\n" );
printf ( "Bugreports: "PACKAGE_BUGREPORT "\n" );
}
2014-09-23 03:24:24 -04:00
/**
* Function bound by 'atexit'.
* Cleanup globally allocated memory.
*/
2014-05-19 15:58:13 -04:00
static void cleanup ()
2014-05-19 15:02:05 -04:00
{
2016-02-10 13:40:19 -05:00
rofi_view_workers_finalize ();
if ( main_loop != NULL ) {
2016-02-06 07:06:58 -05:00
if ( main_loop_source ) {
g_water_xcb_source_unref ( main_loop_source );
2016-01-27 17:49:56 -05:00
}
g_main_loop_unref ( main_loop );
main_loop = NULL;
}
2014-05-19 15:02:05 -04:00
// Cleanup
2014-06-04 15:29:23 -04:00
if ( display != NULL ) {
2015-09-13 11:29:50 -04:00
if ( sncontext != NULL ) {
sn_launchee_context_unref ( sncontext );
sncontext = NULL;
}
if ( sndisplay != NULL ) {
sn_display_unref ( sndisplay );
sndisplay = NULL;
}
XCloseDisplay ( display );
display = NULL;
2014-05-19 15:02:05 -04:00
}
2015-07-28 16:22:18 -04:00
2014-05-19 15:58:13 -04:00
// Cleaning up memory allocated by the Xresources file.
2015-02-03 02:21:59 -05:00
config_xresource_free ();
for ( unsigned int i = 0; i < num_modi; i++ ) {
2016-02-06 07:40:25 -05:00
mode_free ( &( modi[i] ) );
}
g_free ( modi );
2015-04-30 16:42:04 -04:00
// Cleanup the custom keybinding
cleanup_abe ();
g_free ( config_path );
2015-11-24 16:53:40 -05:00
TIMINGS_STOP ();
2014-05-19 15:02:05 -04:00
}
2014-05-20 05:41:39 -04:00
2014-09-23 03:24:24 -04:00
/**
* Parse the switcher string, into internal array of type Mode.
2014-09-23 03:24:24 -04:00
*
* String is split on separator ','
* First the three build-in modi are checked: window, run, ssh
* if that fails, a script-switcher is created.
*/
2016-02-04 03:20:10 -05:00
static int add_mode ( const char * token )
{
unsigned int index = num_modi;
// Resize and add entry.
2016-02-06 07:40:25 -05:00
modi = (Mode * *) g_realloc ( modi, sizeof ( Mode* ) * ( num_modi + 1 ) );
2016-02-04 03:20:10 -05:00
// Window switcher.
#ifdef WINDOW_MODE
if ( strcasecmp ( token, "window" ) == 0 ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = &window_mode;
2016-02-04 03:20:10 -05:00
num_modi++;
}
else if ( strcasecmp ( token, "windowcd" ) == 0 ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = &window_mode_cd;
2016-02-04 03:20:10 -05:00
num_modi++;
}
else
#endif // WINDOW_MODE
// SSh dialog
if ( strcasecmp ( token, "ssh" ) == 0 ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = &ssh_mode;
2016-02-04 03:20:10 -05:00
num_modi++;
}
// Run dialog
else if ( strcasecmp ( token, "run" ) == 0 ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = &run_mode;
2016-02-04 03:20:10 -05:00
num_modi++;
}
else if ( strcasecmp ( token, "drun" ) == 0 ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = &drun_mode;
2016-02-04 03:20:10 -05:00
num_modi++;
}
// combi dialog
else if ( strcasecmp ( token, "combi" ) == 0 ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = &combi_mode;
2016-02-04 03:20:10 -05:00
num_modi++;
}
else {
// If not build in, use custom modi.
Mode *sw = script_switcher_parse_setup ( token );
if ( sw != NULL ) {
2016-02-06 07:40:25 -05:00
modi[num_modi] = sw;
2016-02-04 03:20:10 -05:00
mode_set_config ( sw );
num_modi++;
}
else{
// Report error, don't continue.
fprintf ( stderr, "Invalid script switcher: %s\n", token );
token = NULL;
}
}
return ( index == num_modi ) ? -1 : (int) index;
}
static void setup_modi ( void )
{
char *savept = NULL;
2015-01-08 07:16:44 -05:00
// Make a copy, as strtok will modify it.
char *switcher_str = g_strdup ( config.modi );
// Split token on ','. This modifies switcher_str.
2015-09-19 06:57:48 -04:00
for ( char *token = strtok_r ( switcher_str, ",", &savept ); token != NULL; token = strtok_r ( NULL, ",", &savept ) ) {
2016-02-04 03:20:10 -05:00
add_mode ( token );
}
// Free string that was modified by strtok_r
g_free ( switcher_str );
// We cannot do this in main loop, as we create pointer to string,
// and re-alloc moves that pointer.
mode_set_config ( &ssh_mode );
mode_set_config ( &run_mode );
mode_set_config ( &drun_mode );
#ifdef WINDOW_MODE
mode_set_config ( &window_mode );
mode_set_config ( &window_mode_cd );
#endif // WINDOW_MODE
mode_set_config ( &combi_mode );
}
/**
* @param display Pointer to the X connection to use.
* Load configuration.
* Following priority: (current), X, commandline arguments
*/
static inline void load_configuration ( Display *display )
{
// Load in config from X resources.
2015-02-03 02:21:59 -05:00
config_parse_xresource_options ( display );
config_parse_xresource_options_file ( config_path );
// Parse command line for settings.
2015-03-11 13:32:37 -04:00
config_parse_cmd_options ( );
}
static inline void load_configuration_dynamic ( Display *display )
{
// Load in config from X resources.
config_parse_xresource_options_dynamic ( display );
config_parse_xresource_options_dynamic_file ( config_path );
2015-03-11 13:32:37 -04:00
config_parse_cmd_options_dynamic ( );
}
2015-08-01 10:10:52 -04:00
/**
* Process X11 events in the main-loop (gui-thread) of the application.
*/
static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UNUSED gpointer data )
2015-08-01 10:10:52 -04:00
{
2016-02-08 12:16:16 -05:00
RofiViewState *state = rofi_view_get_active ();
if ( sndisplay != NULL ) {
sn_xcb_display_process_event ( sndisplay, ev );
}
2016-02-08 12:16:16 -05:00
if ( state != NULL ) {
rofi_view_itterrate ( state, ev, &xkb );
2016-02-08 12:16:16 -05:00
if ( rofi_view_get_completed ( state ) ) {
// This menu is done.
2016-02-08 12:16:16 -05:00
rofi_view_finalize ( state );
2016-02-19 13:29:06 -05:00
// cleanup
if ( rofi_view_get_active () == NULL ) {
teardown ( pfd );
g_main_loop_quit ( main_loop );
2016-02-19 13:29:06 -05:00
}
}
}
2016-01-18 15:43:33 -05:00
return G_SOURCE_CONTINUE;
2015-08-01 10:10:52 -04:00
}
/**
* Process signals in the main-loop (gui-thread) of the application.
*
* returns TRUE when mainloop should be stopped.
*/
2016-02-07 14:38:34 -05:00
2016-01-19 02:10:17 -05:00
static gboolean main_loop_signal_handler_int ( G_GNUC_UNUSED gpointer data )
2016-01-18 14:51:35 -05:00
{
// Break out of loop.
g_main_loop_quit ( main_loop );
return G_SOURCE_CONTINUE;
}
static int error_trap_depth = 0;
static void error_trap_push ( G_GNUC_UNUSED SnDisplay *display, G_GNUC_UNUSED Display *xdisplay )
{
++error_trap_depth;
}
2015-09-13 11:29:50 -04:00
static void error_trap_pop ( G_GNUC_UNUSED SnDisplay *display, Display *xdisplay )
{
if ( error_trap_depth == 0 ) {
fprintf ( stderr, "Error trap underflow!\n" );
2015-11-14 13:52:15 -05:00
exit ( EXIT_FAILURE );
2015-09-13 11:29:50 -04:00
}
XSync ( xdisplay, False ); /* get all errors out of the queue */
--error_trap_depth;
}
2016-02-07 14:38:34 -05:00
2016-02-19 13:29:06 -05:00
static gboolean startup ( G_GNUC_UNUSED gpointer data )
{
2016-02-19 13:50:48 -05:00
TICK_N ( "Startup" );
2016-02-19 13:29:06 -05:00
// flags to run immediately and exit
char *sname = NULL;
char *msg = NULL;
2016-02-19 13:50:48 -05:00
//
// Sanity check
if ( config_sanity_check ( display ) ) {
return G_SOURCE_REMOVE;
}
TICK_N ( "Config sanity check" );
// Parse the keybindings.
parse_keys_abe ();
// Check if there is error dialog.
if ( rofi_view_get_active ( ) != NULL ) {
return G_SOURCE_REMOVE;
}
TICK_N ( "Parse ABE" );
2016-02-19 13:29:06 -05:00
// Dmenu mode.
if ( dmenu_mode == TRUE ) {
// force off sidebar mode:
config.sidebar_mode = FALSE;
int retv = run_dmenu ();
if ( retv ) {
2016-02-19 13:50:48 -05:00
rofi_set_return_code ( EXIT_SUCCESS );
2016-02-19 13:29:06 -05:00
// Directly exit.
2016-02-19 13:50:48 -05:00
g_main_loop_quit ( main_loop );
2016-02-19 13:29:06 -05:00
}
}
else if ( find_arg_str ( "-e", &( msg ) ) ) {
int markup = FALSE;
if ( find_arg ( "-markup" ) >= 0 ) {
markup = TRUE;
}
show_error_message ( msg, markup );
}
else if ( find_arg_str ( "-show", &sname ) == TRUE ) {
int index = switcher_get ( sname );
if ( index < 0 ) {
// Add it to the list
index = add_mode ( sname );
// Complain
if ( index >= 0 ) {
fprintf ( stdout, "Mode %s not enabled. Please add it to the list of enabled modi: %s\n",
sname, config.modi );
fprintf ( stdout, "Adding mode: %s\n", sname );
}
// Run it anyway if found.
}
if ( index >= 0 ) {
run_switcher ( index );
}
else {
fprintf ( stderr, "The %s switcher has not been enabled\n", sname );
return G_SOURCE_REMOVE;
}
}
else{
// Daemon mode
fprintf ( stderr, "Rofi daemon mode is now removed.\n" );
fprintf ( stderr, "Please use your window manager binding functionality or xbindkeys to replace it.\n" );
2016-02-19 13:29:06 -05:00
}
return G_SOURCE_REMOVE;
}
2014-05-22 03:33:32 -04:00
int main ( int argc, char *argv[] )
{
2015-11-24 16:53:40 -05:00
TIMINGS_START ();
2015-03-11 13:32:37 -04:00
cmd_set_arguments ( argc, argv );
// Quiet flag
quiet = ( find_arg ( "-quiet" ) >= 0 );
2015-02-03 02:21:59 -05:00
// Version
2015-03-11 13:32:37 -04:00
if ( find_arg ( "-v" ) >= 0 || find_arg ( "-version" ) >= 0 ) {
2015-02-03 02:21:59 -05:00
fprintf ( stdout, "Version: "VERSION "\n" );
exit ( EXIT_SUCCESS );
}
// Detect if we are in dmenu mode.
// This has two possible causes.
// 1 the user specifies it on the command-line.
2015-03-11 13:32:37 -04:00
if ( find_arg ( "-dmenu" ) >= 0 ) {
dmenu_mode = TRUE;
}
// 2 the binary that executed is called dmenu (e.g. symlink to rofi)
else{
// Get the base name of the executable called.
char *base_name = g_path_get_basename ( argv[0] );
dmenu_mode = ( strcmp ( base_name, "dmenu" ) == 0 );
// Free the basename for dmenu detection.
g_free ( base_name );
}
2015-11-24 16:53:40 -05:00
TICK ();
2014-05-20 05:41:39 -04:00
// Get the path to the cache dir.
cache_dir = g_get_user_cache_dir ();
// Create pid file path.
const char *path = g_get_user_runtime_dir ();
if ( path ) {
pidfile = g_build_filename ( path, "rofi.pid", NULL );
}
2015-10-16 02:42:01 -04:00
config_parser_add_option ( xrm_String, "pid", (void * *) &pidfile, "Pidfile location" );
if ( find_arg ( "-config" ) < 0 ) {
const char *cpath = g_get_user_config_dir ();
if ( cpath ) {
config_path = g_build_filename ( cpath, "rofi", "config", NULL );
}
}
else {
char *c = NULL;
find_arg_str ( "-config", &c );
config_path = rofi_expand_path ( c );
}
2015-11-24 16:53:40 -05:00
TICK ();
2014-05-20 05:41:39 -04:00
// Register cleanup function.
atexit ( cleanup );
2014-05-19 15:02:05 -04:00
2015-11-24 16:53:40 -05:00
TICK ();
2015-02-09 13:35:51 -05:00
// Get DISPLAY, first env, then argument.
2014-11-04 10:37:05 -05:00
display_str = getenv ( "DISPLAY" );
2015-03-11 13:32:37 -04:00
find_arg_str ( "-display", &display_str );
2014-01-15 03:27:03 -05:00
2015-11-22 15:10:39 -05:00
if ( setlocale ( LC_ALL, "" ) == NULL ) {
fprintf ( stderr, "Failed to set locale.\n" );
return EXIT_FAILURE;
}
2015-11-22 15:10:39 -05:00
2015-04-23 16:14:31 -04:00
if ( !XSupportsLocale () ) {
fprintf ( stderr, "X11 does not support locales\n" );
2016-01-29 15:56:52 -05:00
return EXIT_FAILURE;
2015-04-23 16:14:31 -04:00
}
if ( XSetLocaleModifiers ( "@im=none" ) == NULL ) {
fprintf ( stderr, "Failed to set locale modifier.\n" );
2016-01-29 15:56:52 -05:00
return EXIT_FAILURE;
2015-04-23 16:14:31 -04:00
}
2014-06-04 15:29:23 -04:00
if ( !( display = XOpenDisplay ( display_str ) ) ) {
2014-03-22 16:04:19 -04:00
fprintf ( stderr, "cannot open display!\n" );
2014-01-10 04:35:38 -05:00
return EXIT_FAILURE;
}
2015-11-24 16:53:40 -05:00
TICK_N ( "Open Display" );
xcb_connection = XGetXCBConnection ( display );
xcb_screen = xcb_aux_get_screen ( xcb_connection, DefaultScreen ( display ) );
if ( xkb_x11_setup_xkb_extension ( xcb_connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION,
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xkb.first_event, NULL ) < 0 ) {
fprintf ( stderr, "cannot setup XKB extension!\n" );
return EXIT_FAILURE;
}
xkb.context = xkb_context_new ( XKB_CONTEXT_NO_FLAGS );
if ( xkb.context == NULL ) {
fprintf ( stderr, "cannot create XKB context!\n" );
return EXIT_FAILURE;
}
xkb.xcb_connection = xcb_connection;
xkb.device_id = xkb_x11_get_core_keyboard_device_id ( xcb_connection );
enum
{
required_events =
( XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
XCB_XKB_EVENT_TYPE_STATE_NOTIFY ),
required_nkn_details =
( XCB_XKB_NKN_DETAIL_KEYCODES ),
required_map_parts =
( XCB_XKB_MAP_PART_KEY_TYPES |
XCB_XKB_MAP_PART_KEY_SYMS |
XCB_XKB_MAP_PART_MODIFIER_MAP |
XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
XCB_XKB_MAP_PART_KEY_ACTIONS |
XCB_XKB_MAP_PART_VIRTUAL_MODS |
XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP ),
required_state_details =
( XCB_XKB_STATE_PART_MODIFIER_BASE |
XCB_XKB_STATE_PART_MODIFIER_LATCH |
XCB_XKB_STATE_PART_MODIFIER_LOCK |
XCB_XKB_STATE_PART_GROUP_BASE |
XCB_XKB_STATE_PART_GROUP_LATCH |
XCB_XKB_STATE_PART_GROUP_LOCK ),
};
static const xcb_xkb_select_events_details_t details = {
.affectNewKeyboard = required_nkn_details,
.newKeyboardDetails = required_nkn_details,
.affectState = required_state_details,
.stateDetails = required_state_details,
};
xcb_xkb_select_events ( xcb_connection, xkb.device_id, required_events, /* affectWhich */
0, /* clear */
0, /* selectAll */
required_map_parts, /* affectMap */
required_map_parts, /* map */
&details );
xkb.keymap = xkb_x11_keymap_new_from_device ( xkb.context, xcb_connection, xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS );
xkb.state = xkb_x11_state_new_from_device ( xkb.keymap, xcb_connection, xkb.device_id );
xkb.compose.table = xkb_compose_table_new_from_locale ( xkb.context, setlocale ( LC_CTYPE, NULL ), 0 );
xkb.compose.state = xkb_compose_state_new ( xkb.compose.table, 0 );
main_loop = g_main_loop_new ( NULL, FALSE );
TICK_N ( "Setup mainloop" );
2015-09-13 11:29:50 -04:00
// startup not.
sndisplay = sn_display_new ( display, error_trap_push, error_trap_pop );
if ( sndisplay != NULL ) {
sncontext = sn_launchee_context_new_from_environment ( sndisplay, DefaultScreen ( display ) );
}
2015-11-24 16:53:40 -05:00
TICK_N ( "Startup Notification" );
2016-01-04 01:59:30 -05:00
// Initialize Xresources subsystem.
config_parse_xresource_init ();
TICK_N ( "Initialize Xresources system" );
2015-04-30 16:42:04 -04:00
// Setup keybinding
setup_abe ();
2015-11-24 16:53:40 -05:00
TICK_N ( "Setup abe" );
2015-04-30 16:42:04 -04:00
if ( find_arg ( "-no-config" ) < 0 ) {
load_configuration ( display );
}
if ( !dmenu_mode ) {
// setup_modi
setup_modi ();
}
else {
// Add dmenu options.
2015-11-14 13:52:15 -05:00
config_parser_add_option ( xrm_Char, "sep", (void * *) &( config.separator ), "Element separator" );
}
if ( find_arg ( "-no-config" ) < 0 ) {
// Reload for dynamic part.
load_configuration_dynamic ( display );
}
// Dump.
// catch help request
if ( find_arg ( "-h" ) >= 0 || find_arg ( "-help" ) >= 0 || find_arg ( "--help" ) >= 0 ) {
help ( argc, argv );
exit ( EXIT_SUCCESS );
}
2015-03-11 13:32:37 -04:00
if ( find_arg ( "-dump-xresources" ) >= 0 ) {
2016-01-07 07:32:33 -05:00
config_parse_xresource_dump ();
exit ( EXIT_SUCCESS );
}
2015-12-10 12:20:04 -05:00
if ( find_arg ( "-dump-xresources-theme" ) >= 0 ) {
2016-01-07 07:32:33 -05:00
config_parse_xresources_theme_dump ();
2015-12-10 12:20:04 -05:00
exit ( EXIT_SUCCESS );
}
2016-02-19 13:50:48 -05:00
x11_setup ( display, &xkb );
main_loop_source = g_water_xcb_source_new_for_connection ( NULL, xcb_connection, main_loop_x11_event_handler, NULL, NULL );
2016-02-19 13:50:48 -05:00
TICK_N ( "X11 Setup " );
2014-08-27 13:44:15 -04:00
2016-02-10 13:40:19 -05:00
rofi_view_workers_initialize ();
// Setup signal handling sources.
2016-01-26 02:36:44 -05:00
// SIGINT
g_unix_signal_add ( SIGINT, main_loop_signal_handler_int, NULL );
2016-02-19 13:29:06 -05:00
g_idle_add ( startup, NULL );
2014-01-23 14:33:13 -05:00
// Start mainloop.
g_main_loop_run ( main_loop );
2016-02-19 13:29:06 -05:00
return return_code;
}