summaryrefslogtreecommitdiff
path: root/userland/ante/ante.c
blob: 753e7f2eaafa573b5a459d762fbadcbb64726141 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <assert.h>
#include <fcntl.h>
#include <libgui.h>
#include <poll.h>
#include <pty.h>
#include <socket.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BACKGROUND_COLOR 0x000000

int file_fd;
char *file_buffer;
uint64_t file_buffer_size;
uint64_t file_size = 0;
uint64_t file_real_position = 0;
GUI_Window *global_w;

typedef enum {
  NORMAL,
  INSERT,
  COMMAND,
} editor_mode_t;

editor_mode_t current_mode = NORMAL;
uint32_t cursor_pos_x = 0;
uint64_t cursor_pos_y = 0;
void draw_file(void);
void write_file(void);

int clamp(int *x, int *y) {
  int clamped = 0;
  if (*x < 0) {
    *x = 0;
    clamped = 1;
  }
  if (*y < 0) {
    *y = 0;
    clamped = 1;
  }
  if (*x + 8 > global_w->sx) {
    *x = (global_w->sx - 8) / 8;
    clamped = 1;
  }
  if (*y + 8 > global_w->sy) {
    *y = (global_w->sy - 8) / 8;
    clamped = 1;
  }
  return clamped;
}

void insert_char(int pos, char c) {
  if (0 == file_size) {
    file_size++;
    file_buffer[0] = c;
    return;
  }

  // Shift the buffer at 'pos' to the right by one.
  memmove(file_buffer + pos + 1, file_buffer + pos, file_size - pos);

  file_buffer[pos] = c;
  file_size++;
}

void delete_char(int pos, int *deleted_newline) {
  if (0 == pos) {
    return;
  }
  // Delete the characther at 'pos'. This makes sense if 'pos' is
  // at the end of the file and as a result the shift operation done
  // later in the function is not overwritting the characther.
  if ('\n' == file_buffer[pos - 1])
    *deleted_newline = 1;
  file_buffer[pos - 1] = 0;

  // Shift the buffer at 'pos' to the left by one
  memmove(file_buffer + pos - 1, file_buffer + pos, file_size - pos);
  file_size--;
}

uint64_t cursor_to_real(int x, int y, int *is_valid) {
  *is_valid = 0;
  if (clamp(&x, &y))
    return file_real_position;
  if (0 == file_size) {
    if (0 == x && 0 == y) {
      *is_valid = 1;
      return 0;
    }
    return 0;
  }
  uint64_t p = 0;
  int cx = 0;
  int cy = 0;
  for (; p < file_size; p++) {
    if (cx == x && cy == y) {
      *is_valid = 1;
      break;
    }
    if ('\n' == file_buffer[p]) {
      cx = 0;
      cy++;
      continue;
    }
    cx++;
  }
  if (*is_valid)
    return p;
  else
    return file_real_position;
}

void key_event(char c) {
  int x = 0;
  int y = 0;
  if (COMMAND == current_mode) {
    if ('w' == c) {
      printf("wrote to file\n");
      write_file();
      current_mode = NORMAL;
      return;
    }
    if ('q' == c) {
      close(file_fd);
      close(global_w->ws_socket);
      current_mode = NORMAL;
      exit(0);
      return;
    }
    return;
  } else if (NORMAL == current_mode) {
    switch (c) {
    case 'j':
      y++;
      break;
    case 'k':
      y--;
      break;
    case 'l':
      x++;
      break;
    case 'h':
      x--;
      break;
    case 'c':
      printf("entering command mode\n");
      current_mode = COMMAND;
      return;
      break;
    case 'i':
      current_mode = INSERT;
      return;
      break;
    default:
      return;
    }
  } else {
    if ('\x1B' == c) {
      current_mode = NORMAL;
      return;
    }
    if ('\b' == c) {
      int deleted_newline = 0;
      delete_char(file_real_position, &deleted_newline);
      if (deleted_newline) {
        y--;
      } else {
        x--;
      }
      GUI_ClearScreen(global_w, BACKGROUND_COLOR);
    } else {
      insert_char(file_real_position, c);
      printf("inserting char: %c\n", c);
      if ('\n' == c) {
        cursor_pos_x = 0;
        y++;
      } else {
        x++;
      }
      GUI_ClearScreen(global_w, BACKGROUND_COLOR);
    }
  }

  int is_valid_position = 0;
  file_real_position =
      cursor_to_real(cursor_pos_x + x, cursor_pos_y + y, &is_valid_position);
  if (!is_valid_position) {
    return;
  }
  GUI_OverwriteFont(global_w, cursor_pos_x * 8, cursor_pos_y * 8,
                    BACKGROUND_COLOR);
  draw_file();
  cursor_pos_x += x;
  cursor_pos_y += y;
  GUI_OverwriteFont(global_w, cursor_pos_x * 8, cursor_pos_y * 8, 0xFFFFFF);

  GUI_UpdateWindow(global_w);
}

void draw_file(void) {
  assert(file_buffer);
  uint32_t screen_pos_x = 0;
  uint64_t screen_pos_y = 0;
  for (uint64_t i = 0; i < file_size; i++) {
    char c = file_buffer[i];
    if ('\n' == c) {
      screen_pos_x = 0;
      screen_pos_y += 8;
      continue;
    }
    GUI_DrawFont(global_w, screen_pos_x, screen_pos_y, c);
    screen_pos_x += 8;
  }
}

void write_file(void) {
  assert(file_buffer);
  pwrite(file_fd, file_buffer, file_size, 0);
}

int open_file(const char *file) {
  file_fd = open(file, O_RDWR, O_CREAT);
  printf("file_fd: %d\n", file_fd);
  if (0 > file_fd) {
    perror("open");
    return 0;
  }

  file_buffer_size = 0x1000;
  file_buffer = malloc(file_buffer_size);
  uint64_t index = 0;
  assert(file_buffer);
  for (;;) {
    char buffer[4096];
    int rc = read(file_fd, buffer, 4096);
    if (-1 == rc) {
      perror("read");
      assert(0);
    }

    if (0 == rc)
      break;

    file_size += rc;
    if (file_size > file_buffer_size) {
      file_buffer_size = file_size;
      file_buffer = realloc(file_buffer, file_buffer_size);
      assert(file_buffer);
    }

    for (int i = 0; i < rc; i++, index++) {
      file_buffer[index] = buffer[i];
    }
  }
  return 1;
}

void run() {
  struct pollfd fds[1];
  fds[0].fd = global_w->ws_socket;
  fds[0].events = POLLIN;
  fds[0].revents = 0;
  for (;; fds[0].revents = 0) {
    poll(fds, 1, 0);
    if (fds[0].revents & POLLIN) {
      WS_EVENT e;
      int rc;
      if (0 >= (rc = read(global_w->ws_socket, &e, sizeof(e))))
        continue;
      if (0 == e.c)
        continue;
      key_event(e.c);
    }
  }
}

int main(int argc, char **argv) {
  if (argc < 2) {
    printf("File has to be specified.\n");
    return 1;
  }
  global_w = GUI_CreateWindow(10, 10, 150 * 4, 150 * 4);
  assert(global_w);
  GUI_ClearScreen(global_w, BACKGROUND_COLOR);
  GUI_UpdateWindow(global_w);

  assert(open_file(argv[1]));
  draw_file();
  run();
  return 0;
}