File: aconnect.py

package info (click to toggle)
python-pyalsa 1.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 496 kB
  • sloc: ansic: 5,616; python: 1,260; makefile: 4
file content (205 lines) | stat: -rw-r--r-- 7,018 bytes parent folder | download | duplicates (4)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#! /usr/bin/python

# aconnect.py -- python port of aconnect
# Copyright (C) 2008 Aldrin Martoq <amartoq@dcc.uchile.cl>
#
# Based on code from aconnect.c from ALSA project
# Copyright (C) 1999 Takashi Iwai
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  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


import sys
sys.path.insert(0, '../pyalsa')

import getopt
import os
from alsaseq import *

LIST_INPUT=1
LIST_OUTPUT=2

def errormsg(msg, *args):
    """ prints an error message to stderr """
    sys.stderr.write(msg % args)
    sys.stderr.write('\n')
    traceback.print_exc(file=sys.stderr)

def fatal(msg, *args):
    """ prints an error message to stderr, and dies """
    errormsg(msg, *args)
    sys.exit(1)

def init_seq():
    """ opens an alsa sequencer """
    try:
        sequencer = Sequencer(name = 'default',
                              clientname = 'aconnect.py',
                              streams = SEQ_OPEN_DUPLEX,
                              mode = SEQ_BLOCK)
        return sequencer
    except SequencerError, e:
        fatal("open sequencer: %e", e)


def usage():
    print  \
        "aconnect - python ALSA sequencer connection manager\n" \
        "Copyright (C) 1999-2000 Takashi Iwai\n" \
        "Copyright (C) 2008 Aldrin Martoq <amartoq@dcc.uchile.cl>\n" \
        "Usage: \n" \
        " * Connection/disconnection between ports\n" \
        "   %s [-options] sender receiver\n" \
        "     sender, receiver = client:port pair\n" \
        "     -d,--disconnect     disconnect\n" \
        "     -e,--exclusive      exclusive connection\n" \
        "     -r,--real #         convert real-time-stamp on queue\n" \
        "     -t,--tick #         convert tick-time-stamp on queue\n" \
        " * List connected ports (no subscription action)\n" \
        "   %s -i|-o [-options]\n" \
        "     -i,--input          list input (readable) ports\n" \
        "     -o,--output         list output (writable) ports\n" \
        "     -l,--list           list current connections of each port\n" \
        " * Remove all exported connections\n" \
        "     -x,--removeall" \
        % (sys.argv[0], sys.argv[0])

def do_list_subs(conn, text):
    l = []
    for c, p, i in conn:
        s = ""
        if i['exclusive']:
            s+= "[ex]"
        if i['time_update']:
            if i['time_real']:
                s += "[%s:%s]" % ("real", i['queue'])
            else:
                s += "[%s:%s]" % ("tick", i['queue'])
        s = "%d:%d%s" % (c, p, s)
        l.append(s)
    if len(l):
        print "\t%s: %s" % (text, ', '.join(l))


def do_list_ports(sequencer, list_perm, list_subs):
    connectionlist = sequencer.connection_list()
    for connections in connectionlist:
        clientname, clientid, connectedports = connections
        clientinfo = sequencer.get_client_info(clientid)
        type = clientinfo['type']
        count = []
        if type == SEQ_USER_CLIENT:
            type = 'user'
        else:
            type = 'kernel'
        for port in connectedports:
            portname, portid, portconns = port
            portinfo = sequencer.get_port_info(portid, clientid)
            caps = portinfo['capability']
            if not (caps & SEQ_PORT_CAP_NO_EXPORT):
                reads = SEQ_PORT_CAP_READ | SEQ_PORT_CAP_SUBS_READ
                write = SEQ_PORT_CAP_WRITE | SEQ_PORT_CAP_SUBS_WRITE
                if (list_perm & LIST_INPUT) and (caps & reads == reads):
                    count.append(port)
                elif (list_perm & LIST_OUTPUT) and (caps & write == write):
                    count.append(port)
        if len(count) > 0:
            print "client %d: '%s' [type=%s]" % (clientid, clientname, type)
            for port in count:
                portname, portid, portconns = port
                print "  %3d '%-16s'" % (portid, portname)
                if list_subs:
                    readconn, writeconn = portconns
                    do_list_subs(readconn, "Connecting To")
                    do_list_subs(writeconn, "Connected From")

def do_unsubscribe(sequencer, args):
    sender = sequencer.parse_address(args[0])
    dest = sequencer.parse_address(args[1])
    try:
        sequencer.get_connect_info(sender, dest)
    except SequencerError:
        print 'No subscription is found'
        return
    try:
        sequencer.disconnect_ports(sender, dest)
    except SequencerError, e:
        fatal("Failed to disconnect ports %s->%s - %s", sender, dest, e)

def do_subscribe(sequencer, args, queue, exclusive, convert_time, convert_real):
    sender = sequencer.parse_address(args[0])
    dest = sequencer.parse_address(args[1])
    try:
        sequencer.get_connect_info(sender, dest)
        print "Connection is already subscribed"
        return
    except SequencerError:
        pass
    sequencer.connect_ports(sender, dest, queue, exclusive, convert_time, convert_real)

def main():
    sequencer = init_seq()
    command = 'subscribe'
    list_perm = 0
    list_subs = 0
    exclusive = 0
    convert_time = 0
    convert_real = 0
    queue = 0

    try:
        opts, args = getopt.getopt(sys.argv[1:], "dior:t:elx", ["disconnect", "input", "output", "real=", "tick=", "exclusive", "list", "removeall"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-d", "--disconnect"):
            command = 'unsubscribe'
        elif o in ("-i", "--input"):
            command = 'list'
            list_perm |= LIST_INPUT
        elif o in ("-o", "--output"):
            command = 'list'
            list_perm |= LIST_OUTPUT
        elif o in ("-e", "--exclusive"):
            exclusive = 1
        elif o in ("-r", "--real"):
            queue = int(a)
            convert_time = 1
            convert_real = 1
        elif o in ("-t", "--tick"):
            queue = int(a)
            convert_time = 1
            convert_real = 0
        elif o in ("-l", "--list"):
            list_subs = 1
        elif o in ("-x", "--removeall"):
            command = 'removeall'

    if command == 'list':
        do_list_ports(sequencer, list_perm, list_subs)
        return

    if len(args) != 2:
        usage()
        sys.exit(2)

    if command == 'unsubscribe':
        do_unsubscribe(sequencer, args)
    elif command == 'subscribe':
        do_subscribe(sequencer, args, queue, exclusive, convert_time, convert_real)


if __name__ == '__main__':
    main()