NetSurf
misc.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Ole Loots <ole@monochrom.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <assert.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdbool.h>
23#include <string.h>
24
25#include <sys/types.h>
26#include <mint/osbind.h>
27
28#include "utils/nsoption.h"
29#include "utils/messages.h"
30#include "utils/utils.h"
31#include "utils/log.h"
32#include "utils/file.h"
33#include "utils/dirent.h"
34#include "netsurf/mouse.h"
35#include "netsurf/keypress.h"
36#include "content/content.h"
37#include "content/hlcache.h"
39
40#include "atari/gui.h"
41#include "atari/toolbar.h"
42
43#include "atari/misc.h"
44#include "atari/encoding.h"
45#include "atari/gemtk/gemtk.h"
46#include "cflib.h"
47
48extern void * h_gem_rsrc;
49
51 const char * fname;
52 bool found;
53};
54
55/* exported function documented in atari/misc/h */
56nserror atari_warn_user(const char *warning, const char *detail)
57{
58 size_t len = 1 + ((warning != NULL) ? strlen(messages_get(warning)) :
59 0) + ((detail != 0) ? strlen(detail) : 0);
60 char message[len];
61 snprintf(message, len, messages_get(warning), detail);
62
63 printf("%s\n", message);
64 gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT, message);
65
66 return NSERROR_OK;
67}
68
69void die(const char *error)
70{
71 printf("%s\n", error);
72 gemtk_msg_box_show(GEMTK_MSG_BOX_ALERT, error);
73 exit(1);
74}
75
76
78
79 struct gui_window * gw;
80 gw = window_list;
81
82 if( handle == 0 ){
83 return( NULL );
84 }
85
86 while(gw != NULL) {
87 if(gw->root->win != NULL
88 && gemtk_wm_get_handle(gw->root->win) == handle) {
89 return(gw);
90 }
91 else
92 gw = gw->next;
93 }
94
95 return( NULL );
96}
97
98
99static int scan_process_list(scan_process_callback cb, void *data)
100{
101 int pid, count = 0;
102 DIR *dir;
103 char*dirname;
104 struct dirent *de;
105
106 if (( dir = opendir("U:/kern")) == NULL)
107 return(0);
108
109 while ((de = readdir( dir)) != NULL) {
110 dirname = de->d_name;
111
112 if( dirname[0] != '1' && dirname[0] != '2' && dirname[0] != '3' && dirname[0] != '4' && dirname[0] != '5'
113 && dirname[0] != '6' && dirname[0] != '7' && dirname[0] != '8' && dirname[0] != '9')
114 continue;
115
116 count++;
117 if (cb != NULL) {
118 /* when callback returns negative value, we stop scanning: */
119 pid = atoi(dirname);
120 if (cb(pid, data)<0) {
121 break;
122 }
123 }
124 }
125
126 closedir(dir);
127
128 return(count);
129}
130
131static int proc_running_callback(int pid, void * arg)
132{
133 char buf[PATH_MAX], fnamepath[256];
134 FILE *fp;
135 int nread;
137
138 data = (struct is_process_running_callback_data *)arg;
139
140 sprintf(fnamepath, "U:\\kern\\%d\\fname", pid);
141 printf("checking: %s\n", fnamepath);
142
143 fp = fopen(fnamepath, "r");
144 if(!fp)
145 return(0);
146
147 nread = fread(buf, 1, PATH_MAX-1, fp);
148 fclose(fp);
149 nread = MIN(PATH_MAX-1, nread);
150
151 if (nread > 0) {
152 buf[nread] = 0;
153
154 char *lastslash = strrchr(buf, '/');
155
156 if(lastslash == NULL)
157 lastslash = strrchr(buf, '\\');
158
159 if(lastslash==NULL)
160 lastslash = buf;
161 else
162 lastslash++;
163
164 if(strcasecmp(lastslash, data->fname)==0){
165 /* found process, check status: */
166 sprintf(fnamepath, "U:\\kern\\%d\\status", pid);
167 fp = fopen(fnamepath, "r");
168 if (fp) {
169 nread = fread(buf, 1, PATH_MAX-1, fp);
170 fclose(fp);
171 if (nread>0) {
172 nread = MIN(PATH_MAX-1,nread);
173 }
174 buf[nread] = 0;
175 if (strstr(buf, "zombie")==NULL) {
176 data->found = true;
177 return(-1);
178 }
179 }
180
181 }
182 }
183 return(0);
184}
185
186bool is_process_running(const char * name)
187{
188 struct is_process_running_callback_data data = {name, false};
189
191
192 return( (data.found==1) ? true : false );
193}
194
195
196void gem_set_cursor( MFORM_EX * cursor )
197{
198 static unsigned char flags = 255;
199 static int number = 255;
200 if( flags == cursor->flags && number == cursor->number )
201 return;
202 if( cursor->flags & MFORM_EX_FLAG_USERFORM ) {
203 gemtk_obj_mouse_sprite(cursor->tree, cursor->number);
204 } else {
205 graf_mouse(cursor->number, NULL );
206 }
207 number = cursor->number;
208 flags = cursor->flags;
209}
210
211
212/* exported interface documented in atari/misc.h */
213long nkc_to_input_key(short nkc, long * ucs4_out)
214{
215 unsigned char ascii = (nkc & 0xFF);
216 long ik = 0;
217
218 // initialize result:
219 *ucs4_out = 0;
220
221 // sanitize input key:
222 nkc = (nkc & (NKF_CTRL|NKF_SHIFT|0xFF));
223
224 /* shift + cntrl key: */
225 if( ((nkc & NKF_CTRL) == NKF_CTRL) && ((nkc & (NKF_SHIFT))!=0) ) {
226
227 }
228 /* cntrl key only: */
229 else if( (nkc & NKF_CTRL) == NKF_CTRL ) {
230 switch ( ascii ) {
231 case 'A':
233 break;
234
235 case 'C':
237 break;
238
239 case 'X':
241 break;
242
243 case 'V':
244 ik = NS_KEY_PASTE;
245 break;
246
247 default:
248 break;
249 }
250 }
251 /* shift key only: */
252 else if( (nkc & NKF_SHIFT) != 0 ) {
253 switch( ascii ) {
254 case NK_TAB:
255 ik = NS_KEY_SHIFT_TAB;
256 break;
257
258 case NK_LEFT:
260 break;
261
262 case NK_RIGHT:
263 ik = NS_KEY_LINE_END;
264 break;
265
266 case NK_UP:
267 ik = NS_KEY_PAGE_UP;
268 break;
269
270 case NK_DOWN:
271 ik = NS_KEY_PAGE_DOWN;
272 break;
273
274 default:
275 break;
276 }
277 }
278 /* No modifier keys: */
279 else {
280 switch( ascii ) {
281
282 case NK_INS:
283 ik = NS_KEY_PASTE;
284 break;
285
286 case NK_BS:
288 break;
289
290 case NK_DEL:
292 break;
293
294 case NK_TAB:
295 ik = NS_KEY_TAB;
296 break;
297
298
299 case NK_ENTER:
300 ik = NS_KEY_NL;
301 break;
302
303 case NK_RET:
304 ik = NS_KEY_CR;
305 break;
306
307 case NK_ESC:
308 ik = NS_KEY_ESCAPE;
309 break;
310
311 case NK_CLRHOME:
313 break;
314
315 case NK_RIGHT:
316 ik = NS_KEY_RIGHT;
317 break;
318
319 case NK_LEFT:
320 ik = NS_KEY_LEFT;
321 break;
322
323 case NK_UP:
324 ik = NS_KEY_UP;
325 break;
326
327 case NK_UNDO:
328 ik = NS_KEY_UNDO;
329 break;
330
331 case NK_DOWN:
332 ik = NS_KEY_DOWN;
333 break;
334
335 case NK_M_PGUP:
336 ik = NS_KEY_PAGE_UP;
337 break;
338
339 case NK_M_PGDOWN:
340 ik = NS_KEY_PAGE_DOWN;
341 break;
342
343 default:
344 break;
345 }
346 }
347
348 if( ik == 0 && ( (nkc & NKF_CTRL)==0) ) {
349 if (ascii >= 9 ) {
350 *ucs4_out = atari_to_ucs4(ascii);
351 }
352 }
353 return ( ik );
354}
355
356/**
357 * Show default file selector
358 *
359 * \param title The selector title.
360 * \param name Default file name
361 * \return a static char pointer or null if the user aborted the selection.
362 */
363const char * file_select(const char * title, const char * name) {
364
365 static char path[PATH_MAX]=""; // First usage : current directory
366 static char fullname[PATH_MAX]="";
367 char tmpname[255];
368 char * use_title = (char*)title;
369
370 if( strlen(name)>254)
371 return( NULL );
372
373 strcpy(tmpname, name);
374
375 if( use_title == NULL ){
376 use_title = (char*)"";
377 }
378
379 if (select_file(path, tmpname, (char*)"*", use_title, NULL)) {
380 snprintf(fullname, PATH_MAX, "%s%s", path, tmpname);
381 return((const char*)&fullname);
382 }
383
384 return( NULL );
385}
386
387
388void dbg_grect(const char * str, GRECT * r)
389{
390 printf("%s: x: %d, y: %d, w: %d, h: %d (x2: %d, y2: %d)\n", str,
391 r->g_x, r->g_y, r->g_w, r->g_h, r->g_x + r->g_w, r->g_y + r->g_h);
392}
393
394void dbg_pxy(const char * str, short * pxy )
395{
396 printf("%s: x: %d, y: %d, w: %d, h: %d\n", str,
397 pxy[0], pxy[1], pxy[2], pxy[3] );
398}
399
400void dbg_rect(const char * str, int * pxy)
401{
402 printf("%s: x0: %d, y0: %d, x1: %d, y1: %d (w: %d, h: %d)\n", str,
403 pxy[0], pxy[1], pxy[2], pxy[3],
404 pxy[2] - pxy[0],
405 pxy[3] - pxy[1] );
406}
407
static struct MinList * window_list
Definition: gui.c:327
#define MFORM_EX_FLAG_USERFORM
Definition: gui.h:51
#define PATH_MAX
Definition: gui.h:31
void die(const char *error)
Cause an abnormal program termination.
Definition: misc.c:69
void dbg_pxy(const char *str, short *pxy)
Definition: misc.c:394
void * h_gem_rsrc
Definition: gui.c:76
static int proc_running_callback(int pid, void *arg)
Definition: misc.c:131
void dbg_grect(const char *str, GRECT *r)
Definition: misc.c:388
const char * file_select(const char *title, const char *name)
Show default file selector.
Definition: misc.c:363
bool is_process_running(const char *name)
Definition: misc.c:186
void gem_set_cursor(MFORM_EX *cursor)
Definition: misc.c:196
nserror atari_warn_user(const char *warning, const char *detail)
Warn the user of an event.
Definition: misc.c:56
void dbg_rect(const char *str, int *pxy)
Definition: misc.c:400
long nkc_to_input_key(short nkc, long *ucs4_out)
Convert NKC to netsurf input key code and/or to ucs4 (depends on keycode).
Definition: misc.c:213
struct gui_window * find_guiwin_by_aes_handle(short handle)
Definition: misc.c:77
static int scan_process_list(scan_process_callback cb, void *data)
Definition: misc.c:99
static uint32_t count(const http_directive *list, lwc_string *key)
Content handling interface.
Cookie Manager (interface).
directory traversal and entry
int atari_to_ucs4(unsigned char atari)
Definition: encoding.c:64
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
int(* scan_process_callback)(int pid, void *data)
Definition: misc.h:49
High-level resource cache interface.
Core mouse and pointer states.
Interface to key press operations.
@ NS_KEY_SHIFT_TAB
Definition: keypress.h:39
@ NS_KEY_LINE_START
Definition: keypress.h:57
@ NS_KEY_CR
Definition: keypress.h:40
@ NS_KEY_RIGHT
Definition: keypress.h:51
@ NS_KEY_LEFT
Definition: keypress.h:50
@ NS_KEY_SELECT_ALL
Definition: keypress.h:32
@ NS_KEY_PASTE
Definition: keypress.h:43
@ NS_KEY_TAB
Definition: keypress.h:36
@ NS_KEY_COPY_SELECTION
Definition: keypress.h:33
@ NS_KEY_DOWN
Definition: keypress.h:53
@ NS_KEY_CUT_SELECTION
Definition: keypress.h:44
@ NS_KEY_PAGE_UP
Definition: keypress.h:65
@ NS_KEY_PAGE_DOWN
Definition: keypress.h:66
@ NS_KEY_UNDO
Definition: keypress.h:70
@ NS_KEY_TEXT_START
Definition: keypress.h:59
@ NS_KEY_LINE_END
Definition: keypress.h:58
@ NS_KEY_DELETE_RIGHT
Definition: keypress.h:55
@ NS_KEY_NL
Definition: keypress.h:38
@ NS_KEY_DELETE_LEFT
Definition: keypress.h:35
@ NS_KEY_UP
Definition: keypress.h:52
@ NS_KEY_ESCAPE
Definition: keypress.h:47
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
#define MIN(a, b)
Definition: os3support.h:51
Interface to utility string handling.
first entry in window list
Definition: gui.c:298
struct gui_window * next
list for cleanup
Definition: gui.h:159
struct s_gui_win_root * root
Definition: gui.h:148
Definition: gui.h:54
OBJECT * tree
Definition: gui.h:57
unsigned char flags
Definition: gui.h:55
int number
Definition: gui.h:56
GUIWIN * win
Definition: gui.h:121
Default operations table for files.
Option reading and saving interface.
Interface to a number of general purpose functionality.
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821