From fa355a4f6376e43d437b80ab69796ca39629099b Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 21 Jun 2008 20:37:39 +0100 Subject: [PATCH] Add new cowmode_t for dream/normal mode --- src/display_cow.c | 38 +++++++++++++++++++++++++++++--------- src/display_cow.h | 9 +++++++-- src/xcowsay.c | 8 +++++--- src/xcowsayd.c | 2 +- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/display_cow.c b/src/display_cow.c index 5c91fbb..b641d9e 100644 --- a/src/display_cow.c +++ b/src/display_cow.c @@ -158,15 +158,15 @@ static int count_words(const char *s) return words; } -void display_cow(bool debug, const char *text, bool run_main) +static void normal_setup(const char *text, bool debug) { char *text_copy = copy_string(text); - + // Trim any trailing newline size_t len = strlen(text_copy); if ('\n' == text_copy[len-1]) text_copy[len-1] = '\0'; - + // Count the words and work out the display time, if neccessary xcowsay.display_time = get_int_option("display_time"); if (xcowsay.display_time < 0) { @@ -189,10 +189,30 @@ void display_cow(bool debug, const char *text, bool run_main) xcowsay.display_time = max_display; debug_msg("Display time too long: clamped to %d\n", max_display); } - + xcowsay.bubble_pixbuf = make_text_bubble(text_copy, &xcowsay.bubble_width, &xcowsay.bubble_height); free(text_copy); +} + +static void dream_setup(const char *file, bool debug) +{ + debug_msg("Dreaming file: %s\n", file); +} + +void display_cow(bool debug, const char *text, bool run_main, cowmode_t mode) +{ + switch (mode) { + case COWMODE_NORMAL: + normal_setup(text, debug); + break; + case COWMODE_DREAM: + dream_setup(text, debug); + break; + default: + fprintf(stderr, "Error: Unsupported cow mode %d\n", mode); + exit(1); + } g_assert(xcowsay.cow_pixbuf); xcowsay.cow = make_shape_from_pixbuf(xcowsay.cow_pixbuf); @@ -240,7 +260,7 @@ void display_cow(bool debug, const char *text, bool run_main) #ifdef WITHOUT_DBUS -bool try_dbus(bool debug, const char *text) +bool try_dbus(bool debug, const char *text, cowmode_t mode) { debug_msg("Skipping DBus (disabled by configure)\n"); return false; @@ -248,7 +268,7 @@ bool try_dbus(bool debug, const char *text) #else -bool try_dbus(bool debug, const char *text) +bool try_dbus(bool debug, const char *text, cowmode_t mode) { DBusGConnection *connection; GError *error; @@ -280,8 +300,8 @@ bool try_dbus(bool debug, const char *text) #endif /* #ifdef WITHOUT_DBUS */ -void display_cow_or_invoke_daemon(bool debug, const char *text) +void display_cow_or_invoke_daemon(bool debug, const char *text, cowmode_t mode) { - if (!try_dbus(debug, text)) - display_cow(debug, text, true); + if (!try_dbus(debug, text, mode)) + display_cow(debug, text, true, mode); } diff --git a/src/display_cow.h b/src/display_cow.h index bb62fa0..c5eed1a 100644 --- a/src/display_cow.h +++ b/src/display_cow.h @@ -28,9 +28,14 @@ #define debug_msg(...) if (debug) printf(__VA_ARGS__); #define debug_err(...) if (debug) g_printerr(__VA_ARGS__); +typedef enum { + COWMODE_NORMAL, + COWMODE_DREAM, +} cowmode_t; + // Show a cow with the given string and clean up afterwards -void display_cow(bool debug, const char *text, bool run_main); -void display_cow_or_invoke_daemon(bool debug, const char *text); +void display_cow(bool debug, const char *text, bool run_main, cowmode_t mode); +void display_cow_or_invoke_daemon(bool debug, const char *text, cowmode_t mode); void cowsay_init(int *argc, char ***argv); #endif diff --git a/src/xcowsay.c b/src/xcowsay.c index 940fe73..b4c7db2 100644 --- a/src/xcowsay.c +++ b/src/xcowsay.c @@ -71,7 +71,7 @@ static void read_from_stdin(void) } data[n] = '\0'; - display_cow_or_invoke_daemon(debug, data); + display_cow_or_invoke_daemon(debug, data, COWMODE_NORMAL); free(data); } @@ -232,14 +232,16 @@ int main(int argc, char **argv) cowsay_init(&argc, &argv); if (dream_file != NULL) { - printf("Dream %s\n", dream_file); + // COWMODE_DREAM not in daemon yet + // TODO: need to make path absolute + display_cow_or_invoke_daemon(debug, dream_file, COWMODE_DREAM); } else if (optind == argc) { read_from_stdin(); } else { char *str = cat_from_index(optind, argc, argv); - display_cow_or_invoke_daemon(debug, str); + display_cow_or_invoke_daemon(debug, str, COWMODE_NORMAL); free(str); } } diff --git a/src/xcowsayd.c b/src/xcowsayd.c index 6185fc3..3cca9be 100644 --- a/src/xcowsayd.c +++ b/src/xcowsayd.c @@ -129,7 +129,7 @@ static gpointer cow_display_thread(gpointer data) // GTK assumes it is being called from the main thread // (and it isn't here) gdk_threads_enter(); - display_cow(debug, mess, false); + display_cow(debug, mess, false, COWMODE_NORMAL); gdk_threads_leave(); g_cond_wait(display_complete, display_lock); -- 2.39.2