client-conf: refactor cookie loaders

Signed-off-by: Alexander Couzens <lynxis@fe80.eu>
This commit is contained in:
Alexander Couzens 2013-08-21 13:48:12 +02:00 committed by Tanu Kaskinen
parent bf9b3f0720
commit dac6a74155
3 changed files with 51 additions and 19 deletions

View file

@ -91,20 +91,10 @@ int pa_client_conf_from_x11(pa_client_conf *c, const char *dname) {
}
if (pa_x11_get_prop(xcb, screen, "PULSE_COOKIE", t, sizeof(t))) {
uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
if (pa_parsehex(t, cookie, sizeof(cookie)) != sizeof(cookie)) {
if (pa_client_conf_load_cookie_from_hex(c, t) < 0) {
pa_log(_("Failed to parse cookie data"));
goto finish;
}
pa_assert(sizeof(cookie) == sizeof(c->cookie));
memcpy(c->cookie, cookie, sizeof(cookie));
c->cookie_valid = true;
pa_xfree(c->cookie_file);
c->cookie_file = NULL;
}
ret = 0;

View file

@ -66,6 +66,8 @@ static const pa_client_conf default_conf = {
.auto_connect_display = false
};
static int parse_cookie_file(pa_client_conf* c);
pa_client_conf *pa_client_conf_new(void) {
pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
@ -130,7 +132,7 @@ int pa_client_conf_load(pa_client_conf *c, const char *filename) {
r = f ? pa_config_parse(fn, f, table, NULL, NULL) : 0;
if (!r)
r = pa_client_conf_load_cookie(c);
r = parse_cookie_file(c);
finish:
pa_xfree(fn);
@ -168,16 +170,13 @@ int pa_client_conf_env(pa_client_conf *c) {
}
if ((e = getenv(ENV_COOKIE_FILE))) {
pa_xfree(c->cookie_file);
c->cookie_file = pa_xstrdup(e);
return pa_client_conf_load_cookie(c);
return pa_client_conf_load_cookie_from_file(c, e);
}
return 0;
}
int pa_client_conf_load_cookie(pa_client_conf* c) {
static int parse_cookie_file(pa_client_conf* c) {
int k;
pa_assert(c);
@ -203,3 +202,40 @@ int pa_client_conf_load_cookie(pa_client_conf* c) {
c->cookie_valid = true;
return 0;
}
int pa_client_conf_load_cookie_from_hex(pa_client_conf* c, const char *cookie_in_hex) {
uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
pa_assert(c);
pa_assert(cookie_in_hex);
if (pa_parsehex(cookie_in_hex, cookie, sizeof(cookie)) != sizeof(cookie)) {
pa_log(_("Failed to parse cookie data"));
return -PA_ERR_INVALID;
}
pa_xfree(c->cookie_file);
c->cookie_file = NULL;
return pa_client_conf_set_cookie(c, cookie, PA_NATIVE_COOKIE_LENGTH);
}
int pa_client_conf_load_cookie_from_file(pa_client_conf *c, const char *cookie_file_path) {
pa_assert(c);
pa_assert(cookie_file_path);
pa_xfree(c->cookie_file);
c->cookie_file = pa_xstrdup(cookie_file_path);
return parse_cookie_file(c);
}
int pa_client_conf_set_cookie(pa_client_conf *c, uint8_t *cookie, size_t cookie_size) {
pa_assert(c);
pa_assert(cookie);
if (cookie_size != PA_NATIVE_COOKIE_LENGTH)
return -PA_ERR_INVALID;
memcpy(c->cookie, cookie, cookie_size);
c->cookie_valid = true;
return 0;
}

View file

@ -48,7 +48,13 @@ int pa_client_conf_load(pa_client_conf *c, const char *filename);
process, overwriting the current settings in *c. */
int pa_client_conf_env(pa_client_conf *c);
/* Load cookie data from c->cookie_file into c->cookie */
int pa_client_conf_load_cookie(pa_client_conf* c);
/* Load cookie data from cookie_file_path into c->cookie */
int pa_client_conf_load_cookie_from_file(pa_client_conf *c, const char *cookie_file_path);
/* Load cookie data from hexdecimal string into c->cookie */
int pa_client_conf_load_cookie_from_hex(pa_client_conf *c, const char *cookie_in_hex);
/* Set cookie direct from memory */
int pa_client_conf_set_cookie(pa_client_conf *c, uint8_t *cookie, size_t cookie_size);
#endif