mirror of
https://github.com/lbonn/rofi
synced 2024-11-30 15:49:17 +00:00
Factorize some view code
This commit is contained in:
parent
98a9635173
commit
183b6b618e
5 changed files with 348 additions and 669 deletions
|
@ -151,4 +151,11 @@ struct RofiViewState
|
|||
};
|
||||
/** @} */
|
||||
|
||||
void rofi_view_reload_message_bar ( struct RofiViewState *state );
|
||||
void rofi_view_calculate_window_position ( struct RofiViewState *state );
|
||||
int rofi_view_calculate_window_height ( struct RofiViewState *state );
|
||||
void rofi_view_window_update_size ( struct RofiViewState * state );
|
||||
void rofi_view_call_thread ( gpointer data, gpointer user_data );
|
||||
void rofi_view_refilter ( struct RofiViewState *state );
|
||||
|
||||
#endif
|
||||
|
|
|
@ -208,6 +208,8 @@ int rofi_view_error_dialog ( const char *msg, int markup );
|
|||
*/
|
||||
void rofi_view_queue_redraw ( void );
|
||||
|
||||
void rofi_view_calculate_window_position ( RofiViewState *state );
|
||||
|
||||
/**
|
||||
* Cleanup internal data of the view.
|
||||
*/
|
||||
|
@ -315,26 +317,22 @@ void rofi_view_get_size ( RofiViewState * state, gint *width, gint *height );
|
|||
|
||||
typedef struct _view_proxy {
|
||||
RofiViewState* (*create) ( Mode *sw, const char *input, MenuFlags menu_flags, void ( *finalize )( RofiViewState * ) );
|
||||
void (*finalize) ( RofiViewState *state );
|
||||
MenuReturn (*get_return_value) ( const RofiViewState *state );
|
||||
unsigned int (*get_next_position) ( const RofiViewState *state );
|
||||
void (*handle_text) ( RofiViewState *state, char *text );
|
||||
void (*handle_mouse_motion) ( RofiViewState *state, gint x, gint y );
|
||||
void (*maybe_update) ( RofiViewState *state );
|
||||
void (*temp_configure_notify) ( RofiViewState *state, xcb_configure_notify_event_t *xce );
|
||||
void (*temp_click_to_exit) ( RofiViewState *state, xcb_window_t target );
|
||||
void (*frame_callback) ( void );
|
||||
unsigned int (*get_completed) ( const RofiViewState *state );
|
||||
const char * (*get_user_input) ( const RofiViewState *state );
|
||||
void (*set_selected_line) ( RofiViewState *state, unsigned int selected_line );
|
||||
unsigned int (*get_selected_line) ( const RofiViewState *state );
|
||||
void (*restart) ( RofiViewState *state );
|
||||
gboolean (*trigger_action) ( RofiViewState *state, BindingsScope scope, guint action );
|
||||
void (*free) ( RofiViewState *state );
|
||||
RofiViewState * (*get_active) ( void );
|
||||
void (*set_active) ( RofiViewState *state );
|
||||
int (*error_dialog) ( const char *msg, int markup );
|
||||
void (*queue_redraw) ( void );
|
||||
void (*calculate_window_position) ( RofiViewState *state );
|
||||
int (*calculate_window_height) ( RofiViewState *state );
|
||||
void (*window_update_size) ( RofiViewState *state );
|
||||
|
||||
void (*cleanup) ( void );
|
||||
Mode * (*get_mode) ( RofiViewState *state );
|
||||
|
@ -350,8 +348,6 @@ typedef struct _view_proxy {
|
|||
void (*get_current_monitor) ( int *width, int *height );
|
||||
void (*capture_screenshot) ( void );
|
||||
|
||||
void (*ellipsize_start) ( RofiViewState *state );
|
||||
|
||||
void (*set_size) ( RofiViewState * state, gint width, gint height );
|
||||
void (*get_size) ( RofiViewState * state, gint *width, gint *height );
|
||||
} view_proxy;
|
||||
|
|
317
source/view.c
317
source/view.c
|
@ -1,8 +1,22 @@
|
|||
#include <config.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <xcb/xcb.h>
|
||||
#include "keyb.h"
|
||||
#include "display.h"
|
||||
|
||||
#include "settings.h"
|
||||
#include "timings.h"
|
||||
|
||||
#include "view.h"
|
||||
#include "view-internal.h"
|
||||
|
||||
|
@ -20,15 +34,17 @@ RofiViewState *rofi_view_create ( Mode *sw, const char *input, MenuFlags menu_fl
|
|||
}
|
||||
|
||||
void rofi_view_finalize ( RofiViewState *state ) {
|
||||
return proxy->finalize ( state );
|
||||
if ( state && state->finalize != NULL ) {
|
||||
state->finalize ( state );
|
||||
}
|
||||
}
|
||||
|
||||
MenuReturn rofi_view_get_return_value ( const RofiViewState *state ) {
|
||||
return proxy->get_return_value ( state );
|
||||
return state->retv;
|
||||
}
|
||||
|
||||
unsigned int rofi_view_get_next_position ( const RofiViewState *state ) {
|
||||
return proxy->get_next_position ( state );
|
||||
return state->selected_line;
|
||||
}
|
||||
|
||||
void rofi_view_handle_text ( RofiViewState *state, char *text ) {
|
||||
|
@ -36,7 +52,12 @@ void rofi_view_handle_text ( RofiViewState *state, char *text ) {
|
|||
}
|
||||
|
||||
void rofi_view_handle_mouse_motion ( RofiViewState *state, gint x, gint y ) {
|
||||
proxy->handle_mouse_motion ( state, x , y );
|
||||
state->mouse.x = x;
|
||||
state->mouse.y = y;
|
||||
if ( state->mouse.motion_target != NULL ) {
|
||||
widget_xy_to_relative ( state->mouse.motion_target, &x, &y );
|
||||
widget_motion_notify ( state->mouse.motion_target, x, y );
|
||||
}
|
||||
}
|
||||
|
||||
void rofi_view_maybe_update ( RofiViewState *state ) {
|
||||
|
@ -56,11 +77,14 @@ void rofi_view_frame_callback ( void ) {
|
|||
}
|
||||
|
||||
unsigned int rofi_view_get_completed ( const RofiViewState *state ) {
|
||||
return proxy->get_completed ( state );
|
||||
return state->quit;
|
||||
}
|
||||
|
||||
const char * rofi_view_get_user_input ( const RofiViewState *state ) {
|
||||
return proxy->get_user_input ( state );
|
||||
if ( state->text ) {
|
||||
return state->text->text;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void rofi_view_set_selected_line ( RofiViewState *state, unsigned int selected_line ) {
|
||||
|
@ -68,11 +92,12 @@ void rofi_view_set_selected_line ( RofiViewState *state, unsigned int selected_l
|
|||
}
|
||||
|
||||
unsigned int rofi_view_get_selected_line ( const RofiViewState *state ) {
|
||||
return proxy->get_selected_line ( state );
|
||||
return state->selected_line;
|
||||
}
|
||||
|
||||
void rofi_view_restart ( RofiViewState *state ) {
|
||||
proxy->restart ( state );
|
||||
state->quit = FALSE;
|
||||
state->retv = MENU_CANCEL;
|
||||
}
|
||||
|
||||
gboolean rofi_view_trigger_action ( RofiViewState *state, BindingsScope scope, guint action ) {
|
||||
|
@ -99,6 +124,238 @@ void rofi_view_queue_redraw ( void ) {
|
|||
proxy->queue_redraw ( );
|
||||
}
|
||||
|
||||
void rofi_view_calculate_window_position ( RofiViewState * state )
|
||||
{
|
||||
proxy->calculate_window_position ( state );
|
||||
}
|
||||
|
||||
void rofi_view_window_update_size ( RofiViewState * state )
|
||||
{
|
||||
proxy->window_update_size ( state );
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread state for workers started for the view.
|
||||
*/
|
||||
typedef struct _thread_state_view
|
||||
{
|
||||
/** Generic thread state. */
|
||||
thread_state st;
|
||||
|
||||
/** Condition. */
|
||||
GCond *cond;
|
||||
/** Lock for condition. */
|
||||
GMutex *mutex;
|
||||
/** Count that is protected by lock. */
|
||||
unsigned int *acount;
|
||||
|
||||
/** Current state. */
|
||||
RofiViewState *state;
|
||||
/** Start row for this worker. */
|
||||
unsigned int start;
|
||||
/** Stop row for this worker. */
|
||||
unsigned int stop;
|
||||
/** Rows processed. */
|
||||
unsigned int count;
|
||||
|
||||
/** Pattern input to filter. */
|
||||
const char *pattern;
|
||||
/** Length of pattern. */
|
||||
glong plen;
|
||||
} thread_state_view;
|
||||
|
||||
static void filter_elements ( thread_state *ts, G_GNUC_UNUSED gpointer user_data )
|
||||
{
|
||||
thread_state_view *t = (thread_state_view *) ts;
|
||||
for ( unsigned int i = t->start; i < t->stop; i++ ) {
|
||||
int match = mode_token_match ( t->state->sw, t->state->tokens, i );
|
||||
// If each token was matched, add it to list.
|
||||
if ( match ) {
|
||||
t->state->line_map[t->start + t->count] = i;
|
||||
if ( config.sort ) {
|
||||
// This is inefficient, need to fix it.
|
||||
char * str = mode_get_completion ( t->state->sw, i );
|
||||
glong slen = g_utf8_strlen ( str, -1 );
|
||||
switch ( config.sorting_method_enum )
|
||||
{
|
||||
case SORT_FZF:
|
||||
t->state->distance[i] = rofi_scorer_fuzzy_evaluate ( t->pattern, t->plen, str, slen );
|
||||
break;
|
||||
case SORT_NORMAL:
|
||||
default:
|
||||
t->state->distance[i] = levenshtein ( t->pattern, t->plen, str, slen );
|
||||
break;
|
||||
}
|
||||
g_free ( str );
|
||||
}
|
||||
t->count++;
|
||||
}
|
||||
}
|
||||
if ( t->acount != NULL ) {
|
||||
g_mutex_lock ( t->mutex );
|
||||
( *( t->acount ) )--;
|
||||
g_cond_signal ( t->cond );
|
||||
g_mutex_unlock ( t->mutex );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Levenshtein Sorting.
|
||||
*/
|
||||
static int lev_sort ( const void *p1, const void *p2, void *arg )
|
||||
{
|
||||
const int *a = p1;
|
||||
const int *b = p2;
|
||||
int *distances = arg;
|
||||
|
||||
return distances[*a] - distances[*b];
|
||||
}
|
||||
|
||||
int rofi_view_calculate_window_height ( RofiViewState *state )
|
||||
{
|
||||
return proxy->calculate_window_height ( state );
|
||||
}
|
||||
|
||||
void rofi_view_reload_message_bar ( RofiViewState *state )
|
||||
{
|
||||
if ( state->mesg_box == NULL ) {
|
||||
return;
|
||||
}
|
||||
char *msg = mode_get_message ( state->sw );
|
||||
if ( msg ) {
|
||||
textbox_text ( state->mesg_tb, msg );
|
||||
widget_enable ( WIDGET ( state->mesg_box ) );
|
||||
g_free ( msg );
|
||||
}
|
||||
else {
|
||||
widget_disable ( WIDGET ( state->mesg_box ) );
|
||||
}
|
||||
}
|
||||
|
||||
static void _rofi_view_reload_row ( RofiViewState *state )
|
||||
{
|
||||
g_free ( state->line_map );
|
||||
g_free ( state->distance );
|
||||
state->num_lines = mode_get_num_entries ( state->sw );
|
||||
state->line_map = g_malloc0_n ( state->num_lines, sizeof ( unsigned int ) );
|
||||
state->distance = g_malloc0_n ( state->num_lines, sizeof ( int ) );
|
||||
listview_set_max_lines ( state->list_view, state->num_lines );
|
||||
rofi_view_reload_message_bar ( state );
|
||||
}
|
||||
|
||||
void rofi_view_refilter ( struct RofiViewState *state )
|
||||
{
|
||||
TICK_N ( "Filter start" );
|
||||
if ( state->reload ) {
|
||||
_rofi_view_reload_row ( state );
|
||||
state->reload = FALSE;
|
||||
}
|
||||
TICK_N ( "Filter reload rows" );
|
||||
if ( state->tokens ) {
|
||||
helper_tokenize_free ( state->tokens );
|
||||
state->tokens = NULL;
|
||||
}
|
||||
TICK_N ( "Filter tokenize" );
|
||||
if ( state->text && strlen ( state->text->text ) > 0 ) {
|
||||
unsigned int j = 0;
|
||||
gchar *pattern = mode_preprocess_input ( state->sw, state->text->text );
|
||||
glong plen = pattern ? g_utf8_strlen ( pattern, -1 ) : 0;
|
||||
state->tokens = helper_tokenize ( pattern, config.case_sensitive );
|
||||
/**
|
||||
* On long lists it can be beneficial to parallelize.
|
||||
* If number of threads is 1, no thread is spawn.
|
||||
* If number of threads > 1 and there are enough (> 1000) items, spawn jobs for the thread pool.
|
||||
* For large lists with 8 threads I see a factor three speedup of the whole function.
|
||||
*/
|
||||
unsigned int nt = MAX ( 1, state->num_lines / 500 );
|
||||
thread_state_view states[nt];
|
||||
GCond cond;
|
||||
GMutex mutex;
|
||||
g_mutex_init ( &mutex );
|
||||
g_cond_init ( &cond );
|
||||
unsigned int count = nt;
|
||||
unsigned int steps = ( state->num_lines + nt ) / nt;
|
||||
for ( unsigned int i = 0; i < nt; i++ ) {
|
||||
states[i].state = state;
|
||||
states[i].start = i * steps;
|
||||
states[i].stop = MIN ( state->num_lines, ( i + 1 ) * steps );
|
||||
states[i].count = 0;
|
||||
states[i].cond = &cond;
|
||||
states[i].mutex = &mutex;
|
||||
states[i].acount = &count;
|
||||
states[i].plen = plen;
|
||||
states[i].pattern = pattern;
|
||||
states[i].st.callback = filter_elements;
|
||||
if ( i > 0 ) {
|
||||
g_thread_pool_push ( tpool, &states[i], NULL );
|
||||
}
|
||||
}
|
||||
// Run one in this thread.
|
||||
rofi_view_call_thread ( &states[0], NULL );
|
||||
// No need to do this with only one thread.
|
||||
if ( nt > 1 ) {
|
||||
g_mutex_lock ( &mutex );
|
||||
while ( count > 0 ) {
|
||||
g_cond_wait ( &cond, &mutex );
|
||||
}
|
||||
g_mutex_unlock ( &mutex );
|
||||
}
|
||||
g_cond_clear ( &cond );
|
||||
g_mutex_clear ( &mutex );
|
||||
for ( unsigned int i = 0; i < nt; i++ ) {
|
||||
if ( j != states[i].start ) {
|
||||
memmove ( &( state->line_map[j] ), &( state->line_map[states[i].start] ), sizeof ( unsigned int ) * ( states[i].count ) );
|
||||
}
|
||||
j += states[i].count;
|
||||
}
|
||||
if ( config.sort ) {
|
||||
g_qsort_with_data ( state->line_map, j, sizeof ( int ), lev_sort, state->distance );
|
||||
}
|
||||
|
||||
// Cleanup + bookkeeping.
|
||||
state->filtered_lines = j;
|
||||
g_free ( pattern );
|
||||
}
|
||||
else{
|
||||
for ( unsigned int i = 0; i < state->num_lines; i++ ) {
|
||||
state->line_map[i] = i;
|
||||
}
|
||||
state->filtered_lines = state->num_lines;
|
||||
}
|
||||
TICK_N ( "Filter matching done" );
|
||||
listview_set_num_elements ( state->list_view, state->filtered_lines );
|
||||
|
||||
if ( state->tb_filtered_rows ) {
|
||||
char *r = g_strdup_printf ( "%u", state->filtered_lines );
|
||||
textbox_text ( state->tb_filtered_rows, r );
|
||||
g_free ( r );
|
||||
}
|
||||
if ( state->tb_total_rows ) {
|
||||
char *r = g_strdup_printf ( "%u", state->num_lines );
|
||||
textbox_text ( state->tb_total_rows, r );
|
||||
g_free ( r );
|
||||
}
|
||||
TICK_N ( "Update filter lines" );
|
||||
|
||||
if ( config.auto_select == TRUE && state->filtered_lines == 1 && state->num_lines > 1 ) {
|
||||
( state->selected_line ) = state->line_map[listview_get_selected ( state->list_view )];
|
||||
state->retv = MENU_OK;
|
||||
state->quit = TRUE;
|
||||
}
|
||||
|
||||
// Size the window.
|
||||
int height = rofi_view_calculate_window_height ( state );
|
||||
if ( height != state->height ) {
|
||||
state->height = height;
|
||||
rofi_view_calculate_window_position ( state );
|
||||
rofi_view_window_update_size ( state );
|
||||
g_debug ( "Resize based on re-filter" );
|
||||
}
|
||||
TICK_N ( "Filter resize window based on window " );
|
||||
state->refilter = FALSE;
|
||||
TICK_N ( "Filter done" );
|
||||
}
|
||||
|
||||
void rofi_view_cleanup ( void ) {
|
||||
proxy->cleanup ( );
|
||||
}
|
||||
|
@ -135,12 +392,50 @@ xcb_window_t rofi_view_get_window ( void ) {
|
|||
return proxy->get_window ( );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param data A thread_state object.
|
||||
* @param user_data User data to pass to thread_state callback
|
||||
*
|
||||
* Small wrapper function that is internally used to pass a job to a worker.
|
||||
*/
|
||||
void rofi_view_call_thread ( gpointer data, gpointer user_data )
|
||||
{
|
||||
thread_state *t = (thread_state *) data;
|
||||
t->callback ( t, user_data );
|
||||
}
|
||||
|
||||
void rofi_view_workers_initialize ( void ) {
|
||||
proxy->workers_initialize ( );
|
||||
TICK_N ( "Setup Threadpool, start" );
|
||||
if ( config.threads == 0 ) {
|
||||
config.threads = 1;
|
||||
long procs = sysconf ( _SC_NPROCESSORS_CONF );
|
||||
if ( procs > 0 ) {
|
||||
config.threads = MIN ( procs, 128l );
|
||||
}
|
||||
}
|
||||
// Create thread pool
|
||||
GError *error = NULL;
|
||||
tpool = g_thread_pool_new ( rofi_view_call_thread, NULL, config.threads, FALSE, &error );
|
||||
if ( error == NULL ) {
|
||||
// Idle threads should stick around for a max of 60 seconds.
|
||||
g_thread_pool_set_max_idle_time ( 60000 );
|
||||
// We are allowed to have
|
||||
g_thread_pool_set_max_threads ( tpool, config.threads, &error );
|
||||
}
|
||||
// If error occurred during setup of pool, tell user and exit.
|
||||
if ( error != NULL ) {
|
||||
g_warning ( "Failed to setup thread pool: '%s'", error->message );
|
||||
g_error_free ( error );
|
||||
exit ( EXIT_FAILURE );
|
||||
}
|
||||
TICK_N ( "Setup Threadpool, done" );
|
||||
}
|
||||
|
||||
void rofi_view_workers_finalize ( void ) {
|
||||
proxy->workers_finalize ( );
|
||||
if ( tpool ) {
|
||||
g_thread_pool_free ( tpool, TRUE, TRUE );
|
||||
tpool = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void rofi_view_get_current_monitor ( int *width, int *height ) {
|
||||
|
@ -152,7 +447,7 @@ void rofi_capture_screenshot ( void ) {
|
|||
}
|
||||
|
||||
void rofi_view_ellipsize_start ( RofiViewState *state ) {
|
||||
proxy->ellipsize_start ( state );
|
||||
listview_set_ellipsize_start ( state->list_view );
|
||||
}
|
||||
|
||||
void rofi_view_set_size ( RofiViewState * state, gint width, gint height ) {
|
||||
|
|
|
@ -69,7 +69,7 @@
|
|||
*/
|
||||
static void wayland_rofi_view_update ( RofiViewState *state, gboolean qr );
|
||||
|
||||
static int calculate_height ( RofiViewState *state );
|
||||
static int wayland_rofi_view_calculate_window_height ( RofiViewState *state );
|
||||
|
||||
/** Thread pool used for filtering */
|
||||
extern GThreadPool *tpool;
|
||||
|
@ -86,10 +86,6 @@ static struct
|
|||
MenuFlags flags;
|
||||
/** List of stacked views */
|
||||
GQueue views;
|
||||
#if 0
|
||||
/** Current work area */
|
||||
workarea mon;
|
||||
#endif
|
||||
/** timeout for reloading */
|
||||
guint idle_timeout;
|
||||
/** debug counter for redraws */
|
||||
|
@ -130,18 +126,6 @@ static char * get_matching_state ( void )
|
|||
return " ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Levenshtein Sorting.
|
||||
*/
|
||||
static int lev_sort ( const void *p1, const void *p2, void *arg )
|
||||
{
|
||||
const int *a = p1;
|
||||
const int *b = p2;
|
||||
int *distances = arg;
|
||||
|
||||
return distances[*a] - distances[*b];
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a screenshot of Rofi at that point in time.
|
||||
*/
|
||||
|
@ -181,12 +165,16 @@ static const int loc_transtable[9] = {
|
|||
WL_WEST
|
||||
};
|
||||
|
||||
static void wayland_rofi_view_calculate_window_position ( RofiViewState *state )
|
||||
{
|
||||
}
|
||||
|
||||
static int rofi_get_location ( RofiViewState *state )
|
||||
{
|
||||
return rofi_theme_get_position ( WIDGET ( state->main_window ), "location", loc_transtable[config.location] );
|
||||
}
|
||||
|
||||
static void rofi_view_window_update_size ( RofiViewState * state )
|
||||
static void wayland_rofi_view_window_update_size ( RofiViewState * state )
|
||||
{
|
||||
widget_resize ( WIDGET ( state->main_window ), state->width, state->height );
|
||||
display_set_surface_dimensions ( state->width, state->height, rofi_get_location(state) );
|
||||
|
@ -207,22 +195,6 @@ static void wayland_rofi_view_get_size ( RofiViewState * state, gint *width, gin
|
|||
*height = state->height;
|
||||
}
|
||||
|
||||
static void rofi_view_reload_message_bar ( RofiViewState *state )
|
||||
{
|
||||
if ( state->mesg_box == NULL ) {
|
||||
return;
|
||||
}
|
||||
char *msg = mode_get_message ( state->sw );
|
||||
if ( msg ) {
|
||||
textbox_text ( state->mesg_tb, msg );
|
||||
widget_enable ( WIDGET ( state->mesg_box ) );
|
||||
g_free ( msg );
|
||||
}
|
||||
else {
|
||||
widget_disable ( WIDGET ( state->mesg_box ) );
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean rofi_view_reload_idle ( G_GNUC_UNUSED gpointer data )
|
||||
{
|
||||
RofiViewState *state = rofi_view_get_active ();
|
||||
|
@ -256,12 +228,6 @@ static void wayland_rofi_view_queue_redraw ( void )
|
|||
}
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_restart ( RofiViewState *state )
|
||||
{
|
||||
state->quit = FALSE;
|
||||
state->retv = MENU_CANCEL;
|
||||
}
|
||||
|
||||
static RofiViewState * wayland_rofi_view_get_active ( void )
|
||||
{
|
||||
return current_active_menu;
|
||||
|
@ -324,31 +290,6 @@ static void wayland_rofi_view_free ( RofiViewState *state )
|
|||
g_free ( state );
|
||||
}
|
||||
|
||||
static MenuReturn wayland_rofi_view_get_return_value ( const RofiViewState *state )
|
||||
{
|
||||
return state->retv;
|
||||
}
|
||||
|
||||
static unsigned int wayland_rofi_view_get_selected_line ( const RofiViewState *state )
|
||||
{
|
||||
return state->selected_line;
|
||||
}
|
||||
|
||||
static unsigned int wayland_rofi_view_get_next_position ( const RofiViewState *state )
|
||||
{
|
||||
unsigned int next_pos = state->selected_line;
|
||||
unsigned int selected = listview_get_selected ( state->list_view );
|
||||
if ( ( selected + 1 ) < state->num_lines ) {
|
||||
( next_pos ) = state->line_map[selected + 1];
|
||||
}
|
||||
return next_pos;
|
||||
}
|
||||
|
||||
static unsigned int wayland_rofi_view_get_completed ( const RofiViewState *state )
|
||||
{
|
||||
return state->quit;
|
||||
}
|
||||
|
||||
static const char * wayland_rofi_view_get_user_input ( const RofiViewState *state )
|
||||
{
|
||||
if ( state->text ) {
|
||||
|
@ -396,52 +337,7 @@ typedef struct _thread_state_view
|
|||
/** Length of pattern. */
|
||||
glong plen;
|
||||
} thread_state_view;
|
||||
/**
|
||||
* @param data A thread_state object.
|
||||
* @param user_data User data to pass to thread_state callback
|
||||
*
|
||||
* Small wrapper function that is internally used to pass a job to a worker.
|
||||
*/
|
||||
static void rofi_view_call_thread ( gpointer data, gpointer user_data )
|
||||
{
|
||||
thread_state *t = (thread_state *) data;
|
||||
t->callback ( t, user_data );
|
||||
}
|
||||
|
||||
static void filter_elements ( thread_state *ts, G_GNUC_UNUSED gpointer user_data )
|
||||
{
|
||||
thread_state_view *t = (thread_state_view *) ts;
|
||||
for ( unsigned int i = t->start; i < t->stop; i++ ) {
|
||||
int match = mode_token_match ( t->state->sw, t->state->tokens, i );
|
||||
// If each token was matched, add it to list.
|
||||
if ( match ) {
|
||||
t->state->line_map[t->start + t->count] = i;
|
||||
if ( config.sort ) {
|
||||
// This is inefficient, need to fix it.
|
||||
char * str = mode_get_completion ( t->state->sw, i );
|
||||
glong slen = g_utf8_strlen ( str, -1 );
|
||||
switch ( config.sorting_method_enum )
|
||||
{
|
||||
case SORT_FZF:
|
||||
t->state->distance[i] = rofi_scorer_fuzzy_evaluate ( t->pattern, t->plen, str, slen );
|
||||
break;
|
||||
case SORT_NORMAL:
|
||||
default:
|
||||
t->state->distance[i] = levenshtein ( t->pattern, t->plen, str, slen );
|
||||
break;
|
||||
}
|
||||
g_free ( str );
|
||||
}
|
||||
t->count++;
|
||||
}
|
||||
}
|
||||
if ( t->acount != NULL ) {
|
||||
g_mutex_lock ( t->mutex );
|
||||
( *( t->acount ) )--;
|
||||
g_cond_signal ( t->cond );
|
||||
g_mutex_unlock ( t->mutex );
|
||||
}
|
||||
}
|
||||
static void wayland___create_window ( MenuFlags menu_flags )
|
||||
{
|
||||
// FIXME: create surface
|
||||
|
@ -657,141 +553,12 @@ static void wayland_rofi_view_update ( RofiViewState *state, gboolean qr )
|
|||
}
|
||||
}
|
||||
|
||||
static void _rofi_view_reload_row ( RofiViewState *state )
|
||||
{
|
||||
g_free ( state->line_map );
|
||||
g_free ( state->distance );
|
||||
state->num_lines = mode_get_num_entries ( state->sw );
|
||||
state->line_map = g_malloc0_n ( state->num_lines, sizeof ( unsigned int ) );
|
||||
state->distance = g_malloc0_n ( state->num_lines, sizeof ( int ) );
|
||||
listview_set_max_lines ( state->list_view, state->num_lines );
|
||||
rofi_view_reload_message_bar ( state );
|
||||
}
|
||||
|
||||
static void rofi_view_refilter ( RofiViewState *state )
|
||||
{
|
||||
TICK_N ( "Filter start" );
|
||||
if ( state->reload ) {
|
||||
_rofi_view_reload_row ( state );
|
||||
state->reload = FALSE;
|
||||
}
|
||||
TICK_N ( "Filter reload rows" );
|
||||
if ( state->tokens ) {
|
||||
helper_tokenize_free ( state->tokens );
|
||||
state->tokens = NULL;
|
||||
}
|
||||
TICK_N ( "Filter tokenize" );
|
||||
if ( state->text && strlen ( state->text->text ) > 0 ) {
|
||||
unsigned int j = 0;
|
||||
gchar *pattern = mode_preprocess_input ( state->sw, state->text->text );
|
||||
glong plen = pattern ? g_utf8_strlen ( pattern, -1 ) : 0;
|
||||
state->tokens = helper_tokenize ( pattern, config.case_sensitive );
|
||||
/**
|
||||
* On long lists it can be beneficial to parallelize.
|
||||
* If number of threads is 1, no thread is spawn.
|
||||
* If number of threads > 1 and there are enough (> 1000) items, spawn jobs for the thread pool.
|
||||
* For large lists with 8 threads I see a factor three speedup of the whole function.
|
||||
*/
|
||||
unsigned int nt = MAX ( 1, state->num_lines / 500 );
|
||||
thread_state_view states[nt];
|
||||
GCond cond;
|
||||
GMutex mutex;
|
||||
g_mutex_init ( &mutex );
|
||||
g_cond_init ( &cond );
|
||||
unsigned int count = nt;
|
||||
unsigned int steps = ( state->num_lines + nt ) / nt;
|
||||
for ( unsigned int i = 0; i < nt; i++ ) {
|
||||
states[i].state = state;
|
||||
states[i].start = i * steps;
|
||||
states[i].stop = MIN ( state->num_lines, ( i + 1 ) * steps );
|
||||
states[i].count = 0;
|
||||
states[i].cond = &cond;
|
||||
states[i].mutex = &mutex;
|
||||
states[i].acount = &count;
|
||||
states[i].plen = plen;
|
||||
states[i].pattern = pattern;
|
||||
states[i].st.callback = filter_elements;
|
||||
if ( i > 0 ) {
|
||||
g_thread_pool_push ( tpool, &states[i], NULL );
|
||||
}
|
||||
}
|
||||
// Run one in this thread.
|
||||
rofi_view_call_thread ( &states[0], NULL );
|
||||
// No need to do this with only one thread.
|
||||
if ( nt > 1 ) {
|
||||
g_mutex_lock ( &mutex );
|
||||
while ( count > 0 ) {
|
||||
g_cond_wait ( &cond, &mutex );
|
||||
}
|
||||
g_mutex_unlock ( &mutex );
|
||||
}
|
||||
g_cond_clear ( &cond );
|
||||
g_mutex_clear ( &mutex );
|
||||
for ( unsigned int i = 0; i < nt; i++ ) {
|
||||
if ( j != states[i].start ) {
|
||||
memmove ( &( state->line_map[j] ), &( state->line_map[states[i].start] ), sizeof ( unsigned int ) * ( states[i].count ) );
|
||||
}
|
||||
j += states[i].count;
|
||||
}
|
||||
if ( config.sort ) {
|
||||
g_qsort_with_data ( state->line_map, j, sizeof ( int ), lev_sort, state->distance );
|
||||
}
|
||||
|
||||
// Cleanup + bookkeeping.
|
||||
state->filtered_lines = j;
|
||||
g_free ( pattern );
|
||||
}
|
||||
else{
|
||||
for ( unsigned int i = 0; i < state->num_lines; i++ ) {
|
||||
state->line_map[i] = i;
|
||||
}
|
||||
state->filtered_lines = state->num_lines;
|
||||
}
|
||||
TICK_N ( "Filter matching done" );
|
||||
listview_set_num_elements ( state->list_view, state->filtered_lines );
|
||||
|
||||
if ( state->tb_filtered_rows ) {
|
||||
char *r = g_strdup_printf ( "%u", state->filtered_lines );
|
||||
textbox_text ( state->tb_filtered_rows, r );
|
||||
g_free ( r );
|
||||
}
|
||||
if ( state->tb_total_rows ) {
|
||||
char *r = g_strdup_printf ( "%u", state->num_lines );
|
||||
textbox_text ( state->tb_total_rows, r );
|
||||
g_free ( r );
|
||||
}
|
||||
TICK_N ( "Update filter lines" );
|
||||
|
||||
if ( config.auto_select == TRUE && state->filtered_lines == 1 && state->num_lines > 1 ) {
|
||||
( state->selected_line ) = state->line_map[listview_get_selected ( state->list_view )];
|
||||
state->retv = MENU_OK;
|
||||
state->quit = TRUE;
|
||||
}
|
||||
|
||||
// Size the window.
|
||||
int height = calculate_height ( state );
|
||||
if ( height != state->height ) {
|
||||
state->height = height;
|
||||
//rofi_view_calculate_window_position ( state );
|
||||
rofi_view_window_update_size ( state );
|
||||
g_debug ( "Resize based on re-filter" );
|
||||
}
|
||||
TICK_N ( "Filter resize window based on window " );
|
||||
state->refilter = FALSE;
|
||||
TICK_N ( "Filter done" );
|
||||
}
|
||||
/**
|
||||
* @param state The Menu Handle
|
||||
*
|
||||
* Check if a finalize function is set, and if sets executes it.
|
||||
*/
|
||||
void process_result ( RofiViewState *state );
|
||||
static void wayland_rofi_view_finalize ( RofiViewState *state )
|
||||
{
|
||||
if ( state && state->finalize != NULL ) {
|
||||
state->finalize ( state );
|
||||
}
|
||||
}
|
||||
|
||||
static void rofi_view_trigger_global_action ( KeyBindingAction action )
|
||||
{
|
||||
|
@ -1058,16 +825,6 @@ static void wayland_rofi_view_handle_text ( RofiViewState *state, char *text )
|
|||
}
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_handle_mouse_motion ( RofiViewState *state, gint x, gint y )
|
||||
{
|
||||
state->mouse.x = x;
|
||||
state->mouse.y = y;
|
||||
if ( state->mouse.motion_target != NULL ) {
|
||||
widget_xy_to_relative ( state->mouse.motion_target, &x, &y );
|
||||
widget_motion_notify ( state->mouse.motion_target, x, y );
|
||||
}
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_maybe_update ( RofiViewState *state )
|
||||
{
|
||||
if ( rofi_view_get_completed ( state ) ) {
|
||||
|
@ -1098,7 +855,7 @@ static void wayland_rofi_view_frame_callback ( void )
|
|||
}
|
||||
}
|
||||
|
||||
static int calculate_height ( RofiViewState *state )
|
||||
static int wayland_rofi_view_calculate_window_height ( RofiViewState *state )
|
||||
{
|
||||
if ( CacheState.fullscreen == TRUE ) {
|
||||
int height = 1080;
|
||||
|
@ -1407,9 +1164,9 @@ static RofiViewState *wayland_rofi_view_create ( Mode *sw,
|
|||
listview_set_fixed_num_lines ( state->list_view );
|
||||
}
|
||||
|
||||
state->height = calculate_height ( state );
|
||||
state->height = wayland_rofi_view_calculate_window_height ( state );
|
||||
// Move the window to the correct x,y position.
|
||||
//rofi_view_calculate_window_position ( state );
|
||||
rofi_view_calculate_window_position ( state );
|
||||
rofi_view_window_update_size ( state );
|
||||
|
||||
state->quit = FALSE;
|
||||
|
@ -1444,7 +1201,7 @@ static int wayland_rofi_view_error_dialog ( const char *msg, int markup )
|
|||
state->height = widget_get_desired_height ( WIDGET ( state->main_window ) );
|
||||
|
||||
// Calculte window position.
|
||||
//rofi_view_calculate_window_position ( state );
|
||||
rofi_view_calculate_window_position ( state );
|
||||
|
||||
// Move the window to the correct x,y position.
|
||||
rofi_view_window_update_size ( state );
|
||||
|
@ -1472,42 +1229,6 @@ static void wayland_rofi_view_cleanup ()
|
|||
g_assert ( g_queue_is_empty ( &( CacheState.views ) ) );
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_workers_initialize ( void )
|
||||
{
|
||||
TICK_N ( "Setup Threadpool, start" );
|
||||
if ( config.threads == 0 ) {
|
||||
config.threads = 1;
|
||||
long procs = sysconf ( _SC_NPROCESSORS_CONF );
|
||||
if ( procs > 0 ) {
|
||||
config.threads = MIN ( procs, 128l );
|
||||
}
|
||||
}
|
||||
// Create thread pool
|
||||
GError *error = NULL;
|
||||
tpool = g_thread_pool_new ( rofi_view_call_thread, NULL, config.threads, FALSE, &error );
|
||||
if ( error == NULL ) {
|
||||
// Idle threads should stick around for a max of 60 seconds.
|
||||
g_thread_pool_set_max_idle_time ( 60000 );
|
||||
// We are allowed to have
|
||||
g_thread_pool_set_max_threads ( tpool, config.threads, &error );
|
||||
}
|
||||
// If error occurred during setup of pool, tell user and exit.
|
||||
if ( error != NULL ) {
|
||||
g_warning ( "Failed to setup thread pool: '%s'", error->message );
|
||||
g_error_free ( error );
|
||||
exit ( EXIT_FAILURE );
|
||||
}
|
||||
TICK_N ( "Setup Threadpool, done" );
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_workers_finalize ( void )
|
||||
{
|
||||
if ( tpool ) {
|
||||
g_thread_pool_free ( tpool, TRUE, TRUE );
|
||||
tpool = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static Mode * wayland_rofi_view_get_mode ( RofiViewState *state )
|
||||
{
|
||||
return state->sw;
|
||||
|
@ -1536,11 +1257,6 @@ static void wayland_rofi_view_clear_input ( RofiViewState *state )
|
|||
}
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_ellipsize_start ( RofiViewState *state )
|
||||
{
|
||||
listview_set_ellipsize_start ( state->list_view );
|
||||
}
|
||||
|
||||
static void wayland_rofi_view_switch_mode ( RofiViewState *state, Mode *mode )
|
||||
{
|
||||
state->sw = mode;
|
||||
|
@ -1563,20 +1279,13 @@ static void wayland_rofi_view_switch_mode ( RofiViewState *state, Mode *mode )
|
|||
|
||||
static view_proxy view_ = {
|
||||
.create = wayland_rofi_view_create,
|
||||
.finalize = wayland_rofi_view_finalize,
|
||||
.get_return_value = wayland_rofi_view_get_return_value,
|
||||
.get_next_position = wayland_rofi_view_get_next_position,
|
||||
.handle_text = wayland_rofi_view_handle_text,
|
||||
.handle_mouse_motion = wayland_rofi_view_handle_mouse_motion,
|
||||
.maybe_update = wayland_rofi_view_maybe_update,
|
||||
.temp_configure_notify = NULL,
|
||||
.temp_click_to_exit = NULL,
|
||||
.frame_callback = wayland_rofi_view_frame_callback,
|
||||
.get_completed = wayland_rofi_view_get_completed,
|
||||
.get_user_input = wayland_rofi_view_get_user_input,
|
||||
.set_selected_line = wayland_rofi_view_set_selected_line,
|
||||
.get_selected_line = wayland_rofi_view_get_selected_line,
|
||||
.restart = wayland_rofi_view_restart,
|
||||
.trigger_action = wayland_rofi_view_trigger_action,
|
||||
.free = wayland_rofi_view_free,
|
||||
.get_active = wayland_rofi_view_get_active,
|
||||
|
@ -1584,6 +1293,10 @@ static view_proxy view_ = {
|
|||
.error_dialog = wayland_rofi_view_error_dialog,
|
||||
.queue_redraw = wayland_rofi_view_queue_redraw,
|
||||
|
||||
.calculate_window_position = wayland_rofi_view_calculate_window_position,
|
||||
.calculate_window_height = wayland_rofi_view_calculate_window_height,
|
||||
.window_update_size = wayland_rofi_view_window_update_size,
|
||||
|
||||
.cleanup = wayland_rofi_view_cleanup,
|
||||
.get_mode = wayland_rofi_view_get_mode,
|
||||
.hide = wayland_rofi_view_hide,
|
||||
|
@ -1593,13 +1306,9 @@ static view_proxy view_ = {
|
|||
.clear_input = wayland_rofi_view_clear_input,
|
||||
.__create_window = wayland___create_window,
|
||||
.get_window = NULL,
|
||||
.workers_initialize = wayland_rofi_view_workers_initialize,
|
||||
.workers_finalize = wayland_rofi_view_workers_finalize,
|
||||
.get_current_monitor = wayland_rofi_view_get_current_monitor,
|
||||
.capture_screenshot = wayland_rofi_view_capture_screenshot,
|
||||
|
||||
.ellipsize_start = wayland_rofi_view_ellipsize_start,
|
||||
|
||||
.set_size = wayland_rofi_view_set_size,
|
||||
.get_size = wayland_rofi_view_get_size,
|
||||
};
|
||||
|
|
|
@ -77,7 +77,7 @@
|
|||
*/
|
||||
static void xcb_rofi_view_update ( RofiViewState *state, gboolean qr );
|
||||
|
||||
static int rofi_view_calculate_height ( RofiViewState *state );
|
||||
static int xcb_rofi_view_calculate_window_height ( RofiViewState *state );
|
||||
|
||||
static void xcb_rofi_view_set_window_title ( const char * title );
|
||||
|
||||
|
@ -161,18 +161,6 @@ static char * get_matching_state ( void )
|
|||
return " ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Levenshtein Sorting.
|
||||
*/
|
||||
static int lev_sort ( const void *p1, const void *p2, void *arg )
|
||||
{
|
||||
const int *a = p1;
|
||||
const int *b = p2;
|
||||
int *distances = arg;
|
||||
|
||||
return distances[*a] - distances[*b];
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a screenshot of Rofi at that point in time.
|
||||
*/
|
||||
|
@ -315,7 +303,7 @@ static const int loc_transtable[9] = {
|
|||
WL_SOUTH | WL_WEST,
|
||||
WL_WEST
|
||||
};
|
||||
static void rofi_view_calculate_window_position ( RofiViewState *state )
|
||||
static void xcb_rofi_view_calculate_window_position ( RofiViewState *state )
|
||||
{
|
||||
int location = rofi_theme_get_position ( WIDGET ( state->main_window ), "location", loc_transtable[config.location] );
|
||||
int anchor = location;
|
||||
|
@ -415,7 +403,7 @@ static void rofi_view_calculate_window_position ( RofiViewState *state )
|
|||
state->y += distance_get_pixel ( y, ROFI_ORIENTATION_VERTICAL );
|
||||
}
|
||||
|
||||
static void rofi_view_window_update_size ( RofiViewState * state )
|
||||
static void xcb_rofi_view_window_update_size ( RofiViewState * state )
|
||||
{
|
||||
uint16_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
|
||||
uint32_t vals[] = { state->x, state->y, state->width, state->height };
|
||||
|
@ -438,22 +426,6 @@ static void rofi_view_window_update_size ( RofiViewState * state )
|
|||
widget_resize ( WIDGET ( state->main_window ), state->width, state->height );
|
||||
}
|
||||
|
||||
static void rofi_view_reload_message_bar ( RofiViewState *state )
|
||||
{
|
||||
if ( state->mesg_box == NULL ) {
|
||||
return;
|
||||
}
|
||||
char *msg = mode_get_message ( state->sw );
|
||||
if ( msg ) {
|
||||
textbox_text ( state->mesg_tb, msg );
|
||||
widget_enable ( WIDGET ( state->mesg_box ) );
|
||||
g_free ( msg );
|
||||
}
|
||||
else {
|
||||
widget_disable ( WIDGET ( state->mesg_box ) );
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean rofi_view_reload_idle ( G_GNUC_UNUSED gpointer data )
|
||||
{
|
||||
if ( current_active_menu ) {
|
||||
|
@ -481,12 +453,6 @@ static void xcb_rofi_view_queue_redraw ( void )
|
|||
}
|
||||
}
|
||||
|
||||
static void xcb_rofi_view_restart ( RofiViewState *state )
|
||||
{
|
||||
state->quit = FALSE;
|
||||
state->retv = MENU_CANCEL;
|
||||
}
|
||||
|
||||
static RofiViewState * xcb_rofi_view_get_active ( void )
|
||||
{
|
||||
return current_active_menu;
|
||||
|
@ -550,39 +516,6 @@ static void xcb_rofi_view_free ( RofiViewState *state )
|
|||
g_free ( state );
|
||||
}
|
||||
|
||||
static MenuReturn xcb_rofi_view_get_return_value ( const RofiViewState *state )
|
||||
{
|
||||
return state->retv;
|
||||
}
|
||||
|
||||
static unsigned int xcb_rofi_view_get_selected_line ( const RofiViewState *state )
|
||||
{
|
||||
return state->selected_line;
|
||||
}
|
||||
|
||||
static unsigned int xcb_rofi_view_get_next_position ( const RofiViewState *state )
|
||||
{
|
||||
unsigned int next_pos = state->selected_line;
|
||||
unsigned int selected = listview_get_selected ( state->list_view );
|
||||
if ( ( selected + 1 ) < state->num_lines ) {
|
||||
( next_pos ) = state->line_map[selected + 1];
|
||||
}
|
||||
return next_pos;
|
||||
}
|
||||
|
||||
static unsigned int xcb_rofi_view_get_completed ( const RofiViewState *state )
|
||||
{
|
||||
return state->quit;
|
||||
}
|
||||
|
||||
static const char * xcb_rofi_view_get_user_input ( const RofiViewState *state )
|
||||
{
|
||||
if ( state->text ) {
|
||||
return state->text->text;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new, 0 initialized RofiViewState structure.
|
||||
*
|
||||
|
@ -593,81 +526,6 @@ static RofiViewState * __rofi_view_state_create ( void )
|
|||
return g_malloc0 ( sizeof ( RofiViewState ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread state for workers started for the view.
|
||||
*/
|
||||
typedef struct _thread_state_view
|
||||
{
|
||||
/** Generic thread state. */
|
||||
thread_state st;
|
||||
|
||||
/** Condition. */
|
||||
GCond *cond;
|
||||
/** Lock for condition. */
|
||||
GMutex *mutex;
|
||||
/** Count that is protected by lock. */
|
||||
unsigned int *acount;
|
||||
|
||||
/** Current state. */
|
||||
RofiViewState *state;
|
||||
/** Start row for this worker. */
|
||||
unsigned int start;
|
||||
/** Stop row for this worker. */
|
||||
unsigned int stop;
|
||||
/** Rows processed. */
|
||||
unsigned int count;
|
||||
|
||||
/** Pattern input to filter. */
|
||||
const char *pattern;
|
||||
/** Length of pattern. */
|
||||
glong plen;
|
||||
} thread_state_view;
|
||||
/**
|
||||
* @param data A thread_state object.
|
||||
* @param user_data User data to pass to thread_state callback
|
||||
*
|
||||
* Small wrapper function that is internally used to pass a job to a worker.
|
||||
*/
|
||||
static void rofi_view_call_thread ( gpointer data, gpointer user_data )
|
||||
{
|
||||
thread_state *t = (thread_state *) data;
|
||||
t->callback ( t, user_data );
|
||||
}
|
||||
|
||||
static void filter_elements ( thread_state *ts, G_GNUC_UNUSED gpointer user_data )
|
||||
{
|
||||
thread_state_view *t = (thread_state_view *) ts;
|
||||
for ( unsigned int i = t->start; i < t->stop; i++ ) {
|
||||
int match = mode_token_match ( t->state->sw, t->state->tokens, i );
|
||||
// If each token was matched, add it to list.
|
||||
if ( match ) {
|
||||
t->state->line_map[t->start + t->count] = i;
|
||||
if ( config.sort ) {
|
||||
// This is inefficient, need to fix it.
|
||||
char * str = mode_get_completion ( t->state->sw, i );
|
||||
glong slen = g_utf8_strlen ( str, -1 );
|
||||
switch ( config.sorting_method_enum )
|
||||
{
|
||||
case SORT_FZF:
|
||||
t->state->distance[i] = rofi_scorer_fuzzy_evaluate ( t->pattern, t->plen, str, slen );
|
||||
break;
|
||||
case SORT_NORMAL:
|
||||
default:
|
||||
t->state->distance[i] = levenshtein ( t->pattern, t->plen, str, slen );
|
||||
break;
|
||||
}
|
||||
g_free ( str );
|
||||
}
|
||||
t->count++;
|
||||
}
|
||||
}
|
||||
if ( t->acount != NULL ) {
|
||||
g_mutex_lock ( t->mutex );
|
||||
( *( t->acount ) )--;
|
||||
g_cond_signal ( t->cond );
|
||||
g_mutex_unlock ( t->mutex );
|
||||
}
|
||||
}
|
||||
static void rofi_view_setup_fake_transparency ( const char* const fake_background )
|
||||
{
|
||||
if ( CacheState.fake_bg == NULL ) {
|
||||
|
@ -1059,142 +917,6 @@ static void xcb_rofi_view_update ( RofiViewState *state, gboolean qr )
|
|||
}
|
||||
}
|
||||
|
||||
static void _rofi_view_reload_row ( RofiViewState *state )
|
||||
{
|
||||
g_free ( state->line_map );
|
||||
g_free ( state->distance );
|
||||
state->num_lines = mode_get_num_entries ( state->sw );
|
||||
state->line_map = g_malloc0_n ( state->num_lines, sizeof ( unsigned int ) );
|
||||
state->distance = g_malloc0_n ( state->num_lines, sizeof ( int ) );
|
||||
listview_set_max_lines ( state->list_view, state->num_lines );
|
||||
rofi_view_reload_message_bar ( state );
|
||||
}
|
||||
|
||||
static void rofi_view_refilter ( RofiViewState *state )
|
||||
{
|
||||
TICK_N ( "Filter start" );
|
||||
if ( state->reload ) {
|
||||
_rofi_view_reload_row ( state );
|
||||
state->reload = FALSE;
|
||||
}
|
||||
TICK_N ( "Filter reload rows" );
|
||||
if ( state->tokens ) {
|
||||
helper_tokenize_free ( state->tokens );
|
||||
state->tokens = NULL;
|
||||
}
|
||||
TICK_N ( "Filter tokenize" );
|
||||
if ( state->text && strlen ( state->text->text ) > 0 ) {
|
||||
unsigned int j = 0;
|
||||
gchar *pattern = mode_preprocess_input ( state->sw, state->text->text );
|
||||
glong plen = pattern ? g_utf8_strlen ( pattern, -1 ) : 0;
|
||||
state->tokens = helper_tokenize ( pattern, config.case_sensitive );
|
||||
/**
|
||||
* On long lists it can be beneficial to parallelize.
|
||||
* If number of threads is 1, no thread is spawn.
|
||||
* If number of threads > 1 and there are enough (> 1000) items, spawn jobs for the thread pool.
|
||||
* For large lists with 8 threads I see a factor three speedup of the whole function.
|
||||
*/
|
||||
unsigned int nt = MAX ( 1, state->num_lines / 500 );
|
||||
thread_state_view states[nt];
|
||||
GCond cond;
|
||||
GMutex mutex;
|
||||
g_mutex_init ( &mutex );
|
||||
g_cond_init ( &cond );
|
||||
unsigned int count = nt;
|
||||
unsigned int steps = ( state->num_lines + nt ) / nt;
|
||||
for ( unsigned int i = 0; i < nt; i++ ) {
|
||||
states[i].state = state;
|
||||
states[i].start = i * steps;
|
||||
states[i].stop = MIN ( state->num_lines, ( i + 1 ) * steps );
|
||||
states[i].count = 0;
|
||||
states[i].cond = &cond;
|
||||
states[i].mutex = &mutex;
|
||||
states[i].acount = &count;
|
||||
states[i].plen = plen;
|
||||
states[i].pattern = pattern;
|
||||
states[i].st.callback = filter_elements;
|
||||
if ( i > 0 ) {
|
||||
g_thread_pool_push ( tpool, &states[i], NULL );
|
||||
}
|
||||
}
|
||||
// Run one in this thread.
|
||||
rofi_view_call_thread ( &states[0], NULL );
|
||||
// No need to do this with only one thread.
|
||||
if ( nt > 1 ) {
|
||||
g_mutex_lock ( &mutex );
|
||||
while ( count > 0 ) {
|
||||
g_cond_wait ( &cond, &mutex );
|
||||
}
|
||||
g_mutex_unlock ( &mutex );
|
||||
}
|
||||
g_cond_clear ( &cond );
|
||||
g_mutex_clear ( &mutex );
|
||||
for ( unsigned int i = 0; i < nt; i++ ) {
|
||||
if ( j != states[i].start ) {
|
||||
memmove ( &( state->line_map[j] ), &( state->line_map[states[i].start] ), sizeof ( unsigned int ) * ( states[i].count ) );
|
||||
}
|
||||
j += states[i].count;
|
||||
}
|
||||
if ( config.sort ) {
|
||||
g_qsort_with_data ( state->line_map, j, sizeof ( int ), lev_sort, state->distance );
|
||||
}
|
||||
|
||||
// Cleanup + bookkeeping.
|
||||
state->filtered_lines = j;
|
||||
g_free ( pattern );
|
||||
}
|
||||
else{
|
||||
for ( unsigned int i = 0; i < state->num_lines; i++ ) {
|
||||
state->line_map[i] = i;
|
||||
}
|
||||
state->filtered_lines = state->num_lines;
|
||||
}
|
||||
TICK_N ( "Filter matching done" );
|
||||
listview_set_num_elements ( state->list_view, state->filtered_lines );
|
||||
|
||||
if ( state->tb_filtered_rows ) {
|
||||
char *r = g_strdup_printf ( "%u", state->filtered_lines );
|
||||
textbox_text ( state->tb_filtered_rows, r );
|
||||
g_free ( r );
|
||||
}
|
||||
if ( state->tb_total_rows ) {
|
||||
char *r = g_strdup_printf ( "%u", state->num_lines );
|
||||
textbox_text ( state->tb_total_rows, r );
|
||||
g_free ( r );
|
||||
}
|
||||
TICK_N ( "Update filter lines" );
|
||||
|
||||
if ( config.auto_select == TRUE && state->filtered_lines == 1 && state->num_lines > 1 ) {
|
||||
( state->selected_line ) = state->line_map[listview_get_selected ( state->list_view )];
|
||||
state->retv = MENU_OK;
|
||||
state->quit = TRUE;
|
||||
}
|
||||
|
||||
// Size the window.
|
||||
int height = rofi_view_calculate_height ( state );
|
||||
if ( height != state->height ) {
|
||||
state->height = height;
|
||||
rofi_view_calculate_window_position ( state );
|
||||
rofi_view_window_update_size ( state );
|
||||
g_debug ( "Resize based on re-filter" );
|
||||
}
|
||||
TICK_N ( "Filter resize window based on window " );
|
||||
state->refilter = FALSE;
|
||||
TICK_N ( "Filter done" );
|
||||
}
|
||||
/**
|
||||
* @param state The Menu Handle
|
||||
*
|
||||
* Check if a finalize function is set, and if sets executes it.
|
||||
*/
|
||||
void process_result ( RofiViewState *state );
|
||||
static void xcb_rofi_view_finalize ( RofiViewState *state )
|
||||
{
|
||||
if ( state && state->finalize != NULL ) {
|
||||
state->finalize ( state );
|
||||
}
|
||||
}
|
||||
|
||||
static void rofi_view_trigger_global_action ( KeyBindingAction action )
|
||||
{
|
||||
RofiViewState *state = rofi_view_get_active ();
|
||||
|
@ -1465,16 +1187,6 @@ static void xcb_rofi_view_handle_text ( RofiViewState *state, char *text )
|
|||
}
|
||||
}
|
||||
|
||||
static void xcb_rofi_view_handle_mouse_motion ( RofiViewState *state, gint x, gint y )
|
||||
{
|
||||
state->mouse.x = x;
|
||||
state->mouse.y = y;
|
||||
if ( state->mouse.motion_target != NULL ) {
|
||||
widget_xy_to_relative ( state->mouse.motion_target, &x, &y );
|
||||
widget_motion_notify ( state->mouse.motion_target, x, y );
|
||||
}
|
||||
}
|
||||
|
||||
static void xcb_rofi_view_maybe_update ( RofiViewState *state )
|
||||
{
|
||||
if ( rofi_view_get_completed ( state ) ) {
|
||||
|
@ -1550,7 +1262,7 @@ static void xcb_rofi_view_frame_callback ( void )
|
|||
}
|
||||
}
|
||||
|
||||
static int rofi_view_calculate_height ( RofiViewState *state )
|
||||
static int xcb_rofi_view_calculate_window_height ( RofiViewState *state )
|
||||
{
|
||||
if ( CacheState.fullscreen == TRUE ) {
|
||||
return CacheState.mon.h;
|
||||
|
@ -1867,7 +1579,7 @@ static RofiViewState *xcb_rofi_view_create ( Mode *sw,
|
|||
listview_set_fixed_num_lines ( state->list_view );
|
||||
}
|
||||
|
||||
state->height = rofi_view_calculate_height ( state );
|
||||
state->height = xcb_rofi_view_calculate_window_height ( state );
|
||||
// Move the window to the correct x,y position.
|
||||
rofi_view_calculate_window_position ( state );
|
||||
rofi_view_window_update_size ( state );
|
||||
|
@ -1884,6 +1596,13 @@ static RofiViewState *xcb_rofi_view_create ( Mode *sw,
|
|||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param state The Menu Handle
|
||||
*
|
||||
* Check if a finalize function is set, and if sets executes it.
|
||||
*/
|
||||
void process_result ( RofiViewState *state );
|
||||
|
||||
static int xcb_rofi_view_error_dialog ( const char *msg, int markup )
|
||||
{
|
||||
RofiViewState *state = __rofi_view_state_create ();
|
||||
|
@ -1973,40 +1692,6 @@ static void xcb_rofi_view_cleanup ()
|
|||
xcb_flush ( xcb->connection );
|
||||
g_assert ( g_queue_is_empty ( &( CacheState.views ) ) );
|
||||
}
|
||||
static void xcb_rofi_view_workers_initialize ( void )
|
||||
{
|
||||
TICK_N ( "Setup Threadpool, start" );
|
||||
if ( config.threads == 0 ) {
|
||||
config.threads = 1;
|
||||
long procs = sysconf ( _SC_NPROCESSORS_CONF );
|
||||
if ( procs > 0 ) {
|
||||
config.threads = MIN ( procs, 128l );
|
||||
}
|
||||
}
|
||||
// Create thread pool
|
||||
GError *error = NULL;
|
||||
tpool = g_thread_pool_new ( rofi_view_call_thread, NULL, config.threads, FALSE, &error );
|
||||
if ( error == NULL ) {
|
||||
// Idle threads should stick around for a max of 60 seconds.
|
||||
g_thread_pool_set_max_idle_time ( 60000 );
|
||||
// We are allowed to have
|
||||
g_thread_pool_set_max_threads ( tpool, config.threads, &error );
|
||||
}
|
||||
// If error occurred during setup of pool, tell user and exit.
|
||||
if ( error != NULL ) {
|
||||
g_warning ( "Failed to setup thread pool: '%s'", error->message );
|
||||
g_error_free ( error );
|
||||
exit ( EXIT_FAILURE );
|
||||
}
|
||||
TICK_N ( "Setup Threadpool, done" );
|
||||
}
|
||||
static void xcb_rofi_view_workers_finalize ( void )
|
||||
{
|
||||
if ( tpool ) {
|
||||
g_thread_pool_free ( tpool, TRUE, TRUE );
|
||||
tpool = NULL;
|
||||
}
|
||||
}
|
||||
static Mode * xcb_rofi_view_get_mode ( RofiViewState *state )
|
||||
{
|
||||
return state->sw;
|
||||
|
@ -2035,11 +1720,6 @@ static void xcb_rofi_view_clear_input ( RofiViewState *state )
|
|||
}
|
||||
}
|
||||
|
||||
static void xcb_rofi_view_ellipsize_start ( RofiViewState *state )
|
||||
{
|
||||
listview_set_ellipsize_start ( state->list_view );
|
||||
}
|
||||
|
||||
static void xcb_rofi_view_switch_mode ( RofiViewState *state, Mode *mode )
|
||||
{
|
||||
state->sw = mode;
|
||||
|
@ -2082,20 +1762,12 @@ static void xcb_rofi_view_set_window_title ( const char * title )
|
|||
|
||||
static view_proxy view_ = {
|
||||
.create = xcb_rofi_view_create,
|
||||
.finalize = xcb_rofi_view_finalize,
|
||||
.get_return_value = xcb_rofi_view_get_return_value,
|
||||
.get_next_position = xcb_rofi_view_get_next_position,
|
||||
.handle_text = xcb_rofi_view_handle_text,
|
||||
.handle_mouse_motion = xcb_rofi_view_handle_mouse_motion,
|
||||
.maybe_update = xcb_rofi_view_maybe_update,
|
||||
.temp_configure_notify = xcb_rofi_view_temp_configure_notify,
|
||||
.temp_click_to_exit = xcb_rofi_view_temp_click_to_exit,
|
||||
.frame_callback = xcb_rofi_view_frame_callback,
|
||||
.get_completed = xcb_rofi_view_get_completed,
|
||||
.get_user_input = xcb_rofi_view_get_user_input,
|
||||
.set_selected_line = xcb_rofi_view_set_selected_line,
|
||||
.get_selected_line = xcb_rofi_view_get_selected_line,
|
||||
.restart = xcb_rofi_view_restart,
|
||||
.trigger_action = xcb_rofi_view_trigger_action,
|
||||
.free = xcb_rofi_view_free,
|
||||
.get_active = xcb_rofi_view_get_active,
|
||||
|
@ -2103,6 +1775,10 @@ static view_proxy view_ = {
|
|||
.error_dialog = xcb_rofi_view_error_dialog,
|
||||
.queue_redraw = xcb_rofi_view_queue_redraw,
|
||||
|
||||
.calculate_window_position = xcb_rofi_view_calculate_window_position,
|
||||
.calculate_window_height = xcb_rofi_view_calculate_window_height,
|
||||
.window_update_size = xcb_rofi_view_window_update_size,
|
||||
|
||||
.cleanup = xcb_rofi_view_cleanup,
|
||||
.get_mode = xcb_rofi_view_get_mode,
|
||||
.hide = xcb_rofi_view_hide,
|
||||
|
@ -2112,13 +1788,9 @@ static view_proxy view_ = {
|
|||
.clear_input = xcb_rofi_view_clear_input,
|
||||
.__create_window = xcb___create_window,
|
||||
.get_window = xcb_rofi_view_get_window,
|
||||
.workers_initialize = xcb_rofi_view_workers_initialize,
|
||||
.workers_finalize = xcb_rofi_view_workers_finalize,
|
||||
.get_current_monitor = xcb_rofi_view_get_current_monitor,
|
||||
.capture_screenshot = xcb_rofi_view_capture_screenshot,
|
||||
|
||||
.ellipsize_start = xcb_rofi_view_ellipsize_start,
|
||||
|
||||
.set_size = NULL,
|
||||
.get_size = NULL,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue