Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * ALSA sequencer Priority Queue
4 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
5 */
6
7#include <linux/time.h>
8#include <linux/slab.h>
9#include <sound/core.h>
10#include "seq_timer.h"
11#include "seq_prioq.h"
12
13
14/* Implementation is a simple linked list for now...
15
16 This priority queue orders the events on timestamp. For events with an
17 equeal timestamp the queue behaves as a FIFO.
18
19 *
20 * +-------+
21 * Head --> | first |
22 * +-------+
23 * |next
24 * +-----v-+
25 * | |
26 * +-------+
27 * |
28 * +-----v-+
29 * | |
30 * +-------+
31 * |
32 * +-----v-+
33 * Tail --> | last |
34 * +-------+
35 *
36
37 */
38
39
40
41/* create new prioq (constructor) */
42struct snd_seq_prioq *snd_seq_prioq_new(void)
43{
44 struct snd_seq_prioq *f;
45
46 f = kzalloc(sizeof(*f), GFP_KERNEL);
47 if (!f)
48 return NULL;
49
50 spin_lock_init(&f->lock);
51 f->head = NULL;
52 f->tail = NULL;
53 f->cells = 0;
54
55 return f;
56}
57
58/* delete prioq (destructor) */
59void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
60{
61 struct snd_seq_prioq *f = *fifo;
62 *fifo = NULL;
63
64 if (f == NULL) {
65 pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n");
66 return;
67 }
68
69 /* release resources...*/
70 /*....................*/
71
72 if (f->cells > 0) {
73 /* drain prioQ */
74 while (f->cells > 0)
75 snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL));
76 }
77
78 kfree(f);
79}
80
81
82
83
84/* compare timestamp between events */
85/* return 1 if a >= b; 0 */
86static inline int compare_timestamp(struct snd_seq_event *a,
87 struct snd_seq_event *b)
88{
89 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
90 /* compare ticks */
91 return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
92 } else {
93 /* compare real time */
94 return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
95 }
96}
97
98/* compare timestamp between events */
99/* return negative if a < b;
100 * zero if a = b;
101 * positive if a > b;
102 */
103static inline int compare_timestamp_rel(struct snd_seq_event *a,
104 struct snd_seq_event *b)
105{
106 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
107 /* compare ticks */
108 if (a->time.tick > b->time.tick)
109 return 1;
110 else if (a->time.tick == b->time.tick)
111 return 0;
112 else
113 return -1;
114 } else {
115 /* compare real time */
116 if (a->time.time.tv_sec > b->time.time.tv_sec)
117 return 1;
118 else if (a->time.time.tv_sec == b->time.time.tv_sec) {
119 if (a->time.time.tv_nsec > b->time.time.tv_nsec)
120 return 1;
121 else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
122 return 0;
123 else
124 return -1;
125 } else
126 return -1;
127 }
128}
129
130/* enqueue cell to prioq */
131int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
132 struct snd_seq_event_cell * cell)
133{
134 struct snd_seq_event_cell *cur, *prev;
135 unsigned long flags;
136 int count;
137 int prior;
138
139 if (snd_BUG_ON(!f || !cell))
140 return -EINVAL;
141
142 /* check flags */
143 prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
144
145 spin_lock_irqsave(&f->lock, flags);
146
147 /* check if this element needs to inserted at the end (ie. ordered
148 data is inserted) This will be very likeley if a sequencer
149 application or midi file player is feeding us (sequential) data */
150 if (f->tail && !prior) {
151 if (compare_timestamp(&cell->event, &f->tail->event)) {
152 /* add new cell to tail of the fifo */
153 f->tail->next = cell;
154 f->tail = cell;
155 cell->next = NULL;
156 f->cells++;
157 spin_unlock_irqrestore(&f->lock, flags);
158 return 0;
159 }
160 }
161 /* traverse list of elements to find the place where the new cell is
162 to be inserted... Note that this is a order n process ! */
163
164 prev = NULL; /* previous cell */
165 cur = f->head; /* cursor */
166
167 count = 10000; /* FIXME: enough big, isn't it? */
168 while (cur != NULL) {
169 /* compare timestamps */
170 int rel = compare_timestamp_rel(&cell->event, &cur->event);
171 if (rel < 0)
172 /* new cell has earlier schedule time, */
173 break;
174 else if (rel == 0 && prior)
175 /* equal schedule time and prior to others */
176 break;
177 /* new cell has equal or larger schedule time, */
178 /* move cursor to next cell */
179 prev = cur;
180 cur = cur->next;
181 if (! --count) {
182 spin_unlock_irqrestore(&f->lock, flags);
183 pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
184 return -EINVAL;
185 }
186 }
187
188 /* insert it before cursor */
189 if (prev != NULL)
190 prev->next = cell;
191 cell->next = cur;
192
193 if (f->head == cur) /* this is the first cell, set head to it */
194 f->head = cell;
195 if (cur == NULL) /* reached end of the list */
196 f->tail = cell;
197 f->cells++;
198 spin_unlock_irqrestore(&f->lock, flags);
199 return 0;
200}
201
202/* return 1 if the current time >= event timestamp */
203static int event_is_ready(struct snd_seq_event *ev, void *current_time)
204{
205 if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK)
206 return snd_seq_compare_tick_time(current_time, &ev->time.tick);
207 else
208 return snd_seq_compare_real_time(current_time, &ev->time.time);
209}
210
211/* dequeue cell from prioq */
212struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
213 void *current_time)
214{
215 struct snd_seq_event_cell *cell;
216 unsigned long flags;
217
218 if (f == NULL) {
219 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
220 return NULL;
221 }
222 spin_lock_irqsave(&f->lock, flags);
223
224 cell = f->head;
225 if (cell && current_time && !event_is_ready(&cell->event, current_time))
226 cell = NULL;
227 if (cell) {
228 f->head = cell->next;
229
230 /* reset tail if this was the last element */
231 if (f->tail == cell)
232 f->tail = NULL;
233
234 cell->next = NULL;
235 f->cells--;
236 }
237
238 spin_unlock_irqrestore(&f->lock, flags);
239 return cell;
240}
241
242/* return number of events available in prioq */
243int snd_seq_prioq_avail(struct snd_seq_prioq * f)
244{
245 if (f == NULL) {
246 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
247 return 0;
248 }
249 return f->cells;
250}
251
252static inline int prioq_match(struct snd_seq_event_cell *cell,
253 int client, int timestamp)
254{
255 if (cell->event.source.client == client ||
256 cell->event.dest.client == client)
257 return 1;
258 if (!timestamp)
259 return 0;
260 switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
261 case SNDRV_SEQ_TIME_STAMP_TICK:
262 if (cell->event.time.tick)
263 return 1;
264 break;
265 case SNDRV_SEQ_TIME_STAMP_REAL:
266 if (cell->event.time.time.tv_sec ||
267 cell->event.time.time.tv_nsec)
268 return 1;
269 break;
270 }
271 return 0;
272}
273
274/* remove cells for left client */
275void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp)
276{
277 register struct snd_seq_event_cell *cell, *next;
278 unsigned long flags;
279 struct snd_seq_event_cell *prev = NULL;
280 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
281
282 /* collect all removed cells */
283 spin_lock_irqsave(&f->lock, flags);
284 cell = f->head;
285 while (cell) {
286 next = cell->next;
287 if (prioq_match(cell, client, timestamp)) {
288 /* remove cell from prioq */
289 if (cell == f->head) {
290 f->head = cell->next;
291 } else {
292 prev->next = cell->next;
293 }
294 if (cell == f->tail)
295 f->tail = cell->next;
296 f->cells--;
297 /* add cell to free list */
298 cell->next = NULL;
299 if (freefirst == NULL) {
300 freefirst = cell;
301 } else {
302 freeprev->next = cell;
303 }
304 freeprev = cell;
305 } else {
306#if 0
307 pr_debug("ALSA: seq: type = %i, source = %i, dest = %i, "
308 "client = %i\n",
309 cell->event.type,
310 cell->event.source.client,
311 cell->event.dest.client,
312 client);
313#endif
314 prev = cell;
315 }
316 cell = next;
317 }
318 spin_unlock_irqrestore(&f->lock, flags);
319
320 /* remove selected cells */
321 while (freefirst) {
322 freenext = freefirst->next;
323 snd_seq_cell_free(freefirst);
324 freefirst = freenext;
325 }
326}
327
328static int prioq_remove_match(struct snd_seq_remove_events *info,
329 struct snd_seq_event *ev)
330{
331 int res;
332
333 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
334 if (ev->dest.client != info->dest.client ||
335 ev->dest.port != info->dest.port)
336 return 0;
337 }
338 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
339 if (! snd_seq_ev_is_channel_type(ev))
340 return 0;
341 /* data.note.channel and data.control.channel are identical */
342 if (ev->data.note.channel != info->channel)
343 return 0;
344 }
345 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
346 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
347 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
348 else
349 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
350 if (!res)
351 return 0;
352 }
353 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
354 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
355 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
356 else
357 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
358 if (res)
359 return 0;
360 }
361 if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
362 if (ev->type != info->type)
363 return 0;
364 }
365 if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
366 /* Do not remove off events */
367 switch (ev->type) {
368 case SNDRV_SEQ_EVENT_NOTEOFF:
369 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
370 return 0;
371 default:
372 break;
373 }
374 }
375 if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
376 if (info->tag != ev->tag)
377 return 0;
378 }
379
380 return 1;
381}
382
383/* remove cells matching remove criteria */
384void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
385 struct snd_seq_remove_events *info)
386{
387 struct snd_seq_event_cell *cell, *next;
388 unsigned long flags;
389 struct snd_seq_event_cell *prev = NULL;
390 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
391
392 /* collect all removed cells */
393 spin_lock_irqsave(&f->lock, flags);
394 cell = f->head;
395
396 while (cell) {
397 next = cell->next;
398 if (cell->event.source.client == client &&
399 prioq_remove_match(info, &cell->event)) {
400
401 /* remove cell from prioq */
402 if (cell == f->head) {
403 f->head = cell->next;
404 } else {
405 prev->next = cell->next;
406 }
407
408 if (cell == f->tail)
409 f->tail = cell->next;
410 f->cells--;
411
412 /* add cell to free list */
413 cell->next = NULL;
414 if (freefirst == NULL) {
415 freefirst = cell;
416 } else {
417 freeprev->next = cell;
418 }
419
420 freeprev = cell;
421 } else {
422 prev = cell;
423 }
424 cell = next;
425 }
426 spin_unlock_irqrestore(&f->lock, flags);
427
428 /* remove selected cells */
429 while (freefirst) {
430 freenext = freefirst->next;
431 snd_seq_cell_free(freefirst);
432 freefirst = freenext;
433 }
434}
435
436
1/*
2 * ALSA sequencer Priority Queue
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/time.h>
23#include <linux/slab.h>
24#include <sound/core.h>
25#include "seq_timer.h"
26#include "seq_prioq.h"
27
28
29/* Implementation is a simple linked list for now...
30
31 This priority queue orders the events on timestamp. For events with an
32 equeal timestamp the queue behaves as a FIFO.
33
34 *
35 * +-------+
36 * Head --> | first |
37 * +-------+
38 * |next
39 * +-----v-+
40 * | |
41 * +-------+
42 * |
43 * +-----v-+
44 * | |
45 * +-------+
46 * |
47 * +-----v-+
48 * Tail --> | last |
49 * +-------+
50 *
51
52 */
53
54
55
56/* create new prioq (constructor) */
57struct snd_seq_prioq *snd_seq_prioq_new(void)
58{
59 struct snd_seq_prioq *f;
60
61 f = kzalloc(sizeof(*f), GFP_KERNEL);
62 if (!f)
63 return NULL;
64
65 spin_lock_init(&f->lock);
66 f->head = NULL;
67 f->tail = NULL;
68 f->cells = 0;
69
70 return f;
71}
72
73/* delete prioq (destructor) */
74void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
75{
76 struct snd_seq_prioq *f = *fifo;
77 *fifo = NULL;
78
79 if (f == NULL) {
80 pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n");
81 return;
82 }
83
84 /* release resources...*/
85 /*....................*/
86
87 if (f->cells > 0) {
88 /* drain prioQ */
89 while (f->cells > 0)
90 snd_seq_cell_free(snd_seq_prioq_cell_out(f));
91 }
92
93 kfree(f);
94}
95
96
97
98
99/* compare timestamp between events */
100/* return 1 if a >= b; 0 */
101static inline int compare_timestamp(struct snd_seq_event *a,
102 struct snd_seq_event *b)
103{
104 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
105 /* compare ticks */
106 return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
107 } else {
108 /* compare real time */
109 return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
110 }
111}
112
113/* compare timestamp between events */
114/* return negative if a < b;
115 * zero if a = b;
116 * positive if a > b;
117 */
118static inline int compare_timestamp_rel(struct snd_seq_event *a,
119 struct snd_seq_event *b)
120{
121 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
122 /* compare ticks */
123 if (a->time.tick > b->time.tick)
124 return 1;
125 else if (a->time.tick == b->time.tick)
126 return 0;
127 else
128 return -1;
129 } else {
130 /* compare real time */
131 if (a->time.time.tv_sec > b->time.time.tv_sec)
132 return 1;
133 else if (a->time.time.tv_sec == b->time.time.tv_sec) {
134 if (a->time.time.tv_nsec > b->time.time.tv_nsec)
135 return 1;
136 else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
137 return 0;
138 else
139 return -1;
140 } else
141 return -1;
142 }
143}
144
145/* enqueue cell to prioq */
146int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
147 struct snd_seq_event_cell * cell)
148{
149 struct snd_seq_event_cell *cur, *prev;
150 unsigned long flags;
151 int count;
152 int prior;
153
154 if (snd_BUG_ON(!f || !cell))
155 return -EINVAL;
156
157 /* check flags */
158 prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
159
160 spin_lock_irqsave(&f->lock, flags);
161
162 /* check if this element needs to inserted at the end (ie. ordered
163 data is inserted) This will be very likeley if a sequencer
164 application or midi file player is feeding us (sequential) data */
165 if (f->tail && !prior) {
166 if (compare_timestamp(&cell->event, &f->tail->event)) {
167 /* add new cell to tail of the fifo */
168 f->tail->next = cell;
169 f->tail = cell;
170 cell->next = NULL;
171 f->cells++;
172 spin_unlock_irqrestore(&f->lock, flags);
173 return 0;
174 }
175 }
176 /* traverse list of elements to find the place where the new cell is
177 to be inserted... Note that this is a order n process ! */
178
179 prev = NULL; /* previous cell */
180 cur = f->head; /* cursor */
181
182 count = 10000; /* FIXME: enough big, isn't it? */
183 while (cur != NULL) {
184 /* compare timestamps */
185 int rel = compare_timestamp_rel(&cell->event, &cur->event);
186 if (rel < 0)
187 /* new cell has earlier schedule time, */
188 break;
189 else if (rel == 0 && prior)
190 /* equal schedule time and prior to others */
191 break;
192 /* new cell has equal or larger schedule time, */
193 /* move cursor to next cell */
194 prev = cur;
195 cur = cur->next;
196 if (! --count) {
197 spin_unlock_irqrestore(&f->lock, flags);
198 pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
199 return -EINVAL;
200 }
201 }
202
203 /* insert it before cursor */
204 if (prev != NULL)
205 prev->next = cell;
206 cell->next = cur;
207
208 if (f->head == cur) /* this is the first cell, set head to it */
209 f->head = cell;
210 if (cur == NULL) /* reached end of the list */
211 f->tail = cell;
212 f->cells++;
213 spin_unlock_irqrestore(&f->lock, flags);
214 return 0;
215}
216
217/* dequeue cell from prioq */
218struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f)
219{
220 struct snd_seq_event_cell *cell;
221 unsigned long flags;
222
223 if (f == NULL) {
224 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
225 return NULL;
226 }
227 spin_lock_irqsave(&f->lock, flags);
228
229 cell = f->head;
230 if (cell) {
231 f->head = cell->next;
232
233 /* reset tail if this was the last element */
234 if (f->tail == cell)
235 f->tail = NULL;
236
237 cell->next = NULL;
238 f->cells--;
239 }
240
241 spin_unlock_irqrestore(&f->lock, flags);
242 return cell;
243}
244
245/* return number of events available in prioq */
246int snd_seq_prioq_avail(struct snd_seq_prioq * f)
247{
248 if (f == NULL) {
249 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
250 return 0;
251 }
252 return f->cells;
253}
254
255
256/* peek at cell at the head of the prioq */
257struct snd_seq_event_cell *snd_seq_prioq_cell_peek(struct snd_seq_prioq * f)
258{
259 if (f == NULL) {
260 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
261 return NULL;
262 }
263 return f->head;
264}
265
266
267static inline int prioq_match(struct snd_seq_event_cell *cell,
268 int client, int timestamp)
269{
270 if (cell->event.source.client == client ||
271 cell->event.dest.client == client)
272 return 1;
273 if (!timestamp)
274 return 0;
275 switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
276 case SNDRV_SEQ_TIME_STAMP_TICK:
277 if (cell->event.time.tick)
278 return 1;
279 break;
280 case SNDRV_SEQ_TIME_STAMP_REAL:
281 if (cell->event.time.time.tv_sec ||
282 cell->event.time.time.tv_nsec)
283 return 1;
284 break;
285 }
286 return 0;
287}
288
289/* remove cells for left client */
290void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp)
291{
292 register struct snd_seq_event_cell *cell, *next;
293 unsigned long flags;
294 struct snd_seq_event_cell *prev = NULL;
295 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
296
297 /* collect all removed cells */
298 spin_lock_irqsave(&f->lock, flags);
299 cell = f->head;
300 while (cell) {
301 next = cell->next;
302 if (prioq_match(cell, client, timestamp)) {
303 /* remove cell from prioq */
304 if (cell == f->head) {
305 f->head = cell->next;
306 } else {
307 prev->next = cell->next;
308 }
309 if (cell == f->tail)
310 f->tail = cell->next;
311 f->cells--;
312 /* add cell to free list */
313 cell->next = NULL;
314 if (freefirst == NULL) {
315 freefirst = cell;
316 } else {
317 freeprev->next = cell;
318 }
319 freeprev = cell;
320 } else {
321#if 0
322 pr_debug("ALSA: seq: type = %i, source = %i, dest = %i, "
323 "client = %i\n",
324 cell->event.type,
325 cell->event.source.client,
326 cell->event.dest.client,
327 client);
328#endif
329 prev = cell;
330 }
331 cell = next;
332 }
333 spin_unlock_irqrestore(&f->lock, flags);
334
335 /* remove selected cells */
336 while (freefirst) {
337 freenext = freefirst->next;
338 snd_seq_cell_free(freefirst);
339 freefirst = freenext;
340 }
341}
342
343static int prioq_remove_match(struct snd_seq_remove_events *info,
344 struct snd_seq_event *ev)
345{
346 int res;
347
348 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
349 if (ev->dest.client != info->dest.client ||
350 ev->dest.port != info->dest.port)
351 return 0;
352 }
353 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
354 if (! snd_seq_ev_is_channel_type(ev))
355 return 0;
356 /* data.note.channel and data.control.channel are identical */
357 if (ev->data.note.channel != info->channel)
358 return 0;
359 }
360 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
361 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
362 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
363 else
364 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
365 if (!res)
366 return 0;
367 }
368 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
369 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
370 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
371 else
372 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
373 if (res)
374 return 0;
375 }
376 if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
377 if (ev->type != info->type)
378 return 0;
379 }
380 if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
381 /* Do not remove off events */
382 switch (ev->type) {
383 case SNDRV_SEQ_EVENT_NOTEOFF:
384 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
385 return 0;
386 default:
387 break;
388 }
389 }
390 if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
391 if (info->tag != ev->tag)
392 return 0;
393 }
394
395 return 1;
396}
397
398/* remove cells matching remove criteria */
399void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
400 struct snd_seq_remove_events *info)
401{
402 struct snd_seq_event_cell *cell, *next;
403 unsigned long flags;
404 struct snd_seq_event_cell *prev = NULL;
405 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
406
407 /* collect all removed cells */
408 spin_lock_irqsave(&f->lock, flags);
409 cell = f->head;
410
411 while (cell) {
412 next = cell->next;
413 if (cell->event.source.client == client &&
414 prioq_remove_match(info, &cell->event)) {
415
416 /* remove cell from prioq */
417 if (cell == f->head) {
418 f->head = cell->next;
419 } else {
420 prev->next = cell->next;
421 }
422
423 if (cell == f->tail)
424 f->tail = cell->next;
425 f->cells--;
426
427 /* add cell to free list */
428 cell->next = NULL;
429 if (freefirst == NULL) {
430 freefirst = cell;
431 } else {
432 freeprev->next = cell;
433 }
434
435 freeprev = cell;
436 } else {
437 prev = cell;
438 }
439 cell = next;
440 }
441 spin_unlock_irqrestore(&f->lock, flags);
442
443 /* remove selected cells */
444 while (freefirst) {
445 freenext = freefirst->next;
446 snd_seq_cell_free(freefirst);
447 freefirst = freenext;
448 }
449}
450
451