source: roaraudio/libroar/vio_select.c @ 3811:49db840fb4f4

Last change on this file since 3811:49db840fb4f4 was 3811:49db840fb4f4, checked in by phi, 14 years ago

fixed some copyright statements

File size: 6.2 KB
Line 
1//vio_select.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010
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   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]);
114   FD_SET(vios[i].internal.fh[0], &rfds);
115   if ( vios[i].internal.fh[0] > max_fh )
116    max_fh = vios[i].internal.fh[0];
117  }
118
119  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
120   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]);
121   FD_SET(vios[i].internal.fh[1], &wfds);
122   if ( vios[i].internal.fh[1] > max_fh )
123    max_fh = vios[i].internal.fh[1];
124  }
125
126  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
127   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]);
128   FD_SET(vios[i].internal.fh[2], &efds);
129   if ( vios[i].internal.fh[2] > max_fh )
130    max_fh = vios[i].internal.fh[2];
131  }
132 }
133
134 // the the select:
135  if ( rtv == NULL ) {
136   tv.tv_sec  = 1024;
137   tv.tv_usec = 0;
138  } else {
139   tv.tv_sec  = rtv->sec;
140   tv.tv_usec = rtv->nsec / 1000;
141  }
142
143
144 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);
145 ret = select(max_fh + 1, &rfds, &wfds, &efds, &tv);
146 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);
147
148 // ret == -1 -> Error
149 // ret ==  0 -> No data
150 if ( ret < 1 ) {
151  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);
152  return ret;
153 }
154
155 // set eventsa:
156 for (i = 0; i < len; i++) {
157  vios[i].eventsa = ROAR_VIO_SELECT_NONE;
158
159  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ )
160   if ( FD_ISSET(vios[i].internal.fh[0], &rfds) )
161    vios[i].eventsa |= ROAR_VIO_SELECT_READ;
162
163  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE )
164   if ( FD_ISSET(vios[i].internal.fh[1], &wfds) )
165    vios[i].eventsa |= ROAR_VIO_SELECT_WRITE;
166
167  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT )
168   if ( FD_ISSET(vios[i].internal.fh[2], &efds) )
169    vios[i].eventsa |= ROAR_VIO_SELECT_EXCEPT;
170 }
171
172 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);
173 return ret;
174}
175
176//ll
Note: See TracBrowser for help on using the repository browser.