Blame hdajackretask/main-gtk.c

Packit 427e91
/* Copyright 2011 David Henningsson, Canonical Ltd.
Packit 427e91
   License: GPLv2+ 
Packit 427e91
*/
Packit 427e91
Packit 427e91
#include <string.h>
Packit 427e91
#include <stdlib.h>
Packit 427e91
#include <gtk gtk.h="">
Packit 427e91
#include <stdbool.h>
Packit 427e91
Packit 427e91
#include "sysfs-pin-configs.h"
Packit 427e91
#include "apply-changes.h"
Packit 427e91
Packit 427e91
typedef struct ui_data_t ui_data_t;
Packit 427e91
Packit 427e91
typedef struct pin_ui_data_t {
Packit 427e91
    pin_configs_t* pin_config;
Packit 427e91
    typical_pins_t pins_info[32];
Packit 427e91
    GtkWidget *frame, *override, *jacktype;
Packit 427e91
    GtkWidget* free_override_cb[FREE_OVERRIDES_COUNT];
Packit 427e91
    ui_data_t* owner;
Packit 427e91
} pin_ui_data_t;
Packit 427e91
Packit 427e91
typedef struct hints_ui_data_t {
Packit 427e91
    gboolean visible;
Packit 427e91
    GtkWidget *frame;
Packit 427e91
    GtkListStore *store;
Packit 427e91
    gchar *values;
Packit 427e91
} hints_ui_data_t;
Packit 427e91
Packit 427e91
struct ui_data_t {
Packit 427e91
    GList* pin_ui_data;
Packit 427e91
    GtkWidget *main_window;
Packit 427e91
    GtkWidget *content_scroll_widget;
Packit 427e91
    GtkWidget *content_inner_box;
Packit 427e91
    GtkWidget *codec_selection_combo;
Packit 427e91
Packit 427e91
    codec_name_t* current_codec;
Packit 427e91
    int sysfs_pincount;
Packit 427e91
    codec_name_t sysfs_codec_names[128];
Packit 427e91
    pin_configs_t sysfs_pins[32];
Packit 427e91
    gboolean free_overrides;
Packit 427e91
    gboolean trust_codec;
Packit 427e91
    gboolean trust_defcfg;
Packit 427e91
    gboolean model_auto;
Packit 427e91
Packit 427e91
    hints_ui_data_t hints;
Packit 427e91
};
Packit 427e91
Packit 427e91
static void update_user_pin_config(ui_data_t* ui, pin_configs_t* cfg);
Packit 427e91
Packit 427e91
static void update_override_sensitive(GtkWidget* sender, pin_ui_data_t* data)
Packit 427e91
{
Packit 427e91
    int i;
Packit 427e91
    gboolean checked = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sender));
Packit 427e91
    gtk_widget_set_sensitive(data->jacktype, checked); 
Packit 427e91
    for (i = 0; i < FREE_OVERRIDES_COUNT; i++)
Packit 427e91
        gtk_widget_set_sensitive(data->free_override_cb[i], checked);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void override_toggled(GtkWidget* sender, pin_ui_data_t* data)
Packit 427e91
{
Packit 427e91
    update_override_sensitive(sender, data);
Packit 427e91
    update_user_pin_config(data->owner, data->pin_config);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void jacktype_changed(GtkWidget* sender, pin_ui_data_t* data)
Packit 427e91
{
Packit 427e91
    update_user_pin_config(data->owner, data->pin_config);
Packit 427e91
}
Packit 427e91
Packit 427e91
static GtkWidget* create_pin_ui(ui_data_t* ui, pin_configs_t* pin_cfg)
Packit 427e91
{
Packit 427e91
    GtkWidget* result;
Packit 427e91
    GtkContainer* box;
Packit 427e91
    pin_ui_data_t* data;
Packit 427e91
Packit 427e91
    int port_conn = get_port_conn(pin_cfg->init_pin_config);
Packit 427e91
    /* Do not show unconnected pins */
Packit 427e91
    if (ui->trust_defcfg && port_conn == 1) 
Packit 427e91
        return NULL;
Packit 427e91
Packit 427e91
    data = calloc(1, sizeof(pin_ui_data_t));
Packit 427e91
    data->pin_config = pin_cfg;
Packit 427e91
    data->owner = ui;
Packit 427e91
Packit 427e91
    { /* Frame */
Packit 427e91
        gchar* d = get_config_description(pin_cfg->init_pin_config);
Packit 427e91
        gchar* c = g_strdup_printf("Pin ID: 0x%02x", pin_cfg->nid);
Packit 427e91
        GtkWidget* label = gtk_label_new(c);
Packit 427e91
        result = gtk_frame_new(d);
Packit 427e91
        data->frame = result;
Packit 427e91
        box = GTK_CONTAINER(gtk_box_new(GTK_ORIENTATION_VERTICAL, 2));
Packit 427e91
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
Packit 427e91
        gtk_container_add(box, label);
Packit 427e91
        g_free(d);
Packit 427e91
        g_free(c);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    { /* Capabilities 
Packit 427e91
        gchar* s = get_caps_description(pin_cfg->pin_caps);
Packit 427e91
        gchar* s2 = g_strdup_printf("Capabilities: %s", strlen(s) > 2 ? s+2 : ""); // Hack for initial comma
Packit 427e91
        GtkWidget* label = gtk_label_new(s2);
Packit 427e91
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
Packit 427e91
        gtk_container_add(box, label);
Packit 427e91
        g_free(s);
Packit 427e91
        g_free(s2); */
Packit 427e91
    }
Packit 427e91
Packit 427e91
    { /* Override */
Packit 427e91
        GtkWidget* override = data->override = gtk_check_button_new_with_label("Override");
Packit 427e91
        GtkWidget* jacktype = data->jacktype = gtk_combo_box_text_new();
Packit 427e91
        GtkWidget* jacktype_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
Packit 427e91
        int index = get_typical_pins(data->pins_info, 32, pin_cfg, ui->trust_codec);
Packit 427e91
        typical_pins_t* current = data->pins_info;
Packit 427e91
        while (current->name) {
Packit 427e91
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(jacktype), current->name);
Packit 427e91
            current++;
Packit 427e91
        }
Packit 427e91
        gtk_combo_box_set_active(GTK_COMBO_BOX(jacktype), index);
Packit 427e91
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(override), pin_cfg->user_override);
Packit 427e91
        g_signal_connect(override, "toggled", G_CALLBACK(override_toggled), data);
Packit 427e91
        g_signal_connect(jacktype, "changed", G_CALLBACK(jacktype_changed), data);
Packit 427e91
Packit 427e91
        gtk_container_add(box, override);
Packit 427e91
        gtk_container_add(GTK_CONTAINER(jacktype_box), jacktype);
Packit 427e91
        if (!ui->free_overrides)
Packit 427e91
            gtk_container_add(box, jacktype_box);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    /* Advanced override */
Packit 427e91
    {
Packit 427e91
        int i;
Packit 427e91
        GtkGrid* grid = GTK_GRID(gtk_grid_new());
Packit 427e91
        gtk_grid_set_row_spacing(grid, 2);
Packit 427e91
        gtk_grid_set_column_spacing(grid, 4);
Packit 427e91
Packit 427e91
        for (i = 0; i < FREE_OVERRIDES_COUNT; i++) {
Packit 427e91
            int index = -1;
Packit 427e91
            int j = 0;
Packit 427e91
            unsigned long act_pincfg = actual_pin_config(pin_cfg);
Packit 427e91
            unsigned long mask = get_free_override_mask(i);
Packit 427e91
            free_override_t* values = get_free_override_list(i);
Packit 427e91
            data->free_override_cb[i] = gtk_combo_box_text_new();
Packit 427e91
            if (!values)
Packit 427e91
                continue;
Packit 427e91
            while (values->name) {
Packit 427e91
                gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(data->free_override_cb[i]), values->name);
Packit 427e91
                if ((act_pincfg & mask) == values->value)
Packit 427e91
                    index = j;
Packit 427e91
                values++;
Packit 427e91
                j++;
Packit 427e91
            }
Packit 427e91
            if (index >= 0)
Packit 427e91
                gtk_combo_box_set_active(GTK_COMBO_BOX(data->free_override_cb[i]), index);
Packit 427e91
            g_signal_connect(data->free_override_cb[i], "changed", G_CALLBACK(jacktype_changed), data);
Packit 427e91
        }
Packit 427e91
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Connectivity"), 0, 0, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[0], 0, 1, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Location"), 1, 0, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[1], 1, 1, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Device"), 2, 0, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[2], 2, 1, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Jack"), 3, 0, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[3], 3, 1, 1, 1);
Packit 427e91
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Color"), 0, 2, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[4], 0, 3, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Jack detection"), 1, 2, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[5], 1, 3, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Channel group"), 2, 2, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[6], 2, 3, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, gtk_label_new("Channel (in group)"), 3, 2, 1, 1);
Packit 427e91
        gtk_grid_attach(grid, data->free_override_cb[7], 3, 3, 1, 1);
Packit 427e91
Packit 427e91
        if (ui->free_overrides)
Packit 427e91
            gtk_container_add(box, GTK_WIDGET(grid));
Packit 427e91
    }
Packit 427e91
    update_override_sensitive(data->override, data);
Packit 427e91
Packit 427e91
    gtk_container_add(GTK_CONTAINER(result), GTK_WIDGET(box));
Packit 427e91
    ui->pin_ui_data = g_list_prepend(ui->pin_ui_data, data);
Packit 427e91
    return result;
Packit 427e91
}
Packit 427e91
Packit 427e91
static void free_pin_ui_data(pin_ui_data_t* data)
Packit 427e91
{
Packit 427e91
    if (!data)
Packit 427e91
        return;
Packit 427e91
    if (data->frame)
Packit 427e91
        gtk_widget_destroy(data->frame);
Packit 427e91
    free(data);
Packit 427e91
}
Packit 427e91
Packit 427e91
static gint pin_config_find(pin_ui_data_t* pin_ui, pin_configs_t* cfg)
Packit 427e91
{
Packit 427e91
    return pin_ui->pin_config == cfg ? 0 : 1;
Packit 427e91
}
Packit 427e91
Packit 427e91
static void update_user_pin_config(ui_data_t* ui, pin_configs_t* cfg)
Packit 427e91
{
Packit 427e91
    pin_ui_data_t* pin_ui;
Packit 427e91
    GList *pos = g_list_find_custom(ui->pin_ui_data, cfg, (GCompareFunc) pin_config_find);
Packit 427e91
    cfg->user_override = FALSE;
Packit 427e91
    if (!pos)
Packit 427e91
        return;
Packit 427e91
    pin_ui = pos->data;
Packit 427e91
    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pin_ui->override)))
Packit 427e91
        return;
Packit 427e91
Packit 427e91
    if (ui->free_overrides) {
Packit 427e91
        int j;
Packit 427e91
        int index;
Packit 427e91
        unsigned long val = 0;
Packit 427e91
        for (j = 0; j < FREE_OVERRIDES_COUNT; j++) {
Packit 427e91
            index = gtk_combo_box_get_active(GTK_COMBO_BOX(pin_ui->free_override_cb[j]));
Packit 427e91
            if (index < 0)
Packit 427e91
                break;
Packit 427e91
            val += get_free_override_list(j)[index].value;
Packit 427e91
        }
Packit 427e91
        if (index < 0)
Packit 427e91
            return;
Packit 427e91
        cfg->user_pin_config = val;
Packit 427e91
    } else {
Packit 427e91
        int index;
Packit 427e91
        index = gtk_combo_box_get_active(GTK_COMBO_BOX(pin_ui->jacktype));
Packit 427e91
        if (index < 0)
Packit 427e91
            return;
Packit 427e91
        cfg->user_pin_config = pin_ui->pins_info[index].pin_set;
Packit 427e91
    }
Packit 427e91
    cfg->user_override = TRUE;
Packit 427e91
}
Packit 427e91
Packit 427e91
static void update_all_user_pin_config(ui_data_t* ui)
Packit 427e91
{
Packit 427e91
    int i;
Packit 427e91
    for (i = 0; i < ui->sysfs_pincount; i++)
Packit 427e91
        update_user_pin_config(ui, &ui->sysfs_pins[i]);
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean update_one_hint(GtkTreeModel *model, GtkTreePath *path,
Packit 427e91
                            GtkTreeIter *iter, gpointer userdata)
Packit 427e91
{
Packit 427e91
    gchar *name, *value;
Packit 427e91
    ui_data_t *ui = userdata;
Packit 427e91
    gtk_tree_model_get(GTK_TREE_MODEL(ui->hints.store), iter, 0, &name, 1, &value, -1);
Packit 427e91
    if (g_strcmp0(value, "default")) {
Packit 427e91
        gchar *s = g_strconcat(name, "=", value, "\n", ui->hints.values, NULL);
Packit 427e91
        g_free(ui->hints.values);
Packit 427e91
        ui->hints.values = s;
Packit 427e91
    }
Packit 427e91
    g_free(name);
Packit 427e91
    g_free(value);
Packit 427e91
    return FALSE;
Packit 427e91
}
Packit 427e91
Packit 427e91
static void update_hints(ui_data_t* ui)
Packit 427e91
{
Packit 427e91
    g_free(ui->hints.values);
Packit 427e91
    ui->hints.values = NULL;
Packit 427e91
    if (ui->hints.visible)
Packit 427e91
        gtk_tree_model_foreach(GTK_TREE_MODEL(ui->hints.store), update_one_hint, ui);
Packit 427e91
}
Packit 427e91
Packit 427e91
static GQuark quark()
Packit 427e91
{
Packit 427e91
    return g_quark_from_static_string("hda-jack-retask-error");
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean validate_user_pin_config(ui_data_t* ui, GError** err)
Packit 427e91
{
Packit 427e91
    int i;
Packit 427e91
Packit 427e91
    if (!ui->current_codec) {
Packit 427e91
        g_set_error(err, quark(), 0, "You must first select a codec!");
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
    update_hints(ui);
Packit 427e91
    update_all_user_pin_config(ui);
Packit 427e91
    if (ui->free_overrides)
Packit 427e91
        return TRUE;
Packit 427e91
Packit 427e91
    /* Check surround configs */
Packit 427e91
    for (i = 0; i < ui->sysfs_pincount; i++) {
Packit 427e91
        unsigned long v = ui->sysfs_pins[i].user_pin_config;
Packit 427e91
        if (!ui->sysfs_pins[i].user_override)
Packit 427e91
            continue;
Packit 427e91
        if ((v & 0xf0) != 0x10)
Packit 427e91
            continue;
Packit 427e91
        if (((v & 0xf) != 0) && !find_pin_channel_match(ui->sysfs_pins, ui->sysfs_pincount, v & 0xf0)) {
Packit 427e91
            g_set_error(err, quark(), 0, "This surround setup also requires a \"front\" channel override.");
Packit 427e91
            return FALSE;
Packit 427e91
        }
Packit 427e91
        if (((v & 0xf) >= 3) && !find_pin_channel_match(ui->sysfs_pins, ui->sysfs_pincount, 2 + (v & 0xf0))) {
Packit 427e91
            g_set_error(err, quark(), 0, "This surround setup also requires a \"back\" channel override.");
Packit 427e91
            return FALSE;
Packit 427e91
        }
Packit 427e91
        if ((v & 0xf) >= 3 && !find_pin_channel_match(ui->sysfs_pins, ui->sysfs_pincount, 1 + (v & 0xf0))) {
Packit 427e91
            g_set_error(err, quark(), 0, "This surround setup also requires a \"Center/LFE\" channel override.");
Packit 427e91
            return FALSE;
Packit 427e91
        }
Packit 427e91
    }
Packit 427e91
    return TRUE;
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean update_tree_one_hint(GtkTreeModel *model, GtkTreePath *path,
Packit 427e91
                            GtkTreeIter *iter, gpointer userdata)
Packit 427e91
{
Packit 427e91
    gchar *name;
Packit 427e91
    ui_data_t *ui = userdata;
Packit 427e91
    gtk_tree_model_get(GTK_TREE_MODEL(ui->hints.store), iter, 0, &name, -1);
Packit 427e91
    gchar *s = strstr(ui->hints.values, name);
Packit 427e91
    if (!s) {
Packit 427e91
        g_free(name);
Packit 427e91
        gtk_list_store_set(ui->hints.store, iter, 1, "default", -1);
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
    s += strlen(name);
Packit 427e91
    while (*s == ' ' || *s == '=') s++;
Packit 427e91
    gchar *s2 = s;
Packit 427e91
    while (*s != '\n' && *s != '\0') s++;
Packit 427e91
    s2 = g_strndup(s2, s - s2);
Packit 427e91
    gtk_list_store_set(ui->hints.store, iter, 1, s2, -1);
Packit 427e91
    g_free(s2);
Packit 427e91
    g_free(name);
Packit 427e91
    return FALSE;
Packit 427e91
}
Packit 427e91
Packit 427e91
static void show_action_result(ui_data_t* ui, GError* err, const gchar* ok_msg)
Packit 427e91
{
Packit 427e91
    GtkWidget* dialog;
Packit 427e91
    const gchar* msg = err ? err->message : ok_msg;
Packit 427e91
    dialog = gtk_message_dialog_new (GTK_WINDOW(ui->main_window),
Packit 427e91
        GTK_DIALOG_DESTROY_WITH_PARENT, err ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, 
Packit 427e91
        GTK_BUTTONS_CLOSE, "%s", msg);
Packit 427e91
    gtk_dialog_run (GTK_DIALOG (dialog));
Packit 427e91
    gtk_widget_destroy (dialog);
Packit 427e91
    if (err)
Packit 427e91
        g_error_free(err);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void apply_now_clicked(GtkButton* button, gpointer user_data)
Packit 427e91
{
Packit 427e91
    GError* err = NULL;
Packit 427e91
    ui_data_t* ui = user_data;
Packit 427e91
    gboolean ok = validate_user_pin_config(ui, &err);
Packit 427e91
    if (ok)
Packit 427e91
        apply_changes_reconfig(ui->sysfs_pins, ui->sysfs_pincount, 
Packit 427e91
            ui->current_codec->card, ui->current_codec->device, 
Packit 427e91
            ui->model_auto ? "auto" : NULL, ui->hints.values, &err);
Packit 427e91
    show_action_result(ui, err, 
Packit 427e91
        "Ok, now go ahead and test to see if it actually worked!\n"
Packit 427e91
        "(Remember, this stuff is still experimental.)");
Packit 427e91
}
Packit 427e91
Packit 427e91
static void apply_boot_clicked(GtkButton* button, gpointer user_data)
Packit 427e91
{
Packit 427e91
    GError* err = NULL;
Packit 427e91
    ui_data_t* ui = user_data;
Packit 427e91
    gboolean ok = validate_user_pin_config(ui, &err);
Packit 427e91
    if (ok) 
Packit 427e91
        apply_changes_boot(ui->sysfs_pins, ui->sysfs_pincount, 
Packit 427e91
            ui->current_codec->card, ui->current_codec->device, 
Packit 427e91
            ui->model_auto ? "auto" : NULL, ui->hints.values, &err);
Packit 427e91
    show_action_result(ui, err,
Packit 427e91
        "Ok, now reboot to test to see if it actually worked!\n"
Packit 427e91
        "(Remember, this stuff is still experimental.)");
Packit 427e91
}
Packit 427e91
Packit 427e91
Packit 427e91
static void reset_boot_clicked(GtkButton* button, gpointer user_data)
Packit 427e91
{
Packit 427e91
    GError* err = NULL;
Packit 427e91
    ui_data_t* ui = user_data;
Packit 427e91
    reset_changes_boot(&err);
Packit 427e91
    show_action_result(ui, err, 
Packit 427e91
        "The previous installed files (if any) of this program have been removed.\n"
Packit 427e91
        "Reboot to finish the uninstallation.");
Packit 427e91
}
Packit 427e91
Packit 427e91
static void resize_main_window(ui_data_t* ui)
Packit 427e91
{
Packit 427e91
    GtkAllocation a;
Packit 427e91
    GtkRequisition r;
Packit 427e91
    gint oldw, oldh, neww, newh, maxw, maxh;
Packit 427e91
    GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(ui->main_window));
Packit 427e91
    gtk_widget_size_request(GTK_WIDGET(ui->content_inner_box), &r);
Packit 427e91
    gtk_widget_get_allocation(ui->content_scroll_widget, &a);
Packit 427e91
//    fprintf(stderr, "W: %d, H: %d, W: %d, H: %d\n", a.width, a.height, r.width, r.height);
Packit 427e91
    gtk_window_get_size(GTK_WINDOW(ui->main_window), &oldw, &oldh);
Packit 427e91
    maxw = screen ? (gdk_screen_get_width(screen)*3)/4 : INT_MAX / 4;
Packit 427e91
    maxh = screen ? (gdk_screen_get_height(screen)*3)/4 : INT_MAX / 4;
Packit 427e91
//    fprintf(stderr, "Before: W: %d, H: %d\n", oldw, oldh);
Packit 427e91
    neww = oldw;
Packit 427e91
    newh = oldh;
Packit 427e91
    if (a.width < r.width) {
Packit 427e91
        neww += 8 + r.width - a.width;
Packit 427e91
        if (neww > maxw)
Packit 427e91
            neww = maxw;
Packit 427e91
    }
Packit 427e91
    if (a.height < r.height) {
Packit 427e91
        newh += 8 + r.height - a.height;
Packit 427e91
        if (newh > maxh)
Packit 427e91
            newh = maxh;
Packit 427e91
    }
Packit 427e91
    if (neww != oldw || newh != oldh) {
Packit 427e91
        gtk_window_resize(GTK_WINDOW(ui->main_window), neww, newh);
Packit 427e91
//        fprintf(stderr, "After: W: %d, H: %d\n", neww, newh);   
Packit 427e91
    }
Packit 427e91
}
Packit 427e91
Packit 427e91
static void update_codec_ui(ui_data_t* ui, bool codec_change)
Packit 427e91
{
Packit 427e91
    int codec_index = gtk_combo_box_get_active(GTK_COMBO_BOX(ui->codec_selection_combo));
Packit 427e91
    int i;
Packit 427e91
Packit 427e91
    g_list_free_full(ui->pin_ui_data, (GDestroyNotify) free_pin_ui_data);
Packit 427e91
    ui->pin_ui_data = NULL;
Packit 427e91
    ui->current_codec = NULL;
Packit 427e91
Packit 427e91
    if (codec_index < 0)
Packit 427e91
        return;
Packit 427e91
    ui->current_codec = &ui->sysfs_codec_names[codec_index];
Packit 427e91
    if (codec_change) {
Packit 427e91
        ui->sysfs_pincount = get_pin_configs_list(ui->sysfs_pins, 32, ui->current_codec->card, ui->current_codec->device);
Packit 427e91
        ui->hints.values = get_hint_overrides(ui->current_codec->card, ui->current_codec->device);
Packit 427e91
        gtk_tree_model_foreach(GTK_TREE_MODEL(ui->hints.store), update_tree_one_hint, ui);
Packit 427e91
    }
Packit 427e91
    for (i = 0; i < ui->sysfs_pincount; i++) {
Packit 427e91
        GtkWidget *w = create_pin_ui(ui, &ui->sysfs_pins[i]);
Packit 427e91
        if (w)
Packit 427e91
            gtk_container_add(GTK_CONTAINER(ui->content_inner_box), w);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    gtk_widget_show_all(GTK_WIDGET(ui->content_inner_box));
Packit 427e91
Packit 427e91
    if (ui->hints.visible)
Packit 427e91
        gtk_widget_show_all(ui->hints.frame);
Packit 427e91
    else
Packit 427e91
        gtk_widget_hide(ui->hints.frame);
Packit 427e91
Packit 427e91
    resize_main_window(ui);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void codec_selected(GtkComboBox* combo, gpointer user_data) 
Packit 427e91
{
Packit 427e91
    update_codec_ui(user_data, true);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void showallpins_toggled(GtkWidget* sender, ui_data_t* ui_data)
Packit 427e91
{
Packit 427e91
    gboolean checked = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sender));
Packit 427e91
    ui_data->trust_defcfg = !checked;
Packit 427e91
    update_codec_ui(ui_data, false);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void automodel_toggled(GtkWidget* sender, ui_data_t* ui_data)
Packit 427e91
{
Packit 427e91
    ui_data->model_auto = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sender));
Packit 427e91
}
Packit 427e91
Packit 427e91
static void free_override_toggled(GtkWidget* sender, ui_data_t* ui_data)
Packit 427e91
{
Packit 427e91
    ui_data->free_overrides = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sender));
Packit 427e91
    update_codec_ui(ui_data, false);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void hints_toggled(GtkWidget* sender, ui_data_t* ui_data)
Packit 427e91
{
Packit 427e91
    ui_data->hints.visible = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sender));
Packit 427e91
    update_codec_ui(ui_data, false);
Packit 427e91
}
Packit 427e91
Packit 427e91
static void hints_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
Packit 427e91
                                GtkTreeViewColumn *column, ui_data_t* ui_data)
Packit 427e91
{
Packit 427e91
    GtkTreeIter iter;
Packit 427e91
    gchar *value;
Packit 427e91
    const gchar *newvalue = "default";
Packit 427e91
Packit 427e91
    gtk_tree_model_get_iter(GTK_TREE_MODEL(ui_data->hints.store), &iter, path);
Packit 427e91
    gtk_tree_model_get(GTK_TREE_MODEL(ui_data->hints.store), &iter, 1, &value, -1);
Packit 427e91
Packit 427e91
    if (!g_strcmp0(value, "default"))
Packit 427e91
        newvalue = "yes";
Packit 427e91
    else if (!g_strcmp0(value, "yes"))
Packit 427e91
        newvalue = "no";
Packit 427e91
    gtk_list_store_set(ui_data->hints.store, &iter, 1, newvalue, -1);
Packit 427e91
Packit 427e91
    g_free(value);
Packit 427e91
}
Packit 427e91
Packit 427e91
Packit 427e91
static const char* readme_text = 
Packit 427e91
#include "README.generated.h"
Packit 427e91
;
Packit 427e91
Packit 427e91
static void documentation_clicked(GtkWidget* sender, ui_data_t* ui)
Packit 427e91
{
Packit 427e91
    GtkDialog* dlg = GTK_DIALOG(gtk_dialog_new_with_buttons("Jack retasking documentation", 
Packit 427e91
        GTK_WINDOW(ui->main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
Packit 427e91
        GTK_STOCK_CLOSE, GTK_RESPONSE_NONE, NULL));
Packit 427e91
    GtkTextView* textview = GTK_TEXT_VIEW(gtk_text_view_new());
Packit 427e91
    GtkContainer* content_area = GTK_CONTAINER(gtk_dialog_get_content_area(dlg));
Packit 427e91
    GtkScrolledWindow* content_scroll = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
Packit 427e91
Packit 427e91
    gtk_text_buffer_set_text(gtk_text_view_get_buffer(textview), readme_text, -1);
Packit 427e91
    gtk_text_view_set_editable(textview, FALSE);
Packit 427e91
    gtk_text_view_set_wrap_mode(textview, GTK_WRAP_WORD);
Packit 427e91
    gtk_text_view_set_cursor_visible(textview, FALSE);
Packit 427e91
    gtk_scrolled_window_add_with_viewport(content_scroll, GTK_WIDGET(textview));
Packit 427e91
    gtk_container_add(content_area, GTK_WIDGET(content_scroll));
Packit 427e91
    gtk_box_set_child_packing(GTK_BOX(content_area), GTK_WIDGET(content_scroll), TRUE, TRUE, 2, GTK_PACK_START);
Packit 427e91
Packit 427e91
    gtk_widget_show_all(GTK_WIDGET(content_area));
Packit 427e91
Packit 427e91
    { /* Resize to fit screen */
Packit 427e91
        GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(ui->main_window));
Packit 427e91
        int neww = screen ? (gdk_screen_get_width(screen)*3)/4 : 800;
Packit 427e91
        int newh = screen ? (gdk_screen_get_height(screen)*3)/4 : 600;
Packit 427e91
        
Packit 427e91
        gtk_window_set_default_size(GTK_WINDOW(dlg), neww, newh);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    gtk_dialog_run(dlg);
Packit 427e91
    gtk_widget_destroy(GTK_WIDGET(dlg));
Packit 427e91
}
Packit 427e91
Packit 427e91
static ui_data_t* create_ui()
Packit 427e91
{
Packit 427e91
    ui_data_t* ui = calloc(sizeof(ui_data_t), 1);
Packit 427e91
    GtkContainer* toplevel_box = GTK_CONTAINER(gtk_box_new(GTK_ORIENTATION_VERTICAL, 2));
Packit 427e91
    GtkContainer* toplevel_2ndbox = GTK_CONTAINER(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2));
Packit 427e91
    ui->content_inner_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
Packit 427e91
    GtkContainer* rightside_box = GTK_CONTAINER(gtk_box_new(GTK_ORIENTATION_VERTICAL, 2));
Packit 427e91
    ui->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
Packit 427e91
    gtk_window_set_title(GTK_WINDOW(ui->main_window), "Jack retasking for HDA Intel sound cards");
Packit 427e91
    g_signal_connect (ui->main_window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
Packit 427e91
Packit 427e91
    ui->trust_codec = TRUE;
Packit 427e91
    ui->trust_defcfg = TRUE;
Packit 427e91
Packit 427e91
    /* Select codec to work with */
Packit 427e91
    {
Packit 427e91
        GtkWidget* combo = ui->codec_selection_combo = gtk_combo_box_text_new();
Packit 427e91
        GtkWidget* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
Packit 427e91
        codec_name_t *n = ui->sysfs_codec_names;
Packit 427e91
        get_codec_name_list(ui->sysfs_codec_names, 128);
Packit 427e91
        while (n->card != -1) {
Packit 427e91
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), n->name);
Packit 427e91
            n++;
Packit 427e91
        }
Packit 427e91
        /* Select the first codec */
Packit 427e91
        if (ui->sysfs_codec_names->card != -1) {
Packit 427e91
            gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
Packit 427e91
            g_signal_connect(combo, "changed", G_CALLBACK(codec_selected), ui);
Packit 427e91
            gtk_container_add(GTK_CONTAINER(box), gtk_label_new("Select a codec:"));
Packit 427e91
            gtk_container_add(GTK_CONTAINER(box), combo);
Packit 427e91
        }
Packit 427e91
        else {
Packit 427e91
            gtk_container_add(GTK_CONTAINER(box), gtk_label_new("No codecs found. Sorry."));
Packit 427e91
            gtk_widget_destroy(combo);
Packit 427e91
        }
Packit 427e91
Packit 427e91
        gtk_container_add(toplevel_box, box);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    /* Add pin content area */
Packit 427e91
    {
Packit 427e91
        GtkWidget* frame = gtk_frame_new("Pin configuration");
Packit 427e91
        GtkScrolledWindow* content_scroll = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
Packit 427e91
        ui->content_scroll_widget = GTK_WIDGET(content_scroll);
Packit 427e91
        gtk_frame_set_label_align(GTK_FRAME(frame), 0.5, 0.5);
Packit 427e91
        gtk_scrolled_window_add_with_viewport(content_scroll, ui->content_inner_box);
Packit 427e91
        gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(content_scroll));
Packit 427e91
        gtk_container_add(toplevel_2ndbox, frame);
Packit 427e91
        gtk_box_set_child_packing(GTK_BOX(toplevel_2ndbox), frame, TRUE, TRUE, 2, GTK_PACK_START);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    /* Create hints */
Packit 427e91
    {
Packit 427e91
        GtkWidget* frame = gtk_frame_new("Hints");
Packit 427e91
        ui->hints.frame = frame;
Packit 427e91
Packit 427e91
        GtkListStore *store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
Packit 427e91
        ui->hints.store = store;
Packit 427e91
        const gchar** names = get_standard_hint_names();
Packit 427e91
        for (; *names; names++) {
Packit 427e91
            GtkTreeIter iter;
Packit 427e91
            gtk_list_store_append(store, &iter);
Packit 427e91
            gtk_list_store_set(store, &iter, 0, *names, 1, "default", -1);
Packit 427e91
        }
Packit 427e91
Packit 427e91
        GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
Packit 427e91
        gtk_tree_view_append_column(GTK_TREE_VIEW(tree), gtk_tree_view_column_new_with_attributes
Packit 427e91
            ("Name", gtk_cell_renderer_text_new(), "text", 0, NULL));
Packit 427e91
        gtk_tree_view_append_column(GTK_TREE_VIEW(tree), gtk_tree_view_column_new_with_attributes
Packit 427e91
            ("Value", gtk_cell_renderer_text_new(), "text", 1, NULL));
Packit 427e91
        g_signal_connect(tree, "row-activated", G_CALLBACK(hints_row_activated), ui);
Packit 427e91
Packit 427e91
        gtk_container_add(GTK_CONTAINER(frame), tree);
Packit 427e91
        gtk_container_add(toplevel_2ndbox, frame);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    /* Create settings */
Packit 427e91
    {
Packit 427e91
        GtkWidget* frame = gtk_frame_new("Options");
Packit 427e91
        GtkContainer* box = GTK_CONTAINER(gtk_button_box_new(GTK_ORIENTATION_VERTICAL));
Packit 427e91
        GtkWidget* check;
Packit 427e91
Packit 427e91
        check = gtk_check_button_new_with_label("Show unconnected pins");
Packit 427e91
        g_signal_connect(check, "toggled", G_CALLBACK(showallpins_toggled), ui);
Packit 427e91
        gtk_container_add(box, check);
Packit 427e91
Packit 427e91
        check = gtk_check_button_new_with_label("Set model=auto");
Packit 427e91
        g_signal_connect(check, "toggled", G_CALLBACK(automodel_toggled), ui);
Packit 427e91
        gtk_container_add(box, check);
Packit 427e91
Packit 427e91
        check = gtk_check_button_new_with_label("Advanced override");
Packit 427e91
        g_signal_connect(check, "toggled", G_CALLBACK(free_override_toggled), ui);
Packit 427e91
        gtk_container_add(box, check);
Packit 427e91
Packit 427e91
        check = gtk_check_button_new_with_label("Parser hints");
Packit 427e91
        g_signal_connect(check, "toggled", G_CALLBACK(hints_toggled), ui);
Packit 427e91
        gtk_container_add(box, check);
Packit 427e91
Packit 427e91
        gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(box));
Packit 427e91
        gtk_container_add(rightside_box, frame);
Packit 427e91
    }
Packit 427e91
Packit 427e91
    /* Create bottom right buttons */
Packit 427e91
    {
Packit 427e91
        GtkWidget* button;
Packit 427e91
        GtkWidget* box = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
Packit 427e91
        button = gtk_button_new_with_label("Read documentation");
Packit 427e91
        g_signal_connect(button, "clicked", G_CALLBACK(documentation_clicked), ui);
Packit 427e91
        gtk_container_add(GTK_CONTAINER(box), button);
Packit 427e91
        button = gtk_button_new_with_label("Apply now");
Packit 427e91
        g_signal_connect(button, "clicked", G_CALLBACK(apply_now_clicked), ui);
Packit 427e91
        gtk_container_add(GTK_CONTAINER(box), button);
Packit 427e91
        button = gtk_button_new_with_label("Install boot override");
Packit 427e91
        g_signal_connect(button, "clicked", G_CALLBACK(apply_boot_clicked), ui);
Packit 427e91
        gtk_container_add(GTK_CONTAINER(box), button);
Packit 427e91
        button = gtk_button_new_with_label("Remove boot override");
Packit 427e91
        g_signal_connect(button, "clicked", G_CALLBACK(reset_boot_clicked), ui);
Packit 427e91
        gtk_container_add(GTK_CONTAINER(box), button);
Packit 427e91
Packit 427e91
        gtk_container_add(rightside_box, box);
Packit 427e91
        gtk_box_set_child_packing(GTK_BOX(rightside_box), box, FALSE, FALSE, 2, GTK_PACK_END);
Packit 427e91
    }
Packit 427e91
    
Packit 427e91
    gtk_container_add(toplevel_2ndbox, GTK_WIDGET(rightside_box));
Packit 427e91
Packit 427e91
    gtk_container_add(GTK_CONTAINER(toplevel_box), GTK_WIDGET(toplevel_2ndbox));
Packit 427e91
    gtk_box_set_child_packing(GTK_BOX(toplevel_box), GTK_WIDGET(toplevel_2ndbox), TRUE, TRUE, 2, GTK_PACK_END);
Packit 427e91
    gtk_container_add(GTK_CONTAINER(ui->main_window), GTK_WIDGET(toplevel_box));
Packit 427e91
Packit 427e91
    return ui;
Packit 427e91
}
Packit 427e91
Packit 427e91
int main(int argc, char *argv[])
Packit 427e91
{
Packit 427e91
    ui_data_t* ui;
Packit 427e91
    gtk_init(&argc, &argv);
Packit 427e91
    ui = create_ui();
Packit 427e91
    gtk_widget_show_all(ui->main_window);
Packit 427e91
    if (ui->codec_selection_combo)
Packit 427e91
        update_codec_ui(ui, true);
Packit 427e91
    gtk_main();
Packit 427e91
    return 0;
Packit 427e91
}