ladishd: project name escaping
[ladish.git] / alsapid / lib.c
blob91fd967a91c55617005ca680c0e7b35e6b3109e7
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the libasound LD_PRELOAD-ed alsapid library
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #define _GNU_SOURCE
29 #include <alsa/asoundlib.h>
30 #include <dlfcn.h>
31 #include <linux/limits.h>
32 #include <stdio.h>
34 #include "alsapid.h"
36 #define API_VERSION "ALSA_0.9"
38 // gcc -g -shared -Wl,--version-script=asound.versions -o libasound.so -fPIC -ldl -Wall -Werror lib.c helper.c
39 // LD_LIBRARY_PATH=alsapid LD_PRELOAD=libasound.so seq24
41 static void create_symlink(int alsa_client_id)
43 char src[PATH_MAX];
44 char dst[PATH_MAX];
46 alsapid_compose_src_link(alsa_client_id, src);
47 alsapid_compose_dst_link(dst);
49 //printf("'%s' -> '%s'\n", src, dst);
50 if (unlink(src) != 0 && errno != ENOENT)
52 fprintf(stderr, "unlink(\"%s\") failed with %d (%s)", src, errno, strerror(errno));
54 if (symlink(dst, src) != 0)
56 fprintf(stderr, "symlink(\"%s\", \"%s\") failed with %d (%s)", dst, src, errno, strerror(errno));
60 static void destroy_symlink(int alsa_client_id)
62 char src[PATH_MAX];
64 alsapid_compose_src_link(alsa_client_id, src);
66 //printf("'%s'\n", src);
67 if (unlink(src) != 0 && errno != ENOENT)
69 fprintf(stderr, "unlink(\"%s\") failed with %d (%s)", src, errno, strerror(errno));
73 //static int (* real_snd_seq_open)(snd_seq_t ** handle, const char * name, int streams, int mode);
74 static int (* real_snd_seq_set_client_name)(snd_seq_t * seq, const char * name);
75 static int (* real_snd_seq_close)(snd_seq_t * handle);
76 //static int (* real_snd_seq_create_port)(snd_seq_t * handle, snd_seq_port_info_t * info);
77 //static int (* real_snd_seq_create_simple_port)(snd_seq_t * seq, const char * name, unsigned int caps, unsigned int type);
79 void __attribute__ ((constructor)) init(void);
81 /* Library constructor */
82 void init(void)
84 // real_snd_seq_open = dlvsym(RTLD_NEXT, "snd_seq_open", API_VERSION);
85 real_snd_seq_set_client_name = dlvsym(RTLD_NEXT, "snd_seq_set_client_name", API_VERSION);
86 real_snd_seq_close = dlvsym(RTLD_NEXT, "snd_seq_close", API_VERSION);
87 // real_snd_seq_create_port = dlvsym(RTLD_NEXT, "snd_seq_create_port", API_VERSION);
88 // real_snd_seq_create_simple_port = dlvsym(RTLD_NEXT, "snd_seq_create_simple_port", API_VERSION);
91 #define CHECK_FUNC(func) \
92 if (real_ ## func == NULL) \
93 { \
94 fprintf(stderr, "dlvsym(\"" #func "\", \""API_VERSION"\") failed.\n"); \
95 return -1; \
98 #if 0
99 int snd_seq_open(snd_seq_t ** handle, const char * name, int streams, int mode)
101 int ret;
103 //printf("Attempt to create alsa client '%s', %d streams and mode %d\n", name, streams, mode);
105 CHECK_FUNC(snd_seq_open);
107 //printf("real_snd_seq_open = %p\n", real_snd_seq_open);
108 ret = real_snd_seq_open(handle, name, streams, mode);
109 if (ret == 0)
111 //printf("alsa client %p (%d) created\n", *handle, snd_seq_client_id(*handle));
113 return ret;
115 #endif
117 int snd_seq_set_client_name(snd_seq_t * seq, const char * name)
119 int ret;
121 //printf("Attempt to set alsa client name to '%s' of %p\n", name, seq); getchar();
123 CHECK_FUNC(snd_seq_set_client_name);
125 ret = real_snd_seq_set_client_name(seq, name);
127 //printf("name set?\n"); getchar();
129 if (ret == 0)
131 //printf("ALSAPID: pid = %lld SETNAME %d '%s'\n", (long long)getpid(), snd_seq_client_id(seq), name);
132 create_symlink(snd_seq_client_id(seq));
135 return ret;
138 int snd_seq_close(snd_seq_t * handle)
140 CHECK_FUNC(snd_seq_close);
142 //printf("ALSAPID: pid = %lld CLOSE %d\n", (long long)getpid(), snd_seq_client_id(handle));
143 destroy_symlink(snd_seq_client_id(handle));
145 return real_snd_seq_close(handle);
148 #if 0
149 int snd_seq_create_port(snd_seq_t * handle, snd_seq_port_info_t * info)
151 int ret;
153 CHECK_FUNC(snd_seq_create_port);
155 ret = real_snd_seq_create_port(handle, info);
157 //printf("port created?\n"); getchar();
159 return ret;
162 int snd_seq_create_simple_port(snd_seq_t * seq, const char * name, unsigned int caps, unsigned int type)
164 int ret;
166 CHECK_FUNC(snd_seq_create_simple_port);
168 ret = real_snd_seq_create_simple_port(seq, name, caps, type);
170 //printf("simple port created?\n"); getchar();
172 return ret;
174 #endif