Skip to content

Commit

Permalink
Add dynamic key binding
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Meyer <[email protected]>
  • Loading branch information
thibaultmeyer committed Jan 20, 2020
1 parent 2e1c1ab commit 9adb8c3
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 69 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ ADD_EXECUTABLE(
src/gui/gui_settings_callback_colorbutton_background.c
src/gui/gui_settings_callback_colorbutton_foreground.c
src/gui/gui_settings_callback_combobox_cpu_frequency.c
src/gui/gui_settings_callback_input_key.c
src/gui/gui_settings_callback_window_destroy.c
src/gui/gui_settings_init.c
src/gui/gui_settings_init_color_background.c
src/gui/gui_settings_init_color_foreground.c
src/gui/gui_settings_init_cpu_frequency.c
src/gui/gui_settings_init_display_mode.c
src/gui/gui_settings_init_key_binding.c
src/gui/gui_toolbox_get_settings_filename.c
src/gui/gui_toolbox_is_gtk_dark_theme_enabled
src/gui/gui_toolbox_load_settings_from_file_or_default.c
Expand Down
52 changes: 34 additions & 18 deletions src/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct s_gui_settings {
uint32_t display_mode;
GdkRGBA color_background;
GdkRGBA color_foreground;
guint keybinding[CHIP8_KEYBOARD_MAX_KEY];
} s_gui_settings;

typedef struct s_gui_settings_combobox_value {
Expand Down Expand Up @@ -66,15 +67,15 @@ GdkPixbuf *gui_image_load_from_memory_scale(const unsigned char *data,
* Callback. Configure main window when activated. Only call once.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_app_activate(GtkApplication *app, gpointer user_data);

/**
* Callback. Update counter.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
gboolean gui_main_callback_chip8_tick_counter();

Expand All @@ -90,7 +91,7 @@ gboolean gui_main_callback_chip8_tick_cpu();
*
* @param app Widget instance
* @param allocation Allocation information
* @param user_data custom user data
* @param user_data Custom user data
*/
gboolean gui_main_callback_drawing_area_draw(GtkWidget *widget, cairo_t *cr, void *data);

Expand All @@ -99,55 +100,55 @@ gboolean gui_main_callback_drawing_area_draw(GtkWidget *widget, cairo_t *cr, voi
*
* @param app Widget instance
* @param allocation Allocation information
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_drawing_area_new_size(GtkWidget *widget, GtkAllocation *allocation, void *data);

/**
* Callback. Load a ROM.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_header_bar_load_rom(GtkApplication *app, gpointer user_data);

/**
* Callback. Reset chip8 CPU.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_header_bar_reset_cpu(GtkApplication *app, gpointer user_data);

/**
* Callback. Show "about" dialog.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_menu_more_about(GtkApplication *app, gpointer user_data);

/**
* Callback. Show "settings" dialog.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_menu_more_settings(GtkApplication *app, gpointer user_data);

/**
* Callback. Main window is destroyed.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_main_callback_window_destroy(GtkWidget *widget, gpointer data);

/**
* Callback. A key has been pressed or released.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
gboolean gui_main_callback_window_key(GtkWidget *widget, GdkEventKey *event, gpointer data);

Expand All @@ -165,31 +166,39 @@ void gui_main_initialize_main_window(GtkApplication *app);
* Settings - Callback - Set the background color.
*
* @param widget Handle to the GTK color button
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_settings_callback_colorbutton_background(GtkColorButton *widget, gpointer user_data);

/**
* Settings - Callback - Set the foreground color.
*
* @param widget Handle to the GTK color button
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_settings_callback_colorbutton_foreground(GtkColorButton *widget, gpointer user_data);

/**
* Settings - Callback - Set the CPU frequency.
*
* @param widget Handle to the combobox widget
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_settings_callback_combobox_cpu_frequency(GtkComboBox *widget, gpointer user_data);

/**
* Settings - Callback - Set binding for a specific key.
*
* @param widget Handle to the GTK entry
* @param key_idx Index of the key to set
*/
void gui_settings_callback_input_key(GtkEntry *entry, uint32_t key_idx);

/**
* Callback. Settings window is destroyed.
*
* @param app GTK application instance
* @param user_data custom user data
* @param user_data Custom user data
*/
void gui_settings_callback_window_destroy(GtkWidget *widget, gpointer data);

Expand All @@ -199,33 +208,40 @@ void gui_settings_callback_window_destroy(GtkWidget *widget, gpointer data);
void gui_settings_init(void);

/**
* Initialize the settings window - Background Color
* Initialize the settings window - Background Color.
*
* @param fixed_container Container to place components
*/
void gui_settings_init_color_background(GtkFixed *fixed_container);

/**
* Initialize the settings window - Foreground Color
* Initialize the settings window - Foreground Color.
*
* @param fixed_container Container to place components
*/
void gui_settings_init_color_foreground(GtkFixed *fixed_container);

/**
* Initialize the settings window - CPU Frequency
* Initialize the settings window - CPU Frequency.
*
* @param fixed_container Container to place components
*/
void gui_settings_init_cpu_frequency(GtkFixed *fixed_container);

/**
* Initialize the settings window - Display Mode
* Initialize the settings window - Display Mode.
*
* @param fixed_container Container to place components
*/
void gui_settings_init_display_mode(GtkFixed *fixed_container);

/**
* Initialize the settings window - Key Binding.
*
* @param fixed_container Container to place components
*/
void gui_settings_init_key_binding(GtkFixed *fixed_container);

/**
* Retrieves the settings filename.
*
Expand Down
56 changes: 5 additions & 51 deletions src/gui/gui_main_callback_window_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,11 @@
gboolean gui_main_callback_window_key(GtkWidget *widget, GdkEventKey *event, gpointer data) {
if (gl_gui_components.chip8_cpu) {
uint8_t value = event->type == GDK_KEY_PRESS ? 1 : 0;
switch (event->keyval) {
case GDK_KEY_ampersand:
gl_gui_components.chip8_cpu->keyboard[1] = value;
break;
case GDK_KEY_eacute:
gl_gui_components.chip8_cpu->keyboard[2] = value;
break;
case GDK_KEY_quotedbl:
gl_gui_components.chip8_cpu->keyboard[3] = value;
break;
case GDK_KEY_apostrophe:
gl_gui_components.chip8_cpu->keyboard[12] = value;
break;
case GDK_KEY_a:
gl_gui_components.chip8_cpu->keyboard[4] = value;
break;
case GDK_KEY_z:
gl_gui_components.chip8_cpu->keyboard[5] = value;
break;
case GDK_KEY_e:
gl_gui_components.chip8_cpu->keyboard[6] = value;
break;
case GDK_KEY_r:
gl_gui_components.chip8_cpu->keyboard[13] = value;
break;
case GDK_KEY_q:
gl_gui_components.chip8_cpu->keyboard[7] = value;
break;
case GDK_KEY_s:
gl_gui_components.chip8_cpu->keyboard[8] = value;
break;
case GDK_KEY_d:
gl_gui_components.chip8_cpu->keyboard[9] = value;
break;
case GDK_KEY_f:
gl_gui_components.chip8_cpu->keyboard[14] = value;
break;
case GDK_KEY_w:
gl_gui_components.chip8_cpu->keyboard[10] = value;
break;
case GDK_KEY_x:
gl_gui_components.chip8_cpu->keyboard[0] = value;
break;
case GDK_KEY_c:
gl_gui_components.chip8_cpu->keyboard[11] = value;
break;
case GDK_KEY_v:
gl_gui_components.chip8_cpu->keyboard[15] = value;
break;
default:
break;

for (uint8_t idx = 0; idx < CHIP8_KEYBOARD_MAX_KEY; ++idx) {
if (gl_gui_settings.keybinding[idx] == event->keyval) {
gl_gui_components.chip8_cpu->keyboard[idx] = value;
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/gui/gui_settings_callback_input_key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "gui.h"

void gui_settings_callback_input_key(GtkEntry *entry, uint32_t key_idx) {
const gchar *data = gtk_entry_get_text(entry);

if (data[0] != 0) {
// Set new value
gl_gui_settings.keybinding[key_idx] = gdk_unicode_to_keyval(data[0]);

// Persists settings
gui_toolbox_save_settings_to_file();
}
}
1 change: 1 addition & 0 deletions src/gui/gui_settings_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void gui_settings_init() {
gui_settings_init_color_foreground(GTK_FIXED(fixed));
gui_settings_init_cpu_frequency(GTK_FIXED(fixed));
gui_settings_init_display_mode(GTK_FIXED(fixed));
gui_settings_init_key_binding(GTK_FIXED(fixed));

// Mark some widget/object to be explicitly deleted when window will be destroyed
GSList *components = g_slist_alloc();
Expand Down
Loading

0 comments on commit 9adb8c3

Please sign in to comment.