From 4634ce391a79c87afde266467c68b9bdd19f0d32 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 26 Feb 2016 23:41:11 -0800 Subject: [PATCH] GUAC-236: Allocate and pass display to all instruction handlers. --- src/guacenc/display.c | 11 +++++++++++ src/guacenc/display.h | 23 +++++++++++++++++++++++ src/guacenc/encode.c | 24 +++++++++++++++++++----- src/guacenc/instruction-blob.c | 3 ++- src/guacenc/instruction-cfill.c | 3 ++- src/guacenc/instruction-copy.c | 3 ++- src/guacenc/instruction-cursor.c | 3 ++- src/guacenc/instruction-dispose.c | 3 ++- src/guacenc/instruction-end.c | 3 ++- src/guacenc/instruction-img.c | 3 ++- src/guacenc/instruction-move.c | 3 ++- src/guacenc/instruction-rect.c | 3 ++- src/guacenc/instruction-shade.c | 3 ++- src/guacenc/instruction-size.c | 3 ++- src/guacenc/instruction-sync.c | 3 ++- src/guacenc/instruction-transfer.c | 3 ++- src/guacenc/instructions.c | 5 +++-- src/guacenc/instructions.h | 13 +++++++++++-- 18 files changed, 93 insertions(+), 22 deletions(-) diff --git a/src/guacenc/display.c b/src/guacenc/display.c index fa800804..6a1e32c8 100644 --- a/src/guacenc/display.c +++ b/src/guacenc/display.c @@ -25,8 +25,19 @@ #include +#include + int guacenc_display_sync(guacenc_display* display, guac_timestamp timestamp) { /* STUB */ return 0; } +guacenc_display* guacenc_display_alloc() { + return (guacenc_display*) calloc(1, sizeof(guacenc_display)); +} + +int guacenc_display_free(guacenc_display* display) { + free(display); + return 0; +} + diff --git a/src/guacenc/display.h b/src/guacenc/display.h index 3f438a4b..e4638db0 100644 --- a/src/guacenc/display.h +++ b/src/guacenc/display.h @@ -101,5 +101,28 @@ typedef struct guacenc_display { */ int guacenc_display_sync(guacenc_display* display, guac_timestamp timestamp); +/** + * Allocates a new Guacamole video encoder display. This display serves as the + * representation of encoding state, as well as the state of the Guacamole + * display as instructions are read and handled. + * + * @return + * The newly-allocated Guacamole video encoder display, or NULL if the + * display could not be allocated. + */ +guacenc_display* guacenc_display_alloc(); + +/** + * Frees all memory associated with the given Guacamole video encoder display, + * and finishes any underlying encoding process. + * + * @param display + * The Guacamole video encoder display to free. + * + * @return + * Zero if the encoding process completed successfully, non-zero otherwise. + */ +int guacenc_display_free(guacenc_display* display); + #endif diff --git a/src/guacenc/encode.c b/src/guacenc/encode.c index 5c2820c6..91cfc0df 100644 --- a/src/guacenc/encode.c +++ b/src/guacenc/encode.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include "display.h" #include "instructions.h" #include "log.h" @@ -40,6 +41,9 @@ * Reads and handles all Guacamole instructions from the given guac_socket * until end-of-stream is reached. * + * @param display + * The current internal display of the Guacamole video encoder. + * * @param path * The name of the file being parsed (for logging purposes). This file * must already be open and available through the given socket. @@ -51,7 +55,8 @@ * Zero on success, non-zero if parsing of Guacamole protocol data through * the given socket fails. */ -static int guacenc_read_instructions(const char* path, guac_socket* socket) { +static int guacenc_read_instructions(guacenc_display* display, + const char* path, guac_socket* socket) { /* Obtain Guacamole protocol parser */ guac_parser* parser = guac_parser_alloc(); @@ -60,7 +65,8 @@ static int guacenc_read_instructions(const char* path, guac_socket* socket) { /* Continuously read and handle all instructions */ while (!guac_parser_read(parser, socket, -1)) { - guacenc_handle_instruction(parser->opcode, parser->argc, parser->argv); + guacenc_handle_instruction(display, parser->opcode, + parser->argc, parser->argv); } /* Fail on read/parse error */ @@ -79,10 +85,16 @@ static int guacenc_read_instructions(const char* path, guac_socket* socket) { int guacenc_encode(const char* path) { + /* Allocate display for encoding process */ + guacenc_display* display = guacenc_display_alloc(); + if (display == NULL) + return 1; + /* Open input file */ int fd = open(path, O_RDONLY); if (fd < 0) { guacenc_log(GUAC_LOG_ERROR, "%s: %s", path, strerror(errno)); + guacenc_display_free(display); return 1; } @@ -92,20 +104,22 @@ int guacenc_encode(const char* path) { guacenc_log(GUAC_LOG_ERROR, "%s: %s", path, guac_status_string(guac_error)); close(fd); + guacenc_display_free(display); return 1; } guacenc_log(GUAC_LOG_INFO, "Encoding \"%s\" ...", path); /* Attempt to read all instructions in the file */ - if (guacenc_read_instructions(path, socket)) { + if (guacenc_read_instructions(display, path, socket)) { guac_socket_free(socket); + guacenc_display_free(display); return 1; } - /* Parsing/encoding was successful */ + /* Close input and finish encoding process */ guac_socket_free(socket); - return 0; + return guacenc_display_free(display); } diff --git a/src/guacenc/instruction-blob.c b/src/guacenc/instruction-blob.c index a149bfd8..f608e036 100644 --- a/src/guacenc/instruction-blob.c +++ b/src/guacenc/instruction-blob.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include @@ -28,7 +29,7 @@ #include -int guacenc_handle_blob(int argc, char** argv) { +int guacenc_handle_blob(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 2) { diff --git a/src/guacenc/instruction-cfill.c b/src/guacenc/instruction-cfill.c index ff35f756..5f83c59f 100644 --- a/src/guacenc/instruction-cfill.c +++ b/src/guacenc/instruction-cfill.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_cfill(int argc, char** argv) { +int guacenc_handle_cfill(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 6) { diff --git a/src/guacenc/instruction-copy.c b/src/guacenc/instruction-copy.c index 56f90fa2..5dbfcb1d 100644 --- a/src/guacenc/instruction-copy.c +++ b/src/guacenc/instruction-copy.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_copy(int argc, char** argv) { +int guacenc_handle_copy(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 9) { diff --git a/src/guacenc/instruction-cursor.c b/src/guacenc/instruction-cursor.c index 2b237a25..dc66c896 100644 --- a/src/guacenc/instruction-cursor.c +++ b/src/guacenc/instruction-cursor.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_cursor(int argc, char** argv) { +int guacenc_handle_cursor(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 7) { diff --git a/src/guacenc/instruction-dispose.c b/src/guacenc/instruction-dispose.c index 17bf320e..8d8333f3 100644 --- a/src/guacenc/instruction-dispose.c +++ b/src/guacenc/instruction-dispose.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_dispose(int argc, char** argv) { +int guacenc_handle_dispose(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 1) { diff --git a/src/guacenc/instruction-end.c b/src/guacenc/instruction-end.c index 55e11a39..3e300e8b 100644 --- a/src/guacenc/instruction-end.c +++ b/src/guacenc/instruction-end.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_end(int argc, char** argv) { +int guacenc_handle_end(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 1) { diff --git a/src/guacenc/instruction-img.c b/src/guacenc/instruction-img.c index 5cf2c884..d67fc402 100644 --- a/src/guacenc/instruction-img.c +++ b/src/guacenc/instruction-img.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_img(int argc, char** argv) { +int guacenc_handle_img(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 6) { diff --git a/src/guacenc/instruction-move.c b/src/guacenc/instruction-move.c index 5472361b..88e5d606 100644 --- a/src/guacenc/instruction-move.c +++ b/src/guacenc/instruction-move.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_move(int argc, char** argv) { +int guacenc_handle_move(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 5) { diff --git a/src/guacenc/instruction-rect.c b/src/guacenc/instruction-rect.c index f9609ab7..9202edf8 100644 --- a/src/guacenc/instruction-rect.c +++ b/src/guacenc/instruction-rect.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_rect(int argc, char** argv) { +int guacenc_handle_rect(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 5) { diff --git a/src/guacenc/instruction-shade.c b/src/guacenc/instruction-shade.c index f796c064..466568c9 100644 --- a/src/guacenc/instruction-shade.c +++ b/src/guacenc/instruction-shade.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_shade(int argc, char** argv) { +int guacenc_handle_shade(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 2) { diff --git a/src/guacenc/instruction-size.c b/src/guacenc/instruction-size.c index 1817bfb1..24375bc7 100644 --- a/src/guacenc/instruction-size.c +++ b/src/guacenc/instruction-size.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_size(int argc, char** argv) { +int guacenc_handle_size(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 3) { diff --git a/src/guacenc/instruction-sync.c b/src/guacenc/instruction-sync.c index 5f818743..ab789724 100644 --- a/src/guacenc/instruction-sync.c +++ b/src/guacenc/instruction-sync.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include @@ -63,7 +64,7 @@ static guac_timestamp guacenc_parse_timestamp(const char* str) { } -int guacenc_handle_sync(int argc, char** argv) { +int guacenc_handle_sync(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 1) { diff --git a/src/guacenc/instruction-transfer.c b/src/guacenc/instruction-transfer.c index ff37d282..f95a2a8f 100644 --- a/src/guacenc/instruction-transfer.c +++ b/src/guacenc/instruction-transfer.c @@ -21,13 +21,14 @@ */ #include "config.h" +#include "display.h" #include "log.h" #include #include -int guacenc_handle_transfer(int argc, char** argv) { +int guacenc_handle_transfer(guacenc_display* display, int argc, char** argv) { /* Verify argument count */ if (argc < 9) { diff --git a/src/guacenc/instructions.c b/src/guacenc/instructions.c index d2324096..ac9fbdf6 100644 --- a/src/guacenc/instructions.c +++ b/src/guacenc/instructions.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include "display.h" #include "instructions.h" #include "log.h" @@ -45,7 +46,7 @@ guacenc_instruction_handler_mapping guacenc_instruction_handler_map[] = { {NULL, NULL} }; -int guacenc_handle_instruction(const char* opcode, +int guacenc_handle_instruction(guacenc_display* display, const char* opcode, int argc, char** argv) { /* Search through mapping for instruction handler having given opcode */ @@ -58,7 +59,7 @@ int guacenc_handle_instruction(const char* opcode, /* Invoke defined handler */ guacenc_instruction_handler* handler = current->handler; if (handler != NULL) - return handler(argc, argv); + return handler(display, argc, argv); /* Log defined but unimplemented instructions */ guacenc_log(GUAC_LOG_DEBUG, "\"%s\" not implemented", opcode); diff --git a/src/guacenc/instructions.h b/src/guacenc/instructions.h index 0139ddb4..c015b271 100644 --- a/src/guacenc/instructions.h +++ b/src/guacenc/instructions.h @@ -24,6 +24,7 @@ #define GUACENC_INSTRUCTIONS_H #include "config.h" +#include "display.h" /** * A callback function which, when invoked, handles a particular Guacamole @@ -32,6 +33,9 @@ * arguments for that instruction are included in the parameters given to the * callback. * + * @param display + * The current internal display of the Guacamole video encoder. + * * @param argc * The number of arguments (excluding opcode) passed to the instruction * being handled by the callback. @@ -44,7 +48,8 @@ * Zero if the instruction was handled successfully, non-zero if an error * occurs. */ -typedef int guacenc_instruction_handler(int argc, char** argv); +typedef int guacenc_instruction_handler(guacenc_display* display, + int argc, char** argv); /** * Mapping of instruction opcode to corresponding handler function. @@ -76,6 +81,9 @@ extern guacenc_instruction_handler_mapping guacenc_instruction_handler_map[]; * Handles the instruction having the given opcode and arguments, encoding the * result to the in-progress video. * + * @param display + * The current internal display of the Guacamole video encoder. + * * @param opcode * The opcode of the instruction being handled. * @@ -91,7 +99,8 @@ extern guacenc_instruction_handler_mapping guacenc_instruction_handler_map[]; * Zero if the instruction was handled successfully, non-zero if an error * occurs. */ -int guacenc_handle_instruction(const char* opcode, int argc, char** argv); +int guacenc_handle_instruction(guacenc_display* display, + const char* opcode, int argc, char** argv); /** * Handler for the Guacamole "blob" instruction.