source: roaraudio/libroar/vio_select.c @ 6056:8d4468a24909

Last change on this file since 6056:8d4468a24909 was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

File size: 7.2 KB
Line 
1//vio_select.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2015
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   if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_NAME, &name) != 0 )
74    name = NULL;
75   ROAR_DBG("roar_vio_select(*): vios[%i]'s name is: '%s'", i, name);
76#endif
77
78   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_NONE;
79
80   if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
81    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_READ_FH, &(vios[i].internal.fh[0])) == -1 ) {
82     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
83     vios[i].internal.fh[0]   = -1;
84    } else {
85     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
86    }
87   }
88
89   if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
90    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_WRITE_FH, &(vios[i].internal.fh[1])) == -1 ) {
91     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
92     vios[i].internal.fh[1]   = -1;
93    } else {
94     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
95    }
96   }
97
98   if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
99    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_FH, &(vios[i].internal.fh[2])) == -1 ) {
100     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
101     vios[i].internal.fh[2]   = -1;
102    } else {
103     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
104    }
105   }
106   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]);
107  } else {
108   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_SELECT;
109   vios[i].internal.fh[0]  = vios[i].fh;
110   vios[i].internal.fh[1]  = vios[i].fh;
111   vios[i].internal.fh[2]  = vios[i].fh;
112  }
113 }
114
115 // check:
116 for (i = 0; i < len; i++) {
117  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
118   continue;
119
120  if ( !( vios[i].internal.action == 0 || vios[i].internal.action == ROAR_VIO_SELECT_ACTION_SELECT ) ) {
121   // we currently do not support non-select selects.
122   // TODO: Fix this.
123   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
124   return -1;
125  }
126 }
127
128#ifdef ROAR_HAVE_SELECT
129 // prepaer fdsets:
130 FD_ZERO(&rfds);
131 FD_ZERO(&wfds);
132 FD_ZERO(&efds);
133
134 for (i = 0; i < len; i++) {
135  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
136   continue;
137
138  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
139   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]);
140   FD_SET(vios[i].internal.fh[0], &rfds);
141   if ( vios[i].internal.fh[0] > max_fh )
142    max_fh = vios[i].internal.fh[0];
143  }
144
145  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
146   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]);
147   FD_SET(vios[i].internal.fh[1], &wfds);
148   if ( vios[i].internal.fh[1] > max_fh )
149    max_fh = vios[i].internal.fh[1];
150  }
151
152  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
153   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]);
154   FD_SET(vios[i].internal.fh[2], &efds);
155   if ( vios[i].internal.fh[2] > max_fh )
156    max_fh = vios[i].internal.fh[2];
157  }
158 }
159
160 // the the select:
161  if ( rtv == NULL ) {
162   tv.tv_sec  = 1024;
163   tv.tv_usec = 0;
164  } else {
165   tv.tv_sec  = rtv->sec;
166   tv.tv_usec = rtv->nsec / 1000;
167   if ( tv.tv_sec == 0 && tv.tv_usec == 0 )
168    tv.tv_usec = 1;
169  }
170
171
172 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);
173 ret = select(max_fh + 1, &rfds, &wfds, &efds, &tv);
174 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);
175
176 // ret == -1 -> Error
177 // ret ==  0 -> No data
178 if ( ret < 1 ) {
179  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);
180  return ret;
181 }
182
183 // set eventsa:
184 for (i = 0; i < len; i++) {
185  if ( vios[i].eventsq & ROAR_VIO_SELECT_NO_RETEST )
186   continue;
187
188  vios[i].eventsa = ROAR_VIO_SELECT_NONE;
189
190  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ )
191   if ( FD_ISSET(vios[i].internal.fh[0], &rfds) )
192    vios[i].eventsa |= ROAR_VIO_SELECT_READ;
193
194  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE )
195   if ( FD_ISSET(vios[i].internal.fh[1], &wfds) )
196    vios[i].eventsa |= ROAR_VIO_SELECT_WRITE;
197
198  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT )
199   if ( FD_ISSET(vios[i].internal.fh[2], &efds) )
200    vios[i].eventsa |= ROAR_VIO_SELECT_EXCEPT;
201 }
202
203#else
204 ret = -1;
205#endif
206
207 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);
208 return ret;
209}
210
211//ll
Note: See TracBrowser for help on using the repository browser.