OSDN Git Service

df2a5f409a30457566d10134a78295be1f702178
[android-x86/external-alsa-lib.git] / src / seq / seq.c
1 /**
2  * \file seq/seq.c
3  * \brief Sequencer Interface
4  * \author Jaroslav Kysela <perex@suse.cz>
5  * \author Abramo Bagnara <abramo@alsa-project.org>
6  * \author Takashi Iwai <tiwai@suse.de>
7  * \date 2000-2001
8  *
9  * See \ref seq page for more details.
10  */
11
12 /* 
13  *  Sequencer Interface - main file
14  *
15  *   This library is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU Lesser General Public License as
17  *   published by the Free Software Foundation; either version 2.1 of
18  *   the License, or (at your option) any later version.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   GNU Lesser General Public License for more details.
24  *
25  *   You should have received a copy of the GNU Lesser General Public
26  *   License along with this library; if not, write to the Free Software
27  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  *
29  */
30
31 /*! \page seq Sequencer interface
32
33 <P>Description...
34
35 */
36
37 #include <dlfcn.h>
38 #include <sys/poll.h>
39 #include "seq_local.h"
40
41 /****************************************************************************
42  *                                                                          *
43  *                                seq.h                                     *
44  *                              Sequencer                                   *
45  *                                                                          *
46  ****************************************************************************/
47
48 /**
49  * \brief get identifier of sequencer handle
50  * \param seq sequencer handle
51  * \return ascii identifier of sequencer handle
52  *
53  * Returns the ASCII identifier of the given sequencer handle. It's the same
54  * identifier specified in snd_seq_open().
55  */
56 const char *snd_seq_name(snd_seq_t *seq)
57 {
58         assert(seq);
59         return seq->name;
60 }
61
62 /**
63  * \brief get type of sequencer handle
64  * \param seq sequencer handle
65  * \return type of sequencer handle
66  *
67  * Returns the type #snd_seq_type_t of the given sequencer handle.
68  */
69 snd_seq_type_t snd_seq_type(snd_seq_t *seq)
70 {
71         assert(seq);
72         return seq->type;
73 }
74
75 static int snd_seq_open_conf(snd_seq_t **seqp, const char *name,
76                              snd_config_t *seq_root, snd_config_t *seq_conf,
77                              int streams, int mode)
78 {
79         const char *str;
80         char buf[256];
81         int err;
82         snd_config_t *conf, *type_conf = NULL;
83         snd_config_iterator_t i, next;
84         const char *id;
85         const char *lib = NULL, *open_name = NULL;
86         int (*open_func)(snd_seq_t **, const char *,
87                          snd_config_t *, snd_config_t *, 
88                          int, int) = NULL;
89 #ifndef PIC
90         extern void *snd_seq_open_symbols(void);
91 #endif
92         void *h;
93         if (snd_config_get_type(seq_conf) != SND_CONFIG_TYPE_COMPOUND) {
94                 if (name)
95                         SNDERR("Invalid type for SEQ %s definition", name);
96                 else
97                         SNDERR("Invalid type for SEQ definition");
98                 return -EINVAL;
99         }
100         err = snd_config_search(seq_conf, "type", &conf);
101         if (err < 0) {
102                 SNDERR("type is not defined");
103                 return err;
104         }
105         err = snd_config_get_id(conf, &id);
106         if (err < 0) {
107                 SNDERR("unable to get id");
108                 return err;
109         }
110         err = snd_config_get_string(conf, &str);
111         if (err < 0) {
112                 SNDERR("Invalid type for %s", id);
113                 return err;
114         }
115         err = snd_config_search_definition(seq_root, "seq_type", str, &type_conf);
116         if (err >= 0) {
117                 if (snd_config_get_type(type_conf) != SND_CONFIG_TYPE_COMPOUND) {
118                         SNDERR("Invalid type for SEQ type %s definition", str);
119                         goto _err;
120                 }
121                 snd_config_for_each(i, next, type_conf) {
122                         snd_config_t *n = snd_config_iterator_entry(i);
123                         const char *id;
124                         if (snd_config_get_id(n, &id) < 0)
125                                 continue;
126                         if (strcmp(id, "comment") == 0)
127                                 continue;
128                         if (strcmp(id, "lib") == 0) {
129                                 err = snd_config_get_string(n, &lib);
130                                 if (err < 0) {
131                                         SNDERR("Invalid type for %s", id);
132                                         goto _err;
133                                 }
134                                 continue;
135                         }
136                         if (strcmp(id, "open") == 0) {
137                                 err = snd_config_get_string(n, &open_name);
138                                 if (err < 0) {
139                                         SNDERR("Invalid type for %s", id);
140                                         goto _err;
141                                 }
142                                 continue;
143                         }
144                         SNDERR("Unknown field %s", id);
145                         err = -EINVAL;
146                         goto _err;
147                 }
148         }
149         if (!open_name) {
150                 open_name = buf;
151                 snprintf(buf, sizeof(buf), "_snd_seq_%s_open", str);
152         }
153 #ifndef PIC
154         snd_seq_open_symbols();
155 #endif
156         h = snd_dlopen(lib, RTLD_NOW);
157         if (h)
158                 open_func = snd_dlsym(h, open_name, SND_DLSYM_VERSION(SND_SEQ_DLSYM_VERSION));
159         err = 0;
160         if (!h) {
161                 SNDERR("Cannot open shared library %s", lib);
162                 err = -ENOENT;
163         } else if (!open_func) {
164                 SNDERR("symbol %s is not defined inside %s", open_name, lib);
165                 snd_dlclose(h);
166                 err = -ENXIO;
167         }
168        _err:
169         if (type_conf)
170                 snd_config_delete(type_conf);
171         return err >= 0 ? open_func(seqp, name, seq_root, seq_conf, streams, mode) : err;
172 }
173
174 static int snd_seq_open_noupdate(snd_seq_t **seqp, snd_config_t *root,
175                                  const char *name, int streams, int mode)
176 {
177         int err;
178         snd_config_t *seq_conf;
179         err = snd_config_search_definition(root, "seq", name, &seq_conf);
180         if (err < 0) {
181                 SNDERR("Unknown SEQ %s", name);
182                 return err;
183         }
184         err = snd_seq_open_conf(seqp, name, root, seq_conf, streams, mode);
185         snd_config_delete(seq_conf);
186         return err;
187 }
188
189
190 /**
191  * \brief Open the ALSA sequencer
192  *
193  * \param seqp Pointer to a snd_seq_t pointer.  This pointer must be
194  * kept and passed to most of the other sequencer functions.
195  * \param name The sequencer's "name".  This is \em not a name you make
196  * up for your own purposes; it has special significance to the ALSA
197  * library.  Usually you need to pass \c "default" here.
198  * \param streams The read/write mode of the sequencer.  Can be one of
199  * three values:
200  * - #SND_SEQ_OPEN_OUTPUT - open the sequencer for output only
201  * - #SND_SEQ_OPEN_INPUT - open the sequencer for input only
202  * - #SND_SEQ_OPEN_DUPLEX - open the sequencer for output and input
203  * \note Internally, these are translated to \c O_WRONLY, \c O_RDONLY and
204  * \O_RDWR respectively and used as the second argument to the C library
205  * open() call.
206  * \param mode Optional modifier.  Can be either 0, or
207  * #SND_SEQ_NONBLOCK, which will make read/write operations
208  * non-blocking.  This can also be set later using #snd_seq_nonblock().
209  * \return 0 on success otherwise a negative error code
210  *
211  * Creates a new handle and opens a connection to the kernel
212  * sequencer interface.
213  * After a client is created successfully, an event
214  * with #SND_SEQ_EVENT_CLIENT_START is broadcast to announce port.
215  */
216 int snd_seq_open(snd_seq_t **seqp, const char *name, 
217                  int streams, int mode)
218 {
219         int err;
220         assert(seqp && name);
221         err = snd_config_update();
222         if (err < 0)
223                 return err;
224         return snd_seq_open_noupdate(seqp, snd_config, name, streams, mode);
225 }
226
227 /**
228  * \brief Open the ALSA sequencer using local configuration
229  *
230  * \param seqp Pointer to a snd_seq_t pointer.
231  * \param streams The read/write mode of the sequencer.
232  * \param mode Optional modifier
233  * \param lconf Local configuration
234  * \return 0 on success otherwise a negative error code
235  *
236  * See the snd_seq_open() function for further details. The extension
237  * is that the given configuration is used to resolve abstract name.
238  */
239 int snd_seq_open_lconf(snd_seq_t **seqp, const char *name, 
240                        int streams, int mode, snd_config_t *lconf)
241 {
242         assert(seqp && name && lconf);
243         return snd_seq_open_noupdate(seqp, lconf, name, streams, mode);
244 }
245
246 /**
247  * \brief Close the sequencer
248  * \param handle Handle returned from #snd_seq_open()
249  * \return 0 on success otherwise a negative error code
250  *
251  * Closes the sequencer client and releases its resources.
252  * After a client is closed, an event with
253  * #SND_SEQ_EVENT_CLIENT_EXIT is broadcast to announce port.
254  * The connection between other clients are disconnected.
255  * Call this just before exiting your program.
256  */
257 int snd_seq_close(snd_seq_t *seq)
258 {
259         int err;
260         assert(seq);
261         err = seq->ops->close(seq);
262         if (err < 0)
263                 return err;
264         if (seq->obuf)
265                 free(seq->obuf);
266         if (seq->ibuf)
267                 free(seq->ibuf);
268         if (seq->tmpbuf)
269                 free(seq->tmpbuf);
270         if (seq->name)
271                 free(seq->name);
272         free(seq);
273         return 0;
274 }
275
276 /**
277  * \brief Returns the number of poll descriptors
278  * \param seq sequencer handle
279  * \param events the poll events to be checked (\c POLLIN and \c POLLOUT)
280  * \return the number of poll descriptors.
281  *
282  * Get the number of poll descriptors.  The polling events to be checked
283  * can be specified by the second argument.  When both input and output
284  * are checked, pass \c POLLIN|POLLOUT
285  */
286 int snd_seq_poll_descriptors_count(snd_seq_t *seq, short events)
287 {
288         int result = 0;
289         assert(seq);
290         if (events & POLLIN) {
291                 assert(seq->streams & SND_SEQ_OPEN_INPUT);
292                 result++;
293         }
294         if (events & POLLOUT) {
295                 assert(seq->streams & SND_SEQ_OPEN_OUTPUT);
296                 result++;
297         }
298         return result ? 1 : 0;
299 }
300
301 /**
302  * \brief Get poll descriptors
303  * \param seq sequencer handle
304  * \param pfds array of poll descriptors
305  * \param space space in the poll descriptor array
306  * \param events polling events to be checked (\c POLLIN and \c POLLOUT)
307  * \return count of filled descriptors
308  *
309  * Get poll descriptors assigned to the sequencer handle.
310  */
311 int snd_seq_poll_descriptors(snd_seq_t *seq, struct pollfd *pfds, unsigned int space, short events)
312 {
313         short revents = 0;
314
315         assert(seq);
316         if ((events & POLLIN) && space >= 1) {
317                 assert(seq->streams & SND_SEQ_OPEN_INPUT);
318                 revents |= POLLIN|POLLERR;
319         }
320         if ((events & POLLOUT) && space >= 1) {
321                 assert(seq->streams & SND_SEQ_OPEN_INPUT);
322                 revents |= POLLOUT|POLLERR;
323         }
324         if (!revents)
325                 return 0;
326         pfds->fd = seq->poll_fd;
327         pfds->events = revents;
328         return 1;
329 }
330
331 /**
332  * \brief get returned events from poll descriptors
333  * \param seq sequencer handle
334  * \param pfds array of poll descriptors
335  * \param nfds count of poll descriptors
336  * \param revents returned events
337  * \return zero if success, otherwise a negative error code
338  */
339 int snd_seq_poll_descriptors_revents(snd_seq_t *seq, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
340 {
341         assert(seq && pfds && revents);
342         if (nfds == 1) {
343                 *revents = pfds->revents;
344                 return 0;
345         }
346         return -EINVAL;
347 }
348
349 /**
350  * \brief Set nonblock mode
351  * \param seq sequencer handle
352  * \param nonblock 0 = block, 1 = nonblock mode
353  * \return 0 on success otherwise a negative error code
354  *
355  * Change the blocking mode of the given client.
356  * In block mode, the client falls into sleep when it fills the
357  * output memory pool with full events.  The client will be woken up
358  * after a certain amount of free space becomes available.
359  */
360 int snd_seq_nonblock(snd_seq_t *seq, int nonblock)
361 {
362         int err;
363         assert(seq);
364         err = seq->ops->nonblock(seq, nonblock);
365         if (err < 0)
366                 return err;
367         if (nonblock)
368                 seq->mode |= SND_SEQ_NONBLOCK;
369         else
370                 seq->mode &= ~SND_SEQ_NONBLOCK;
371         return 0;
372 }
373
374 /**
375  * \brief Get the client id
376  * \param seq sequencer handle
377  * \return the client id
378  *
379  * Returns the id of the specified client.
380  * If an error occurs, function returns the negative error code.
381  * A client id is necessary to inquiry or to set the client information.
382  * A user client is assigned from 128 to 191.
383  */
384 int snd_seq_client_id(snd_seq_t *seq)
385 {
386         assert(seq);
387         return seq->client;
388 }
389
390 /**
391  * \brief Return the size of output buffer
392  * \param seq sequencer handle
393  * \return the size of output buffer in bytes
394  *
395  * Obtains the size of output buffer.
396  * This buffer is used to store decoded byte-stream of output events
397  * before transferring to sequencer.
398  */
399 size_t snd_seq_get_output_buffer_size(snd_seq_t *seq)
400 {
401         assert(seq);
402         if (!seq->obuf)
403                 return 0;
404         return seq->obufsize;
405 }
406
407 /**
408  * \brief Return the size of input buffer
409  * \param seq sequencer handle
410  * \return the size of input buffer in bytes
411  *
412  * Obtains the size of input buffer.
413  * This buffer is used to read byte-stream of input events from sequencer.
414  */
415 size_t snd_seq_get_input_buffer_size(snd_seq_t *seq)
416 {
417         assert(seq);
418         if (!seq->ibuf)
419                 return 0;
420         return seq->ibufsize * sizeof(snd_seq_event_t);
421 }
422
423 /**
424  * \brief Change the size of output buffer
425  * \param seq sequencer handle
426  * \param size the size of output buffer to be changed in bytes
427  * \return 0 on success otherwise a negative error code
428  *
429  * Changes the size of output buffer.
430  */
431 int snd_seq_set_output_buffer_size(snd_seq_t *seq, size_t size)
432 {
433         assert(seq && seq->obuf);
434         assert(size >= sizeof(snd_seq_event_t));
435         snd_seq_drop_output(seq);
436         if (size != seq->obufsize) {
437                 char *newbuf;
438                 newbuf = calloc(1, size);
439                 if (newbuf == NULL)
440                         return -ENOMEM;
441                 free(seq->obuf);
442                 seq->obuf = newbuf;
443                 seq->obufsize = size;
444         }
445         return 0;
446 }
447
448 /**
449  * \brief Resize the input buffer
450  * \param seq sequencer handle
451  * \param size the size of input buffer to be changed in bytes
452  * \return 0 on success otherwise a negative error code
453  *
454  * Changes the size of input buffer.
455  */
456 int snd_seq_set_input_buffer_size(snd_seq_t *seq, size_t size)
457 {
458         assert(seq && seq->ibuf);
459         assert(size >= sizeof(snd_seq_event_t));
460         snd_seq_drop_input(seq);
461         size = (size + sizeof(snd_seq_event_t) - 1) / sizeof(snd_seq_event_t);
462         if (size != seq->ibufsize) {
463                 snd_seq_event_t *newbuf;
464                 newbuf = calloc(sizeof(snd_seq_event_t), size);
465                 if (newbuf == NULL)
466                         return -ENOMEM;
467                 free(seq->ibuf);
468                 seq->ibuf = newbuf;
469                 seq->ibufsize = size;
470         }
471         return 0;
472 }
473
474
475 /**
476  * \brief Get size of #snd_seq_system_info_t
477  * \return size in bytes
478  */
479 size_t snd_seq_system_info_sizeof()
480 {
481         return sizeof(snd_seq_system_info_t);
482 }
483
484 /**
485  * \brief Allocate an empty #snd_seq_system_info_t using standard malloc
486  * \param ptr returned pointer
487  * \return 0 on success otherwise negative error code
488  */
489 int snd_seq_system_info_malloc(snd_seq_system_info_t **ptr)
490 {
491         assert(ptr);
492         *ptr = calloc(1, sizeof(snd_seq_system_info_t));
493         if (!*ptr)
494                 return -ENOMEM;
495         return 0;
496 }
497
498 /**
499  * \brief Frees a previously allocated #snd_seq_system_info_t
500  * \param pointer to object to free
501  */
502 void snd_seq_system_info_free(snd_seq_system_info_t *obj)
503 {
504         free(obj);
505 }
506
507 /**
508  * \brief Copy one #snd_seq_system_info_t to another
509  * \param dst pointer to destination
510  * \param src pointer to source
511  */
512 void snd_seq_system_info_copy(snd_seq_system_info_t *dst, const snd_seq_system_info_t *src)
513 {
514         assert(dst && src);
515         *dst = *src;
516 }
517
518
519 /**
520  * \brief Get maximum number of queues
521  * \param info #snd_seq_system_info_t container
522  * \return maximum number of queues
523  */
524 int snd_seq_system_info_get_queues(const snd_seq_system_info_t *info)
525 {
526         assert(info);
527         return info->queues;
528 }
529
530 /**
531  * \brief Get maximum number of clients
532  * \param info #snd_seq_system_info_t container
533  * \return maximum number of clients
534  */
535 int snd_seq_system_info_get_clients(const snd_seq_system_info_t *info)
536 {
537         assert(info);
538         return info->clients;
539 }
540
541 /**
542  * \brief Get maximum number of ports
543  * \param info #snd_seq_system_info_t container
544  * \return maximum number of ports
545  */
546 int snd_seq_system_info_get_ports(const snd_seq_system_info_t *info)
547 {
548         assert(info);
549         return info->ports;
550 }
551
552 /**
553  * \brief Get maximum number of channels
554  * \param info #snd_seq_system_info_t container
555  * \return maximum number of channels
556  */
557 int snd_seq_system_info_get_channels(const snd_seq_system_info_t *info)
558 {
559         assert(info);
560         return info->channels;
561 }
562
563 /**
564  * \brief Get the current number of clients
565  * \param info #snd_seq_system_info_t container
566  * \return current number of clients
567  */
568 int snd_seq_system_info_get_cur_clients(const snd_seq_system_info_t *info)
569 {
570         assert(info);
571         return info->cur_clients;
572 }
573
574 /**
575  * \brief Get the current number of queues
576  * \param info #snd_seq_system_info_t container
577  * \return current number of queues
578  */
579 int snd_seq_system_info_get_cur_queues(const snd_seq_system_info_t *info)
580 {
581         assert(info);
582         return info->cur_queues;
583 }
584
585 /**
586  * \brief obtain the sequencer system information
587  * \param seq sequencer handle
588  * \param info the pointer to be stored
589  * \return 0 on success otherwise a negative error code
590  *
591  * Stores the global system information of ALSA sequencer system.
592  * The returned data contains
593  * the maximum available numbers of queues, clients, ports and channels.
594  */
595 int snd_seq_system_info(snd_seq_t *seq, snd_seq_system_info_t * info)
596 {
597         assert(seq && info);
598         return seq->ops->system_info(seq, info);
599 }
600
601
602 /*----------------------------------------------------------------*/
603
604 /**
605  * \brief get size of #snd_seq_client_info_t
606  * \return size in bytes
607  */
608 size_t snd_seq_client_info_sizeof()
609 {
610         return sizeof(snd_seq_client_info_t);
611 }
612
613 /**
614  * \brief allocate an empty #snd_seq_client_info_t using standard malloc
615  * \param ptr returned pointer
616  * \return 0 on success otherwise negative error code
617  */
618 int snd_seq_client_info_malloc(snd_seq_client_info_t **ptr)
619 {
620         assert(ptr);
621         *ptr = calloc(1, sizeof(snd_seq_client_info_t));
622         if (!*ptr)
623                 return -ENOMEM;
624         return 0;
625 }
626
627 /**
628  * \brief frees a previously allocated #snd_seq_client_info_t
629  * \param pointer to object to free
630  */
631 void snd_seq_client_info_free(snd_seq_client_info_t *obj)
632 {
633         free(obj);
634 }
635
636 /**
637  * \brief copy one #snd_seq_client_info_t to another
638  * \param dst pointer to destination
639  * \param src pointer to source
640  */
641 void snd_seq_client_info_copy(snd_seq_client_info_t *dst, const snd_seq_client_info_t *src)
642 {
643         assert(dst && src);
644         *dst = *src;
645 }
646
647
648 /**
649  * \brief Get client id of a client_info container
650  * \param info client_info container
651  * \return client id
652  */
653 int snd_seq_client_info_get_client(const snd_seq_client_info_t *info)
654 {
655         assert(info);
656         return info->client;
657 }
658
659 /**
660  * \brief Get client type of a client_info container
661  * \param info client_info container
662  * \return client type
663  *
664  * The client type is either #SEQ_CLIENT_TYPE_KERNEL or #SEQ_CLIENT_TYPE_USER
665  * for kernel or user client respectively.
666  */
667 snd_seq_client_type_t snd_seq_client_info_get_type(const snd_seq_client_info_t *info)
668 {
669         assert(info);
670         return info->type;
671 }
672
673 /**
674  * \brief Get the name of a client_info container
675  * \param info client_info container
676  * \return name string
677  */
678 const char *snd_seq_client_info_get_name(snd_seq_client_info_t *info)
679 {
680         assert(info);
681         return info->name;
682 }
683
684 /**
685  * \brief Get the broadcast filter usage of a client_info container
686  * \param info client_info container
687  * \return 1 if broadcast is accepted
688  */
689 int snd_seq_client_info_get_broadcast_filter(const snd_seq_client_info_t *info)
690 {
691         assert(info);
692         return (info->filter & SNDRV_SEQ_FILTER_BROADCAST) ? 1 : 0;
693 }
694
695 /**
696  * \brief Get the error-bounce usage of a client_info container
697  * \param info client_info container
698  * \return 1 if error-bounce is enabled
699  */
700 int snd_seq_client_info_get_error_bounce(const snd_seq_client_info_t *info)
701 {
702         assert(info);
703         return (info->filter & SNDRV_SEQ_FILTER_BOUNCE) ? 1 : 0;
704 }
705
706 /**
707  * \brief Get the event filter bitmap of a client_info container
708  * \param info client_info container
709  * \return NULL if no event filter, or pointer to event filter bitmap
710  */
711 const unsigned char *snd_seq_client_info_get_event_filter(const snd_seq_client_info_t *info)
712 {
713         assert(info);
714         if (info->filter & SNDRV_SEQ_FILTER_USE_EVENT)
715                 return info->event_filter;
716         else
717                 return NULL;
718 }
719
720 /**
721  * \brief Get the number of opened ports of a client_info container
722  * \param info client_info container
723  * \return number of opened ports
724  */
725 int snd_seq_client_info_get_num_ports(const snd_seq_client_info_t *info)
726 {
727         assert(info);
728         return info->num_ports;
729 }
730
731 /**
732  * \brief Get the number of lost events of a client_info container
733  * \param info client_info container
734  * \return number of lost events
735  */
736 int snd_seq_client_info_get_event_lost(const snd_seq_client_info_t *info)
737 {
738         assert(info);
739         return info->event_lost;
740 }
741
742 /**
743  * \brief Set the client id of a client_info container
744  * \param info client_info container
745  * \param client client id
746  */
747 void snd_seq_client_info_set_client(snd_seq_client_info_t *info, int client)
748 {
749         assert(info);
750         info->client = client;
751 }
752
753 /**
754  * \brief Set the name of a client_info container
755  * \param info client_info container
756  * \param name name string
757  */
758 void snd_seq_client_info_set_name(snd_seq_client_info_t *info, const char *name)
759 {
760         assert(info && name);
761         strncpy(info->name, name, sizeof(info->name));
762 }
763
764 /**
765  * \brief Set the broadcast filter usage of a client_info container
766  * \param info client_info container
767  * \param val non-zero if broadcast is accepted
768  */
769 void snd_seq_client_info_set_broadcast_filter(snd_seq_client_info_t *info, int val)
770 {
771         assert(info);
772         if (val)
773                 info->filter |= SNDRV_SEQ_FILTER_BROADCAST;
774         else
775                 info->filter &= ~SNDRV_SEQ_FILTER_BROADCAST;
776 }
777
778 /**
779  * \brief Set the error-bounce usage of a client_info container
780  * \param info client_info container
781  * \param val non-zero if error is bounced
782  */
783 void snd_seq_client_info_set_error_bounce(snd_seq_client_info_t *info, int val)
784 {
785         assert(info);
786         if (val)
787                 info->filter |= SNDRV_SEQ_FILTER_BOUNCE;
788         else
789                 info->filter &= ~SNDRV_SEQ_FILTER_BOUNCE;
790 }
791
792 /**
793  * \brief Set the event filter bitmap of a client_info container
794  * \param info client_info container
795  * \param filter event filter bitmap
796  */
797 void snd_seq_client_info_set_event_filter(snd_seq_client_info_t *info, unsigned char *filter)
798 {
799         assert(info);
800         if (! filter)
801                 info->filter &= ~SNDRV_SEQ_FILTER_USE_EVENT;
802         else {
803                 info->filter |= SNDRV_SEQ_FILTER_USE_EVENT;
804                 memcpy(info->event_filter, filter, sizeof(info->event_filter));
805         }
806 }
807
808
809 /**
810  * \brief obtain the information of the given client
811  * \param seq sequencer handle
812  * \param client client id
813  * \param info the pointer to be stored
814  * \return 0 on success otherwise a negative error code
815  * 
816  * Obtains the information of the client with a client id specified by
817  * info argument.
818  * The obtained information is written on info parameter.
819  */
820 int snd_seq_get_any_client_info(snd_seq_t *seq, int client, snd_seq_client_info_t *info)
821 {
822         assert(seq && info && client >= 0);
823         memset(info, 0, sizeof(snd_seq_client_info_t));
824         info->client = client;
825         return seq->ops->get_client_info(seq, info);
826 }
827
828 /**
829  * \brief obtain the current client information
830  * \param seq sequencer handle
831  * \param info the pointer to be stored
832  * \return 0 on success otherwise a negative error code
833  *
834  * Obtains the information of the current client stored on info.
835  * client and type fields are ignored.
836  */
837 int snd_seq_get_client_info(snd_seq_t *seq, snd_seq_client_info_t *info)
838 {
839         return snd_seq_get_any_client_info(seq, seq->client, info);
840 }
841
842 /**
843  * \brief set the current client information
844  * \param seq sequencer handle
845  * \param info the client info data to set
846  * \return 0 on success otherwise a negative error code
847  *
848  * Obtains the information of the current client stored on info.
849  * client and type fields are ignored.
850  */
851 int snd_seq_set_client_info(snd_seq_t *seq, snd_seq_client_info_t *info)
852 {
853         assert(seq && info);
854         info->client = seq->client;
855         info->type = USER_CLIENT;
856         return seq->ops->set_client_info(seq, info);
857 }
858
859 /**
860  * \brief query the next matching client
861  * \param seq sequencer handle
862  * \param info query pattern and result
863  *
864  * Queries the next matching client with the given condition in
865  * info argument.
866  * The search begins at the client with an id one greater than
867  * client field in info.
868  * If name field in info is not empty, the client name is compared.
869  * If a matching client is found, its attributes are stored o
870  * info and returns zero.
871  * Otherwise returns a negative error code.
872  */
873 int snd_seq_query_next_client(snd_seq_t *seq, snd_seq_client_info_t *info)
874 {
875         assert(seq && info);
876         return seq->ops->query_next_client(seq, info);
877 }
878
879
880 /*----------------------------------------------------------------*/
881
882
883 /*
884  * Port
885  */
886
887 /**
888  * \brief get size of #snd_seq_port_info_t
889  * \return size in bytes
890  */
891 size_t snd_seq_port_info_sizeof()
892 {
893         return sizeof(snd_seq_port_info_t);
894 }
895
896 /**
897  * \brief allocate an empty #snd_seq_port_info_t using standard malloc
898  * \param ptr returned pointer
899  * \return 0 on success otherwise negative error code
900  */
901 int snd_seq_port_info_malloc(snd_seq_port_info_t **ptr)
902 {
903         assert(ptr);
904         *ptr = calloc(1, sizeof(snd_seq_port_info_t));
905         if (!*ptr)
906                 return -ENOMEM;
907         return 0;
908 }
909
910 /**
911  * \brief frees a previously allocated #snd_seq_port_info_t
912  * \param pointer to object to free
913  */
914 void snd_seq_port_info_free(snd_seq_port_info_t *obj)
915 {
916         free(obj);
917 }
918
919 /**
920  * \brief copy one #snd_seq_port_info_t to another
921  * \param dst pointer to destination
922  * \param src pointer to source
923  */
924 void snd_seq_port_info_copy(snd_seq_port_info_t *dst, const snd_seq_port_info_t *src)
925 {
926         assert(dst && src);
927         *dst = *src;
928 }
929
930
931 /**
932  * \brief Get client id of a port_info container
933  * \param info port_info container
934  * \return client id
935  */
936 int snd_seq_port_info_get_client(const snd_seq_port_info_t *info)
937 {
938         assert(info);
939         return info->addr.client;
940 }
941
942 /**
943  * \brief Get port id of a port_info container
944  * \param info port_info container
945  * \return port id
946  */
947 int snd_seq_port_info_get_port(const snd_seq_port_info_t *info)
948 {
949         assert(info);
950         return info->addr.port;
951 }
952
953 /**
954  * \brief Get client/port address of a port_info container
955  * \param info port_info container
956  * \return client/port address pointer
957  */
958 const snd_seq_addr_t *snd_seq_port_info_get_addr(const snd_seq_port_info_t *info)
959 {
960         assert(info);
961         return (snd_seq_addr_t *)&info->addr;
962 }
963
964 /**
965  * \brief Get the name of a port_info container
966  * \param info port_info container
967  * \return name string
968  */
969 const char *snd_seq_port_info_get_name(const snd_seq_port_info_t *info)
970 {
971         assert(info);
972         return info->name;
973 }
974
975 /**
976  * \brief Get the capability bits of a port_info container
977  * \param info port_info container
978  * \return capability bits
979  */
980 unsigned int snd_seq_port_info_get_capability(const snd_seq_port_info_t *info)
981 {
982         assert(info);
983         return info->capability;
984 }
985
986 /**
987  * \brief Get the type bits of a port_info container
988  * \param info port_info container
989  * \return port type bits
990  */
991 unsigned int snd_seq_port_info_get_type(const snd_seq_port_info_t *info)
992 {
993         assert(info);
994         return info->type;
995 }
996
997 /**
998  * \brief Get the number of read subscriptions of a port_info container
999  * \param info port_info container
1000  * \return number of read subscriptions
1001  */
1002 int snd_seq_port_info_get_read_use(const snd_seq_port_info_t *info)
1003 {
1004         assert(info);
1005         return info->read_use;
1006 }
1007
1008 /**
1009  * \brief Get the number of write subscriptions of a port_info container
1010  * \param info port_info container
1011  * \return number of write subscriptions
1012  */
1013 int snd_seq_port_info_get_write_use(const snd_seq_port_info_t *info)
1014 {
1015         assert(info);
1016         return info->write_use;
1017 }
1018
1019 /**
1020  * \brief Get the midi channels of a port_info container
1021  * \param info port_info container
1022  * \return number of midi channels (default 0)
1023  */
1024 int snd_seq_port_info_get_midi_channels(const snd_seq_port_info_t *info)
1025 {
1026         assert(info);
1027         return info->midi_channels;
1028 }
1029
1030 /**
1031  * \brief Get the midi voices of a port_info container
1032  * \param info port_info container
1033  * \return number of midi voices (default 0)
1034  */
1035 int snd_seq_port_info_get_midi_voices(const snd_seq_port_info_t *info)
1036 {
1037         assert(info);
1038         return info->midi_voices;
1039 }
1040
1041 /**
1042  * \brief Get the synth voices of a port_info container
1043  * \param info port_info container
1044  * \return number of synth voices (default 0)
1045  */
1046 int snd_seq_port_info_get_synth_voices(const snd_seq_port_info_t *info)
1047 {
1048         assert(info);
1049         return info->synth_voices;
1050 }
1051
1052 /**
1053  * \brief Get the port-specified mode of a port_info container
1054  * \param info port_info container
1055  * \return 1 if port id is specified at creation
1056  */
1057 int snd_seq_port_info_get_port_specified(const snd_seq_port_info_t *info)
1058 {
1059         assert(info);
1060         return (info->flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? 1 : 0;
1061 }
1062
1063 /**
1064  * \brief Set the client id of a port_info container
1065  * \param info port_info container
1066  * \param client client id
1067  */
1068 void snd_seq_port_info_set_client(snd_seq_port_info_t *info, int client)
1069 {
1070         assert(info);
1071         info->addr.client = client;
1072 }
1073
1074 /**
1075  * \brief Set the port id of a port_info container
1076  * \param info port_info container
1077  * \param port port id
1078  */
1079 void snd_seq_port_info_set_port(snd_seq_port_info_t *info, int port)
1080 {
1081         assert(info);
1082         info->addr.port = port;
1083 }
1084
1085 /**
1086  * \brief Set the client/port address of a port_info container
1087  * \param info port_info container
1088  * \param addr client/port address
1089  */
1090 void snd_seq_port_info_set_addr(snd_seq_port_info_t *info, const snd_seq_addr_t *addr)
1091 {
1092         assert(info);
1093         info->addr = *(struct sndrv_seq_addr *)addr;
1094 }
1095
1096 /**
1097  * \brief Set the name of a port_info container
1098  * \param info port_info container
1099  * \param name name string
1100  */
1101 void snd_seq_port_info_set_name(snd_seq_port_info_t *info, const char *name)
1102 {
1103         assert(info && name);
1104         strncpy(info->name, name, sizeof(info->name));
1105 }
1106
1107 /**
1108  * \brief set the capability bits of a port_info container
1109  * \param info port_info container
1110  * \param capability capability bits
1111  */
1112 void snd_seq_port_info_set_capability(snd_seq_port_info_t *info, unsigned int capability)
1113 {
1114         assert(info);
1115         info->capability = capability;
1116 }
1117
1118 /**
1119  * \brief Get the type bits of a port_info container
1120  * \param info port_info container
1121  * \return port type bits
1122  */
1123 void snd_seq_port_info_set_type(snd_seq_port_info_t *info, unsigned int type)
1124 {
1125         assert(info);
1126         info->type = type;
1127 }
1128
1129 /**
1130  * \brief set the midi channels of a port_info container
1131  * \param info port_info container
1132  * \param channels midi channels (default 0)
1133  */
1134 void snd_seq_port_info_set_midi_channels(snd_seq_port_info_t *info, int channels)
1135 {
1136         assert(info);
1137         info->midi_channels = channels;
1138 }
1139
1140 /**
1141  * \brief set the midi voices of a port_info container
1142  * \param info port_info container
1143  * \param voices midi voices (default 0)
1144  */
1145 void snd_seq_port_info_set_midi_voices(snd_seq_port_info_t *info, int voices)
1146 {
1147         assert(info);
1148         info->midi_voices = voices;
1149 }
1150
1151 /**
1152  * \brief set the synth voices of a port_info container
1153  * \param info port_info container
1154  * \param voices synth voices (default 0)
1155  */
1156 void snd_seq_port_info_set_synth_voices(snd_seq_port_info_t *info, int voices)
1157 {
1158         assert(info);
1159         info->synth_voices = voices;
1160 }
1161
1162 /**
1163  * \brief Set the port-specified mode of a port_info container
1164  * \param info port_info container
1165  * \param val non-zero if specifying the port id at creation
1166  */
1167 void snd_seq_port_info_set_port_specified(snd_seq_port_info_t *info, int val)
1168 {
1169         assert(info);
1170         if (val)
1171                 info->flags |= SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
1172         else
1173                 info->flags &= ~SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
1174 }
1175
1176
1177 /**
1178  * \brief create a sequencer port on the current client
1179  * \param seq sequencer handle
1180  * \param port port information for the new port
1181  * \return 0 on success otherwise a negative error code
1182  *
1183  * Creates a sequencer port on the current client.
1184  * The attributes of created port is specified in \a info argument.
1185  *
1186  * The client field in \a info argument is overwritten with the current client id.
1187  * The port id to be created can be specified via #snd_seq_port_info_set_port_specified.
1188  * You can get the created port id by reading the port pointer via #snd_seq_port_info_get_port.
1189  *
1190  * Each port has the capability bit-masks to specify the access capability
1191  * of the port from other clients.
1192  * The capability bit flags are defined as follows:
1193  * - #SND_SEQ_PORT_CAP_READ Readable from this port
1194  * - #SND_SEQ_PORT_CAP_WRITE Writable to this port.
1195  * - #SND_SEQ_PORT_CAP_SYNC_READ For synchronization (not implemented)
1196  * - #SND_SEQ_PORT_CAP_SYNC_WRITE For synchronization (not implemented)
1197  * - #SND_SEQ_PORT_CAP_DUPLEX Read/write duplex access is supported
1198  * - #SND_SEQ_PORT_CAP_SUBS_READ Read subscription is allowed
1199  * - #SND_SEQ_PORT_CAP_SUBS_WRITE Write subscription is allowed
1200  * - #SND_SEQ_PORT_CAP_SUBS_NO_EXPORT Subscription management from 3rd client is disallowed
1201  *
1202  * Each port has also the type bitmasks defined as follows:
1203  * - #SND_SEQ_PORT_TYPE_SPECIFIC Hardware specific port
1204  * - #SND_SEQ_PORT_TYPE_MIDI_GENERIC Generic MIDI device
1205  * - #SND_SEQ_PORT_TYPE_MIDI_GM General MIDI compatible device
1206  * - #SND_SEQ_PORT_TYPE_MIDI_GS GS compatible device
1207  * - #SND_SEQ_PORT_TYPE_MIDI_XG XG compatible device
1208  * - #SND_SEQ_PORT_TYPE_MIDI_MT32 MT-32 compatible device
1209  * - #SND_SEQ_PORT_TYPE_SYNTH Synth device
1210  * - #SND_SEQ_PORT_TYPE_DIRECT_SAMPLE Sampling device (supporting download)
1211  * - #SND_SEQ_PORT_TYPE_SAMPLE Sampling device (sample can be downloaded at any time)
1212  * - #SND_SEQ_PORT_TYPE_APPLICATION Application (sequencer/editor)
1213  *
1214  * A port may contain specific midi channels, midi voices and synth voices.
1215  * These values could be zero as default.
1216  */
1217 int snd_seq_create_port(snd_seq_t *seq, snd_seq_port_info_t * port)
1218 {
1219         assert(seq && port);
1220         port->addr.client = seq->client;
1221         return seq->ops->create_port(seq, port);
1222 }
1223
1224 /**
1225  * \brief delete a sequencer port on the current client
1226  * \param seq sequencer handle
1227  * \param port port to be deleted
1228  * \return 0 on success otherwise a negative error code
1229  *
1230  * Deletes the existing sequencer port on the current client.
1231  */
1232 int snd_seq_delete_port(snd_seq_t *seq, int port)
1233 {
1234         snd_seq_port_info_t pinfo;
1235         assert(seq);
1236         memset(&pinfo, 0, sizeof(pinfo));
1237         pinfo.addr.client = seq->client;
1238         pinfo.addr.port = port;
1239         return seq->ops->delete_port(seq, &pinfo);
1240 }
1241
1242 /**
1243  * \brief obtain the information of a port on an arbitrary client
1244  * \param seq sequencer handle
1245  * \param client client id to get
1246  * \param port port id to get
1247  * \param info pointer information returns
1248  * \return 0 on success otherwise a negative error code
1249  */
1250 int snd_seq_get_any_port_info(snd_seq_t *seq, int client, int port, snd_seq_port_info_t * info)
1251 {
1252         assert(seq && info && client >= 0 && port >= 0);
1253         memset(info, 0, sizeof(snd_seq_port_info_t));
1254         info->addr.client = client;
1255         info->addr.port = port;
1256         return seq->ops->get_port_info(seq, info);
1257 }
1258
1259 /**
1260  * \brief obtain the information of a port on the current client
1261  * \param seq sequencer handle
1262  * \param port port id to get
1263  * \param info pointer information returns
1264  * \return 0 on success otherwise a negative error code
1265  */
1266 int snd_seq_get_port_info(snd_seq_t *seq, int port, snd_seq_port_info_t * info)
1267 {
1268         return snd_seq_get_any_port_info(seq, seq->client, port, info);
1269 }
1270
1271 /**
1272  * \brief set the information of a port on the current client
1273  * \param seq sequencer handle
1274  * \param port port to be set
1275  * \param info port information to be set
1276  * \return 0 on success otherwise a negative error code
1277  */
1278 int snd_seq_set_port_info(snd_seq_t *seq, int port, snd_seq_port_info_t * info)
1279 {
1280         assert(seq && info && port >= 0);
1281         info->addr.client = seq->client;
1282         info->addr.port = port;
1283         return seq->ops->set_port_info(seq, info);
1284 }
1285
1286 /**
1287  * \brief query the next matching port
1288  * \param seq sequencer handle
1289  * \param info query pattern and result
1290
1291  * Queries the next matching port on the client specified in
1292  * \a info argument.
1293  * The search begins at the next port specified in
1294  * port field of \a info argument.
1295  * For finding the first port at a certain client, give -1.
1296  *
1297  * If a matching port is found, its attributes are stored on
1298  * \a info and function returns zero.
1299  * Otherwise, a negative error code is returned.
1300  */
1301 int snd_seq_query_next_port(snd_seq_t *seq, snd_seq_port_info_t *info)
1302 {
1303         assert(seq && info);
1304         return seq->ops->query_next_port(seq, info);
1305 }
1306
1307
1308 /*----------------------------------------------------------------*/
1309
1310 /*
1311  * subscription
1312  */
1313
1314
1315 /**
1316  * \brief get size of #snd_seq_port_subscribe_t
1317  * \return size in bytes
1318  */
1319 size_t snd_seq_port_subscribe_sizeof()
1320 {
1321         return sizeof(snd_seq_port_subscribe_t);
1322 }
1323
1324 /**
1325  * \brief allocate an empty #snd_seq_port_subscribe_t using standard malloc
1326  * \param ptr returned pointer
1327  * \return 0 on success otherwise negative error code
1328  */
1329 int snd_seq_port_subscribe_malloc(snd_seq_port_subscribe_t **ptr)
1330 {
1331         assert(ptr);
1332         *ptr = calloc(1, sizeof(snd_seq_port_subscribe_t));
1333         if (!*ptr)
1334                 return -ENOMEM;
1335         return 0;
1336 }
1337
1338 /**
1339  * \brief frees a previously allocated #snd_seq_port_subscribe_t
1340  * \param pointer to object to free
1341  */
1342 void snd_seq_port_subscribe_free(snd_seq_port_subscribe_t *obj)
1343 {
1344         free(obj);
1345 }
1346
1347 /**
1348  * \brief copy one #snd_seq_port_subscribe_t to another
1349  * \param dst pointer to destination
1350  * \param src pointer to source
1351  */
1352 void snd_seq_port_subscribe_copy(snd_seq_port_subscribe_t *dst, const snd_seq_port_subscribe_t *src)
1353 {
1354         assert(dst && src);
1355         *dst = *src;
1356 }
1357
1358
1359 /**
1360  * \brief Get sender address of a port_subscribe container
1361  * \param info port_subscribe container
1362  * \param addr sender address
1363  */
1364 const snd_seq_addr_t *snd_seq_port_subscribe_get_sender(const snd_seq_port_subscribe_t *info)
1365 {
1366         assert(info);
1367         return (snd_seq_addr_t *)&info->sender;
1368 }
1369
1370 /**
1371  * \brief Get destination address of a port_subscribe container
1372  * \param info port_subscribe container
1373  * \param addr destination address
1374  */
1375 const snd_seq_addr_t *snd_seq_port_subscribe_get_dest(const snd_seq_port_subscribe_t *info)
1376 {
1377         assert(info);
1378         return (snd_seq_addr_t *)&info->dest;
1379 }
1380
1381 /**
1382  * \brief Get the queue id of a port_subscribe container
1383  * \param info port_subscribe container
1384  * \return queue id
1385  */
1386 int snd_seq_port_subscribe_get_queue(const snd_seq_port_subscribe_t *info)
1387 {
1388         assert(info);
1389         return info->queue;
1390 }
1391
1392 /**
1393  * \brief Get the exclusive mode of a port_subscribe container
1394  * \param info port_subscribe container
1395  * \return 1 if exclusive mode
1396  */
1397 int snd_seq_port_subscribe_get_exclusive(const snd_seq_port_subscribe_t *info)
1398 {
1399         assert(info);
1400         return (info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE) ? 1 : 0;
1401 }
1402
1403 /**
1404  * \brief Get the time-update mode of a port_subscribe container
1405  * \param info port_subscribe container
1406  * \return 1 if update timestamp
1407  */
1408 int snd_seq_port_subscribe_get_time_update(const snd_seq_port_subscribe_t *info)
1409 {
1410         assert(info);
1411         return (info->flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) ? 1 : 0;
1412 }
1413
1414 /**
1415  * \brief Get the real-time update mode of a port_subscribe container
1416  * \param info port_subscribe container
1417  * \return 1 if real-time update mode
1418  */
1419 int snd_seq_port_subscribe_get_time_real(const snd_seq_port_subscribe_t *info)
1420 {
1421         assert(info);
1422         return (info->flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 1 : 0;
1423 }
1424
1425 /**
1426  * \brief Set sender address of a port_subscribe container
1427  * \param info port_subscribe container
1428  * \param addr sender address
1429  */
1430 void snd_seq_port_subscribe_set_sender(snd_seq_port_subscribe_t *info, const snd_seq_addr_t *addr)
1431 {
1432         assert(info);
1433         memcpy(&info->sender, addr, sizeof(*addr));
1434 }
1435       
1436 /**
1437  * \brief Set destination address of a port_subscribe container
1438  * \param info port_subscribe container
1439  * \param addr destination address
1440  */
1441 void snd_seq_port_subscribe_set_dest(snd_seq_port_subscribe_t *info, const snd_seq_addr_t *addr)
1442 {
1443         assert(info);
1444         memcpy(&info->dest, addr, sizeof(*addr));
1445 }
1446
1447 /**
1448  * \brief Set the queue id of a port_subscribe container
1449  * \param info port_subscribe container
1450  * \param q queue id
1451  */
1452 void snd_seq_port_subscribe_set_queue(snd_seq_port_subscribe_t *info, int q)
1453 {
1454         assert(info);
1455         info->queue = q;
1456 }
1457
1458 /**
1459  * \brief Set the voices of a port_subscribe container
1460  * \param info port_subscribe container
1461  * \param voices voices to be allocated (0 = don't care)
1462  */
1463 void snd_seq_port_subscribe_set_voices(snd_seq_port_subscribe_t *info, unsigned int voices)
1464 {
1465         assert(info);
1466         info->voices = voices;
1467 }
1468
1469 /**
1470  * \brief Set the exclusive mode of a port_subscribe container
1471  * \param info port_subscribe container
1472  * \param val non-zero to enable
1473  */
1474 void snd_seq_port_subscribe_set_exclusive(snd_seq_port_subscribe_t *info, int val)
1475 {
1476         assert(info);
1477         if (val)
1478                 info->flags |= SNDRV_SEQ_PORT_SUBS_EXCLUSIVE;
1479         else
1480                 info->flags &= ~SNDRV_SEQ_PORT_SUBS_EXCLUSIVE;
1481 }
1482
1483 /**
1484  * \brief Set the time-update mode of a port_subscribe container
1485  * \param info port_subscribe container
1486  * \param val non-zero to enable
1487  */
1488 void snd_seq_port_subscribe_set_time_update(snd_seq_port_subscribe_t *info, int val)
1489 {
1490         assert(info);
1491         if (val)
1492                 info->flags |= SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
1493         else
1494                 info->flags &= ~SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
1495 }
1496
1497 /**
1498  * \brief Set the real-time mode of a port_subscribe container
1499  * \param info port_subscribe container
1500  * \param val non-zero to enable
1501  */
1502 void snd_seq_port_subscribe_set_time_real(snd_seq_port_subscribe_t *info, int val)
1503 {
1504         assert(info);
1505         if (val)
1506                 info->flags |= SNDRV_SEQ_PORT_SUBS_TIME_REAL;
1507         else
1508                 info->flags &= ~SNDRV_SEQ_PORT_SUBS_TIME_REAL;
1509 }
1510
1511
1512 /**
1513  * \brief obtain subscription information
1514  * \param seq sequencer handle
1515  * \param sub pointer to return the subscription information
1516  * \return 0 on success otherwise a negative error code
1517  */
1518 int snd_seq_get_port_subscription(snd_seq_t *seq, snd_seq_port_subscribe_t * sub)
1519 {
1520         assert(seq && sub);
1521         return seq->ops->get_port_subscription(seq, sub);
1522 }
1523
1524 /**
1525  * \brief subscribe a port connection
1526  * \param seq sequencer handle
1527  * \param sub subscription information
1528  * \return 0 on success otherwise a negative error code
1529  *
1530  * Subscribes a connection between two ports.
1531  * The subscription information is stored in sub argument.
1532  */
1533 int snd_seq_subscribe_port(snd_seq_t *seq, snd_seq_port_subscribe_t * sub)
1534 {
1535         assert(seq && sub);
1536         return seq->ops->subscribe_port(seq, sub);
1537 }
1538
1539 /**
1540  * \brief unsubscribe a connection between ports
1541  * \param seq sequencer handle
1542  * \param sub subscription information to disconnect
1543  * \return 0 on success otherwise a negative error code
1544  *
1545  * Unsubscribes a connection between two ports,
1546  * described in sender and dest fields in sub argument.
1547  */
1548 int snd_seq_unsubscribe_port(snd_seq_t *seq, snd_seq_port_subscribe_t * sub)
1549 {
1550         assert(seq && sub);
1551         return seq->ops->unsubscribe_port(seq, sub);
1552 }
1553
1554
1555 /**
1556  * \brief get size of #snd_seq_query_subscribe_t
1557  * \return size in bytes
1558  */
1559 size_t snd_seq_query_subscribe_sizeof()
1560 {
1561         return sizeof(snd_seq_query_subscribe_t);
1562 }
1563
1564 /**
1565  * \brief allocate an empty #snd_seq_query_subscribe_t using standard malloc
1566  * \param ptr returned pointer
1567  * \return 0 on success otherwise negative error code
1568  */
1569 int snd_seq_query_subscribe_malloc(snd_seq_query_subscribe_t **ptr)
1570 {
1571         assert(ptr);
1572         *ptr = calloc(1, sizeof(snd_seq_query_subscribe_t));
1573         if (!*ptr)
1574                 return -ENOMEM;
1575         return 0;
1576 }
1577
1578 /**
1579  * \brief frees a previously allocated #snd_seq_query_subscribe_t
1580  * \param pointer to object to free
1581  */
1582 void snd_seq_query_subscribe_free(snd_seq_query_subscribe_t *obj)
1583 {
1584         free(obj);
1585 }
1586
1587 /**
1588  * \brief copy one #snd_seq_query_subscribe_t to another
1589  * \param dst pointer to destination
1590  * \param src pointer to source
1591  */
1592 void snd_seq_query_subscribe_copy(snd_seq_query_subscribe_t *dst, const snd_seq_query_subscribe_t *src)
1593 {
1594         assert(dst && src);
1595         *dst = *src;
1596 }
1597
1598
1599 /**
1600  * \brief Get the client id of a query_subscribe container
1601  * \param info query_subscribe container
1602  * \return client id
1603  */
1604 int snd_seq_query_subscribe_get_client(const snd_seq_query_subscribe_t *info)
1605 {
1606         assert(info);
1607         return info->root.client;
1608 }
1609
1610 /**
1611  * \brief Get the port id of a query_subscribe container
1612  * \param info query_subscribe container
1613  * \return port id
1614  */
1615 int snd_seq_query_subscribe_get_port(const snd_seq_query_subscribe_t *info)
1616 {
1617         assert(info);
1618         return info->root.port;
1619 }
1620
1621 /**
1622  * \brief Get the client/port address of a query_subscribe container
1623  * \param info query_subscribe container
1624  * \return client/port address pointer
1625  */
1626 const snd_seq_addr_t *snd_seq_query_subscribe_get_root(const snd_seq_query_subscribe_t *info)
1627 {
1628         assert(info);
1629         return (snd_seq_addr_t *)&info->root;
1630 }
1631
1632 /**
1633  * \brief Get the query type of a query_subscribe container
1634  * \param info query_subscribe container
1635  * \return query type
1636  */
1637 snd_seq_query_subs_type_t snd_seq_query_subscribe_get_type(const snd_seq_query_subscribe_t *info)
1638 {
1639         assert(info);
1640         return info->type;
1641 }
1642
1643 /**
1644  * \brief Get the index of subscriber of a query_subscribe container
1645  * \param info query_subscribe container
1646  * \return subscriber's index
1647  */
1648 int snd_seq_query_subscribe_get_index(const snd_seq_query_subscribe_t *info)
1649 {
1650         assert(info);
1651         return info->index;
1652 }
1653
1654 /**
1655  * \brief Get the number of subscriptions of a query_subscribe container
1656  * \param info query_subscribe container
1657  * \return number of subscriptions
1658  */
1659 int snd_seq_query_subscribe_get_num_subs(const snd_seq_query_subscribe_t *info)
1660 {
1661         assert(info);
1662         return info->num_subs;
1663 }       
1664
1665 /**
1666  * \brief Get the address of subscriber of a query_subscribe container
1667  * \param info query_subscribe container
1668  * \return subscriber's address pointer
1669  */
1670 const snd_seq_addr_t *snd_seq_query_subscribe_get_addr(const snd_seq_query_subscribe_t *info)
1671 {
1672         assert(info);
1673         return (snd_seq_addr_t *)&info->addr;
1674 }
1675
1676 /**
1677  * \brief Get the queue id of subscriber of a query_subscribe container
1678  * \param info query_subscribe container
1679  * \return subscriber's queue id
1680  */
1681 int snd_seq_query_subscribe_get_queue(const snd_seq_query_subscribe_t *info)
1682 {
1683         assert(info);
1684         return info->queue;
1685 }
1686
1687 /**
1688  * \brief Get the exclusive mode of a query_subscribe container
1689  * \param info query_subscribe container
1690  * \return 1 if exclusive mode
1691  */
1692 int snd_seq_query_subscribe_get_exclusive(const snd_seq_query_subscribe_t *info)
1693 {
1694         assert(info);
1695         return (info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE) ? 1 : 0;
1696 }
1697
1698 /**
1699  * \brief Get the time-update mode of a query_subscribe container
1700  * \param info query_subscribe container
1701  * \return 1 if update timestamp
1702  */
1703 int snd_seq_query_subscribe_get_time_update(const snd_seq_query_subscribe_t *info)
1704 {
1705         assert(info);
1706         return (info->flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) ? 1 : 0;
1707 }
1708
1709 /**
1710  * \brief Get the real-time update mode of a query_subscribe container
1711  * \param info query_subscribe container
1712  * \return 1 if real-time update mode
1713  */
1714 int snd_seq_query_subscribe_get_time_real(const snd_seq_query_subscribe_t *info)
1715 {
1716         assert(info);
1717         return (info->flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) ? 1 : 0;
1718 }
1719
1720 /**
1721  * \brief Set the client id of a query_subscribe container
1722  * \param info query_subscribe container
1723  * \param client client id
1724  */
1725 void snd_seq_query_subscribe_set_client(snd_seq_query_subscribe_t *info, int client)
1726 {
1727         assert(info);
1728         info->root.client = client;
1729 }
1730
1731 /**
1732  * \brief Set the port id of a query_subscribe container
1733  * \param info query_subscribe container
1734  * \param port port id
1735  */
1736 void snd_seq_query_subscribe_set_port(snd_seq_query_subscribe_t *info, int port)
1737 {
1738         assert(info);
1739         info->root.port = port;
1740 }
1741
1742 /**
1743  * \brief Set the client/port address of a query_subscribe container
1744  * \param info query_subscribe container
1745  * \param addr client/port address pointer
1746  */
1747 void snd_seq_query_subscribe_set_root(snd_seq_query_subscribe_t *info, const snd_seq_addr_t *addr)
1748 {
1749         assert(info);
1750         info->root = *(struct sndrv_seq_addr *)addr;
1751 }
1752
1753 /**
1754  * \brief Set the query type of a query_subscribe container
1755  * \param info query_subscribe container
1756  * \param type query type
1757  */
1758 void snd_seq_query_subscribe_set_type(snd_seq_query_subscribe_t *info, snd_seq_query_subs_type_t type)
1759 {
1760         assert(info);
1761         info->type = type;
1762 }
1763
1764 /**
1765  * \brief Set the subscriber's index to be queried
1766  * \param info query_subscribe container
1767  * \param index index to be queried
1768  */
1769 void snd_seq_query_subscribe_set_index(snd_seq_query_subscribe_t *info, int index)
1770 {
1771         assert(info);
1772         info->index = index;
1773 }
1774
1775
1776 /**
1777  * \brief query port subscriber list
1778  * \param seq sequencer handle
1779  * \param subs subscription to query
1780  * \return 0 on success otherwise a negative error code
1781  *
1782  * Queries the subscribers accessing to a port.
1783  * The query information is specified in subs argument.
1784  *
1785  */
1786 int snd_seq_query_port_subscribers(snd_seq_t *seq, snd_seq_query_subscribe_t * subs)
1787 {
1788         assert(seq && subs);
1789         return seq->ops->query_port_subscribers(seq, subs);
1790 }
1791
1792 /*----------------------------------------------------------------*/
1793
1794 /*
1795  * queue handlers
1796  */
1797
1798 /**
1799  * \brief get size of #snd_seq_queue_info_t
1800  * \return size in bytes
1801  */
1802 size_t snd_seq_queue_info_sizeof()
1803 {
1804         return sizeof(snd_seq_queue_info_t);
1805 }
1806
1807 /**
1808  * \brief allocate an empty #snd_seq_queue_info_t using standard malloc
1809  * \param ptr returned pointer
1810  * \return 0 on success otherwise negative error code
1811  */
1812 int snd_seq_queue_info_malloc(snd_seq_queue_info_t **ptr)
1813 {
1814         assert(ptr);
1815         *ptr = calloc(1, sizeof(snd_seq_queue_info_t));
1816         if (!*ptr)
1817                 return -ENOMEM;
1818         return 0;
1819 }
1820
1821 /**
1822  * \brief frees a previously allocated #snd_seq_queue_info_t
1823  * \param pointer to object to free
1824  */
1825 void snd_seq_queue_info_free(snd_seq_queue_info_t *obj)
1826 {
1827         free(obj);
1828 }
1829
1830 /**
1831  * \brief copy one #snd_seq_queue_info_t to another
1832  * \param dst pointer to destination
1833  * \param src pointer to source
1834  */
1835 void snd_seq_queue_info_copy(snd_seq_queue_info_t *dst, const snd_seq_queue_info_t *src)
1836 {
1837         assert(dst && src);
1838         *dst = *src;
1839 }
1840
1841
1842 /**
1843  * \brief Get the queue id of a queue_info container
1844  * \param info queue_info container
1845  * \return queue id
1846  */
1847 int snd_seq_queue_info_get_queue(const snd_seq_queue_info_t *info)
1848 {
1849         assert(info);
1850         return info->queue;
1851 }
1852
1853 /**
1854  * \brief Get the name of a queue_info container
1855  * \param info queue_info container
1856  * \return name string
1857  */
1858 const char *snd_seq_queue_info_get_name(const snd_seq_queue_info_t *info)
1859 {
1860         assert(info);
1861         return info->name;
1862 }
1863
1864 /**
1865  * \brief Get the owner client id of a queue_info container
1866  * \param info queue_info container
1867  * \return owner client id
1868  */
1869 int snd_seq_queue_info_get_owner(const snd_seq_queue_info_t *info)
1870 {
1871         assert(info);
1872         return info->owner;
1873 }
1874
1875 /**
1876  * \brief Get the lock status of a queue_info container
1877  * \param info queue_info container
1878  * \return lock status --- non-zero = locked
1879  */
1880 int snd_seq_queue_info_get_locked(const snd_seq_queue_info_t *info)
1881 {
1882         assert(info);
1883         return info->locked;
1884 }
1885
1886 /**
1887  * \brief Get the conditional bit flags of a queue_info container
1888  * \param info queue_info container
1889  * \return conditional bit flags
1890  */
1891 unsigned int snd_seq_queue_info_get_flags(const snd_seq_queue_info_t *info)
1892 {
1893         assert(info);
1894         return info->flags;
1895 }
1896
1897 /**
1898  * \brief Set the name of a queue_info container
1899  * \param info queue_info container
1900  * \param name name string
1901  */
1902 void snd_seq_queue_info_set_name(snd_seq_queue_info_t *info, const char *name)
1903 {
1904         assert(info && name);
1905         strncpy(info->name, name, sizeof(info->name));
1906 }
1907
1908 /**
1909  * \brief Set the owner client id of a queue_info container
1910  * \param info queue_info container
1911  * \param owner client id
1912  */
1913 void snd_seq_queue_info_set_owner(snd_seq_queue_info_t *info, int owner)
1914 {
1915         assert(info);
1916         info->owner = owner;
1917 }
1918
1919 /**
1920  * \brief Set the lock status of a queue_info container
1921  * \param info queue_info container
1922  * \param locked lock status
1923  */
1924 void snd_seq_queue_info_set_locked(snd_seq_queue_info_t *info, int locked)
1925 {
1926         assert(info);
1927         info->locked = locked;
1928 }
1929
1930 /**
1931  * \brief Set the conditional bit flags of a queue_info container
1932  * \param info queue_info container
1933  * \param flags conditional bit flags
1934  */
1935 void snd_seq_queue_info_set_flags(snd_seq_queue_info_t *info, unsigned int flags)
1936 {
1937         assert(info);
1938         info->flags = flags;
1939 }
1940
1941
1942 /**
1943  * \brief create a queue
1944  * \param seq sequencer handle
1945  * \param info queue information to initialize
1946  * \return the queue id (zero or positive) on success otherwise a negative error code
1947  */
1948 int snd_seq_create_queue(snd_seq_t *seq, snd_seq_queue_info_t *info)
1949 {
1950         int err;
1951         assert(seq && info);
1952         info->owner = seq->client;
1953         err = seq->ops->create_queue(seq, info);
1954         if (err < 0)
1955                 return err;
1956         return info->queue;
1957 }
1958
1959 /**
1960  * \brief allocate a queue with the specified name
1961  * \param seq sequencer handle
1962  * \param name the name of the new queue
1963  * \return the queue id (zero or positive) on success otherwise a negative error code
1964  */ 
1965 int snd_seq_alloc_named_queue(snd_seq_t *seq, const char *name)
1966 {
1967         snd_seq_queue_info_t info;
1968         memset(&info, 0, sizeof(info));
1969         info.locked = 1;
1970         if (name)
1971                 strncpy(info.name, name, sizeof(info.name) - 1);
1972         return snd_seq_create_queue(seq, &info);
1973 }
1974
1975 /**
1976  * \brief allocate a queue
1977  * \param seq sequencer handle
1978  * \return the queue id (zero or positive) on success otherwise a negative error code
1979  */ 
1980 int snd_seq_alloc_queue(snd_seq_t *seq)
1981 {
1982         return snd_seq_alloc_named_queue(seq, NULL);
1983 }
1984
1985 /**
1986  * \brief delete the specified queue
1987  * \param seq sequencer handle
1988  * \param q queue id to delete
1989  * \return 0 on success otherwise a negative error code
1990  */
1991 int snd_seq_free_queue(snd_seq_t *seq, int q)
1992 {
1993         snd_seq_queue_info_t info;
1994         assert(seq);
1995         memset(&info, 0, sizeof(info));
1996         info.queue = q;
1997         return seq->ops->delete_queue(seq, &info);
1998 }
1999
2000 /**
2001  * \brief obtain queue attributes
2002  * \param seq sequencer handle
2003  * \param q queue id to query
2004  * \param info information returned
2005  * \return 0 on success otherwise a negative error code
2006  */
2007 int snd_seq_get_queue_info(snd_seq_t *seq, int q, snd_seq_queue_info_t *info)
2008 {
2009         assert(seq && info);
2010         info->queue = q;
2011         return seq->ops->get_queue_info(seq, info);
2012 }
2013
2014 /**
2015  * \brief change the queue attributes
2016  * \param seq sequencer handle
2017  * \param q queue id to change
2018  * \param info information changed
2019  * \return 0 on success otherwise a negative error code
2020  */
2021 int snd_seq_set_queue_info(snd_seq_t *seq, int q, snd_seq_queue_info_t *info)
2022 {
2023         assert(seq && info);
2024         info->queue = q;
2025         return seq->ops->set_queue_info(seq, info);
2026 }
2027
2028 /**
2029  * \brief query the matching queue with the specified name
2030  * \param seq sequencer handle
2031  * \param name the name string to query
2032  * \return the queue id if found or negative error code
2033  *
2034  * Searches the matching queue with the specified name string.
2035  */
2036 int snd_seq_query_named_queue(snd_seq_t *seq, const char *name)
2037 {
2038         int err;
2039         snd_seq_queue_info_t info;
2040         assert(seq && name);
2041         strncpy(info.name, name, sizeof(info.name));
2042         err = seq->ops->get_named_queue(seq, &info);
2043         if (err < 0)
2044                 return err;
2045         return info.queue;
2046 }
2047
2048 /**
2049  * \brief Get the queue usage flag to the client
2050  * \param seq sequencer handle
2051  * \param q queue id
2052  * \param client client id
2053  * \return 1 = client is allowed to access the queue, 0 = not allowed, 
2054  *     otherwise a negative error code
2055  */
2056 int snd_seq_get_queue_usage(snd_seq_t *seq, int q)
2057 {
2058         struct sndrv_seq_queue_client info;
2059         int err;
2060         assert(seq);
2061         memset(&info, 0, sizeof(info));
2062         info.queue = q;
2063         info.client = seq->client;
2064         if ((err = seq->ops->get_queue_client(seq, &info)) < 0)
2065                 return err;
2066         return info.used;
2067 }
2068
2069 /**
2070  * \brief Set the queue usage flag to the client
2071  * \param seq sequencer handle
2072  * \param q queue id
2073  * \param client client id
2074  * \param used non-zero if the client is allowed
2075  * \return 0 on success otherwise a negative error code
2076  */
2077 int snd_seq_set_queue_usage(snd_seq_t *seq, int q, int used)
2078 {
2079         struct sndrv_seq_queue_client info;
2080         assert(seq);
2081         memset(&info, 0, sizeof(info));
2082         info.queue = q;
2083         info.client = seq->client;
2084         info.used = used ? 1 : 0;
2085         return seq->ops->set_queue_client(seq, &info);
2086 }
2087
2088
2089 /**
2090  * \brief get size of #snd_seq_queue_status_t
2091  * \return size in bytes
2092  */
2093 size_t snd_seq_queue_status_sizeof()
2094 {
2095         return sizeof(snd_seq_queue_status_t);
2096 }
2097
2098 /**
2099  * \brief allocate an empty #snd_seq_queue_status_t using standard malloc
2100  * \param ptr returned pointer
2101  * \return 0 on success otherwise negative error code
2102  */
2103 int snd_seq_queue_status_malloc(snd_seq_queue_status_t **ptr)
2104 {
2105         assert(ptr);
2106         *ptr = calloc(1, sizeof(snd_seq_queue_status_t));
2107         if (!*ptr)
2108                 return -ENOMEM;
2109         return 0;
2110 }
2111
2112 /**
2113  * \brief frees a previously allocated #snd_seq_queue_status_t
2114  * \param pointer to object to free
2115  */
2116 void snd_seq_queue_status_free(snd_seq_queue_status_t *obj)
2117 {
2118         free(obj);
2119 }
2120
2121 /**
2122  * \brief copy one #snd_seq_queue_status_t to another
2123  * \param dst pointer to destination
2124  * \param src pointer to source
2125  */
2126 void snd_seq_queue_status_copy(snd_seq_queue_status_t *dst, const snd_seq_queue_status_t *src)
2127 {
2128         assert(dst && src);
2129         *dst = *src;
2130 }
2131
2132
2133 /**
2134  * \brief Get the queue id of a queue_status container
2135  * \param info queue_status container
2136  * \return queue id
2137  */
2138 int snd_seq_queue_status_get_queue(const snd_seq_queue_status_t *info)
2139 {
2140         assert(info);
2141         return info->queue;
2142 }
2143
2144 /**
2145  * \brief Get the number of events of a queue_status container
2146  * \param info queue_status container
2147  * \return number of events
2148  */
2149 int snd_seq_queue_status_get_events(const snd_seq_queue_status_t *info)
2150 {
2151         assert(info);
2152         return info->events;
2153 }
2154
2155 /**
2156  * \brief Get the tick time of a queue_status container
2157  * \param info queue_status container
2158  * \return tick time
2159  */
2160 snd_seq_tick_time_t snd_seq_queue_status_get_tick_time(const snd_seq_queue_status_t *info)
2161 {
2162         assert(info);
2163         return info->tick;
2164 }
2165
2166 /**
2167  * \brief Get the real time of a queue_status container
2168  * \param info queue_status container
2169  * \param time real time
2170  */
2171 const snd_seq_real_time_t *snd_seq_queue_status_get_real_time(const snd_seq_queue_status_t *info)
2172 {
2173         assert(info);
2174         return (snd_seq_real_time_t *)&info->time;
2175 }
2176
2177 /**
2178  * \brief Get the running status bits of a queue_status container
2179  * \param info queue_status container
2180  * \return running status bits
2181  */
2182 unsigned int snd_seq_queue_status_get_status(const snd_seq_queue_status_t *info)
2183 {
2184         assert(info);
2185         return info->running;
2186 }
2187
2188
2189 /**
2190  * \brief obtain the running state of the queue
2191  * \param seq sequencer handle
2192  * \param q queue id to query
2193  * \param status pointer to store the current status
2194  * \return 0 on success otherwise a negative error code
2195  *
2196  * Obtains the running state of the specified queue q.
2197  */
2198 int snd_seq_get_queue_status(snd_seq_t *seq, int q, snd_seq_queue_status_t * status)
2199 {
2200         assert(seq && status);
2201         memset(status, 0, sizeof(snd_seq_queue_status_t));
2202         status->queue = q;
2203         return seq->ops->get_queue_status(seq, status);
2204 }
2205
2206
2207 /**
2208  * \brief get size of #snd_seq_queue_tempo_t
2209  * \return size in bytes
2210  */
2211 size_t snd_seq_queue_tempo_sizeof()
2212 {
2213         return sizeof(snd_seq_queue_tempo_t);
2214 }
2215
2216 /**
2217  * \brief allocate an empty #snd_seq_queue_tempo_t using standard malloc
2218  * \param ptr returned pointer
2219  * \return 0 on success otherwise negative error code
2220  */
2221 int snd_seq_queue_tempo_malloc(snd_seq_queue_tempo_t **ptr)
2222 {
2223         assert(ptr);
2224         *ptr = calloc(1, sizeof(snd_seq_queue_tempo_t));
2225         if (!*ptr)
2226                 return -ENOMEM;
2227         return 0;
2228 }
2229
2230 /**
2231  * \brief frees a previously allocated #snd_seq_queue_tempo_t
2232  * \param pointer to object to free
2233  */
2234 void snd_seq_queue_tempo_free(snd_seq_queue_tempo_t *obj)
2235 {
2236         free(obj);
2237 }
2238
2239 /**
2240  * \brief copy one #snd_seq_queue_tempo_t to another
2241  * \param dst pointer to destination
2242  * \param src pointer to source
2243  */
2244 void snd_seq_queue_tempo_copy(snd_seq_queue_tempo_t *dst, const snd_seq_queue_tempo_t *src)
2245 {
2246         assert(dst && src);
2247         *dst = *src;
2248 }
2249
2250
2251 /**
2252  * \brief Get the queue id of a queue_status container
2253  * \param info queue_status container
2254  * \return queue id
2255  */
2256 int snd_seq_queue_tempo_get_queue(const snd_seq_queue_tempo_t *info)
2257 {
2258         assert(info);
2259         return info->queue;
2260 }
2261
2262 /**
2263  * \brief Get the tempo of a queue_status container
2264  * \param info queue_status container
2265  * \return tempo value
2266  */
2267 unsigned int snd_seq_queue_tempo_get_tempo(const snd_seq_queue_tempo_t *info)
2268 {
2269         assert(info);
2270         return info->tempo;
2271 }
2272
2273 /**
2274  * \brief Get the ppq of a queue_status container
2275  * \param info queue_status container
2276  * \return ppq value
2277  */
2278 int snd_seq_queue_tempo_get_ppq(const snd_seq_queue_tempo_t *info)
2279 {
2280         assert(info);
2281         return info->ppq;
2282 }
2283
2284 /**
2285  * \brief Get the timer skew value of a queue_status container
2286  * \param info queue_status container
2287  * \return timer skew value
2288  */
2289 unsigned int snd_seq_queue_tempo_get_skew(const snd_seq_queue_tempo_t *info)
2290 {
2291         assert(info);
2292         return info->skew_value;
2293 }
2294
2295 /**
2296  * \brief Get the timer skew base value of a queue_status container
2297  * \param info queue_status container
2298  * \return timer skew base value
2299  */
2300 unsigned int snd_seq_queue_tempo_get_skew_base(const snd_seq_queue_tempo_t *info)
2301 {
2302         assert(info);
2303         return info->skew_base;
2304 }
2305
2306 /**
2307  * \brief Set the tempo of a queue_status container
2308  * \param info queue_status container
2309  * \param tempo tempo value
2310  */
2311 void snd_seq_queue_tempo_set_tempo(snd_seq_queue_tempo_t *info, unsigned int tempo)
2312 {
2313         assert(info);
2314         info->tempo = tempo;
2315 }
2316
2317 /**
2318  * \brief Set the ppq of a queue_status container
2319  * \param info queue_status container
2320  * \param ppq ppq value
2321  */
2322 void snd_seq_queue_tempo_set_ppq(snd_seq_queue_tempo_t *info, int ppq)
2323 {
2324         assert(info);
2325         info->ppq = ppq;
2326 }
2327
2328 /**
2329  * \brief Set the timer skew value of a queue_status container
2330  * \param info queue_status container
2331  * \param skew timer skew value
2332  *
2333  * The skew of timer is calculated as skew / base.
2334  * For example, to play with double speed, pass base * 2 as the skew value.
2335  */
2336 void snd_seq_queue_tempo_set_skew(snd_seq_queue_tempo_t *info, unsigned int skew)
2337 {
2338         assert(info);
2339         info->skew_value = skew;
2340 }
2341
2342 /**
2343  * \brief Set the timer skew base value of a queue_status container
2344  * \param info queue_status container
2345  * \param base timer skew base value
2346  */
2347 void snd_seq_queue_tempo_set_skew_base(snd_seq_queue_tempo_t *info, unsigned int base)
2348 {
2349         assert(info);
2350         info->skew_base = base;
2351 }
2352
2353 /**
2354  * \brief obtain the current tempo of the queue
2355  * \param seq sequencer handle
2356  * \param q queue id to be queried
2357  * \param tempo pointer to store the current tempo
2358  * \return 0 on success otherwise a negative error code
2359  */
2360 int snd_seq_get_queue_tempo(snd_seq_t *seq, int q, snd_seq_queue_tempo_t * tempo)
2361 {
2362         assert(seq && tempo);
2363         memset(tempo, 0, sizeof(snd_seq_queue_tempo_t));
2364         tempo->queue = q;
2365         return seq->ops->get_queue_tempo(seq, tempo);
2366 }
2367
2368 /**
2369  * \brief set the tempo of the queue
2370  * \param seq sequencer handle
2371  * \param q queue id to change the tempo
2372  * \param tempo tempo information
2373  * \return 0 on success otherwise a negative error code
2374  */
2375 int snd_seq_set_queue_tempo(snd_seq_t *seq, int q, snd_seq_queue_tempo_t * tempo)
2376 {
2377         assert(seq && tempo);
2378         tempo->queue = q;
2379         return seq->ops->set_queue_tempo(seq, tempo);
2380 }
2381
2382
2383 /*----------------------------------------------------------------*/
2384
2385 /**
2386  * \brief get size of #snd_seq_queue_timer_t
2387  * \return size in bytes
2388  */
2389 size_t snd_seq_queue_timer_sizeof()
2390 {
2391         return sizeof(snd_seq_queue_timer_t);
2392 }
2393
2394 /**
2395  * \brief allocate an empty #snd_seq_queue_timer_t using standard malloc
2396  * \param ptr returned pointer
2397  * \return 0 on success otherwise negative error code
2398  */
2399 int snd_seq_queue_timer_malloc(snd_seq_queue_timer_t **ptr)
2400 {
2401         assert(ptr);
2402         *ptr = calloc(1, sizeof(snd_seq_queue_timer_t));
2403         if (!*ptr)
2404                 return -ENOMEM;
2405         return 0;
2406 }
2407
2408 /**
2409  * \brief frees a previously allocated #snd_seq_queue_timer_t
2410  * \param pointer to object to free
2411  */
2412 void snd_seq_queue_timer_free(snd_seq_queue_timer_t *obj)
2413 {
2414         free(obj);
2415 }
2416
2417 /**
2418  * \brief copy one #snd_seq_queue_timer_t to another
2419  * \param dst pointer to destination
2420  * \param src pointer to source
2421  */
2422 void snd_seq_queue_timer_copy(snd_seq_queue_timer_t *dst, const snd_seq_queue_timer_t *src)
2423 {
2424         assert(dst && src);
2425         *dst = *src;
2426 }
2427
2428
2429 /**
2430  * \brief Get the queue id of a queue_timer container
2431  * \param info queue_timer container
2432  * \return queue id
2433  */
2434 int snd_seq_queue_timer_get_queue(const snd_seq_queue_timer_t *info)
2435 {
2436         assert(info);
2437         return info->queue;
2438 }
2439
2440 /**
2441  * \brief Get the timer type of a queue_timer container
2442  * \param info queue_timer container
2443  * \return timer type
2444  */
2445 snd_seq_queue_timer_type_t snd_seq_queue_timer_get_type(const snd_seq_queue_timer_t *info)
2446 {
2447         assert(info);
2448         return (snd_seq_queue_timer_type_t)info->type;
2449 }
2450
2451 /**
2452  * \brief Get the timer id of a queue_timer container
2453  * \param info queue_timer container
2454  * \return timer id pointer
2455  */
2456 const snd_timer_id_t *snd_seq_queue_timer_get_id(const snd_seq_queue_timer_t *info)
2457 {
2458         assert(info);
2459         return &info->u.alsa.id;
2460 }
2461
2462 /**
2463  * \brief Get the timer resolution of a queue_timer container
2464  * \param info queue_timer container
2465  * \return timer resolution
2466  */
2467 unsigned int snd_seq_queue_timer_get_resolution(const snd_seq_queue_timer_t *info)
2468 {
2469         assert(info);
2470         return info->u.alsa.resolution;
2471 }
2472
2473 /**
2474  * \brief Set the timer type of a queue_timer container
2475  * \param info queue_timer container
2476  * \param type timer type
2477  */
2478 void snd_seq_queue_timer_set_type(snd_seq_queue_timer_t *info, snd_seq_queue_timer_type_t type)
2479 {
2480         assert(info);
2481         info->type = (int)type;
2482 }
2483         
2484 /**
2485  * \brief Set the timer id of a queue_timer container
2486  * \param info queue_timer container
2487  * \param id timer id pointer
2488  */
2489 void snd_seq_queue_timer_set_id(snd_seq_queue_timer_t *info, const snd_timer_id_t *id)
2490 {
2491         assert(info && id);
2492         info->u.alsa.id = *id;
2493 }
2494
2495 /**
2496  * \brief Set the timer resolution of a queue_timer container
2497  * \param info queue_timer container
2498  * \param resolution timer resolution
2499  */
2500 void snd_seq_queue_timer_set_resolution(snd_seq_queue_timer_t *info, unsigned int resolution)
2501 {
2502         assert(info);
2503         info->u.alsa.resolution = resolution;
2504 }
2505
2506
2507 /**
2508  * \brief obtain the queue timer information
2509  * \param seq sequencer handle
2510  * \param q queue id to query
2511  * \param timer pointer to store the timer information
2512  * \return 0 on success otherwise a negative error code
2513  */
2514 int snd_seq_get_queue_timer(snd_seq_t *seq, int q, snd_seq_queue_timer_t * timer)
2515 {
2516         assert(seq && timer);
2517         memset(timer, 0, sizeof(snd_seq_queue_timer_t));
2518         timer->queue = q;
2519         return seq->ops->get_queue_timer(seq, timer);
2520 }
2521
2522 /**
2523  * \brief set the queue timer information
2524  * \param seq sequencer handle
2525  * \param q queue id to change the timer
2526  * \param timer timer information
2527  * \return 0 on success otherwise a negative error code
2528  */
2529 int snd_seq_set_queue_timer(snd_seq_t *seq, int q, snd_seq_queue_timer_t * timer)
2530 {
2531         assert(seq && timer);
2532         timer->queue = q;
2533         return seq->ops->set_queue_timer(seq, timer);
2534 }
2535
2536 /*----------------------------------------------------------------*/
2537
2538 /**
2539  * \brief create an event cell
2540  * \return the cell pointer allocated
2541  */
2542 snd_seq_event_t *snd_seq_create_event(void)
2543 {
2544         return (snd_seq_event_t *) calloc(1, sizeof(snd_seq_event_t));
2545 }
2546
2547 /**
2548  * \brief free an event
2549  *
2550  * this is obsolete.  only for compatibility
2551  */
2552 #ifndef DOXYGEN
2553 int snd_seq_free_event(snd_seq_event_t *ev ATTRIBUTE_UNUSED)
2554 #else
2555 int snd_seq_free_event(snd_seq_event_t *ev)
2556 #endif
2557 {
2558         return 0;
2559 }
2560
2561 /**
2562  * \brief calculates the (encoded) byte-stream size of the event
2563  * \param ev the event
2564  * \return the size of decoded bytes
2565  */
2566 ssize_t snd_seq_event_length(snd_seq_event_t *ev)
2567 {
2568         ssize_t len = sizeof(snd_seq_event_t);
2569         assert(ev);
2570         if (sndrv_seq_ev_is_variable(ev))
2571                 len += ev->data.ext.len;
2572         return len;
2573 }
2574
2575 /*----------------------------------------------------------------*/
2576
2577 /*
2578  * output to sequencer
2579  */
2580
2581 /**
2582  * \brief output an event
2583  * \param seq sequencer handle
2584  * \param ev event to be output
2585  * \return the number of remaining events or a negative error code
2586  *
2587  * An event is once expanded on the output buffer.
2588  * The output buffer will be drained automatically if it becomes full.
2589  *
2590  * If events remain unprocessed on output buffer before drained,
2591  * the size of total byte data on output buffer is returned.
2592  * If the output buffer is empty, this returns zero.
2593  */
2594 int snd_seq_event_output(snd_seq_t *seq, snd_seq_event_t *ev)
2595 {
2596         int result;
2597
2598         result = snd_seq_event_output_buffer(seq, ev);
2599         if (result == -EAGAIN) {
2600                 result = snd_seq_drain_output(seq);
2601                 if (result < 0)
2602                         return result;
2603                 return snd_seq_event_output_buffer(seq, ev);
2604         }
2605         return result;
2606 }
2607
2608 /**
2609  * \brief output an event onto the lib buffer without draining buffer
2610  * \param seq sequencer handle
2611  * \param ev event to be output
2612  * \return the byte size of remaining events. \c -EAGAIN if the buffer becomes full.
2613  *
2614  * This function doesn't drain buffer unlike snd_seq_event_output().
2615  */
2616 int snd_seq_event_output_buffer(snd_seq_t *seq, snd_seq_event_t *ev)
2617 {
2618         int len;
2619         assert(seq && ev);
2620         len = snd_seq_event_length(ev);
2621         if (len < 0)
2622                 return -EINVAL;
2623         if ((size_t) len >= seq->obufsize)
2624                 return -EINVAL;
2625         if ((seq->obufsize - seq->obufused) < (size_t) len)
2626                 return -EAGAIN;
2627         memcpy(seq->obuf + seq->obufused, ev, sizeof(snd_seq_event_t));
2628         seq->obufused += sizeof(snd_seq_event_t);
2629         if (sndrv_seq_ev_is_variable(ev)) {
2630                 memcpy(seq->obuf + seq->obufused, ev->data.ext.ptr, ev->data.ext.len);
2631                 seq->obufused += ev->data.ext.len;
2632         }
2633         return seq->obufused;
2634 }
2635
2636 /*
2637  * allocate the temporary buffer
2638  */
2639 static int alloc_tmpbuf(snd_seq_t *seq, size_t len)
2640 {
2641         size_t size = ((len + sizeof(snd_seq_event_t) - 1) / sizeof(snd_seq_event_t));
2642         if (seq->tmpbuf == NULL) {
2643                 if (size > DEFAULT_TMPBUF_SIZE)
2644                         seq->tmpbufsize = size;
2645                 else
2646                         seq->tmpbufsize = DEFAULT_TMPBUF_SIZE;
2647                 seq->tmpbuf = malloc(seq->tmpbufsize * sizeof(snd_seq_event_t));
2648                 if (seq->tmpbuf == NULL)
2649                         return -ENOMEM;
2650         }  else if (len > seq->tmpbufsize) {
2651                 seq->tmpbuf = realloc(seq->tmpbuf, size * sizeof(snd_seq_event_t));
2652                 if (seq->tmpbuf == NULL)
2653                         return -ENOMEM;
2654                 seq->tmpbufsize = size;
2655         }
2656         return 0;
2657 }
2658
2659 /**
2660  * \brief output an event directly to the sequencer NOT through output buffer
2661  * \param seq sequencer handle
2662  * \param ev event to be output
2663  * \return the byte size sent to sequencer or a negative error code
2664  *
2665  * This function sends an event to the sequencer directly not through the
2666  * output buffer.  When the event is a variable length event, a temporary
2667  * buffer is allocated inside alsa-lib and the data is copied there before
2668  * actually sent.
2669  */
2670 int snd_seq_event_output_direct(snd_seq_t *seq, snd_seq_event_t *ev)
2671 {
2672         ssize_t len;
2673         void *buf;
2674
2675         len = snd_seq_event_length(ev);
2676         if (len < 0)
2677                 return len;
2678         else if (len == sizeof(*ev)) {
2679                 buf = ev;
2680         } else {
2681                 if (alloc_tmpbuf(seq, (size_t)len) < 0)
2682                         return -ENOMEM;
2683                 *seq->tmpbuf = *ev;
2684                 memcpy(seq->tmpbuf + 1, ev->data.ext.ptr, ev->data.ext.len);
2685                 buf = seq->tmpbuf;
2686         }
2687         return seq->ops->write(seq, buf, (size_t) len);
2688 }
2689
2690 /**
2691  * \brief return the size of pending events on output buffer
2692  * \param seq sequencer handle
2693  * \return the byte size of total of pending events
2694  */
2695 int snd_seq_event_output_pending(snd_seq_t *seq)
2696 {
2697         assert(seq);
2698         return seq->obufused;
2699 }
2700
2701 /**
2702  * \brief drain output buffer to sequencer
2703  * \param seq sequencer handle
2704  * \return 0 when all events are drained and sent to sequencer.
2705  *         When events still remain on the buffer, the byte size of remaining
2706  *         events are returned.  On error a negative error code is returned.
2707  */
2708 int snd_seq_drain_output(snd_seq_t *seq)
2709 {
2710         ssize_t result, processed = 0;
2711         assert(seq);
2712         while (seq->obufused > 0) {
2713                 result = seq->ops->write(seq, seq->obuf, seq->obufused);
2714                 if (result < 0) {
2715                         if (result == -EAGAIN && processed)
2716                                 return seq->obufused;
2717                         return result;
2718                 }
2719                 if ((size_t)result < seq->obufused)
2720                         memmove(seq->obuf, seq->obuf + result, seq->obufused - result);
2721                 seq->obufused -= result;
2722         }
2723         return 0;
2724 }
2725
2726 /**
2727  * \brief extract the first event in output buffer
2728  * \param seq sequencer handle
2729  * \param ev_res event pointer to be extracted
2730  * \return 0 on success otherwise a negative error code
2731  *
2732  * Extracts the first event in output buffer.
2733  * If ev_res is NULL, just remove the event.
2734  */
2735 int snd_seq_extract_output(snd_seq_t *seq, snd_seq_event_t **ev_res)
2736 {
2737         size_t len, olen;
2738         snd_seq_event_t ev;
2739         assert(seq);
2740         if (ev_res)
2741                 *ev_res = NULL;
2742         if ((olen = seq->obufused) < sizeof(snd_seq_event_t))
2743                 return -ENOENT;
2744         memcpy(&ev, (snd_seq_event_t*)seq->obuf, sizeof(snd_seq_event_t));
2745         len = snd_seq_event_length(&ev);
2746         if (ev_res) {
2747                 /* extract the event */
2748                 if (alloc_tmpbuf(seq, len) < 0)
2749                         return -ENOMEM;
2750                 memcpy(seq->tmpbuf, seq->obuf, len);
2751                 *ev_res = seq->tmpbuf;
2752         }
2753         seq->obufused = olen - len;
2754         memmove(seq->obuf, seq->obuf + len, seq->obufused);
2755         return 0;
2756 }
2757
2758 /*----------------------------------------------------------------*/
2759
2760 /*
2761  * input from sequencer
2762  */
2763
2764 /*
2765  * read from sequencer to input buffer
2766  */
2767 static ssize_t snd_seq_event_read_buffer(snd_seq_t *seq)
2768 {
2769         ssize_t len;
2770         len = seq->ops->read(seq, seq->ibuf, seq->ibufsize * sizeof(snd_seq_event_t));
2771         if (len < 0)
2772                 return len;
2773         seq->ibuflen = len / sizeof(snd_seq_event_t);
2774         seq->ibufptr = 0;
2775         return seq->ibuflen;
2776 }
2777
2778 static int snd_seq_event_retrieve_buffer(snd_seq_t *seq, snd_seq_event_t **retp)
2779 {
2780         size_t ncells;
2781         snd_seq_event_t *ev;
2782
2783         *retp = ev = &seq->ibuf[seq->ibufptr];
2784         seq->ibufptr++;
2785         seq->ibuflen--;
2786         if (! sndrv_seq_ev_is_variable(ev))
2787                 return 1;
2788         ncells = (ev->data.ext.len + sizeof(snd_seq_event_t) - 1) / sizeof(snd_seq_event_t);
2789         if (seq->ibuflen < ncells) {
2790                 seq->ibuflen = 0; /* clear buffer */
2791                 *retp = NULL;
2792                 return -EINVAL;
2793         }
2794         ev->data.ext.ptr = ev + 1;
2795         seq->ibuflen -= ncells;
2796         seq->ibufptr += ncells;
2797         return 1;
2798 }
2799
2800 /**
2801  * \brief retrieve an event from sequencer
2802  * \param seq sequencer handle
2803  * \param ev event pointer to be stored
2804  * \return 
2805  *
2806  * Obtains an input event from sequencer.
2807  * The event is created via snd_seq_create_event(), and its pointer is stored on
2808  * ev argument.
2809  *
2810  * This function firstly receives the event byte-stream data from sequencer
2811  * as much as possible at once.  Then it retrieves the first event record
2812  * and store the pointer on ev.
2813  * By calling this function sequentially, events are extracted from the input buffer.
2814  *
2815  * If there is no input from sequencer, function falls into sleep
2816  * in blocking mode until an event is received,
2817  * or returns \c -EAGAIN error in non-blocking mode.
2818  * Occasionally, this function may return \c -ENOSPC error.
2819  * This means that the input FIFO of sequencer overran, and some events are
2820  * lost.
2821  * Once this error is returned, the input FIFO is cleared automatically.
2822  *
2823  * Function returns the byte size of remaining events on the input buffer
2824  * if an event is successfully received.
2825  * Application can determine from the returned value whether to call
2826  * input once more or not.
2827  */
2828 int snd_seq_event_input(snd_seq_t *seq, snd_seq_event_t **ev)
2829 {
2830         int err;
2831         assert(seq);
2832         *ev = NULL;
2833         if (seq->ibuflen <= 0) {
2834                 if ((err = snd_seq_event_read_buffer(seq)) < 0)
2835                         return err;
2836         }
2837
2838         return snd_seq_event_retrieve_buffer(seq, ev);
2839 }
2840
2841 /*
2842  * read input data from sequencer if available
2843  */
2844 static int snd_seq_event_input_feed(snd_seq_t *seq, int timeout)
2845 {
2846         struct pollfd pfd;
2847         int err;
2848         pfd.fd = seq->poll_fd;
2849         pfd.events = POLLIN;
2850         err = poll(&pfd, 1, timeout);
2851         if (err < 0) {
2852                 SYSERR("poll");
2853                 return -errno;
2854         }
2855         if (pfd.revents & POLLIN) 
2856                 return snd_seq_event_read_buffer(seq);
2857         return seq->ibuflen;
2858 }
2859
2860 /**
2861  * \brief check events in input buffer
2862  * \return the byte size of remaining input events on input buffer.
2863  *
2864  * If events remain on the input buffer of user-space, function returns
2865  * the total byte size of events on it.
2866  * If fetch_sequencer argument is non-zero,
2867  * this function checks the presence of events on sequencer FIFO
2868  * When events exist, they are transferred to the input buffer,
2869  * and the number of received events are returned.
2870  * If fetch_sequencer argument is zero and
2871  * no events remain on the input buffer, function simply returns zero.
2872  */
2873 int snd_seq_event_input_pending(snd_seq_t *seq, int fetch_sequencer)
2874 {
2875         if (seq->ibuflen == 0 && fetch_sequencer) {
2876                 return snd_seq_event_input_feed(seq, 0);
2877         }
2878         return seq->ibuflen;
2879 }
2880
2881 /*----------------------------------------------------------------*/
2882
2883 /*
2884  * clear event buffers
2885  */
2886
2887 /**
2888  * \brief remove all events on user-space output buffer
2889  * \param seq sequencer handle
2890  *
2891  * Removes all events on user-space output buffer.
2892  * Unlike snd_seq_drain_output(), this function doesn't remove
2893  * events on output memory pool of sequencer.
2894  */
2895 int snd_seq_drop_output_buffer(snd_seq_t *seq)
2896 {
2897         assert(seq);
2898         seq->obufused = 0;
2899         return 0;
2900 }
2901
2902 /**
2903  * \brief remove all events on user-space input FIFO
2904  * \param seq sequencer handle
2905  */
2906 int snd_seq_drop_input_buffer(snd_seq_t *seq)
2907 {
2908         assert(seq);
2909         seq->ibufptr = 0;
2910         seq->ibuflen = 0;
2911         return 0;
2912 }
2913
2914 /**
2915  * \brief remove all events on output buffer
2916  * \param seq sequencer handle
2917  *
2918  * Removes all events on both user-space output buffer and
2919  * output memory pool on kernel.
2920  */
2921 int snd_seq_drop_output(snd_seq_t *seq)
2922 {
2923         snd_seq_remove_events_t rminfo;
2924         assert(seq);
2925         seq->obufused = 0; /* drain output buffer */
2926
2927         memset(&rminfo, 0, sizeof(rminfo));
2928         rminfo.remove_mode = SNDRV_SEQ_REMOVE_OUTPUT;
2929
2930         return snd_seq_remove_events(seq, &rminfo);
2931 }
2932
2933 /**
2934  * \brief clear input buffer and and remove events in sequencer queue
2935  * \param seq sequencer handle
2936  */
2937 int snd_seq_drop_input(snd_seq_t *seq)
2938 {
2939         snd_seq_remove_events_t rminfo;
2940         assert(seq);
2941
2942         seq->ibufptr = 0;       /* drain input buffer */
2943         seq->ibuflen = 0;
2944
2945         memset(&rminfo, 0, sizeof(rminfo));
2946         rminfo.remove_mode = SNDRV_SEQ_REMOVE_INPUT;
2947
2948         return snd_seq_remove_events(seq, &rminfo);
2949 }
2950
2951
2952 /**
2953  * \brief get size of #snd_seq_remove_events_t
2954  * \return size in bytes
2955  */
2956 size_t snd_seq_remove_events_sizeof()
2957 {
2958         return sizeof(snd_seq_remove_events_t);
2959 }
2960
2961 /**
2962  * \brief allocate an empty #snd_seq_remove_events_t using standard malloc
2963  * \param ptr returned pointer
2964  * \return 0 on success otherwise negative error code
2965  */
2966 int snd_seq_remove_events_malloc(snd_seq_remove_events_t **ptr)
2967 {
2968         assert(ptr);
2969         *ptr = calloc(1, sizeof(snd_seq_remove_events_t));
2970         if (!*ptr)
2971                 return -ENOMEM;
2972         return 0;
2973 }
2974
2975 /**
2976  * \brief frees a previously allocated #snd_seq_remove_events_t
2977  * \param pointer to object to free
2978  */
2979 void snd_seq_remove_events_free(snd_seq_remove_events_t *obj)
2980 {
2981         free(obj);
2982 }
2983
2984 /**
2985  * \brief copy one #snd_seq_remove_events_t to another
2986  * \param dst pointer to destination
2987  * \param src pointer to source
2988  */
2989 void snd_seq_remove_events_copy(snd_seq_remove_events_t *dst, const snd_seq_remove_events_t *src)
2990 {
2991         assert(dst && src);
2992         *dst = *src;
2993 }
2994
2995
2996 /**
2997  * \brief Get the removal condition bits
2998  * \param info remove_events container
2999  * \return removal condition bits
3000  */
3001 unsigned int snd_seq_remove_events_get_condition(const snd_seq_remove_events_t *info)
3002 {
3003         assert(info);
3004         return info->remove_mode;
3005 }
3006
3007 /**
3008  * \brief Get the queue as removal condition
3009  * \param info remove_events container
3010  * \return queue id
3011  */
3012 int snd_seq_remove_events_get_queue(const snd_seq_remove_events_t *info)
3013 {
3014         assert(info);
3015         return info->queue;
3016 }
3017
3018 /**
3019  * \brief Get the event timestamp as removal condition
3020  * \param info remove_events container
3021  * \return time stamp
3022  */
3023 const snd_seq_timestamp_t *snd_seq_remove_events_get_time(const snd_seq_remove_events_t *info)
3024 {
3025         assert(info);
3026         return (snd_seq_timestamp_t *)&info->time;
3027 }
3028
3029 /**
3030  * \brief Get the event destination address as removal condition
3031  * \param info remove_events container
3032  * \return destination address
3033  */
3034 const snd_seq_addr_t *snd_seq_remove_events_get_dest(const snd_seq_remove_events_t *info)
3035 {
3036         assert(info);
3037         return (snd_seq_addr_t *)&info->dest;
3038 }
3039
3040 /**
3041  * \brief Get the event channel as removal condition
3042  * \param info remove_events container
3043  * \return channel number
3044  */
3045 int snd_seq_remove_events_get_channel(const snd_seq_remove_events_t *info)
3046 {
3047         assert(info);
3048         return info->channel;
3049 }
3050
3051 /**
3052  * \brief Get the event type as removal condition
3053  * \param info remove_events container
3054  * \return event type
3055  */
3056 int snd_seq_remove_events_get_event_type(const snd_seq_remove_events_t *info)
3057 {
3058         assert(info);
3059         return info->type;
3060 }
3061
3062 /**
3063  * \brief Get the event tag id as removal condition
3064  * \param info remove_events container
3065  * \return tag id
3066  */
3067 int snd_seq_remove_events_get_tag(const snd_seq_remove_events_t *info)
3068 {
3069         assert(info);
3070         return info->tag;
3071 }
3072
3073 /**
3074  * \brief Set the removal condition bits
3075  * \param info remove_events container
3076  * \param flags removal condition bits
3077  */
3078 void snd_seq_remove_events_set_condition(snd_seq_remove_events_t *info, unsigned int flags)
3079 {
3080         assert(info);
3081         info->remove_mode = flags;
3082 }
3083
3084 /**
3085  * \brief Set the queue as removal condition
3086  * \param info remove_events container
3087  * \param queue queue id
3088  */
3089 void snd_seq_remove_events_set_queue(snd_seq_remove_events_t *info, int queue)
3090 {
3091         assert(info);
3092         info->queue = queue;
3093 }
3094
3095 /**
3096  * \brief Set the timestamp as removal condition
3097  * \param info remove_events container
3098  * \param time timestamp pointer
3099  */
3100 void snd_seq_remove_events_set_time(snd_seq_remove_events_t *info, const snd_seq_timestamp_t *time)
3101 {
3102         assert(info);
3103         info->time = *(union sndrv_seq_timestamp *)time;
3104 }
3105
3106 /**
3107  * \brief Set the destination address as removal condition
3108  * \param info remove_events container
3109  * \param addr destination address
3110  */
3111 void snd_seq_remove_events_set_dest(snd_seq_remove_events_t *info, const snd_seq_addr_t *addr)
3112 {
3113         assert(info);
3114         info->dest = *(struct sndrv_seq_addr *)addr;
3115 }
3116
3117 /**
3118  * \brief Set the channel as removal condition
3119  * \param info remove_events container
3120  * \param channel channel number
3121  */
3122 void snd_seq_remove_events_set_channel(snd_seq_remove_events_t *info, int channel)
3123 {
3124         assert(info);
3125         info->channel = channel;
3126 }
3127
3128 /**
3129  * \brief Set the event type as removal condition
3130  * \param info remove_events container
3131  * \param type event type
3132  */
3133 void snd_seq_remove_events_set_event_type(snd_seq_remove_events_t *info, int type)
3134 {
3135         assert(info);
3136         info->type = type;
3137 }
3138
3139 /**
3140  * \brief Set the event tag as removal condition
3141  * \param info remove_events container
3142  * \param tag tag id
3143  */
3144 void snd_seq_remove_events_set_tag(snd_seq_remove_events_t *info, int tag)
3145 {
3146         assert(info);
3147         info->tag = tag;
3148 }
3149
3150
3151 /* compare timestamp between events */
3152 /* return 1 if a >= b; otherwise return 0 */
3153 static inline int snd_seq_compare_tick_time(snd_seq_tick_time_t *a, snd_seq_tick_time_t *b)
3154 {
3155         /* compare ticks */
3156         return (*a >= *b);
3157 }
3158
3159 static inline int snd_seq_compare_real_time(snd_seq_real_time_t *a, snd_seq_real_time_t *b)
3160 {
3161         /* compare real time */
3162         if (a->tv_sec > b->tv_sec)
3163                 return 1;
3164         if ((a->tv_sec == b->tv_sec) && (a->tv_nsec >= b->tv_nsec))
3165                 return 1;
3166         return 0;
3167 }
3168
3169 /* Routine to match events to be removed */
3170 static int remove_match(snd_seq_remove_events_t *info, snd_seq_event_t *ev)
3171 {
3172         int res;
3173
3174         if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
3175                 if (ev->dest.client != info->dest.client ||
3176                                 ev->dest.port != info->dest.port)
3177                         return 0;
3178         }
3179         if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
3180                 if (! sndrv_seq_ev_is_channel_type(ev))
3181                         return 0;
3182                 /* data.note.channel and data.control.channel are identical */
3183                 if (ev->data.note.channel != info->channel)
3184                         return 0;
3185         }
3186         if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
3187                 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
3188                         res = snd_seq_compare_tick_time(&ev->time.tick, (snd_seq_tick_time_t *)&info->time.tick);
3189                 else
3190                         res = snd_seq_compare_real_time(&ev->time.time, (snd_seq_real_time_t *)&info->time.time);
3191                 if (!res)
3192                         return 0;
3193         }
3194         if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
3195                 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
3196                         res = snd_seq_compare_tick_time(&ev->time.tick, (snd_seq_tick_time_t *)&info->time.tick);
3197                 else
3198                         res = snd_seq_compare_real_time(&ev->time.time, (snd_seq_real_time_t *)&info->time.time);
3199                 if (res)
3200                         return 0;
3201         }
3202         if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
3203                 if (ev->type != info->type)
3204                         return 0;
3205         }
3206         if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
3207                 /* Do not remove off events */
3208                 switch (ev->type) {
3209                 case SNDRV_SEQ_EVENT_NOTEOFF:
3210                 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
3211                         return 0;
3212                 default:
3213                         break;
3214                 }
3215         }
3216         if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
3217                 if (info->tag != ev->tag)
3218                         return 0;
3219         }
3220
3221         return 1;
3222 }
3223
3224 /**
3225  * \brief remove events on input/output buffers
3226  * \param seq sequencer handle
3227  * \param rmp remove event container
3228  *
3229  * Removes matching events with the given condition from input/output buffers.
3230  * The removal condition is specified in rmp argument.
3231  */
3232 int snd_seq_remove_events(snd_seq_t *seq, snd_seq_remove_events_t *rmp)
3233 {
3234         if (rmp->remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
3235                 /*
3236                  * First deal with any events that are still buffered
3237                  * in the library.
3238                  */
3239                 snd_seq_drop_input_buffer(seq);
3240         }
3241
3242         if (rmp->remove_mode & SNDRV_SEQ_REMOVE_OUTPUT) {
3243                 /*
3244                  * First deal with any events that are still buffered
3245                  * in the library.
3246                  */
3247                  if (rmp->remove_mode & ~(SNDRV_SEQ_REMOVE_INPUT|SNDRV_SEQ_REMOVE_OUTPUT)) {
3248                          /* The simple case - remove all */
3249                          snd_seq_drop_output_buffer(seq);
3250                 } else {
3251                         char *ep;
3252                         size_t len;
3253                         snd_seq_event_t *ev;
3254
3255                         ep = seq->obuf;
3256                         while (ep - seq->obuf < (ssize_t)seq->obufused) {
3257
3258                                 ev = (snd_seq_event_t *) ep;
3259                                 len = snd_seq_event_length(ev);
3260
3261                                 if (remove_match(rmp, ev)) {
3262                                         /* Remove event */
3263                                         seq->obufused -= len;
3264                                         memmove(ep, ep + len, seq->obufused - (ep - seq->obuf));
3265                                 } else {
3266                                         ep += len;
3267                                 }
3268                         }
3269                 }
3270         }
3271
3272         return seq->ops->remove_events(seq, rmp);
3273 }
3274
3275 /*----------------------------------------------------------------*/
3276
3277 /*
3278  * client memory pool
3279  */
3280
3281 /**
3282  * \brief get size of #snd_seq_client_pool_t
3283  * \return size in bytes
3284  */
3285 size_t snd_seq_client_pool_sizeof()
3286 {
3287         return sizeof(snd_seq_client_pool_t);
3288 }
3289
3290 /**
3291  * \brief allocate an empty #snd_seq_client_pool_t using standard malloc
3292  * \param ptr returned pointer
3293  * \return 0 on success otherwise negative error code
3294  */
3295 int snd_seq_client_pool_malloc(snd_seq_client_pool_t **ptr)
3296 {
3297         assert(ptr);
3298         *ptr = calloc(1, sizeof(snd_seq_client_pool_t));
3299         if (!*ptr)
3300                 return -ENOMEM;
3301         return 0;
3302 }
3303
3304 /**
3305  * \brief frees a previously allocated #snd_seq_client_pool_t
3306  * \param pointer to object to free
3307  */
3308 void snd_seq_client_pool_free(snd_seq_client_pool_t *obj)
3309 {
3310         free(obj);
3311 }
3312
3313 /**
3314  * \brief copy one #snd_seq_client_pool_t to another
3315  * \param dst pointer to destination
3316  * \param src pointer to source
3317  */
3318 void snd_seq_client_pool_copy(snd_seq_client_pool_t *dst, const snd_seq_client_pool_t *src)
3319 {
3320         assert(dst && src);
3321         *dst = *src;
3322 }
3323
3324
3325 /**
3326  * \brief Get the client id of a queue_info container
3327  * \param info client_pool container
3328  * \return client id
3329  */
3330 int snd_seq_client_pool_get_client(const snd_seq_client_pool_t *info)
3331 {
3332         assert(info);
3333         return info->client;
3334 }
3335
3336 /**
3337  * \brief Get the output pool size of a queue_info container
3338  * \param info client_pool container
3339  * \return output pool size
3340  */
3341 size_t snd_seq_client_pool_get_output_pool(const snd_seq_client_pool_t *info)
3342 {
3343         assert(info);
3344         return info->output_pool;
3345 }
3346
3347 /**
3348  * \brief Get the input pool size of a queue_info container
3349  * \param info client_pool container
3350  * \return input pool size
3351  */
3352 size_t snd_seq_client_pool_get_input_pool(const snd_seq_client_pool_t *info)
3353 {
3354         assert(info);
3355         return info->input_pool;
3356 }
3357
3358 /**
3359  * \brief Get the output room size of a queue_info container
3360  * \param info client_pool container
3361  * \return output room size
3362  */
3363 size_t snd_seq_client_pool_get_output_room(const snd_seq_client_pool_t *info)
3364 {
3365         assert(info);
3366         return info->output_room;
3367 }
3368
3369 /**
3370  * \brief Get the available size on output pool of a queue_info container
3371  * \param info client_pool container
3372  * \return available output size
3373  */
3374 size_t snd_seq_client_pool_get_output_free(const snd_seq_client_pool_t *info)
3375 {
3376         assert(info);
3377         return info->output_free;
3378 }
3379
3380 /**
3381  * \brief Get the available size on input pool of a queue_info container
3382  * \param info client_pool container
3383  * \return available input size
3384  */
3385 size_t snd_seq_client_pool_get_input_free(const snd_seq_client_pool_t *info)
3386 {
3387         assert(info);
3388         return info->input_free;
3389 }
3390
3391 /**
3392  * \brief Set the output pool size of a queue_info container
3393  * \param info client_pool container
3394  * \param size output pool size
3395  */
3396 void snd_seq_client_pool_set_output_pool(snd_seq_client_pool_t *info, size_t size)
3397 {
3398         assert(info);
3399         info->output_pool = size;
3400 }
3401
3402 /**
3403  * \brief Set the input pool size of a queue_info container
3404  * \param info client_pool container
3405  * \param size input pool size
3406  */
3407 void snd_seq_client_pool_set_input_pool(snd_seq_client_pool_t *info, size_t size)
3408 {
3409         assert(info);
3410         info->input_pool = size;
3411 }
3412
3413 /**
3414  * \brief Set the output room size of a queue_info container
3415  * \param info client_pool container
3416  * \param size output room size
3417  */
3418 void snd_seq_client_pool_set_output_room(snd_seq_client_pool_t *info, size_t size)
3419 {
3420         assert(info);
3421         info->output_room = size;
3422 }
3423
3424
3425 /**
3426  * \brief obtain the pool information of the current client
3427  * \param seq sequencer handle
3428  * \param info information to be stored
3429  */
3430 int snd_seq_get_client_pool(snd_seq_t *seq, snd_seq_client_pool_t *info)
3431 {
3432         assert(seq && info);
3433         info->client = seq->client;
3434         return seq->ops->get_client_pool(seq, info);
3435 }
3436
3437 /**
3438  * \brief set the pool information
3439  * \param seq sequencer handle
3440  * \param info information to update
3441  *
3442  * Sets the pool information of the current client.
3443  * The client field in \a info is replaced automatically with the current id.
3444  */
3445 int snd_seq_set_client_pool(snd_seq_t *seq, snd_seq_client_pool_t *info)
3446 {
3447         assert(seq && info);
3448         info->client = seq->client;
3449         return seq->ops->set_client_pool(seq, info);
3450 }
3451
3452 /*----------------------------------------------------------------*/
3453
3454 /*
3455  * misc.
3456  */
3457
3458 /**
3459  * \brief set a bit flag
3460  */
3461 void snd_seq_set_bit(int nr, void *array)
3462 {
3463         ((unsigned int *)array)[nr >> 5] |= 1UL << (nr & 31);
3464 }
3465
3466 /**
3467  * \brief change a bit flag
3468  */
3469 int snd_seq_change_bit(int nr, void *array)
3470 {
3471         int result;
3472
3473         result = ((((unsigned int *)array)[nr >> 5]) & (1UL << (nr & 31))) ? 1 : 0;
3474         ((unsigned int *)array)[nr >> 5] |= 1UL << (nr & 31);
3475         return result;
3476 }
3477
3478 /**
3479  * \brief get a bit flag state
3480  */
3481 int snd_seq_get_bit(int nr, void *array)
3482 {
3483         return ((((unsigned int *)array)[nr >> 5]) & (1UL << (nr & 31))) ? 1 : 0;
3484 }
3485
3486
3487 /**
3488  * instrument layer
3489  */
3490
3491 /**
3492  * \brief get size of #snd_instr_header_t
3493  * \return size in bytes
3494  */
3495 size_t snd_instr_header_sizeof(void)
3496 {
3497         return sizeof(snd_instr_header_t);
3498 }
3499
3500 /**
3501  * \brief allocate an empty #snd_instr_header_t using standard malloc
3502  * \param ptr returned pointer
3503  * \param len additional data length
3504  * \return 0 on success otherwise negative error code
3505  */
3506 int snd_instr_header_malloc(snd_instr_header_t **ptr, size_t len)
3507 {
3508         assert(ptr);
3509         *ptr = calloc(1, sizeof(snd_instr_header_t) + len);
3510         if (!*ptr)
3511                 return -ENOMEM;
3512         (*ptr)->len = len;
3513         return 0;
3514 }
3515
3516 /**
3517  * \brief frees a previously allocated #snd_instr_header_t
3518  * \param pointer to object to free
3519  */
3520 void snd_instr_header_free(snd_instr_header_t *obj)
3521 {
3522         free(obj);
3523 }
3524
3525 /**
3526  * \brief copy one #snd_instr_header_t to another
3527  * \param dst pointer to destination
3528  * \param src pointer to source
3529  */
3530 void snd_instr_header_copy(snd_instr_header_t *dst, const snd_instr_header_t *src)
3531 {
3532         assert(dst && src);
3533         *dst = *src;
3534 }
3535
3536 /**
3537  * \brief Get the instrument id of an instr_header container
3538  * \param info instr_header container
3539  * \return instrument id pointer
3540  */
3541 const snd_seq_instr_t *snd_instr_header_get_id(const snd_instr_header_t *info)
3542 {
3543         assert(info);
3544         return (snd_seq_instr_t *)&info->id.instr;
3545 }
3546
3547 /**
3548  * \brief Get the cluster id of an instr_header container
3549  * \param info instr_header container
3550  * \return cluster id
3551  */
3552 snd_seq_instr_cluster_t snd_instr_header_get_cluster(const snd_instr_header_t *info)
3553 {
3554         assert(info);
3555         return info->id.cluster;
3556 }
3557
3558 /**
3559  * \brief Get the command of an instr_header container
3560  * \param info instr_header container
3561  * \return command type
3562  */
3563 unsigned int snd_instr_header_get_cmd(const snd_instr_header_t *info)
3564 {
3565         assert(info);
3566         return info->cmd;
3567 }
3568
3569 /**
3570  * \brief Get the length of extra data of an instr_header container
3571  * \param info instr_header container
3572  * \return the length in bytes
3573  */
3574 size_t snd_instr_header_get_len(const snd_instr_header_t *info)
3575 {
3576         assert(info);
3577         return info->len;
3578 }
3579
3580 /**
3581  * \brief Get the data name of an instr_header container
3582  * \param info instr_header container
3583  * \return the name string
3584  */
3585 const char *snd_instr_header_get_name(const snd_instr_header_t *info)
3586 {
3587         assert(info);
3588         return info->data.name;
3589 }
3590
3591 /**
3592  * \brief Get the data type of an instr_header container
3593  * \param info instr_header container
3594  * \return the data type
3595  */
3596 int snd_instr_header_get_type(const snd_instr_header_t *info)
3597 {
3598         assert(info);
3599         return info->data.type;
3600 }
3601
3602 /**
3603  * \brief Get the data format of an instr_header container
3604  * \param info instr_header container
3605  * \return the data format string
3606  */
3607 const char *snd_instr_header_get_format(const snd_instr_header_t *info)
3608 {
3609         assert(info);
3610         return info->data.data.format;
3611 }
3612
3613 /**
3614  * \brief Get the data alias of an instr_header container
3615  * \param info instr_header container
3616  * \return the data alias id
3617  */
3618 const snd_seq_instr_t *snd_instr_header_get_alias(const snd_instr_header_t *info)
3619 {
3620         assert(info);
3621         return (snd_seq_instr_t *)&info->data.data.alias;
3622 }
3623
3624 /**
3625  * \brief Get the extra data pointer of an instr_header container
3626  * \param info instr_header container
3627  * \return the extra data pointer
3628  */
3629 void *snd_instr_header_get_data(const snd_instr_header_t *info)
3630 {
3631         assert(info);
3632         return (void*)((char*)info + sizeof(*info));
3633 }
3634
3635 /**
3636  * \brief Get the flag to follow alias of an instr_header container
3637  * \param info instr_header container
3638  * \return 1 if follow alias
3639  */
3640 int snd_instr_header_get_follow_alias(const snd_instr_header_t *info)
3641 {
3642         assert(info);
3643         return (info->flags & SNDRV_SEQ_INSTR_QUERY_FOLLOW_ALIAS) ? 1 : 0;
3644 }
3645
3646 /**
3647  * \brief Set the instrument id of an instr_header container
3648  * \param info instr_header container
3649  * \param id instrument id pointer
3650  */
3651 void snd_instr_header_set_id(snd_instr_header_t *info, const snd_seq_instr_t *id)
3652 {
3653         assert(info && id);
3654         info->id.instr = *(struct sndrv_seq_instr *)id;
3655 }
3656
3657 /**
3658  * \brief Set the cluster id of an instr_header container
3659  * \param info instr_header container
3660  * \param cluster cluster id
3661  */
3662 void snd_instr_header_set_cluster(snd_instr_header_t *info, snd_seq_instr_cluster_t cluster)
3663 {
3664         assert(info);
3665         info->id.cluster = cluster;
3666 }
3667
3668 /**
3669  * \brief Set the command of an instr_header container
3670  * \param info instr_header container
3671  * \param cmd command type
3672  */
3673 void snd_instr_header_set_cmd(snd_instr_header_t *info, unsigned int cmd)
3674 {
3675         assert(info);
3676         info->cmd = cmd;
3677 }
3678
3679 /**
3680  * \brief Set the length of extra data of an instr_header container
3681  * \param info instr_header container
3682  * \param len size of extra data in bytes
3683  */
3684 void snd_instr_header_set_len(snd_instr_header_t *info, size_t len)
3685 {
3686         assert(info);
3687         info->len = len;
3688 }
3689
3690 /**
3691  * \brief Set the data name of an instr_header container
3692  * \param info instr_header container
3693  * \param name the name string
3694  */
3695 void snd_instr_header_set_name(snd_instr_header_t *info, const char *name)
3696 {
3697         assert(info && name);
3698         strncpy(info->data.name, name, sizeof(info->data.name));
3699 }
3700
3701 /**
3702  * \brief Set the data type of an instr_header container
3703  * \param info instr_header container
3704  * \param type the data type
3705  */
3706 void snd_instr_header_set_type(snd_instr_header_t *info, int type)
3707 {
3708         assert(info);
3709         info->data.type = type;
3710 }
3711
3712 /**
3713  * \brief Set the data format of an instr_header container
3714  * \param info instr_header container
3715  * \param format the data format string
3716  */
3717 void snd_instr_header_set_format(snd_instr_header_t *info, const char *format)
3718 {
3719         assert(info && format);
3720         strncpy(info->data.data.format, format, sizeof(info->data.data.format));
3721 }
3722
3723 /**
3724  * \brief Set the data alias id of an instr_header container
3725  * \param info instr_header container
3726  * \param instr alias instrument id
3727  */
3728 void snd_instr_header_set_alias(snd_instr_header_t *info, const snd_seq_instr_t *instr)
3729 {
3730         assert(info && instr);
3731         info->data.data.alias = *(struct sndrv_seq_instr *)instr;
3732 }
3733
3734 /**
3735  * \brief Set the flag to follow alias of an instr_header container
3736  * \param info instr_header container
3737  * \param val 1 if follow alias
3738  */
3739 void snd_instr_header_set_follow_alias(snd_instr_header_t *info, int val)
3740 {
3741         assert(info);
3742         if (val)
3743                 info->flags |= SNDRV_SEQ_INSTR_QUERY_FOLLOW_ALIAS;
3744         else
3745                 info->flags &= ~SNDRV_SEQ_INSTR_QUERY_FOLLOW_ALIAS;
3746 }