Reflow monkey code, it was annoying me too much

This commit is contained in:
Daniel Silverstone 2017-06-09 22:04:58 +01:00
parent 7272b59458
commit fe9104096f
12 changed files with 824 additions and 832 deletions

View File

@ -24,11 +24,11 @@
#include "monkey/401login.h" #include "monkey/401login.h"
typedef struct monkey401 { typedef struct monkey401 {
struct monkey401 *r_next, *r_prev; struct monkey401 *r_next, *r_prev;
uint32_t num; uint32_t num;
lwc_string *host; /* Ignore */ lwc_string *host; /* Ignore */
nserror (*cb)(bool,void*); nserror (*cb)(bool,void*);
void *pw; void *pw;
} monkey401_t; } monkey401_t;
static monkey401_t *m4_ring = NULL; static monkey401_t *m4_ring = NULL;
@ -37,19 +37,19 @@ static uint32_t m4_ctr = 0;
void gui_401login_open(nsurl *url, const char *realm, void gui_401login_open(nsurl *url, const char *realm,
nserror (*cb)(bool proceed, void *pw), void *cbpw) nserror (*cb)(bool proceed, void *pw), void *cbpw)
{ {
monkey401_t *m4t = calloc(sizeof(*m4t), 1); monkey401_t *m4t = calloc(sizeof(*m4t), 1);
if (m4t == NULL) { if (m4t == NULL) {
cb(false, cbpw); cb(false, cbpw);
return; return;
} }
m4t->cb = cb; m4t->cb = cb;
m4t->pw = cbpw; m4t->pw = cbpw;
m4t->num = m4_ctr++; m4t->num = m4_ctr++;
RING_INSERT(m4_ring, m4t); RING_INSERT(m4_ring, m4t);
fprintf(stdout, "401LOGIN OPEN M4 %u URL %s REALM %s\n", fprintf(stdout, "401LOGIN OPEN M4 %u URL %s REALM %s\n",
m4t->num, nsurl_access(url), realm); m4t->num, nsurl_access(url), realm);
} }

View File

@ -26,125 +26,125 @@
#include "monkey/bitmap.h" #include "monkey/bitmap.h"
struct bitmap { struct bitmap {
void *ptr; void *ptr;
size_t rowstride; size_t rowstride;
int width; int width;
int height; int height;
unsigned int state; unsigned int state;
}; };
static void *bitmap_create(int width, int height, unsigned int state) static void *bitmap_create(int width, int height, unsigned int state)
{ {
struct bitmap *ret = calloc(sizeof(*ret), 1); struct bitmap *ret = calloc(sizeof(*ret), 1);
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
ret->width = width; ret->width = width;
ret->height = height; ret->height = height;
ret->state = state; ret->state = state;
ret->ptr = calloc(width, height * 4); ret->ptr = calloc(width, height * 4);
if (ret->ptr == NULL) { if (ret->ptr == NULL) {
free(ret); free(ret);
return NULL; return NULL;
} }
return ret; return ret;
} }
static void bitmap_destroy(void *bitmap) static void bitmap_destroy(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
free(bmap->ptr); free(bmap->ptr);
free(bmap); free(bmap);
} }
static void bitmap_set_opaque(void *bitmap, bool opaque) static void bitmap_set_opaque(void *bitmap, bool opaque)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
if (opaque) if (opaque)
bmap->state |= (BITMAP_OPAQUE); bmap->state |= (BITMAP_OPAQUE);
else else
bmap->state &= ~(BITMAP_OPAQUE); bmap->state &= ~(BITMAP_OPAQUE);
} }
static bool bitmap_test_opaque(void *bitmap) static bool bitmap_test_opaque(void *bitmap)
{ {
return false; return false;
} }
static bool bitmap_get_opaque(void *bitmap) static bool bitmap_get_opaque(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
return (bmap->state & BITMAP_OPAQUE) == BITMAP_OPAQUE; return (bmap->state & BITMAP_OPAQUE) == BITMAP_OPAQUE;
} }
static unsigned char *bitmap_get_buffer(void *bitmap) static unsigned char *bitmap_get_buffer(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
return (unsigned char *)(bmap->ptr); return (unsigned char *)(bmap->ptr);
} }
static size_t bitmap_get_rowstride(void *bitmap) static size_t bitmap_get_rowstride(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
return bmap->width * 4; return bmap->width * 4;
} }
static size_t bitmap_get_bpp(void *bitmap) static size_t bitmap_get_bpp(void *bitmap)
{ {
/* OMG?! */ /* OMG?! */
return 4; return 4;
} }
static bool bitmap_save(void *bitmap, const char *path, unsigned flags) static bool bitmap_save(void *bitmap, const char *path, unsigned flags)
{ {
return true; return true;
} }
static void bitmap_modified(void *bitmap) static void bitmap_modified(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
bmap->state |= BITMAP_MODIFIED; bmap->state |= BITMAP_MODIFIED;
} }
static int bitmap_get_width(void *bitmap) static int bitmap_get_width(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
return bmap->width; return bmap->width;
} }
static int bitmap_get_height(void *bitmap) static int bitmap_get_height(void *bitmap)
{ {
struct bitmap *bmap = bitmap; struct bitmap *bmap = bitmap;
return bmap->height; return bmap->height;
} }
static nserror bitmap_render(struct bitmap *bitmap, static nserror bitmap_render(struct bitmap *bitmap,
struct hlcache_handle *content) struct hlcache_handle *content)
{ {
fprintf(stdout, "GENERIC BITMAP RENDER\n"); fprintf(stdout, "GENERIC BITMAP RENDER\n");
return NSERROR_OK; return NSERROR_OK;
} }
static struct gui_bitmap_table bitmap_table = { static struct gui_bitmap_table bitmap_table = {
.create = bitmap_create, .create = bitmap_create,
.destroy = bitmap_destroy, .destroy = bitmap_destroy,
.set_opaque = bitmap_set_opaque, .set_opaque = bitmap_set_opaque,
.get_opaque = bitmap_get_opaque, .get_opaque = bitmap_get_opaque,
.test_opaque = bitmap_test_opaque, .test_opaque = bitmap_test_opaque,
.get_buffer = bitmap_get_buffer, .get_buffer = bitmap_get_buffer,
.get_rowstride = bitmap_get_rowstride, .get_rowstride = bitmap_get_rowstride,
.get_width = bitmap_get_width, .get_width = bitmap_get_width,
.get_height = bitmap_get_height, .get_height = bitmap_get_height,
.get_bpp = bitmap_get_bpp, .get_bpp = bitmap_get_bpp,
.save = bitmap_save, .save = bitmap_save,
.modified = bitmap_modified, .modified = bitmap_modified,
.render = bitmap_render, .render = bitmap_render,
}; };
struct gui_bitmap_table *monkey_bitmap_table = &bitmap_table; struct gui_bitmap_table *monkey_bitmap_table = &bitmap_table;

View File

@ -42,71 +42,71 @@ static struct gui_window *gw_ring = NULL;
/* exported function documented in monkey/browser.h */ /* exported function documented in monkey/browser.h */
nserror monkey_warn_user(const char *warning, const char *detail) nserror monkey_warn_user(const char *warning, const char *detail)
{ {
fprintf(stderr, "WARN %s %s\n", warning, detail); fprintf(stderr, "WARN %s %s\n", warning, detail);
return NSERROR_OK; return NSERROR_OK;
} }
struct gui_window * struct gui_window *
monkey_find_window_by_num(uint32_t win_num) monkey_find_window_by_num(uint32_t win_num)
{ {
struct gui_window *ret = NULL; struct gui_window *ret = NULL;
RING_ITERATE_START(struct gui_window, gw_ring, c_ring) { RING_ITERATE_START(struct gui_window, gw_ring, c_ring) {
if (c_ring->win_num == win_num) { if (c_ring->win_num == win_num) {
ret = c_ring; ret = c_ring;
RING_ITERATE_STOP(gw_ring, c_ring); RING_ITERATE_STOP(gw_ring, c_ring);
} }
} RING_ITERATE_END(gw_ring, c_ring); } RING_ITERATE_END(gw_ring, c_ring);
return ret; return ret;
} }
void void
monkey_kill_browser_windows(void) monkey_kill_browser_windows(void)
{ {
while (gw_ring != NULL) { while (gw_ring != NULL) {
browser_window_destroy(gw_ring->bw); browser_window_destroy(gw_ring->bw);
} }
} }
static struct gui_window * static struct gui_window *
gui_window_create(struct browser_window *bw, gui_window_create(struct browser_window *bw,
struct gui_window *existing, struct gui_window *existing,
gui_window_create_flags flags) gui_window_create_flags flags)
{ {
struct gui_window *ret = calloc(sizeof(*ret), 1); struct gui_window *ret = calloc(sizeof(*ret), 1);
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
ret->win_num = win_ctr++; ret->win_num = win_ctr++;
ret->bw = bw; ret->bw = bw;
ret->width = 800; ret->width = 800;
ret->height = 600; ret->height = 600;
fprintf(stdout, "WINDOW NEW WIN %u FOR %p EXISTING %p NEWTAB %s CLONE %s\n", 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", ret->win_num, bw, existing, flags & GW_CREATE_TAB ? "TRUE" : "FALSE",
flags & GW_CREATE_CLONE ? "TRUE" : "FALSE"); flags & GW_CREATE_CLONE ? "TRUE" : "FALSE");
fprintf(stdout, "WINDOW SIZE WIN %u WIDTH %d HEIGHT %d\n", fprintf(stdout, "WINDOW SIZE WIN %u WIDTH %d HEIGHT %d\n",
ret->win_num, ret->width, ret->height); ret->win_num, ret->width, ret->height);
RING_INSERT(gw_ring, ret); RING_INSERT(gw_ring, ret);
return ret; return ret;
} }
static void static void
gui_window_destroy(struct gui_window *g) gui_window_destroy(struct gui_window *g)
{ {
fprintf(stdout, "WINDOW DESTROY WIN %u\n", g->win_num); fprintf(stdout, "WINDOW DESTROY WIN %u\n", g->win_num);
RING_REMOVE(gw_ring, g); RING_REMOVE(gw_ring, g);
free(g); free(g);
} }
static void static void
gui_window_set_title(struct gui_window *g, const char *title) gui_window_set_title(struct gui_window *g, const char *title)
{ {
fprintf(stdout, "WINDOW TITLE WIN %u STR %s\n", g->win_num, title); fprintf(stdout, "WINDOW TITLE WIN %u STR %s\n", g->win_num, title);
} }
/** /**
@ -122,36 +122,36 @@ static nserror
gui_window_get_dimensions(struct gui_window *g, int *width, int *height, gui_window_get_dimensions(struct gui_window *g, int *width, int *height,
bool scaled) bool scaled)
{ {
fprintf(stdout, "WINDOW GET_DIMENSIONS WIN %u WIDTH %d HEIGHT %d\n", fprintf(stdout, "WINDOW GET_DIMENSIONS WIN %u WIDTH %d HEIGHT %d\n",
g->win_num, g->width, g->height); g->win_num, g->width, g->height);
*width = g->width; *width = g->width;
*height = g->height; *height = g->height;
return NSERROR_OK; return NSERROR_OK;
} }
static void static void
gui_window_new_content(struct gui_window *g) gui_window_new_content(struct gui_window *g)
{ {
fprintf(stdout, "WINDOW NEW_CONTENT WIN %u\n", g->win_num); fprintf(stdout, "WINDOW NEW_CONTENT WIN %u\n", g->win_num);
} }
static void static void
gui_window_set_icon(struct gui_window *g, struct hlcache_handle *icon) gui_window_set_icon(struct gui_window *g, struct hlcache_handle *icon)
{ {
fprintf(stdout, "WINDOW NEW_ICON WIN %u\n", g->win_num); fprintf(stdout, "WINDOW NEW_ICON WIN %u\n", g->win_num);
} }
static void static void
gui_window_start_throbber(struct gui_window *g) gui_window_start_throbber(struct gui_window *g)
{ {
fprintf(stdout, "WINDOW START_THROBBER WIN %u\n", g->win_num); fprintf(stdout, "WINDOW START_THROBBER WIN %u\n", g->win_num);
} }
static void static void
gui_window_stop_throbber(struct gui_window *g) gui_window_stop_throbber(struct gui_window *g)
{ {
fprintf(stdout, "WINDOW STOP_THROBBER WIN %u\n", g->win_num); fprintf(stdout, "WINDOW STOP_THROBBER WIN %u\n", g->win_num);
} }
@ -168,12 +168,12 @@ gui_window_stop_throbber(struct gui_window *g)
static nserror static nserror
gui_window_set_scroll(struct gui_window *gw, const struct rect *rect) gui_window_set_scroll(struct gui_window *gw, const struct rect *rect)
{ {
gw->scrollx = rect->x0; gw->scrollx = rect->x0;
gw->scrolly = rect->y0; gw->scrolly = rect->y0;
fprintf(stdout, "WINDOW SET_SCROLL WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", fprintf(stdout, "WINDOW SET_SCROLL WIN %u X0 %d Y0 %d X1 %d Y1 %d\n",
gw->win_num, rect->x0, rect->y0, rect->x1, rect->y1); gw->win_num, rect->x0, rect->y0, rect->x1, rect->y1);
return NSERROR_OK; return NSERROR_OK;
} }
@ -204,144 +204,144 @@ monkey_window_invalidate_area(struct gui_window *gw, const struct rect *rect)
static void static void
gui_window_update_extent(struct gui_window *g) gui_window_update_extent(struct gui_window *g)
{ {
int width, height; int width, height;
if (browser_window_get_extents(g->bw, false, &width, &height) != NSERROR_OK) if (browser_window_get_extents(g->bw, false, &width, &height) != NSERROR_OK)
return; return;
fprintf(stdout, "WINDOW UPDATE_EXTENT WIN %u WIDTH %d HEIGHT %d\n", fprintf(stdout, "WINDOW UPDATE_EXTENT WIN %u WIDTH %d HEIGHT %d\n",
g->win_num, width, height); g->win_num, width, height);
} }
static void static void
gui_window_set_status(struct gui_window *g, const char *text) 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); fprintf(stdout, "WINDOW SET_STATUS WIN %u STR %s\n", g->win_num, text);
} }
static void static void
gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape) gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape)
{ {
const char *ptr_name = "UNKNOWN"; const char *ptr_name = "UNKNOWN";
switch (shape) { switch (shape) {
case GUI_POINTER_POINT: case GUI_POINTER_POINT:
ptr_name = "POINT"; ptr_name = "POINT";
break; break;
case GUI_POINTER_CARET: case GUI_POINTER_CARET:
ptr_name = "CARET"; ptr_name = "CARET";
break; break;
case GUI_POINTER_UP: case GUI_POINTER_UP:
ptr_name = "UP"; ptr_name = "UP";
break; break;
case GUI_POINTER_DOWN: case GUI_POINTER_DOWN:
ptr_name = "DOWN"; ptr_name = "DOWN";
break; break;
case GUI_POINTER_LEFT: case GUI_POINTER_LEFT:
ptr_name = "LEFT"; ptr_name = "LEFT";
break; break;
case GUI_POINTER_RIGHT: case GUI_POINTER_RIGHT:
ptr_name = "RIGHT"; ptr_name = "RIGHT";
break; break;
case GUI_POINTER_LD: case GUI_POINTER_LD:
ptr_name = "LD"; ptr_name = "LD";
break; break;
case GUI_POINTER_RD: case GUI_POINTER_RD:
ptr_name = "RD"; ptr_name = "RD";
break; break;
case GUI_POINTER_LU: case GUI_POINTER_LU:
ptr_name = "LU"; ptr_name = "LU";
break; break;
case GUI_POINTER_RU: case GUI_POINTER_RU:
ptr_name = "RU"; ptr_name = "RU";
break; break;
case GUI_POINTER_CROSS: case GUI_POINTER_CROSS:
ptr_name = "CROSS"; ptr_name = "CROSS";
break; break;
case GUI_POINTER_MOVE: case GUI_POINTER_MOVE:
ptr_name = "MOVE"; ptr_name = "MOVE";
break; break;
case GUI_POINTER_WAIT: case GUI_POINTER_WAIT:
ptr_name = "WAIT"; ptr_name = "WAIT";
break; break;
case GUI_POINTER_HELP: case GUI_POINTER_HELP:
ptr_name = "HELP"; ptr_name = "HELP";
break; break;
case GUI_POINTER_MENU: case GUI_POINTER_MENU:
ptr_name = "MENU"; ptr_name = "MENU";
break; break;
case GUI_POINTER_PROGRESS: case GUI_POINTER_PROGRESS:
ptr_name = "PROGRESS"; ptr_name = "PROGRESS";
break; break;
case GUI_POINTER_NO_DROP: case GUI_POINTER_NO_DROP:
ptr_name = "NO_DROP"; ptr_name = "NO_DROP";
break; break;
case GUI_POINTER_NOT_ALLOWED: case GUI_POINTER_NOT_ALLOWED:
ptr_name = "NOT_ALLOWED"; ptr_name = "NOT_ALLOWED";
break; break;
case GUI_POINTER_DEFAULT: case GUI_POINTER_DEFAULT:
ptr_name = "DEFAULT"; ptr_name = "DEFAULT";
break; break;
default: default:
break; break;
} }
fprintf(stdout, "WINDOW SET_POINTER WIN %u POINTER %s\n", g->win_num, ptr_name); fprintf(stdout, "WINDOW SET_POINTER WIN %u POINTER %s\n", g->win_num, ptr_name);
} }
static nserror static nserror
gui_window_set_url(struct gui_window *g, nsurl *url) 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)); fprintf(stdout, "WINDOW SET_URL WIN %u URL %s\n", g->win_num, nsurl_access(url));
return NSERROR_OK; return NSERROR_OK;
} }
static bool static bool
gui_window_get_scroll(struct gui_window *g, int *sx, int *sy) gui_window_get_scroll(struct gui_window *g, int *sx, int *sy)
{ {
fprintf(stdout, "WINDOW GET_SCROLL WIN %u X %d Y %d\n", fprintf(stdout, "WINDOW GET_SCROLL WIN %u X %d Y %d\n",
g->win_num, g->scrollx, g->scrolly); g->win_num, g->scrollx, g->scrolly);
*sx = g->scrollx; *sx = g->scrollx;
*sy = g->scrolly; *sy = g->scrolly;
return true; return true;
} }
static bool static bool
gui_window_scroll_start(struct gui_window *g) gui_window_scroll_start(struct gui_window *g)
{ {
fprintf(stdout, "WINDOW SCROLL_START WIN %u\n", g->win_num); fprintf(stdout, "WINDOW SCROLL_START WIN %u\n", g->win_num);
g->scrollx = g->scrolly = 0; g->scrollx = g->scrolly = 0;
return true; return true;
} }
static void static void
gui_window_place_caret(struct gui_window *g, int x, int y, int height, gui_window_place_caret(struct gui_window *g, int x, int y, int height,
const struct rect *clip) const struct rect *clip)
{ {
fprintf(stdout, "WINDOW PLACE_CARET WIN %u X %d Y %d HEIGHT %d\n", fprintf(stdout, "WINDOW PLACE_CARET WIN %u X %d Y %d HEIGHT %d\n",
g->win_num, x, y, height); g->win_num, x, y, height);
} }
static void static void
gui_window_remove_caret(struct gui_window *g) gui_window_remove_caret(struct gui_window *g)
{ {
fprintf(stdout, "WINDOW REMOVE_CARET WIN %u\n", g->win_num); fprintf(stdout, "WINDOW REMOVE_CARET WIN %u\n", g->win_num);
} }
static bool static bool
gui_window_drag_start(struct gui_window *g, gui_drag_type type, gui_window_drag_start(struct gui_window *g, gui_drag_type type,
const struct rect *rect) const struct rect *rect)
{ {
fprintf(stdout, "WINDOW SCROLL_START WIN %u TYPE %i\n", g->win_num, type); fprintf(stdout, "WINDOW SCROLL_START WIN %u TYPE %i\n", g->win_num, type);
return false; return false;
} }
static nserror static nserror
gui_window_save_link(struct gui_window *g, nsurl *url, const char *title) 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", fprintf(stdout, "WINDOW SAVE_LINK WIN %u URL %s TITLE %s\n",
g->win_num, nsurl_access(url), title); g->win_num, nsurl_access(url), title);
return NSERROR_OK; return NSERROR_OK;
} }
@ -351,169 +351,169 @@ gui_window_save_link(struct gui_window *g, nsurl *url, const char *title)
static void static void
monkey_window_handle_new(int argc, char **argv) monkey_window_handle_new(int argc, char **argv)
{ {
nsurl *url = NULL; nsurl *url = NULL;
nserror error = NSERROR_OK; nserror error = NSERROR_OK;
if (argc > 3) if (argc > 3)
return; return;
if (argc == 3) { if (argc == 3) {
error = nsurl_create(argv[2], &url); error = nsurl_create(argv[2], &url);
} }
if (error == NSERROR_OK) { if (error == NSERROR_OK) {
error = browser_window_create(BW_CREATE_HISTORY, error = browser_window_create(BW_CREATE_HISTORY,
url, url,
NULL, NULL,
NULL, NULL,
NULL); NULL);
if (url != NULL) { if (url != NULL) {
nsurl_unref(url); nsurl_unref(url);
} }
} }
if (error != NSERROR_OK) { if (error != NSERROR_OK) {
monkey_warn_user(messages_get_errorcode(error), 0); monkey_warn_user(messages_get_errorcode(error), 0);
} }
} }
static void static void
monkey_window_handle_destroy(int argc, char **argv) monkey_window_handle_destroy(int argc, char **argv)
{ {
struct gui_window *gw; struct gui_window *gw;
uint32_t nr = atoi((argc > 2) ? argv[2] : "-1"); uint32_t nr = atoi((argc > 2) ? argv[2] : "-1");
gw = monkey_find_window_by_num(nr); gw = monkey_find_window_by_num(nr);
if (gw == NULL) { if (gw == NULL) {
fprintf(stdout, "ERROR WINDOW NUM BAD\n"); fprintf(stdout, "ERROR WINDOW NUM BAD\n");
} else { } else {
browser_window_destroy(gw->bw); browser_window_destroy(gw->bw);
} }
} }
static void static void
monkey_window_handle_go(int argc, char **argv) monkey_window_handle_go(int argc, char **argv)
{ {
struct gui_window *gw; struct gui_window *gw;
nsurl *url; nsurl *url;
nsurl *ref_url = NULL; nsurl *ref_url = NULL;
nserror error; nserror error;
if (argc < 4 || argc > 5) { if (argc < 4 || argc > 5) {
fprintf(stdout, "ERROR WINDOW GO ARGS BAD\n"); fprintf(stdout, "ERROR WINDOW GO ARGS BAD\n");
return; return;
} }
gw = monkey_find_window_by_num(atoi(argv[2])); gw = monkey_find_window_by_num(atoi(argv[2]));
if (gw == NULL) { if (gw == NULL) {
fprintf(stdout, "ERROR WINDOW NUM BAD\n"); fprintf(stdout, "ERROR WINDOW NUM BAD\n");
return; return;
} }
error = nsurl_create(argv[3], &url); error = nsurl_create(argv[3], &url);
if (error == NSERROR_OK) { if (error == NSERROR_OK) {
if (argc == 5) { if (argc == 5) {
error = nsurl_create(argv[4], &ref_url); error = nsurl_create(argv[4], &ref_url);
} }
if (error == NSERROR_OK) { if (error == NSERROR_OK) {
error = browser_window_navigate(gw->bw, error = browser_window_navigate(gw->bw,
url, url,
ref_url, ref_url,
BW_NAVIGATE_HISTORY, BW_NAVIGATE_HISTORY,
NULL, NULL,
NULL, NULL,
NULL); NULL);
if (ref_url != NULL) { if (ref_url != NULL) {
nsurl_unref(ref_url); nsurl_unref(ref_url);
} }
} }
nsurl_unref(url); nsurl_unref(url);
} }
if (error != NSERROR_OK) { if (error != NSERROR_OK) {
monkey_warn_user(messages_get_errorcode(error), 0); monkey_warn_user(messages_get_errorcode(error), 0);
} }
} }
static void static void
monkey_window_handle_redraw(int argc, char **argv) monkey_window_handle_redraw(int argc, char **argv)
{ {
struct gui_window *gw; struct gui_window *gw;
struct rect clip; struct rect clip;
struct redraw_context ctx = { struct redraw_context ctx = {
.interactive = true, .interactive = true,
.background_images = true, .background_images = true,
.plot = monkey_plotters .plot = monkey_plotters
}; };
if (argc != 3 && argc != 7) { if (argc != 3 && argc != 7) {
fprintf(stdout, "ERROR WINDOW REDRAW ARGS BAD\n"); fprintf(stdout, "ERROR WINDOW REDRAW ARGS BAD\n");
return; return;
} }
gw = monkey_find_window_by_num(atoi(argv[2])); gw = monkey_find_window_by_num(atoi(argv[2]));
if (gw == NULL) { if (gw == NULL) {
fprintf(stdout, "ERROR WINDOW NUM BAD\n"); fprintf(stdout, "ERROR WINDOW NUM BAD\n");
return; return;
} }
clip.x0 = 0; clip.x0 = 0;
clip.y0 = 0; clip.y0 = 0;
clip.x1 = gw->width; clip.x1 = gw->width;
clip.y1 = gw->height; clip.y1 = gw->height;
if (argc == 7) { if (argc == 7) {
clip.x0 = atoi(argv[3]); clip.x0 = atoi(argv[3]);
clip.y0 = atoi(argv[4]); clip.y0 = atoi(argv[4]);
clip.x1 = atoi(argv[5]); clip.x1 = atoi(argv[5]);
clip.y1 = atoi(argv[6]); clip.y1 = atoi(argv[6]);
} }
LOG("Issue redraw"); LOG("Issue redraw");
fprintf(stdout, "WINDOW REDRAW WIN %d START\n", atoi(argv[2])); fprintf(stdout, "WINDOW REDRAW WIN %d START\n", atoi(argv[2]));
browser_window_redraw(gw->bw, gw->scrollx, gw->scrolly, &clip, &ctx); browser_window_redraw(gw->bw, gw->scrollx, gw->scrolly, &clip, &ctx);
fprintf(stdout, "WINDOW REDRAW WIN %d STOP\n", atoi(argv[2])); fprintf(stdout, "WINDOW REDRAW WIN %d STOP\n", atoi(argv[2]));
} }
static void static void
monkey_window_handle_reload(int argc, char **argv) monkey_window_handle_reload(int argc, char **argv)
{ {
struct gui_window *gw; struct gui_window *gw;
if (argc != 3 && argc != 4) { if (argc != 3 && argc != 4) {
fprintf(stdout, "ERROR WINDOW RELOAD ARGS BAD\n"); fprintf(stdout, "ERROR WINDOW RELOAD ARGS BAD\n");
} }
gw = monkey_find_window_by_num(atoi(argv[2])); gw = monkey_find_window_by_num(atoi(argv[2]));
if (gw == NULL) { if (gw == NULL) {
fprintf(stdout, "ERROR WINDOW NUM BAD\n"); fprintf(stdout, "ERROR WINDOW NUM BAD\n");
} else { } else {
browser_window_reload(gw->bw, argc == 4); browser_window_reload(gw->bw, argc == 4);
} }
} }
void void
monkey_window_handle_command(int argc, char **argv) monkey_window_handle_command(int argc, char **argv)
{ {
if (argc == 1) if (argc == 1)
return; return;
if (strcmp(argv[1], "NEW") == 0) { if (strcmp(argv[1], "NEW") == 0) {
monkey_window_handle_new(argc, argv); monkey_window_handle_new(argc, argv);
} else if (strcmp(argv[1], "DESTROY") == 0) { } else if (strcmp(argv[1], "DESTROY") == 0) {
monkey_window_handle_destroy(argc, argv); monkey_window_handle_destroy(argc, argv);
} else if (strcmp(argv[1], "GO") == 0) { } else if (strcmp(argv[1], "GO") == 0) {
monkey_window_handle_go(argc, argv); monkey_window_handle_go(argc, argv);
} else if (strcmp(argv[1], "REDRAW") == 0) { } else if (strcmp(argv[1], "REDRAW") == 0) {
monkey_window_handle_redraw(argc, argv); monkey_window_handle_redraw(argc, argv);
} else if (strcmp(argv[1], "RELOAD") == 0) { } else if (strcmp(argv[1], "RELOAD") == 0) {
monkey_window_handle_reload(argc, argv); monkey_window_handle_reload(argc, argv);
} else { } else {
fprintf(stdout, "ERROR WINDOW COMMAND UNKNOWN %s\n", argv[1]); fprintf(stdout, "ERROR WINDOW COMMAND UNKNOWN %s\n", argv[1]);
} }
} }

View File

@ -25,16 +25,16 @@ extern struct gui_window_table *monkey_window_table;
extern struct gui_download_table *monkey_download_table; extern struct gui_download_table *monkey_download_table;
struct gui_window { struct gui_window {
struct gui_window *r_next; struct gui_window *r_next;
struct gui_window *r_prev; struct gui_window *r_prev;
uint32_t win_num; uint32_t win_num;
struct browser_window *bw; struct browser_window *bw;
int width, height; int width, height;
int scrollx, scrolly; int scrollx, scrolly;
char *host; /* Ignore this, it's in case RING*() gets debugging for fetchers */ char *host; /* Ignore this, it's in case RING*() gets debugging for fetchers */
}; };

View File

@ -25,11 +25,11 @@
#include "monkey/cert.h" #include "monkey/cert.h"
typedef struct monkey_cert { typedef struct monkey_cert {
struct monkey_cert *r_next, *r_prev; struct monkey_cert *r_next, *r_prev;
uint32_t num; uint32_t num;
char *host; /* Ignore */ char *host; /* Ignore */
nserror (*cb)(bool,void*); nserror (*cb)(bool,void*);
void *pw; void *pw;
} monkey_cert_t; } monkey_cert_t;
static monkey_cert_t *cert_ring = NULL; static monkey_cert_t *cert_ring = NULL;
@ -40,20 +40,20 @@ gui_cert_verify(nsurl *url, const struct ssl_cert_info *certs,
unsigned long num, nserror (*cb)(bool proceed, void *pw), unsigned long num, nserror (*cb)(bool proceed, void *pw),
void *cbpw) void *cbpw)
{ {
monkey_cert_t *m4t = calloc(sizeof(*m4t), 1); monkey_cert_t *m4t = calloc(sizeof(*m4t), 1);
if (m4t == NULL) { if (m4t == NULL) {
return NSERROR_NOMEM; return NSERROR_NOMEM;
} }
m4t->cb = cb; m4t->cb = cb;
m4t->pw = cbpw; m4t->pw = cbpw;
m4t->num = cert_ctr++; m4t->num = cert_ctr++;
RING_INSERT(cert_ring, m4t); RING_INSERT(cert_ring, m4t);
fprintf(stdout, "SSLCERT VERIFY CERT %u URL %s\n", fprintf(stdout, "SSLCERT VERIFY CERT %u URL %s\n",
m4t->num, nsurl_access(url)); m4t->num, nsurl_access(url));
return NSERROR_OK; return NSERROR_OK;
} }

View File

@ -28,9 +28,9 @@
#include "monkey/dispatch.h" #include "monkey/dispatch.h"
typedef struct cmdhandler { typedef struct cmdhandler {
struct cmdhandler *r_next, *r_prev; struct cmdhandler *r_next, *r_prev;
const char *cmd; const char *cmd;
handle_command_fn fn; handle_command_fn fn;
} monkey_cmdhandler_t; } monkey_cmdhandler_t;
static monkey_cmdhandler_t *handler_ring = NULL; static monkey_cmdhandler_t *handler_ring = NULL;
@ -38,68 +38,68 @@ static monkey_cmdhandler_t *handler_ring = NULL;
nserror nserror
monkey_register_handler(const char *cmd, handle_command_fn fn) monkey_register_handler(const char *cmd, handle_command_fn fn)
{ {
monkey_cmdhandler_t *ret = calloc(sizeof(*ret), 1); monkey_cmdhandler_t *ret = calloc(sizeof(*ret), 1);
if (ret == NULL) { if (ret == NULL) {
LOG("Unable to allocate handler"); LOG("Unable to allocate handler");
return NSERROR_NOMEM; return NSERROR_NOMEM;
} }
ret->cmd = strdup(cmd); ret->cmd = strdup(cmd);
ret->fn = fn; ret->fn = fn;
RING_INSERT(handler_ring, ret); RING_INSERT(handler_ring, ret);
return NSERROR_OK; return NSERROR_OK;
} }
void void
monkey_process_command(void) monkey_process_command(void)
{ {
char buffer[PATH_MAX]; char buffer[PATH_MAX];
int argc = 0; int argc = 0;
char **argv = NULL; char **argv = NULL;
char *p, *r = NULL; char *p, *r = NULL;
handle_command_fn fn = NULL; handle_command_fn fn = NULL;
char **nargv; char **nargv;
if (fgets(buffer, PATH_MAX, stdin) == NULL) { if (fgets(buffer, PATH_MAX, stdin) == NULL) {
/* end of input or read error so issue QUIT */ /* end of input or read error so issue QUIT */
sprintf(buffer, "QUIT\n"); sprintf(buffer, "QUIT\n");
} }
/* remove newline */ /* remove newline */
buffer[strlen(buffer) - 1] = '\0'; buffer[strlen(buffer) - 1] = '\0';
argv = malloc(sizeof *argv); argv = malloc(sizeof *argv);
if (argv == NULL) { if (argv == NULL) {
return; return;
} }
argc = 1; argc = 1;
*argv = buffer; *argv = buffer;
for (p = r = buffer; *p != '\0'; p++) { for (p = r = buffer; *p != '\0'; p++) {
if (*p == ' ') { if (*p == ' ') {
nargv = realloc(argv, sizeof(*argv) * (argc + 1)); nargv = realloc(argv, sizeof(*argv) * (argc + 1));
if (nargv == NULL) { if (nargv == NULL) {
/* reallocation of argument vector failed, try using what is /* reallocation of argument vector failed, try using what is
* already processed. * already processed.
*/ */
break; break;
} else { } else {
argv = nargv; argv = nargv;
} }
argv[argc++] = r = p + 1; argv[argc++] = r = p + 1;
*p = '\0'; *p = '\0';
} }
} }
RING_ITERATE_START(monkey_cmdhandler_t, handler_ring, handler) { RING_ITERATE_START(monkey_cmdhandler_t, handler_ring, handler) {
if (strcmp(argv[0], handler->cmd) == 0) { if (strcmp(argv[0], handler->cmd) == 0) {
fn = handler->fn; fn = handler->fn;
RING_ITERATE_STOP(handler_ring, handler); RING_ITERATE_STOP(handler_ring, handler);
} }
} RING_ITERATE_END(handler_ring, handler); } RING_ITERATE_END(handler_ring, handler);
if (fn != NULL) { if (fn != NULL) {
fn(argc, argv); fn(argc, argv);
} }
free(argv); free(argv);
} }

View File

@ -30,11 +30,11 @@
static uint32_t dwin_ctr = 0; static uint32_t dwin_ctr = 0;
struct gui_download_window { struct gui_download_window {
struct gui_download_window *r_next; struct gui_download_window *r_next;
struct gui_download_window *r_prev; struct gui_download_window *r_prev;
struct gui_window *g; struct gui_window *g;
uint32_t dwin_num; uint32_t dwin_num;
char *host; /* ignore */ char *host; /* ignore */
}; };
static struct gui_download_window *dw_ring = NULL; static struct gui_download_window *dw_ring = NULL;
@ -43,44 +43,44 @@ static struct gui_download_window *
gui_download_window_create(download_context *ctx, gui_download_window_create(download_context *ctx,
struct gui_window *parent) struct gui_window *parent)
{ {
struct gui_download_window *ret = calloc(sizeof(*ret), 1); struct gui_download_window *ret = calloc(sizeof(*ret), 1);
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
ret->g = parent; ret->g = parent;
ret->dwin_num = dwin_ctr++; ret->dwin_num = dwin_ctr++;
RING_INSERT(dw_ring, ret); RING_INSERT(dw_ring, ret);
fprintf(stdout, "DOWNLOAD_WINDOW CREATE DWIN %u WIN %u\n", fprintf(stdout, "DOWNLOAD_WINDOW CREATE DWIN %u WIN %u\n",
ret->dwin_num, parent->win_num); ret->dwin_num, parent->win_num);
return ret; return ret;
} }
static nserror static nserror
gui_download_window_data(struct gui_download_window *dw, gui_download_window_data(struct gui_download_window *dw,
const char *data, unsigned int size) const char *data, unsigned int size)
{ {
fprintf(stdout, "DOWNLOAD_WINDOW DATA DWIN %u SIZE %u DATA %s\n", fprintf(stdout, "DOWNLOAD_WINDOW DATA DWIN %u SIZE %u DATA %s\n",
dw->dwin_num, size, data); dw->dwin_num, size, data);
return NSERROR_OK; return NSERROR_OK;
} }
static void static void
gui_download_window_error(struct gui_download_window *dw, gui_download_window_error(struct gui_download_window *dw,
const char *error_msg) const char *error_msg)
{ {
fprintf(stdout, "DOWNLOAD_WINDOW ERROR DWIN %u ERROR %s\n", fprintf(stdout, "DOWNLOAD_WINDOW ERROR DWIN %u ERROR %s\n",
dw->dwin_num, error_msg); dw->dwin_num, error_msg);
} }
static void static void
gui_download_window_done(struct gui_download_window *dw) gui_download_window_done(struct gui_download_window *dw)
{ {
fprintf(stdout, "DOWNLOAD_WINDOW DONE DWIN %u\n", fprintf(stdout, "DOWNLOAD_WINDOW DONE DWIN %u\n",
dw->dwin_num); dw->dwin_num);
RING_REMOVE(dw_ring, dw); RING_REMOVE(dw_ring, dw);
free(dw); free(dw);
} }
static struct gui_download_table download_table = { static struct gui_download_table download_table = {

View File

@ -36,18 +36,18 @@ extern char **respaths;
static nsurl *gui_get_resource_url(const char *path) static nsurl *gui_get_resource_url(const char *path)
{ {
char buf[PATH_MAX]; char buf[PATH_MAX];
nsurl *url = NULL; nsurl *url = NULL;
netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url); netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url);
return url; return url;
} }
static struct gui_fetch_table fetch_table = { static struct gui_fetch_table fetch_table = {
.filetype = monkey_fetch_filetype, .filetype = monkey_fetch_filetype,
.get_resource_url = gui_get_resource_url, .get_resource_url = gui_get_resource_url,
}; };
struct gui_fetch_table *monkey_fetch_table = &fetch_table; struct gui_fetch_table *monkey_fetch_table = &fetch_table;

View File

@ -30,11 +30,11 @@
#include "monkey/layout.h" #include "monkey/layout.h"
static nserror nsfont_width(const plot_font_style_t *fstyle, static nserror nsfont_width(const plot_font_style_t *fstyle,
const char *string, size_t length, const char *string, size_t length,
int *width) int *width)
{ {
*width = (fstyle->size * utf8_bounded_length(string, length)) / FONT_SIZE_SCALE; *width = (fstyle->size * utf8_bounded_length(string, length)) / FONT_SIZE_SCALE;
return NSERROR_OK; return NSERROR_OK;
} }
/** /**
@ -50,14 +50,14 @@ static nserror nsfont_width(const plot_font_style_t *fstyle,
*/ */
static nserror nsfont_position_in_string(const plot_font_style_t *fstyle, static nserror nsfont_position_in_string(const plot_font_style_t *fstyle,
const char *string, size_t length, const char *string, size_t length,
int x, size_t *char_offset, int *actual_x) int x, size_t *char_offset, int *actual_x)
{ {
*char_offset = x / (fstyle->size / FONT_SIZE_SCALE); *char_offset = x / (fstyle->size / FONT_SIZE_SCALE);
if (*char_offset > length) if (*char_offset > length)
*char_offset = length; *char_offset = length;
*actual_x = *char_offset * (fstyle->size / FONT_SIZE_SCALE); *actual_x = *char_offset * (fstyle->size / FONT_SIZE_SCALE);
return NSERROR_OK; return NSERROR_OK;
} }
@ -85,27 +85,27 @@ static nserror nsfont_position_in_string(const plot_font_style_t *fstyle,
*/ */
static nserror nsfont_split(const plot_font_style_t *fstyle, static nserror nsfont_split(const plot_font_style_t *fstyle,
const char *string, size_t length, const char *string, size_t length,
int x, size_t *char_offset, int *actual_x) int x, size_t *char_offset, int *actual_x)
{ {
int c_off = *char_offset = x / (fstyle->size / FONT_SIZE_SCALE); int c_off = *char_offset = x / (fstyle->size / FONT_SIZE_SCALE);
if (*char_offset > length) { if (*char_offset > length) {
*char_offset = length; *char_offset = length;
} else { } else {
while (*char_offset > 0) { while (*char_offset > 0) {
if (string[*char_offset] == ' ') if (string[*char_offset] == ' ')
break; break;
(*char_offset)--; (*char_offset)--;
} }
if (*char_offset == 0) { if (*char_offset == 0) {
*char_offset = c_off; *char_offset = c_off;
while (*char_offset < length && string[*char_offset] != ' ') { while (*char_offset < length && string[*char_offset] != ' ') {
(*char_offset)++; (*char_offset)++;
} }
} }
} }
*actual_x = *char_offset * (fstyle->size / FONT_SIZE_SCALE); *actual_x = *char_offset * (fstyle->size / FONT_SIZE_SCALE);
return NSERROR_OK; return NSERROR_OK;
} }
static struct gui_layout_table layout_table = { static struct gui_layout_table layout_table = {

View File

@ -66,8 +66,8 @@ static bool monkey_done = false;
*/ */
static void die(const char * const error) static void die(const char * const error)
{ {
fprintf(stderr, "DIE %s\n", error); fprintf(stderr, "DIE %s\n", error);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/** obtain language from environment /** obtain language from environment
@ -78,29 +78,29 @@ static void die(const char * const error)
*/ */
static const char *get_language(void) static const char *get_language(void)
{ {
const char *lang; const char *lang;
lang = getenv("LANGUAGE"); lang = getenv("LANGUAGE");
if ((lang != NULL) && (lang[0] != '\0')) { if ((lang != NULL) && (lang[0] != '\0')) {
return lang; return lang;
} }
lang = getenv("LC_ALL"); lang = getenv("LC_ALL");
if ((lang != NULL) && (lang[0] != '\0')) { if ((lang != NULL) && (lang[0] != '\0')) {
return lang; return lang;
} }
lang = getenv("LC_MESSAGES"); lang = getenv("LC_MESSAGES");
if ((lang != NULL) && (lang[0] != '\0')) { if ((lang != NULL) && (lang[0] != '\0')) {
return lang; return lang;
} }
lang = getenv("LANG"); lang = getenv("LANG");
if ((lang != NULL) && (lang[0] != '\0')) { if ((lang != NULL) && (lang[0] != '\0')) {
return lang; return lang;
} }
return NULL; return NULL;
} }
@ -120,92 +120,92 @@ static const char *get_language(void)
*/ */
static const char * const *get_languagev(void) static const char * const *get_languagev(void)
{ {
static const char *langv[LANGV_SIZE]; static const char *langv[LANGV_SIZE];
int langidx = 0; /* index of next entry in vector */ int langidx = 0; /* index of next entry in vector */
static char langs[LANGS_SIZE]; static char langs[LANGS_SIZE];
char *curp; /* next language parameter in langs string */ char *curp; /* next language parameter in langs string */
const char *lange; /* language from environment variable */ const char *lange; /* language from environment variable */
int lang_len; int lang_len;
char *cln; /* colon in lange */ char *cln; /* colon in lange */
/* return cached vector */ /* return cached vector */
if (langv[0] != NULL) { if (langv[0] != NULL) {
return &langv[0]; return &langv[0];
}
curp = &langs[0];
lange = get_language();
if (lange != NULL) {
lang_len = strlen(lange) + 1;
if (lang_len < (LANGS_SIZE - 2)) {
memcpy(curp, lange, lang_len);
while ((curp[0] != 0) &&
(langidx < (LANGV_SIZE - 2))) {
/* avoid using strchrnul as it is not portable */
cln = strchr(curp, ':');
if (cln == NULL) {
langv[langidx++] = curp;
curp += lang_len;
break;
} else {
if ((cln - curp) > 1) {
/* only place non empty entries in vector */
langv[langidx++] = curp;
}
*cln++ = 0; /* null terminate */
lang_len -= (cln - curp);
curp = cln;
} }
}
}
}
/* ensure C language is present */ curp = &langs[0];
langv[langidx++] = curp;
*curp++ = 'C';
*curp++ = 0;
langv[langidx] = NULL;
return &langv[0]; lange = get_language();
if (lange != NULL) {
lang_len = strlen(lange) + 1;
if (lang_len < (LANGS_SIZE - 2)) {
memcpy(curp, lange, lang_len);
while ((curp[0] != 0) &&
(langidx < (LANGV_SIZE - 2))) {
/* avoid using strchrnul as it is not portable */
cln = strchr(curp, ':');
if (cln == NULL) {
langv[langidx++] = curp;
curp += lang_len;
break;
} else {
if ((cln - curp) > 1) {
/* only place non empty entries in vector */
langv[langidx++] = curp;
}
*cln++ = 0; /* null terminate */
lang_len -= (cln - curp);
curp = cln;
}
}
}
}
/* ensure C language is present */
langv[langidx++] = curp;
*curp++ = 'C';
*curp++ = 0;
langv[langidx] = NULL;
return &langv[0];
} }
/* Stolen from gtk/gui.c */ /* Stolen from gtk/gui.c */
static char ** static char **
nsmonkey_init_resource(const char *resource_path) nsmonkey_init_resource(const char *resource_path)
{ {
const char * const *langv; const char * const *langv;
char **pathv; /* resource path string vector */ char **pathv; /* resource path string vector */
char **respath; /* resource paths vector */ char **respath; /* resource paths vector */
pathv = filepath_path_to_strvec(resource_path); pathv = filepath_path_to_strvec(resource_path);
langv = get_languagev(); langv = get_languagev();
respath = filepath_generate(pathv, langv); respath = filepath_generate(pathv, langv);
filepath_free_strvec(pathv); filepath_free_strvec(pathv);
return respath; return respath;
} }
static void monkey_quit(void) static void monkey_quit(void)
{ {
urldb_save_cookies(nsoption_charp(cookie_jar)); urldb_save_cookies(nsoption_charp(cookie_jar));
urldb_save(nsoption_charp(url_file)); urldb_save(nsoption_charp(url_file));
monkey_fetch_filetype_fin(); monkey_fetch_filetype_fin();
} }
static nserror gui_launch_url(struct nsurl *url) static nserror gui_launch_url(struct nsurl *url)
{ {
fprintf(stdout, "GENERIC LAUNCH URL %s\n", nsurl_access(url)); fprintf(stdout, "GENERIC LAUNCH URL %s\n", nsurl_access(url));
return NSERROR_OK; return NSERROR_OK;
} }
static void quit_handler(int argc, char **argv) static void quit_handler(int argc, char **argv)
{ {
monkey_done = true; monkey_done = true;
} }
/** /**
@ -216,12 +216,12 @@ static void quit_handler(int argc, char **argv)
*/ */
static nserror set_defaults(struct nsoption_s *defaults) static nserror set_defaults(struct nsoption_s *defaults)
{ {
/* Set defaults for absent option strings */ /* Set defaults for absent option strings */
nsoption_setnull_charp(cookie_file, strdup("~/.netsurf/Cookies")); nsoption_setnull_charp(cookie_file, strdup("~/.netsurf/Cookies"));
nsoption_setnull_charp(cookie_jar, strdup("~/.netsurf/Cookies")); nsoption_setnull_charp(cookie_jar, strdup("~/.netsurf/Cookies"));
nsoption_setnull_charp(url_file, strdup("~/.netsurf/URLs")); nsoption_setnull_charp(url_file, strdup("~/.netsurf/URLs"));
return NSERROR_OK; return NSERROR_OK;
} }
@ -230,170 +230,169 @@ static nserror set_defaults(struct nsoption_s *defaults)
*/ */
static bool nslog_stream_configure(FILE *fptr) static bool nslog_stream_configure(FILE *fptr)
{ {
/* set log stream to be non-buffering */ /* set log stream to be non-buffering */
setbuf(fptr, NULL); setbuf(fptr, NULL);
return true; return true;
} }
static struct gui_misc_table monkey_misc_table = { static struct gui_misc_table monkey_misc_table = {
.schedule = monkey_schedule, .schedule = monkey_schedule,
.warning = monkey_warn_user, .warning = monkey_warn_user,
.quit = monkey_quit, .quit = monkey_quit,
.launch_url = gui_launch_url, .launch_url = gui_launch_url,
.cert_verify = gui_cert_verify, .cert_verify = gui_cert_verify,
.login = gui_401login_open, .login = gui_401login_open,
}; };
static void monkey_run(void) static void monkey_run(void)
{ {
fd_set read_fd_set, write_fd_set, exc_fd_set; fd_set read_fd_set, write_fd_set, exc_fd_set;
int max_fd; int max_fd;
int rdy_fd; int rdy_fd;
int schedtm; int schedtm;
struct timeval tv; struct timeval tv;
struct timeval* timeout; struct timeval* timeout;
while (!monkey_done) { while (!monkey_done) {
/* clears fdset */ /* clears fdset */
fetch_fdset(&read_fd_set, &write_fd_set, &exc_fd_set, &max_fd); fetch_fdset(&read_fd_set, &write_fd_set, &exc_fd_set, &max_fd);
/* add stdin to the set */ /* add stdin to the set */
if (max_fd < 0) { if (max_fd < 0) {
max_fd = 0; max_fd = 0;
} }
FD_SET(0, &read_fd_set); FD_SET(0, &read_fd_set);
FD_SET(0, &exc_fd_set); FD_SET(0, &exc_fd_set);
/* discover the next scheduled event time */ /* discover the next scheduled event time */
schedtm = monkey_schedule_run(); schedtm = monkey_schedule_run();
/* setup timeout */ /* setup timeout */
switch (schedtm) { switch (schedtm) {
case -1: case -1:
LOG("Iterate blocking"); LOG("Iterate blocking");
fprintf(stdout, "GENERIC POLL BLOCKING\n"); fprintf(stdout, "GENERIC POLL BLOCKING\n");
timeout = NULL; timeout = NULL;
break; break;
case 0: case 0:
LOG("Iterate immediate"); LOG("Iterate immediate");
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 0; tv.tv_usec = 0;
timeout = &tv; timeout = &tv;
break; break;
default: default:
LOG("Iterate non-blocking"); LOG("Iterate non-blocking");
fprintf(stdout, "GENERIC POLL TIMED %d\n", schedtm); fprintf(stdout, "GENERIC POLL TIMED %d\n", schedtm);
tv.tv_sec = schedtm / 1000; /* miliseconds to seconds */ tv.tv_sec = schedtm / 1000; /* miliseconds to seconds */
tv.tv_usec = (schedtm % 1000) * 1000; /* remainder to microseconds */ tv.tv_usec = (schedtm % 1000) * 1000; /* remainder to microseconds */
timeout = &tv; timeout = &tv;
break; break;
} }
rdy_fd = select(max_fd + 1,
&read_fd_set,
&write_fd_set,
&exc_fd_set,
timeout);
if (rdy_fd < 0) {
monkey_done = true;
} else if (rdy_fd > 0) {
if (FD_ISSET(0, &read_fd_set)) {
monkey_process_command();
}
}
}
rdy_fd = select(max_fd + 1,
&read_fd_set,
&write_fd_set,
&exc_fd_set,
timeout);
if (rdy_fd < 0) {
monkey_done = true;
} else if (rdy_fd > 0) {
if (FD_ISSET(0, &read_fd_set)) {
monkey_process_command();
}
}
}
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *messages; char *messages;
char *options; char *options;
char buf[PATH_MAX]; char buf[PATH_MAX];
nserror ret; nserror ret;
struct netsurf_table monkey_table = { struct netsurf_table monkey_table = {
.misc = &monkey_misc_table, .misc = &monkey_misc_table,
.window = monkey_window_table, .window = monkey_window_table,
.download = monkey_download_table, .download = monkey_download_table,
.fetch = monkey_fetch_table, .fetch = monkey_fetch_table,
.bitmap = monkey_bitmap_table, .bitmap = monkey_bitmap_table,
.layout = monkey_layout_table, .layout = monkey_layout_table,
}; };
ret = netsurf_register(&monkey_table); ret = netsurf_register(&monkey_table);
if (ret != NSERROR_OK) { if (ret != NSERROR_OK) {
die("NetSurf operation table failed registration"); die("NetSurf operation table failed registration");
} }
/* Unbuffer stdin/out/err */ /* Unbuffer stdin/out/err */
setbuf(stdin, NULL); setbuf(stdin, NULL);
setbuf(stdout, NULL); setbuf(stdout, NULL);
setbuf(stderr, NULL); setbuf(stderr, NULL);
/* Prep the search paths */ /* Prep the search paths */
respaths = nsmonkey_init_resource("${HOME}/.netsurf/:${NETSURFRES}:"MONKEY_RESPATH":./monkey/res"); respaths = nsmonkey_init_resource("${HOME}/.netsurf/:${NETSURFRES}:"MONKEY_RESPATH":./monkey/res");
/* initialise logging. Not fatal if it fails but not much we can do /* initialise logging. Not fatal if it fails but not much we can do
* about it either. * about it either.
*/ */
nslog_init(nslog_stream_configure, &argc, argv); nslog_init(nslog_stream_configure, &argc, argv);
/* user options setup */ /* user options setup */
ret = nsoption_init(set_defaults, &nsoptions, &nsoptions_default); ret = nsoption_init(set_defaults, &nsoptions, &nsoptions_default);
if (ret != NSERROR_OK) { if (ret != NSERROR_OK) {
die("Options failed to initialise"); die("Options failed to initialise");
} }
options = filepath_find(respaths, "Choices"); options = filepath_find(respaths, "Choices");
nsoption_read(options, nsoptions); nsoption_read(options, nsoptions);
free(options); free(options);
nsoption_commandline(&argc, argv, nsoptions); nsoption_commandline(&argc, argv, nsoptions);
messages = filepath_find(respaths, "Messages"); messages = filepath_find(respaths, "Messages");
ret = messages_add_from_file(messages); ret = messages_add_from_file(messages);
if (ret != NSERROR_OK) { if (ret != NSERROR_OK) {
LOG("Messages failed to load"); LOG("Messages failed to load");
} }
/* common initialisation */ /* common initialisation */
ret = netsurf_init(NULL); ret = netsurf_init(NULL);
free(messages); free(messages);
if (ret != NSERROR_OK) { if (ret != NSERROR_OK) {
die("NetSurf failed to initialise"); die("NetSurf failed to initialise");
} }
filepath_sfinddef(respaths, buf, "mime.types", "/etc/"); filepath_sfinddef(respaths, buf, "mime.types", "/etc/");
monkey_fetch_filetype_init(buf); monkey_fetch_filetype_init(buf);
urldb_load(nsoption_charp(url_file)); urldb_load(nsoption_charp(url_file));
urldb_load_cookies(nsoption_charp(cookie_file)); urldb_load_cookies(nsoption_charp(cookie_file));
ret = monkey_register_handler("QUIT", quit_handler); ret = monkey_register_handler("QUIT", quit_handler);
if (ret != NSERROR_OK) { if (ret != NSERROR_OK) {
die("quit handler failed to register"); die("quit handler failed to register");
} }
ret = monkey_register_handler("WINDOW", monkey_window_handle_command); ret = monkey_register_handler("WINDOW", monkey_window_handle_command);
if (ret != NSERROR_OK) { if (ret != NSERROR_OK) {
die("window handler fialed to register"); die("window handler fialed to register");
} }
fprintf(stdout, "GENERIC STARTED\n"); fprintf(stdout, "GENERIC STARTED\n");
monkey_run(); monkey_run();
fprintf(stdout, "GENERIC CLOSING_DOWN\n"); fprintf(stdout, "GENERIC CLOSING_DOWN\n");
monkey_kill_browser_windows(); monkey_kill_browser_windows();
netsurf_exit(); netsurf_exit();
fprintf(stdout, "GENERIC FINISHED\n"); fprintf(stdout, "GENERIC FINISHED\n");
/* finalise options */ /* finalise options */
nsoption_finalise(nsoptions, nsoptions_default); nsoption_finalise(nsoptions, nsoptions_default);
return 0; return 0;
} }

View File

@ -58,8 +58,8 @@ monkey_plot_clip(const struct redraw_context *ctx, const struct rect *clip)
*/ */
static nserror static nserror
monkey_plot_arc(const struct redraw_context *ctx, monkey_plot_arc(const struct redraw_context *ctx,
const plot_style_t *style, const plot_style_t *style,
int x, int y, int radius, int angle1, int angle2) int x, int y, int radius, int angle1, int angle2)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT ARC X %d Y %d RADIUS %d ANGLE1 %d ANGLE2 %d\n", "PLOT ARC X %d Y %d RADIUS %d ANGLE1 %d ANGLE2 %d\n",
@ -82,8 +82,8 @@ monkey_plot_arc(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_disc(const struct redraw_context *ctx, monkey_plot_disc(const struct redraw_context *ctx,
const plot_style_t *style, const plot_style_t *style,
int x, int y, int radius) int x, int y, int radius)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT DISC X %d Y %d RADIUS %d\n", "PLOT DISC X %d Y %d RADIUS %d\n",
@ -105,8 +105,8 @@ monkey_plot_disc(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_line(const struct redraw_context *ctx, monkey_plot_line(const struct redraw_context *ctx,
const plot_style_t *style, const plot_style_t *style,
const struct rect *line) const struct rect *line)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT LINE X0 %d Y0 %d X1 %d Y1 %d\n", "PLOT LINE X0 %d Y0 %d X1 %d Y1 %d\n",
@ -130,8 +130,8 @@ monkey_plot_line(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_rectangle(const struct redraw_context *ctx, monkey_plot_rectangle(const struct redraw_context *ctx,
const plot_style_t *style, const plot_style_t *style,
const struct rect *rect) const struct rect *rect)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT RECT X0 %d Y0 %d X1 %d Y1 %d\n", "PLOT RECT X0 %d Y0 %d X1 %d Y1 %d\n",
@ -156,9 +156,9 @@ monkey_plot_rectangle(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_polygon(const struct redraw_context *ctx, monkey_plot_polygon(const struct redraw_context *ctx,
const plot_style_t *style, const plot_style_t *style,
const int *p, const int *p,
unsigned int n) unsigned int n)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT POLYGON VERTICIES %d\n", "PLOT POLYGON VERTICIES %d\n",
@ -183,11 +183,11 @@ monkey_plot_polygon(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_path(const struct redraw_context *ctx, monkey_plot_path(const struct redraw_context *ctx,
const plot_style_t *pstyle, const plot_style_t *pstyle,
const float *p, const float *p,
unsigned int n, unsigned int n,
float width, float width,
const float transform[6]) const float transform[6])
{ {
fprintf(stdout, fprintf(stdout,
"PLOT PATH VERTICIES %d WIDTH %f\n", "PLOT PATH VERTICIES %d WIDTH %f\n",
@ -222,12 +222,12 @@ monkey_plot_path(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_bitmap(const struct redraw_context *ctx, monkey_plot_bitmap(const struct redraw_context *ctx,
struct bitmap *bitmap, struct bitmap *bitmap,
int x, int y, int x, int y,
int width, int width,
int height, int height,
colour bg, colour bg,
bitmap_flags_t flags) bitmap_flags_t flags)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT BITMAP X %d Y %d WIDTH %d HEIGHT %d\n", "PLOT BITMAP X %d Y %d WIDTH %d HEIGHT %d\n",
@ -249,11 +249,11 @@ monkey_plot_bitmap(const struct redraw_context *ctx,
*/ */
static nserror static nserror
monkey_plot_text(const struct redraw_context *ctx, monkey_plot_text(const struct redraw_context *ctx,
const struct plot_font_style *fstyle, const struct plot_font_style *fstyle,
int x, int x,
int y, int y,
const char *text, const char *text,
size_t length) size_t length)
{ {
fprintf(stdout, fprintf(stdout,
"PLOT TEXT X %d Y %d STR %*s\n", "PLOT TEXT X %d Y %d STR %*s\n",

View File

@ -38,10 +38,10 @@ static struct nscallback *schedule_list = NULL;
*/ */
struct nscallback struct nscallback
{ {
struct nscallback *next; struct nscallback *next;
struct timeval tv; struct timeval tv;
void (*callback)(void *p); void (*callback)(void *p);
void *p; void *p;
}; };
/** /**
@ -54,170 +54,163 @@ struct nscallback
*/ */
static nserror schedule_remove(void (*callback)(void *p), void *p) static nserror schedule_remove(void (*callback)(void *p), void *p)
{ {
struct nscallback *cur_nscb; struct nscallback *cur_nscb;
struct nscallback *prev_nscb; struct nscallback *prev_nscb;
struct nscallback *unlnk_nscb; struct nscallback *unlnk_nscb;
/* check there is something on the list to remove */ /* check there is something on the list to remove */
if (schedule_list == NULL) { if (schedule_list == NULL) {
return NSERROR_OK; return NSERROR_OK;
} }
SRLOG("removing %p, %p", callback, p); SRLOG("removing %p, %p", callback, p);
cur_nscb = schedule_list; cur_nscb = schedule_list;
prev_nscb = NULL; prev_nscb = NULL;
while (cur_nscb != NULL) { while (cur_nscb != NULL) {
if ((cur_nscb->callback == callback) && if ((cur_nscb->callback == callback) &&
(cur_nscb->p == p)) { (cur_nscb->p == p)) {
/* item to remove */ /* item to remove */
SRLOG("callback entry %p removing %p(%p)", SRLOG("callback entry %p removing %p(%p)",
cur_nscb, cur_nscb->callback, cur_nscb->p); cur_nscb, cur_nscb->callback, cur_nscb->p);
/* remove callback */ /* remove callback */
unlnk_nscb = cur_nscb; unlnk_nscb = cur_nscb;
cur_nscb = unlnk_nscb->next; cur_nscb = unlnk_nscb->next;
if (prev_nscb == NULL) { if (prev_nscb == NULL) {
schedule_list = cur_nscb; schedule_list = cur_nscb;
} else { } else {
prev_nscb->next = cur_nscb; prev_nscb->next = cur_nscb;
} }
free (unlnk_nscb); free (unlnk_nscb);
} else { } else {
/* move to next element */ /* move to next element */
prev_nscb = cur_nscb; prev_nscb = cur_nscb;
cur_nscb = prev_nscb->next; cur_nscb = prev_nscb->next;
} }
} }
return NSERROR_OK; return NSERROR_OK;
} }
/* exported function documented in framebuffer/schedule.h */ /* exported function documented in framebuffer/schedule.h */
nserror monkey_schedule(int tival, void (*callback)(void *p), void *p) nserror monkey_schedule(int tival, void (*callback)(void *p), void *p)
{ {
struct nscallback *nscb; struct nscallback *nscb;
struct timeval tv; struct timeval tv;
nserror ret; nserror ret;
/* ensure uniqueness of the callback and context */ /* ensure uniqueness of the callback and context */
ret = schedule_remove(callback, p); ret = schedule_remove(callback, p);
if ((tival < 0) || (ret != NSERROR_OK)) { if ((tival < 0) || (ret != NSERROR_OK)) {
return ret; return ret;
} }
SRLOG("Adding %p(%p) in %d", callback, p, tival); SRLOG("Adding %p(%p) in %d", callback, p, tival);
tv.tv_sec = tival / 1000; /* miliseconds to seconds */ tv.tv_sec = tival / 1000; /* miliseconds to seconds */
tv.tv_usec = (tival % 1000) * 1000; /* remainder to microseconds */ tv.tv_usec = (tival % 1000) * 1000; /* remainder to microseconds */
nscb = calloc(1, sizeof(struct nscallback)); nscb = calloc(1, sizeof(struct nscallback));
gettimeofday(&nscb->tv, NULL); gettimeofday(&nscb->tv, NULL);
timeradd(&nscb->tv, &tv, &nscb->tv); timeradd(&nscb->tv, &tv, &nscb->tv);
nscb->callback = callback; nscb->callback = callback;
nscb->p = p; nscb->p = p;
/* add to list front */ /* add to list front */
nscb->next = schedule_list; nscb->next = schedule_list;
schedule_list = nscb; schedule_list = nscb;
return NSERROR_OK; return NSERROR_OK;
} }
/* exported function documented in framebuffer/schedule.h */ /* exported function documented in framebuffer/schedule.h */
int monkey_schedule_run(void) int monkey_schedule_run(void)
{ {
struct timeval tv; struct timeval tv;
struct timeval nexttime; struct timeval nexttime;
struct timeval rettime; struct timeval rettime;
struct nscallback *cur_nscb; struct nscallback *cur_nscb;
struct nscallback *prev_nscb; struct nscallback *prev_nscb;
struct nscallback *unlnk_nscb; struct nscallback *unlnk_nscb;
if (schedule_list == NULL) if (schedule_list == NULL)
return -1; return -1;
/* reset enumeration to the start of the list */ /* reset enumeration to the start of the list */
cur_nscb = schedule_list; cur_nscb = schedule_list;
prev_nscb = NULL; prev_nscb = NULL;
nexttime = cur_nscb->tv;
gettimeofday(&tv, NULL);
while (cur_nscb != NULL) {
if (timercmp(&tv, &cur_nscb->tv, >)) {
/* scheduled time */
/* remove callback */
unlnk_nscb = cur_nscb;
if (prev_nscb == NULL) {
schedule_list = unlnk_nscb->next;
} else {
prev_nscb->next = unlnk_nscb->next;
}
unlnk_nscb->callback(unlnk_nscb->p);
free(unlnk_nscb);
/* need to deal with callback modifying the list. */
if (schedule_list == NULL)
return -1; /* no more callbacks scheduled */
/* reset enumeration to the start of the list */
cur_nscb = schedule_list;
prev_nscb = NULL;
nexttime = cur_nscb->tv;
} else {
/* if the time to the event is sooner than the
* currently recorded soonest event record it
*/
if (timercmp(&nexttime, &cur_nscb->tv, >)) {
nexttime = cur_nscb->tv; nexttime = cur_nscb->tv;
}
/* move to next element */
prev_nscb = cur_nscb;
cur_nscb = prev_nscb->next;
}
}
/* make rettime relative to now */ gettimeofday(&tv, NULL);
timersub(&nexttime, &tv, &rettime);
SRLOG("returning time to next event as %ldms", while (cur_nscb != NULL) {
(rettime.tv_sec * 1000) + (rettime.tv_usec / 1000)); if (timercmp(&tv, &cur_nscb->tv, >)) {
/* scheduled time */
/* return next event time in milliseconds (24days max wait) */ /* remove callback */
return (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000); unlnk_nscb = cur_nscb;
if (prev_nscb == NULL) {
schedule_list = unlnk_nscb->next;
} else {
prev_nscb->next = unlnk_nscb->next;
}
unlnk_nscb->callback(unlnk_nscb->p);
free(unlnk_nscb);
/* need to deal with callback modifying the list. */
if (schedule_list == NULL)
return -1; /* no more callbacks scheduled */
/* reset enumeration to the start of the list */
cur_nscb = schedule_list;
prev_nscb = NULL;
nexttime = cur_nscb->tv;
} else {
/* if the time to the event is sooner than the
* currently recorded soonest event record it
*/
if (timercmp(&nexttime, &cur_nscb->tv, >)) {
nexttime = cur_nscb->tv;
}
/* move to next element */
prev_nscb = cur_nscb;
cur_nscb = prev_nscb->next;
}
}
/* make rettime relative to now */
timersub(&nexttime, &tv, &rettime);
SRLOG("returning time to next event as %ldms",
(rettime.tv_sec * 1000) + (rettime.tv_usec / 1000));
/* return next event time in milliseconds (24days max wait) */
return (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000);
} }
void monkey_schedule_list(void) void monkey_schedule_list(void)
{ {
struct timeval tv; struct timeval tv;
struct nscallback *cur_nscb; struct nscallback *cur_nscb;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
LOG("schedule list at %lld:%ld", (long long)tv.tv_sec, tv.tv_usec); LOG("schedule list at %lld:%ld", (long long)tv.tv_sec, tv.tv_usec);
cur_nscb = schedule_list; cur_nscb = schedule_list;
while (cur_nscb != NULL) { while (cur_nscb != NULL) {
LOG("Schedule %p at %lld:%ld", LOG("Schedule %p at %lld:%ld",
cur_nscb, (long long)cur_nscb->tv.tv_sec, cur_nscb->tv.tv_usec); cur_nscb, (long long)cur_nscb->tv.tv_sec, cur_nscb->tv.tv_usec);
cur_nscb = cur_nscb->next; cur_nscb = cur_nscb->next;
} }
} }
/*
* Local Variables:
* c-basic-offset:2
* End:
*/