From bdb05fa08abe1781827a3c095f26d7d0ac4dbe82 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 12 Mar 2011 17:27:18 +0000 Subject: Give Monkey a hearing aid, add 'WINDOW NEW' 'WINDOW GO' and 'WINDOW DESTROY' commands, along with 'QUIT' svn path=/trunk/netsurf/; revision=11974 --- monkey/Makefile.target | 2 +- monkey/browser.c | 122 ++++++++++++++++++++++++++++++++++++------------- monkey/browser.h | 3 ++ monkey/dispatch.c | 86 ++++++++++++++++++++++++++++++++++ monkey/dispatch.h | 28 ++++++++++++ monkey/main.c | 23 ++++++++-- monkey/poll.c | 57 ++++++++++++++++++++++- monkey/poll.h | 24 ++++++++++ 8 files changed, 308 insertions(+), 37 deletions(-) create mode 100644 monkey/dispatch.c create mode 100644 monkey/dispatch.h create mode 100644 monkey/poll.h (limited to 'monkey') diff --git a/monkey/Makefile.target b/monkey/Makefile.target index e4af5dbde..3a6d070b2 100644 --- a/monkey/Makefile.target +++ b/monkey/Makefile.target @@ -57,7 +57,7 @@ # S_MONKEY are sources purely for the MONKEY build S_MONKEY := main.c utils.c filetype.c schedule.c system_colour.c \ bitmap.c plot.c browser.c download.c thumbnail.c \ - 401login.c cert.c font.c poll.c + 401login.c cert.c font.c poll.c dispatch.c S_MONKEY := $(addprefix monkey/,$(S_MONKEY)) diff --git a/monkey/browser.c b/monkey/browser.c index b7afa658e..606884451 100644 --- a/monkey/browser.c +++ b/monkey/browser.c @@ -24,6 +24,7 @@ #include "desktop/browser.h" #include "desktop/gui.h" #include "utils/ring.h" +#include "utils/log.h" #include "monkey/browser.h" @@ -90,9 +91,9 @@ gui_create_browser_window(struct browser_window *bw, ret->width = 800; ret->height = 600; - fprintf(stdout, "BROWSER_WINDOW NEW WIN %u FOR %p CLONE %p NEWTAB %s\n", + fprintf(stdout, "WINDOW NEW WIN %u FOR %p CLONE %p NEWTAB %s\n", ret->win_num, bw, clone, new_tab ? "TRUE" : "FALSE"); - fprintf(stdout, "BROWSER_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); RING_INSERT(gw_ring, ret); @@ -109,7 +110,7 @@ gui_window_get_browser_window(struct gui_window *g) void gui_window_destroy(struct gui_window *g) { - fprintf(stdout, "BROWSER_WINDOW DESTROY WIN %u\n", g->win_num); + fprintf(stdout, "WINDOW DESTROY WIN %u\n", g->win_num); RING_REMOVE(gw_ring, g); free(g); } @@ -117,26 +118,20 @@ gui_window_destroy(struct gui_window *g) void gui_window_set_title(struct gui_window *g, const char *title) { - fprintf(stdout, "BROWSER_WINDOW TITLE WIN %u STR %s\n", g->win_num, title); + fprintf(stdout, "WINDOW TITLE WIN %u STR %s\n", g->win_num, title); } void gui_window_redraw_window(struct gui_window *g) { - fprintf(stdout, "BROWSER_WINDOW REDRAW WIN %u\n", g->win_num); -} - -void -gui_launch_url(const char *url) -{ - fprintf(stdout, "GENERIC LAUNCH URL %s\n", url); + fprintf(stdout, "WINDOW REDRAW WIN %u\n", g->win_num); } void gui_window_get_dimensions(struct gui_window *g, int *width, int *height, bool scaled) { - fprintf(stdout, "BROWSER_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); *width = g->width; *height = g->height; @@ -145,25 +140,25 @@ gui_window_get_dimensions(struct gui_window *g, int *width, int *height, void gui_window_new_content(struct gui_window *g) { - fprintf(stdout, "BROWSER_WINDOW NEW_CONTENT WIN %u\n", g->win_num); + fprintf(stdout, "WINDOW NEW_CONTENT WIN %u\n", g->win_num); } void gui_window_set_icon(struct gui_window *g, hlcache_handle *icon) { - fprintf(stdout, "BROWSER_WINDOW NEW_ICON WIN %u\n", g->win_num); + fprintf(stdout, "WINDOW NEW_ICON WIN %u\n", g->win_num); } void gui_window_start_throbber(struct gui_window *g) { - fprintf(stdout, "BROWSER_WINDOW START_THROBBER WIN %u\n", g->win_num); + fprintf(stdout, "WINDOW START_THROBBER WIN %u\n", g->win_num); } void gui_window_stop_throbber(struct gui_window *g) { - fprintf(stdout, "BROWSER_WINDOW STOP_THROBBER WIN %u\n", g->win_num); + fprintf(stdout, "WINDOW STOP_THROBBER WIN %u\n", g->win_num); } void @@ -171,14 +166,14 @@ gui_window_set_scroll(struct gui_window *g, int sx, int sy) { g->scrollx = sx; g->scrolly = sy; - fprintf(stdout, "BROWSER_WINDOW SET_SCROLL WIN %u X %d Y %d\n", g->win_num, sx, sy); + fprintf(stdout, "WINDOW SET_SCROLL WIN %u X %d Y %d\n", g->win_num, sx, sy); } void gui_window_update_box(struct gui_window *g, const union content_msg_data *data) { - fprintf(stdout, "BROWSER_WINDOW UPDATE_BOX WIN %u X %d Y %d WIDTH %d HEIGHT %d\n", + fprintf(stdout, "WINDOW UPDATE_BOX WIN %u X %d Y %d WIDTH %d HEIGHT %d\n", g->win_num, data->redraw.x, data->redraw.y, data->redraw.width, data->redraw.height); @@ -190,7 +185,7 @@ gui_window_update_extent(struct gui_window *g) if (!g->bw->current_content) return; - fprintf(stdout, "BROWSER_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, content_get_width(g->bw->current_content), content_get_height(g->bw->current_content)); @@ -199,7 +194,7 @@ gui_window_update_extent(struct gui_window *g) void gui_window_set_status(struct gui_window *g, const char *text) { - fprintf(stdout, "BROWSER_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); } void @@ -268,19 +263,19 @@ gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape) default: break; } - fprintf(stdout, "BROWSER_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); } void gui_window_set_scale(struct gui_window *g, float scale) { - fprintf(stdout, "BROWSER_WINDOW SET_SCALE WIN %u SCALE %f\n", g->win_num, scale); + fprintf(stdout, "WINDOW SET_SCALE WIN %u SCALE %f\n", g->win_num, scale); } void gui_window_set_url(struct gui_window *g, const char *url) { - fprintf(stdout, "BROWSER_WINDOW SET_URL WIN %u URL %s\n", g->win_num, url); + fprintf(stdout, "WINDOW SET_URL WIN %u URL %s\n", g->win_num, url); } void @@ -293,7 +288,7 @@ gui_drag_save_object(gui_save_type type, hlcache_handle *c, bool gui_window_get_scroll(struct gui_window *g, int *sx, int *sy) { - fprintf(stdout, "BROWSER_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); *sx = g->scrollx; *sy = g->scrolly; @@ -303,7 +298,7 @@ gui_window_get_scroll(struct gui_window *g, int *sx, int *sy) bool gui_window_scroll_start(struct gui_window *g) { - fprintf(stdout, "BROWSER_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; return true; } @@ -312,7 +307,7 @@ void gui_window_position_frame(struct gui_window *g, int x0, int y0, int x1, int y1) { - fprintf(stdout, "BROWSER_WINDOW POSITION_FRAME WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", + fprintf(stdout, "WINDOW POSITION_FRAME WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", g->win_num, x0, y0, x1, y1); } @@ -331,7 +326,7 @@ void gui_window_scroll_visible(struct gui_window *g, int x0, int y0, int x1, int y1) { - fprintf(stdout, "BROWSER_WINDOW SCROLL_VISIBLE WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", + fprintf(stdout, "WINDOW SCROLL_VISIBLE WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", g->win_num, x0, y0, x1, y1); } @@ -382,21 +377,21 @@ gui_copy_to_clipboard(struct selection *s) void gui_window_place_caret(struct gui_window *g, int x, int y, int height) { - fprintf(stdout, "BROWSER_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); } void gui_window_remove_caret(struct gui_window *g) { - fprintf(stdout, "BROWSER_WINDOW REMOVE_CARET WIN %u\n", g->win_num); + fprintf(stdout, "WINDOW REMOVE_CARET WIN %u\n", g->win_num); } bool gui_window_box_scroll_start(struct gui_window *g, int x0, int y0, int x1, int y1) { - fprintf(stdout, "BROWSER_WINDOW SCROLL_START WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", + fprintf(stdout, "WINDOW SCROLL_START WIN %u X0 %d Y0 %d X1 %d Y1 %d\n", g->win_num, x0, y0, x1, y1); return false; } @@ -405,7 +400,7 @@ void gui_create_form_select_menu(struct browser_window *bw, struct form_control *control) { - fprintf(stdout, "BROWSER_WINDOW SELECT_MENU WIN %u\n", + fprintf(stdout, "WINDOW SELECT_MENU WIN %u\n", bw->window->win_num); } @@ -413,6 +408,69 @@ void gui_window_save_link(struct gui_window *g, const char *url, const char *title) { - fprintf(stdout, "BROWSER_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, url, title); } + + +/**** Handlers ****/ + +static void +monkey_window_handle_new(int argc, char **argv) +{ + struct browser_window *bw; + if (argc > 3) + return; + bw = browser_window_create((argc == 3) ? argv[2] : NULL, NULL, NULL, true, false); +} + +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; + + 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"); + } else { + browser_window_go(gw->bw, argv[3], (argc == 5) ? argv[4] : NULL, true); + } + +} + +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); + } + +} diff --git a/monkey/browser.h b/monkey/browser.h index a8a565180..d0e39e08a 100644 --- a/monkey/browser.h +++ b/monkey/browser.h @@ -39,4 +39,7 @@ struct gui_window { struct gui_window *monkey_find_window_by_num(uint32_t win_num); struct gui_window *monkey_find_window_by_content(hlcache_handle *content); void monkey_window_process_reformats(void); + +void monkey_window_handle_command(int argc, char **argv); + #endif /* NETSURF_MONKEY_BROWSER_H */ diff --git a/monkey/dispatch.c b/monkey/dispatch.c new file mode 100644 index 000000000..c1b3edce4 --- /dev/null +++ b/monkey/dispatch.c @@ -0,0 +1,86 @@ +/* + * Copyright 2011 Daniel Silverstone + * + * 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 . + */ + +#include +#include +#include +#include + +#include "utils/log.h" +#include "utils/utils.h" +#include "utils/ring.h" + +#include "desktop/netsurf.h" +#include "monkey/dispatch.h" + +typedef struct cmdhandler { + struct cmdhandler *r_next, *r_prev; + const char *cmd; + handle_command_fn fn; +} monkey_cmdhandler_t; + +static monkey_cmdhandler_t *handler_ring = NULL; + +void +monkey_register_handler(const char *cmd, handle_command_fn fn) +{ + monkey_cmdhandler_t *ret = calloc(sizeof(*ret), 1); + if (ret == NULL) + die("Unable to allocate handler"); + ret->cmd = strdup(cmd); + ret->fn = fn; + RING_INSERT(handler_ring, ret); +} + +void +monkey_process_command(void) +{ + char buffer[PATH_MAX]; + int argc = 0; + char **argv = NULL; + char *p, *r = NULL; + handle_command_fn fn = NULL; + + if (fgets(buffer, PATH_MAX, stdin) == NULL) { + netsurf_quit = true; + } + + buffer[strlen(buffer)-1] = '\0'; + + argv = malloc(sizeof *argv); + argc = 1; + *argv = buffer; + + for (p = r = buffer; *p != '\0'; p++) { + if (*p == ' ') { + argv = realloc(argv, sizeof(*argv) * (argc + 1)); + argv[argc++] = r = p + 1; + *p = '\0'; + } + } + + RING_ITERATE_START(monkey_cmdhandler_t, handler_ring, handler) { + if (strcmp(argv[0], handler->cmd) == 0) { + fn = handler->fn; + RING_ITERATE_STOP(handler_ring, handler); + } + } RING_ITERATE_END(handler_ring, handler); + + if (fn != NULL) + fn(argc, argv); +} diff --git a/monkey/dispatch.h b/monkey/dispatch.h new file mode 100644 index 000000000..fe8f4e2f8 --- /dev/null +++ b/monkey/dispatch.h @@ -0,0 +1,28 @@ +/* + * Copyright 2011 Daniel Silverstone + * + * 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 . + */ + +#ifndef NETSURF_MONKEY_DISPATCH_H +#define NETSURF_MONKEY_DISPATCH_H 1 + +typedef void (*handle_command_fn)(int argc, char **argv); + +void monkey_register_handler(const char *cmd, handle_command_fn fn); + +void monkey_process_command(void); + +#endif /* NETSURF_MONKEY_DISPATCH_H */ diff --git a/monkey/main.c b/monkey/main.c index 1da7e3608..1dab747e2 100644 --- a/monkey/main.c +++ b/monkey/main.c @@ -22,6 +22,9 @@ #include "monkey/filetype.h" #include "monkey/options.h" +#include "monkey/poll.h" +#include "monkey/dispatch.h" +#include "monkey/browser.h" #include "content/urldb.h" #include "content/fetchers/resource.h" @@ -75,6 +78,17 @@ char* gui_find_resource(const char *filename) return path_to_url(resource_sfind(respaths, buf, filename)); } +void +gui_launch_url(const char *url) +{ + fprintf(stdout, "GENERIC LAUNCH URL %s\n", url); +} + +static void quit_handler(int argc, char **argv) +{ + netsurf_quit = true; +} + int main(int argc, char **argv) { @@ -110,11 +124,14 @@ main(int argc, char **argv) sslcert_init("content.png"); - browser_window_create("http://www.netsurf-browser.org/welcome/", 0, 0, true, false); + monkey_prepare_input(); + monkey_register_handler("QUIT", quit_handler); + monkey_register_handler("WINDOW", monkey_window_handle_command); + fprintf(stdout, "GENERIC STARTED\n"); netsurf_main_loop(); - + fprintf(stdout, "GENERIC CLOSING_DOWN\n"); netsurf_exit(); - + fprintf(stdout, "GENERIC FINISHED\n"); return 0; } diff --git a/monkey/poll.c b/monkey/poll.c index ee7a32d4a..52da61344 100644 --- a/monkey/poll.c +++ b/monkey/poll.c @@ -23,6 +23,8 @@ #include "monkey/schedule.h" #include "monkey/browser.h" #include "content/fetchers/curl.h" +#include "monkey/dispatch.h" +#include "monkey/poll.h" #ifdef DEBUG_POLL_LOOP #include "utils/log.h" @@ -33,7 +35,60 @@ #include -void gui_poll(bool active) +typedef struct { + GSource gs; + GPollFD pf; +} MonkeySource; + +static gboolean monkey_source_prepare(GSource *source, + gint *timeout_) +{ + *timeout_ = -1; + return FALSE; +} + + +static gboolean monkey_source_check(GSource *source) +{ + MonkeySource *ms = (MonkeySource *)source; + if (ms->pf.revents & G_IO_IN) { + return TRUE; + } + return FALSE; +} + +static gboolean monkey_source_dispatch(GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + monkey_process_command(); + return TRUE; +} + +static void monkey_source_finalize(GSource *source) +{ + +} + +GSourceFuncs monkey_source_funcs = { + .prepare = monkey_source_prepare, + .check = monkey_source_check, + .dispatch = monkey_source_dispatch, + .finalize = monkey_source_finalize, +}; + +void +monkey_prepare_input(void) +{ + MonkeySource *gs = (MonkeySource *)g_source_new(&monkey_source_funcs, sizeof *gs); + gs->pf.fd = 0; + gs->pf.events = G_IO_IN | G_IO_ERR; + g_source_add_poll((GSource *)gs, &gs->pf); + g_source_attach((GSource *)gs, NULL); +} + +void +gui_poll(bool active) { CURLMcode code; fd_set read_fd_set, write_fd_set, exc_fd_set; diff --git a/monkey/poll.h b/monkey/poll.h new file mode 100644 index 000000000..1aebe6856 --- /dev/null +++ b/monkey/poll.h @@ -0,0 +1,24 @@ +/* + * Copyright 2011 Daniel Silverstone + * + * 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 . + */ + +#ifndef NETSURF_MONKEY_POLL_H +#define NETSURF_MONKEY_POLL_H 1 + +void monkey_prepare_input(void); + +#endif /* NETSURF_MONKEY_POLL_H */ -- cgit v1.2.3