summaryrefslogtreecommitdiff
path: root/frontends/monkey/browser.c
blob: 3bf0dd0362426625897a858da1eadd266fb52618 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.org>
 *
 * 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/>.
 */

/* Browser-related callbacks */

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

#include "utils/utils.h"
#include "utils/ring.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/nsurl.h"
#include "netsurf/mouse.h"
#include "netsurf/window.h"
#include "netsurf/browser_window.h"
#include "netsurf/plotters.h"

#include "monkey/browser.h"
#include "monkey/plot.h"

static uint32_t win_ctr = 0;

static struct gui_window *gw_ring = NULL;

/* exported function documented in monkey/browser.h */
nserror monkey_warn_user(const char *warning, const char *detail)
{
  fprintf(stderr, "WARN %s %s\n", warning, detail);
  return NSERROR_OK;
}

struct gui_window *
monkey_find_window_by_num(uint32_t win_num)
{
  struct gui_window *ret = NULL;
  
  RING_ITERATE_START(struct gui_window, gw_ring, c_ring) {
    if (c_ring->win_num == win_num) {
      ret = c_ring;
      RING_ITERATE_STOP(gw_ring, c_ring);
    }
  } RING_ITERATE_END(gw_ring, c_ring);
  
  return ret;
}


/**
 * callback from core to reformat a window.
 */
static void monkey_window_reformat(struct gui_window *gw)
{
	browser_window_reformat(gw->bw,	false, gw->width, gw->height);
}

void
monkey_kill_browser_windows(void)
{
  while (gw_ring != NULL) {
    browser_window_destroy(gw_ring->bw);
  }
}

static struct gui_window *
gui_window_create(struct browser_window *bw,
		struct gui_window *existing,
		gui_window_create_flags flags)
{
  struct gui_window *ret = calloc(sizeof(*ret), 1);
  if (ret == NULL)
    return NULL;
  
  ret->win_num = win_ctr++;
  ret->bw = bw;
  
  ret->width = 800;
  ret->height = 600;
  
  fprintf(stdout, "WINDOW NEW WIN %u FOR %p EXISTING %p NEWTAB %s CLONE %s\n",
          ret->win_num, bw, existing, flags & GW_CREATE_TAB ? "TRUE" : "FALSE",
          flags & GW_CREATE_CLONE ? "TRUE" : "FALSE");
  fprintf(stdout, "WINDOW SIZE WIN %u WIDTH %d HEIGHT %d\n",
          ret->win_num, ret->width, ret->height);
  
  RING_INSERT(gw_ring, ret);
  
  return ret;
}

static void
gui_window_destroy(struct gui_window *g)
{
  fprintf(stdout, "WINDOW DESTROY WIN %u\n", g->win_num);
  RING_REMOVE(gw_ring, g);
  free(g);
}

static void
gui_window_set_title(struct gui_window *g, const char *title)
{
  fprintf(stdout, "WINDOW TITLE WIN %u STR %s\n", g->win_num, title);
}

static void
gui_window_redraw_window(struct gui_window *g)
{
  fprintf(stdout, "WINDOW REDRAW WIN %u\n", g->win_num);
}

static void
gui_window_get_dimensions(struct gui_window *g, int *width, int *height,
                          bool scaled)
{
  fprintf(stdout, "WINDOW GET_DIMENSIONS WIN %u WIDTH %d HEIGHT %d\n",
          g->win_num, g->width, g->height);
  *width = g->width;
  *height = g->height;
}

static void
gui_window_new_content(struct gui_window *g)
{
  fprintf(stdout, "WINDOW NEW_CONTENT WIN %u\n", g->win_num);
}

static void
gui_window_set_icon(struct gui_window *g, struct hlcache_handle *icon)
{
  fprintf(stdout, "WINDOW NEW_ICON WIN %u\n", g->win_num);
}

static void
gui_window_start_throbber(struct gui_window *g)
{
  fprintf(stdout, "WINDOW START_THROBBER WIN %u\n", g->win_num);
}

static void
gui_window_stop_throbber(struct gui_window *g)
{
  fprintf(stdout, "WINDOW STOP_THROBBER WIN %u\n", g->win_num);
}

static void
gui_window_set_scroll(struct gui_window *g, int sx, int sy)
{
  g->scrollx = sx;
  g->scrolly = sy;
  fprintf(stdout, "WINDOW SET_SCROLL WIN %u X %d Y %d\n", g->win_num, sx, sy);
}

static void
gui_window_update_box(struct gui_window *g, const struct rect *rect)
{
  fprintf(stdout, "WINDOW UPDATE_BOX WIN %u X %d Y %d WIDTH %d HEIGHT %d\n",
          g->win_num, rect->x0, rect->y0,
          (rect->x1 - rect->x0), (rect->y1 - rect->y0));
  
}

static void
gui_window_update_extent(struct gui_window *g)
{
  int width, height;

  if (browser_window_get_extents(g->bw, false, &width, &height) != NSERROR_OK)
    return;

  fprintf(stdout, "WINDOW UPDATE_EXTENT WIN %u WIDTH %d HEIGHT %d\n", 
          g->win_num, width, height);
}

static void
gui_window_set_status(struct gui_window *g, const char *text)
{
  fprintf(stdout, "WINDOW SET_STATUS WIN %u STR %s\n", g->win_num, text);
}

static void
gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape)
{
  const char *ptr_name = "UNKNOWN";
  
  switch (shape) {
  case GUI_POINTER_POINT:
    ptr_name = "POINT";
    break;
  case GUI_POINTER_CARET:
    ptr_name = "CARET";
    break;
  case GUI_POINTER_UP:
    ptr_name = "UP";
    break;
  case GUI_POINTER_DOWN:
    ptr_name = "DOWN";
    break;
  case GUI_POINTER_LEFT:
    ptr_name = "LEFT";
    break;
  case GUI_POINTER_RIGHT:
    ptr_name = "RIGHT";
    break;
  case GUI_POINTER_LD:
    ptr_name = "LD";
    break;
  case GUI_POINTER_RD:
    ptr_name = "RD";
    break;
  case GUI_POINTER_LU:
    ptr_name = "LU";
    break;
  case GUI_POINTER_RU:
    ptr_name = "RU";
    break;
  case GUI_POINTER_CROSS:
    ptr_name = "CROSS";
    break;
  case GUI_POINTER_MOVE:
    ptr_name = "MOVE";
    break;
  case GUI_POINTER_WAIT:
    ptr_name = "WAIT";
    break;
  case GUI_POINTER_HELP:
    ptr_name = "HELP";
    break;
  case GUI_POINTER_MENU:
    ptr_name = "MENU";
    break;
  case GUI_POINTER_PROGRESS:
    ptr_name = "PROGRESS";
    break;
  case GUI_POINTER_NO_DROP:
    ptr_name = "NO_DROP";
    break;
  case GUI_POINTER_NOT_ALLOWED:
    ptr_name = "NOT_ALLOWED";
    break;
  case GUI_POINTER_DEFAULT:
    ptr_name = "DEFAULT";
    break;
  default:
    break;
  }
  fprintf(stdout, "WINDOW SET_POINTER WIN %u POINTER %s\n", g->win_num, ptr_name);
}

static nserror
gui_window_set_url(struct gui_window *g, nsurl *url)
{
  fprintf(stdout, "WINDOW SET_URL WIN %u URL %s\n", g->win_num, nsurl_access(url));
  return NSERROR_OK;
}

static bool
gui_window_get_scroll(struct gui_window *g, int *sx, int *sy)
{
  fprintf(stdout, "WINDOW GET_SCROLL WIN %u X %d Y %d\n",
          g->win_num, g->scrollx, g->scrolly);
  *sx = g->scrollx;
  *sy = g->scrolly;
  return true;
}

static bool
gui_window_scroll_start(struct gui_window *g)
{
  fprintf(stdout, "WINDOW SCROLL_START WIN %u\n", g->win_num);
  g->scrollx = g->scrolly = 0;
  return true;
}

static void
gui_window_scroll_visible(struct gui_window *g, int x0, int y0,
                          int x1, int y1)
{
  fprintf(stdout, "WINDOW SCROLL_VISIBLE WIN %u X0 %d Y0 %d X1 %d Y1 %d\n",
          g->win_num, x0, y0, x1, y1);
}

static void
gui_window_place_caret(struct gui_window *g, int x, int y, int height,
		const struct rect *clip)
{
  fprintf(stdout, "WINDOW PLACE_CARET WIN %u X %d Y %d HEIGHT %d\n",
          g->win_num, x, y, height);
}

static void
gui_window_remove_caret(struct gui_window *g)
{
  fprintf(stdout, "WINDOW REMOVE_CARET WIN %u\n", g->win_num);
}

static bool
gui_window_drag_start(struct gui_window *g, gui_drag_type type,
                      const struct rect *rect)
{
  fprintf(stdout, "WINDOW SCROLL_START WIN %u TYPE %i\n", g->win_num, type);
  return false;
}

static nserror
gui_window_save_link(struct gui_window *g, nsurl *url, const char *title)
{
  fprintf(stdout, "WINDOW SAVE_LINK WIN %u URL %s TITLE %s\n",
          g->win_num, nsurl_access(url), title);
  return NSERROR_OK;
}



/**** Handlers ****/

static void
monkey_window_handle_new(int argc, char **argv)
{
  nsurl *url = NULL;
  nserror error = NSERROR_OK;

  if (argc > 3)
    return;

  if (argc == 3) {
    error = nsurl_create(argv[2], &url);
  }
  if (error == NSERROR_OK) {
    error = browser_window_create(BW_CREATE_HISTORY,
				  url,
				  NULL,
				  NULL,
				  NULL);
    if (url != NULL) {
      nsurl_unref(url);
    }
  }
  if (error != NSERROR_OK) {
    monkey_warn_user(messages_get_errorcode(error), 0);
  }
}

static void
monkey_window_handle_destroy(int argc, char **argv)
{
  struct gui_window *gw;
  uint32_t nr = atoi((argc > 2) ? argv[2] : "-1");
  
  gw = monkey_find_window_by_num(nr);
  
  if (gw == NULL) {
    fprintf(stdout, "ERROR WINDOW NUM BAD\n");
  } else {
    browser_window_destroy(gw->bw);
  }
}

static void
monkey_window_handle_go(int argc, char **argv)
{
  struct gui_window *gw;
  nsurl *url;
  nsurl *ref_url = NULL;
  nserror error;
  
  if (argc < 4 || argc > 5) {
    fprintf(stdout, "ERROR WINDOW GO ARGS BAD\n");
    return;
  }
  
  gw = monkey_find_window_by_num(atoi(argv[2]));
  
  if (gw == NULL) {
    fprintf(stdout, "ERROR WINDOW NUM BAD\n");
    return;
  }

  error = nsurl_create(argv[3], &url);
  if (error == NSERROR_OK) {
    if (argc == 5) {
      error = nsurl_create(argv[4], &ref_url);
    }

    if (error == NSERROR_OK) {
      error = browser_window_navigate(gw->bw,
				      url,
				      ref_url,
				      BW_NAVIGATE_HISTORY,
				      NULL,
				      NULL,
				      NULL);
      if (ref_url != NULL) {
	nsurl_unref(ref_url);
      }
    }
    nsurl_unref(url);
  }

  if (error != NSERROR_OK) {
    monkey_warn_user(messages_get_errorcode(error), 0);
  }
}

static void
monkey_window_handle_redraw(int argc, char **argv)
{
  struct gui_window *gw;
  struct rect clip;
  struct redraw_context ctx = {
    .interactive = true,
    .background_images = true,
    .plot = monkey_plotters
  };
  
  if (argc != 3 && argc != 7) {
    fprintf(stdout, "ERROR WINDOW REDRAW ARGS BAD\n");
    return;
  }

  gw = monkey_find_window_by_num(atoi(argv[2]));
  
  if (gw == NULL) {
    fprintf(stdout, "ERROR WINDOW NUM BAD\n");
    return;
  }
  
  clip.x0 = 0;
  clip.y0 = 0;
  clip.x1 = gw->width;
  clip.y1 = gw->height;
  
  if (argc == 7) {
    clip.x0 = atoi(argv[3]);
    clip.y0 = atoi(argv[4]);
    clip.x1 = atoi(argv[5]);
    clip.y1 = atoi(argv[6]);
  }
  
  LOG("Issue redraw");
  fprintf(stdout, "WINDOW REDRAW WIN %d START\n", atoi(argv[2]));
  browser_window_redraw(gw->bw, gw->scrollx, gw->scrolly, &clip, &ctx);  
  fprintf(stdout, "WINDOW REDRAW WIN %d STOP\n", atoi(argv[2]));
}

static void
monkey_window_handle_reload(int argc, char **argv)
{
  struct gui_window *gw;
  if (argc != 3 && argc != 4) {
    fprintf(stdout, "ERROR WINDOW RELOAD ARGS BAD\n");
  }
  
  gw = monkey_find_window_by_num(atoi(argv[2]));
  
  if (gw == NULL) {
    fprintf(stdout, "ERROR WINDOW NUM BAD\n");
  } else {
    browser_window_reload(gw->bw, argc == 4);
  }
}


void
monkey_window_handle_command(int argc, char **argv)
{
  if (argc == 1)
    return;
  
  if (strcmp(argv[1], "NEW") == 0) {
    monkey_window_handle_new(argc, argv);
  } else if (strcmp(argv[1], "DESTROY") == 0) {
    monkey_window_handle_destroy(argc, argv);
  } else if (strcmp(argv[1], "GO") == 0) {
    monkey_window_handle_go(argc, argv);
  } else if (strcmp(argv[1], "REDRAW") == 0) {
    monkey_window_handle_redraw(argc, argv);
  } else if (strcmp(argv[1], "RELOAD") == 0) {
    monkey_window_handle_reload(argc, argv);
  } else {
    fprintf(stdout, "ERROR WINDOW COMMAND UNKNOWN %s\n", argv[1]);
  }
  
}

static struct gui_window_table window_table = {
	.create = gui_window_create,
	.destroy = gui_window_destroy,
	.redraw = gui_window_redraw_window,
	.update = gui_window_update_box,
	.get_scroll = gui_window_get_scroll,
	.set_scroll = gui_window_set_scroll,
	.get_dimensions = gui_window_get_dimensions,
	.update_extent = gui_window_update_extent,
	.reformat = monkey_window_reformat,

	.set_title = gui_window_set_title,
	.set_url = gui_window_set_url,
	.set_icon = gui_window_set_icon,
	.set_status = gui_window_set_status,
	.set_pointer = gui_window_set_pointer,
	.place_caret = gui_window_place_caret,
	.remove_caret = gui_window_remove_caret,
	.drag_start = gui_window_drag_start,
	.save_link = gui_window_save_link,
	.scroll_visible = gui_window_scroll_visible,
	.scroll_start = gui_window_scroll_start,
	.new_content = gui_window_new_content,
	.start_throbber = gui_window_start_throbber,
	.stop_throbber = gui_window_stop_throbber,
};

struct gui_window_table *monkey_window_table = &window_table;