1 | //libroaross.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | * NOTE for everyone want's to change something and send patches: |
---|
24 | * read README and HACKING! There a addition information on |
---|
25 | * the license of this document you need to read before you send |
---|
26 | * any patches. |
---|
27 | * |
---|
28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
29 | * or libpulse*: |
---|
30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
33 | */ |
---|
34 | |
---|
35 | #include "roaraudio.h" |
---|
36 | |
---|
37 | #if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS) |
---|
38 | #if defined(__OpenBSD__) || defined(__NetBSD__) |
---|
39 | #include <soundcard.h> |
---|
40 | #else |
---|
41 | #include <sys/soundcard.h> |
---|
42 | #endif |
---|
43 | #include <sys/ioctl.h> |
---|
44 | |
---|
45 | #ifdef ROAR_HAVE_H_SYS_TYPES |
---|
46 | #include <sys/types.h> |
---|
47 | #endif |
---|
48 | |
---|
49 | #ifdef ROAR_HAVE_H_FCNTL |
---|
50 | #include <fcntl.h> |
---|
51 | #endif |
---|
52 | |
---|
53 | #ifdef ROAR_HAVE_H_UNISTD |
---|
54 | #include <unistd.h> |
---|
55 | #endif |
---|
56 | |
---|
57 | #include <sys/stat.h> |
---|
58 | #include <dlfcn.h> |
---|
59 | |
---|
60 | #if defined(RTLD_NEXT) |
---|
61 | #define REAL_LIBC RTLD_NEXT |
---|
62 | #else |
---|
63 | #define REAL_LIBC ((void *) -1L) |
---|
64 | #endif |
---|
65 | |
---|
66 | #define _MAX_POINTER 8 |
---|
67 | |
---|
68 | struct session { |
---|
69 | int refc; |
---|
70 | struct roar_connection con; |
---|
71 | }; |
---|
72 | |
---|
73 | static struct session _session = {.refc = 0}; |
---|
74 | |
---|
75 | struct handle { |
---|
76 | int refc; // refrence counter |
---|
77 | struct session * session; |
---|
78 | }; |
---|
79 | |
---|
80 | static struct { |
---|
81 | int (*open)(const char *pathname, int flags, mode_t mode); |
---|
82 | int (*close)(int fd); |
---|
83 | ssize_t (*write)(int fd, const void *buf, size_t count); |
---|
84 | ssize_t (*read)(int fd, void *buf, size_t count); |
---|
85 | } _os; |
---|
86 | |
---|
87 | static struct { |
---|
88 | int fh; |
---|
89 | struct handle * handle; |
---|
90 | } _ptr[_MAX_POINTER]; |
---|
91 | |
---|
92 | static void _init_os (void) { |
---|
93 | memset(&_os, 0, sizeof(_os)); |
---|
94 | |
---|
95 | _os.open = dlsym(REAL_LIBC, "open"); |
---|
96 | _os.close = dlsym(REAL_LIBC, "close"); |
---|
97 | _os.write = dlsym(REAL_LIBC, "write"); |
---|
98 | _os.read = dlsym(REAL_LIBC, "read"); |
---|
99 | } |
---|
100 | |
---|
101 | static void _init_ptr (void) { |
---|
102 | int i; |
---|
103 | |
---|
104 | for (i = 0; i < _MAX_POINTER; i++) { |
---|
105 | _ptr[i].fh = -1; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | static void _init (void) { |
---|
110 | static int inited = 0; |
---|
111 | |
---|
112 | if ( !inited ) { |
---|
113 | _init_os(); |
---|
114 | _init_ptr(); |
---|
115 | inited++; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | static int _open_dummy (void) { |
---|
120 | int p[2]; |
---|
121 | |
---|
122 | if ( pipe(p) == -1 ) |
---|
123 | return -1; |
---|
124 | |
---|
125 | close(p[1]); |
---|
126 | |
---|
127 | return p[0]; |
---|
128 | } |
---|
129 | |
---|
130 | static struct session * _open_session (char * server, char * name) { |
---|
131 | if ( _session.refc == 0 ) { |
---|
132 | |
---|
133 | if ( name == NULL ) |
---|
134 | name = "libroaross client"; |
---|
135 | |
---|
136 | if ( roar_simple_connect(&(_session.con), server, name) == -1 ) |
---|
137 | return NULL; |
---|
138 | |
---|
139 | _session.refc++; |
---|
140 | } else { |
---|
141 | _session.refc++; |
---|
142 | return &_session; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | static void _close_session(struct session * session) { |
---|
147 | if ( session == NULL ) |
---|
148 | return; |
---|
149 | |
---|
150 | session->refc--; |
---|
151 | |
---|
152 | if ( session->refc == 0 ) { |
---|
153 | roar_disconnect(&(session->con)); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | // ------------------------------------- |
---|
159 | // emulated functions follow: |
---|
160 | // ------------------------------------- |
---|
161 | |
---|
162 | int open(const char *pathname, int flags, ...) { |
---|
163 | mode_t mode = 0; |
---|
164 | va_list args; |
---|
165 | |
---|
166 | _init(); |
---|
167 | |
---|
168 | if (flags & O_CREAT) { |
---|
169 | va_start(args, flags); |
---|
170 | mode = va_arg(args, mode_t); |
---|
171 | va_end(args); |
---|
172 | } |
---|
173 | |
---|
174 | return _os.open(pathname, flags, mode); |
---|
175 | } |
---|
176 | |
---|
177 | int close(int fd) { |
---|
178 | _init(); |
---|
179 | |
---|
180 | return _os.close(fd); |
---|
181 | } |
---|
182 | |
---|
183 | ssize_t write(int fd, const void *buf, size_t count) { |
---|
184 | _init(); |
---|
185 | |
---|
186 | return _os.write(fd, buf, count); |
---|
187 | } |
---|
188 | |
---|
189 | ssize_t read(int fd, void *buf, size_t count) { |
---|
190 | _init(); |
---|
191 | |
---|
192 | return _os.read(fd, buf, count); |
---|
193 | } |
---|
194 | |
---|
195 | #endif |
---|
196 | |
---|
197 | //ll |
---|