/**
* rofi
*
* MIT/X11 License
* Copyright (c) 2012 Sean Pringle <sean.pringle@gmail.com>
* Modified 2013-2016 Qball Cow <qball@gmpclient.org>
*
* 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:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* 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>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <time.h>
#include <locale.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xkb.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <xkbcommon/xkbcommon-x11.h>
#include <sys/types.h>
#include <glib-unix.h>
#include <libgwater-xcb.h>
#include "xcb-internal.h"
#include "xkb-internal.h"
#include "settings.h"
#include "mode.h"
#include "rofi.h"
#include "helper.h"
#include "widgets/textbox.h"
#include "x11-helper.h"
#include "xrmoptions.h"
#include "dialogs/dialogs.h"
#include "view.h"
#include "view-internal.h"
#include "gitconfig.h"
#include "theme.h"
// Pidfile.
char *pidfile = NULL;
const char *cache_dir = NULL;
/** global structure holding the keyboard status */
struct xkb_stuff xkb = {
.xcb_connection = NULL,
.context = NULL,
.keymap = NULL,
.state = NULL,
.compose = {
.table = NULL,
.state = NULL
}
};
/** Path to the configuration file */
char *config_path = NULL;
/** Array holding all activated modi. */
Mode **modi = NULL;
/** Number of activated modi in #modi array */
unsigned int num_modi = 0;
/** Current selected mode */
unsigned int curr_switcher = 0;
/** Glib main loop. */
GMainLoop *main_loop = NULL;
/** GWater xcb source, signalling events from the X server */
GWaterXcbSource *main_loop_source = NULL;
/** Flag indicating we are in dmenu mode. */
static int dmenu_mode = FALSE;
/** Rofi's return code */
int return_code = EXIT_SUCCESS;
void process_result ( RofiViewState *state );
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++ ) {
if ( strcmp ( mode_get_name ( modi[i] ), name ) == 0 ) {
return i;
}
}
return -1;
}
/**
* Do needed steps to start showing the gui
*/
static int setup ()
{
// Create pid file
int pfd = create_pid_file ( pidfile );
if ( pfd >= 0 ) {
// Request truecolor visual.
x11_create_visual_and_colormap ( );
textbox_setup ();
}
return pfd;
}
/**
* Teardown the gui.
*/
static void teardown ( int pfd )
{
// Cleanup font setup.
textbox_cleanup ( );
// Release the window.
release_keyboard ( );
release_pointer ( );
// Cleanup view
rofi_view_cleanup ();
// Cleanup pid file.
remove_pid_file ( pfd );
}
static void run_switcher ( ModeMode mode )
{
// Otherwise check if requested mode is enabled.
for ( unsigned int i = 0; i < num_modi; i++ ) {
if ( !mode_init ( modi[i] ) ) {
rofi_view_error_dialog ( ERROR_MSG ( "Failed to initialize all the modi." ), ERROR_MSG_MARKUP );
return;
}
}
// Error dialog must have been created.
if ( rofi_view_get_active () != NULL ) {
return;
}
curr_switcher = mode;
RofiViewState * state = rofi_view_create ( modi[mode], config.filter, NULL, MENU_PROMPT_COLON, process_result );
if ( state ) {
rofi_view_set_active