source: roaraudio/libroar/vio_select.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

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