source: roaraudio/libroaross/libroaross.c @ 3138:f61424cdf4fb

Last change on this file since 3138:f61424cdf4fb was 3138:f61424cdf4fb, checked in by phi, 14 years ago

very early start of OSS emulation

File size: 3.7 KB
Line 
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
59#define _MAX_POINTER  8
60
61struct session {
62 int refc;
63 struct roar_connection con;
64};
65
66static struct session _session = {.refc = 0};
67
68struct handle {
69 int refc; // refrence counter
70 struct session * session;
71};
72
73static struct {
74 int     (*open)(const char *pathname, int flags, mode_t mode);
75 int     (*close)(int fd);
76 ssize_t (*write)(int fd, const void *buf, size_t count);
77 ssize_t (*read)(int fd, void *buf, size_t count);
78} _os;
79
80static struct {
81 int fh;
82 struct handle * handle;
83} _ptr[_MAX_POINTER];
84
85static void _init_os (void) {
86 memset(&_os, 0, sizeof(_os));
87}
88
89static void _init_ptr (void) {
90 int i;
91
92 for (i = 0; i < _MAX_POINTER; i++) {
93  _ptr[i].fh = -1;
94 }
95}
96
97static void _init (void) {
98 static int inited = 0;
99
100 if ( !inited ) {
101  _init_os();
102  _init_ptr();
103  inited++;
104 }
105}
106
107static int _open_dummy (void) {
108 int p[2];
109
110 if ( pipe(p) == -1 )
111  return -1;
112
113 close(p[1]);
114
115 return p[0];
116}
117
118static struct session * _open_session (char * server, char * name) {
119 if ( _session.refc == 0 ) {
120
121  if ( name == NULL )
122   name = "libroaross client";
123
124  if ( roar_simple_connect(&(_session.con), server, name) == -1 )
125   return NULL;
126
127  _session.refc++;
128 } else {
129  _session.refc++;
130  return &_session;
131 }
132}
133
134static void _close_session(struct session * session) {
135 if ( session == NULL )
136  return;
137
138 session->refc--;
139
140 if ( session->refc == 0 ) {
141  roar_disconnect(&(session->con));
142 }
143}
144
145
146// -------------------------------------
147// emulated functions follow:
148// -------------------------------------
149
150int     open(const char *pathname, int flags, ...) {
151 mode_t mode = 0;
152 va_list args;
153
154 _init();
155
156 if (flags & O_CREAT) {
157  va_start(args, flags);
158  mode = va_arg(args, mode_t);
159  va_end(args);
160 }
161
162 return _os.open(pathname, flags, mode);
163}
164
165int     close(int fd) {
166 _init();
167
168 return _os.close(fd);
169}
170
171ssize_t write(int fd, const void *buf, size_t count) {
172 _init();
173
174 return _os.write(fd, buf, count);
175}
176
177ssize_t read(int fd, void *buf, size_t count) {
178 _init();
179
180 return _os.read(fd, buf, count);
181}
182
183#endif
184
185//ll
Note: See TracBrowser for help on using the repository browser.