summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/s_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/s_main.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/s_main.c1676
1 files changed, 1676 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/s_main.c b/apps/plugins/pdbox/PDa/src/s_main.c
new file mode 100644
index 0000000000..84f4cfbbc2
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/s_main.c
@@ -0,0 +1,1676 @@
1/* Copyright (c) 1997-1999 Miller Puckette and others.
2* For information on usage and redistribution, and for a DISCLAIMER OF ALL
3* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
4
5/* IOhannes :
6 * hacked the code to add advanced multidevice-support
7 * 1311:forum::für::umläute:2001
8 */
9
10char pd_version[] = "Pd version 0.37.4\n";
11char pd_compiletime[] = __TIME__;
12char pd_compiledate[] = __DATE__;
13
14#include "m_pd.h"
15#include "m_imp.h"
16#include "s_stuff.h"
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <limits.h>
20#include <string.h>
21#include <stdio.h>
22#include <fcntl.h>
23#include <stdlib.h>
24
25#ifdef UNIX
26#include <unistd.h>
27#endif
28#ifdef MSW
29#include <io.h>
30#include <windows.h>
31#include <winbase.h>
32#endif
33
34void pd_init(void);
35int sys_argparse(int argc, char **argv);
36void sys_findprogdir(char *progname);
37int sys_startgui(const char *guipath);
38int sys_rcfile(void);
39int m_scheduler(void);
40void sys_addhelppath(char *p);
41void alsa_adddev(char *name);
42
43int sys_debuglevel;
44int sys_verbose;
45int sys_noloadbang;
46int sys_nogui;
47int sys_stdin = 0;
48char *sys_guicmd;
49t_symbol *sys_libdir;
50static t_symbol *sys_guidir;
51static t_namelist *sys_externlist;
52static t_namelist *sys_openlist;
53static t_namelist *sys_messagelist;
54static int sys_version;
55int sys_oldtclversion; /* hack to warn g_rtext.c about old text sel */
56
57int sys_nmidiout = 1;
58#ifdef MSW
59int sys_nmidiin = 0;
60#else
61int sys_nmidiin = 1;
62#endif
63int sys_midiindevlist[MAXMIDIINDEV] = {1};
64int sys_midioutdevlist[MAXMIDIOUTDEV] = {1};
65
66static int sys_main_srate = DEFAULTSRATE;
67static int sys_main_advance = DEFAULTADVANCE;
68
69/* IOhannes { */
70
71 /* here the "-1" counts signify that the corresponding vector hasn't been
72 specified in command line arguments; sys_open_audio will detect this
73 and fill things in. */
74int sys_nsoundin = -1;
75int sys_nsoundout = -1;
76int sys_soundindevlist[MAXAUDIOINDEV];
77int sys_soundoutdevlist[MAXAUDIOOUTDEV];
78
79int sys_nchin = -1;
80int sys_nchout = -1;
81int sys_chinlist[MAXAUDIOINDEV];
82int sys_choutlist[MAXAUDIOOUTDEV];
83/* } IOhannes */
84
85
86typedef struct _fontinfo
87{
88 int fi_fontsize;
89 int fi_maxwidth;
90 int fi_maxheight;
91 int fi_hostfontsize;
92 int fi_width;
93 int fi_height;
94} t_fontinfo;
95
96 /* these give the nominal point size and maximum height of the characters
97 in the six fonts. */
98
99static t_fontinfo sys_fontlist[] = {
100 {8, 5, 9, 0, 0, 0}, {10, 7, 13, 0, 0, 0}, {12, 9, 16, 0, 0, 0},
101 {16, 10, 20, 0, 0, 0}, {24, 15, 25, 0, 0, 0}, {36, 25, 45, 0, 0, 0}};
102#define NFONT (sizeof(sys_fontlist)/sizeof(*sys_fontlist))
103
104/* here are the actual font size structs on msp's systems:
105MSW:
106font 8 5 9 8 5 11
107font 10 7 13 10 6 13
108font 12 9 16 14 8 16
109font 16 10 20 16 10 18
110font 24 15 25 16 10 18
111font 36 25 42 36 22 41
112
113linux:
114font 8 5 9 8 5 9
115font 10 7 13 12 7 13
116font 12 9 16 14 9 15
117font 16 10 20 16 10 19
118font 24 15 25 24 15 24
119font 36 25 42 36 22 41
120*/
121
122static t_fontinfo *sys_findfont(int fontsize)
123{
124 unsigned int i;
125 t_fontinfo *fi;
126 for (i = 0, fi = sys_fontlist; i < (NFONT-1); i++, fi++)
127 if (fontsize < fi[1].fi_fontsize) return (fi);
128 return (sys_fontlist + (NFONT-1));
129}
130
131int sys_nearestfontsize(int fontsize)
132{
133 return (sys_findfont(fontsize)->fi_fontsize);
134}
135
136int sys_hostfontsize(int fontsize)
137{
138 return (sys_findfont(fontsize)->fi_hostfontsize);
139}
140
141int sys_fontwidth(int fontsize)
142{
143 return (sys_findfont(fontsize)->fi_width);
144}
145
146int sys_fontheight(int fontsize)
147{
148 return (sys_findfont(fontsize)->fi_height);
149}
150
151int sys_defaultfont;
152#ifdef MSW
153#define DEFAULTFONT 12
154#else
155#define DEFAULTFONT 10
156#endif
157
158static void openit(const char *dirname, const char *filename)
159{
160 char dirbuf[MAXPDSTRING], *nameptr;
161 int fd = open_via_path(dirname, filename, "", dirbuf, &nameptr,
162 MAXPDSTRING, 0);
163 if (fd)
164 {
165 close (fd);
166 glob_evalfile(0, gensym(nameptr), gensym(dirbuf));
167 }
168 else
169 error("%s: can't open", filename);
170}
171
172#define NHOSTFONT 7
173
174/* this is called from the gui process. The first argument is the cwd, and
175succeeding args give the widths and heights of known fonts. We wait until
176these are known to open files and send messages specified on the command line.
177We ask the GUI to specify the "cwd" in case we don't have a local OS to get it
178from; for instance we could be some kind of RT embedded system. However, to
179really make this make sense we would have to implement
180open(), read(), etc, calls to be served somehow from the GUI too. */
181
182void glob_initfromgui(void *dummy, t_symbol *s, int argc, t_atom *argv)
183{
184 char *cwd = atom_getsymbolarg(0, argc, argv)->s_name;
185 t_namelist *nl;
186 unsigned int i, j;
187 if (argc != 2 + 3 * NHOSTFONT) bug("glob_initfromgui");
188 for (i = 0; i < NFONT; i++)
189 {
190 int wantheight = sys_fontlist[i].fi_maxheight;
191 for (j = 0; j < NHOSTFONT-1; j++)
192 {
193 if (atom_getintarg(3 * (j + 1) + 3, argc, argv) > wantheight)
194 break;
195 }
196 /* j is now the "real" font index for the desired font index i. */
197 sys_fontlist[i].fi_hostfontsize = atom_getintarg(3 * j + 1, argc, argv);
198 sys_fontlist[i].fi_width = atom_getintarg(3 * j + 2, argc, argv);
199 sys_fontlist[i].fi_height = atom_getintarg(3 * j + 3, argc, argv);
200 }
201#if 0
202 for (i = 0; i < 6; i++)
203 fprintf(stderr, "font %d %d %d %d %d\n",
204 sys_fontlist[i].fi_fontsize,
205 sys_fontlist[i].fi_maxheight,
206 sys_fontlist[i].fi_hostfontsize,
207 sys_fontlist[i].fi_width,
208 sys_fontlist[i].fi_height);
209#endif
210 /* load dynamic libraries specified with "-lib" args */
211 for (nl = sys_externlist; nl; nl = nl->nl_next)
212 if (!sys_load_lib(cwd, nl->nl_string))
213 post("%s: can't load library", nl->nl_string);
214 namelist_free(sys_externlist);
215 sys_externlist = 0;
216 /* open patches specifies with "-open" args */
217 for (nl = sys_openlist; nl; nl = nl->nl_next)
218 openit(cwd, nl->nl_string);
219 namelist_free(sys_openlist);
220 sys_openlist = 0;
221 /* send messages specified with "-send" args */
222 for (nl = sys_messagelist; nl; nl = nl->nl_next)
223 {
224 t_binbuf *b = binbuf_new();
225 binbuf_text(b, nl->nl_string, strlen(nl->nl_string));
226 binbuf_eval(b, 0, 0, 0);
227 binbuf_free(b);
228 }
229 namelist_free(sys_messagelist);
230 sys_messagelist = 0;
231 sys_oldtclversion = atom_getfloatarg(1 + 3 * NHOSTFONT, argc, argv);
232}
233
234static void sys_afterargparse(void);
235
236/* this is called from main() in s_entry.c */
237int sys_main(int argc, char **argv)
238{
239#ifdef PD_DEBUG
240 fprintf(stderr, "Pd: COMPILED FOR DEBUGGING\n");
241#endif
242 pd_init();
243 sys_findprogdir(argv[0]); /* set sys_progname, guipath */
244#ifdef UNIX
245 sys_rcfile(); /* parse the startup file */
246#endif
247 if (sys_argparse(argc, argv)) /* parse cmd line */
248 return (1);
249 sys_afterargparse(); /* post-argparse settings */
250 if (sys_verbose || sys_version) fprintf(stderr, "%scompiled %s %s\n",
251 pd_version, pd_compiletime, pd_compiledate);
252 if (sys_version) /* if we were just asked our version, exit here. */
253 return (0);
254 if (sys_startgui(sys_guidir->s_name)) /* start the gui */
255 return(1);
256 /* open audio and MIDI */
257 sys_open_midi(sys_nmidiin, sys_midiindevlist,
258 sys_nmidiout, sys_midioutdevlist);
259 sys_open_audio(sys_nsoundin, sys_soundindevlist, sys_nchin, sys_chinlist,
260 sys_nsoundout, sys_soundoutdevlist, sys_nchout, sys_choutlist,
261 sys_main_srate, sys_main_advance, 1);
262 /* run scheduler until it quits */
263
264 return (m_scheduler_pda());
265 return (m_scheduler());
266}
267
268static char *(usagemessage[]) = {
269"usage: pd [-flags] [file]...\n",
270"\naudio configuration flags:\n",
271"-r <n> -- specify sample rate\n",
272"-audioindev ... -- audio in devices; e.g., \"1,3\" for first and third\n",
273"-audiooutdev ... -- audio out devices (same)\n",
274"-audiodev ... -- specify input and output together\n",
275"-inchannels ... -- audio input channels (by device, like \"2\" or \"16,8\")\n",
276"-outchannels ... -- number of audio out channels (same)\n",
277"-channels ... -- specify both input and output channels\n",
278"-audiobuf <n> -- specify size of audio buffer in msec\n",
279"-blocksize <n> -- specify audio I/O block size in sample frames\n",
280"-sleepgrain <n> -- specify number of milliseconds to sleep when idle\n",
281"-nodac -- suppress audio output\n",
282"-noadc -- suppress audio input\n",
283"-noaudio -- suppress audio input and output (-nosound is synonym) \n",
284"-listdev -- list audio and MIDI devices\n",
285
286#ifdef USEAPI_OSS
287"-oss -- use OSS audio API\n",
288"-32bit ----- allow 32 bit OSS audio (for RME Hammerfall)\n",
289#endif
290
291#ifdef USEAPI_ALSA
292"-alsa -- use ALSA audio API\n",
293"-alsaadd <name> -- add an ALSA device name to list\n",
294"-alsadev <n> ----- obsolete: use -audiodev\n",
295#endif
296
297#ifdef USEAPI_JACK
298"-jack -- use JACK audio API\n",
299#endif
300
301#ifdef USEAPI_PORTAUDIO
302#ifdef MSW
303"-asio -- use ASIO audio driver (via Portaudio)\n",
304"-pa -- synonym for -asio\n",
305#else
306"-pa -- use Portaudio API\n",
307#endif
308#endif
309
310#ifdef USEAPI_MMIO
311"-mmio -- use MMIO audio API (default for Windows)\n",
312#endif
313" (default audio API for this platform: ", API_DEFSTRING, ")\n\n",
314
315"\nMIDI configuration flags:\n",
316"-midiindev ... -- midi in device list; e.g., \"1,3\" for first and third\n",
317"-midioutdev ... -- midi out device list, same format\n",
318"-mididev ... -- specify -midioutdev and -midiindev together\n",
319"-nomidiin -- suppress MIDI input\n",
320"-nomidiout -- suppress MIDI output\n",
321"-nomidi -- suppress MIDI input and output\n",
322
323"\nother flags:\n",
324"-path <path> -- add to file search path\n",
325"-helppath <path> -- add to help file search path\n",
326"-open <file> -- open file(s) on startup\n",
327"-lib <file> -- load object library(s)\n",
328"-font <n> -- specify default font size in points\n",
329"-verbose -- extra printout on startup and when searching for files\n",
330"-version -- don't run Pd; just print out which version it is \n",
331"-d <n> -- specify debug level\n",
332"-noloadbang -- suppress all loadbangs\n",
333"-nogui -- suppress starting the GUI\n",
334"-stdin -- scan stdin for keypresses\n",
335"-guicmd \"cmd...\" -- substitute another GUI program (e.g., rsh)\n",
336"-send \"msg...\" -- send a message at startup (after patches are loaded)\n",
337#ifdef UNIX
338"-rt or -realtime -- use real-time priority\n",
339"-nrt -- don't use real-time priority\n",
340#endif
341};
342
343static void sys_parsedevlist(int *np, int *vecp, int max, char *str)
344{
345 int n = 0;
346 while (n < max)
347 {
348 if (!*str) break;
349 else
350 {
351 char *endp;
352 vecp[n] = strtol(str, &endp, 10);
353 if (endp == str)
354 break;
355 n++;
356 if (!endp)
357 break;
358 str = endp + 1;
359 }
360 }
361 *np = n;
362}
363
364static int sys_getmultidevchannels(int n, int *devlist)
365{
366 int sum = 0;
367 if (n<0)return(-1);
368 if (n==0)return 0;
369 while(n--)sum+=*devlist++;
370 return sum;
371}
372
373
374 /* this routine tries to figure out where to find the auxilliary files
375 Pd will need to run. This is either done by looking at the command line
376 invokation for Pd, or if that fails, by consulting the variable
377 INSTALL_PREFIX. In MSW, we don't try to use INSTALL_PREFIX. */
378void sys_findprogdir(char *progname)
379{
380 char sbuf[MAXPDSTRING], sbuf2[MAXPDSTRING], *sp;
381 char *lastslash;
382#ifdef UNIX
383 struct stat statbuf;
384#endif
385
386 /* find out by what string Pd was invoked; put answer in "sbuf". */
387#ifdef MSW
388 GetModuleFileName(NULL, sbuf2, sizeof(sbuf2));
389 sbuf2[MAXPDSTRING-1] = 0;
390 sys_unbashfilename(sbuf2, sbuf);
391#endif /* MSW */
392#ifdef UNIX
393 strncpy(sbuf, progname, MAXPDSTRING);
394 sbuf[MAXPDSTRING-1] = 0;
395#endif
396 lastslash = strrchr(sbuf, '/');
397 if (lastslash)
398 {
399 /* bash last slash to zero so that sbuf is directory pd was in,
400 e.g., ~/pd/bin */
401 *lastslash = 0;
402 /* go back to the parent from there, e.g., ~/pd */
403 lastslash = strrchr(sbuf, '/');
404 if (lastslash)
405 {
406 strncpy(sbuf2, sbuf, lastslash-sbuf);
407 sbuf2[lastslash-sbuf] = 0;
408 }
409 else strcpy(sbuf2, "..");
410 }
411 else
412 {
413 /* no slashes found. Try INSTALL_PREFIX. */
414#ifdef INSTALL_PREFIX
415 strcpy(sbuf2, INSTALL_PREFIX);
416#else
417 strcpy(sbuf2, ".");
418#endif
419 }
420 /* now we believe sbuf2 holds the parent directory of the directory
421 pd was found in. We now want to infer the "lib" directory and the
422 "gui" directory. In "simple" UNIX installations, the layout is
423 .../bin/pd
424 .../bin/pd-gui
425 .../doc
426 and in "complicated" UNIX installations, it's:
427 .../bin/pd
428 .../lib/pd/bin/pd-gui
429 .../lib/pd/doc
430 To decide which, we stat .../lib/pd; if that exists, we assume it's
431 the complicated layout. In MSW, it's the "simple" layout, but
432 the gui program is straight wish80:
433 .../bin/pd
434 .../bin/wish80.exe
435 .../doc
436 */
437#ifdef UNIX
438 strncpy(sbuf, sbuf2, MAXPDSTRING-30);
439 sbuf[MAXPDSTRING-30] = 0;
440 strcat(sbuf, "/lib/pd");
441 if (stat(sbuf, &statbuf) >= 0)
442 {
443 /* complicated layout: lib dir is the one we just stat-ed above */
444 sys_libdir = gensym(sbuf);
445 /* gui lives in .../lib/pd/bin */
446 strncpy(sbuf, sbuf2, MAXPDSTRING-30);
447 sbuf[MAXPDSTRING-30] = 0;
448 strcat(sbuf, "/lib/pd/bin");
449 sys_guidir = gensym(sbuf);
450 }
451 else
452 {
453 /* simple layout: lib dir is the parent */
454 sys_libdir = gensym(sbuf2);
455 /* gui lives in .../bin */
456 strncpy(sbuf, sbuf2, MAXPDSTRING-30);
457 sbuf[MAXPDSTRING-30] = 0;
458 strcat(sbuf, "/bin");
459 sys_guidir = gensym(sbuf);
460 }
461#endif
462#ifdef MSW
463 sys_libdir = gensym(sbuf2);
464 sys_guidir = &s_; /* in MSW the guipath just depends on the libdir */
465#endif
466}
467
468#ifdef MSW
469static int sys_mmio = 1;
470#else
471static int sys_mmio = 0;
472#endif
473
474int sys_argparse(int argc, char **argv)
475{
476 char sbuf[MAXPDSTRING];
477 int i;
478 argc--; argv++;
479 while ((argc > 0) && **argv == '-')
480 {
481 if (!strcmp(*argv, "-r") && argc > 1 &&
482 sscanf(argv[1], "%d", &sys_main_srate) >= 1)
483 {
484 argc -= 2;
485 argv += 2;
486 }
487 else if (!strcmp(*argv, "-inchannels"))
488 { /* IOhannes */
489 sys_parsedevlist(&sys_nchin,
490 sys_chinlist, MAXAUDIOINDEV, argv[1]);
491
492 if (!sys_nchin)
493 goto usage;
494
495 argc -= 2; argv += 2;
496 }
497 else if (!strcmp(*argv, "-outchannels"))
498 { /* IOhannes */
499 sys_parsedevlist(&sys_nchout, sys_choutlist,
500 MAXAUDIOOUTDEV, argv[1]);
501
502 if (!sys_nchout)
503 goto usage;
504
505 argc -= 2; argv += 2;
506 }
507 else if (!strcmp(*argv, "-channels"))
508 {
509 sys_parsedevlist(&sys_nchin, sys_chinlist,MAXAUDIOINDEV,
510 argv[1]);
511 sys_parsedevlist(&sys_nchout, sys_choutlist,MAXAUDIOOUTDEV,
512 argv[1]);
513
514 if (!sys_nchout)
515 goto usage;
516
517 argc -= 2; argv += 2;
518 }
519 else if (!strcmp(*argv, "-soundbuf") || !strcmp(*argv, "-audiobuf"))
520 {
521 sys_main_advance = atoi(argv[1]);
522 argc -= 2; argv += 2;
523 }
524 else if (!strcmp(*argv, "-blocksize"))
525 {
526 sys_setblocksize(atoi(argv[1]));
527 argc -= 2; argv += 2;
528 }
529 else if (!strcmp(*argv, "-sleepgrain"))
530 {
531 sys_sleepgrain = 1000 * atoi(argv[1]);
532 argc -= 2; argv += 2;
533 }
534 else if (!strcmp(*argv, "-nodac"))
535 { /* IOhannes */
536 sys_nsoundout=0;
537 sys_nchout = 0;
538 argc--; argv++;
539 }
540 else if (!strcmp(*argv, "-noadc"))
541 { /* IOhannes */
542 sys_nsoundin=0;
543 sys_nchin = 0;
544 argc--; argv++;
545 }
546 else if (!strcmp(*argv, "-nosound") || !strcmp(*argv, "-noaudio"))
547 { /* IOhannes */
548 sys_nsoundin=sys_nsoundout = 0;
549 sys_nchin = sys_nchout = 0;
550 argc--; argv++;
551 }
552#ifdef USEAPI_OSS
553 else if (!strcmp(*argv, "-oss"))
554 {
555 sys_set_audio_api(API_OSS);
556 argc--; argv++;
557 }
558 else if (!strcmp(*argv, "-32bit"))
559 {
560 sys_set_audio_api(API_OSS);
561 oss_set32bit();
562 argc--; argv++;
563 }
564#endif
565#ifdef USEAPI_ALSA
566 else if (!strcmp(*argv, "-alsa"))
567 {
568 sys_set_audio_api(API_ALSA);
569 argc--; argv++;
570 }
571 else if (!strcmp(*argv, "-alsaadd"))
572 {
573 if (argc > 1)
574 alsa_adddev(argv[1]);
575 else goto usage;
576 argc -= 2; argv +=2;
577 }
578 /* obsolete flag for setting ALSA device number or name */
579 else if (!strcmp(*argv, "-alsadev"))
580 {
581 int devno = 0;
582 if (argv[1][0] >= '1' && argv[1][0] <= '9')
583 devno = 1 + 2 * (atoi(argv[1]) - 1);
584 else if (!strncmp(argv[1], "hw:", 3))
585 devno = 1 + 2 * atoi(argv[1]+3);
586 else if (!strncmp(argv[1], "plughw:", 7))
587 devno = 2 + 2 * atoi(argv[1]+7);
588 else goto usage;
589 sys_nsoundin = sys_nsoundout = 1;
590 sys_soundindevlist[0] = sys_soundoutdevlist[0] = devno;
591 sys_set_audio_api(API_ALSA);
592 argc -= 2; argv +=2;
593 }
594#endif
595#ifdef USEAPI_JACK
596 else if (!strcmp(*argv, "-jack"))
597 {
598 sys_set_audio_api(API_JACK);
599 argc--; argv++;
600 }
601#endif
602#ifdef USEAPI_PORTAUDIO
603 else if (!strcmp(*argv, "-pa") || !strcmp(*argv, "-portaudio")
604#ifdef MSW
605 || !strcmp(*argv, "-asio")
606#endif
607 )
608 {
609 sys_set_audio_api(API_PORTAUDIO);
610 sys_mmio = 0;
611 argc--; argv++;
612 }
613#endif
614#ifdef USEAPI_MMIO
615 else if (!strcmp(*argv, "-mmio"))
616 {
617 sys_set_audio_api(API_MMIO);
618 sys_mmio = 1;
619 argc--; argv++;
620 }
621#endif
622 else if (!strcmp(*argv, "-nomidiin"))
623 {
624 sys_nmidiin = 0;
625 argc--; argv++;
626 }
627 else if (!strcmp(*argv, "-nomidiout"))
628 {
629 sys_nmidiout = 0;
630 argc--; argv++;
631 }
632 else if (!strcmp(*argv, "-nomidi"))
633 {
634 sys_nmidiin = sys_nmidiout = 0;
635 argc--; argv++;
636 }
637 else if (!strcmp(*argv, "-midiindev"))
638 {
639 sys_parsedevlist(&sys_nmidiin, sys_midiindevlist, MAXMIDIINDEV,
640 argv[1]);
641 if (!sys_nmidiin)
642 goto usage;
643 argc -= 2; argv += 2;
644 }
645 else if (!strcmp(*argv, "-midioutdev"))
646 {
647 sys_parsedevlist(&sys_nmidiout, sys_midioutdevlist, MAXMIDIOUTDEV,
648 argv[1]);
649 if (!sys_nmidiout)
650 goto usage;
651 argc -= 2; argv += 2;
652 }
653 else if (!strcmp(*argv, "-mididev"))
654 {
655 sys_parsedevlist(&sys_nmidiin, sys_midiindevlist, MAXMIDIINDEV,
656 argv[1]);
657 sys_parsedevlist(&sys_nmidiout, sys_midioutdevlist, MAXMIDIOUTDEV,
658 argv[1]);
659 if (!sys_nmidiout)
660 goto usage;
661 argc -= 2; argv += 2;
662 }
663 else if (!strcmp(*argv, "-path"))
664 {
665 sys_addpath(argv[1]);
666 argc -= 2; argv += 2;
667 }
668 else if (!strcmp(*argv, "-helppath"))
669 {
670 sys_addhelppath(argv[1]);
671 argc -= 2; argv += 2;
672 }
673 else if (!strcmp(*argv, "-open") && argc > 1)
674 {
675 sys_openlist = namelist_append(sys_openlist, argv[1]);
676 argc -= 2; argv += 2;
677 }
678 else if (!strcmp(*argv, "-lib") && argc > 1)
679 {
680 sys_externlist = namelist_append(sys_externlist, argv[1]);
681 argc -= 2; argv += 2;
682 }
683 else if (!strcmp(*argv, "-font") && argc > 1)
684 {
685 sys_defaultfont = sys_nearestfontsize(atoi(argv[1]));
686 argc -= 2;
687 argv += 2;
688 }
689 else if (!strcmp(*argv, "-verbose"))
690 {
691 sys_verbose = 1;
692 argc--; argv++;
693 }
694 else if (!strcmp(*argv, "-version"))
695 {
696 sys_version = 1;
697 argc--; argv++;
698 }
699 else if (!strcmp(*argv, "-d") && argc > 1 &&
700 sscanf(argv[1], "%d", &sys_debuglevel) >= 1)
701 {
702 argc -= 2;
703 argv += 2;
704 }
705 else if (!strcmp(*argv, "-noloadbang"))
706 {
707 sys_noloadbang = 1;
708 argc--; argv++;
709 }
710 else if (!strcmp(*argv, "-nogui"))
711 {
712 sys_nogui = 1;
713 argc--; argv++;
714 }
715 else if (!strcmp(*argv, "-stdin"))
716 {
717 sys_stdin = 1;
718 argc--; argv++;
719 }
720 else if (!strcmp(*argv, "-guicmd") && argc > 1)
721 {
722 sys_guicmd = argv[1];
723 argc -= 2; argv += 2;
724 }
725 else if (!strcmp(*argv, "-send") && argc > 1)
726 {
727 sys_messagelist = namelist_append(sys_messagelist, argv[1]);
728 argc -= 2; argv += 2;
729 }
730 else if (!strcmp(*argv, "-listdev"))
731 {
732 sys_listdevs();
733 argc--; argv++;
734 }
735#ifdef UNIX
736 else if (!strcmp(*argv, "-rt") || !strcmp(*argv, "-realtime"))
737 {
738 sys_hipriority = 1;
739 argc--; argv++;
740 }
741 else if (!strcmp(*argv, "-nrt"))
742 {
743 sys_hipriority = 0;
744 argc--; argv++;
745 }
746#endif
747 else if (!strcmp(*argv, "-soundindev") ||
748 !strcmp(*argv, "-audioindev"))
749 { /* IOhannes */
750 sys_parsedevlist(&sys_nsoundin, sys_soundindevlist,
751 MAXAUDIOINDEV, argv[1]);
752 if (!sys_nsoundin)
753 goto usage;
754 argc -= 2; argv += 2;
755 }
756 else if (!strcmp(*argv, "-soundoutdev") ||
757 !strcmp(*argv, "-audiooutdev"))
758 { /* IOhannes */
759 sys_parsedevlist(&sys_nsoundout, sys_soundoutdevlist,
760 MAXAUDIOOUTDEV, argv[1]);
761 if (!sys_nsoundout)
762 goto usage;
763 argc -= 2; argv += 2;
764 }
765 else if (!strcmp(*argv, "-sounddev") || !strcmp(*argv, "-audiodev"))
766 {
767 sys_parsedevlist(&sys_nsoundin, sys_soundindevlist,
768 MAXAUDIOINDEV, argv[1]);
769 sys_parsedevlist(&sys_nsoundout, sys_soundoutdevlist,
770 MAXAUDIOOUTDEV, argv[1]);
771 if (!sys_nsoundout)
772 goto usage;
773 argc -= 2; argv += 2;
774 }
775 else
776 {
777 unsigned int i;
778 usage:
779 for (i = 0; i < sizeof(usagemessage)/sizeof(*usagemessage); i++)
780 fprintf(stderr, "%s", usagemessage[i]);
781 return (1);
782 }
783 }
784 if (!sys_defaultfont)
785 sys_defaultfont = DEFAULTFONT;
786 for (; argc > 0; argc--, argv++)
787 sys_openlist = namelist_append(sys_openlist, *argv);
788
789
790 return (0);
791}
792
793int sys_getblksize(void)
794{
795 return (DEFDACBLKSIZE);
796}
797
798 /* stuff to do, once, after calling sys_argparse() -- which may itself
799 be called twice because of the .pdrc hack. */
800static void sys_afterargparse(void)
801{
802 char sbuf[MAXPDSTRING];
803 int i;
804 /* add "extra" library to path */
805 strncpy(sbuf, sys_libdir->s_name, MAXPDSTRING-30);
806 sbuf[MAXPDSTRING-30] = 0;
807 strcat(sbuf, "/extra");
808 sys_addpath(sbuf);
809 strncpy(sbuf, sys_libdir->s_name, MAXPDSTRING-30);
810 sbuf[MAXPDSTRING-30] = 0;
811 strcat(sbuf, "/intern");
812 sys_addpath(sbuf);
813 /* add "doc/5.reference" library to helppath */
814 strncpy(sbuf, sys_libdir->s_name, MAXPDSTRING-30);
815 sbuf[MAXPDSTRING-30] = 0;
816 strcat(sbuf, "/doc/5.reference");
817 sys_addhelppath(sbuf);
818 /* correct to make audio and MIDI device lists zero based. On
819 MMIO, however, "1" really means the second device (the first one
820 is "mapper" which is was not included when the command args were
821 set up, so we leave it that way for compatibility. */
822 if (!sys_mmio)
823 {
824 for (i = 0; i < sys_nsoundin; i++)
825 sys_soundindevlist[i]--;
826 for (i = 0; i < sys_nsoundout; i++)
827 sys_soundoutdevlist[i]--;
828 }
829 for (i = 0; i < sys_nmidiin; i++)
830 sys_midiindevlist[i]--;
831 for (i = 0; i < sys_nmidiout; i++)
832 sys_midioutdevlist[i]--;
833}
834
835static void sys_addreferencepath(void)
836{
837 char sbuf[MAXPDSTRING];
838}
839/* Copyright (c) 1997-1999 Miller Puckette and others.
840* For information on usage and redistribution, and for a DISCLAIMER OF ALL
841* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
842
843/* IOhannes :
844 * hacked the code to add advanced multidevice-support
845 * 1311:forum::für::umläute:2001
846 */
847
848char pd_version[] = "Pd version 0.37.4\n";
849char pd_compiletime[] = __TIME__;
850char pd_compiledate[] = __DATE__;
851
852#include "m_pd.h"
853#include "m_imp.h"
854#include "s_stuff.h"
855#include <sys/types.h>
856#include <sys/stat.h>
857#include <limits.h>
858#include <string.h>
859#include <stdio.h>
860#include <fcntl.h>
861#include <stdlib.h>
862
863#ifdef UNIX
864#include <unistd.h>
865#endif
866#ifdef MSW
867#include <io.h>
868#include <windows.h>
869#include <winbase.h>
870#endif
871
872void pd_init(void);
873int sys_argparse(int argc, char **argv);
874void sys_findprogdir(char *progname);
875int sys_startgui(const char *guipath);
876int sys_rcfile(void);
877int m_scheduler(void);
878void sys_addhelppath(char *p);
879void alsa_adddev(char *name);
880
881int sys_debuglevel;
882int sys_verbose;
883int sys_noloadbang;
884int sys_nogui;
885int sys_stdin = 0;
886char *sys_guicmd;
887t_symbol *sys_libdir;
888static t_symbol *sys_guidir;
889static t_namelist *sys_externlist;
890static t_namelist *sys_openlist;
891static t_namelist *sys_messagelist;
892static int sys_version;
893int sys_oldtclversion; /* hack to warn g_rtext.c about old text sel */
894
895int sys_nmidiout = 1;
896#ifdef MSW
897int sys_nmidiin = 0;
898#else
899int sys_nmidiin = 1;
900#endif
901int sys_midiindevlist[MAXMIDIINDEV] = {1};
902int sys_midioutdevlist[MAXMIDIOUTDEV] = {1};
903
904static int sys_main_srate = DEFAULTSRATE;
905static int sys_main_advance = DEFAULTADVANCE;
906
907/* IOhannes { */
908
909 /* here the "-1" counts signify that the corresponding vector hasn't been
910 specified in command line arguments; sys_open_audio will detect this
911 and fill things in. */
912int sys_nsoundin = -1;
913int sys_nsoundout = -1;
914int sys_soundindevlist[MAXAUDIOINDEV];
915int sys_soundoutdevlist[MAXAUDIOOUTDEV];
916
917int sys_nchin = -1;
918int sys_nchout = -1;
919int sys_chinlist[MAXAUDIOINDEV];
920int sys_choutlist[MAXAUDIOOUTDEV];
921/* } IOhannes */
922
923
924typedef struct _fontinfo
925{
926 int fi_fontsize;
927 int fi_maxwidth;
928 int fi_maxheight;
929 int fi_hostfontsize;
930 int fi_width;
931 int fi_height;
932} t_fontinfo;
933
934 /* these give the nominal point size and maximum height of the characters
935 in the six fonts. */
936
937static t_fontinfo sys_fontlist[] = {
938 {8, 5, 9, 0, 0, 0}, {10, 7, 13, 0, 0, 0}, {12, 9, 16, 0, 0, 0},
939 {16, 10, 20, 0, 0, 0}, {24, 15, 25, 0, 0, 0}, {36, 25, 45, 0, 0, 0}};
940#define NFONT (sizeof(sys_fontlist)/sizeof(*sys_fontlist))
941
942/* here are the actual font size structs on msp's systems:
943MSW:
944font 8 5 9 8 5 11
945font 10 7 13 10 6 13
946font 12 9 16 14 8 16
947font 16 10 20 16 10 18
948font 24 15 25 16 10 18
949font 36 25 42 36 22 41
950
951linux:
952font 8 5 9 8 5 9
953font 10 7 13 12 7 13
954font 12 9 16 14 9 15
955font 16 10 20 16 10 19
956font 24 15 25 24 15 24
957font 36 25 42 36 22 41
958*/
959
960static t_fontinfo *sys_findfont(int fontsize)
961{
962 unsigned int i;
963 t_fontinfo *fi;
964 for (i = 0, fi = sys_fontlist; i < (NFONT-1); i++, fi++)
965 if (fontsize < fi[1].fi_fontsize) return (fi);
966 return (sys_fontlist + (NFONT-1));
967}
968
969int sys_nearestfontsize(int fontsize)
970{
971 return (sys_findfont(fontsize)->fi_fontsize);
972}
973
974int sys_hostfontsize(int fontsize)
975{
976 return (sys_findfont(fontsize)->fi_hostfontsize);
977}
978
979int sys_fontwidth(int fontsize)
980{
981 return (sys_findfont(fontsize)->fi_width);
982}
983
984int sys_fontheight(int fontsize)
985{
986 return (sys_findfont(fontsize)->fi_height);
987}
988
989int sys_defaultfont;
990#ifdef MSW
991#define DEFAULTFONT 12
992#else
993#define DEFAULTFONT 10
994#endif
995
996static void openit(const char *dirname, const char *filename)
997{
998 char dirbuf[MAXPDSTRING], *nameptr;
999 int fd = open_via_path(dirname, filename, "", dirbuf, &nameptr,
1000 MAXPDSTRING, 0);
1001 if (fd)
1002 {
1003 close (fd);
1004 glob_evalfile(0, gensym(nameptr), gensym(dirbuf));
1005 }
1006 else
1007 error("%s: can't open", filename);
1008}
1009
1010#define NHOSTFONT 7
1011
1012/* this is called from the gui process. The first argument is the cwd, and
1013succeeding args give the widths and heights of known fonts. We wait until
1014these are known to open files and send messages specified on the command line.
1015We ask the GUI to specify the "cwd" in case we don't have a local OS to get it
1016from; for instance we could be some kind of RT embedded system. However, to
1017really make this make sense we would have to implement
1018open(), read(), etc, calls to be served somehow from the GUI too. */
1019
1020void glob_initfromgui(void *dummy, t_symbol *s, int argc, t_atom *argv)
1021{
1022 char *cwd = atom_getsymbolarg(0, argc, argv)->s_name;
1023 t_namelist *nl;
1024 unsigned int i, j;
1025 if (argc != 2 + 3 * NHOSTFONT) bug("glob_initfromgui");
1026 for (i = 0; i < NFONT; i++)
1027 {
1028 int wantheight = sys_fontlist[i].fi_maxheight;
1029 for (j = 0; j < NHOSTFONT-1; j++)
1030 {
1031 if (atom_getintarg(3 * (j + 1) + 3, argc, argv) > wantheight)
1032 break;
1033 }
1034 /* j is now the "real" font index for the desired font index i. */
1035 sys_fontlist[i].fi_hostfontsize = atom_getintarg(3 * j + 1, argc, argv);
1036 sys_fontlist[i].fi_width = atom_getintarg(3 * j + 2, argc, argv);
1037 sys_fontlist[i].fi_height = atom_getintarg(3 * j + 3, argc, argv);
1038 }
1039#if 0
1040 for (i = 0; i < 6; i++)
1041 fprintf(stderr, "font %d %d %d %d %d\n",
1042 sys_fontlist[i].fi_fontsize,
1043 sys_fontlist[i].fi_maxheight,
1044 sys_fontlist[i].fi_hostfontsize,
1045 sys_fontlist[i].fi_width,
1046 sys_fontlist[i].fi_height);
1047#endif
1048 /* load dynamic libraries specified with "-lib" args */
1049 for (nl = sys_externlist; nl; nl = nl->nl_next)
1050 if (!sys_load_lib(cwd, nl->nl_string))
1051 post("%s: can't load library", nl->nl_string);
1052 namelist_free(sys_externlist);
1053 sys_externlist = 0;
1054 /* open patches specifies with "-open" args */
1055 for (nl = sys_openlist; nl; nl = nl->nl_next)
1056 openit(cwd, nl->nl_string);
1057 namelist_free(sys_openlist);
1058 sys_openlist = 0;
1059 /* send messages specified with "-send" args */
1060 for (nl = sys_messagelist; nl; nl = nl->nl_next)
1061 {
1062 t_binbuf *b = binbuf_new();
1063 binbuf_text(b, nl->nl_string, strlen(nl->nl_string));
1064 binbuf_eval(b, 0, 0, 0);
1065 binbuf_free(b);
1066 }
1067 namelist_free(sys_messagelist);
1068 sys_messagelist = 0;
1069 sys_oldtclversion = atom_getfloatarg(1 + 3 * NHOSTFONT, argc, argv);
1070}
1071
1072static void sys_afterargparse(void);
1073
1074/* this is called from main() in s_entry.c */
1075int sys_main(int argc, char **argv)
1076{
1077#ifdef PD_DEBUG
1078 fprintf(stderr, "Pd: COMPILED FOR DEBUGGING\n");
1079#endif
1080 pd_init();
1081 sys_findprogdir(argv[0]); /* set sys_progname, guipath */
1082#ifdef UNIX
1083 sys_rcfile(); /* parse the startup file */
1084#endif
1085 if (sys_argparse(argc, argv)) /* parse cmd line */
1086 return (1);
1087 sys_afterargparse(); /* post-argparse settings */
1088 if (sys_verbose || sys_version) fprintf(stderr, "%scompiled %s %s\n",
1089 pd_version, pd_compiletime, pd_compiledate);
1090 if (sys_version) /* if we were just asked our version, exit here. */
1091 return (0);
1092 if (sys_startgui(sys_guidir->s_name)) /* start the gui */
1093 return(1);
1094 /* open audio and MIDI */
1095 sys_open_midi(sys_nmidiin, sys_midiindevlist,
1096 sys_nmidiout, sys_midioutdevlist);
1097 sys_open_audio(sys_nsoundin, sys_soundindevlist, sys_nchin, sys_chinlist,
1098 sys_nsoundout, sys_soundoutdevlist, sys_nchout, sys_choutlist,
1099 sys_main_srate, sys_main_advance, 1);
1100 /* run scheduler until it quits */
1101
1102 return (m_scheduler_pda());
1103 return (m_scheduler());
1104}
1105
1106static char *(usagemessage[]) = {
1107"usage: pd [-flags] [file]...\n",
1108"\naudio configuration flags:\n",
1109"-r <n> -- specify sample rate\n",
1110"-audioindev ... -- audio in devices; e.g., \"1,3\" for first and third\n",
1111"-audiooutdev ... -- audio out devices (same)\n",
1112"-audiodev ... -- specify input and output together\n",
1113"-inchannels ... -- audio input channels (by device, like \"2\" or \"16,8\")\n",
1114"-outchannels ... -- number of audio out channels (same)\n",
1115"-channels ... -- specify both input and output channels\n",
1116"-audiobuf <n> -- specify size of audio buffer in msec\n",
1117"-blocksize <n> -- specify audio I/O block size in sample frames\n",
1118"-sleepgrain <n> -- specify number of milliseconds to sleep when idle\n",
1119"-nodac -- suppress audio output\n",
1120"-noadc -- suppress audio input\n",
1121"-noaudio -- suppress audio input and output (-nosound is synonym) \n",
1122"-listdev -- list audio and MIDI devices\n",
1123
1124#ifdef USEAPI_OSS
1125"-oss -- use OSS audio API\n",
1126"-32bit ----- allow 32 bit OSS audio (for RME Hammerfall)\n",
1127#endif
1128
1129#ifdef USEAPI_ALSA
1130"-alsa -- use ALSA audio API\n",
1131"-alsaadd <name> -- add an ALSA device name to list\n",
1132"-alsadev <n> ----- obsolete: use -audiodev\n",
1133#endif
1134
1135#ifdef USEAPI_JACK
1136"-jack -- use JACK audio API\n",
1137#endif
1138
1139#ifdef USEAPI_PORTAUDIO
1140#ifdef MSW
1141"-asio -- use ASIO audio driver (via Portaudio)\n",
1142"-pa -- synonym for -asio\n",
1143#else
1144"-pa -- use Portaudio API\n",
1145#endif
1146#endif
1147
1148#ifdef USEAPI_MMIO
1149"-mmio -- use MMIO audio API (default for Windows)\n",
1150#endif
1151" (default audio API for this platform: ", API_DEFSTRING, ")\n\n",
1152
1153"\nMIDI configuration flags:\n",
1154"-midiindev ... -- midi in device list; e.g., \"1,3\" for first and third\n",
1155"-midioutdev ... -- midi out device list, same format\n",
1156"-mididev ... -- specify -midioutdev and -midiindev together\n",
1157"-nomidiin -- suppress MIDI input\n",
1158"-nomidiout -- suppress MIDI output\n",
1159"-nomidi -- suppress MIDI input and output\n",
1160
1161"\nother flags:\n",
1162"-path <path> -- add to file search path\n",
1163"-helppath <path> -- add to help file search path\n",
1164"-open <file> -- open file(s) on startup\n",
1165"-lib <file> -- load object library(s)\n",
1166"-font <n> -- specify default font size in points\n",
1167"-verbose -- extra printout on startup and when searching for files\n",
1168"-version -- don't run Pd; just print out which version it is \n",
1169"-d <n> -- specify debug level\n",
1170"-noloadbang -- suppress all loadbangs\n",
1171"-nogui -- suppress starting the GUI\n",
1172"-stdin -- scan stdin for keypresses\n",
1173"-guicmd \"cmd...\" -- substitute another GUI program (e.g., rsh)\n",
1174"-send \"msg...\" -- send a message at startup (after patches are loaded)\n",
1175#ifdef UNIX
1176"-rt or -realtime -- use real-time priority\n",
1177"-nrt -- don't use real-time priority\n",
1178#endif
1179};
1180
1181static void sys_parsedevlist(int *np, int *vecp, int max, char *str)
1182{
1183 int n = 0;
1184 while (n < max)
1185 {
1186 if (!*str) break;
1187 else
1188 {
1189 char *endp;
1190 vecp[n] = strtol(str, &endp, 10);
1191 if (endp == str)
1192 break;
1193 n++;
1194 if (!endp)
1195 break;
1196 str = endp + 1;
1197 }
1198 }
1199 *np = n;
1200}
1201
1202static int sys_getmultidevchannels(int n, int *devlist)
1203{
1204 int sum = 0;
1205 if (n<0)return(-1);
1206 if (n==0)return 0;
1207 while(n--)sum+=*devlist++;
1208 return sum;
1209}
1210
1211
1212 /* this routine tries to figure out where to find the auxilliary files
1213 Pd will need to run. This is either done by looking at the command line
1214 invokation for Pd, or if that fails, by consulting the variable
1215 INSTALL_PREFIX. In MSW, we don't try to use INSTALL_PREFIX. */
1216void sys_findprogdir(char *progname)
1217{
1218 char sbuf[MAXPDSTRING], sbuf2[MAXPDSTRING], *sp;
1219 char *lastslash;
1220#ifdef UNIX
1221 struct stat statbuf;
1222#endif
1223
1224 /* find out by what string Pd was invoked; put answer in "sbuf". */
1225#ifdef MSW
1226 GetModuleFileName(NULL, sbuf2, sizeof(sbuf2));
1227 sbuf2[MAXPDSTRING-1] = 0;
1228 sys_unbashfilename(sbuf2, sbuf);
1229#endif /* MSW */
1230#ifdef UNIX
1231 strncpy(sbuf, progname, MAXPDSTRING);
1232 sbuf[MAXPDSTRING-1] = 0;
1233#endif
1234 lastslash = strrchr(sbuf, '/');
1235 if (lastslash)
1236 {
1237 /* bash last slash to zero so that sbuf is directory pd was in,
1238 e.g., ~/pd/bin */
1239 *lastslash = 0;
1240 /* go back to the parent from there, e.g., ~/pd */
1241 lastslash = strrchr(sbuf, '/');
1242 if (lastslash)
1243 {
1244 strncpy(sbuf2, sbuf, lastslash-sbuf);
1245 sbuf2[lastslash-sbuf] = 0;
1246 }
1247 else strcpy(sbuf2, "..");
1248 }
1249 else
1250 {
1251 /* no slashes found. Try INSTALL_PREFIX. */
1252#ifdef INSTALL_PREFIX
1253 strcpy(sbuf2, INSTALL_PREFIX);
1254#else
1255 strcpy(sbuf2, ".");
1256#endif
1257 }
1258 /* now we believe sbuf2 holds the parent directory of the directory
1259 pd was found in. We now want to infer the "lib" directory and the
1260 "gui" directory. In "simple" UNIX installations, the layout is
1261 .../bin/pd
1262 .../bin/pd-gui
1263 .../doc
1264 and in "complicated" UNIX installations, it's:
1265 .../bin/pd
1266 .../lib/pd/bin/pd-gui
1267 .../lib/pd/doc
1268 To decide which, we stat .../lib/pd; if that exists, we assume it's
1269 the complicated layout. In MSW, it's the "simple" layout, but
1270 the gui program is straight wish80:
1271 .../bin/pd
1272 .../bin/wish80.exe
1273 .../doc
1274 */
1275#ifdef UNIX
1276 strncpy(sbuf, sbuf2, MAXPDSTRING-30);
1277 sbuf[MAXPDSTRING-30] = 0;
1278 strcat(sbuf, "/lib/pd");
1279 if (stat(sbuf, &statbuf) >= 0)
1280 {
1281 /* complicated layout: lib dir is the one we just stat-ed above */
1282 sys_libdir = gensym(sbuf);
1283 /* gui lives in .../lib/pd/bin */
1284 strncpy(sbuf, sbuf2, MAXPDSTRING-30);
1285 sbuf[MAXPDSTRING-30] = 0;
1286 strcat(sbuf, "/lib/pd/bin");
1287 sys_guidir = gensym(sbuf);
1288 }
1289 else
1290 {
1291 /* simple layout: lib dir is the parent */
1292 sys_libdir = gensym(sbuf2);
1293 /* gui lives in .../bin */
1294 strncpy(sbuf, sbuf2, MAXPDSTRING-30);
1295 sbuf[MAXPDSTRING-30] = 0;
1296 strcat(sbuf, "/bin");
1297 sys_guidir = gensym(sbuf);
1298 }
1299#endif
1300#ifdef MSW
1301 sys_libdir = gensym(sbuf2);
1302 sys_guidir = &s_; /* in MSW the guipath just depends on the libdir */
1303#endif
1304}
1305
1306#ifdef MSW
1307static int sys_mmio = 1;
1308#else
1309static int sys_mmio = 0;
1310#endif
1311
1312int sys_argparse(int argc, char **argv)
1313{
1314 char sbuf[MAXPDSTRING];
1315 int i;
1316 argc--; argv++;
1317 while ((argc > 0) && **argv == '-')
1318 {
1319 if (!strcmp(*argv, "-r") && argc > 1 &&
1320 sscanf(argv[1], "%d", &sys_main_srate) >= 1)
1321 {
1322 argc -= 2;
1323 argv += 2;
1324 }
1325 else if (!strcmp(*argv, "-inchannels"))
1326 { /* IOhannes */
1327 sys_parsedevlist(&sys_nchin,
1328 sys_chinlist, MAXAUDIOINDEV, argv[1]);
1329
1330 if (!sys_nchin)
1331 goto usage;
1332
1333 argc -= 2; argv += 2;
1334 }
1335 else if (!strcmp(*argv, "-outchannels"))
1336 { /* IOhannes */
1337 sys_parsedevlist(&sys_nchout, sys_choutlist,
1338 MAXAUDIOOUTDEV, argv[1]);
1339
1340 if (!sys_nchout)
1341 goto usage;
1342
1343 argc -= 2; argv += 2;
1344 }
1345 else if (!strcmp(*argv, "-channels"))
1346 {
1347 sys_parsedevlist(&sys_nchin, sys_chinlist,MAXAUDIOINDEV,
1348 argv[1]);
1349 sys_parsedevlist(&sys_nchout, sys_choutlist,MAXAUDIOOUTDEV,
1350 argv[1]);
1351
1352 if (!sys_nchout)
1353 goto usage;
1354
1355 argc -= 2; argv += 2;
1356 }
1357 else if (!strcmp(*argv, "-soundbuf") || !strcmp(*argv, "-audiobuf"))
1358 {
1359 sys_main_advance = atoi(argv[1]);
1360 argc -= 2; argv += 2;
1361 }
1362 else if (!strcmp(*argv, "-blocksize"))
1363 {
1364 sys_setblocksize(atoi(argv[1]));
1365 argc -= 2; argv += 2;
1366 }
1367 else if (!strcmp(*argv, "-sleepgrain"))
1368 {
1369 sys_sleepgrain = 1000 * atoi(argv[1]);
1370 argc -= 2; argv += 2;
1371 }
1372 else if (!strcmp(*argv, "-nodac"))
1373 { /* IOhannes */
1374 sys_nsoundout=0;
1375 sys_nchout = 0;
1376 argc--; argv++;
1377 }
1378 else if (!strcmp(*argv, "-noadc"))
1379 { /* IOhannes */
1380 sys_nsoundin=0;
1381 sys_nchin = 0;
1382 argc--; argv++;
1383 }
1384 else if (!strcmp(*argv, "-nosound") || !strcmp(*argv, "-noaudio"))
1385 { /* IOhannes */
1386 sys_nsoundin=sys_nsoundout = 0;
1387 sys_nchin = sys_nchout = 0;
1388 argc--; argv++;
1389 }
1390#ifdef USEAPI_OSS
1391 else if (!strcmp(*argv, "-oss"))
1392 {
1393 sys_set_audio_api(API_OSS);
1394 argc--; argv++;
1395 }
1396 else if (!strcmp(*argv, "-32bit"))
1397 {
1398 sys_set_audio_api(API_OSS);
1399 oss_set32bit();
1400 argc--; argv++;
1401 }
1402#endif
1403#ifdef USEAPI_ALSA
1404 else if (!strcmp(*argv, "-alsa"))
1405 {
1406 sys_set_audio_api(API_ALSA);
1407 argc--; argv++;
1408 }
1409 else if (!strcmp(*argv, "-alsaadd"))
1410 {
1411 if (argc > 1)
1412 alsa_adddev(argv[1]);
1413 else goto usage;
1414 argc -= 2; argv +=2;
1415 }
1416 /* obsolete flag for setting ALSA device number or name */
1417 else if (!strcmp(*argv, "-alsadev"))
1418 {
1419 int devno = 0;
1420 if (argv[1][0] >= '1' && argv[1][0] <= '9')
1421 devno = 1 + 2 * (atoi(argv[1]) - 1);
1422 else if (!strncmp(argv[1], "hw:", 3))
1423 devno = 1 + 2 * atoi(argv[1]+3);
1424 else if (!strncmp(argv[1], "plughw:", 7))
1425 devno = 2 + 2 * atoi(argv[1]+7);
1426 else goto usage;
1427 sys_nsoundin = sys_nsoundout = 1;
1428 sys_soundindevlist[0] = sys_soundoutdevlist[0] = devno;
1429 sys_set_audio_api(API_ALSA);
1430 argc -= 2; argv +=2;
1431 }
1432#endif
1433#ifdef USEAPI_JACK
1434 else if (!strcmp(*argv, "-jack"))
1435 {
1436 sys_set_audio_api(API_JACK);
1437 argc--; argv++;
1438 }
1439#endif
1440#ifdef USEAPI_PORTAUDIO
1441 else if (!strcmp(*argv, "-pa") || !strcmp(*argv, "-portaudio")
1442#ifdef MSW
1443 || !strcmp(*argv, "-asio")
1444#endif
1445 )
1446 {
1447 sys_set_audio_api(API_PORTAUDIO);
1448 sys_mmio = 0;
1449 argc--; argv++;
1450 }
1451#endif
1452#ifdef USEAPI_MMIO
1453 else if (!strcmp(*argv, "-mmio"))
1454 {
1455 sys_set_audio_api(API_MMIO);
1456 sys_mmio = 1;
1457 argc--; argv++;
1458 }
1459#endif
1460 else if (!strcmp(*argv, "-nomidiin"))
1461 {
1462 sys_nmidiin = 0;
1463 argc--; argv++;
1464 }
1465 else if (!strcmp(*argv, "-nomidiout"))
1466 {
1467 sys_nmidiout = 0;
1468 argc--; argv++;
1469 }
1470 else if (!strcmp(*argv, "-nomidi"))
1471 {
1472 sys_nmidiin = sys_nmidiout = 0;
1473 argc--; argv++;
1474 }
1475 else if (!strcmp(*argv, "-midiindev"))
1476 {
1477 sys_parsedevlist(&sys_nmidiin, sys_midiindevlist, MAXMIDIINDEV,
1478 argv[1]);
1479 if (!sys_nmidiin)
1480 goto usage;
1481 argc -= 2; argv += 2;
1482 }
1483 else if (!strcmp(*argv, "-midioutdev"))
1484 {
1485 sys_parsedevlist(&sys_nmidiout, sys_midioutdevlist, MAXMIDIOUTDEV,
1486 argv[1]);
1487 if (!sys_nmidiout)
1488 goto usage;
1489 argc -= 2; argv += 2;
1490 }
1491 else if (!strcmp(*argv, "-mididev"))
1492 {
1493 sys_parsedevlist(&sys_nmidiin, sys_midiindevlist, MAXMIDIINDEV,
1494 argv[1]);
1495 sys_parsedevlist(&sys_nmidiout, sys_midioutdevlist, MAXMIDIOUTDEV,
1496 argv[1]);
1497 if (!sys_nmidiout)
1498 goto usage;
1499 argc -= 2; argv += 2;
1500 }
1501 else if (!strcmp(*argv, "-path"))
1502 {
1503 sys_addpath(argv[1]);
1504 argc -= 2; argv += 2;
1505 }
1506 else if (!strcmp(*argv, "-helppath"))
1507 {
1508 sys_addhelppath(argv[1]);
1509 argc -= 2; argv += 2;
1510 }
1511 else if (!strcmp(*argv, "-open") && argc > 1)
1512 {
1513 sys_openlist = namelist_append(sys_openlist, argv[1]);
1514 argc -= 2; argv += 2;
1515 }
1516 else if (!strcmp(*argv, "-lib") && argc > 1)
1517 {
1518 sys_externlist = namelist_append(sys_externlist, argv[1]);
1519 argc -= 2; argv += 2;
1520 }
1521 else if (!strcmp(*argv, "-font") && argc > 1)
1522 {
1523 sys_defaultfont = sys_nearestfontsize(atoi(argv[1]));
1524 argc -= 2;
1525 argv += 2;
1526 }
1527 else if (!strcmp(*argv, "-verbose"))
1528 {
1529 sys_verbose = 1;
1530 argc--; argv++;
1531 }
1532 else if (!strcmp(*argv, "-version"))
1533 {
1534 sys_version = 1;
1535 argc--; argv++;
1536 }
1537 else if (!strcmp(*argv, "-d") && argc > 1 &&
1538 sscanf(argv[1], "%d", &sys_debuglevel) >= 1)
1539 {
1540 argc -= 2;
1541 argv += 2;
1542 }
1543 else if (!strcmp(*argv, "-noloadbang"))
1544 {
1545 sys_noloadbang = 1;
1546 argc--; argv++;
1547 }
1548 else if (!strcmp(*argv, "-nogui"))
1549 {
1550 sys_nogui = 1;
1551 argc--; argv++;
1552 }
1553 else if (!strcmp(*argv, "-stdin"))
1554 {
1555 sys_stdin = 1;
1556 argc--; argv++;
1557 }
1558 else if (!strcmp(*argv, "-guicmd") && argc > 1)
1559 {
1560 sys_guicmd = argv[1];
1561 argc -= 2; argv += 2;
1562 }
1563 else if (!strcmp(*argv, "-send") && argc > 1)
1564 {
1565 sys_messagelist = namelist_append(sys_messagelist, argv[1]);
1566 argc -= 2; argv += 2;
1567 }
1568 else if (!strcmp(*argv, "-listdev"))
1569 {
1570 sys_listdevs();
1571 argc--; argv++;
1572 }
1573#ifdef UNIX
1574 else if (!strcmp(*argv, "-rt") || !strcmp(*argv, "-realtime"))
1575 {
1576 sys_hipriority = 1;
1577 argc--; argv++;
1578 }
1579 else if (!strcmp(*argv, "-nrt"))
1580 {
1581 sys_hipriority = 0;
1582 argc--; argv++;
1583 }
1584#endif
1585 else if (!strcmp(*argv, "-soundindev") ||
1586 !strcmp(*argv, "-audioindev"))
1587 { /* IOhannes */
1588 sys_parsedevlist(&sys_nsoundin, sys_soundindevlist,
1589 MAXAUDIOINDEV, argv[1]);
1590 if (!sys_nsoundin)
1591 goto usage;
1592 argc -= 2; argv += 2;
1593 }
1594 else if (!strcmp(*argv, "-soundoutdev") ||
1595 !strcmp(*argv, "-audiooutdev"))
1596 { /* IOhannes */
1597 sys_parsedevlist(&sys_nsoundout, sys_soundoutdevlist,
1598 MAXAUDIOOUTDEV, argv[1]);
1599 if (!sys_nsoundout)
1600 goto usage;
1601 argc -= 2; argv += 2;
1602 }
1603 else if (!strcmp(*argv, "-sounddev") || !strcmp(*argv, "-audiodev"))
1604 {
1605 sys_parsedevlist(&sys_nsoundin, sys_soundindevlist,
1606 MAXAUDIOINDEV, argv[1]);
1607 sys_parsedevlist(&sys_nsoundout, sys_soundoutdevlist,
1608 MAXAUDIOOUTDEV, argv[1]);
1609 if (!sys_nsoundout)
1610 goto usage;
1611 argc -= 2; argv += 2;
1612 }
1613 else
1614 {
1615 unsigned int i;
1616 usage:
1617 for (i = 0; i < sizeof(usagemessage)/sizeof(*usagemessage); i++)
1618 fprintf(stderr, "%s", usagemessage[i]);
1619 return (1);
1620 }
1621 }
1622 if (!sys_defaultfont)
1623 sys_defaultfont = DEFAULTFONT;
1624 for (; argc > 0; argc--, argv++)
1625 sys_openlist = namelist_append(sys_openlist, *argv);
1626
1627
1628 return (0);
1629}
1630
1631int sys_getblksize(void)
1632{
1633 return (DEFDACBLKSIZE);
1634}
1635
1636 /* stuff to do, once, after calling sys_argparse() -- which may itself
1637 be called twice because of the .pdrc hack. */
1638static void sys_afterargparse(void)
1639{
1640 char sbuf[MAXPDSTRING];
1641 int i;
1642 /* add "extra" library to path */
1643 strncpy(sbuf, sys_libdir->s_name, MAXPDSTRING-30);
1644 sbuf[MAXPDSTRING-30] = 0;
1645 strcat(sbuf, "/extra");
1646 sys_addpath(sbuf);
1647 strncpy(sbuf, sys_libdir->s_name, MAXPDSTRING-30);
1648 sbuf[MAXPDSTRING-30] = 0;
1649 strcat(sbuf, "/intern");
1650 sys_addpath(sbuf);
1651 /* add "doc/5.reference" library to helppath */
1652 strncpy(sbuf, sys_libdir->s_name, MAXPDSTRING-30);
1653 sbuf[MAXPDSTRING-30] = 0;
1654 strcat(sbuf, "/doc/5.reference");
1655 sys_addhelppath(sbuf);
1656 /* correct to make audio and MIDI device lists zero based. On
1657 MMIO, however, "1" really means the second device (the first one
1658 is "mapper" which is was not included when the command args were
1659 set up, so we leave it that way for compatibility. */
1660 if (!sys_mmio)
1661 {
1662 for (i = 0; i < sys_nsoundin; i++)
1663 sys_soundindevlist[i]--;
1664 for (i = 0; i < sys_nsoundout; i++)
1665 sys_soundoutdevlist[i]--;
1666 }
1667 for (i = 0; i < sys_nmidiin; i++)
1668 sys_midiindevlist[i]--;
1669 for (i = 0; i < sys_nmidiout; i++)
1670 sys_midioutdevlist[i]--;
1671}
1672
1673static void sys_addreferencepath(void)
1674{
1675 char sbuf[MAXPDSTRING];
1676}