1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-07-31 21:59:25 -04:00

Put things together, making desktop menu work

This commit is contained in:
Dave Davenport 2015-11-03 17:34:02 +01:00
parent 6f404ddc8e
commit 80ac118655
7 changed files with 144 additions and 81 deletions

View file

@ -49,14 +49,15 @@ typedef enum
NORMAL = 0, NORMAL = 0,
URGENT = 1, URGENT = 1,
ACTIVE = 2, ACTIVE = 2,
MARKUP = 4,
// Alternating row. // Alternating row.
ALT = 4, ALT = 8,
// Render font highlighted (inverted colors.) // Render font highlighted (inverted colors.)
HIGHLIGHT = 8, HIGHLIGHT = 16,
FMOD_MASK = ( ALT | HIGHLIGHT ), FMOD_MASK = ( ALT | HIGHLIGHT ),
STATE_MASK = ~( ALT | HIGHLIGHT ) STATE_MASK = ~( MARKUP | ALT | HIGHLIGHT )
} TextBoxFontType; } TextBoxFontType;
textbox* textbox_create ( TextboxFlags flags, textbox* textbox_create ( TextboxFlags flags,

View file

@ -80,16 +80,40 @@ static void exec_cmd ( const char *cmd, int run_in_term )
execsh ( cmd, run_in_term ); execsh ( cmd, run_in_term );
} }
typedef struct _DRunModeEntry
{
char *exec;
unsigned int terminal;
} DRunModeEntry;
typedef struct _DRunModePrivateData
{
unsigned int id;
char **cmd_list;
DRunModeEntry *entry_list;
unsigned int cmd_list_length;
} DRunModePrivateData;
static void exec_cmd_entry ( DRunModeEntry *e )
{
// strip % arguments
gchar *str = g_strdup ( e->exec );
for ( ssize_t i = 0; str && str[i]; i++ ) {
if ( str[i] == '%' ) {
while ( str[i] != ' ' && str[i] != '\0' ) {
str[i++] = ' ';
}
}
}
execsh ( str, e->terminal );
g_free ( str );
}
/** /**
* Internal spider used to get list of executables. * Internal spider used to get list of executables.
*/ */
static char** get_apps ( unsigned int *length ) static void get_apps_dir ( DRunModePrivateData *pd, const char *bp )
{ {
char **retv = NULL;
const char * const * dr = g_get_system_data_dirs();
while(dr != NULL && *dr != NULL ) {
gchar *bp = g_build_filename(*dr, "applications", NULL);
DIR *dir = opendir ( bp ); DIR *dir = opendir ( bp );
if ( dir != NULL ) { if ( dir != NULL ) {
@ -103,52 +127,85 @@ static char** get_apps ( unsigned int *length )
if ( dent->d_name[0] == '.' ) { if ( dent->d_name[0] == '.' ) {
continue; continue;
} }
gchar *path = g_build_filename(bp, dent->d_name, NULL); gchar *path = g_build_filename ( bp, dent->d_name, NULL );
GKeyFile *kf = g_key_file_new(); GKeyFile *kf = g_key_file_new ();
GError *error = NULL; GError *error = NULL;
// TODO: check what flags to set; // TODO: check what flags to set;
g_key_file_load_from_file(kf, path, 0, NULL); g_key_file_load_from_file ( kf, path, 0, NULL );
if ( error == NULL ) { if ( error == NULL ) {
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( *retv ) ); if ( g_key_file_has_key ( kf, "Desktop Entry", "Exec", NULL ) ) {
if( g_key_file_has_key(kf, "Desktop Entry", "Name", NULL) ) { pd->cmd_list = g_realloc ( pd->cmd_list, ( ( pd->cmd_list_length ) + 2 ) * sizeof ( *( pd->cmd_list ) ) );
pd->entry_list = g_realloc ( pd->entry_list, ( pd->cmd_list_length + 2 ) * sizeof ( *( pd->entry_list ) ) );
if ( g_key_file_has_key ( kf, "Desktop Entry", "Name", NULL ) ) {
gchar *n = NULL; gchar *n = NULL;
gchar *gn = NULL; gchar *gn = NULL;
n = g_key_file_get_string(kf, "Desktop Entry", "Name", NULL); n = g_key_file_get_string ( kf, "Desktop Entry", "Name", NULL );
gn = g_key_file_get_string(kf, "Desktop Entry", "GenericName", NULL); gn = g_key_file_get_string ( kf, "Desktop Entry", "GenericName", NULL );
retv[( *length )] = g_strdup_printf("%-30s\t%s", n, gn?gn:""); if ( gn == NULL ) {
g_free(n); g_free(gn); pd->cmd_list[pd->cmd_list_length] = g_markup_escape_text ( n, -1 );
} else {
retv[(*length)] = g_strdup(dent->d_name);
} }
retv[( *length ) + 1] = NULL; else {
( *length )++; ( pd->cmd_list )[( pd->cmd_list_length )] = g_markup_printf_escaped (
} else { "%s <span weight='light' size='small'><i>(%s)</i></span>",
g_error_free(error); n,
gn ? gn : "" );
} }
g_key_file_free(kf); g_free ( n ); g_free ( gn );
g_free(path); }
else {
( pd->cmd_list )[( pd->cmd_list_length )] = g_strdup ( dent->d_name );
}
pd->entry_list[pd->cmd_list_length].exec = g_key_file_get_string ( kf, "Desktop Entry", "Exec", NULL );
if ( g_key_file_has_key ( kf, "Desktop Entry", "Terminal", NULL ) ) {
pd->entry_list[pd->cmd_list_length].terminal = g_key_file_get_boolean ( kf, "Desktop Entry", "Terminal", NULL );
}
( pd->cmd_list )[( pd->cmd_list_length ) + 1] = NULL;
( pd->cmd_list_length )++;
}
}
else {
g_error_free ( error );
}
g_key_file_free ( kf );
g_free ( path );
} }
closedir ( dir ); closedir ( dir );
} }
dr++;
g_free(bp);
}
// No sorting needed.
if ( ( *length ) == 0 ) {
return retv;
}
return retv;
} }
typedef struct _DRunModePrivateData static void get_apps ( DRunModePrivateData *pd )
{ {
unsigned int id; const char * const * dr = g_get_system_data_dirs ();
char **cmd_list; const char * const * iter = dr;
unsigned int cmd_list_length; while ( iter != NULL && *iter != NULL && **iter != '\0' ) {
} DRunModePrivateData; gboolean skip = FALSE;
for ( size_t i = 0; !skip && dr[i] != ( *iter ); i++ ) {
skip = ( g_strcmp0 ( *iter, dr[i] ) == 0 );
}
if ( skip ) {
iter++;
continue;
}
gchar *bp = g_build_filename ( *iter, "applications", NULL );
get_apps_dir ( pd, bp );
g_free ( bp );
iter++;
}
const char *d = g_get_user_data_dir ();
for ( size_t i = 0; dr[i] != NULL; i++ ) {
if ( g_strcmp0 ( d, dr[i] ) == 0 ) {
// Done this already, no need to repeat.
return;
}
}
if ( d ) {
gchar *bp = g_build_filename ( d, "applications", NULL );
get_apps_dir ( pd, bp );
g_free ( bp );
}
}
static void drun_mode_init ( Switcher *sw ) static void drun_mode_init ( Switcher *sw )
{ {
@ -163,7 +220,7 @@ static char ** drun_mode_get_data ( unsigned int *length, Switcher *sw )
DRunModePrivateData *rmpd = (DRunModePrivateData *) sw->private_data; DRunModePrivateData *rmpd = (DRunModePrivateData *) sw->private_data;
if ( rmpd->cmd_list == NULL ) { if ( rmpd->cmd_list == NULL ) {
rmpd->cmd_list_length = 0; rmpd->cmd_list_length = 0;
rmpd->cmd_list = get_apps ( &( rmpd->cmd_list_length ) ); get_apps ( rmpd );
} }
if ( length != NULL ) { if ( length != NULL ) {
*length = rmpd->cmd_list_length; *length = rmpd->cmd_list_length;
@ -189,7 +246,7 @@ static SwitcherMode drun_mode_result ( int mretv, char **input, unsigned int sel
retv = ( mretv & MENU_LOWER_MASK ); retv = ( mretv & MENU_LOWER_MASK );
} }
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) { else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
exec_cmd ( rmpd->cmd_list[selected_line], shift ); exec_cmd_entry ( &( rmpd->entry_list[selected_line] ) );
} }
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) { else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
exec_cmd ( *input, shift ); exec_cmd ( *input, shift );
@ -201,14 +258,19 @@ static void drun_mode_destroy ( Switcher *sw )
{ {
DRunModePrivateData *rmpd = (DRunModePrivateData *) sw->private_data; DRunModePrivateData *rmpd = (DRunModePrivateData *) sw->private_data;
if ( rmpd != NULL ) { if ( rmpd != NULL ) {
g_strfreev(rmpd->cmd_list); g_strfreev ( rmpd->cmd_list );
for ( size_t i = 0; i < rmpd->cmd_list_length; i++ ) {
g_free ( rmpd->entry_list[i].exec );
}
g_free ( rmpd->entry_list );
g_free ( rmpd ); g_free ( rmpd );
sw->private_data = NULL; sw->private_data = NULL;
} }
} }
static const char *mgrv ( unsigned int selected_line, void *sw, G_GNUC_UNUSED int *state ) static const char *mgrv ( unsigned int selected_line, void *sw, int *state )
{ {
*state |= MARKUP;
return drun_mode_get_data ( NULL, sw )[selected_line]; return drun_mode_get_data ( NULL, sw )[selected_line];
} }

View file

@ -549,7 +549,7 @@ static void window_mode_destroy ( Switcher *sw )
} }
} }
static const char *mgrv ( unsigned int selected_line, void *sw, G_GNUC_UNUSED int *state ) static const char *mgrv ( unsigned int selected_line, void *sw, int *state )
{ {
SwitcherModePrivateData *rmpd = ( (Switcher *) sw )->private_data; SwitcherModePrivateData *rmpd = ( (Switcher *) sw )->private_data;
if ( window_client ( display, rmpd->ids->array[selected_line] )->demands ) { if ( window_client ( display, rmpd->ids->array[selected_line] )->demands ) {

View file

@ -143,7 +143,7 @@ void textbox_text ( textbox *tb, const char *text )
} }
} }
if ( tb->flags & TB_MARKUP ) { if ( tb->flags & TB_MARKUP || tb->tbft & MARKUP ) {
pango_layout_set_markup ( tb->layout, tb->text, strlen ( tb->text ) ); pango_layout_set_markup ( tb->layout, tb->text, strlen ( tb->text ) );
} }
else { else {
@ -648,8 +648,8 @@ void textbox_setup ( Display *display )
parse_color ( display, config.menu_hlbg_active, &( colors[ACTIVE].hlbg ) ); parse_color ( display, config.menu_hlbg_active, &( colors[ACTIVE].hlbg ) );
} }
PangoFontMap *font_map = pango_cairo_font_map_get_default (); PangoFontMap *font_map = pango_cairo_font_map_get_default ();
if(config.dpi > 0 ) { if ( config.dpi > 0 ) {
pango_cairo_font_map_set_resolution((PangoCairoFontMap*)font_map, (double)config.dpi); pango_cairo_font_map_set_resolution ( (PangoCairoFontMap *) font_map, (double) config.dpi );
} }
p_context = pango_font_map_create_context ( font_map ); p_context = pango_font_map_create_context ( font_map );
} }