diff options
author | Brendan Taylor <whateley@gmail.com> | 2011-09-13 20:50:59 +0000 |
---|---|---|
committer | Brendan Taylor <whateley@gmail.com> | 2011-09-13 20:50:59 +0000 |
commit | 809c4a5a219c3d5262fb6035c048ff70411cd37e (patch) | |
tree | 5ff457e62afb1d882f62b0f79b4b758289099af4 /src | |
parent | 9f19ab832505ffd721cd466eba1a479ad4ff7cd4 (diff) | |
parent | e18b7eae54c0b62d93fa1f3c0596d45122a04541 (diff) |
Merge remote-tracking branch 'keis/event-format' into experimental
Diffstat (limited to 'src')
-rw-r--r-- | src/events.c | 96 | ||||
-rw-r--r-- | src/events.h | 31 | ||||
-rw-r--r-- | src/type.h | 4 | ||||
-rw-r--r-- | src/uzbl-core.c | 20 |
4 files changed, 122 insertions, 29 deletions
diff --git a/src/events.c b/src/events.c index 29e069f..081a942 100644 --- a/src/events.c +++ b/src/events.c @@ -3,6 +3,8 @@ ** (c) 2009 by Robert Manea */ +#include <glib.h> + #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,41 +167,100 @@ 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 ‘...’ - g_string_append_printf (event_message, "%.2f", va_arg (vargs, double)); + + // Make sure the formatted double fits in the buffer + if (event_message->allocated_len - event_message->len < G_ASCII_DTOSTR_BUF_SIZE) + g_string_set_size (event_message, event_message->len + G_ASCII_DTOSTR_BUF_SIZE); + + // format in C locale + char *tmp = g_ascii_formatd ( + event_message->str + event_message->len, + event_message->allocated_len - event_message->len, + "%.2f", va_arg (vargs, double)); + event_message->len += strlen(tmp); break; } } - g_string_append_c(event_message, '\n'); + 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'); + + if (uzbl.state.events_stdout) + send_event_stdout (event_message); + send_event_socket (event_message); + } +} + +void +event_free(Event *event) { + g_string_free ((GString*)event, TRUE); +} - if (uzbl.state.events_stdout) - send_event_stdout (event_message); - send_event_socket (event_message); +void +vsend_event(int type, const gchar *custom_event, va_list vargs) { + if (type >= LAST_EVENT) + return; - g_string_free (event_message, TRUE); + 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); @@ -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 5c4f3b7..b233f8b 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); |