2 * Copyright (C) 2009, 2010 Daniel Borkmann <daniel@netsniff-ng.org> and
3 * Emmanuel Roullit <emmanuel@netsniff-ng.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
29 #define SPINNER_SLEEP_TIME 250000
31 static const char spinning_chars
[] = { '|', '/', '-', '\\' };
33 void spinner_trigger_event(struct spinner_thread_context
*ctx
)
38 void spinner_set_msg(struct spinner_thread_context
*ctx
, const char *msg
)
43 strlcpy(ctx
->msg
, msg
, sizeof(ctx
->msg
) - 1);
46 void spinner_cancel(struct spinner_thread_context
*ctx
)
49 pthread_cancel(ctx
->thread
);
52 int spinner_create(struct spinner_thread_context
*ctx
)
56 rc
= pthread_create(&ctx
->thread
, NULL
, print_progress_spinner
, ctx
);
61 rc
= pthread_detach(ctx
->thread
);
66 void *print_progress_spinner(void *arg
)
68 uint8_t spin_count
= 0;
69 uint64_t prev_events
= 0;
70 struct spinner_thread_context
*ctx
=
71 (struct spinner_thread_context
*)arg
;
78 info("\b%c", spinning_chars
[spin_count
]);
80 usleep(SPINNER_SLEEP_TIME
);
82 if (prev_events
!= ctx
->events
) {
84 spin_count
%= sizeof(spinning_chars
);
85 prev_events
= ctx
->events
;