source: roaraudio/libroar/vio_jumbo.c @ 5388:e5cc8e03a3e1

Last change on this file since 5388:e5cc8e03a3e1 was 5388:e5cc8e03a3e1, checked in by phi, 12 years ago

Added support for refcount based VIOs as well as dynamically allocated VIOs (non-stack or global VIOs) (Closes: #127)

File size: 4.6 KB
Line 
1//vio_jumbo.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38int     roar_vio_open_jumbo    (struct roar_vio_calls * calls, struct roar_vio_calls * vio, size_t buffersize) {
39 struct roar_vio_jumbo * self;
40
41 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_jumbo))) == NULL ) {
42  return -1;
43 }
44
45 memset(self, 0, sizeof(struct roar_vio_jumbo));
46
47 self->backend = vio;
48
49 if ( roar_buffer_new(&(self->buffer), buffersize) == -1 ) {
50  roar_mm_free(self);
51  return -1;
52 }
53
54 memset(calls, 0, sizeof(struct roar_vio_calls));
55 calls->flags    = ROAR_VIO_FLAGS_NONE;
56 calls->refc     = 1;
57 calls->inst     = self;
58 calls->close    = roar_vio_jumbo_close;
59 calls->read     = roar_vio_jumbo_read;
60 calls->write    = roar_vio_jumbo_write;
61 calls->lseek    = roar_vio_jumbo_lseek;
62 calls->sync     = roar_vio_jumbo_sync;
63 calls->ctl      = roar_vio_jumbo_ctl;
64
65 return 0;
66}
67
68int     roar_vio_jumbo_close   (struct roar_vio_calls * vio) {
69 struct roar_vio_jumbo * self = vio->inst;
70
71 if ( roar_vio_jumbo_sync(vio) == -1 )
72  return -1;
73
74 roar_buffer_free(self->buffer);
75 roar_mm_free(self);
76
77 return 0;
78}
79
80ssize_t roar_vio_jumbo_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
81 struct roar_vio_jumbo * self = vio->inst;
82
83 return roar_vio_read(self->backend, buf, count);
84}
85
86ssize_t roar_vio_jumbo_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
87 struct roar_vio_jumbo * self = vio->inst;
88 size_t   buflen;
89 void   * data;
90
91 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
92
93 if ( roar_buffer_get_len(self->buffer, &buflen) == -1 )
94  return -1;
95
96 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
97
98 if ( roar_buffer_get_data(self->buffer, &data) == -1 )
99  return -1;
100
101 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
102
103 if ( (self->pos + count) > buflen ) {
104  if ( roar_vio_jumbo_sync(vio) == -1 )
105   return -1;
106
107  // in case we write something that is longer than the buffer
108  if ( count > buflen ) {
109   return roar_vio_write(self->backend, data, count);
110  }
111
112  memcpy(data, buf, count);
113  self->pos = count;
114 } else {
115  memcpy(data + self->pos, buf, count);
116  self->pos += count;
117 }
118
119 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
120
121 return count;
122}
123
124roar_off_t   roar_vio_jumbo_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
125 struct roar_vio_jumbo * self = vio->inst;
126
127 if ( roar_vio_jumbo_sync(vio) == -1 )
128  return (roar_off_t) -1;
129
130 return roar_vio_lseek(self->backend, offset, whence);
131}
132
133int     roar_vio_jumbo_sync    (struct roar_vio_calls * vio) {
134 struct roar_vio_jumbo * self = vio->inst;
135 void                  * data;
136
137 if ( self->pos == 0 )
138  return 0;
139
140 if ( roar_buffer_get_data(self->buffer, &data) == -1 )
141  return -1;
142
143 // TODO: do this a bit more intelergent (RE?)
144 if ( roar_vio_write(self->backend, data, self->pos) != (ssize_t)self->pos )
145  return -1;
146
147 self->pos = 0;
148
149 return 0;
150}
151
152int     roar_vio_jumbo_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
153 struct roar_vio_jumbo * self = vio->inst;
154
155 switch (cmd) {
156  case ROAR_VIO_CTL_NONBLOCK:
157    return roar_vio_ctl(self->backend, cmd, data);
158   break;
159 }
160
161 roar_err_set(ROAR_ERROR_BADRQC);
162 return -1;
163}
164
165//ll
Note: See TracBrowser for help on using the repository browser.