source: roaraudio/libroar/vio_select.c @ 5470:a67fd926e963

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

support new flag ROAR_VIO_SELECT_NO_RETEST

File size: 7.1 KB
Line 
1//vio_select.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
38ssize_t roar_vio_select(struct roar_vio_select * vios, size_t len, struct roar_vio_selecttv * rtv, struct roar_vio_selectctl * ctl) {
39#ifdef ROAR_HAVE_SELECT
40 struct timeval tv;
41 fd_set rfds, wfds, efds;
42#endif
43 size_t i;
44#ifdef ROAR_HAVE_SELECT
45 int max_fh = -1;
46#endif
47 int ret;
48#ifdef DEBUG
49 char * name;
50#endif
51
52 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = ?", vios, (long long unsigned int)len, rtv, ctl);
53
54 if ( len == 0 ) {
55  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = 0", vios, (long long unsigned int)len, rtv, ctl);
56  return 0;
57 }
58
59 if ( vios == NULL ) {
60  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
61  return -1;
62 }
63
64 // pepaer internal structs:
65 for (i = 0; i < len; i++) {
66  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
67   continue;
68
69  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i].fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].fh);
70  if ( vios[i].fh == -1 ) {
71#ifdef DEBUG
72   name = NULL;
73   roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_NAME, &name);
74   ROAR_DBG("roar_vio_select(*): vios[%i]'s name is: '%s'", i, name);
75#endif
76
77   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_NONE;
78
79   if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
80    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_READ_FH, &(vios[i].internal.fh[0])) == -1 ) {
81     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
82     vios[i].internal.fh[0]   = -1;
83    } else {
84     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
85    }
86   }
87
88   if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
89    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_WRITE_FH, &(vios[i].internal.fh[1])) == -1 ) {
90     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
91     vios[i].internal.fh[1]   = -1;
92    } else {
93     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
94    }
95   }
96
97   if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
98    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_FH, &(vios[i].internal.fh[2])) == -1 ) {
99     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
100     vios[i].internal.fh[2]   = -1;
101    } else {
102     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
103    }
104   }
105   ROAR_DBG("roar_vio_select(*): vios[%i].internal.fh[] = {%i, %i, %i}", i, vios[i].internal.fh[0], vios[i].internal.fh[1], vios[i].internal.fh[2]);
106  } else {
107   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_SELECT;
108   vios[i].internal.fh[0]  = vios[i].fh;
109   vios[i].internal.fh[1]  = vios[i].fh;
110   vios[i].internal.fh[2]  = vios[i].fh;
111  }
112 }
113
114 // check:
115 for (i = 0; i < len; i++) {
116  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
117   continue;
118
119  if ( !( vios[i].internal.action == 0 || vios[i].internal.action == ROAR_VIO_SELECT_ACTION_SELECT ) ) {
120   // we currently do not support non-select selects.
121   // TODO: Fix this.
122   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
123   return -1;
124  }
125 }
126
127#ifdef ROAR_HAVE_SELECT
128 // prepaer fdsets:
129 FD_ZERO(&rfds);
130 FD_ZERO(&wfds);
131 FD_ZERO(&efds);
132
133 for (i = 0; i < len; i++) {
134  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
135   continue;
136
137  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
138   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i] is READ, fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].internal.fh[0]);
139   FD_SET(vios[i].internal.fh[0], &rfds);
140   if ( vios[i].internal.fh[0] > max_fh )
141    max_fh = vios[i].internal.fh[0];
142  }
143
144  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
145   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i] is WRITE, fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].internal.fh[1]);
146   FD_SET(vios[i].internal.fh[1], &wfds);
147   if ( vios[i].internal.fh[1] > max_fh )
148    max_fh = vios[i].internal.fh[1];
149  }
150
151  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
152   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i] is EXCEPT, fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].internal.fh[2]);
153   FD_SET(vios[i].internal.fh[2], &efds);
154   if ( vios[i].internal.fh[2] > max_fh )
155    max_fh = vios[i].internal.fh[2];
156  }
157 }
158
159 // the the select:
160  if ( rtv == NULL ) {
161   tv.tv_sec  = 1024;
162   tv.tv_usec = 0;
163  } else {
164   tv.tv_sec  = rtv->sec;
165   tv.tv_usec = rtv->nsec / 1000;
166   if ( tv.tv_sec == 0 && tv.tv_usec == 0 )
167    tv.tv_usec = 1;
168  }
169
170
171 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): Doing select() with max_fh=%i", vios, (long long unsigned int)len, rtv, ctl, max_fh);
172 ret = select(max_fh + 1, &rfds, &wfds, &efds, &tv);
173 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): select() returned %i", vios, (long long unsigned int)len, rtv, ctl, ret);
174
175 // ret == -1 -> Error
176 // ret ==  0 -> No data
177 if ( ret < 1 ) {
178  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);
179  return ret;
180 }
181
182 // set eventsa:
183 for (i = 0; i < len; i++) {
184  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
185   continue;
186
187  vios[i].eventsa = ROAR_VIO_SELECT_NONE;
188
189  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ )
190   if ( FD_ISSET(vios[i].internal.fh[0], &rfds) )
191    vios[i].eventsa |= ROAR_VIO_SELECT_READ;
192
193  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE )
194   if ( FD_ISSET(vios[i].internal.fh[1], &wfds) )
195    vios[i].eventsa |= ROAR_VIO_SELECT_WRITE;
196
197  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT )
198   if ( FD_ISSET(vios[i].internal.fh[2], &efds) )
199    vios[i].eventsa |= ROAR_VIO_SELECT_EXCEPT;
200 }
201
202#else
203 ret = -1;
204#endif
205
206 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);
207 return ret;
208}
209
210//ll
Note: See TracBrowser for help on using the repository browser.