source: roaraudio/libroar/vio_jumbo.c @ 5324:d8a11b9bfafb

Last change on this file since 5324:d8a11b9bfafb was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

File size: 4.6 KB
Line 
1//vio_jumbo.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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->inst     = self;
56 calls->close    = roar_vio_jumbo_close;
57 calls->read     = roar_vio_jumbo_read;
58 calls->write    = roar_vio_jumbo_write;
59 calls->lseek    = roar_vio_jumbo_lseek;
60 calls->sync     = roar_vio_jumbo_sync;
61 calls->ctl      = roar_vio_jumbo_ctl;
62
63 return 0;
64}
65
66int     roar_vio_jumbo_close   (struct roar_vio_calls * vio) {
67 struct roar_vio_jumbo * self = vio->inst;
68
69 if ( roar_vio_jumbo_sync(vio) == -1 )
70  return -1;
71
72 roar_buffer_free(self->buffer);
73 roar_mm_free(self);
74
75 return 0;
76}
77
78ssize_t roar_vio_jumbo_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
79 struct roar_vio_jumbo * self = vio->inst;
80
81 return roar_vio_read(self->backend, buf, count);
82}
83
84ssize_t roar_vio_jumbo_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
85 struct roar_vio_jumbo * self = vio->inst;
86 size_t   buflen;
87 void   * data;
88
89 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
90
91 if ( roar_buffer_get_len(self->buffer, &buflen) == -1 )
92  return -1;
93
94 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
95
96 if ( roar_buffer_get_data(self->buffer, &data) == -1 )
97  return -1;
98
99 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
100
101 if ( (self->pos + count) > buflen ) {
102  if ( roar_vio_jumbo_sync(vio) == -1 )
103   return -1;
104
105  // in case we write something that is longer than the buffer
106  if ( count > buflen ) {
107   return roar_vio_write(self->backend, data, count);
108  }
109
110  memcpy(data, buf, count);
111  self->pos = count;
112 } else {
113  memcpy(data + self->pos, buf, count);
114  self->pos += count;
115 }
116
117 ROAR_DBG("roar_vio_jumbo_write(vio=%p, buf=%p, count=%lu) = ?", vio, buf, (unsigned long) count);
118
119 return count;
120}
121
122roar_off_t   roar_vio_jumbo_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
123 struct roar_vio_jumbo * self = vio->inst;
124
125 if ( roar_vio_jumbo_sync(vio) == -1 )
126  return (roar_off_t) -1;
127
128 return roar_vio_lseek(self->backend, offset, whence);
129}
130
131int     roar_vio_jumbo_sync    (struct roar_vio_calls * vio) {
132 struct roar_vio_jumbo * self = vio->inst;
133 void                  * data;
134
135 if ( self->pos == 0 )
136  return 0;
137
138 if ( roar_buffer_get_data(self->buffer, &data) == -1 )
139  return -1;
140
141 // TODO: do this a bit more intelergent (RE?)
142 if ( roar_vio_write(self->backend, data, self->pos) != (ssize_t)self->pos )
143  return -1;
144
145 self->pos = 0;
146
147 return 0;
148}
149
150int     roar_vio_jumbo_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
151 struct roar_vio_jumbo * self = vio->inst;
152
153 switch (cmd) {
154  case ROAR_VIO_CTL_NONBLOCK:
155    return roar_vio_ctl(self->backend, cmd, data);
156   break;
157 }
158
159 roar_err_set(ROAR_ERROR_BADRQC);
160 return -1;
161}
162
163//ll
Note: See TracBrowser for help on using the repository browser.