1 
2 
3 /*
4  *  Author: Arvin Schnell
5  */
6 
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include <iostream>
13 
14 using std::cerr;
15 
16 #include <X11/Xlib.h>
17 #include <X11/xpm.h>
18 #include <Xm/Xm.h>
19 #include <Xm/MainW.h>
20 #include <Xm/AtomMgr.h>
21 #include <Xm/Protocols.h>
22 
23 #include "Sample.h"
24 #include "main.h"
25 #include "control.h"
26 #include "utils.h"
27 #include "config.h"
28 
29 #include "xanalyser.xpm"
30 #include "xscope.xpm"
31 
32 
33 const char* program_name;
34 
35 Widget shell, toplevel;
36 XtAppContext app_context;
37 
38 Pixmap a_iconpixmap, a_iconmask;
39 Pixmap s_iconpixmap, s_iconmask;
40 
41 Atom wm_delete_window;
42 
43 
44 // Xresources
45 
46 XAnalyser xanalyser;
47 
48 XtResource resources[] = {
49 
50     { "samplerate", "SampleRate", XtRInt, sizeof (int),
51       XtOffsetOf (XAnalyser, sample_rate), XtRImmediate, (XtPointer) 44100 },
52     { "samplelength", "SampleLength", XtRInt, sizeof (int),
53       XtOffsetOf (XAnalyser, sample_length), XtRImmediate, (XtPointer) 2048 },
54     { "sampleresolution", "SampleResolution", XtRInt, sizeof (int),
55       XtOffsetOf (XAnalyser, sample_resolution), XtRImmediate, (XtPointer) 16 },
56     { "sampledevice", "SampleDevice", XtRString, sizeof (String),
57       XtOffsetOf (XAnalyser, sample_device), XtRString, (XtPointer) "default" },
58     { "dcadjust", "DCAdjust", XtRBoolean, sizeof (Boolean),
59       XtOffsetOf (XAnalyser, dcadjust), XtRImmediate, (XtPointer) True },
60     { "source", "Source", XtRString, sizeof (String),
61       XtOffsetOf (XAnalyser, source), XtRString, (XtPointer) "Soundcard" },
62     { "filename", "Filename", XtRString, sizeof (String),
63       XtOffsetOf (XAnalyser, filename), XtRString, (XtPointer) "test.cdr" },
64     { "upperdb", "UpperDB", XtRFloat, sizeof (float),
65       XtOffsetOf (XAnalyser, upper_db), XtRString, (XtPointer) "+10.0" },
66     { "lowerdb", "LowerDB", XtRFloat, sizeof (float),
67       XtOffsetOf (XAnalyser, lower_db), XtRString, (XtPointer) "-100.0" },
68     { "alpha", "Alpha", XtRFloat, sizeof (float),
69       XtOffsetOf (XAnalyser, alpha), XtRString, (XtPointer) "20.0" },
70     { "decay", "Decay", XtRFloat, sizeof (float),
71       XtOffsetOf (XAnalyser, decay), XtRString, (XtPointer) "48.0" },
72     { "stereo", "Stereo", XtRBoolean, sizeof (Boolean),
73       XtOffsetOf (XAnalyser, stereo), XtRImmediate, (XtPointer) True },
74 
75     { "zoom", "Zoom", XtRFloat, sizeof (float),
76       XtOffsetOf (XAnalyser, zoom), XtRString, (XtPointer) "6.0" },
77 
78     { "datacolor", "DataColor", XtRPixel, sizeof (Pixel),
79       XtOffsetOf (XAnalyser, datacolor), XtRString, (XtPointer) "firebrick1" },
80     { "backgroundcolor", "BackgroundColor", XtRPixel, sizeof (Pixel),
81       XtOffsetOf (XAnalyser, backgroundcolor), XtRString, (XtPointer) "gray20" },
82     { "markercolor", "MarkerColor", XtRPixel, sizeof (Pixel),
83       XtOffsetOf (XAnalyser, markercolor), XtRString, (XtPointer) "yellow" },
84     { "majorgridcolor", "MajorGridColor", XtRPixel, sizeof (Pixel),
85       XtOffsetOf (XAnalyser, majorgridcolor), XtRString, (XtPointer) "gray60" },
86     { "minorgridcolor", "MinorGridColor", XtRPixel, sizeof (Pixel),
87       XtOffsetOf (XAnalyser, minorgridcolor), XtRString, (XtPointer) "gray40" },
88     { "gridfont", "GridFont", XtRFontStruct, sizeof (XFontStruct*),
89       XtOffsetOf (XAnalyser, gridfont), XtRString, (XtPointer) "fixed" }
90 
91 };
92 
93 
94 XrmOptionDescRec cmdline_options[] = {
95     { "-rate", ".samplerate", XrmoptionSepArg, "" },
96     { "-length", ".samplelength", XrmoptionSepArg, "" },
97     { "-resolution", ".sampleresolution", XrmoptionSepArg, "" },
98     { "-device", ".sampledevice", XrmoptionSepArg, "" },
99     { "-mono", ".stereo", XrmoptionNoArg, "False" },
100     { "-stereo", ".stereo", XrmoptionNoArg, "True" },
101     { "-soundcard", ".source", XrmoptionNoArg, "Soundcard" },
102     { "-diskfile", ".source", XrmoptionNoArg, "Diskfile" },
103     { "-filename", ".filename", XrmoptionSepArg, "" },
104     { "-dcadjust", ".dcadjust", XrmoptionSepArg, "" }
105 };
106 
107 
108 String fallbacks[] = {
109 #include "XAnalyser.ad.h"
110     0
111 };
112 
113 
114 void usage () __attribute__ ((__noreturn__));
115 void
usage()116 usage ()
117 {
118     cerr << "usage: " << program_name << " [-rate rate] [-length length] "
119 	"[-resolution resolution] [-device device] [-dcadjust bool] "
120 	"[-soundcard] [-diskfile] [-file file]\n";
121 
122     exit (EXIT_FAILURE);
123 }
124 
125 
126 int
main(int argc,char * argv[])127 main (int argc, char* argv[])
128 {
129     program_name = argv[0];
130 
131     shell = XtAppInitialize (&app_context, "XAnalyser",
132 			     cmdline_options, XtNumber (cmdline_options),
133 			     &argc, argv, fallbacks, 0, 0);
134 
135     if (argc != 1)
136 	usage ();
137 
138     XtGetApplicationResources (shell, (XtPointer) &xanalyser,
139 			       resources, XtNumber (resources), 0, 0);
140 
141     toplevel = XmCreateMainWindow (shell, "MainWindow", 0, 0);
142     XtManageChild (toplevel);
143 
144     control_create ();
145 
146     XtRealizeWidget (shell);
147 
148     wm_delete_window = XmInternAtom (XtDisplay (toplevel),
149 				     "WM_DELETE_WINDOW", False);
150 
151     XmAddWMProtocolCallback (shell, wm_delete_window, control_close, 0);
152 
153     // icon
154 
155     Display* display = XtDisplay (shell);
156 
157     XpmCreatePixmapFromData (display, XtWindow (shell),
158 			     xanalyser_xpm, &a_iconpixmap, &a_iconmask, 0);
159 
160     XpmCreatePixmapFromData (display, XtWindow (shell),
161 			     xscope_xpm, &s_iconpixmap, &s_iconmask, 0);
162 
163     // FIXME: under high cpu/X11 load the icon does not appear
164     // usleep( 500 );  // works (sometimes), but I don't like it
165 
166     XtVaSetValues (shell, XtNiconPixmap, a_iconpixmap, XtNiconMask,
167 		   a_iconmask, (char*) NULL);
168 
169     if (strcasecmp (xanalyser.source, "Soundcard") == 0)
170 	sample.source = Sample::SOUNDCARD;
171     else
172 	sample.source = Sample::DISKFILE;
173 
174     sample.filename = xanalyser.filename;
175     sample.rate = xanalyser.sample_rate;
176     sample.length = xanalyser.sample_length;
177     sample.resolution = xanalyser.sample_resolution;
178     sample.device = xanalyser.sample_device;
179 
180     control_set_default ();
181 
182     analyser_manage ();
183     scope_manage ();
184 
185     set_sensitivity ();
186 
187     XtAppMainLoop (app_context);
188 }
189