From 4bb14837dd09777e45793bda42512d900c6b500e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 8 Nov 2004 23:48:19 +0000 Subject: implemented pax11publish.c git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@275 fefdeb5f-60dc-0310-8127-8f9354f1896f --- polyp/Makefile.am | 6 +- polyp/authkey.c | 71 +++++++++++--- polyp/authkey.h | 2 + polyp/client-conf.c | 2 +- polyp/daemon-conf.c | 4 +- polyp/pax11publish.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++ polyp/polyplib-simple.c | 8 +- polyp/util.c | 41 ++++++++ polyp/util.h | 1 + 9 files changed, 362 insertions(+), 19 deletions(-) create mode 100644 polyp/pax11publish.c (limited to 'polyp') diff --git a/polyp/Makefile.am b/polyp/Makefile.am index b0b863e5..c1d81062 100644 --- a/polyp/Makefile.am +++ b/polyp/Makefile.am @@ -31,7 +31,7 @@ AM_LDADD=$(PTHREAD_LIBS) -lm AM_LIBADD=$(PTHREAD_LIBS) -lm EXTRA_DIST = default.pa.in daemon.conf.in client.conf.in depmod.py esdcompat.sh.in module-defs.h.m4 -bin_PROGRAMS = polypaudio pacat pactl paplay +bin_PROGRAMS = polypaudio pacat pactl paplay pax11publish bin_SCRIPTS = esdcompat.sh noinst_PROGRAMS = \ mainloop-test \ @@ -441,6 +441,10 @@ parec_simple_SOURCES = parec-simple.c parec_simple_LDADD = $(AM_LDADD) libpolyp-@PA_MAJORMINOR@.la libpolyp-simple-@PA_MAJORMINOR@.la libpolyp-error-@PA_MAJORMINOR@.la libpolyp-mainloop-@PA_MAJORMINOR@.la parec_simple_CFLAGS = $(AM_CFLAGS) +pax11publish_SOURCES = pax11publish.c util.c xmalloc.c log.c authkey.c client-conf.c conf-parser.c +pax11publish_CFLAGS = $(AM_CFLAGS) $(X_CFLAGS) +pax11publish_LDADD = $(AM_LDADD) $(X_PRE_LIBS) -lX11 $(X_LIBS) $(X_EXTRA_LIB) + mainloop_test_SOURCES = mainloop-test.c mainloop_test_CFLAGS = $(AM_CFLAGS) mainloop_test_LDADD = $(AM_LDADD) libpolyp-mainloop-@PA_MAJORMINOR@.la libpolyp-@PA_MAJORMINOR@.la diff --git a/polyp/authkey.c b/polyp/authkey.c index 773484e9..ef395680 100644 --- a/polyp/authkey.c +++ b/polyp/authkey.c @@ -68,7 +68,7 @@ static int generate(int fd, void *data, size_t length) { lseek(fd, 0, SEEK_SET); if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) { - pa_log(__FILE__": failed to write cookie file\n"); + pa_log(__FILE__": failed to write cookie file: %s\n", strerror(errno)); goto finish; } @@ -91,7 +91,7 @@ static int load(const char *fn, void *data, size_t length) { if ((fd = open(fn, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { if (errno != EACCES || (fd = open(fn, O_RDONLY)) < 0) { - pa_log(__FILE__": failed to open cookie file '%s'\n", fn); + pa_log(__FILE__": failed to open cookie file '%s': %s\n", fn, strerror(errno)); goto finish; } else writable = 0; @@ -100,7 +100,7 @@ static int load(const char *fn, void *data, size_t length) { unlock = pa_lock_fd(fd, 1) >= 0; if ((r = pa_loop_read(fd, data, length)) < 0) { - pa_log(__FILE__": failed to read cookie file '%s'\n", fn); + pa_log(__FILE__": failed to read cookie file '%s': %s\n", fn, strerror(errno)); goto finish; } @@ -144,21 +144,28 @@ int pa_authkey_load(const char *path, void *data, size_t length) { return ret; } -int pa_authkey_load_from_home(const char *fn, void *data, size_t length) { - char path[PATH_MAX]; - const char *p; - - assert(fn && data && length); +static const char *normalize_path(const char *fn, char *s, size_t l) { + assert(fn && s && l > 0); if (fn[0] != '/') { char homedir[PATH_MAX]; if (!pa_get_home_dir(homedir, sizeof(homedir))) - return -2; + return NULL; - snprintf(path, sizeof(path), "%s/%s", homedir, fn); - p = path; - } else - p = fn; + snprintf(s, l, "%s/%s", homedir, fn); + return s; + } + + return fn; +} + +int pa_authkey_load_from_home(const char *fn, void *data, size_t length) { + char path[PATH_MAX]; + const char *p; + assert(fn && data && length); + + if (!(p = normalize_path(fn, path, sizeof(path)))) + return -2; return pa_authkey_load(p, data, length); } @@ -171,3 +178,41 @@ int pa_authkey_load_auto(const char *fn, void *data, size_t length) { else return pa_authkey_load_from_home(fn, data, length); } + +int pa_authkey_save(const char *fn, const void *data, size_t length) { + int fd = -1; + int unlock = 0, ret = -1; + ssize_t r; + char path[PATH_MAX]; + const char *p; + assert(fn && data && length); + + if (!(p = normalize_path(fn, path, sizeof(path)))) + return -2; + + if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { + pa_log(__FILE__": failed to open cookie file '%s': %s\n", fn, strerror(errno)); + goto finish; + } + + unlock = pa_lock_fd(fd, 1) >= 0; + + if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) { + pa_log(__FILE__": failed to read cookie file '%s': %s\n", fn, strerror(errno)); + goto finish; + } + + ret = 0; + +finish: + + if (fd >= 0) { + + if (unlock) + pa_lock_fd(fd, 0); + + close(fd); + } + + return ret; +} diff --git a/polyp/authkey.h b/polyp/authkey.h index acdcc24d..2bef3529 100644 --- a/polyp/authkey.h +++ b/polyp/authkey.h @@ -28,4 +28,6 @@ int pa_authkey_load(const char *path, void *data, size_t len); int pa_authkey_load_from_home(const char *fn, void *data, size_t length); int pa_authkey_load_auto(const char *fn, void *data, size_t length); +int pa_authkey_save(const char *path, const void *data, size_t length); + #endif diff --git a/polyp/client-conf.c b/polyp/client-conf.c index 47c5b49c..0f442c99 100644 --- a/polyp/client-conf.c +++ b/polyp/client-conf.c @@ -102,7 +102,7 @@ int pa_client_conf_load(struct pa_client_conf *c, const char *filename) { goto finish; } - r = pa_config_parse(fn, f, table, NULL); + r = f ? pa_config_parse(fn, f, table, NULL) : 0; finish: pa_xfree(fn); diff --git a/polyp/daemon-conf.c b/polyp/daemon-conf.c index e87acd1d..1c6486b7 100644 --- a/polyp/daemon-conf.c +++ b/polyp/daemon-conf.c @@ -182,12 +182,12 @@ int pa_daemon_conf_load(struct pa_daemon_conf *c, const char *filename) { fopen(c->config_file = pa_xstrdup(filename), "r") : pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file); - if (!f && errno != EINTR) { + if (!f && errno != ENOENT) { pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno)); goto finish; } - r = pa_config_parse(c->config_file, f, table, NULL); + r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0; finish: if (f) diff --git a/polyp/pax11publish.c b/polyp/pax11publish.c new file mode 100644 index 00000000..d1391dcf --- /dev/null +++ b/polyp/pax11publish.c @@ -0,0 +1,246 @@ +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + polypaudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include +#include + +#include "util.h" +#include "log.h" +#include "authkey.h" +#include "native-common.h" +#include "client-conf.h" + +static void set_x11_prop(Display *d, const char *name, const char *data) { + Atom a = XInternAtom(d, name, False); + XChangeProperty(d, RootWindow(d, 0), a, XA_STRING, 8, PropModeReplace, (unsigned char*) data, strlen(data)+1); +} + +static void del_x11_prop(Display *d, const char *name) { + Atom a = XInternAtom(d, name, False); + XDeleteProperty(d, RootWindow(d, 0), a); +} + +static char* get_x11_prop(Display *d, const char *name, char *p, size_t l) { + Atom actual_type; + int actual_format; + unsigned long nitems; + unsigned long nbytes_after; + unsigned char *prop = NULL; + char *ret = NULL; + + Atom a = XInternAtom(d, name, False); + if (XGetWindowProperty(d, RootWindow(d, 0), a, 0, (l+2)/4, False, XA_STRING, &actual_type, &actual_format, &nitems, &nbytes_after, &prop) != Success) + goto finish; + + if (actual_type != XA_STRING) + goto finish; + + memcpy(p, prop, nitems); + p[nitems] = 0; + + ret = p; + +finish: + + if (prop) + XFree(prop); + + return ret; +} + +int main(int argc, char *argv[]) { + const char *dname = NULL, *sink = NULL, *source = NULL, *server = NULL, *cookie_file = PA_NATIVE_COOKIE_FILE; + int c, ret = 1; + Display *d = NULL; + enum { DUMP, EXPORT, IMPORT, REMOVE } mode = DUMP; + + while ((c = getopt(argc, argv, "deiD:S:O:I:c:hr")) != -1) { + switch (c) { + case 'D' : + dname = optarg; + break; + case 'h': + printf("%s [-D display] [-S server] [-O sink] [-I source] [-c file] [-d|-e|-i|-r]\n", pa_path_get_filename(argv[0])); + ret = 0; + goto finish; + case 'd': + mode = DUMP; + break; + case 'e': + mode = EXPORT; + break; + case 'i': + mode = IMPORT; + break; + case 'r': + mode = REMOVE; + break; + case 'c': + cookie_file = optarg; + break; + case 'I': + source = optarg; + break; + case 'O': + sink = optarg; + break; + case 'S': + server = optarg; + break; + default: + fprintf(stderr, "Failed to parse command line.\n"); + goto finish; + } + } + + if (!(d = XOpenDisplay(dname))) { + pa_log(__FILE__": XOpenDisplay() failed\n"); + goto finish; + } + + switch (mode) { + case DUMP: { + char t[1024]; + if (!get_x11_prop(d, "POLYP_SERVER", t, sizeof(t))) + goto finish; + + printf("Server: %s\n", t); + if (get_x11_prop(d, "POLYP_SOURCE", t, sizeof(t))) + printf("Source: %s\n", t); + if (get_x11_prop(d, "POLYP_SINK", t, sizeof(t))) + printf("Sink: %s\n", t); + if (get_x11_prop(d, "POLYP_COOKIE", t, sizeof(t))) + printf("Cookie: %s\n", t); + + break; + } + + case IMPORT: { + char t[1024]; + if (!get_x11_prop(d, "POLYP_SERVER", t, sizeof(t))) + goto finish; + + printf("POLYP_SERVER='%s'\nexport POLYP_SERVER\n", t); + + if (get_x11_prop(d, "POLYP_SOURCE", t, sizeof(t))) + printf("POLYP_SOURCE='%s'\nexport POLYP_SOURCE\n", t); + if (get_x11_prop(d, "POLYP_SINK", t, sizeof(t))) + printf("POLYP_SINK='%s'\nexport POLYP_SINK\n", t); + + if (get_x11_prop(d, "POLYP_COOKIE", t, sizeof(t))) { + uint8_t cookie[PA_NATIVE_COOKIE_LENGTH]; + size_t l; + if ((l = pa_parsehex(t, cookie, sizeof(cookie))) == (size_t) -1) { + fprintf(stderr, "Failed to parse cookie data\n"); + goto finish; + } + + if (pa_authkey_save(cookie_file, cookie, l) < 0) { + fprintf(stderr, "Failed to save cookie data\n"); + goto finish; + } + } + + break; + } + + case EXPORT: { + struct pa_client_conf *c = pa_client_conf_new(); + uint8_t cookie[PA_NATIVE_COOKIE_LENGTH]; + char hx[PA_NATIVE_COOKIE_LENGTH*2+1]; + assert(c); + + if (pa_client_conf_load(c, NULL) < 0) { + fprintf(stderr, "Failed to load client configuration file.\n"); + goto finish; + } + + if (pa_client_conf_env(c) < 0) { + fprintf(stderr, "Failed to read environment configuration data.\n"); + goto finish; + } + + del_x11_prop(d, "POLYP_ID"); + + if (server) + set_x11_prop(d, "POLYP_SERVER", c->default_server); + else if (c->default_server) + set_x11_prop(d, "POLYP_SERVER", c->default_server); + else { + char hn[256]; + pa_get_host_name(hn, sizeof(hn)); + set_x11_prop(d, "POLYP_SERVER", hn); + } + + if (sink) + set_x11_prop(d, "POLYP_SINK", sink); + else if (c->default_sink) + set_x11_prop(d, "POLYP_SINK", c->default_sink); + + if (source) + set_x11_prop(d, "POLYP_SOURCE", source); + if (c->default_source) + set_x11_prop(d, "POLYP_SOURCE", c->default_source); + + pa_client_conf_free(c); + + if (pa_authkey_load_auto(cookie_file, cookie, sizeof(cookie)) < 0) { + fprintf(stderr, "Failed to load cookie data\n"); + goto finish; + } + + set_x11_prop(d, "POLYP_COOKIE", pa_hexstr(cookie, sizeof(cookie), hx, sizeof(hx))); + break; + } + + case REMOVE: + del_x11_prop(d, "POLYP_SERVER"); + del_x11_prop(d, "POLYP_SINK"); + del_x11_prop(d, "POLYP_SOURCE"); + del_x11_prop(d, "POLYP_ID"); + del_x11_prop(d, "POLYP_COOKIE"); + break; + + default: + fprintf(stderr, "No yet implemented.\n"); + goto finish; + } + + ret = 0; + +finish: + + if (d) { + XSync(d, False); + XCloseDisplay(d); + } + + return ret; +} diff --git a/polyp/polyplib-simple.c b/polyp/polyplib-simple.c index 0e180af0..aed1ca55 100644 --- a/polyp/polyplib-simple.c +++ b/polyp/polyplib-simple.c @@ -325,8 +325,12 @@ static void latency_complete(struct pa_stream *s, const struct pa_latency_info * if (!l) p->dead = 1; - else - p->latency = l->buffer_usec + l->sink_usec + l->transport_usec; + else { + int negative = 0; + p->latency = pa_stream_get_latency(s, l, &negative); + if (negative) + p->latency = 0; + } } pa_usec_t pa_simple_get_playback_latency(struct pa_simple *p, int *perror) { diff --git a/polyp/util.c b/polyp/util.c index 97b3a26b..b4c16dbb 100644 --- a/polyp/util.c +++ b/polyp/util.c @@ -732,3 +732,44 @@ char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength) { s[j < slength ? j : slength] = 0; return s; } + +/* Convert a hexadecimal digit to a number or -1 if invalid */ +static int hexc(char c) { + if (c >= '0' && c <= '9') + return c - '0'; + + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + + return -1; +} + +/* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */ +size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) { + size_t j = 0; + assert(p && d); + + while (j < dlength && *p) { + int b; + + if ((b = hexc(*(p++))) < 0) + return (size_t) -1; + + d[j] = (uint8_t) (b << 4); + + if (!*p) + return (size_t) -1; + + if ((b = hexc(*(p++))) < 0) + return (size_t) -1; + + d[j] |= (uint8_t) b; + + j++; + } + + return j; +} diff --git a/polyp/util.h b/polyp/util.h index 2b7e6bbe..2448b0fa 100644 --- a/polyp/util.h +++ b/polyp/util.h @@ -82,5 +82,6 @@ int pa_unlock_lockfile(int fd); FILE *pa_open_config_file(const char *env, const char *global, const char *local, char **result); char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength); +size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength); #endif -- cgit