| 1 | /* -*- Mode: C ; c-basic-offset: 2 -*- */ |
|---|
| 2 | /* |
|---|
| 3 | * LADI Session Handler (ladish) |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name> |
|---|
| 6 | * |
|---|
| 7 | ************************************************************************** |
|---|
| 8 | * This file contains implementation of the libasound LD_PRELOAD-ed alsapid library |
|---|
| 9 | ************************************************************************** |
|---|
| 10 | * |
|---|
| 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. |
|---|
| 15 | * |
|---|
| 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. |
|---|
| 20 | * |
|---|
| 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. |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | #define _GNU_SOURCE |
|---|
| 28 | |
|---|
| 29 | #include <alsa/asoundlib.h> |
|---|
| 30 | #include <dlfcn.h> |
|---|
| 31 | #include <linux/limits.h> |
|---|
| 32 | #include <stdio.h> |
|---|
| 33 | |
|---|
| 34 | #include "alsapid.h" |
|---|
| 35 | |
|---|
| 36 | #define API_VERSION "ALSA_0.9" |
|---|
| 37 | |
|---|
| 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 |
|---|
| 40 | |
|---|
| 41 | static void create_symlink(int alsa_client_id) |
|---|
| 42 | { |
|---|
| 43 | char src[PATH_MAX]; |
|---|
| 44 | char dst[PATH_MAX]; |
|---|
| 45 | |
|---|
| 46 | alsapid_compose_src_link(alsa_client_id, src); |
|---|
| 47 | alsapid_compose_dst_link(dst); |
|---|
| 48 | |
|---|
| 49 | //printf("'%s' -> '%s'\n", src, dst); |
|---|
| 50 | if (unlink(src) != 0 && errno != ENOENT) |
|---|
| 51 | { |
|---|
| 52 | fprintf(stderr, "unlink(\"%s\") failed with %d (%s)", src, errno, strerror(errno)); |
|---|
| 53 | } |
|---|
| 54 | if (symlink(dst, src) != 0) |
|---|
| 55 | { |
|---|
| 56 | fprintf(stderr, "symlink(\"%s\", \"%s\") failed with %d (%s)", dst, src, errno, strerror(errno)); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | static void destroy_symlink(int alsa_client_id) |
|---|
| 61 | { |
|---|
| 62 | char src[PATH_MAX]; |
|---|
| 63 | |
|---|
| 64 | alsapid_compose_src_link(alsa_client_id, src); |
|---|
| 65 | |
|---|
| 66 | //printf("'%s'\n", src); |
|---|
| 67 | if (unlink(src) != 0 && errno != ENOENT) |
|---|
| 68 | { |
|---|
| 69 | fprintf(stderr, "unlink(\"%s\") failed with %d (%s)", src, errno, strerror(errno)); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 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); |
|---|
| 78 | |
|---|
| 79 | static void __attribute__ ((constructor)) init(void); |
|---|
| 80 | |
|---|
| 81 | /* Library constructor */ |
|---|
| 82 | void init(void) |
|---|
| 83 | { |
|---|
| 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); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | #define CHECK_FUNC(func) \ |
|---|
| 92 | if (real_ ## func == NULL) \ |
|---|
| 93 | { \ |
|---|
| 94 | fprintf(stderr, "dlvsym(\"" #func "\", \""API_VERSION"\") failed.\n"); \ |
|---|
| 95 | return -1; \ |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | #if 0 |
|---|
| 99 | int snd_seq_open(snd_seq_t ** handle, const char * name, int streams, int mode) |
|---|
| 100 | { |
|---|
| 101 | int ret; |
|---|
| 102 | |
|---|
| 103 | //printf("Attempt to create alsa client '%s', %d streams and mode %d\n", name, streams, mode); |
|---|
| 104 | |
|---|
| 105 | CHECK_FUNC(snd_seq_open); |
|---|
| 106 | |
|---|
| 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) |
|---|
| 110 | { |
|---|
| 111 | //printf("alsa client %p (%d) created\n", *handle, snd_seq_client_id(*handle)); |
|---|
| 112 | } |
|---|
| 113 | return ret; |
|---|
| 114 | } |
|---|
| 115 | #endif |
|---|
| 116 | |
|---|
| 117 | int snd_seq_set_client_name(snd_seq_t * seq, const char * name) |
|---|
| 118 | { |
|---|
| 119 | int ret; |
|---|
| 120 | |
|---|
| 121 | //printf("Attempt to set alsa client name to '%s' of %p\n", name, seq); getchar(); |
|---|
| 122 | |
|---|
| 123 | CHECK_FUNC(snd_seq_set_client_name); |
|---|
| 124 | |
|---|
| 125 | ret = real_snd_seq_set_client_name(seq, name); |
|---|
| 126 | |
|---|
| 127 | //printf("name set?\n"); getchar(); |
|---|
| 128 | |
|---|
| 129 | if (ret == 0) |
|---|
| 130 | { |
|---|
| 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)); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | return ret; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | int snd_seq_close(snd_seq_t * handle) |
|---|
| 139 | { |
|---|
| 140 | CHECK_FUNC(snd_seq_close); |
|---|
| 141 | |
|---|
| 142 | //printf("ALSAPID: pid = %lld CLOSE %d\n", (long long)getpid(), snd_seq_client_id(handle)); |
|---|
| 143 | destroy_symlink(snd_seq_client_id(handle)); |
|---|
| 144 | |
|---|
| 145 | return real_snd_seq_close(handle); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | #if 0 |
|---|
| 149 | int snd_seq_create_port(snd_seq_t * handle, snd_seq_port_info_t * info) |
|---|
| 150 | { |
|---|
| 151 | int ret; |
|---|
| 152 | |
|---|
| 153 | CHECK_FUNC(snd_seq_create_port); |
|---|
| 154 | |
|---|
| 155 | ret = real_snd_seq_create_port(handle, info); |
|---|
| 156 | |
|---|
| 157 | //printf("port created?\n"); getchar(); |
|---|
| 158 | |
|---|
| 159 | return ret; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | int snd_seq_create_simple_port(snd_seq_t * seq, const char * name, unsigned int caps, unsigned int type) |
|---|
| 163 | { |
|---|
| 164 | int ret; |
|---|
| 165 | |
|---|
| 166 | CHECK_FUNC(snd_seq_create_simple_port); |
|---|
| 167 | |
|---|
| 168 | ret = real_snd_seq_create_simple_port(seq, name, caps, type); |
|---|
| 169 | |
|---|
| 170 | //printf("simple port created?\n"); getchar(); |
|---|
| 171 | |
|---|
| 172 | return ret; |
|---|
| 173 | } |
|---|
| 174 | #endif |
|---|