User:Compyx: Difference between revisions

From vice-emu
Jump to navigation Jump to search
 
(52 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Who am I =
= Personal notes, observations and rants =


I'm an old C64 scener, look me up at csdk.dk if you don't believe me ;)
== SDL UI code ==


= Personal devlog =
Since I'm tasked with updating the SDL code to use the generic UI actions and hotkeys code and use those UI actions from the menu items, hotkeys and custom joystick mappings, and the code is poorly documented, I'll be writing down some observations on how the SDL UI code works here, and some notes on how to move the code to using the shared UI actions and hotkeys code.


=== Menu items ===


== TODO list ==
==== Current API ====


* 2016-09-xx: <s>Try to build VICE on Minix 3.2.x</s> (2016-10-05: Minix' own ports systems fails miserably building subversion)
Menu items are defined with the type <b><tt>ui_menu_entry_t</tt></b> (in <tt>uimenu.h</tt>):
* 2016-09-29: Test Android SDL2 build
<syntaxhighlight lang="C">
* 2016-09-29: <s>Fix build system for *BSD ports</s> (2016-10-03: Done for FreeBSD, NetBSD, DragonflyBSD)
typedef struct ui_menu_entry_s {
* 2016-10-02: <s>Gtk2 crashes NetBSD VirtualBox VM, why? Logging doesn't help, empty log</s>
    char *string;
* 2016-10-08: Clean up resource/cmdline code, using some kind of memory management/free list.
    ui_menu_entry_type_t type;
* 2016-10-09: Clean up c1541 and update man page.
    ui_callback_t callback;
    ui_callback_data_t data;
    ui_menu_status_type_t status;
} ui_menu_entry_t;
</syntaxhighlight>


== BSD build system adventures ==
===== String =====


The <i><tt>string</tt></i> is used to display the item text in the menus. It is used by <tt>sdl_ui_display_item()</tt> to render a menu item.


=== FreeBSD/PC-BSD ===
===== Type =====


Version(s) tested: PC-BSD 10.3 amd64 (PC-BSD which is a preconfigured X11 install of FreeBSD)
The <i><tt>type</tt></i> determines what to do when the user activates the item, types include radio buttons, toggle buttons, submenus, and dialogs.
The various menu item types are defined as:
<syntaxhighlight lang="C">
typedef enum {
    /* Text item (no operation): if data == 1 text colors are inverted */
    MENU_ENTRY_TEXT = 0,


Use gmake when building VICE! Use normal make when installing from the ports system.
    /* Resource toggle: no UI needed, callback is used */
    MENU_ENTRY_RESOURCE_TOGGLE,


    /* Resource radio: no UI needed, callback is used, data is the resource value */
    MENU_ENTRY_RESOURCE_RADIO,


===== Ports =====
    /* Resource int: needs UI, callback is used */
    MENU_ENTRY_RESOURCE_INT,


* Xaw: builds and runs
    /* Resource string: needs UI, callback is used */
* Gtk2: builds and runs
    MENU_ENTRY_RESOURCE_STRING,
* Gtk3: builds and runs
* SDL1: builds and runs
* SDL2: builds and runs (very well even, best performance of the ports I've tested, most likely thanks to VirtualBox using HW rendering on my host)


(Tested in VirtualBox VM with guest additions).
    /* Submenu: needs UI, data points to the submenu */
Still need to do a write-up on which libraries should be installed for building and for each specific port.
    MENU_ENTRY_SUBMENU,


=== NetBSD ===
    /* Dynamic submenu: needs UI, data points to the submenu, hotkeys disabled */
    MENU_ENTRY_DYNAMIC_SUBMENU,


Version(s) tested: 7.0.1 amd64 (on VirtualBox 5.1.6)
    /* Custom dialog: needs UI */
    MENU_ENTRY_DIALOG,


Use gmake!
    /* Other: no UI needed */
    MENU_ENTRY_OTHER,


Some notes/observations:
    /* Other: no UI needed */
* It has few weird conflicts with library interdependencies: a lot of foo.so.x needed but foo.so.y found, etc. May need to test the pkgsrc system, see if that behave any better, and perhaps run the system on bare metal.
    MENU_ENTRY_OTHER_TOGGLE
* /dev/joy0 and /dev/joy1 can't be opened, <machine/joystick.h> is there and compiling against it works, same goes for USB joysticks, after installing libusb, usbhidapi and some others, configure sees the USB joystick support, but it doesn't work. Most likely a VirtualBox problem.
} ui_menu_entry_type_t;
* HW rendering failed to build (failed at libdrm) while installing MesaLib7 through pkgsrc. So some ports had horrible performance.
</syntaxhighlight>
* Sounds seems to work, but VBox doesn't output anything, most likely again a VBox issue, there are no guest-additions for NetBSD, there is an effort started in 2013 to port FreeBSD's guest additions to NetBSD, but that was abandoned.


All issues are related to properly configuring NetBSD, not something for VICE to worry about.
===== Callback and Data =====


===== Ports =====
When an item is activated the <i><tt>callback</tt></i> is called with <i><tt>data</i></tt> as its argument. The return value of the <i><tt>callback</i></tt> (a <tt>const char*</tt>) is used to update the item's state or to exit the UI (more on that later).


* Xaw: builds and runs
Callbacks are prototyped in <tt>uimenu.h</tt>:
* Gtk2: builds and runs
<syntaxhighlight lang="C">typedef const char *(*ui_callback_t)(int activated, ui_callback_data_t param);</syntaxhighlight>
* Gtk3: builds, starts, but hangs on initializing the Gtk3 widget for displaying the actual screen ("Gtk-WARNING **: Allocating size to GtkWindow 0x7f7ff493cd20 without calling gtk_widget_get_preferred_width/height(). How does the code know the size to allocate?")
* SDL1: builds and runs, no HW acceleration yet, needs Mesa, which fails to build
* SDL2: builds and 'runs' (5fps, most likely due to Gallium using software rendering)


===== In-tree libraries =====
These callbacks are triggered both by displaying an item (<tt>activated == 0</tt>) and by activating an item (<tt>activated == 1</tt>). The return value is used to either display something in front of the item, like <tt>sdl_menu_text_tick</tt> (a tick mark), nothing (<tt>NULL</tt>) or the special <tt>sdl_menu_text_exit_ui</tt> ("\1") which means the activation succeeded and the UI must be exited (for example when triggering soft/hard reset). The <i><tt>param</tt></i> argument is the <tt>data</tt> field of the menu item definition.


--enable-static-ffmpeg fails when building src/lib/libx264 => configure script needs bash, standard shell fails.
Some helper macros and functions exit for the callbacks in <tt>menu_common.{c,h}</tt>.
<syntaxhighlight lang="C">
#define UI_MENU_CALLBACK(name) \
    const char *name(int activated, ui_callback_data_t param)


=== OpenBSD ===
#define UI_MENU_DEFINE_TOGGLE(resource)                              \
    static UI_MENU_CALLBACK(toggle_##resource##_callback)            \
    {                                                                \
        return sdl_ui_menu_toggle_helper(activated, #resource);      \
    }


Version(s) tested: 6.0 amd64
#define UI_MENU_DEFINE_RADIO(resource)                               \
    static UI_MENU_CALLBACK(radio_##resource##_callback)              \
    {                                                                \
        return sdl_ui_menu_radio_helper(activated, param, #resource); \
    }


<s>Can't get the bloody X server to behave :) (Again in VBox VM)</s>
#define UI_MENU_DEFINE_STRING(resource)                                \
    static UI_MENU_CALLBACK(string_##resource##_callback)             \
    {                                                                  \
        return sdl_ui_menu_string_helper(activated, param, #resource); \
    }


OpenBSD doesn't provide guest additions for VirtualBox, so for me sound output failed.
#define UI_MENU_DEFINE_INT(resource)                                \
    static UI_MENU_CALLBACK(int_##resource##_callback)              \
    {                                                              \
        return sdl_ui_menu_int_helper(activated, param, #resource); \
    }


==== Ports ====
#define UI_MENU_DEFINE_FILE_STRING(resource)                                \
    static UI_MENU_CALLBACK(file_string_##resource##_callback)              \
    {                                                                      \
        return sdl_ui_menu_file_string_helper(activated, param, #resource); \
    }


* Xaw: builds and runs fine
#define UI_MENU_DEFINE_SLIDER(resource, min, max)                                  \
* Gtk2:
    static UI_MENU_CALLBACK(slider_##resource##_callback)                          \
    {                                                                              \
        return sdl_ui_menu_slider_helper(activated, param, #resource, min, max);  \
    }
</syntaxhighlight>


=== Dragonfly BSD ===
These are used with a resource name as their first argument and expand to the beginning of a function definition of a callback. For example:
<syntaxhighlight lang="C">
UI_MENU_DEFINE_RADIO(VICIIBorderMode)


Version(s) tested: 4.6 amd64 (inside a VirtualBox 5.1.6 VM, configured as FreeBSD)
static const ui_menu_entry_t vicii_border_menu[] = {
    { "Normal",
      MENU_ENTRY_RESOURCE_RADIO,
      radio_VICIIBorderMode_callback,
      (ui_callback_data_t)VICII_NORMAL_BORDERS },
    { "Full",
      MENU_ENTRY_RESOURCE_RADIO,
      radio_VICIIBorderMode_callback,
      (ui_callback_data_t)VICII_FULL_BORDERS },
    /* Debug and None snipped */
    SDL_MENU_LIST_END
};
</syntaxhighlight>


Use gmake when building VICE.
The macro at the top expands to:
<syntaxhighlight lang="C">
static const char *radio_VICIIBorderMode_callback(int activated, ui_callback_data_t param)
{
    return sdl_ui_menu_radio_helper(activated, param, "VICIIBorderMode"); 
}
</syntaxhighlight>


Needed to add <b><tt>snd_driver_load="YES"</tt></b> to <tt>/boot/loader.conf</tt> to get access to dev/dsp for sound output.
The helper function is defined (in <tt>menu_common.c</tt>as:
<syntaxhighlight lang="C">
const char *sdl_ui_menu_radio_helper(int activated, ui_callback_data_t param, const char *resource_name)
{
    if (activated) {
        if (resources_query_type(resource_name) == RES_INTEGER) {
            resources_set_int(resource_name, vice_ptr_to_int(param));
        } else {
            resources_set_string(resource_name, (char *)param);
        }
    } else {
        int v;
        const char *w;
        if (resources_query_type(resource_name) == RES_INTEGER) {
            if (resources_get_int(resource_name, &v) == 0) {
                if (v == vice_ptr_to_int(param)) {
                    return sdl_menu_text_tick;
                }
            }
        } else {
            if (resources_get_string(resource_name, &w) == 0) {
                if (!strcmp(w, (char *)param)) {
                    return sdl_menu_text_tick;
                }
            }
        }
    }
    return NULL;
}
</syntaxhighlight>


==== Ports ====
So if activated the resource is set to <tt>param</tt> and <tt>NULL</tt> is returned: The <i><tt>callback</tt></i> and its <i><tt>data</i></tt> are used in <tt>sdl_ui_menu_item_activate()</tt> as follows:
<syntaxhighlight lang="C">
const char *p;


All issues related to VICE hanging on sound buffer problems most likely have to do with me having configured ALSA without really knowing what the hell I was doing. Last time I had to configure ALSA has been over 10 years ago on Linux ;) Using Intel HD audio in VirtualBox at least removes the
p = item->callback(1, item->data);
crashes in the various ports, but buffer overflows are common, but I'm using a VM with a single CPU @ 2GHz and 1GB memory.
if (p == sdl_menu_text_exit_ui) {
    return MENU_RETVAL_EXIT_UI;
}
</syntaxhighlight>


* Xaw: builds and runs
If not activated (when rendering the item's text), <tt>sdl_menu_text_tick</tt> is returned if the <tt>param</tt> matches the current resource value, otherwise <tt>NULL</tt> is returned. The <i><tt>callback</tt></i> and its <i><tt>data</i></tt> are used in <tt>sdl_ui_display_item()</tt> as follows:
* Gtk2: builds and runs, but any selection in the GUI that needs to pop up a dialog hangs VICE, unless you disable sound and then re-enable sound.
<syntaxhighlight lang="C">
* Gtk3: builds and runs, same issues as the Gtk2 port, selecting an item in the GUI causes ALSA to overflow the sound buffer, and VICE hangs
int istoggle = 0, status = 0;
* SDL1: builds and runs
char string[3] = {0x20, 0x20, 0};
* SDL2: builds and runs


istoggle = (item->type == MENU_ENTRY_RESOURCE_TOGGLE) ||
          (item->type == MENU_ENTRY_RESOURCE_RADIO) ||
          (item->type == MENU_ENTRY_OTHER_TOGGLE);


== Minix build system ==
itemdata = item->callback(0, item->data);


Minix 3.3.0 doesn't provide X11 out of the box (eg binary packages), so I'll start with 3.2.1. I have no experience with Minx, except for installing it once about ten years ago, and deciding it sucked.
if ((itemdata != NULL) && !strcmp(itemdata, MENU_NOT_AVAILABLE_STRING)) {
    /* menu is not available */
    status = 2;
} else {
    /* print tick-mark for toggles and radio buttons at the start of the line */
    if (istoggle) {
        status = (itemdata == NULL) ? 0 : 1;
        string[0] = (itemdata == NULL) ? MENU_CHECKMARK_UNCHECKED_CHAR : itemdata[0];
    }
}
</syntaxhighlight>


=== Minix 3.2.1 ===
In the above example when clicking on "Full" the integer resource "VICIIBorderMode" is set to the <i></tt>data</tt></i> field's value <tt>VICII_FULL_BORDERS</tt> (<tt><b>(void*)1</b></tt>). When rendering the menu the callback for "Full" will return <tt>sdl_menu_text_tick</tt> and a tick mark (green filled circle) is rendered in front of the item and the other menu items' callbacks return <tt>NULL</tt> since their <i><tt>data</tt></i> doesn't match the value of "VICIIBorderMode"; which the UI code then renders as an empty red circle (<tt>MENU_CHECKMARK_UNCHECKED_CHAR</tt>).


Useless. Subversion must be built from source through pksrc, but building fails with circular dependencies with libtool. GCC is 2.95(!), which is ancient.
===== Status =====


=== Minix 3.3.0 ===
The <tt><i>status</tt></i> field is used for radio buttons to display the selection's state.
<syntaxhighlight lang="C">
typedef enum {
    MENU_STATUS_ACTIVE = 0,
    MENU_STATUS_INACTIVE = 1,
    MENU_STATUS_NA = 2
} ui_menu_status_type_t;
</syntaxhighlight>


Perhaps I'll look a this when I have absolutely nothing better to do, or am very drunk.
==== Moving to UI Actions ====


== Preparing for removing Gtk2 in favour of Gtk3 ==
Work is done on moving to using UI actions in the [https://sourceforge.net/p/vice-emu/code/HEAD/tree/branches/compyx/sdl-uiactions/ compyx/sdl-uiactions] branch. Currently UI actions for menu items work and are being added.


Since most modern operating systems used for VICE have support for Gtk3 and Gtk2 is no longer maintained, it makes sense to prepare VICE for using Gtk3 in stead of Gtk2. This means, for now, using a gtk2legacy wrapper module Groepaz started. This provided wrappers around Gtk2 code using the Gtk3 API.
===== Initialization of UI actions =====


The idea is to use the Gtk3 API in all Gtk/Gnome code while providing wrappers in gtk2legacy that translate those Gtk3 API calls into equivalent Gtk2 API calls. Once the Gtk2 API has finally been deprecated, we can simply remove gtk2legacy and use pure Gtk3.
# <tt>ui_actions_init()</tt>, <tt>ui_hotkeys_resource_init()</tt> and <tt>ui_hotkeys_cmdline_init()</tt> are called in <tt>main_program()</tt> (in <tt>src/main.c</tt>), setting up the UI actions and registering hotkeys resources and command line options
# <tt>ui_hotkeys_init()</tt> is called in <tt>kbd_arch_init()</tt> (in <tt>src/arch/sdl/kbd.c</tt>), initializing the new hotkeys system, but not used further currently
# <tt>hotkeys_iterate_menu()</tt> is called in <tt>ui_init_finalize()</tt> to store pointers to menu items that have UI action IDs so the action handlers can use the callbacks (see below).
# The UI action handlers are registered in <tt>ui_init_finalize()</tt>


The minimum Gtk3 version supported should be 3.16, since that introduces GtkGlArea, which should replace GtkGlExt.
===== Extending the menu code =====
Fields are added to the <tt><b>ui_menu_entry_t</b></tt> struct:
<syntaxhighlight lang="C">
/* extra members for the UI actions */
int        action;      /**< UI action ID */
const char *activated;    /**< text to return when the item is activated */
</syntaxhighlight>


The <tt>sdl_ui_menu_item_activate()</tt> function has been updated to check the <tt><i>action</i></tt> field, and if &gt; 0, call <tt>ui_action_trigger()</tt> and return the <tt><i>activated</i></tt> field to the UI code so the UI can respond accordingly (keep going, show toggle/radio indicator, or exit UI).


{| class="wikitable sortable"
The current menu callbacks are left in place for the current hotkeys/joymapping implementation. Some of these callbacks can be removed once the UI actions are also supported for the hotkeys/joymappings, others need to remain (perhaps in a slightly altered form) since they contain the implementation of dialogs such as file choosers.
|+ GTk3 support for various operating systems
! scope="col" | OS
! scope="col" | distro/version
! scope="col" | arch
! scope="col" | Gtk2 version
! scope="col" | Gtk3 version
! scope="col" | date checked
! scope="col" | notes
|-
| Linux || Debian Jessie 8.6 || amd64 || 2.24.25 || 3.14.5 || 2016-10-09 ||
|-
| Linux || Fedora 24 || amd64 || 2.24.31 || 3.20.9 || 2016-10-05 ||
|-
| Linux || Mint 17.3 || amd64 || 2.24.3 || 3.10.18 || 2016-10-05 ||
|-
| Linux || Mint 18.0 || amd64 || 2.24.30 || 3.18.9 || 2016-10-05 ||
|-
| BSD || FreeBSD 10.3 || amd64 || 2.24.29 || 3.18.8 || 2016-10-05 || used the ports collection
|-
| BSD || FreeBSD 11.0 || amd64 || 2.24.29 || 3.18.8 || 2016-10-12 ||
|-
| BSD || NetBSD 7.0.1 || amd64 ||| 2.24.30 || 3.20.6 || 2016-10-05 || used binary (pkgin) packages
|-
| BSD || OpenBSD 6.0 || amd64 ||
|-
| BSD ||DragonflyBSD || amd64 ||
|-
| Minix || 3.2.1 || i386 || n/a || n/a || 2016-10-05 || the whole Minix build system is broken
|}


In order to be able to show such a dialog and pass the correct data back to the UI code the function <tt>sdl_ui_menu_item_activate_by_action(int action)</tt> has been added. It checks whether the UI is currently active, and if so, calls sdl_ui_menu_item_activate(), otherwise it sets a CPU trap to activate the UI and trigger the menu item's callback.


=== Porting Gtk2 container types to Gtk3 container types ===
Current (2023-06-22) implementation with a lot of debugging noise:
<syntaxhighlight lang="C">
void sdl_ui_menu_item_activate_by_action(int action)
{
    ui_action_map_t *map = ui_action_map_get(action);
    printf("%s(): map = %p\n", __func__, (const void *)map);
    if (map != NULL) {
        ui_menu_entry_t *item = map->menu_item[0];
        printf("%s(): item = %p\n", __func__, (const void *)item);
        if (item != NULL) {
            if (sdl_menu_state) {
                /* menu is already active */
                printf("%s(): menu is already active, activating item\n", __func__);
                /* we can call sdl_ui_menu_item_activate() because that would trigger
                * the action again */
                if (item->callback != NULL) {
                    item->callback(1 /*activated*/, item->data);
                } else {
                    fprintf(stderr, "%s(): error: no callback to trigger!\n", __func__);
                }
            } else {
                printf("%s(): menu is not active, calling interrupt_maincpu_trigger_trap()\n", __func__);
                interrupt_maincpu_trigger_trap(sdl_ui_trap, item);
            }
        }
    }
}
</syntaxhighlight>


The current advise of Gtk3 is to use GtkGrid for any kind of layout container. Even GtkBox will someday be deprecated in favour of GtkGrid. For now, I'll use GtkBox untill the Gtk2 port has been completely removed, then we can tackle this.
===== TODO =====


==== GtkHBox and GtkVBox ====
* <s>Handling of <tt>MENU_ENTRY_RESOURCE_RADIO</tt> items with string resources, currently only integer resources are handled in the actions code for radio items.</s>
* <s>Handling of <tt>MENU_ENTRY_RESOURCE_INT<tt> items.</s>
* <s>Handling of <tt>MENU_ENTRY_RESOURCE_STRING<tt> items.</s>


GtkHBox and GtkVBox have been deprecated in favour of GtkBox.
=== Hotkeys ===


I wrote a wrapper in gtk2legacy.c so Gtk2 code can use GtkBox while using the underlying GtkHBox/GtkVBox widgets. Naturally these calls aren't fully compatible, gtk_hbox_new() and gtk_vbox_new() use a `homogeneous` argument which determines if all widgets should be the same size. Since all VICE code use FALSE for this, gtk_box_new() works without having to set the homogeneous property afterwards.
=== Joystick mappings ===
 
==== GtkTable ====
 
GtkTable has been deprecated in favour of GtkGrid, porting this should be too much hassle, but lets see.

Latest revision as of 18:49, 19 July 2023

Personal notes, observations and rants

SDL UI code

Since I'm tasked with updating the SDL code to use the generic UI actions and hotkeys code and use those UI actions from the menu items, hotkeys and custom joystick mappings, and the code is poorly documented, I'll be writing down some observations on how the SDL UI code works here, and some notes on how to move the code to using the shared UI actions and hotkeys code.

Menu items

Current API

Menu items are defined with the type ui_menu_entry_t (in uimenu.h):

typedef struct ui_menu_entry_s {
    char *string;
    ui_menu_entry_type_t type;
    ui_callback_t callback;
    ui_callback_data_t data;
    ui_menu_status_type_t status;
} ui_menu_entry_t;
String

The string is used to display the item text in the menus. It is used by sdl_ui_display_item() to render a menu item.

Type

The type determines what to do when the user activates the item, types include radio buttons, toggle buttons, submenus, and dialogs. The various menu item types are defined as:

typedef enum {
    /* Text item (no operation): if data == 1 text colors are inverted */
    MENU_ENTRY_TEXT = 0,

    /* Resource toggle: no UI needed, callback is used */
    MENU_ENTRY_RESOURCE_TOGGLE,

    /* Resource radio: no UI needed, callback is used, data is the resource value */
    MENU_ENTRY_RESOURCE_RADIO,

    /* Resource int: needs UI, callback is used */
    MENU_ENTRY_RESOURCE_INT,

    /* Resource string: needs UI, callback is used */
    MENU_ENTRY_RESOURCE_STRING,

    /* Submenu: needs UI, data points to the submenu */
    MENU_ENTRY_SUBMENU,

    /* Dynamic submenu: needs UI, data points to the submenu, hotkeys disabled */
    MENU_ENTRY_DYNAMIC_SUBMENU,

    /* Custom dialog: needs UI */
    MENU_ENTRY_DIALOG,

    /* Other: no UI needed */
    MENU_ENTRY_OTHER,

    /* Other: no UI needed */
    MENU_ENTRY_OTHER_TOGGLE
} ui_menu_entry_type_t;
Callback and Data

When an item is activated the callback is called with data as its argument. The return value of the callback (a const char*) is used to update the item's state or to exit the UI (more on that later).

Callbacks are prototyped in uimenu.h:

typedef const char *(*ui_callback_t)(int activated, ui_callback_data_t param);

These callbacks are triggered both by displaying an item (activated == 0) and by activating an item (activated == 1). The return value is used to either display something in front of the item, like sdl_menu_text_tick (a tick mark), nothing (NULL) or the special sdl_menu_text_exit_ui ("\1") which means the activation succeeded and the UI must be exited (for example when triggering soft/hard reset). The param argument is the data field of the menu item definition.

Some helper macros and functions exit for the callbacks in menu_common.{c,h}.

#define UI_MENU_CALLBACK(name) \
    const char *name(int activated, ui_callback_data_t param)

#define UI_MENU_DEFINE_TOGGLE(resource)                              \
    static UI_MENU_CALLBACK(toggle_##resource##_callback)            \
    {                                                                \
        return sdl_ui_menu_toggle_helper(activated, #resource);      \
    }

#define UI_MENU_DEFINE_RADIO(resource)                                \
    static UI_MENU_CALLBACK(radio_##resource##_callback)              \
    {                                                                 \
        return sdl_ui_menu_radio_helper(activated, param, #resource); \
    }

#define UI_MENU_DEFINE_STRING(resource)                                \
    static UI_MENU_CALLBACK(string_##resource##_callback)              \
    {                                                                  \
        return sdl_ui_menu_string_helper(activated, param, #resource); \
    }

#define UI_MENU_DEFINE_INT(resource)                                \
    static UI_MENU_CALLBACK(int_##resource##_callback)              \
    {                                                               \
        return sdl_ui_menu_int_helper(activated, param, #resource); \
    }

#define UI_MENU_DEFINE_FILE_STRING(resource)                                \
    static UI_MENU_CALLBACK(file_string_##resource##_callback)              \
    {                                                                       \
        return sdl_ui_menu_file_string_helper(activated, param, #resource); \
    }

#define UI_MENU_DEFINE_SLIDER(resource, min, max)                                  \
    static UI_MENU_CALLBACK(slider_##resource##_callback)                          \
    {                                                                              \
        return sdl_ui_menu_slider_helper(activated, param, #resource, min, max);   \
    }

These are used with a resource name as their first argument and expand to the beginning of a function definition of a callback. For example:

UI_MENU_DEFINE_RADIO(VICIIBorderMode)

static const ui_menu_entry_t vicii_border_menu[] = {
    { "Normal",
      MENU_ENTRY_RESOURCE_RADIO,
      radio_VICIIBorderMode_callback,
      (ui_callback_data_t)VICII_NORMAL_BORDERS },
    { "Full",
      MENU_ENTRY_RESOURCE_RADIO,
      radio_VICIIBorderMode_callback,
      (ui_callback_data_t)VICII_FULL_BORDERS },
    /* Debug and None snipped */
    SDL_MENU_LIST_END
};

The macro at the top expands to:

static const char *radio_VICIIBorderMode_callback(int activated, ui_callback_data_t param)
{
    return sdl_ui_menu_radio_helper(activated, param, "VICIIBorderMode");  
}

The helper function is defined (in menu_common.cas:

const char *sdl_ui_menu_radio_helper(int activated, ui_callback_data_t param, const char *resource_name)
{
    if (activated) {
        if (resources_query_type(resource_name) == RES_INTEGER) {
            resources_set_int(resource_name, vice_ptr_to_int(param));
        } else {
            resources_set_string(resource_name, (char *)param);
        }
    } else {
        int v;
        const char *w;
        if (resources_query_type(resource_name) == RES_INTEGER) {
            if (resources_get_int(resource_name, &v) == 0) {
                if (v == vice_ptr_to_int(param)) {
                    return sdl_menu_text_tick;
                }
            }
        } else {
            if (resources_get_string(resource_name, &w) == 0) {
                if (!strcmp(w, (char *)param)) {
                    return sdl_menu_text_tick;
                }
            }
        }
    }
    return NULL;
}

So if activated the resource is set to param and NULL is returned: The callback and its data are used in sdl_ui_menu_item_activate() as follows:

const char *p;

p = item->callback(1, item->data);
if (p == sdl_menu_text_exit_ui) {
    return MENU_RETVAL_EXIT_UI;
}
If not activated (when rendering the item's text), sdl_menu_text_tick is returned if the param matches the current resource value, otherwise NULL is returned. The callback and its data are used in sdl_ui_display_item() as follows:
int istoggle = 0, status = 0;
char string[3] = {0x20, 0x20, 0};

istoggle = (item->type == MENU_ENTRY_RESOURCE_TOGGLE) ||
           (item->type == MENU_ENTRY_RESOURCE_RADIO) ||
           (item->type == MENU_ENTRY_OTHER_TOGGLE);

itemdata = item->callback(0, item->data);

if ((itemdata != NULL) && !strcmp(itemdata, MENU_NOT_AVAILABLE_STRING)) {
    /* menu is not available */
    status = 2;
} else {
    /* print tick-mark for toggles and radio buttons at the start of the line */
    if (istoggle) {
        status = (itemdata == NULL) ? 0 : 1;
        string[0] = (itemdata == NULL) ? MENU_CHECKMARK_UNCHECKED_CHAR : itemdata[0];
    }
}

In the above example when clicking on "Full" the integer resource "VICIIBorderMode" is set to the data field's value VICII_FULL_BORDERS ((void*)1). When rendering the menu the callback for "Full" will return sdl_menu_text_tick and a tick mark (green filled circle) is rendered in front of the item and the other menu items' callbacks return NULL since their data doesn't match the value of "VICIIBorderMode"; which the UI code then renders as an empty red circle (MENU_CHECKMARK_UNCHECKED_CHAR).

Status

The status field is used for radio buttons to display the selection's state.

typedef enum {
    MENU_STATUS_ACTIVE = 0,
    MENU_STATUS_INACTIVE = 1,
    MENU_STATUS_NA = 2
} ui_menu_status_type_t;

Moving to UI Actions

Work is done on moving to using UI actions in the compyx/sdl-uiactions branch. Currently UI actions for menu items work and are being added.

Initialization of UI actions
  1. ui_actions_init(), ui_hotkeys_resource_init() and ui_hotkeys_cmdline_init() are called in main_program() (in src/main.c), setting up the UI actions and registering hotkeys resources and command line options
  2. ui_hotkeys_init() is called in kbd_arch_init() (in src/arch/sdl/kbd.c), initializing the new hotkeys system, but not used further currently
  3. hotkeys_iterate_menu() is called in ui_init_finalize() to store pointers to menu items that have UI action IDs so the action handlers can use the callbacks (see below).
  4. The UI action handlers are registered in ui_init_finalize()
Extending the menu code

Fields are added to the ui_menu_entry_t struct:

/* extra members for the UI actions */
int         action;       /**< UI action ID */
const char *activated;    /**< text to return when the item is activated */

The sdl_ui_menu_item_activate() function has been updated to check the action field, and if > 0, call ui_action_trigger() and return the activated field to the UI code so the UI can respond accordingly (keep going, show toggle/radio indicator, or exit UI).

The current menu callbacks are left in place for the current hotkeys/joymapping implementation. Some of these callbacks can be removed once the UI actions are also supported for the hotkeys/joymappings, others need to remain (perhaps in a slightly altered form) since they contain the implementation of dialogs such as file choosers.

In order to be able to show such a dialog and pass the correct data back to the UI code the function sdl_ui_menu_item_activate_by_action(int action) has been added. It checks whether the UI is currently active, and if so, calls sdl_ui_menu_item_activate(), otherwise it sets a CPU trap to activate the UI and trigger the menu item's callback.

Current (2023-06-22) implementation with a lot of debugging noise:

void sdl_ui_menu_item_activate_by_action(int action)
{
    ui_action_map_t *map = ui_action_map_get(action);
    printf("%s(): map = %p\n", __func__, (const void *)map);
    if (map != NULL) {
        ui_menu_entry_t *item = map->menu_item[0];
        printf("%s(): item = %p\n", __func__, (const void *)item);
        if (item != NULL) {
            if (sdl_menu_state) {
                /* menu is already active */
                printf("%s(): menu is already active, activating item\n", __func__);
                /* we can call sdl_ui_menu_item_activate() because that would trigger
                 * the action again */
                if (item->callback != NULL) {
                    item->callback(1 /*activated*/, item->data);
                } else {
                    fprintf(stderr, "%s(): error: no callback to trigger!\n", __func__);
                }
            } else {
                printf("%s(): menu is not active, calling interrupt_maincpu_trigger_trap()\n", __func__);
                interrupt_maincpu_trigger_trap(sdl_ui_trap, item);
            }
        }
    }
}
TODO
  • Handling of MENU_ENTRY_RESOURCE_RADIO items with string resources, currently only integer resources are handled in the actions code for radio items.
  • Handling of MENU_ENTRY_RESOURCE_INT items.
  • Handling of MENU_ENTRY_RESOURCE_STRING items.

Hotkeys

Joystick mappings