rofi/source/mode.c

249 lines
7 KiB
C
Raw Normal View History

/*
* rofi
*
* MIT/X11 License
2023-01-14 12:02:35 +00:00
* Copyright © 2013-2023 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.
*
*/
2021-08-17 23:16:45 +00:00
#include "mode.h"
2016-01-07 18:47:37 +00:00
#include "rofi.h"
2016-01-07 20:27:20 +00:00
#include "xrmoptions.h"
2021-08-17 23:16:45 +00:00
#include <glib.h>
#include <stdio.h>
#include <string.h>
2016-01-07 18:47:37 +00:00
#include "rofi-icon-fetcher.h"
2016-01-07 18:47:37 +00:00
// This one should only be in mode implementations.
#include "helper.h"
2016-01-07 18:47:37 +00:00
#include "mode-private.h"
/**
* @ingroup MODE
* @{
*/
2021-08-17 23:16:45 +00:00
int mode_init(Mode *mode) {
g_return_val_if_fail(mode != NULL, FALSE);
g_return_val_if_fail(mode->_init != NULL, FALSE);
Merging in the Recursive file browser. Squashed commit of the following: commit 92e730076d461622dc81e44e87ec456317514904 Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 11 18:17:12 2023 +0200 [Doc] Add regex filtering to recursivebrowser. commit ee80c8487f9765b1e6e8ab8219a6baea089cf5af Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 11 17:49:29 2023 +0200 [recursivebrowser] Update manpage. commit a24b68f52362aaf1461935c2340e3bf5e31da59d Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 11 17:37:56 2023 +0200 [Mode] Add some extra validating of the mode selected to complete. commit cf497e8685e806521c0f61922827687adce268c9 Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 4 15:12:31 2023 +0200 [Recursive browser] Make completer selectable. commit 722f07a803c28a406d8a610f31a24b3f7247b9ba Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 4 14:36:14 2023 +0200 Add methods for completer to modes. commit 7972420c30275514751802d1ed517a45bbd83da1 Author: Qball Cow <qball@blame.services> Date: Thu Jun 1 21:56:06 2023 +0200 Prepare updates for new APIs. commit dd3035a1a61f8196d394f6867701a0e1b3af30ac Author: Dave Davenport <qball@gmpclient.org> Date: Wed May 10 19:24:48 2023 +0200 [RB] Fix regex and cleanups commit 4d2941caf32dfb946aee54c467c1319c7a89804a Author: Dave Davenport <qball@blame.services> Date: Wed May 10 18:09:54 2023 +0200 [RB] Add (unfinished regex test) commit 848277001fc8cf9afc538067f2afa24a174f8c7f Author: Dave Davenport <qball@blame.services> Date: Wed May 10 17:49:16 2023 +0200 [RB] Pull the scanning into a separate thread. commit f369a7f63f618bbcad10c18e73f7e2b117c515f1 Author: Dave Davenport <qball@gmpclient.org> Date: Wed May 3 18:35:15 2023 +0200 [Recursive File Browser] First test version.
2023-06-12 17:07:00 +00:00
if (mode->type == MODE_TYPE_UNSET) {
g_warning("Mode '%s' does not have a type set. Please update mode/plugin.",
mode->name);
}
if ((mode->type & MODE_TYPE_COMPLETER) == MODE_TYPE_COMPLETER) {
if (mode->_completer_result == NULL) {
g_error(
"Mode '%s' is incomplete and does not implement _completer_result.",
mode->name);
}
}
// to make sure this is initialized correctly.
mode->fallback_icon_fetch_uid = 0;
mode->fallback_icon_not_found = FALSE;
2021-08-17 23:16:45 +00:00
return mode->_init(mode);
2016-01-07 18:47:37 +00:00
}
2021-08-17 23:16:45 +00:00
void mode_destroy(Mode *mode) {
g_assert(mode != NULL);
g_assert(mode->_destroy != NULL);
mode->_destroy(mode);
2016-01-07 18:47:37 +00:00
}
2021-08-17 23:16:45 +00:00
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);
2016-01-07 18:47:37 +00:00
}
2021-08-17 23:16:45 +00:00
char *mode_get_display_value(const Mode *mode, unsigned int selected_line,
int *state, GList **attribute_list,
int get_entry) {
g_assert(mode != NULL);
g_assert(state != NULL);
g_assert(mode->_get_display_value != NULL);
2016-01-07 18:47:37 +00:00
2021-08-17 23:16:45 +00:00
return mode->_get_display_value(mode, selected_line, state, attribute_list,
get_entry);
2016-01-07 20:27:20 +00:00
}
cairo_surface_t *mode_get_icon(Mode *mode, unsigned int selected_line,
2022-07-22 22:28:55 +00:00
unsigned int height) {
2021-08-17 23:16:45 +00:00
g_assert(mode != NULL);
2017-04-12 15:58:32 +00:00
2021-08-17 23:16:45 +00:00
if (mode->_get_icon != NULL) {
cairo_surface_t *icon = mode->_get_icon(mode, selected_line, height);
if (icon) {
return icon;
}
2021-08-17 23:16:45 +00:00
}
if (mode->fallback_icon_not_found == TRUE) {
return NULL;
}
if (mode->fallback_icon_fetch_uid > 0) {
cairo_surface_t *icon =
rofi_icon_fetcher_get(mode->fallback_icon_fetch_uid);
return icon;
}
ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
if (wid) {
/** Load user entires */
Property *p =
rofi_theme_find_property(wid, P_STRING, "fallback-icon", TRUE);
if (p != NULL && (p->type == P_STRING && p->value.s)) {
mode->fallback_icon_fetch_uid =
rofi_icon_fetcher_query(p->value.s, height);
return NULL;
}
}
mode->fallback_icon_not_found = TRUE;
2021-08-17 23:16:45 +00:00
return NULL;
2017-04-12 15:58:32 +00:00
}
2021-08-17 23:16:45 +00:00
char *mode_get_completion(const Mode *mode, unsigned int selected_line) {
g_assert(mode != NULL);
if (mode->_get_completion != NULL) {
return mode->_get_completion(mode, selected_line);
}
int state;
g_assert(mode->_get_display_value != NULL);
return mode->_get_display_value(mode, selected_line, &state, NULL, TRUE);
2016-01-07 20:27:20 +00:00
}
2021-08-17 23:16:45 +00:00
ModeMode mode_result(Mode *mode, int menu_retv, char **input,
unsigned int selected_line) {
if (menu_retv & MENU_NEXT) {
return NEXT_DIALOG;
}
if (menu_retv & MENU_PREVIOUS) {
return PREVIOUS_DIALOG;
}
if (menu_retv & MENU_QUICK_SWITCH) {
return menu_retv & MENU_LOWER_MASK;
}
2021-08-17 23:16:45 +00:00
g_assert(mode != NULL);
g_assert(mode->_result != NULL);
g_assert(input != NULL);
2021-08-17 23:16:45 +00:00
return mode->_result(mode, menu_retv, input, selected_line);
2016-01-07 20:27:20 +00:00
}
2021-08-17 23:16:45 +00:00
int mode_token_match(const Mode *mode, rofi_int_matcher **tokens,
unsigned int selected_line) {
g_assert(mode != NULL);
g_assert(mode->_token_match != NULL);
return mode->_token_match(mode, tokens, selected_line);
2016-01-07 20:27:20 +00:00
}
2021-08-17 23:16:45 +00:00
const char *mode_get_name(const Mode *mode) {
g_assert(mode != NULL);
return mode->name;
2016-01-07 20:27:20 +00:00
}
2021-08-17 23:16:45 +00:00
void mode_free(Mode **mode) {
g_assert(mode != NULL);
g_assert((*mode) != NULL);
if ((*mode)->free != NULL) {
(*mode)->free(*mode);
}
(*mode) = NULL;
2016-01-07 20:27:20 +00:00
}
2021-08-17 23:16:45 +00:00
void *mode_get_private_data(const Mode *mode) {
g_assert(mode != NULL);
return mode->private_data;
2016-01-07 20:27:20 +00:00
}
2021-08-17 23:16:45 +00:00
void mode_set_private_data(Mode *mode, void *pd) {
g_assert(mode != NULL);
if (pd != NULL) {
g_assert(mode->private_data == NULL);
}
mode->private_data = pd;
2016-01-07 18:47:37 +00:00
}
2021-08-17 23:16:45 +00:00
const char *mode_get_display_name(const Mode *mode) {
/** Find the widget */
2021-08-17 23:16:45 +00:00
ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
if (wid) {
/** Check string property */
2021-08-17 23:16:45 +00:00
Property *p = rofi_theme_find_property(wid, P_STRING, "display-name", TRUE);
if (p != NULL && p->type == P_STRING) {
2021-06-15 10:31:32 +00:00
return p->value.s;
}
}
2021-08-17 23:16:45 +00:00
if (mode->display_name != NULL) {
2021-06-15 10:31:32 +00:00
return mode->display_name;
2021-08-17 23:16:45 +00:00
}
return mode->name;
}
2021-08-17 23:16:45 +00:00
void mode_set_config(Mode *mode) {
snprintf(mode->cfg_name_key, 128, "display-%s", mode->name);
config_parser_add_option(xrm_String, mode->cfg_name_key,
(void **)&(mode->display_name),
"The display name of this browser");
}
2021-08-17 23:16:45 +00:00
char *mode_preprocess_input(Mode *mode, const char *input) {
if (mode->_preprocess_input) {
return mode->_preprocess_input(mode, input);
}
return g_strdup(input);
}
2021-08-17 23:16:45 +00:00
char *mode_get_message(const Mode *mode) {
if (mode->_get_message) {
return mode->_get_message(mode);
}
return NULL;
}
Merging in the Recursive file browser. Squashed commit of the following: commit 92e730076d461622dc81e44e87ec456317514904 Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 11 18:17:12 2023 +0200 [Doc] Add regex filtering to recursivebrowser. commit ee80c8487f9765b1e6e8ab8219a6baea089cf5af Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 11 17:49:29 2023 +0200 [recursivebrowser] Update manpage. commit a24b68f52362aaf1461935c2340e3bf5e31da59d Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 11 17:37:56 2023 +0200 [Mode] Add some extra validating of the mode selected to complete. commit cf497e8685e806521c0f61922827687adce268c9 Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 4 15:12:31 2023 +0200 [Recursive browser] Make completer selectable. commit 722f07a803c28a406d8a610f31a24b3f7247b9ba Author: Dave Davenport <qball@gmpclient.org> Date: Sun Jun 4 14:36:14 2023 +0200 Add methods for completer to modes. commit 7972420c30275514751802d1ed517a45bbd83da1 Author: Qball Cow <qball@blame.services> Date: Thu Jun 1 21:56:06 2023 +0200 Prepare updates for new APIs. commit dd3035a1a61f8196d394f6867701a0e1b3af30ac Author: Dave Davenport <qball@gmpclient.org> Date: Wed May 10 19:24:48 2023 +0200 [RB] Fix regex and cleanups commit 4d2941caf32dfb946aee54c467c1319c7a89804a Author: Dave Davenport <qball@blame.services> Date: Wed May 10 18:09:54 2023 +0200 [RB] Add (unfinished regex test) commit 848277001fc8cf9afc538067f2afa24a174f8c7f Author: Dave Davenport <qball@blame.services> Date: Wed May 10 17:49:16 2023 +0200 [RB] Pull the scanning into a separate thread. commit f369a7f63f618bbcad10c18e73f7e2b117c515f1 Author: Dave Davenport <qball@gmpclient.org> Date: Wed May 3 18:35:15 2023 +0200 [Recursive File Browser] First test version.
2023-06-12 17:07:00 +00:00
Mode *mode_create(const Mode *mode) {
if (mode->_create) {
return mode->_create();
}
return NULL;
}
ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input,
unsigned int selected_line, char **path) {
if ((mode->type & MODE_TYPE_COMPLETER) == 0) {
g_warning("Trying to call completer_result on non completion mode.");
return 0;
}
if (mode->_completer_result) {
return mode->_completer_result(mode, menu_retv, input, selected_line, path);
}
return 0;
}
gboolean mode_is_completer(const Mode *mode) {
if (mode) {
if ((mode->type & MODE_TYPE_COMPLETER) == MODE_TYPE_COMPLETER) {
return TRUE;
}
}
return FALSE;
}
2020-10-12 19:39:36 +00:00
/**@}*/