File: application.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (333 lines) | stat: -rw-r--r-- 8,664 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// application.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <InterViews/banner.h>
#include <InterViews/event.h>
#include <InterViews/frame.h>
#include <InterViews/world.h>
#include "application.h"
#include "controller.h"
#include "converter.h"
#include "datafile.h"
#include "sound.h"
#include "vmessage.h"
#include <String.h>
#include <iostream.h>
#include "localdefs.h"

#ifdef NeXT 
extern "C" {
	char *strerror(int);
	int sleep(unsigned);
}
#elif defined(sgi)
#include <errno.h>
extern "C" {
	char *strerror(int);
}
#else
#include <errno.h>
#ifndef linux
extern char *sys_errlist[];
char *strerror(int errn) {
	return(sys_errlist[errn]);
}
#endif
#endif /* NeXT */

class GlobalResourceList {
	friend Application;
private:
	class ResourceMember {
	public:
		ResourceMember(const char* resname, const char* resval)
			: next(nil), resourceName(resname), resourceValue(resval) {}
		~ResourceMember() {}
		boolean nameMatches(const char* name) { return resourceName == name; }
		void setValue(const char* value) { resourceValue = value; }
		const char* value() { return resourceValue; }
		ResourceMember* next;
	private:
		String resourceName;
		String resourceValue;
	};
	void add(const char* resname, const char* resval);
	const char* retrieve(const char* resname);
protected:
	GlobalResourceList() {}
	~GlobalResourceList();
	static ResourceMember* head;
};

GlobalResourceList::ResourceMember* GlobalResourceList::head = nil;

GlobalResourceList::~GlobalResourceList() {
	ResourceMember* member = head;
	while(member != nil) {
		ResourceMember* next = member->next;
		delete member;
		member = next;
	}
}

void
GlobalResourceList::add(const char* resname, const char* resval) {
	if(head == nil) {
		head = new ResourceMember(resname, resval);
	}
	else {
		ResourceMember* member = head;
		while(member->next != nil) {
			if(member->nameMatches(resname)) {
				member->setValue(resval);	// if resource in list, just reset
				return;
			}
			member = member->next;
		}
		// otherwise add new member at end of list
		member->next = new ResourceMember(resname, resval);
	}
}	

const char *
GlobalResourceList::retrieve(const char* resname) {
	ResourceMember* member = head;
	while(member != nil) {
		if(member->nameMatches(resname))
			return member->value();
		member = member->next;
	}
	return nil;
}

int Application::worldVisible = 0;
World* Application::world = nil;
StatusBar* Application::statusBar = nil;
GlobalResourceList* Application::resourceList = nil;
Controller* Application::global_controller = nil;

extern void testit(World *);
extern void read_mxvrc();

Application::Application(int argc, char** argv,
		struct OptionDesc options[], struct PropertyData properties[]) {
#if defined(NeXT)
	malloc_singlethreaded();
#endif
	world = new World("MiXViews", argc, argv, options, properties);
	resourceList = new GlobalResourceList;
	read_mxvrc();
	Controller* controller = nil;
	if(argc == 1) {				// default when no args
		Sound* temp = new Sound(1.0, Sound::defaultSampleRate(), 1,
				Sound::defaultDataType());
		temp->modified(false);	// to avoid save warning when closed
		controller = new Controller(temp);
		controller->display(world);
	}
	else for(int argnum = 1; argnum < argc; ) {
		double skip = 0, dur = 0;
		char* filename = argv[argnum++];
		if(argnum < argc)
			argnum = extractSkipAndDurationArgs(argv, argc, argnum, skip, dur);
		DataFile* file = new DataFile(filename, "r", skip, dur);
		controller = Controller::create(file);
		if(controller)
			controller->display(world);
	}
	set_File_error_handler(error_handler);	// wait until after windows up
	createStatusBar(world);
	inform();
	worldVisible = true;
	testit(world);
}

int
Application::extractSkipAndDurationArgs(char** args, int nargs, int indx,
		double& skip, double& dur) {
	int maxarg = nargs - 1;
	while(indx <= maxarg) {
		char* arg = args[indx];
		if(strcmp(arg, "-skip") == 0 && indx <= maxarg) {
			indx++;
			skip = double(atof(args[indx++]));
			if(skip <= 0.0) {
				cerr << "Inskip arguments must be greater than zero -- ";
				cerr << "setting to " << (skip = -skip) << endl;
			}
		}
		else if(strcmp(arg, "-duration") == 0 && indx <= maxarg) {
			indx++;
			dur = double(atof(args[indx++]));
			if(dur <= 0.0) {
				cerr << "Duration arguments must be greater than zero -- ";
				cerr << "setting to " << (dur = -dur) << endl;
			}
		}
		else if(*arg == '-') {
			cerr << "Unknown flag: `" << arg << "` ignored." << endl;
			indx++;
		}
		else break;
	}
	return indx;
}

Application::~Application() {
	Converter::destroyInstance();	// in case of program exit
	delete resourceList;
	cout << "Exiting mxv." << endl;
}

int
Application::Run() {
	if(world != nil)
		world->Run();
	return 0;
}

void
Application::setGlobalController(Controller* c) { global_controller = c; }

boolean
Application::isGlobalController(Controller* c) {
	return c == global_controller;
}

void
Application::busy(boolean bsy) {
	global_controller->busy(bsy);
}

void
Application::inform(const char* string, boolean wait) {
	if(string == nil)
		return;
	static char msg[256];
	sprintf(msg, "%s%s", "Status:  ", string);
	if(statusBar != nil)
		statusBar->setText(msg);
	else
		cerr << msg << "\n";
	if(wait)
		sleep(1);
}

void
Application::inform() {
	inform("Ready");
}

void
Application::die(const char* string) {
	cerr << string << "\n";
	cerr.flush();
	abort();
}

void
Application::error(const char* msg1, const char* msg2) {
	const char* errmsg = errno ? strerror(errno) : nil;
	const char* str2 = msg2 ? msg2 : errmsg;
	const char* str3 = msg2 ? errmsg : nil;
	alert(msg1, str2, str3);
	errno = 0;
}

void
Application::error_handler(const char* msg) {
	error(msg);
}

void
Application::alert(const char* msg1, const char* msg2,
		const char* msg3, const char* btn) {
	if(global_controller)
		global_controller->alert(msg1, msg2, msg3, btn);
	else
		cerr << msg1 << endl << (msg2 ? msg2 : "") << endl << (msg3 ? msg3 : "") << endl;
}

boolean
Application::confirm(const char* msg1, const char* msg2,
		const char* msg3, Response r, const char* btn1, const char* btn2) {
	return global_controller->confirm(msg1, msg2, msg3, r, btn1, btn2);
}

Response
Application::choice(const char* msg1, const char* msg2, const char* msg3,
		Response r, const char* btn1, const char* btn2, const char* btn3) {
	return global_controller->choice(msg1, msg2, msg3, r, btn1, btn2, btn3);
}

int
Application::handle(Request &r) {
	return global_controller->handleRequest(r);
}

void
Application::createStatusBar(World *world) {
	statusBar = new StatusBar("MXV Status", 
		"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
	world->Insert(
		new TitleFrame(new Banner("", "MXV Status", ""), statusBar, 1), 
		300,
		world->Height() - 80
	);
}

void
Application::setGlobalResource(const char* resName, const char* resValue) {
	resourceList->add(resName, resValue);
}

// look in global resource list to see if value has been set
// if not there, attempt to retrieve from global attribute list

const char *
Application::getGlobalResource(const char* resName) {
	const char* gRes = resourceList->retrieve(resName);
	return (gRes != nil) ? gRes : world->GetAttribute(resName);
}

// short cut for checking if resource is true or set to yes

boolean
Application::globalResourceIsTrue(const char* resName) {
	static Regex Yes("YES\\|[Yy]es\\|TRUE\\|[Tt]rue");
	const char* res = getGlobalResource(resName);
	return (res != nil && Yes.match(res, strlen(res)) != -1);
}

boolean
Application::isLittleEndian() {
	int testInt = 1;
	char* endbyte = (char *) &testInt;
	return (*endbyte);
}