Blame hdajackretask/apply-changes.c

Packit 427e91
/* Copyright 2011 David Henningsson, Canonical Ltd.
Packit 427e91
   License: GPLv2+ 
Packit 427e91
*/
Packit 427e91
Packit 427e91
#include <stdio.h>
Packit 427e91
#include <unistd.h>
Packit 427e91
#include <string.h>
Packit 427e91
#include <glib.h>
Packit 427e91
#include <errno.h>
Packit 427e91
#include <glib gstdio.h="">
Packit 427e91
Packit 427e91
#include "apply-changes.h"
Packit 427e91
Packit 427e91
static gchar* tempdir = NULL;
Packit 427e91
static gchar* scriptfile = NULL;
Packit 427e91
static gchar* errorfile = NULL;
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 ensure_tempdir(GError** err)
Packit 427e91
{
Packit 427e91
    if (!tempdir) {
Packit 427e91
        tempdir = g_dir_make_tmp("hda-jack-retask-XXXXXX", err);
Packit 427e91
        if (!tempdir)
Packit 427e91
            return FALSE;
Packit 427e91
        scriptfile = g_strdup_printf("%s/script.sh", tempdir);
Packit 427e91
        errorfile = g_strdup_printf("%s/errors.log", tempdir);
Packit 427e91
    }
Packit 427e91
    g_unlink(errorfile); /* Ignore file does not exist error */
Packit 427e91
    return TRUE;
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean create_reconfig_script(pin_configs_t* pins, int entries, int card, int device, 
Packit 427e91
    const char* model, const char* hints, GError** err)
Packit 427e91
{
Packit 427e91
    gchar* hwdir = g_strdup_printf("/sys/class/sound/hwC%dD%d", card, device);
Packit 427e91
    gchar destbuf[150*40] = "#!/bin/sh\n";
Packit 427e91
    int bufleft = sizeof(destbuf) - strlen(destbuf);
Packit 427e91
    gboolean ok = FALSE;
Packit 427e91
    gchar* s = destbuf + strlen(destbuf);
Packit 427e91
Packit 427e91
    if (!ensure_tempdir(err))
Packit 427e91
        goto cleanup;
Packit 427e91
Packit 427e91
    if (model) {
Packit 427e91
        int l = g_snprintf(s, bufleft, "echo \"%s\" | tee %s/modelname 2>>%s\n",
Packit 427e91
            model, hwdir, errorfile);
Packit 427e91
        bufleft-=l;
Packit 427e91
        s+=l;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    if (hints) {
Packit 427e91
        int l = g_snprintf(s, bufleft, "echo \"%s\" | tee %s/hints 2>>%s\n",
Packit 427e91
            hints, hwdir, errorfile);
Packit 427e91
        bufleft-=l;
Packit 427e91
        s+=l;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    while (entries) {
Packit 427e91
        int l = g_snprintf(s, bufleft, "echo \"0x%02x 0x%08x\" | tee %s/user_pin_configs 2>>%s\n",
Packit 427e91
            pins->nid, (unsigned int) actual_pin_config(pins), hwdir, errorfile);
Packit 427e91
        bufleft-=l;
Packit 427e91
        s+=l;
Packit 427e91
        pins++;
Packit 427e91
        entries--;
Packit 427e91
    }    
Packit 427e91
Packit 427e91
    if (bufleft < g_snprintf(s, bufleft, "echo 1 | tee %s/reconfig 2>>%s", hwdir, errorfile)) {
Packit 427e91
        g_set_error(err, quark(), 0, "Bug in %s:%d!", __FILE__, __LINE__);
Packit 427e91
        goto cleanup;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    if (!g_file_set_contents(scriptfile, destbuf, -1, err))
Packit 427e91
        goto cleanup;
Packit 427e91
    
Packit 427e91
    ok = TRUE;
Packit 427e91
cleanup:
Packit 427e91
    g_free(hwdir);
Packit 427e91
    return ok;
Packit 427e91
}
Packit 427e91
Packit 427e91
Packit 427e91
//#define SUDO_COMMAND "gksudo --description \"Jack retasking\""
Packit 427e91
#define SUDO_COMMAND "pkexec"
Packit 427e91
Packit 427e91
gboolean run_sudo_script(const gchar* script_name, GError** err)
Packit 427e91
{
Packit 427e91
    gchar* errfilecontents = NULL;
Packit 427e91
    gchar* cmdline = g_strdup_printf("%s %s", SUDO_COMMAND, script_name);
Packit 427e91
    int exit_status;
Packit 427e91
    gsize errlen;
Packit 427e91
    gboolean ok;
Packit 427e91
Packit 427e91
    g_chmod(script_name, 0755);
Packit 427e91
    g_spawn_command_line_sync(cmdline, NULL, NULL, &exit_status, NULL);
Packit 427e91
    if (errorfile && g_file_get_contents(errorfile, &errfilecontents, &errlen, NULL) && errlen) {
Packit 427e91
        g_set_error(err, quark(), 0, "%s", errfilecontents);
Packit 427e91
        ok = FALSE;
Packit 427e91
    }
Packit 427e91
    else ok = TRUE;
Packit 427e91
Packit 427e91
    g_free(errfilecontents);
Packit 427e91
    g_free(cmdline);
Packit 427e91
    return ok;
Packit 427e91
}
Packit 427e91
Packit 427e91
static gchar* get_pulseaudio_client_conf()
Packit 427e91
{
Packit 427e91
    /* Reference: See src/pulsecore/core-util.c in pulseaudio */
Packit 427e91
    gchar* fname;
Packit 427e91
    gchar* dir = g_strdup_printf("%s/.pulse", g_get_home_dir());
Packit 427e91
    if (access(dir, F_OK) < 0) {
Packit 427e91
	const gchar* xch = g_getenv("XDG_CONFIG_HOME");
Packit 427e91
	g_free(dir);
Packit 427e91
	if (xch)
Packit 427e91
	    dir = g_strdup_printf("%s/pulse", xch);
Packit 427e91
	else
Packit 427e91
	    dir = g_strdup_printf("%s/.config/pulse", g_get_home_dir());
Packit 427e91
    }
Packit 427e91
    fname = g_strdup_printf("%s/client.conf", dir);
Packit 427e91
    g_free(dir);
Packit 427e91
    return fname;
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean kill_pulseaudio(gboolean* was_killed, int card, GError** err)
Packit 427e91
{
Packit 427e91
    gchar* fuser = NULL, *fuser2 = NULL;
Packit 427e91
    gchar* s = NULL;
Packit 427e91
    gchar* clientconf = NULL;
Packit 427e91
    gboolean ok;
Packit 427e91
    *was_killed = FALSE;
Packit 427e91
    /* Is PA having a lock on the sound card? */
Packit 427e91
    s = g_strdup_printf("fuser -v /dev/snd/controlC%d", card);
Packit 427e91
    /* Due to some bug in fuser, stdout and stderr output is unclear. Better check both. */
Packit 427e91
    if (!(ok = g_spawn_command_line_sync(s, &fuser, &fuser2, NULL, err))) 
Packit 427e91
        goto cleanup;
Packit 427e91
    if ((ok = strstr(fuser, "pulseaudio") == NULL && strstr(fuser2, "pulseaudio") == NULL))
Packit 427e91
        goto cleanup; // PulseAudio not locking the sound card
Packit 427e91
Packit 427e91
    clientconf = get_pulseaudio_client_conf();
Packit 427e91
    if (!(ok = !g_file_test(clientconf, G_FILE_TEST_EXISTS))) {
Packit 427e91
        g_set_error(err, quark(), 0, "Cannot block PulseAudio from respawning:\n"
Packit 427e91
            "Please either remove '%s' or kill PulseAudio manually.", clientconf);
Packit 427e91
        goto cleanup;
Packit 427e91
    }
Packit 427e91
    
Packit 427e91
    if (!(ok = g_file_set_contents(clientconf, "autospawn=no\n", -1, err)))
Packit 427e91
        goto cleanup;
Packit 427e91
Packit 427e91
    *was_killed = TRUE;
Packit 427e91
    ok = g_spawn_command_line_sync("pulseaudio -k", NULL, NULL, NULL, err);
Packit 427e91
Packit 427e91
cleanup:
Packit 427e91
    g_free(clientconf);
Packit 427e91
    g_free(fuser);
Packit 427e91
    g_free(fuser2);
Packit 427e91
    g_free(s);
Packit 427e91
    return ok;
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean restore_pulseaudio(gboolean was_killed, GError** err) 
Packit 427e91
{
Packit 427e91
    gchar* clientconf = get_pulseaudio_client_conf();
Packit 427e91
    if (was_killed && g_unlink(clientconf) != 0) {
Packit 427e91
        g_set_error(err, quark(), 0, "%s", g_strerror(errno));
Packit 427e91
        g_free(clientconf);
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
    g_free(clientconf);
Packit 427e91
    return TRUE;
Packit 427e91
}
Packit 427e91
Packit 427e91
gboolean apply_changes_reconfig(pin_configs_t* pins, int entries, int card, int device, 
Packit 427e91
    const char* model, const char* hints, GError** err)
Packit 427e91
{
Packit 427e91
    gboolean result = FALSE;
Packit 427e91
//    gchar* script_name = NULL;
Packit 427e91
    gboolean pa_killed = FALSE;
Packit 427e91
    /* Check for users of the sound card */
Packit 427e91
    /* Kill pulseaudio if necessary (and possible) */
Packit 427e91
    if (!kill_pulseaudio(&pa_killed, card, err))
Packit 427e91
        goto cleanup;
Packit 427e91
    /* Create script */
Packit 427e91
    if (!create_reconfig_script(pins, entries, card, device, model, hints, err))
Packit 427e91
        goto cleanup;
Packit 427e91
    /* Run script as root */
Packit 427e91
    if (!run_sudo_script(scriptfile, err))
Packit 427e91
        goto cleanup;
Packit 427e91
    result = TRUE;
Packit 427e91
cleanup:
Packit 427e91
    if (!restore_pulseaudio(pa_killed, result ? err : NULL)) {
Packit 427e91
        result = FALSE;
Packit 427e91
    }
Packit 427e91
//    g_free(script_name);
Packit 427e91
    return result;
Packit 427e91
}
Packit 427e91
Packit 427e91
static gboolean create_firmware_file(pin_configs_t* pins, int entries, int card, int device, 
Packit 427e91
    const char* model, const char* hints, GError** err)
Packit 427e91
{
Packit 427e91
    gboolean ok;
Packit 427e91
    gchar destbuf[40*40+40*24] = "";
Packit 427e91
    gchar* s = destbuf;
Packit 427e91
    gchar* filename = g_strdup_printf("%s/hda-jack-retask.fw", tempdir);
Packit 427e91
    unsigned int address, codec_vendorid, codec_ssid;
Packit 427e91
    int bufleft = sizeof(destbuf);
Packit 427e91
    int l;
Packit 427e91
Packit 427e91
    get_codec_header(card, device, &address, &codec_vendorid, &codec_ssid);
Packit 427e91
    l = g_snprintf(s, bufleft, "[codec]\n0x%08x 0x%08x %u\n\n[pincfg]\n", codec_vendorid, codec_ssid, address);
Packit 427e91
    bufleft -= l;
Packit 427e91
    s += l;
Packit 427e91
Packit 427e91
    while (entries) {
Packit 427e91
        l = g_snprintf(s, bufleft, "0x%02x 0x%08x\n", pins->nid, (unsigned int) actual_pin_config(pins));
Packit 427e91
        bufleft -= l;
Packit 427e91
        s += l;
Packit 427e91
        pins++;
Packit 427e91
        entries--;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    if (model) {
Packit 427e91
        int l = g_snprintf(s, bufleft, "\n[model]\n%s\n", model);
Packit 427e91
        bufleft-=l;
Packit 427e91
        s+=l;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    if (hints) {
Packit 427e91
        int l = g_snprintf(s, bufleft, "\n[hints]\n%s\n", hints);
Packit 427e91
        bufleft-=l;
Packit 427e91
        s+=l;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    ok = g_file_set_contents(filename, destbuf, -1, err);
Packit 427e91
    g_free(filename);
Packit 427e91
    return ok;
Packit 427e91
}
Packit 427e91
Packit 427e91
Packit 427e91
static const gchar* remove_script =
Packit 427e91
"#!/bin/sh\n"
Packit 427e91
"rm /etc/modprobe.d/hda-jack-retask.conf 2>>%s\n"
Packit 427e91
"rm /lib/firmware/hda-jack-retask.fw 2>>%s\n";
Packit 427e91
Packit 427e91
static const gchar* retask_conf = 
Packit 427e91
"# This file was added by the program 'hda-jack-retask'.\n"
Packit 427e91
"# If you want to revert the changes made by this program, you can simply erase this file and reboot your computer.\n"
Packit 427e91
"options snd-hda-intel patch=hda-jack-retask.fw,hda-jack-retask.fw,hda-jack-retask.fw,hda-jack-retask.fw\n";
Packit 427e91
Packit 427e91
static const gchar* install_script =
Packit 427e91
"#!/bin/sh\n"
Packit 427e91
"mv %s/hda-jack-retask.fw /lib/firmware/hda-jack-retask.fw\n 2>>%s\n"
Packit 427e91
"mv %s/hda-jack-retask.conf /etc/modprobe.d/hda-jack-retask.conf 2>>%s\n";
Packit 427e91
Packit 427e91
gboolean apply_changes_boot(pin_configs_t* pins, int entries, int card, int device, 
Packit 427e91
    const char* model, const char* hints, GError** err)
Packit 427e91
{
Packit 427e91
    gchar *s;
Packit 427e91
Packit 427e91
    if (!ensure_tempdir(err))
Packit 427e91
        return FALSE;
Packit 427e91
Packit 427e91
    if (!create_firmware_file(pins, entries, card, device, model, hints, err))
Packit 427e91
        return FALSE;
Packit 427e91
Packit 427e91
    /* Create hda-jack-retask.conf */
Packit 427e91
    s = g_strdup_printf("%s/hda-jack-retask.conf", tempdir);
Packit 427e91
    if (!g_file_set_contents(s, retask_conf, -1, err)) {
Packit 427e91
        g_free(s);
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
    g_free(s);
Packit 427e91
Packit 427e91
    /* Create install script */    
Packit 427e91
    s = g_strdup_printf(install_script, tempdir, errorfile, tempdir, errorfile);
Packit 427e91
    if (!g_file_set_contents(scriptfile, s, -1, err)) {
Packit 427e91
        g_free(s);
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
    g_free(s);
Packit 427e91
Packit 427e91
    /* Run script as root */
Packit 427e91
    if (!run_sudo_script(scriptfile, err))
Packit 427e91
        return FALSE;
Packit 427e91
    return TRUE;
Packit 427e91
}
Packit 427e91
Packit 427e91
gboolean reset_changes_boot(GError** err)
Packit 427e91
{
Packit 427e91
    gchar *s;
Packit 427e91
Packit 427e91
    if ((g_file_test("/etc/modprobe.d/hda-jack-retask.conf", G_FILE_TEST_EXISTS) == 0) &&
Packit 427e91
        (g_file_test("/lib/firmware/hda-jack-retask.fw", G_FILE_TEST_EXISTS) == 0))
Packit 427e91
    {
Packit 427e91
        g_set_error(err, quark(), 0, "No boot override is currently installed, nothing to remove.");
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
Packit 427e91
    if (!ensure_tempdir(err))
Packit 427e91
        return FALSE;
Packit 427e91
    s = g_strdup_printf(remove_script, errorfile, errorfile);
Packit 427e91
    if (!g_file_set_contents(scriptfile, s, -1, err)) {
Packit 427e91
        g_free(s);
Packit 427e91
        return FALSE;
Packit 427e91
    }
Packit 427e91
    g_free(s);
Packit 427e91
Packit 427e91
    /* Run script as root */
Packit 427e91
    if (!run_sudo_script(scriptfile, err))
Packit 427e91
        return FALSE;
Packit 427e91
    return TRUE;
Packit 427e91
}
Packit 427e91