File: gui_midi.c

package info (click to toggle)
hexter 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,184 kB
  • sloc: ansic: 10,188; sh: 10,061; makefile: 76
file content (140 lines) | stat: -rw-r--r-- 3,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* hexter DSSI software synthesizer GUI
 *
 * Copyright (C) 2004, 2009 Sean Bolton and others.
 *
 * Portions of this file may have come from specimen, copyright
 * (c) 2004 Pete Bessman under the GNU General Public License
 * version 2.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#ifdef MIDI_ALSA

#include <alsa/asoundlib.h>

#include <gtk/gtk.h>

#include "gui_main.h"
#include "gui_midi.h"

int        sysex_enabled = 0;

snd_seq_t *sysex_seq_handle;
int        sysex_seq_client_id;
int        sysex_seq_port_id;
gint       sysex_seq_gtk_fd_tag;

sysex_callback_function *sysex_handler_callback;

void
sysex_receive_callback(gpointer data, gint source,
                       GdkInputCondition condition)
{
    snd_seq_event_t *ev;

    do {
        if (snd_seq_event_input(sysex_seq_handle, &ev) <= 0)
            continue;

        if (ev->type == SND_SEQ_EVENT_SYSEX) {
            (*sysex_handler_callback)(ev->data.ext.len, ev->data.ext.ptr);
        }

        snd_seq_free_event(ev);

    } while (snd_seq_event_input_pending(sysex_seq_handle, 0) > 0);
}

int
sysex_start(sysex_callback_function *handler, char **errmsg)
{
    char buf[33];
    int npfd;
    struct pollfd pfd;

    if (snd_seq_open(&sysex_seq_handle, "default", SND_SEQ_OPEN_INPUT,
                     SND_SEQ_NONBLOCK) < 0) {
         *errmsg = "Error opening ALSA sequencer.\n";
         return 0;
    }

    snprintf(buf, 33, "hexter %s", user_friendly_id);
    snd_seq_set_client_name(sysex_seq_handle, buf);

    sysex_seq_client_id = snd_seq_client_id(sysex_seq_handle);

    if ((sysex_seq_port_id = snd_seq_create_simple_port(sysex_seq_handle,
                                 "sys-ex receive",
                                 SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
                                 SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
        *errmsg = "Error creating sequencer port.\n";
        snd_seq_close(sysex_seq_handle);
        return 0;
    }

    sysex_handler_callback = handler;

    /* tell GTK+ to watch seq fd */
    npfd = snd_seq_poll_descriptors_count(sysex_seq_handle, POLLIN);
    if (npfd != 1) {
        *errmsg = "Incompatible ALSA version.\n";
        snd_seq_close(sysex_seq_handle);
        return 0;
    }
    snd_seq_poll_descriptors(sysex_seq_handle, &pfd, npfd, POLLIN);
    sysex_seq_gtk_fd_tag = gdk_input_add(pfd.fd, GDK_INPUT_READ,
                                         sysex_receive_callback, NULL);

    sysex_enabled = 1;
    return 1;
}

void
sysex_stop(void)
{
    gdk_input_remove(sysex_seq_gtk_fd_tag);
    snd_seq_delete_simple_port (sysex_seq_handle, sysex_seq_port_id);
    snd_seq_close(sysex_seq_handle);
    sysex_enabled = 0;
}

#else /* MIDI_ALSA */

#include "gui_main.h"
#include "gui_midi.h"

int sysex_enabled = 0;

int
sysex_start(sysex_callback_function *handler, char **errmsg)
{
    *errmsg = "MIDI sys-ex support not available!\n";
    sysex_enabled = 0;
    return 0;
}

void
sysex_stop(void)
{
    sysex_enabled = 0;
}

#endif /* MIDI_ALSA */