aboutsummaryrefslogtreecommitdiff
path: root/ui_ani.c
blob: c18ad4bef1f5f35190db9db27b9204fde067a652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <string.h>

#include "ui.h"
#include "ui_ani.h"

#define ANIC_INITSTATE '|'


struct anic *
init_anic(unsigned int x, unsigned int y, chtype attrs, char *fmt)
{
  struct anic *a = calloc(1, sizeof(struct anic));

  a->x = x;
  a->y = y;
  a->state = ANIC_INITSTATE;
  a->attrs = attrs;
  if (fmt != NULL) {
    a->fmt = strdup(fmt);
  }
  return (a);
}

void
free_anic(struct anic *a)
{
  if (a->fmt != NULL) {
    free(a->fmt);
  }
  free(a);
}

int
anic_cb(WINDOW *win, void *data, bool timed_out)
{
  struct anic *a = (struct anic *) data;
  char *tmp;
  int retval = UICB_OK;

  if (a == NULL) return (UICB_ERR_UNDEF);
  if (timed_out == true) {
    switch (a->state) {
      default:
      case '|': a->state = '/'; break;
      case '/': a->state = '-'; break;
      case '-': a->state = '\\'; break;
      case '\\': a->state = '|'; break;
    }
  }
  attron(a->attrs);
  if (a->fmt != NULL) {
    if (asprintf(&tmp, a->fmt, a->state) <= 0) {
      retval = UICB_ERR_BUF;
    }
  } else {
    asprintf(&tmp, "%c", a->state);
  }
  if (win != NULL) {
    mvwprintw(win, a->y, a->x, tmp);
  } else {
    mvprintw(a->y, a->x, tmp);
  }
  free(tmp);
  attroff(a->attrs);
  return (retval);
}

void
register_anic(struct anic *a)
{
  struct ui_callbacks cbs;
  cbs.ui_element = anic_cb;
  cbs.ui_input = NULL;
  register_ui_elt(&cbs, (void *) a, NULL);
}