From eabeb419e37673ebd79c9c1355eac82068393103 Mon Sep 17 00:00:00 2001 From: keis Date: Sun, 11 Sep 2011 15:58:45 +0200 Subject: remove event formatting from run_parsed_command * split event formatting and sending into separate functions and expose both in events.h * make it possible to format array of strings --- src/events.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-------- src/events.h | 31 ++++++++++++++++++++++++- src/type.h | 4 +++- src/uzbl-core.c | 20 +++++++--------- 4 files changed, 103 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/events.c b/src/events.c index fdb6b2a..198839b 100644 --- a/src/events.c +++ b/src/events.c @@ -3,6 +3,8 @@ ** (c) 2009 by Robert Manea */ +#include + #include "uzbl-core.h" #include "events.h" #include "util.h" @@ -58,6 +60,11 @@ const char *event_table[LAST_EVENT] = { "BLUR_ELEMENT" }; +/* for now this is just a alias for GString */ +struct _Event { + GString message; +}; + void event_buffer_timeout(guint sec) { struct itimerval t; @@ -141,12 +148,12 @@ send_event_stdout(GString *msg) { fflush(stdout); } -void -vsend_event(int type, const gchar *custom_event, va_list vargs) { - GString *event_message = g_string_sized_new (512); - +Event * +vformat_event(int type, const gchar *custom_event, va_list vargs) { if (type >= LAST_EVENT) - return; + return NULL; + + GString *event_message = g_string_sized_new (512); const gchar *event = custom_event ? custom_event : event_table[type]; char* str; @@ -160,21 +167,39 @@ vsend_event(int type, const gchar *custom_event, va_list vargs) { case TYPE_INT: g_string_append_printf (event_message, "%d", va_arg (vargs, int)); break; + case TYPE_STR: /* a string that needs to be escaped */ g_string_append_c (event_message, '\''); append_escaped (event_message, va_arg (vargs, char*)); g_string_append_c (event_message, '\''); break; + case TYPE_FORMATTEDSTR: /* a string has already been escaped */ g_string_append (event_message, va_arg (vargs, char*)); break; + + case TYPE_STR_ARRAY: + ; /* gcc is acting up and requires a expression before the variables */ + GArray *a = va_arg (vargs, GArray*); + const char *p; + int i = 0; + while ((p = argv_idx(a, i++))) { + if (i != 0) + g_string_append_c(event_message, ' '); + g_string_append_c (event_message, '\''); + append_escaped (event_message, p); + g_string_append_c (event_message, '\''); + } + break; + case TYPE_NAME: str = va_arg (vargs, char*); g_assert (valid_name (str)); g_string_append (event_message, str); break; + case TYPE_FLOAT: // ‘float’ is promoted to ‘double’ when passed through ‘...’ @@ -192,8 +217,26 @@ vsend_event(int type, const gchar *custom_event, va_list vargs) { } } + return (Event*) event_message; +} + +Event * +format_event(int type, const gchar *custom_event, ...) { + va_list vargs, vacopy; + va_start (vargs, custom_event); + va_copy (vacopy, vargs); + Event *event = vformat_event (type, custom_event, vacopy); + va_end (vacopy); + va_end (vargs); + return event; +} + +void +send_formatted_event(const Event *event) { // A event string is not supposed to contain newlines as it will be // interpreted as two events + GString *event_message = (GString*)event; + if (!strchr(event_message->str, '\n')) { g_string_append_c(event_message, '\n'); @@ -201,14 +244,23 @@ vsend_event(int type, const gchar *custom_event, va_list vargs) { send_event_stdout (event_message); send_event_socket (event_message); } +} - g_string_free (event_message, TRUE); +void +event_free(Event *event) { + g_string_free ((GString*)event, TRUE); +} + +void +vsend_event(int type, const gchar *custom_event, va_list vargs) { + if (type >= LAST_EVENT) + return; + + Event *event = vformat_event(type, custom_event, vargs); + send_formatted_event (event); + event_free (event); } -/* - * build event string and send over the supported interfaces - * custom_event == NULL indicates an internal event -*/ void send_event(int type, const gchar *custom_event, ...) { va_list vargs, vacopy; diff --git a/src/events.h b/src/events.h index 8e89899..73d0712 100644 --- a/src/events.h +++ b/src/events.h @@ -31,18 +31,47 @@ enum event_type { LAST_EVENT }; +typedef struct _Event Event; +struct _Event; + void event_buffer_timeout(guint sec); void replay_buffered_events(); +/* + * build event string + */ +Event * +format_event(int type, const gchar *custom_event, ...) G_GNUC_NULL_TERMINATED; + +Event * +vformat_event(int type, const gchar *custom_event, va_list vargs); + +/* + * send a already formatted event string over the supported interfaces. + * returned event string should be freed by `event_free` + */ void -vsend_event(int type, const gchar *custom_event, va_list vargs); +send_formatted_event(const Event *event); +/* + * frees a event string + */ +void +event_free(Event *event); + +/* + * build event string and send over the supported interfaces + * this is the same as calling `format_event` and then `send_formatted_event` + */ void send_event(int type, const gchar *custom_event, ...) G_GNUC_NULL_TERMINATED; +void +vsend_event(int type, const gchar *custom_event, va_list vargs); + gchar * get_modifier_mask(guint state); diff --git a/src/type.h b/src/type.h index 24fc97f..20de0b4 100644 --- a/src/type.h +++ b/src/type.h @@ -7,7 +7,9 @@ enum ptr_type { TYPE_STR, TYPE_FLOAT, TYPE_NAME, - TYPE_FORMATTEDSTR // used by send_event + // used by send_event + TYPE_FORMATTEDSTR, + TYPE_STR_ARRAY }; typedef struct { diff --git a/src/uzbl-core.c b/src/uzbl-core.c index af60767..b37582e 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -619,21 +619,17 @@ run_parsed_command(const CommandInfo *c, GArray *a, GString *result) { if(strcmp("set", c->key) && strcmp("event", c->key) && strcmp("request", c->key)) { - // FIXME, build string inside send_event - GString *param = g_string_new(""); - const gchar *p; - guint i = 0; - while ((p = argv_idx(a, i++))) - g_string_append_printf(param, " '%s'", p); - - /* might be destructive on array a */ - c->function(uzbl.gui.web_view, a, result); - send_event(COMMAND_EXECUTED, NULL, + Event *event = format_event (COMMAND_EXECUTED, NULL, TYPE_NAME, c->key, - TYPE_FORMATTEDSTR, param->str, + TYPE_STR_ARRAY, a, NULL); - g_string_free(param, TRUE); + + /* might be destructive on array a */ + c->function(uzbl.gui.web_view, a, result); + + send_formatted_event (event); + event_free (event); } else c->function(uzbl.gui.web_view, a, result); -- cgit v1.2.3