1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-02-10 15:44:41 -05:00

[Mode] Add some wrappers and use object via accessors.

* Make sure file is only shown once in parsed list, not twice.
This commit is contained in:
Qball Cow 2025-01-26 20:07:04 +01:00
parent 613bc5814c
commit e068ddbc01
4 changed files with 53 additions and 20 deletions

View file

@ -31,9 +31,6 @@
#include <gmodule.h> #include <gmodule.h>
G_BEGIN_DECLS G_BEGIN_DECLS
/** ABI version to check if loaded plugin is compatible. */
#define ABI_VERSION 7u
/** /**
* Indicator what type of mode this is. * Indicator what type of mode this is.
* For now it can be the classic switcher, or also implement a completer. * For now it can be the classic switcher, or also implement a completer.

View file

@ -29,7 +29,12 @@
#define ROFI_MODE_H #define ROFI_MODE_H
#include "rofi-types.h" #include "rofi-types.h"
#include <cairo.h> #include <cairo.h>
#include <gmodule.h>
G_BEGIN_DECLS G_BEGIN_DECLS
/** ABI version to check if loaded plugin is compatible. */
#define ABI_VERSION 7u
/** /**
* @defgroup MODE Mode * @defgroup MODE Mode
* *
@ -277,6 +282,28 @@ ModeMode mode_completer_result(Mode *sw, int menu_retv, char **input,
* @returns TRUE if mode can be used as completer. * @returns TRUE if mode can be used as completer.
*/ */
gboolean mode_is_completer(const Mode *sw); gboolean mode_is_completer(const Mode *sw);
/**
* @param sw The mode to query
*
* @returns the modes ABI version.
*/
int mode_get_abi_version(Mode *const mode);
/**
* @param sw The mode to query
* @param mod The GModule used to load the mode
*
* Set GModule used to load this plugin, this is used to
* unload it on shutdown.
*/
void mode_plugin_set_module(Mode *mode, GModule *mod);
/**
* @param sw The mode to query
*
* @returns the GModule used to load this plugin. NULL if not a plugin.
*/
GModule *mode_plugin_get_module(Mode *mode);
/**@}*/ /**@}*/
G_END_DECLS G_END_DECLS
#endif #endif

View file

@ -159,6 +159,11 @@ const char *mode_get_name(const Mode *mode) {
return mode->name; return mode->name;
} }
int mode_get_abi_version(Mode *const mode) {
g_assert(mode != NULL);
return mode->abi_version;
}
void mode_free(Mode **mode) { void mode_free(Mode **mode) {
g_assert(mode != NULL); g_assert(mode != NULL);
g_assert((*mode) != NULL); g_assert((*mode) != NULL);
@ -245,4 +250,10 @@ gboolean mode_is_completer(const Mode *mode) {
return FALSE; return FALSE;
} }
void mode_plugin_set_module(Mode *mode, GModule *mod){
mode->module = mod;
}
GModule *mode_plugin_get_module(Mode *mode){
return mode->module;
}
/**@}*/ /**@}*/

View file

@ -74,10 +74,6 @@
#include "timings.h" #include "timings.h"
// Plugin abi version.
// TODO: move this check to mode.c
#include "mode-private.h"
/** Location of pidfile for this instance. */ /** Location of pidfile for this instance. */
char *pidfile = NULL; char *pidfile = NULL;
/** Location of Cache directory. */ /** Location of Cache directory. */
@ -205,7 +201,7 @@ static void run_mode_index(ModeMode mode) {
for (unsigned int i = 0; i < num_modes; i++) { for (unsigned int i = 0; i < num_modes; i++) {
if (!mode_init(modes[i])) { if (!mode_init(modes[i])) {
GString *str = g_string_new("Failed to initialize the mode: "); GString *str = g_string_new("Failed to initialize the mode: ");
g_string_append(str, modes[i]->name); g_string_append(str, mode_get_name(modes[i]));
g_string_append(str, "\n"); g_string_append(str, "\n");
rofi_view_error_dialog(str->str, ERROR_MSG_MARKUP); rofi_view_error_dialog(str->str, ERROR_MSG_MARKUP);
@ -306,7 +302,7 @@ static void print_list_of_modes(int is_term) {
} }
printf(" • %s%s%s%s\n", active ? "+" : "", printf(" • %s%s%s%s\n", active ? "+" : "",
is_term ? (active ? color_green : color_red) : "", is_term ? (active ? color_green : color_red) : "",
available_modes[i]->name, is_term ? color_reset : ""); mode_get_name(available_modes[i]), is_term ? color_reset : "");
} }
} }
static void print_main_application_options(int is_term) { static void print_main_application_options(int is_term) {
@ -473,7 +469,7 @@ static void help_print_mode_not_found(const char *mode) {
} }
} }
g_string_append_printf(str, " * %s%s\n", active ? "+" : "", g_string_append_printf(str, " * %s%s\n", active ? "+" : "",
available_modes[i]->name); mode_get_name(available_modes[i]));
} }
rofi_add_error_message(str); rofi_add_error_message(str);
} }
@ -487,7 +483,7 @@ static void help_print_no_arguments(void) {
g_string_append(emesg, "The following modes are enabled:\n"); g_string_append(emesg, "The following modes are enabled:\n");
for (unsigned int j = 0; j < num_modes; j++) { for (unsigned int j = 0; j < num_modes; j++) {
g_string_append_printf(emesg, " • <span color=\"green\">%s</span>\n", g_string_append_printf(emesg, " • <span color=\"green\">%s</span>\n",
modes[j]->name); mode_get_name(modes[j]));
} }
g_string_append(emesg, "\nThe following modes can be enabled:\n"); g_string_append(emesg, "\nThe following modes can be enabled:\n");
for (unsigned int i = 0; i < num_available_modes; i++) { for (unsigned int i = 0; i < num_available_modes; i++) {
@ -500,7 +496,7 @@ static void help_print_no_arguments(void) {
} }
if (!active) { if (!active) {
g_string_append_printf(emesg, " • <span color=\"red\">%s</span>\n", g_string_append_printf(emesg, " • <span color=\"red\">%s</span>\n",
available_modes[i]->name); mode_get_name(available_modes[i]));
} }
} }
g_string_append(emesg, "\nTo activate a mode, add it to the list in " g_string_append(emesg, "\nTo activate a mode, add it to the list in "
@ -563,7 +559,7 @@ static void cleanup(void) {
Mode *rofi_collect_modes_search(const char *name) { Mode *rofi_collect_modes_search(const char *name) {
for (unsigned int i = 0; i < num_available_modes; i++) { for (unsigned int i = 0; i < num_available_modes; i++) {
if (g_strcmp0(name, available_modes[i]->name) == 0) { if (g_strcmp0(name, mode_get_name(available_modes[i])) == 0) {
return available_modes[i]; return available_modes[i];
} }
} }
@ -575,7 +571,7 @@ Mode *rofi_collect_modes_search(const char *name) {
* @returns TRUE when success. * @returns TRUE when success.
*/ */
static gboolean rofi_collectmodes_add(Mode *mode) { static gboolean rofi_collectmodes_add(Mode *mode) {
Mode *m = rofi_collect_modes_search(mode->name); Mode *m = rofi_collect_modes_search(mode_get_name(mode));
if (m == NULL) { if (m == NULL) {
available_modes = available_modes =
g_realloc(available_modes, sizeof(Mode *) * (num_available_modes + 1)); g_realloc(available_modes, sizeof(Mode *) * (num_available_modes + 1));
@ -603,13 +599,13 @@ static void rofi_collectmodes_dir(const char *base_dir) {
if (mod) { if (mod) {
Mode *m = NULL; Mode *m = NULL;
if (g_module_symbol(mod, "mode", (gpointer *)&m)) { if (g_module_symbol(mod, "mode", (gpointer *)&m)) {
if (m->abi_version != ABI_VERSION) { if (mode_get_abi_version(m) != ABI_VERSION) {
g_warning("ABI version of plugin: '%s' does not match: %08X " g_warning("ABI version of plugin: '%s' does not match: %08X "
"expecting: %08X", "expecting: %08X",
dn, m->abi_version, ABI_VERSION); dn, mode_get_abi_version(m), ABI_VERSION);
g_module_close(mod); g_module_close(mod);
} else { } else {
m->module = mod; mode_plugin_set_module(m, mod);
if (!rofi_collectmodes_add(m)) { if (!rofi_collectmodes_add(m)) {
g_module_close(mod); g_module_close(mod);
} }
@ -673,8 +669,8 @@ static void rofi_collectmodes_setup(void) {
} }
static void rofi_collectmodes_destroy(void) { static void rofi_collectmodes_destroy(void) {
for (unsigned int i = 0; i < num_available_modes; i++) { for (unsigned int i = 0; i < num_available_modes; i++) {
if (available_modes[i]->module) { if (mode_plugin_get_module(available_modes[i])) {
GModule *mod = available_modes[i]->module; GModule *mod = mode_plugin_get_module(available_modes[i]);
available_modes[i] = NULL; available_modes[i] = NULL;
g_module_close(mod); g_module_close(mod);
} }
@ -1073,7 +1069,9 @@ int main(int argc, char *argv[]) {
extern const char *rasi_theme_file_extensions[]; extern const char *rasi_theme_file_extensions[];
char *file2 = char *file2 =
helper_get_theme_path(config_path, rasi_theme_file_extensions, NULL); helper_get_theme_path(config_path, rasi_theme_file_extensions, NULL);
char *filename = rofi_theme_parse_prepare_file(file2); GFile *gf = g_file_new_for_path(file2);
char *filename = g_file_get_path(gf);
g_object_unref(gf);
g_free(file2); g_free(file2);
if (filename && g_file_test(filename, G_FILE_TEST_EXISTS)) { if (filename && g_file_test(filename, G_FILE_TEST_EXISTS)) {
if (rofi_theme_parse_file(filename)) { if (rofi_theme_parse_file(filename)) {