1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

First cleanup steps.

This commit is contained in:
Dave Davenport 2016-01-07 19:47:37 +01:00
parent 51b5511017
commit e8daff0f6a
14 changed files with 365 additions and 255 deletions

View file

@ -32,6 +32,7 @@ AM_CFLAGS=\
rofi_SOURCES=\
source/rofi.c\
source/mode.c\
source/keyb.c\
config/config.c\
source/helper.c\
@ -50,6 +51,8 @@ rofi_SOURCES=\
source/dialogs/window.c\
source/dialogs/script.c\
include/rofi.h\
include/mode.h\
include/mode-private.h\
include/settings.h\
include/keyb.h\
include/helper.h\
@ -132,6 +135,8 @@ rofi_test_SOURCES=\
source/history.c\
config/config.c\
include/rofi.h\
include/mode.h\
include/mode-private.h\
include/settings.h\
include/history.h\
test/history-test.c
@ -145,6 +150,8 @@ textbox_test_SOURCES=\
source/helper.c\
include/keyb.h\
include/rofi.h\
include/mode.h\
include/mode-private.h\
include/settings.h\
include/textbox.h\
include/x11-helper.h\
@ -155,6 +162,8 @@ textbox_test_SOURCES=\
helper_test_SOURCES=\
config/config.c\
include/rofi.h\
include/mode.h\
include/mode-private.h\
source/helper.c\
include/helper.h\
include/xrmoptions.h\

79
include/mode-private.h Normal file
View file

@ -0,0 +1,79 @@
#ifndef ROFI_MODE_PRIVATE_H
#define ROFI_MODE_PRIVATE_H
typedef void ( *switcher_free )( Mode *data );
typedef char * ( *switcher_get_display_value )( const Mode *sw, unsigned int selected_line, int *state, int get_entry );
typedef char * ( *switcher_get_completion )( const Mode *sw, unsigned int selected_line );
/**
* @param tokens List of (input) tokens to match.
* @param input The entry to match against.
* @param case_sensitive Whether case is significant.
* @param index The current selected index.
* @param data User data.
*
* Function prototype for the matching algorithm.
*
* @returns 1 when it matches, 0 if not.
*/
typedef int ( *switcher_token_match )( const Mode *data, char **tokens, int not_ascii, int case_sensitive, unsigned int index );
typedef void ( *__mode_init )( Mode *sw );
typedef unsigned int ( *__mode_get_num_entries )( const Mode *sw );
typedef void ( *__mode_destroy )( Mode *sw );
typedef ModeMode ( *switcher_result )( Mode *sw, int menu_retv, char **input, unsigned int selected_line );
typedef int ( *switcher_is_not_ascii )( const Mode *sw, unsigned int index );
/**
* Structure defining a switcher.
* It consists of a name, callback and if enabled
* a textbox for the sidebar-mode.
*/
struct _Mode
{
/** Name (max 31 char long) */
char name[32];
/** Keybindings (keysym and modmask) */
char * keycfg;
char * keystr;
KeySym keysym;
unsigned int modmask;
/**
* A switcher normally consists of the following parts:
*/
/** Initialize the Mode */
__mode_init _init;
/** Destroy the switcher, e.g. free all its memory. */
__mode_destroy _destroy;
/** Get number of entries to display. (unfiltered). */
__mode_get_num_entries _get_num_entries;
/** Check if the element is ascii. */
switcher_is_not_ascii is_not_ascii;
/** Process the result of the user selection. */
switcher_result result;
/** Token match. */
switcher_token_match token_match;
/** Get the string to display for the entry. */
switcher_get_display_value mgrv;
/** Get the 'completed' entry. */
switcher_get_completion get_completion;
/** Pointer to private data. */
void *private_data;
/**
* Free SWitcher
* Only to be used when the switcher object itself is dynamic.
* And has data in `ed`
*/
switcher_free free;
/** Extra fields for script */
void *ed;
};
#endif // ROFI_MODE_PRIVATE_H

87
include/mode.h Normal file
View file

@ -0,0 +1,87 @@
#ifndef ROFI_MODE_H
#define ROFI_MODE_H
/**
* @defgroup MODE Mode
*
* The 'object' that makes a mode in rofi.
* @{
*/
typedef struct _Mode Mode;
/**
* Enum used to sum the possible states of ROFI.
*/
typedef enum
{
/** Exit. */
MODE_EXIT = 1000,
/** Skip to the next cycle-able dialog. */
NEXT_DIALOG = 1001,
/** Reload current DIALOG */
RELOAD_DIALOG = 1002,
/** Previous dialog */
PREVIOUS_DIALOG = 1003
} ModeMode;
/**
* State returned by the rofi window.
*/
typedef enum
{
/** Entry is selected. */
MENU_OK = 0x00010000,
/** User canceled the operation. (e.g. pressed escape) */
MENU_CANCEL = 0x00020000,
/** User requested a mode switch */
MENU_NEXT = 0x00040000,
/** Custom (non-matched) input was entered. */
MENU_CUSTOM_INPUT = 0x00080000,
/** User wanted to delete entry from history. */
MENU_ENTRY_DELETE = 0x00100000,
/** User wants to jump to another switcher. */
MENU_QUICK_SWITCH = 0x00200000,
/** Go to the previous menu. */
MENU_PREVIOUS = 0x00400000,
/** Modifiers */
MENU_SHIFT = 0x10000000,
/** Mask */
MENU_LOWER_MASK = 0x0000FFFF
} MenuReturn;
/**
* @param mode The mode to initialize
*
* Initialize mode
*/
void mode_init ( Mode *mode );
/**
* @param mode The mode to destroy
*
* Destroy the mode
*/
void mode_destroy ( Mode *mode );
/**
* @param mode The mode to query
*
* Get the number of entries in the mode.
*
* @returns an unsigned in with the number of entries.
*/
unsigned int mode_get_num_entries ( const Mode *sw );
/**
* @param mode The mode to query
* @param selected_line The entry to query
* @param state The state of the entry [out]
* @param get_entry If the should be returned.
*
* Returns the string as it should be displayed for the entry and the state of how it should be displayed.
*
* @returns allocated new string and state when get_entry is TRUE otherwise just the state.
*/
char * mode_get_display_value ( const Mode *mode, unsigned int selected_line, int *state, int get_entry );
/*@}*/
#endif

View file

@ -9,6 +9,7 @@
#include <cairo-xlib.h>
#include "timings.h"
#include "keyb.h"
#include "mode.h"
/**
* @defgroup Widgets Widgets
@ -23,53 +24,13 @@
*/
extern const char *cache_dir;
typedef struct _Mode Mode;
/**
* Enum used to sum the possible states of ROFI.
*/
typedef enum
{
/** Exit. */
MODE_EXIT = 1000,
/** Skip to the next cycle-able dialog. */
NEXT_DIALOG = 1001,
/** Reload current DIALOG */
RELOAD_DIALOG = 1002,
/** Previous dialog */
PREVIOUS_DIALOG = 1003
} ModeMode;
/**
* State returned by the rofi window.
*/
typedef enum
{
/** Entry is selected. */
MENU_OK = 0x00010000,
/** User canceled the operation. (e.g. pressed escape) */
MENU_CANCEL = 0x00020000,
/** User requested a mode switch */
MENU_NEXT = 0x00040000,
/** Custom (non-matched) input was entered. */
MENU_CUSTOM_INPUT = 0x00080000,
/** User wanted to delete entry from history. */
MENU_ENTRY_DELETE = 0x00100000,
/** User wants to jump to another switcher. */
MENU_QUICK_SWITCH = 0x00200000,
/** Go to the previous menu. */
MENU_PREVIOUS = 0x00400000,
/** Modifiers */
MENU_SHIFT = 0x10000000,
/** Mask */
MENU_LOWER_MASK = 0x0000FFFF
} MenuReturn;
/**
* @param sig The caught signal
* @param msg The error message to show.
* @param markup The error message uses pango markup.
*
* Catch the exit signal generated by X.
* The error message to show.
*/
void catch_exit ( __attribute__( ( unused ) ) int sig );
void error_dialog ( const char *msg, int markup );
/**
* @param sw the Mode to show.
@ -90,90 +51,6 @@ void catch_exit ( __attribute__( ( unused ) ) int sig );
MenuReturn menu ( Mode *sw, char **input, char *prompt, unsigned int *selected_line, unsigned int *next_pos, const char *message )
__attribute__ ( ( nonnull ( 1, 2, 3, 4 ) ) );
/**
* @param msg The error message to show.
* @param markup The error message uses pango markup.
*
* The error message to show.
*/
void error_dialog ( const char *msg, int markup );
typedef void ( *switcher_free )( Mode *data );
typedef char * ( *switcher_get_display_value )( const Mode *sw, unsigned int selected_line, int *state, int get_entry );
typedef char * ( *switcher_get_completion )( const Mode *sw, unsigned int selected_line );
/**
* @param tokens List of (input) tokens to match.
* @param input The entry to match against.
* @param case_sensitive Whether case is significant.
* @param index The current selected index.
* @param data User data.
*
* Function prototype for the matching algorithm.
*
* @returns 1 when it matches, 0 if not.
*/
typedef int ( *switcher_token_match )( const Mode *data, char **tokens, int not_ascii, int case_sensitive, unsigned int index );
typedef void ( *switcher_init )( Mode *sw );
typedef unsigned int ( *switcher_get_num_entries )( const Mode *sw );
typedef void ( *switcher_destroy )( Mode *sw );
typedef ModeMode ( *switcher_result )( Mode *sw, int menu_retv, char **input, unsigned int selected_line );
typedef int ( *switcher_is_not_ascii )( const Mode *sw, unsigned int index );
/**
* Structure defining a switcher.
* It consists of a name, callback and if enabled
* a textbox for the sidebar-mode.
*/
struct _Mode
{
/** Name (max 31 char long) */
char name[32];
/** Keybindings (keysym and modmask) */
char * keycfg;
char * keystr;
KeySym keysym;
unsigned int modmask;
/**
* A switcher normally consists of the following parts:
*/
/** Initialize the Mode */
switcher_init init;
/** Destroy the switcher, e.g. free all its memory. */
switcher_destroy destroy;
/** Get number of entries to display. (unfiltered). */
switcher_get_num_entries get_num_entries;
/** Check if the element is ascii. */
switcher_is_not_ascii is_not_ascii;
/** Process the result of the user selection. */
switcher_result result;
/** Token match. */
switcher_token_match token_match;
/** Get the string to display for the entry. */
switcher_get_display_value mgrv;
/** Get the 'completed' entry. */
switcher_get_completion get_completion;
/** Pointer to private data. */
void *private_data;
/**
* Free SWitcher
* Only to be used when the switcher object itself is dynamic.
* And has data in `ed`
*/
switcher_free free;
/** Extra fields for script */
void *ed;
};
/** Reset terminal */
#define color_reset "\033[0m"
/** Set terminal text bold */
@ -182,6 +59,15 @@ struct _Mode
#define color_italic "\033[2m"
/** Set terminal foreground text green */
#define color_green "\033[0;32m"
/**
* @param msg The error message to show.
* @param markup If the message contains pango markup.
*
* Create a dialog showing the msg.
*
* @returns EXIT_FAILURE if failed to create dialog, EXIT_SUCCESS if succesfull
*/
int show_error_message ( const char *msg, int markup );
/*@}*/
#endif

View file

@ -32,6 +32,7 @@
#include <dialogs/dialogs.h>
#include "mode-private.h"
/**
* Combi Mode
*/
@ -108,12 +109,12 @@ static void combi_mode_init ( Mode *sw )
pd->starts = g_malloc0 ( sizeof ( int ) * pd->num_switchers );
pd->lengths = g_malloc0 ( sizeof ( int ) * pd->num_switchers );
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
pd->switchers[i]->init ( pd->switchers[i] );
mode_init ( pd->switchers[i] );
}
if ( pd->cmd_list_length == 0 ) {
pd->cmd_list_length = 0;
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
unsigned int length = pd->switchers[i]->get_num_entries ( pd->switchers[i] );;
unsigned int length = mode_get_num_entries ( pd->switchers[i] );
pd->starts[i] = pd->cmd_list_length;
pd->lengths[i] = length;
pd->cmd_list_length += length;
@ -134,7 +135,7 @@ static void combi_mode_destroy ( Mode *sw )
g_free ( pd->lengths );
// Cleanup switchers.
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
pd->switchers[i]->destroy ( pd->switchers[i] );
mode_destroy ( pd->switchers[i] );
}
g_free ( pd->switchers );
g_free ( pd );
@ -264,10 +265,10 @@ Mode combi_mode =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = combi_mode_init,
.get_num_entries = combi_mode_get_num_entries,
._init = combi_mode_init,
._get_num_entries = combi_mode_get_num_entries,
.result = combi_mode_result,
.destroy = combi_mode_destroy,
._destroy = combi_mode_destroy,
.token_match = combi_mode_match,
.get_completion = combi_get_completion,
.mgrv = combi_mgrv,

View file

@ -40,6 +40,7 @@
#include "helper.h"
#include "xrmoptions.h"
#include "mode-private.h"
// We limit at 1000000 rows for now.
#define DMENU_MAX_ROWS 1000000
@ -330,10 +331,10 @@ Mode dmenu_mode =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = dmenu_mode_init,
.get_num_entries = dmenu_mode_get_num_entries,
._init = dmenu_mode_init,
._get_num_entries = dmenu_mode_get_num_entries,
.result = NULL,
.destroy = dmenu_mode_free,
._destroy = dmenu_mode_free,
.token_match = dmenu_token_match,
.mgrv = get_display_data,
.get_completion = NULL,
@ -344,7 +345,7 @@ Mode dmenu_mode =
int dmenu_switcher_dialog ( void )
{
dmenu_mode.init ( &dmenu_mode );
mode_init ( &dmenu_mode );
DmenuModePrivateData *pd = (DmenuModePrivateData *) dmenu_mode.private_data;
char *input = NULL;
int retv = FALSE;
@ -468,7 +469,7 @@ int dmenu_switcher_dialog ( void )
} while ( restart );
g_free ( input );
dmenu_mode.destroy ( &dmenu_mode );
mode_destroy ( &dmenu_mode );
return retv;
}

View file

@ -43,6 +43,8 @@
#include "helper.h"
#include "dialogs/drun.h"
#include "mode-private.h"
#define RUN_CACHE_FILE "rofi-2.runcache"
static inline int execsh ( const char *cmd, int run_in_term )
@ -363,10 +365,10 @@ Mode drun_mode =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = drun_mode_init,
.get_num_entries = drun_mode_get_num_entries,
._init = drun_mode_init,
._get_num_entries = drun_mode_get_num_entries,
.result = drun_mode_result,
.destroy = drun_mode_destroy,
._destroy = drun_mode_destroy,
.token_match = drun_token_match,
.get_completion = drun_get_completion,
.mgrv = mgrv,

View file

@ -49,6 +49,7 @@
#include "history.h"
#include "dialogs/run.h"
#include "mode-private.h"
/**
* Name of the history file where previously choosen commands are stored.
*/
@ -384,10 +385,10 @@ Mode run_mode =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = run_mode_init,
.get_num_entries = run_mode_get_num_entries,
._init = run_mode_init,
._get_num_entries = run_mode_get_num_entries,
.result = run_mode_result,
.destroy = run_mode_destroy,
._destroy = run_mode_destroy,
.token_match = run_token_match,
.mgrv = mgrv,
.get_completion = NULL,

View file

@ -38,6 +38,7 @@
#include "dialogs/script.h"
#include "helper.h"
#include "mode-private.h"
static char **get_script_output ( const char *command, unsigned int *length )
{
char **retv = NULL;
@ -194,10 +195,10 @@ Mode *script_switcher_parse_setup ( const char *str )
sw->free = script_switcher_free;
sw->keysym = None;
sw->modmask = AnyModifier;
sw->init = script_mode_init;
sw->get_num_entries = script_mode_get_num_entries;
sw->_init = script_mode_init;
sw->_get_num_entries = script_mode_get_num_entries;
sw->result = script_mode_result;
sw->destroy = script_mode_destroy;
sw->_destroy = script_mode_destroy;
sw->token_match = script_token_match;
sw->get_completion = NULL,
sw->mgrv = mgrv;

View file

@ -48,6 +48,7 @@
#include "history.h"
#include "dialogs/ssh.h"
#include "mode-private.h"
/**
* Name of the history file where previously choosen hosts are stored.
*/
@ -493,10 +494,10 @@ Mode ssh_mode =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = ssh_mode_init,
.get_num_entries = ssh_mode_get_num_entries,
._init = ssh_mode_init,
._get_num_entries = ssh_mode_get_num_entries,
.result = ssh_mode_result,
.destroy = ssh_mode_destroy,
._destroy = ssh_mode_destroy,
.token_match = ssh_token_match,
.mgrv = mgrv,
.get_completion = NULL,

View file

@ -44,6 +44,7 @@
#include "x11-helper.h"
#include "i3-support.h"
#include "dialogs/window.h"
#include "mode-private.h"
#define WINLIST 32
@ -589,10 +590,10 @@ Mode window_mode =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = window_mode_init,
.get_num_entries = window_mode_get_num_entries,
._init = window_mode_init,
._get_num_entries = window_mode_get_num_entries,
.result = window_mode_result,
.destroy = window_mode_destroy,
._destroy = window_mode_destroy,
.token_match = window_match,
.mgrv = mgrv,
.get_completion = NULL,
@ -606,10 +607,10 @@ Mode window_mode_cd =
.keycfg = NULL,
.keystr = NULL,
.modmask = AnyModifier,
.init = window_mode_init_cd,
.get_num_entries = window_mode_get_num_entries,
._init = window_mode_init_cd,
._get_num_entries = window_mode_get_num_entries,
.result = window_mode_result,
.destroy = window_mode_destroy,
._destroy = window_mode_destroy,
.token_match = window_match,
.mgrv = mgrv,
.get_completion = NULL,

39
source/mode.c Normal file
View file

@ -0,0 +1,39 @@
#include "rofi.h"
#include "mode.h"
// This one should only be in mode implementations.
#include "mode-private.h"
/**
* @ingroup MODE
* @{
*/
void mode_init ( Mode *mode )
{
g_assert ( mode != NULL );
g_assert ( mode->_init != NULL );
mode->_init ( mode );
}
void mode_destroy ( Mode *mode )
{
g_assert ( mode != NULL );
g_assert ( mode->_destroy != NULL );
mode->_destroy ( mode );
}
unsigned int mode_get_num_entries ( const Mode *mode )
{
g_assert ( mode != NULL );
g_assert ( mode->_get_num_entries != NULL );
return mode->_get_num_entries ( mode );
}
char * mode_get_display_value ( const Mode *mode, unsigned int selected_line, int *state, int get_entry )
{
g_assert ( mode != NULL );
g_assert ( mode->mgrv != NULL );
return mode->mgrv ( mode, selected_line, state, get_entry );
}
/*@}*/

View file

@ -60,6 +60,8 @@
#include "xrmoptions.h"
#include "dialogs/dialogs.h"
// This one should only be in mode implementations.
#include "mode-private.h"
ModeMode switcher_run ( char **input, Mode *sw );
typedef enum _MainLoopEvent
@ -1018,7 +1020,7 @@ static void menu_draw ( MenuState *state, cairo_t *d )
{
TextBoxFontType type = ( ( ( i % state->max_rows ) & 1 ) == 0 ) ? NORMAL : ALT;
int fstate = 0;
char *text = state->sw->mgrv ( state->sw, state->line_map[i + offset], &fstate, TRUE );
char *text = mode_get_display_value ( state->sw, state->line_map[i + offset], &fstate, TRUE );
TextBoxFontType tbft = fstate | ( ( i + offset ) == state->selected ? HIGHLIGHT : type );
textbox_font ( state->boxes[i], tbft );
textbox_text ( state->boxes[i], text );
@ -1033,7 +1035,7 @@ static void menu_draw ( MenuState *state, cairo_t *d )
for ( i = 0; i < max_elements; i++ ) {
TextBoxFontType type = ( ( ( i % state->max_rows ) & 1 ) == 0 ) ? NORMAL : ALT;
int fstate = 0;
state->sw->mgrv ( state->sw, state->line_map[i + offset], &fstate, FALSE );
mode_get_display_value ( state->sw, state->line_map[i + offset], &fstate, FALSE );
TextBoxFontType tbft = fstate | ( ( i + offset ) == state->selected ? HIGHLIGHT : type );
textbox_font ( state->boxes[i], tbft );
textbox_draw ( state->boxes[i], d );
@ -1274,7 +1276,7 @@ MenuReturn menu ( Mode *sw, char **input, char *prompt, unsigned int *selected_l
.border = config.padding + config.menu_bw
};
// Request the lines to show.
state.num_lines = sw->get_num_entries ( sw );
state.num_lines = mode_get_num_entries ( sw );
state.lines_not_ascii = g_malloc0_n ( state.num_lines, sizeof ( int ) );
// find out which lines contain non-ascii codepoints, so we can be faster in some cases.
@ -1887,7 +1889,7 @@ static void run_switcher ( ModeMode mode )
// Otherwise check if requested mode is enabled.
char *input = g_strdup ( config.filter );
for ( unsigned int i = 0; i < num_modi; i++ ) {
modi[i].sw->init ( modi[i].sw );
mode_init ( modi[i].sw );
}
do {
ModeMode retv;
@ -1918,7 +1920,7 @@ static void run_switcher ( ModeMode mode )
} while ( mode != MODE_EXIT );
g_free ( input );
for ( unsigned int i = 0; i < num_modi; i++ ) {
modi[i].sw->destroy ( modi[i].sw );
mode_destroy ( modi[i].sw );
}
// cleanup
teardown ( pfd );