summaryrefslogtreecommitdiff
path: root/frontends/atari/misc.c
blob: 4866f05b1380711e09ec8e7a42f684e41ccb7af1 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright 2010 Ole Loots <ole@monochrom.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include <sys/types.h>
#include <mint/osbind.h>

#include "utils/nsoption.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/log.h"
#include "utils/file.h"
#include "utils/dirent.h"
#include "netsurf/mouse.h"
#include "netsurf/keypress.h"
#include "content/content.h"
#include "content/hlcache.h"
#include "desktop/cookie_manager.h"

#include "atari/gui.h"
#include "atari/toolbar.h"

#include "atari/misc.h"
#include "atari/encoding.h"
#include "atari/gemtk/gemtk.h"
#include "cflib.h"

extern void * h_gem_rsrc;

struct is_process_running_callback_data {
	const char * fname;
	bool found;
};

/* exported function documented in atari/misc/h */
nserror atari_warn_user(const char *warning, const char *detail)
{
	size_t len = 1 + ((warning != NULL) ? strlen(messages_get(warning)) :
			0) + ((detail != 0) ? strlen(detail) : 0);
	char message[len];
	snprintf(message, len, messages_get(warning), detail);

	printf("%s\n", message);
	gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT, message);

	return NSERROR_OK;
}

void die(const char *error)
{
	printf("%s\n", error);
	gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT, error);
	exit(1);
}


struct gui_window * find_guiwin_by_aes_handle(short handle){

	struct gui_window * gw;
	gw = window_list;

	if( handle == 0 ){
		return( NULL );
	}

	while(gw != NULL) {
		if(gw->root->win != NULL
			&& gemtk_wm_get_handle(gw->root->win) == handle) {
				return(gw);
		}
		else
			gw = gw->next;
	}

	return( NULL );
}


static int scan_process_list(scan_process_callback cb, void *data)
{
	int pid, count = 0;
	DIR	*dir;
	char*dirname;
	struct dirent *de;

	if (( dir = opendir("U:/kern")) == NULL)
		return(0);

	while ((de = readdir( dir)) != NULL) {
		dirname = de->d_name;

		if( dirname[0] != '1' && dirname[0] != '2' && dirname[0] != '3' && dirname[0] != '4' && dirname[0] != '5'
		 && dirname[0] != '6' && dirname[0] != '7' && dirname[0] != '8' && dirname[0] != '9')
			continue;

		count++;
		if (cb != NULL) {
			/* when callback returns negative value, we stop scanning: */
			pid = atoi(dirname);
			if (cb(pid, data)<0) {
				break;
			}
		}
	}

	closedir(dir);

	return(count);
}

static int proc_running_callback(int pid, void * arg)
{
	char buf[PATH_MAX], fnamepath[256];
	FILE *fp;
	int nread;
	struct is_process_running_callback_data *data;

	data = (struct is_process_running_callback_data *)arg;

	sprintf(fnamepath, "U:\\kern\\%d\\fname", pid);
	printf("checking: %s\n", fnamepath);

	fp = fopen(fnamepath, "r");
	if(!fp)
		return(0);

	nread = fread(buf, 1, PATH_MAX-1, fp);
	fclose(fp);
	nread = MIN(PATH_MAX-1, nread);

	if (nread > 0) {
		buf[nread] = 0;

		char *lastslash = strrchr(buf, '/');

		if(lastslash == NULL)
			lastslash = strrchr(buf, '\\');

		if(lastslash==NULL)
			lastslash = buf;
		else
			lastslash++;

		if(strcasecmp(lastslash, data->fname)==0){
			/* found process, check status: */
			sprintf(fnamepath, "U:\\kern\\%d\\status", pid);
			fp = fopen(fnamepath, "r");
			if (fp) {
				nread = fread(buf, 1, PATH_MAX-1, fp);
				fclose(fp);
				if (nread>0) {
					nread = MIN(PATH_MAX-1,nread);
				}
				buf[nread] = 0;
				if (strstr(buf, "zombie")==NULL) {
					data->found = true;
					return(-1);
				}
			}

		}
	}
	return(0);
}

bool is_process_running(const char * name)
{
	struct is_process_running_callback_data data = {name, false};

	scan_process_list(proc_running_callback, &data);

	return( (data.found==1) ? true : false );
}


void gem_set_cursor( MFORM_EX * cursor )
{
	static unsigned char flags = 255;
	static int number = 255;
	if( flags == cursor->flags && number == cursor->number )
		return;
	if( cursor->flags & MFORM_EX_FLAG_USERFORM ) {
		gemtk_obj_mouse_sprite(cursor->tree, cursor->number);
	} else {
		graf_mouse(cursor->number, NULL );
	}
	number = cursor->number;
	flags = cursor->flags;
}


/* exported interface documented in atari/misc.h */
long nkc_to_input_key(short nkc, long * ucs4_out)
{
	unsigned char ascii = (nkc & 0xFF);
	long ik = 0;

	// initialize result:
	*ucs4_out = 0;

	// sanitize input key:
	nkc = (nkc & (NKF_CTRL|NKF_SHIFT|0xFF));

	/* shift + cntrl key: */
	if( ((nkc & NKF_CTRL) == NKF_CTRL) && ((nkc & (NKF_SHIFT))!=0) ) {

	}
	/* cntrl key only: */
	else if( (nkc & NKF_CTRL) == NKF_CTRL ) {
		switch ( ascii ) {
			case 'A':
				ik = NS_KEY_SELECT_ALL;
			break;

			case 'C':
				ik = NS_KEY_COPY_SELECTION;
			break;

			case 'X':
				ik = NS_KEY_CUT_SELECTION;
			break;

			case 'V':
				ik = NS_KEY_PASTE;
			break;

			default:
			break;
		}
	}
	/* shift key only: */
	else if( (nkc & NKF_SHIFT) != 0 ) {
		switch( ascii ) {
			case NK_TAB:
				ik = NS_KEY_SHIFT_TAB;
			break;

			case NK_LEFT:
				ik = NS_KEY_LINE_START;
			break;

			case NK_RIGHT:
				ik = NS_KEY_LINE_END;
			break;

			case NK_UP:
				ik = NS_KEY_PAGE_UP;
			break;

			case NK_DOWN:
				ik = NS_KEY_PAGE_DOWN;
			break;

			default:
			break;
		}
	}
	/* No modifier keys: */
	else {
		switch( ascii ) {

			case NK_INS:
				ik = NS_KEY_PASTE;
				break;

			case NK_BS:
				ik = NS_KEY_DELETE_LEFT;
			break;

			case NK_DEL:
				ik = NS_KEY_DELETE_RIGHT;
			break;

			case NK_TAB:
				ik = NS_KEY_TAB;
			break;


			case NK_ENTER:
				ik = NS_KEY_NL;
			break;

			case NK_RET:
				ik = NS_KEY_CR;
			break;

			case NK_ESC:
				ik = NS_KEY_ESCAPE;
			break;

			case NK_CLRHOME:
				ik = NS_KEY_TEXT_START;
			break;

			case NK_RIGHT:
				ik = NS_KEY_RIGHT;
			break;

			case NK_LEFT:
				ik = NS_KEY_LEFT;
			break;

			case NK_UP:
				ik = NS_KEY_UP;
			break;

			case NK_UNDO:
				ik = NS_KEY_UNDO;
			break;

			case NK_DOWN:
				ik = NS_KEY_DOWN;
			break;

			case NK_M_PGUP:
				ik = NS_KEY_PAGE_UP;
			break;

			case NK_M_PGDOWN:
				ik = NS_KEY_PAGE_DOWN;
			break;

			default:
			break;
		}
	}

	if( ik == 0 && ( (nkc & NKF_CTRL)==0)  ) {
		if (ascii >= 9 ) {
			*ucs4_out = atari_to_ucs4(ascii);
		}
	}
	return ( ik );
}

/**
 * Show default file selector
 *
 * \param title  The selector title.
 * \param name	 Default file name
 * \return a static char pointer or null if the user aborted the selection.
 */
const char * file_select(const char * title, const char * name) {

	static char path[PATH_MAX]=""; // First usage : current directory
	static char fullname[PATH_MAX]="";
	char tmpname[255];
	char * use_title = (char*)title;

	if( strlen(name)>254)
		return( NULL );

	strcpy(tmpname, name);

	if( use_title == NULL ){
		use_title = (char*)"";
	}

	if (select_file(path, tmpname, (char*)"*", use_title, NULL)) {
		snprintf(fullname, PATH_MAX, "%s%s", path, tmpname);
		return((const char*)&fullname);
	}

	return( NULL );
}


void dbg_grect(const char * str, GRECT * r)
{
	printf("%s: x: %d, y: %d, w: %d, h: %d (x2: %d, y2: %d)\n", str,
		r->g_x, r->g_y, r->g_w, r->g_h, r->g_x + r->g_w, r->g_y + r->g_h);
}

void dbg_pxy(const char * str, short * pxy )
{
	printf("%s: x: %d, y: %d, w: %d, h: %d\n", str,
		pxy[0], pxy[1], pxy[2], pxy[3] );
}

void dbg_rect(const char * str, int * pxy)
{
	printf("%s: x0: %d, y0: %d, x1: %d, y1: %d (w: %d, h: %d)\n", str,
		pxy[0], pxy[1], pxy[2], pxy[3],
		pxy[2] - pxy[0],
		pxy[3] - pxy[1] );
}