aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/hd24connect.cpp
blob: b31b758c691d6b92ad8521a8167c3e2172cb6f24 (plain)
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
#define MAINDEBUG 0
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include "ui_hd24connect.h"
#include <hd24fs.h>
#include <hd24utils.h>
#include <FL/x.H> //dave
#ifdef LINUX
#include <X11/xpm.h> //dave
#endif
#include "images/hd24connect_64x64.xpm" //dave
#include <iostream>
#include <string>
#include <stdlib.h>
#include <hd24utils.h>
#ifndef MAX_PATH
#define MAX_PATH 127
#endif

#ifdef WINDOWS
#include <windows.h>
#define IDI_ICON1 101
#endif
string device;
string headerfilename;
int force;
int maintmode;
int wavefixmode;
int testmode;
#ifdef LINUX
#define TIMEOUT 0.03
#endif
#define ARGDEV "--dev="
#define ARGHEADER "--header="
#define ARGFORCE "--force"
#define ARGMAINT "--maint"
#define ARGWAVEFIX "--wavefix"
#define ARGTEST "--test"

#include "selftest.cpp"

int parsecommandline(int argc, char ** argv) 
{
	int invalid=0;
	force=0;
	maintmode=0;
	wavefixmode=0;
	testmode=0;
	device="";
	for (int c=1;c<argc;c++) {

		string arg=argv[c];
		if (arg.substr(0,1)=="-") {
			if (arg.substr(1,1)!="-") {
				continue; // skip single-dash arguments
				// for instance to skip -display
				// and to skip macOS process number
			}
		}
		if (arg.substr(0,strlen(ARGTEST))==ARGTEST) {
			testmode=1;
			continue;
		}
		if (arg.substr(0,strlen(ARGDEV))==ARGDEV) {
			device=arg.substr(strlen(ARGDEV));
			continue;
		}
		if (arg.substr(0,strlen(ARGMAINT))==ARGMAINT) {
			maintmode=1;
#if (MAINDEBUG==1)
			cout << "Running in maintenance mode." << endl;
#endif
			continue;
		}
		if (arg.substr(0,strlen(ARGWAVEFIX))==ARGWAVEFIX) {
			wavefixmode=1;
#if (MAINDEBUG==1)
			cout << "Running in wavefix mode." << endl;
#endif
			continue;
		}
		if (arg.substr(0,strlen(ARGHEADER))==ARGHEADER) {
			headerfilename=arg.substr(strlen(ARGHEADER));
			continue;
		}
		if (arg.substr(0,strlen(ARGFORCE))==(ARGFORCE)) {
			force=1;
			continue;
		}
#ifdef DARWIN
		// on MacOS ignore all crap on the command line
		// (system adds process ID info etc)
		continue;
#endif

#if (MAINDEBUG==1)
		cout << "Invalid argument: " << arg << endl;
#endif
		invalid=1;
	}
	return invalid;
}

int isabsolutepath(const char* pathname) {
  if (pathname[0]=='/') return (1==1);
  if (pathname[0]=='\\') return (1==1);
  if (pathname[1]==':') return (1==1);
  return (1==0);
}

void getprogdir(const char* currpath,const char* callpath,char* result) {
        if (isabsolutepath(callpath)) {
                strncpy(result,callpath,strlen(callpath)+1);
                return;
        }
        strcat(result,currpath);
#ifdef WINDOWS
        strcat(result,"\\\0");
#else
        strcat(result,"/\0");
#endif

        if (strcmp(callpath,"./")!=0) {
                strcat(result,callpath);
        }
        return;
}


int main(int argc, char **argv) {

	int result=0;

	/* First, the program will figure out its own
	   absolute program path. This will be used
           for loading libraries after program startup,
           rather than having hard dependencies *during*. */
	char* currentworkingdir=(char*)malloc(2048);
#if (MAINDEBUG==1)
	cout << "Welcome to HD24connect" << endl;
#endif
        string currname=argv[0];
        string rawname="";
#if (MAINDEBUG==1)
	cout << "Currname=" << currname << endl;
#endif
        do {
                string lastchar="";
                lastchar+=currname.substr(currname.length()-1,1);
                if ((lastchar=="/") || (lastchar=="\\")) {
                        break;
                }
                rawname=currname.substr(currname.length()-1,1)+rawname;
                currname=currname.substr(0,currname.length()-1);
           
                if (currname=="") break;
        } while (1==1);
        // currname=path given on commandline
	
        char cwd[2048];
        char* mycwd=getcwd(&cwd[0],2048);
	mycwd=&cwd[0];
#if (MAINDEBUG==1)
	cout << "cwd=" << cwd << endl;
#endif
        char absprogpath[2048];
        char fullname[2048];
	absprogpath[0]='\0';
        getprogdir(&cwd[0],currname.c_str(),&absprogpath[0]);

#if (MAINDEBUG==1)
	cout << "absprogpath=" << absprogpath << endl;
#endif
        free(currentworkingdir);

	/*
         * Either absprogpath now contains absolute program path
         * (if called from current path or by specifying path)
         * or we need to traverse the OS PATH.
         */
	strncpy(fullname,absprogpath,2048);
        strcat(fullname,rawname.c_str());
#if (MAINDEBUG==1)
	cout << "fullname=" << fullname << endl;
#endif
        if (!(hd24utils::fileExists(fullname))) {
		/* File according to full name does not exist. */
#if (MAINDEBUG==1)
		cout << "rawname=" << rawname << ", path=" << getenv("PATH") << endl;
#endif
        	hd24utils::findfile(&rawname[0],getenv("PATH"),&absprogpath[0]);
		if (strlen(absprogpath)==0) {
			cout << "Cannot find executable. "<< endl;
			return 0;
		}
	}
	// The final word on which path contains the exe is absprogpath.
#if (MAINDEBUG==1) 
	cout << "The final word on where the exe is, is: " << absprogpath << endl;
	cout << "main debugmode enabled." << endl;
	cout << " Sizeof float=" << sizeof(float) << endl;
#endif	
	char initial_path[MAX_PATH];
	mycwd=getcwd(initial_path,MAX_PATH);
	// This prints the path+filename to ourselves:
	// If we run from current dir, argv is just filename;
	// otherwise missing part of path+filename.
	// So initial path+filename always renders the complete path.
	int invalid=parsecommandline(argc,argv);
	if (invalid!=0) {
		return invalid;
	}
	//force=0;

	hd24fs* sysob=NULL;
	if (testmode==1)
	{
#ifdef LINUX
		fl_open_display();
#endif
		HD24UserInterface ui(argc,argv,absprogpath);
		// testmode active- run self test.
		result=runselftest(&ui);
		cout << "Selftest run complete - Thank you for using HD24connect." << endl;
		ui.finish();
		return result;
	}	
	
	string* imagedir=hd24utils::getlastdir("driveimagedir");
#if (MAINDEBUG==1) 
	cout << "imagedir="<<*imagedir<< endl;
#endif		
	if (device=="") {
		sysob=new hd24fs(imagedir->c_str(),hd24fs::MODE_RDWR);
	} else {
		cout << "Trying to use " << device << " as hd24 device." << endl;
		sysob=new hd24fs(imagedir->c_str(),hd24fs::MODE_RDWR,&device,(force==1));
		if (!(sysob->isOpen())) 
		{
			if (!force) {
				cout << "Cannot open hd24 device" << endl;
				delete sysob;
				return 1;
			}
		}
	}
	delete imagedir;
	if (headerfilename!="") 
	{
		if (!(sysob->useheaderfile(headerfilename))) 
		{
			cout << "Couldn't load header file "<<headerfilename << endl;
		};
	}
#if (MAINDEBUG==1) 
	cout << "mdb(2)" << endl;
#endif	
//	Fl::scheme("gtk+");
#ifdef LINUX
	fl_open_display();
#endif
	HD24UserInterface ui(argc,argv,absprogpath);
#if (MAINDEBUG==1) 
	cout << "mdb(3b)" << endl;
#endif	
	sysob->setmaintenancemode(maintmode);
	sysob->setwavefixmode(wavefixmode);
#if (MAINDEBUG==1) 
	cout << "mdb(3c)" << endl;
#endif	
	Fl_Window *window = ui.make_window(sysob);
#if (MAINDEBUG==1) 
	cout << "mdb(3d)" << endl;
#endif	
#ifdef WINDOWS

	window->icon((char *)LoadIcon(fl_display,MAKEINTRESOURCE(IDI_ICON1)));
#endif
	window->end();
	window->show(); //argc, argv);

#ifdef LINUX //dave
        Pixmap p, mask;
        XpmCreatePixmapFromData(fl_display,DefaultRootWindow(fl_display),(char **)hd24connect_xpm, &p, &mask, NULL);

/*
FLTK support for transparent background is broken. So instead of this:
window->icon((char *)p);
We will write the following: */
XWMHints *hints=NULL;
hints = XGetWMHints(fl_display, fl_xid(window));
hints->icon_pixmap = p;
hints->icon_mask = mask;
hints->flags = IconPixmapHint | IconMaskHint;
XSetWMHints(fl_display, fl_xid(window), hints);
/* -- end of transparency fix */
#endif
#if (MAINDEBUG==1) 
	cout << "mdb(3)" << endl;
#endif	
#if (MAINDEBUG==1) 
	cout << "mdb(4)" << endl;
#endif	
	if (!(sysob->isOpen())) 
	{
		window->deactivate();
		fl_message("No valid HD24 drive is connected to the system.\nUse the File menu to resolve this problem.");
		window->activate();
	}
	// run normally.
	result= Fl::run(); 
	ui.control->ready(0);
#ifndef WINDOWS
	cout << "Thank you for using HD24connect." << endl;
#endif
//	ui.finish();
	
#ifdef hints
	if (hints!=NULL)
	{
		delete hints;
	}
#endif
	return result;
}