summaryrefslogtreecommitdiff
path: root/src/display.c
blob: 146d31e94d07998a1ba110c865872fbfe5d04abd (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#define _DEFAULT_SOURCE
#include "display.h"

#include "buffer.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#define ESC 0x1b

struct render_command {
  uint32_t col;
  uint32_t row;

  uint8_t *data;
  uint32_t len;

  uint8_t *fmt;
  uint32_t fmt_len;
};

struct command_list {
  struct render_command *cmds;
  uint64_t ncmds;
  uint64_t capacity;

  uint32_t xoffset;
  uint32_t yoffset;

  uint8_t format[64];
  uint32_t format_len;

  alloc_fn allocator;

  char name[16];
};

struct winsize getsize() {
  struct winsize ws;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
  return ws;
}

struct display display_create() {

  struct winsize ws = getsize();

  // save old settings
  struct termios orig_term;
  tcgetattr(0, &orig_term);

  // set terminal to raw mode
  struct termios term;
  cfmakeraw(&term);

  tcsetattr(0, TCSADRAIN, &term);

  return (struct display){
      .orig_term = orig_term,
      .term = term,
      .height = ws.ws_row,
      .width = ws.ws_col,
  };
}

void display_resize(struct display *display) {
  struct winsize sz = getsize();
  display->width = sz.ws_col;
  display->height = sz.ws_row;
}

void display_destroy(struct display *display) {
  // reset old terminal mode
  tcsetattr(0, TCSADRAIN, &display->orig_term);
}

void putbytes(uint8_t *line_bytes, uint32_t line_length) {
  for (uint32_t bytei = 0; bytei < line_length; ++bytei) {
    uint8_t byte = line_bytes[bytei];

    if (byte == '\t') {
      fputs("    ", stdout);
    } else {
      fputc(byte, stdout);
    }
  }
}

void putbyte(uint8_t c) { putc(c, stdout); }

void put_ansiparm(int n) {
  int q = n / 10;
  if (q != 0) {
    int r = q / 10;
    if (r != 0) {
      putbyte((r % 10) + '0');
    }
    putbyte((q % 10) + '0');
  }
  putbyte((n % 10) + '0');
}

void put_format(uint8_t *bytes, uint32_t nbytes) {
  putbyte(ESC);
  putbyte('[');
  putbyte('0');

  if (nbytes > 0) {
    putbyte(';');
    putbytes(bytes, nbytes);
  }

  putbyte('m');
}

void display_move_cursor(struct display *display, uint32_t row, uint32_t col) {
  putbyte(ESC);
  putbyte('[');
  put_ansiparm(row + 1);
  putbyte(';');
  put_ansiparm(col + 1);
  putbyte('H');
}

void display_clear(struct display *display) {
  display_move_cursor(display, 0, 0);
  uint8_t bytes[] = {ESC, '[', 'J'};
  putbytes(bytes, 3);
}

void delete_to_eol() {
  uint8_t bytes[] = {ESC, '[', 'K'};
  putbytes(bytes, 3);
}

struct command_list *command_list_create(uint32_t capacity, alloc_fn allocator,
                                         uint32_t xoffset, uint32_t yoffset,
                                         const char *name) {
  struct command_list *command_list = allocator(sizeof(struct command_list));

  command_list->capacity = capacity;
  command_list->ncmds = 0;
  command_list->xoffset = xoffset;
  command_list->yoffset = yoffset;
  command_list->format_len = 0;
  strncpy(command_list->name, name, 15);

  command_list->cmds = allocator(sizeof(struct render_command) * capacity);
  command_list->allocator = allocator;

  return command_list;
}

void push_format(struct command_list *list, const char *fmt, ...) {
  va_list args;
  va_start(args, fmt);

  if (list->format_len > 0) {
    list->format[list->format_len] = ';';
    ++list->format_len;
  }

  uint32_t format_space_left = sizeof(list->format) - list->format_len;
  list->format_len += vsnprintf((char *)(list->format + list->format_len),
                                format_space_left, fmt, args);

  va_end(args);
}

void set_cmd_format(struct command_list *list, struct render_command *cmd) {
  if (list->format_len > 0) {
    cmd->fmt = list->allocator(list->format_len);
    memcpy(cmd->fmt, list->format, list->format_len);
    cmd->fmt_len = list->format_len;
  } else {
    cmd->fmt = NULL;
    cmd->fmt_len = 0;
  }
}

void reset_format(struct command_list *list) { list->format_len = 0; }

void command_list_draw_text(struct command_list *list, uint32_t col,
                            uint32_t row, uint8_t *data, uint32_t len) {
  if (list->ncmds == list->capacity) {
    // TODO: better
    return;
  }

  struct render_command *cmd = &list->cmds[list->ncmds];
  cmd->data = data;
  cmd->col = col + list->xoffset;
  cmd->row = row + list->yoffset;
  cmd->len = len;

  set_cmd_format(list, cmd);

  ++list->ncmds;
}

void command_list_draw_text_copy(struct command_list *list, uint32_t col,
                                 uint32_t row, uint8_t *data, uint32_t len) {
  uint8_t *bytes = (uint8_t *)list->allocator(len);
  memcpy(bytes, data, len);

  command_list_draw_text(list, col, row, bytes, len);
}

void command_list_set_index_color_fg(struct command_list *list,
                                     uint8_t color_idx) {
  if (color_idx < 8) {
    push_format(list, "%d", 30 + color_idx);
  } else if (color_idx < 16) {
    push_format(list, "%d", 90 + color_idx - 8);
  } else {
    push_format(list, "38;5;%d", color_idx);
  }
}
void command_list_set_color_fg(struct command_list *list, uint8_t red,
                               uint8_t green, uint8_t blue) {
  push_format(list, "38;2;%d;%d;%d", red, green, blue);
}

void command_list_set_index_color_bg(struct command_list *list,
                                     uint8_t color_idx) {
  if (color_idx < 8) {
    push_format(list, "%d", 40 + color_idx);
  } else if (color_idx < 16) {
    push_format(list, "%d", 100 + color_idx - 8);
  } else {
    push_format(list, "48;5;%d", color_idx);
  }
}

void command_list_set_color_bg(struct command_list *list, uint8_t red,
                               uint8_t green, uint8_t blue) {
  push_format(list, "48;2;%d;%d;%d", red, green, blue);
}

void command_list_reset_color(struct command_list *list) { reset_format(list); }

void display_render(struct display *display,
                    struct command_list *command_list) {

  struct command_list *cl = command_list;

  for (uint64_t cmdi = 0; cmdi < cl->ncmds; ++cmdi) {
    struct render_command *cmd = &cl->cmds[cmdi];
    display_move_cursor(display, cmd->row, cmd->col);
    put_format(cmd->fmt, cmd->fmt_len);
    putbytes(cmd->data, cmd->len);
    delete_to_eol();
  }
}

void display_begin_render(struct display *display) {}
void display_end_render(struct display *display) { fflush(stdout); }