~ubuntu-branches/ubuntu/lucid/jack-audio-connection-kit/lucid

« back to all changes in this revision

Viewing changes to tools/ipload.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-12-06 11:05:15 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081206110515-xa9v9pajr9jqvfvg
Tags: 0.115.6-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - Redirect stderr in bash completion (Debian #504488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
#include <signal.h>
 
5
#include <unistd.h>
 
6
#include <getopt.h>
 
7
#include <jack/jack.h>
 
8
#include <jack/intclient.h>
 
9
 
 
10
jack_client_t *client;
 
11
jack_intclient_t intclient;
 
12
char *client_name;
 
13
char *intclient_name;
 
14
char *load_name;
 
15
char *load_init = NULL;
 
16
char *server_name = NULL;
 
17
int wait_opt = 0;
 
18
 
 
19
void
 
20
signal_handler (int sig)
 
21
{
 
22
        jack_status_t status;
 
23
 
 
24
        fprintf (stderr, "signal received, unloading...");
 
25
        status = jack_internal_client_unload (client, intclient);
 
26
        if (status & JackFailure)
 
27
                fprintf (stderr, "(failed), status = 0x%2.0x\n", status);
 
28
        else
 
29
                fprintf (stderr, "(succeeded)\n");
 
30
        jack_client_close (client);
 
31
        exit (0);
 
32
}
 
33
 
 
34
void
 
35
show_usage ()
 
36
{
 
37
        fprintf (stderr, "usage: %s [ options ] client-name [ load-name "
 
38
                 "[ init-string]]\n\noptions:\n", client_name);
 
39
        fprintf (stderr,
 
40
                 "\t-h, --help \t\t print help message\n"
 
41
                 "\t-i, --init string\t initialize string\n"
 
42
                 "\t-s, --server name\t select JACK server\n"
 
43
                 "\t-w, --wait \t\t wait for signal, then unload\n"
 
44
                 "\n"
 
45
                );
 
46
}
 
47
 
 
48
int
 
49
parse_args (int argc, char *argv[])
 
50
{
 
51
        int c;
 
52
        int option_index = 0;
 
53
        char *short_options = "hi:s:w";
 
54
        struct option long_options[] = {
 
55
                { "help", 0, 0, 'h' },
 
56
                { "init", required_argument, 0, 'i' },
 
57
                { "server", required_argument, 0, 's' },
 
58
                { "wait", 0, 0, 'w' },
 
59
                { 0, 0, 0, 0 }
 
60
        };
 
61
 
 
62
        client_name = strrchr(argv[0], '/');
 
63
        if (client_name == NULL) {
 
64
                client_name = argv[0];
 
65
        } else {
 
66
                client_name++;
 
67
        }
 
68
 
 
69
        while ((c = getopt_long (argc, argv, short_options, long_options,
 
70
                                 &option_index)) >= 0) {
 
71
                switch (c) {
 
72
                case 'i':
 
73
                        load_init = optarg;
 
74
                        break;
 
75
                case 's':
 
76
                        server_name = optarg;
 
77
                        break;
 
78
                case 'w':
 
79
                        wait_opt = 1;
 
80
                        break;
 
81
                case 'h':
 
82
                default:
 
83
                        show_usage ();
 
84
                        return 1;
 
85
                }
 
86
        }
 
87
 
 
88
        if (optind == argc) {           /* no positional args? */
 
89
                show_usage ();
 
90
                return 1;
 
91
        }
 
92
        if (optind < argc)
 
93
                load_name = intclient_name = argv[optind++];
 
94
 
 
95
        if (optind < argc)
 
96
                load_name = argv[optind++];
 
97
 
 
98
        if (optind < argc)
 
99
                load_init = argv[optind++];
 
100
 
 
101
        //fprintf (stderr, "client-name = `%s', load-name = `%s', "
 
102
        //       "load-init = `%s', wait = %d\n",
 
103
        //       intclient_name, load_name, load_init, wait_opt);
 
104
 
 
105
        return 0;                       /* args OK */
 
106
}
 
107
 
 
108
int
 
109
main (int argc, char *argv[])
 
110
{
 
111
        jack_status_t status;
 
112
 
 
113
        /* parse and validate command arguments */
 
114
        if (parse_args (argc, argv))
 
115
                exit (1);               /* invalid command line */
 
116
 
 
117
        /* first, become a JACK client */
 
118
        client = jack_client_open (client_name, JackServerName,
 
119
                                   &status, server_name);
 
120
        if (client == NULL) {
 
121
                fprintf (stderr, "jack_client_open() failed, "
 
122
                         "status = 0x%2.0x\n", status);
 
123
                if (status & JackServerFailed) {
 
124
                        fprintf (stderr, "Unable to connect to JACK server\n");
 
125
                }
 
126
                exit (1);
 
127
        }
 
128
        if (status & JackServerStarted) {
 
129
                fprintf (stderr, "JACK server started\n");
 
130
        }
 
131
        if (status & JackNameNotUnique) {
 
132
                client_name = jack_get_client_name(client);
 
133
                fprintf (stderr, "unique name `%s' assigned\n", client_name);
 
134
        }
 
135
 
 
136
        /* then, load the internal client */
 
137
        intclient = jack_internal_client_load (client, intclient_name,
 
138
                                               (JackLoadName|JackLoadInit),
 
139
                                               &status, load_name, load_init);
 
140
        if (status & JackFailure) {
 
141
                fprintf (stderr, "could not load %s, status = 0x%2.0x\n",
 
142
                         load_name, status);
 
143
                return 2;
 
144
        }
 
145
        if (status & JackNameNotUnique) {
 
146
                intclient_name =
 
147
                        jack_get_internal_client_name (client, intclient);
 
148
                fprintf (stderr, "unique internal client name `%s' assigned\n",
 
149
                         intclient_name);
 
150
        }
 
151
 
 
152
        fprintf (stdout, "%s is running.\n", load_name);
 
153
 
 
154
        if (wait_opt) {
 
155
                /* define a signal handler to unload the client, then
 
156
                 * wait for it to exit */
 
157
                signal (SIGQUIT, signal_handler);
 
158
                signal (SIGTERM, signal_handler);
 
159
                signal (SIGHUP, signal_handler);
 
160
                signal (SIGINT, signal_handler);
 
161
 
 
162
                while (1) {
 
163
                        sleep (1);
 
164
                }
 
165
        }
 
166
 
 
167
        return 0;
 
168
}
 
169
        
 
170