summaryrefslogtreecommitdiff
path: root/src/user/libterm/terminal.c
blob: 549194fa65841e0c1013631b2f05c8951cd3e963 (plain) (blame)
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
#include <libterm/command.h>

#include <knob/format.h>
#include <knob/block.h>
#include <knob/ipc.h>

_task_handle_t term_task;

void term_set_dimensions(uint32_t width, uint32_t height) {
  struct terminal_command cmd = {
    .kind = SET_DIMENSIONS,
    .as_coords = {
      .x = width,
      .y = height
    }
  };

  try_send_command(&cmd);
}

void term_get_dimensions(uint32_t *width, uint32_t *height) {
  struct terminal_command cmd = {
    .kind = GET_DIMENSIONS
  };

  if (try_send_command(&cmd)) {
    union terminal_response rs;
    if (try_get_response(&rs)) {
      *width = rs.as_coords.x;
      *height = rs.as_coords.y;
    }
  }
}

void term_paint() {
  struct terminal_command cmd = {
    .kind = PAINT
  };

  try_send_command(&cmd);
}

void term_clear() {
  struct terminal_command cmd = {
    .kind = CLEAR
  };

  try_send_command(&cmd);
}

void term_set_color(_pixel_t fg, _pixel_t bg) {
  struct terminal_command cmd = {
    .kind = SET_COLOR,
    .as_color = {
      .fg = fg,
      .bg = bg
    }
  };

  try_send_command(&cmd);
}

void term_set_cursor(uint32_t new_y, uint32_t new_x) {
  struct terminal_command cmd = {
    .kind = SET_CURSOR,
    .as_coords = {
      .y = new_y,
      .x = new_x
    }
  };

  try_send_command(&cmd);
}

void term_cursor_left() {
  struct terminal_command cmd = {
    .kind = CURSOR_LEFT
  };

  try_send_command(&cmd);
}

void term_cursor_right() {
  struct terminal_command cmd = {
    .kind = CURSOR_RIGHT
  };

  try_send_command(&cmd);
}

void term_cursor_up() {
  struct terminal_command cmd = {
    .kind = CURSOR_UP
  };

  try_send_command(&cmd);
}

void term_cursor_down() {
  struct terminal_command cmd = {
    .kind = CURSOR_DOWN
  };

  try_send_command(&cmd);
}

void term_add_char(char ch) {
  struct terminal_command cmd = {
    .kind = ADD_CHAR,
    .as_char = ch
  };

  try_send_command(&cmd);
}

void term_add_sn_no_ww(const char *s, uint32_t n) {
  struct terminal_command cmd = {
    .kind = ADD_SN_NO_WORDWRAP,
    .as_uint = n
  };

  if (try_send_command(&cmd))
    try_send_ipc(term_task, s, n);
}

void term_add_sz_no_ww(const char *sz) {
  term_add_sn_no_ww(sz, strlen(sz));
}

void term_add_sz(const char *sz) {
  const uint32_t len = strlen(sz);

  struct terminal_command cmd = {
    .kind = ADD_SN,
    .as_uint = len
  };

  if (try_send_command(&cmd))
    try_send_ipc(term_task, sz, len);
}

void term_addf_no_ww_v(const char *fmt, va_list args) {
  char *const msg = format_v(fmt, args);
  term_add_sz_no_ww(msg);
  free_block(msg);
}

void term_addf_no_ww(const char *fmt, ...) {
  va_list args;
  va_start(args, fmt);
  term_addf_no_ww_v(fmt, args);
  va_end(args);
}

void term_addf_v(const char *fmt, va_list args) {
  char *const msg = format_v(fmt, args);
  term_add_sz(msg);
  free_block(msg);
}

void term_addf(const char *fmt, ...) {
  va_list args;
  va_start(args, fmt);
  term_addf_v(fmt, args);
  va_end(args);
}

struct key_packet term_get_key_blocking() {
  struct terminal_command cmd = {
    .kind = GET_KEY
  };

  try_send_command(&cmd);

  union terminal_response rs;
  try_get_response(&rs);

  return rs.as_key;
}