Indention of code
[netsniff-ng.git] / src / cursor.c
blobc5ba2006e097f6b5eed375b6c70fb370d01cd005
1 /*
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
13 * for more details.
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
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <pthread.h>
23 #include <assert.h>
25 #include "cursor.h"
26 #include "macros.h"
27 #include "strlcpy.h"
29 #define SPINNER_SLEEP_TIME 250000
31 static const char spinning_chars[] = { '|', '/', '-', '\\' };
33 void spinner_trigger_event(struct spinner_thread_context *ctx)
35 ctx->events++;
38 void spinner_set_msg(struct spinner_thread_context *ctx, const char *msg)
40 assert(ctx);
41 assert(msg);
43 strlcpy(ctx->msg, msg, sizeof(ctx->msg) - 1);
46 void spinner_cancel(struct spinner_thread_context *ctx)
48 if (ctx->active)
49 pthread_cancel(ctx->thread);
52 int spinner_create(struct spinner_thread_context *ctx)
54 int rc;
56 rc = pthread_create(&ctx->thread, NULL, print_progress_spinner, ctx);
58 if (rc != 0)
59 return (rc);
61 rc = pthread_detach(ctx->thread);
63 return (rc);
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;
73 ctx->active = 1;
75 info("%s", ctx->msg);
77 while (1) {
78 info("\b%c", spinning_chars[spin_count]);
79 fflush(stdout);
80 usleep(SPINNER_SLEEP_TIME);
82 if (prev_events != ctx->events) {
83 spin_count++;
84 spin_count %= sizeof(spinning_chars);
85 prev_events = ctx->events;