-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapply-changes.c
383 lines (330 loc) · 11.2 KB
/
apply-changes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* Copyright 2011 David Henningsson, Canonical Ltd.
License: GPLv2+
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>
#include <errno.h>
#include <glib/gstdio.h>
#include "apply-changes.h"
static gchar* tempdir = NULL;
static gchar* scriptfile = NULL;
static gchar* errorfile = NULL;
struct soundserver {
enum {
PULSEAUDIO,
PIPEWIRE
} type;
gboolean was_killed;
gchar *user;
};
static GQuark quark()
{
return g_quark_from_static_string("hda-jack-retask-error");
}
static gboolean ensure_tempdir(GError** err)
{
if (!tempdir) {
tempdir = g_dir_make_tmp("hda-jack-retask-XXXXXX", err);
if (!tempdir)
return FALSE;
scriptfile = g_strdup_printf("%s/script.sh", tempdir);
errorfile = g_strdup_printf("%s/errors.log", tempdir);
}
g_unlink(errorfile); /* Ignore file does not exist error */
return TRUE;
}
static gboolean create_reconfig_script(pin_configs_t* pins, int entries, int card, int device,
const char* model, const char* hints, GError** err)
{
gchar* hwdir = g_strdup_printf("/sys/class/sound/hwC%dD%d", card, device);
gchar destbuf[150*40] = "#!/bin/sh\n";
int bufleft = sizeof(destbuf) - strlen(destbuf);
gboolean ok = FALSE;
gchar* s = destbuf + strlen(destbuf);
if (!ensure_tempdir(err))
goto cleanup;
if (model) {
int l = g_snprintf(s, bufleft, "echo \"%s\" | tee %s/modelname 2>>%s\n",
model, hwdir, errorfile);
bufleft-=l;
s+=l;
}
if (hints) {
int l = g_snprintf(s, bufleft, "echo \"%s\" | tee %s/hints 2>>%s\n",
hints, hwdir, errorfile);
bufleft-=l;
s+=l;
}
while (entries) {
int l = g_snprintf(s, bufleft, "echo \"0x%02x 0x%08x\" | tee %s/user_pin_configs 2>>%s\n",
pins->nid, (unsigned int) actual_pin_config(pins), hwdir, errorfile);
bufleft-=l;
s+=l;
pins++;
entries--;
}
if (bufleft < g_snprintf(s, bufleft, "echo 1 | tee %s/reconfig 2>>%s", hwdir, errorfile)) {
g_set_error(err, quark(), 0, "Bug in %s:%d!", __FILE__, __LINE__);
goto cleanup;
}
if (!g_file_set_contents(scriptfile, destbuf, -1, err))
goto cleanup;
ok = TRUE;
cleanup:
g_free(hwdir);
return ok;
}
//#define SUDO_COMMAND "gksudo --description \"Jack retasking\""
#define SUDO_COMMAND "pkexec"
gboolean run_sudo_script(const gchar* script_name, GError** err)
{
gchar* errfilecontents = NULL;
gchar* cmdline = g_strdup_printf("%s %s", SUDO_COMMAND, script_name);
int exit_status;
gsize errlen;
gboolean ok;
g_chmod(script_name, 0755);
g_spawn_command_line_sync(cmdline, NULL, NULL, &exit_status, NULL);
if (errorfile && g_file_get_contents(errorfile, &errfilecontents, &errlen, NULL) && errlen) {
g_set_error(err, quark(), 0, "%s", errfilecontents);
ok = FALSE;
}
else ok = TRUE;
g_free(errfilecontents);
g_free(cmdline);
return ok;
}
static gchar* get_pulseaudio_client_conf()
{
/* Reference: See src/pulsecore/core-util.c in pulseaudio */
gchar* fname;
gchar* dir = g_strdup_printf("%s/.pulse", g_get_home_dir());
if (access(dir, F_OK) < 0) {
const gchar* xch = g_getenv("XDG_CONFIG_HOME");
g_free(dir);
if (xch)
dir = g_strdup_printf("%s/pulse", xch);
else
dir = g_strdup_printf("%s/.config/pulse", g_get_home_dir());
}
fname = g_strdup_printf("%s/client.conf", dir);
g_free(dir);
return fname;
}
static gboolean call_systemctl(gchar* user, gchar* operation, gchar *unit, GError **err)
{
gchar* s;
gboolean ok;
if (getuid() == 0) {
// special case for root
// XDG_RUNTIME_DIR setup seems to be mandatory for Fedora, may differ for other distros
s = g_strdup_printf("runuser -l %s -c 'XDG_RUNTIME_DIR=/var/run/user/$(id -u) systemctl --user %s %s'", user, operation, unit);
} else {
s = g_strdup_printf("systemctl --user %s %s", operation, unit);
}
ok = g_spawn_command_line_sync(s, NULL, NULL, NULL, err);
g_free(s);
return ok;
}
static gboolean kill_soundserver(struct soundserver* state, int card, GError** err)
{
gchar* fuser = NULL, *fuser2 = NULL;
gchar* s = NULL;
gchar* clientconf = NULL;
gboolean ok;
char *p;
state->type = PULSEAUDIO;
state->was_killed = FALSE;
state->user = NULL;
/* Is PA having a lock on the sound card? */
s = g_strdup_printf("fuser -v /dev/snd/controlC%d", card);
/* Due to some bug in fuser, stdout and stderr output is unclear. Better check both. */
if (!(ok = g_spawn_command_line_sync(s, &fuser, &fuser2, NULL, err)))
goto cleanup;
if (strstr(fuser, "pulseaudio") != NULL || strstr(fuser2, "pulseaudio") != NULL) {
clientconf = get_pulseaudio_client_conf();
if (!(ok = !g_file_test(clientconf, G_FILE_TEST_EXISTS))) {
g_set_error(err, quark(), 0, "Cannot block PulseAudio from respawning:\n"
"Please either remove '%s' or kill PulseAudio manually.", clientconf);
goto cleanup;
}
if (!(ok = g_file_set_contents(clientconf, "autospawn=no\n", -1, err)))
goto cleanup;
state->was_killed = TRUE;
ok = g_spawn_command_line_sync("pulseaudio -k", NULL, NULL, NULL, err);
} else if ((p = strstr(fuser, "wireplumber")) != NULL || (p = strstr(fuser2, "wireplumber")) != NULL) {
*p = '\0';
while (p != fuser && p != fuser2 && *p != '\n')
p--;
if (*p == '\n')
p++;
GRegex *regex;
GMatchInfo *match_info;
regex = g_regex_new (" ([a-zA-Z0-9_-]+) ", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
g_regex_match (regex, p, 0, &match_info);
if (g_match_info_matches (match_info))
state->user = g_match_info_fetch (match_info, 1);
g_match_info_free (match_info);
g_regex_unref (regex);
state->type = PIPEWIRE;
ok = call_systemctl(state->user, "stop", "wireplumber.service", err);
state->was_killed = ok;
} else {
// Sound server not locking the sound card
}
cleanup:
g_free(clientconf);
g_free(fuser);
g_free(fuser2);
g_free(s);
return ok;
}
static gboolean restore_soundserver(struct soundserver* state, GError** err)
{
gboolean ok = FALSE;
gchar* clientconf;
switch (state->type) {
case PULSEAUDIO:
clientconf = get_pulseaudio_client_conf();
if (state->was_killed && g_unlink(clientconf) != 0) {
g_set_error(err, quark(), 0, "%s", g_strerror(errno));
g_free(clientconf);
goto cleanup;
}
g_free(clientconf);
ok = TRUE;
break;
case PIPEWIRE:
if (state->was_killed)
ok = call_systemctl(state->user, "start", "wireplumber.service", err);
else
ok = TRUE;
break;
}
cleanup:
g_free(state->user);
state->user = NULL;
return ok;
}
gboolean apply_changes_reconfig(pin_configs_t* pins, int entries, int card, int device,
const char* model, const char* hints, GError** err)
{
gboolean result = FALSE;
// gchar* script_name = NULL;
struct soundserver state = { 0 };
/* Check for users of the sound card */
/* Kill pulseaudio if necessary (and possible) */
if (!kill_soundserver(&state, card, err))
goto cleanup;
/* Create script */
if (!create_reconfig_script(pins, entries, card, device, model, hints, err))
goto cleanup;
/* Run script as root */
if (!run_sudo_script(scriptfile, err))
goto cleanup;
result = TRUE;
cleanup:
if (!restore_soundserver(&state, result ? err : NULL)) {
result = FALSE;
}
// g_free(script_name);
return result;
}
static gboolean create_firmware_file(pin_configs_t* pins, int entries, int card, int device,
const char* model, const char* hints, GError** err)
{
gboolean ok;
gchar destbuf[40*40+40*24] = "";
gchar* s = destbuf;
gchar* filename = g_strdup_printf("%s/hda-jack-retask.fw", tempdir);
unsigned int address, codec_vendorid, codec_ssid;
int bufleft = sizeof(destbuf);
int l;
get_codec_header(card, device, &address, &codec_vendorid, &codec_ssid);
l = g_snprintf(s, bufleft, "[codec]\n0x%08x 0x%08x %u\n\n[pincfg]\n", codec_vendorid, codec_ssid, address);
bufleft -= l;
s += l;
while (entries) {
l = g_snprintf(s, bufleft, "0x%02x 0x%08x\n", pins->nid, (unsigned int) actual_pin_config(pins));
bufleft -= l;
s += l;
pins++;
entries--;
}
if (model) {
int l = g_snprintf(s, bufleft, "\n[model]\n%s\n", model);
bufleft-=l;
s+=l;
}
if (hints) {
int l = g_snprintf(s, bufleft, "\n[hints]\n%s\n", hints);
bufleft-=l;
s+=l;
}
ok = g_file_set_contents(filename, destbuf, -1, err);
g_free(filename);
return ok;
}
static const gchar* remove_script =
"#!/bin/sh\n"
"rm /etc/modprobe.d/hda-jack-retask.conf 2>>%s\n"
"rm /lib/firmware/hda-jack-retask.fw 2>>%s\n";
static const gchar* retask_conf =
"# This file was added by the program 'hda-jack-retask'.\n"
"# If you want to revert the changes made by this program, you can simply erase this file and reboot your computer.\n"
"options snd-hda-intel patch=hda-jack-retask.fw,hda-jack-retask.fw,hda-jack-retask.fw,hda-jack-retask.fw\n";
static const gchar* install_script =
"#!/bin/sh\n"
"mv %s/hda-jack-retask.fw /lib/firmware/hda-jack-retask.fw\n 2>>%s\n"
"mv %s/hda-jack-retask.conf /etc/modprobe.d/hda-jack-retask.conf 2>>%s\n";
gboolean apply_changes_boot(pin_configs_t* pins, int entries, int card, int device,
const char* model, const char* hints, GError** err)
{
gchar *s;
if (!ensure_tempdir(err))
return FALSE;
if (!create_firmware_file(pins, entries, card, device, model, hints, err))
return FALSE;
/* Create hda-jack-retask.conf */
s = g_strdup_printf("%s/hda-jack-retask.conf", tempdir);
if (!g_file_set_contents(s, retask_conf, -1, err)) {
g_free(s);
return FALSE;
}
g_free(s);
/* Create install script */
s = g_strdup_printf(install_script, tempdir, errorfile, tempdir, errorfile);
if (!g_file_set_contents(scriptfile, s, -1, err)) {
g_free(s);
return FALSE;
}
g_free(s);
/* Run script as root */
if (!run_sudo_script(scriptfile, err))
return FALSE;
return TRUE;
}
gboolean reset_changes_boot(GError** err)
{
gchar *s;
if ((g_file_test("/etc/modprobe.d/hda-jack-retask.conf", G_FILE_TEST_EXISTS) == 0) &&
(g_file_test("/lib/firmware/hda-jack-retask.fw", G_FILE_TEST_EXISTS) == 0))
{
g_set_error(err, quark(), 0, "No boot override is currently installed, nothing to remove.");
return FALSE;
}
if (!ensure_tempdir(err))
return FALSE;
s = g_strdup_printf(remove_script, errorfile, errorfile);
if (!g_file_set_contents(scriptfile, s, -1, err)) {
g_free(s);
return FALSE;
}
g_free(s);
/* Run script as root */
if (!run_sudo_script(scriptfile, err))
return FALSE;
return TRUE;
}