From 1e7a3779b06e980b48e2e6b9f95fbbf43df6dffe Mon Sep 17 00:00:00 2001 From: Lutz Mueller Date: Thu, 10 Jan 2002 23:43:34 +0000 Subject: [PATCH] =?UTF-8?q?2002-01-11=20=20Lutz=20M=EF=BF=BDller=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * libgphoto2/gphoto2-context.[c,h]: Support for multiple progress reports at once (especially sub-progress reports). git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@3799 67ed7778-7388-44ab-90cf-0a291f65f57c --- ChangeLog | 5 +++ frontends/command-line/main.c | 44 ++++++++++++++++++---- libgphoto2/gphoto2-abilities-list.c | 9 +++-- libgphoto2/gphoto2-context.c | 58 ++++++++++++++++++++++++----- libgphoto2/gphoto2-context.h | 46 +++++++++++++++-------- 5 files changed, 126 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 150fc6236..b4eb1819a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-01-11 Lutz Müller + + * libgphoto2/gphoto2-context.[c,h]: Support for multiple progress + reports at once (especially sub-progress reports). + 2002-01-10 Lutz Müller * libgphoto2/gphoto2-filesys.[c,h]: Add a GPContext to all functions diff --git a/frontends/command-line/main.c b/frontends/command-line/main.c index 2894cbd6e..af027768c 100644 --- a/frontends/command-line/main.c +++ b/frontends/command-line/main.c @@ -1232,11 +1232,38 @@ ctx_error_func (GPContext *context, const char *format, va_list args, fflush (stderr); } -static void -ctx_progress_func (GPContext *context, float percentage, void *data) +static float targets[] = {0, 0, 0, 0, 0, 0, 0, 0}; + +static unsigned int +ctx_progress_start_func (GPContext *context, float target, + const char *format, va_list args, void *data) { - fprintf (stderr, "Progress: %02.01f\r", percentage * 100.); + unsigned int id; + + for (id = 0; id < 7; id++) + if (!targets[id]) + break; + targets[id] = target; + + vfprintf (stderr, format, args); + fprintf (stderr, "\n"); fflush (stderr); + + return (id); +} + +static void +ctx_progress_update_func (GPContext *context, unsigned int id, + float current, void *data) +{ + printf ("Percent completed: %02.01f\r", current / targets[id]); + fflush(stdout); +} + +static void +ctx_progress_stop_func (GPContext *context, unsigned int id, void *data) +{ + targets[id] = 0; } static GPContextFeedback @@ -1270,10 +1297,13 @@ init_globals (void) glob_recurse = 0; glob_context = gp_context_new (); - gp_context_set_cancel_func (glob_context, ctx_cancel_func, NULL); - gp_context_set_progress_func (glob_context, ctx_progress_func, NULL); - gp_context_set_error_func (glob_context, ctx_error_func, NULL); - gp_context_set_status_func (glob_context, ctx_status_func, NULL); + gp_context_set_cancel_func (glob_context, ctx_cancel_func, NULL); + gp_context_set_progress_funcs (glob_context, + ctx_progress_start_func, + ctx_progress_update_func, + ctx_progress_stop_func, NULL); + gp_context_set_error_func (glob_context, ctx_error_func, NULL); + gp_context_set_status_func (glob_context, ctx_status_func, NULL); return GP_OK; } diff --git a/libgphoto2/gphoto2-abilities-list.c b/libgphoto2/gphoto2-abilities-list.c index 313074541..01356b06b 100644 --- a/libgphoto2/gphoto2-abilities-list.c +++ b/libgphoto2/gphoto2-abilities-list.c @@ -139,7 +139,7 @@ gp_abilities_list_load_dir (CameraAbilitiesList *list, const char *dir, CameraLibraryAbilitiesFunc ab; CameraText text; int x, old_count, new_count; - unsigned int i, n; + unsigned int i, n, p; const char *filename; #ifdef HAVE_LTDL CameraList flist; @@ -246,8 +246,8 @@ gp_abilities_list_load_dir (CameraAbilitiesList *list, const char *dir, GP_SYSTEM_CLOSEDIR (d); d = GP_SYSTEM_OPENDIR (dir); - gp_context_status (context, _("Loading camera drivers from '%s'..."), - dir); + p = gp_context_progress_start (context, n, + _("Loading camera drivers from '%s'..."), dir); i = 0; do { @@ -260,7 +260,7 @@ gp_abilities_list_load_dir (CameraAbilitiesList *list, const char *dir, * possiblility to cancel. */ i++; - gp_context_progress (context, (float) i / (float) n); + gp_context_progress_update (context, p, i); if (gp_context_cancel (context) == GP_CONTEXT_FEEDBACK_CANCEL) return (GP_ERROR_CANCEL); @@ -344,6 +344,7 @@ gp_abilities_list_load_dir (CameraAbilitiesList *list, const char *dir, } } } while (de); + gp_context_progress_stop (context, p); #endif return (GP_OK); diff --git a/libgphoto2/gphoto2-context.c b/libgphoto2/gphoto2-context.c index 06ddf642d..a109c3ce2 100644 --- a/libgphoto2/gphoto2-context.c +++ b/libgphoto2/gphoto2-context.c @@ -28,8 +28,10 @@ struct _GPContext GPContextIdleFunc idle_func; void *idle_func_data; - GPContextProgressFunc progress_func; - void *progress_func_data; + GPContextProgressStartFunc progress_start_func; + GPContextProgressUpdateFunc progress_update_func; + GPContextProgressStopFunc progress_stop_func; + void *progress_func_data; GPContextErrorFunc error_func; void *error_func_data; @@ -127,15 +129,46 @@ gp_context_idle (GPContext *context) context->idle_func (context, context->idle_func_data); } +unsigned int +gp_context_progress_start (GPContext *context, float target, + const char *format, ...) +{ + va_list args; + unsigned int id; + + if (!context) + return (0); + + if (context->progress_start_func) { + va_start (args, format); + id = context->progress_start_func (context, target, format, + args, context->progress_func_data); + va_end (args); + } + + return (0); +} + void -gp_context_progress (GPContext *context, float percentage) +gp_context_progress_update (GPContext *context, unsigned int id, float current) { if (!context) return; - if (context->progress_func) - context->progress_func (context, percentage, - context->progress_func_data); + if (context->progress_update_func) + context->progress_update_func (context, id, current, + context->progress_func_data); +} + +void +gp_context_progress_stop (GPContext *context, unsigned int id) +{ + if (!context) + return; + + if (context->progress_stop_func) + context->progress_stop_func (context, id, + context->progress_func_data); } void @@ -233,14 +266,19 @@ gp_context_set_idle_func (GPContext *context, GPContextIdleFunc func, } void -gp_context_set_progress_func (GPContext *context, GPContextProgressFunc func, - void *data) +gp_context_set_progress_funcs (GPContext *context, + GPContextProgressStartFunc start_func, + GPContextProgressUpdateFunc update_func, + GPContextProgressStopFunc stop_func, + void *data) { if (!context) return; - context->progress_func = func; - context->progress_func_data = data; + context->progress_start_func = start_func; + context->progress_update_func = update_func; + context->progress_stop_func = stop_func; + context->progress_func_data = data; } void diff --git a/libgphoto2/gphoto2-context.h b/libgphoto2/gphoto2-context.h index 7a5067786..3837e655f 100644 --- a/libgphoto2/gphoto2-context.h +++ b/libgphoto2/gphoto2-context.h @@ -38,8 +38,6 @@ enum _GPContextFeedback { /* Functions */ typedef void (* GPContextIdleFunc) (GPContext *context, void *data); -typedef void (* GPContextProgressFunc) (GPContext *context, float percentage, - void *data); typedef void (* GPContextErrorFunc) (GPContext *context, const char *format, va_list args, void *data); typedef void (* GPContextStatusFunc) (GPContext *context, const char *format, @@ -49,28 +47,46 @@ typedef GPContextFeedback (* GPContextQuestionFunc) (GPContext *context, va_list args, void *data); typedef GPContextFeedback (* GPContextCancelFunc) (GPContext *context, void *data); +typedef unsigned int (* GPContextProgressStartFunc) (GPContext *context, + float target, + const char *format, + va_list args, void *data); +typedef void (* GPContextProgressUpdateFunc) (GPContext *context, + unsigned int id, + float current, + void *data); +typedef void (* GPContextProgressStopFunc) (GPContext *context, + unsigned int id, + void *data); /* Setting those functions (frontends) */ -void gp_context_set_idle_func (GPContext *context, - GPContextIdleFunc func, void *data); -void gp_context_set_progress_func (GPContext *context, - GPContextProgressFunc func, void *data); -void gp_context_set_error_func (GPContext *context, - GPContextErrorFunc func, void *data); -void gp_context_set_status_func (GPContext *context, - GPContextStatusFunc func, void *data); -void gp_context_set_question_func (GPContext *context, - GPContextQuestionFunc func, void *data); -void gp_context_set_cancel_func (GPContext *context, - GPContextCancelFunc func, void *data); +void gp_context_set_idle_func (GPContext *context, + GPContextIdleFunc func, void *data); +void gp_context_set_progress_funcs (GPContext *context, + GPContextProgressStartFunc start_func, + GPContextProgressUpdateFunc update_func, + GPContextProgressStopFunc stop_func, + void *data); +void gp_context_set_error_func (GPContext *context, + GPContextErrorFunc func, void *data); +void gp_context_set_status_func (GPContext *context, + GPContextStatusFunc func, void *data); +void gp_context_set_question_func (GPContext *context, + GPContextQuestionFunc func, void *data); +void gp_context_set_cancel_func (GPContext *context, + GPContextCancelFunc func, void *data); /* Calling those functions (backends) */ void gp_context_idle (GPContext *context); -void gp_context_progress (GPContext *context, float percentage); void gp_context_error (GPContext *context, const char *format, ...); void gp_context_status (GPContext *context, const char *format, ...); GPContextFeedback gp_context_question (GPContext *context, const char *format, ...); GPContextFeedback gp_context_cancel (GPContext *context); +unsigned int gp_context_progress_start (GPContext *context, float target, + const char *format, ...); +void gp_context_progress_update (GPContext *context, unsigned int id, + float current); +void gp_context_progress_stop (GPContext *context, unsigned int id); #endif /* __GPHOTO2_CONTEXT_H__ */