source: roaraudio/libroar/vio_select.c @ 3767:9bfa5f13666f

Last change on this file since 3767:9bfa5f13666f was 3767:9bfa5f13666f, checked in by phi, 14 years ago

added select like feature for VIOs

File size: 5.3 KB
Line 
1//vio_select.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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
38ssize_t roar_vio_select(struct roar_vio_select * vios, size_t len, struct roar_vio_selecttv * rtv, struct roar_vio_selectctl * ctl) {
39 struct timeval tv;
40 size_t i;
41 int max_fh = -1;
42 int ret;
43 fd_set rfds, wfds, efds;
44
45 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = ?", vios, (long long unsigned int)len, rtv, ctl);
46
47 if ( len == 0 ) {
48  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = 0", vios, (long long unsigned int)len, rtv, ctl);
49  return 0;
50 }
51
52 if ( vios == NULL ) {
53  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
54  return -1;
55 }
56
57 // pepaer interl structs:
58 for (i = 0; i < len; i++) {
59  if ( vios[i].fh == -1 ) {
60   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_NONE;
61
62   if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
63    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_READ_FH, &(vios[i].internal.fh[0])) == -1 ) {
64     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
65     vios[i].internal.fh[0]   = -1;
66    } else {
67     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
68    }
69   }
70
71   if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
72    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_WRITE_FH, &(vios[i].internal.fh[1])) == -1 ) {
73     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
74     vios[i].internal.fh[1]   = -1;
75    } else {
76     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
77    }
78   }
79
80   if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
81    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_FH, &(vios[i].internal.fh[2])) == -1 ) {
82     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
83     vios[i].internal.fh[2]   = -1;
84    } else {
85     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
86    }
87   }
88  } else {
89   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_SELECT;
90   vios[i].internal.fh[0]  = vios[i].fh;
91   vios[i].internal.fh[1]  = vios[i].fh;
92   vios[i].internal.fh[2]  = vios[i].fh;
93  }
94 }
95
96 // check:
97 for (i = 0; i < len; i++) {
98  if ( !( vios[i].internal.action == 0 || vios[i].internal.action == ROAR_VIO_SELECT_ACTION_SELECT ) ) {
99   // we currently do not support non-select selects.
100   // TODO: Fix this.
101   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
102   return -1;
103  }
104 }
105
106 // prepaer fdsets:
107 FD_ZERO(&rfds);
108 FD_ZERO(&wfds);
109 FD_ZERO(&efds);
110
111 for (i = 0; i < len; i++) {
112  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
113   FD_SET(vios[i].internal.fh[0], &rfds);
114   if ( vios[i].internal.fh[0] > max_fh )
115    max_fh = vios[i].internal.fh[0];
116  }
117
118  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
119   FD_SET(vios[i].internal.fh[1], &wfds);
120   if ( vios[i].internal.fh[1] > max_fh )
121    max_fh = vios[i].internal.fh[1];
122  }
123
124  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
125   FD_SET(vios[i].internal.fh[2], &efds);
126   if ( vios[i].internal.fh[2] > max_fh )
127    max_fh = vios[i].internal.fh[2];
128  }
129 }
130
131 // the the select:
132  if ( rtv == NULL ) {
133   tv.tv_sec  = 1024;
134   tv.tv_usec = 0;
135  } else {
136   tv.tv_sec  = rtv->sec;
137   tv.tv_usec = rtv->nsec / 1000;
138  }
139
140 ret = select(max_fh + 1, &rfds, &wfds, &efds, &tv);
141
142 // ret == -1 -> Error
143 // ret ==  0 -> No data
144 if ( ret < 1 )
145  return ret;
146
147 // set eventsa:
148 for (i = 0; i < len; i++) {
149  vios[i].eventsa = ROAR_VIO_SELECT_NONE;
150
151  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ )
152   if ( FD_ISSET(vios[i].internal.fh[0], &rfds) )
153    vios[i].eventsa |= ROAR_VIO_SELECT_READ;
154
155  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE )
156   if ( FD_ISSET(vios[i].internal.fh[1], &wfds) )
157    vios[i].eventsa |= ROAR_VIO_SELECT_WRITE;
158
159  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT )
160   if ( FD_ISSET(vios[i].internal.fh[2], &efds) )
161    vios[i].eventsa |= ROAR_VIO_SELECT_EXCEPT;
162 }
163
164 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = %lli", vios, (long long unsigned int)len, rtv, ctl, (long long int)ret);
165 return ret;
166}
167
168//ll
Note: See TracBrowser for help on using the repository browser.