Subversion Repositories SvarDOS

Rev

Rev 1276 | Rev 1283 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1275 mateusz.vi 1
/* */
2
 
1282 mateusz.vi 3
#include <dos.h>
4
#include <fcntl.h>
1275 mateusz.vi 5
#include <stdlib.h>
6
#include <string.h>
7
 
8
#include "mdr\cout.h"
9
#include "mdr\keyb.h"
10
 
1282 mateusz.vi 11
#include "svarlang\svarlang.h"
12
 
1275 mateusz.vi 13
#define COL_TXT        0
14
#define COL_STATUSBAR1 1
15
#define COL_STATUSBAR2 2
16
#define COL_SCROLLBAR  3
17
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
18
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
19
 
20
#define SCROLL_CURSOR 0xB1
21
 
22
 
23
struct line {
24
  struct line *prev;
25
  struct line *next;
26
  unsigned short len;
27
  char payload[1];
28
};
29
 
30
struct linedb {
31
  struct line *topscreen;
32
  struct line *cursor;
33
  unsigned short xoffset;
34
};
35
 
36
 
37
void line_add(struct linedb *db, const char *line) {
38
  unsigned short slen = strlen(line);
39
  struct line *l;
40
 
41
  /* trim out CR/LF line endings */
42
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
43
    slen -= 2;
44
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
45
    slen--;
46
  }
47
 
48
  l =  calloc(1, sizeof(struct line) + slen + 1);
49
  l->prev = db->cursor;
50
  if (db->cursor) {
51
    l->next = db->cursor->next;
52
    db->cursor->next = l;
53
    l->next->prev = l;
54
  }
55
  db->cursor = l;
56
  memcpy(l->payload, line, slen);
57
  l->len = slen;
58
}
59
 
60
 
61
void db_rewind(struct linedb *db) {
62
  if (db->cursor == NULL) return;
63
  while (db->cursor->prev) db->cursor = db->cursor->prev;
64
  db->topscreen = db->cursor;
65
}
66
 
67
 
68
void load_colorscheme(void) {
69
  scheme[COL_TXT] = 0x17;
70
  scheme[COL_STATUSBAR1] = 0x70;
71
  scheme[COL_STATUSBAR2] = 0x78;
72
  scheme[COL_SCROLLBAR] = 0x70;
73
}
74
 
75
 
1282 mateusz.vi 76
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
1275 mateusz.vi 77
  unsigned char i;
78
  const char *s = "HELP";
79
  unsigned char helpcol = screenw - (strlen(s) + 4);
80
 
81
  /* clear screen */
82
  mdr_cout_cls(scheme[COL_TXT]);
83
 
84
  /* status bar */
85
  for (i = 0; i < helpcol; i++) {
86
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
87
    if (*fname != 0) fname++;
88
  }
89
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
90
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
91
 
92
  /* scroll bar */
93
  for (i = 0; i < (screenh - 1); i++) {
94
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
95
  }
96
}
97
 
98
 
1282 mateusz.vi 99
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
1275 mateusz.vi 100
  unsigned char y = 0;
101
  unsigned char len;
102
  struct line *l;
103
 
1282 mateusz.vi 104
  /* DEBUG TODO FIXME */
105
  static char m = 'a';
106
  m++;
107
  if (m > 'z') m = 'a';
108
 
1275 mateusz.vi 109
  for (l = db->topscreen; l != NULL; l = l->next) {
1282 mateusz.vi 110
 
111
    /* skip lines that do not to be refreshed */
112
    if ((y < uidirtyfrom) || (y > uidirtyto)) continue;
113
 
1275 mateusz.vi 114
    if (db->xoffset < l->len) {
115
      len = mdr_cout_str(y, 0, l->payload + db->xoffset, scheme[COL_TXT], screenw - 1);
116
    } else {
117
      len = 0;
118
    }
119
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
1282 mateusz.vi 120
 
121
    /* FIXME DEBUG */
122
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
123
 
1275 mateusz.vi 124
    if (y == screenh - 2) break;
125
    y++;
126
  }
127
}
128
 
129
 
1282 mateusz.vi 130
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 131
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
132
 
133
  if (db->cursor->len < db->xoffset) {
134
    *cursorpos = 0;
135
    db->xoffset = db->cursor->len;
1282 mateusz.vi 136
    *uidirtyfrom = 0;
137
    *uidirtyto = 0xff;
1275 mateusz.vi 138
  } else {
139
    *cursorpos = db->cursor->len - db->xoffset;
140
  }
141
}
142
 
143
 
1282 mateusz.vi 144
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 145
  if (db->cursor->prev != NULL) {
146
    db->cursor = db->cursor->prev;
147
    if (*cursorposy == 0) {
148
      db->topscreen = db->cursor;
1282 mateusz.vi 149
      *uidirtyfrom = 0;
150
      *uidirtyto = 0xff;
1275 mateusz.vi 151
    } else {
152
      *cursorposy -= 1;
153
    }
154
  }
155
}
156
 
157
 
1282 mateusz.vi 158
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1275 mateusz.vi 159
  /* adjust xoffset to make sure eol is visible on screen */
1282 mateusz.vi 160
  if (db->xoffset > db->cursor->len) {
161
    db->xoffset = db->cursor->len - 1;
162
    *uidirtyfrom = 0;
163
    *uidirtyto = 0xff;
164
  }
165
 
166
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
167
    db->xoffset = db->cursor->len - screenw + 2;
168
    *uidirtyfrom = 0;
169
    *uidirtyto = 0xff;
170
  }
1275 mateusz.vi 171
  *cursorposx = db->cursor->len - db->xoffset;
172
}
173
 
174
 
1282 mateusz.vi 175
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 176
  if (db->cursor->next != NULL) {
177
    db->cursor = db->cursor->next;
178
    if (*cursorposy < screenh - 2) {
179
      *cursorposy += 1;
180
    } else {
181
      db->topscreen = db->topscreen->next;
1282 mateusz.vi 182
      *uidirtyfrom = 0;
183
      *uidirtyto = 0xff;
1276 mateusz.vi 184
    }
185
  }
186
}
187
 
188
 
1282 mateusz.vi 189
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
1276 mateusz.vi 190
  *cursorposx = 0;
1282 mateusz.vi 191
  if (db->xoffset != 0) {
192
    db->xoffset = 0;
193
    *uidirtyfrom = 0;
194
    *uidirtyto = 0xff;
195
  }
1276 mateusz.vi 196
}
197
 
198
 
1275 mateusz.vi 199
int main(int argc, char **argv) {
1282 mateusz.vi 200
  int fd;
1275 mateusz.vi 201
  const char *fname = NULL;
202
  char buff[1024];
203
  struct linedb db;
204
  unsigned char screenw = 0, screenh = 0;
205
  unsigned char cursorposx = 0, cursorposy = 0;
1282 mateusz.vi 206
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
1275 mateusz.vi 207
 
208
  bzero(&db, sizeof(db));
209
 
1282 mateusz.vi 210
  svarlang_autoload_nlspath("sved");
211
 
1275 mateusz.vi 212
  if ((argc != 2) || (argv[1][0] == '/')) {
213
    mdr_coutraw_puts("usage: sved file.txt");
214
    return(0);
215
  }
216
 
217
  fname = argv[1];
218
 
219
  mdr_coutraw_puts("");
220
 
1282 mateusz.vi 221
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
222
    mdr_coutraw_puts("Failed to open file:");
223
    mdr_coutraw_puts(fname);
1275 mateusz.vi 224
    return(1);
225
  }
226
 
1282 mateusz.vi 227
  /* load file */
228
  {
229
    unsigned int prevlen = 0, len, llen;
230
 
231
    do {
232
      if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) != 0) break;
233
      len += prevlen;
234
 
235
      /* look for nearest \n and replace with 0*/
236
      for (llen = 0; buff[llen] != '\n'; llen++) {
237
        if (llen == sizeof(buff)) break;
238
      }
239
      buff[llen] = 0;
240
      if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
241
      line_add(&db, buff);
242
 
243
      len -= llen + 1;
244
      memmove(buff, buff + llen + 1, len);
245
      prevlen = len;
246
    } while (len > 0);
247
 
1275 mateusz.vi 248
  }
1282 mateusz.vi 249
 
1275 mateusz.vi 250
  /* add an empty line at end if not present already */
251
  if (db.cursor->len != 0) line_add(&db, "");
252
 
1282 mateusz.vi 253
  _dos_close(fd);
1275 mateusz.vi 254
 
255
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
256
  ui_basic(screenw, screenh, fname);
257
 
258
  db_rewind(&db);
259
 
260
  for (;;) {
261
    int k;
262
 
1282 mateusz.vi 263
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 264
    mdr_cout_locate(cursorposy, cursorposx);
265
 
1282 mateusz.vi 266
    if (uidirtyfrom != 0xff) {
267
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
268
      uidirtyfrom = 0xff;
269
    }
1275 mateusz.vi 270
 
271
    k = keyb_getkey();
1282 mateusz.vi 272
 
1275 mateusz.vi 273
    if (k == 0x150) { /* down */
1282 mateusz.vi 274
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 275
 
276
    } else if (k == 0x148) { /* up */
1282 mateusz.vi 277
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 278
 
279
    } else if (k == 0x14D) { /* right */
280
      if (db.cursor->len > db.xoffset + cursorposx) {
281
        if (cursorposx < screenw - 2) {
282
          cursorposx++;
283
        } else {
284
          db.xoffset++;
1282 mateusz.vi 285
          uidirtyfrom = 0;
286
          uidirtyto = 0xff;
1275 mateusz.vi 287
        }
1276 mateusz.vi 288
      } else {
1282 mateusz.vi 289
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
290
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 291
      }
292
 
293
    } else if (k == 0x14B) { /* left */
294
      if (cursorposx > 0) {
295
        cursorposx--;
296
      } else if (db.xoffset > 0) {
297
        db.xoffset--;
1282 mateusz.vi 298
        uidirtyfrom = 0;
299
        uidirtyto = 0xff;
1276 mateusz.vi 300
      } else if (db.cursor->prev != NULL) { /* jump to end of line above */
1282 mateusz.vi 301
        cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
302
        cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
1275 mateusz.vi 303
      }
304
 
1282 mateusz.vi 305
    } else if (k == 0x149) { /* pgup */
306
      // TODO
307
 
308
    } else if (k == 0x151) { /* pgdown */
309
      // TODO
310
 
311
    } else if (k == 0x147) { /* home */
312
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
313
 
314
    } else if (k == 0x14F) { /* end */
315
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
316
 
1275 mateusz.vi 317
    } else if (k == 0x1B) { /* ESC */
318
      break;
319
 
1282 mateusz.vi 320
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
321
      char buff[4];
322
      const char *HEX = "0123456789ABCDEF";
323
      buff[0] = HEX[(k >> 8) & 15];
324
      buff[1] = HEX[(k >> 4) & 15];
325
      buff[2] = HEX[k & 15];
326
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
327
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
1275 mateusz.vi 328
      keyb_getkey();
329
      break;
330
    }
331
  }
332
 
333
  mdr_cout_close();
334
 
335
  /* TODO free memory */
336
 
337
  return(0);
338
}