UI Actions: Difference between revisions

From vice-emu
Jump to navigation Jump to search
(Add example code)
No edit summary
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Rationale ==
<span id="ui-actions"></span>
= UI Actions =


In the Gtk3 UI the concept of "UI actions" was introduced to allow for a unified interface to trigger certain actions in the emulator and its user interface . UI actions decouple hotkeys from menu items so hotkeys can be defined that do not have a corresponding menu item. This interface will also allow calling actions from customizable joystick mappings, reusing the code triggered by menu items and/or hotkeys.
<span id="introduction"></span>
== Introduction ==


Since most user interactions are independent of what UI toolkit is used it makes sense to generalize these interactions and make all current and future UIs use this UI actions system. For example "soft reset" or "attach disk to unit #8" may use different dialogs, menu items or hotkeys, the underlying logic (and code) will be the same. So providing a predefined -- and extendable -- set of UI actions will aid in development of future UIs.
The UI actions are a way to trigger specific emulators actions from menu items, hotkeys or controller buttons without code duplication and letting VICE deal with issues like threading and avoiding showing multiple dialogs or retriggering actions that haven’t finished yet.


== Mechanism ==
UI actions are managed through an API living in <code>src/arch/shared/uiactions.h</code>. Actions are triggered by specifying action ID in menu items, or by using an action name in hotkey and joy map files (which VICE translates to IDs).


The UI actions API code resides in [https://sourceforge.net/p/vice-emu/code/HEAD/tree/trunk/vice/src/arch/shared/uiactions.h src/arch/uiactions.h] and [https://sourceforge.net/p/vice-emu/code/HEAD/tree/trunk/vice/src/arch/shared/uiactions.c src/arch/uiactions.c]. The code is documented using Doxygen docblocks, so the documentation can be generated with <tt>doc/mkdoxy.sh</tt> or just read in the files themselves.
The file <code>src/arch/shared/uiactions.h</code> contains the UI action IDs available, while the file <code>src/arch/shared/uiactions.c</code> contains a table mapping these IDs to action names.


UI actions are triggered using a unique ID and calling a function: <tt>void ui_action_trigger(int action);</tt>. The IDs are available in the [https://sourceforge.net/p/vice-emu/code/HEAD/tree/trunk/vice/src/arch/shared/uiactions.h src/arch/uiactions.h] header, these are all prefixed with <tt>ACTION_</tt>.
New actions can be created by adding a unqiue ID in the header file and an entry (with a unique name) in the aformentioned table in the source file. An ''action handler'' is required to implement the action’s logic, this handler is a callback function that gets passed some information about itself (the entry in the table, an <code>ui_action_map_t*</code>). The action will need to be registered with the UI actions system via <code>ui_actions_register()</code>. (See below)


At emulator boot the UI code calls <tt>void ui_actions_init(void)</tt> to initialize the UI actions system, sets a UI-specific dispatch function with <tt>void ui_actions_set_dispatch(void (*dispatch)(const ui_action_map_t *))</tt> and registers UI action handlers with <tt>void ui_actions_register(const ui_action_map_t *mappings)</tt>.
<span id="basic-api-usage"></span>
== Basic API usage ==


=== Example ===
The basic functions and types used by the UI actions are relatively simple. A selection of the most used functions and types is listed here.
Example code helps a lot, the following is taken from the Gtk3 UI code and cobbled together.


Here we have a few action handler implementations (from src/arch/gtk3/actions-machine.c. Some of this is Gtk3-specific, other handlers can be moved into generic code:
<span id="general-functions-dealing-with-the-ui-actions-system-itself"></span>
<syntaxhighlight lang="c" line>
=== General functions dealing with the UI actions system itself: ===
static void confirm_exit_callback(GtkDialog *dialog, gboolean result)
 
{
<syntaxhighlight lang="c">void  ui_actions_init          (void);
    if (result) {
void ui_actions_set_dispatch  (void (*dispatch)(ui_action_map_t *));
        mainlock_release();
void  ui_actions_register      (const ui_action_map_t *mappings);
        archdep_vice_exit(0);
void  ui_actions_shutdown      (void);</syntaxhighlight>
        mainlock_obtain();
VICE will call <code>ui_actions_init()</code> and <code>ui_actions_shutdown()</code>, and calling <code>ui_actions_set_dispatch()</code> is optional. So the only function required to use is <code>ui_actions_register()</code>.
    }
 
    /* mark action finished in case the user selected "No" */
The function <code>ui_actions_set_dispatch()</code> can be used to install a function that “intercepts” an action before it’s triggered by VICE, in which case the dispatch function is responsible for calling <code>ui_action_trigger(map-&gt;action)</code>. The dispatch function is used in the Gtk3 port to make sure an action that is marked as requiring the UI thread is executed on the UI thread. More on this later.
     ui_action_finish(ACTION_QUIT);
 
}
The '''<code>ui_action_map_t</code>''' type is used to register actions and to access information on an action. It maps action IDs to actions handlers and hotkeys.
 
<syntaxhighlight lang="c">typedef struct ui_action_map_s {
     int    action;         /**< action ID */


/** \brief  Quit emulator, possibly popping up a confirmation dialog */
    /*
static void quit_action(void)
    * Action handler data
{
    */
    int confirm = FALSE;


     resources_get_int("ConfirmOnExit", &confirm);
     void (*handler)(struct ui_action_map_s*); /**< function handling the action */
    if (!confirm) {
    void  *data;           /**< optional user data */
        ui_action_finish(ACTION_QUIT);
        archdep_vice_exit(0);
        return;
    }


     vice_gtk3_message_confirm(
     /* modes */
            confirm_exit_callback,
    bool  blocks;          /**< action blocks (the same action cannot be
            "Exit VICE",
                                triggered again until it finishes) */
            "Do you really wish to exit VICE?");
    bool  dialog;          /**< action pops up a dialog (only one dialog action
}
                                is allowed at a time), this implies using the
                                UI thread */
    bool  uithread;       /**< must run on the UI thread */


/** \brief  Open the monitor */
    /* state */
static void monitor_open_action(void)
     bool  is_busy;         /**< action is busy */
{
     int server = 0;


     /* don't spawn the monitor if the monitor server is running */
     /* Hotkey data left out */
    resources_get_int("MonitorServer", &server);
} ui_action_map_t;</syntaxhighlight>
    if (server == 0) {
The “Hotkey data” can be ignored: UI actions and hotkeys are closely related, and VICE uses the same struct to store hotkey data.
        vsync_suspend_speed_eval();
        if (ui_pause_active()) {
            ui_pause_enter_monitor();
        } else {
            monitor_startup_trap();
        }
    }
}


/** \brief  Trigger soft reset of the machine */
In its simplest form an initialization of the map requires providing values for <code>.action</code> and <code>.handler</code>. When providing initializers for UI action maps please use C99 designated initializers: this looks a lot cleaner and allows for adding members to the UI action map type without causing compiler warnings with existing code.
static void reset_soft_action(void)
{
    machine_trigger_reset(MACHINE_RESET_MODE_SOFT);
    ui_pause_disable();
}


/** \brief  Trigger hard reset of the machine */
<span id="simple-example-of-an-action-handler"></span>
static void reset_hard_action(void)
==== Simple example of an action handler ====
{
    machine_trigger_reset(MACHINE_RESET_MODE_HARD);
    ui_pause_disable();
}


/** \brief  Toggle PET userport diagnostic pin */
<syntaxhighlight lang="c">/* Action handler for toggling warp mode */
static void diagnostic_pin_toggle_action(void)
static void warp_mode_toggle_action(ui_action_map_t *self)
{
{
     int active = 0;
     vsync_set_warp_mode(!vsync_get_warp_mode());
 
    resources_get_int("DiagPin", &active);
    resources_set_int("DiagPin", active ? 0 : 1);
}
}
</syntaxhighlight>


This is the list of action handlers defined above we'll be passing to <tt>ui_actions_register()</tt>:
/* List of actions to register (just one here) */
<syntaxhighlight lang="c" line>
static const ui_action_map_t some_actions[] = {
/** \brief  List of machine-related actions */
     {   .action = ACTION_WARP_MODE_TOGGLE,
static const ui_action_map_t machine_actions[] = {
         .handler = warp_mode_toggle_action
     {
        .action = ACTION_QUIT,
        .handler = quit_action,
        .blocks = true,
        .dialog = true
    },
    {
        .action = ACTION_MONITOR_OPEN,
        .handler = monitor_open_action,
        .uithread = true
    },
    {
        .action = ACTION_RESET_SOFT,
         .handler = reset_soft_action
    },
    {
        .action = ACTION_RESET_HARD,
        .handler = reset_hard_action
    },
    {
        .action = ACTION_DIAGNOSTIC_PIN_TOGGLE,
        .handler = diagnostic_pin_toggle_action,
        /* no need for UI thread, the status bar code will update the LED when
        * it runs */
        .uithread = false
     },
     },
 
     UI_ACTION_MAP_TERMINATOR   /* macro used to terminate a list of actions */
     UI_ACTION_MAP_TERMINATOR
};
};
</syntaxhighlight>


Here we initialize the UI actions system, set a dispatch handler and register the action handlers listed above:
<syntaxhighlight lang="c" line>
/*
* Helper that pushes the handler on the UI thread (details will follow)
*/
static gboolean ui_action_dispatch_impl(gpointer data)
{
    void (*handler)(void) = data;
    handler();
    return FALSE;
}


/*
/* Register the action: */
* This is the action dispatch function, whenever ui_action_trigger() is called this
ui_actions_register(some_actions);</syntaxhighlight>
* function is called in response.
Here we have a single action handler to toggle warp mode, in this case it does not use its ''self'' argument, but the argument can be used to access, for example, the action’s<code>.id</code> member or the <code>.data</code> member to be able to write an action handler that can be used for multiple IDs.
*/
static void ui_action_dispatch(const ui_action_map_t *map)
{
    if (mainlock_is_vice_thread()) {
        /* we're on the main thread, push to UI thread */
        gdk_threads_add_timeout(0, ui_action_dispatch_impl, (gpointer)(map->handler));
    } else {
        /* we're already on the UI thread */
        map->handler();
    }
}


/* Initialize UI actions */
We register the action with a call to <code>ui_actions_register()</code>, after which VICE will call <code>warp_mode_toggle_action()</code> when a menu item, hotkey or controller button is used that is mapped to <code>ACTION_WARP_MODE_TOGGLE</code>.
ui_actions_init();


/* Set dispatch function */
<span id="functions-dealing-with-individual-actions"></span>
ui_actions_set_dispatch(ui_action_dispatch);
=== Functions dealing with individual actions: ===


/* Register some actions */
<syntaxhighlight lang="c">void        ui_action_trigger        (int action);
ui_actions_register(machine_actions);
void        ui_action_finish          (int action);
</syntaxhighlight>
int        ui_action_get_id          (const char *name);
const char *ui_action_get_name        (int action);
const char *ui_action_get_desc        (int action);
char *      ui_action_get_hotkey_label(int action);</syntaxhighlight>

Latest revision as of 08:20, 2 October 2023

UI Actions

Introduction

The UI actions are a way to trigger specific emulators actions from menu items, hotkeys or controller buttons without code duplication and letting VICE deal with issues like threading and avoiding showing multiple dialogs or retriggering actions that haven’t finished yet.

UI actions are managed through an API living in src/arch/shared/uiactions.h. Actions are triggered by specifying action ID in menu items, or by using an action name in hotkey and joy map files (which VICE translates to IDs).

The file src/arch/shared/uiactions.h contains the UI action IDs available, while the file src/arch/shared/uiactions.c contains a table mapping these IDs to action names.

New actions can be created by adding a unqiue ID in the header file and an entry (with a unique name) in the aformentioned table in the source file. An action handler is required to implement the action’s logic, this handler is a callback function that gets passed some information about itself (the entry in the table, an ui_action_map_t*). The action will need to be registered with the UI actions system via ui_actions_register(). (See below)

Basic API usage

The basic functions and types used by the UI actions are relatively simple. A selection of the most used functions and types is listed here.

General functions dealing with the UI actions system itself:

void  ui_actions_init           (void);
void  ui_actions_set_dispatch   (void (*dispatch)(ui_action_map_t *));
void  ui_actions_register       (const ui_action_map_t *mappings);
void  ui_actions_shutdown       (void);

VICE will call ui_actions_init() and ui_actions_shutdown(), and calling ui_actions_set_dispatch() is optional. So the only function required to use is ui_actions_register().

The function ui_actions_set_dispatch() can be used to install a function that “intercepts” an action before it’s triggered by VICE, in which case the dispatch function is responsible for calling ui_action_trigger(map->action). The dispatch function is used in the Gtk3 port to make sure an action that is marked as requiring the UI thread is executed on the UI thread. More on this later.

The ui_action_map_t type is used to register actions and to access information on an action. It maps action IDs to actions handlers and hotkeys.

typedef struct ui_action_map_s {
    int    action;          /**< action ID */

    /*
     * Action handler data
     */

    void (*handler)(struct ui_action_map_s*); /**< function handling the action */
    void  *data;            /**< optional user data */

    /* modes */
    bool   blocks;          /**< action blocks (the same action cannot be
                                 triggered again until it finishes) */
    bool   dialog;          /**< action pops up a dialog (only one dialog action
                                 is allowed at a time), this implies using the
                                 UI thread */
    bool   uithread;        /**< must run on the UI thread */

    /* state */
    bool   is_busy;         /**< action is busy */

    /* Hotkey data left out */
} ui_action_map_t;

The “Hotkey data” can be ignored: UI actions and hotkeys are closely related, and VICE uses the same struct to store hotkey data.

In its simplest form an initialization of the map requires providing values for .action and .handler. When providing initializers for UI action maps please use C99 designated initializers: this looks a lot cleaner and allows for adding members to the UI action map type without causing compiler warnings with existing code.

Simple example of an action handler

/* Action handler for toggling warp mode */
static void warp_mode_toggle_action(ui_action_map_t *self)
{
    vsync_set_warp_mode(!vsync_get_warp_mode());
}

/* List of actions to register (just one here) */
static const ui_action_map_t some_actions[] = {
    {   .action  = ACTION_WARP_MODE_TOGGLE,
        .handler = warp_mode_toggle_action
    },
    UI_ACTION_MAP_TERMINATOR    /* macro used to terminate a list of actions */
};


/* Register the action: */
ui_actions_register(some_actions);

Here we have a single action handler to toggle warp mode, in this case it does not use its self argument, but the argument can be used to access, for example, the action’s.id member or the .data member to be able to write an action handler that can be used for multiple IDs.

We register the action with a call to ui_actions_register(), after which VICE will call warp_mode_toggle_action() when a menu item, hotkey or controller button is used that is mapped to ACTION_WARP_MODE_TOGGLE.

Functions dealing with individual actions:

void        ui_action_trigger         (int action);
void        ui_action_finish          (int action);
int         ui_action_get_id          (const char *name);
const char *ui_action_get_name        (int action);
const char *ui_action_get_desc        (int action);
char *      ui_action_get_hotkey_label(int action);