File: gl_emul.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (750 lines) | stat: -rw-r--r-- 14,552 bytes parent folder | download | duplicates (16)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/* Copyright (C) 2002 Andrew Apted <ajapted@users.sourceforge.net> */
/* NetHack may be freely redistributed.  See license for details.  */

/*
 * SDL/GL window port for NetHack & Slash'EM.
 *
 * Simple terminal emulation.  Puts(), Putc(), Gotoxy(), etc etc.
 * Uses TileWindow for the output.  NOTE that the write_y member of
 * TextWindow goes from the bottom up, i.e. 0 is the lowest line.
 * Also handles line input stuff (including extended commands).
 */

#include "hack.h"

#if defined(GL_GRAPHICS) || defined(SDL_GRAPHICS)

#define WINGL_INTERNAL
#include "winGL.h"
#include "func_tab.h"

#include <string.h>
#include <ctype.h>


extern int NDECL(extcmd_via_menu);

#define HEIGHT_ASKNAME  1
#define DEPTH_ASKNAME   8

#define MAX_NAME_LEN     20
#define MAX_EXT_CMD_LEN  20


void sdlgl_emul_startup(void)
{
  /* does nothing */
}

void sdlgl_emul_shutdown(void)
{
  /* does nothing */
}

void Sdlgl_start_screen(void)
{
  /* does nothing (needed for interface) */
}

void Sdlgl_end_screen(void)
{
  /* does nothing (needed for interface) */
}

struct TextWindow *sdlgl_new_textwin(int type)
{
  struct TextWindow *win;

  win = (struct TextWindow *) alloc(sizeof(struct TextWindow));
  memset(win, 0, sizeof(struct TextWindow));

  win->type = type;
  win->is_menu = -1;
  win->base = NULL;
  win->write_x = win->write_y = 0;
  win->write_cursor = 0;
  win->write_col = win->fill_col = (BLACK<<8) + L_GREY;
  win->scrollback = NULL;

  return win;
}

void sdlgl_free_textwin(struct TextWindow *win)
{
  if (win->base)
  {
    sdlgl_free_tilewin(win->base);
    win->base = NULL;
  }

  free(win);
}


/* ---------------------------------------------------------------- */

static void copy_line_to_scrollback(struct TextWindow *win, int y)
{
  int x, tw;

  struct TileWindow *front = win->base;
  struct TileWindow *back  = win->scrollback;
  
  assert(front && back);

  tw = front->total_w;
  assert(back->total_w == tw);
  assert(0 <= y && y < front->total_h);

  /* ignore empty lines */
  
  for (x=0; x < tw; x++)
    if (front->tiles[y * tw + x].fg != TILE_EMPTY &&
        front->tiles[y * tw + x].fg != CHAR_2_TILE(' '))
      break;

  if (x >= tw)
    return;

  /* scroll up the scroll back */
  
  sdlgl_copy_area(back, 0, 0, tw, back->total_h - 1, 0, 1);
   
  sdlgl_transfer_area(front, 0, y, tw, 1, back, 0, 0);

  if (win->scrollback_size < back->total_h)
    win->scrollback_size += 1;
}

static void do_scroll_up(struct TextWindow *win)
{
  int tw = win->base->total_w;
  int th = win->base->total_h;

  if (win->scrollback && win->scrollback_enable)
    copy_line_to_scrollback(win, th - 1);
   
  /* copy lines upward */
  sdlgl_copy_area(win->base, 0, 0, tw, th - 1, 0, 1);
      
  /* blank out bottom line */
  sdlgl_blank_area(win->base, 0, 0, tw, 1);
}

static void do_wrapping(struct TextWindow *win)
{
  int tw = win->base->total_w;
  int th = win->base->total_h;

  if (win->write_x < 0)
  {
    if (win->write_y >= th - 1)
      win->write_x = 0;
    else
    {
      win->write_x = tw - 1;
      win->write_y += 1;
    }
  }
  
  if (win->write_y >= th)
    win->write_y = th - 1;

  if (win->write_x >= tw)
  {
    win->write_x = 0; 
    win->write_y -= 1;
  }

  for (; win->write_y < 0; win->write_y += 1)
  {
    do_scroll_up(win);
  }

  if (win->write_cursor)
    sdlgl_set_cursor(win->base, win->write_x, win->write_y, 1);
}

static void do_putc(struct TextWindow *win, char t)
{
  int x = win->write_x;
  int y = win->write_y;
  
  int tw = win->base->total_w;
  int th = win->base->total_h;

  assert(0 <= x && x < tw);
  assert(0 <= y && y < th);

  sdlgl_store_char(win->base, x, y, t, win->write_col);

  win->write_x += 1;
  do_wrapping(win);
}

void sdlgl_putc(struct TextWindow *win, int c)
{
  /* make sure character is in range */
  c = ((unsigned int) c) & 0xFF;
   
  assert(c >= 0);

  /* handle special cases */
  switch (c)
  {
    case 0:    /* NUL: do nothing */
      break;
      
    case C('g'):   /* ^G: bel */
    {
      Sdlgl_nhbell();
      break;
    }

    case C('h'):   /* ^H: backspace */
    {
      sdlgl_backsp(win);
      break;
    }

    case C('i'):   /* ^I: tab */
    {
      int num;
      for (num=8 - (win->write_x & 7); num > 0; num--)
        do_putc(win, ' ');
      break;
    }

    case C('j'):  /* ^J: LF */
    {
      win->write_x = 0;
      win->write_y -= 1;
      do_wrapping(win);
      break;
    }

    case C('l'):  /* ^L: clear */
    {
      int y;

      for (y=0; y < win->show_h + 1; y++)
        sdlgl_putc(win, '\n');

      sdlgl_home(win);
      break;
    }

    case C('m'):  /* ^M: CR */
    {
      win->write_x = 0; 
      break;
    }

    /* not special ?  Just display the tile normally */

    default:
      do_putc(win, c);
      break;
  }

  if (win->write_cursor && win->base)
    sdlgl_set_cursor(win->base, win->write_x, win->write_y, 1);
}

void sdlgl_puts(struct TextWindow *win, const char *s)
{
  for (; *s; s++)
    sdlgl_putc(win, (int) (unsigned char) *s);
}

void sdlgl_puts_nolf(struct TextWindow *win, const char *s)
{
  for (; *s; s++)
    if (*s != '\n' && *s != '\r')
      sdlgl_putc(win, (int) (unsigned char) *s);
}


/* ---------------------------------------------------------------- */

void sdlgl_gotoxy(struct TextWindow *win, int x, int y)
{
  int tw = win->base->total_w;
  int th = win->base->total_h;

  if (x < 0)   x = 0;
  if (x >= tw) x = tw - 1;

  if (y < 0)   y = 0;
  if (y >= th) y = th - 1;
  
  win->write_x = x;
  win->write_y = th - 1 - y;

  if (win->write_cursor)
    sdlgl_set_cursor(win->base, win->write_x, win->write_y, 1);
}

void sdlgl_home(struct TextWindow *win)
{
  sdlgl_gotoxy(win, 0, 0);
}

void sdlgl_backsp(struct TextWindow *win)
{
  win->write_x -= 1;
  do_wrapping(win);
}

/* Note: none of these clear routines affects the write position.
 */
void sdlgl_clear_end(struct TextWindow *win)
{
  int x = win->write_x;
  int y = win->write_y;

  int len = win->base->total_w - x;

  if (len > 0)
    sdlgl_blank_area(win->base, x, y, len, 1);
}

void sdlgl_clear_eos(struct TextWindow *win)
{
  int y;

  int bu_x = win->write_x;
  int bu_y = win->write_y;

  /* handle current line */
  sdlgl_clear_end(win);

  /* handle the remaining lines underneath */
  for (y=0; y < bu_y; y++)
  {
    win->write_x = 0;
    win->write_y = y;
    
    sdlgl_clear_end(win);
  }

  win->write_x = bu_x;
  win->write_y = bu_y;
}

void sdlgl_clear(struct TextWindow *win)
{
  int bu_x = win->write_x;
  int bu_y = win->write_y;

  sdlgl_home(win);    
  sdlgl_clear_eos(win);

  win->write_x = bu_x;
  win->write_y = bu_y;
}

void sdlgl_enable_cursor(struct TextWindow *win, int enable)
{
  assert(win->base);

  win->write_cursor = enable;
  win->base->curs_block = 1;
  win->base->curs_color = CURSOR_COL;

  if (enable)
  {
    sdlgl_set_cursor(win->base, win->write_x, win->write_y, 1);
  }
  else
  {
    sdlgl_set_cursor(win->base, -1, -1, 1);
  }
}


/* ---------------------------------------------------------------- */

static void do_getlin(struct TextWindow *win, const char *query, 
    char *bufp, int is_name)
{
  int max = BUFSZ-1;
  int len = 0;
  int ch;

  sdlgl_gotoxy(win, 0, 0);

  /* show the prompt */
  sdlgl_puts(win, query);
  sdlgl_putc(win, ' ');

  sdlgl_enable_cursor(win, 1);

  /* loop invariant: bufp[len] == 0 */
  bufp[0] = 0;
  
  for (;;)
  {
    sdlgl_flush();
    ch = sdlgl_get_key(POSKEY_ALLOW_REPEAT);

    if (ch == C('p'))
    {
      Sdlgl_doprev_message();
      continue;
    }

    if (sdlgl_alt_prev)
      sdlgl_remove_scrollback();

    if (ch == '\033')  /* escape key ? */
    {
      strcpy(bufp, "\033");
      break;
    }

    if (ch == '\b')  /* backspace ? */
    {
      if (len > 0)
      {
        bufp[--len] = 0;
        sdlgl_puts(win, "\b \b");
      }
      continue;
    }

    if (ch == '\n' || ch == '\r')  /* return ? */
    {
      if (! is_name || len > 0)
        break;
    }

    /* for name input, limit characters to letters + a few symbols.
     */
    if (is_name && !(letter(ch) || ch == '_' ||
        ch == '-' || ch == '@'))
      continue;
    else if (is_name && len >= MAX_NAME_LEN)
      continue;

    if (' ' <= ch && ch <= '~')
    {
      if (len < max)
      {
        bufp[len++] = ch;
        bufp[len] = 0;
        sdlgl_putc(win, ch);
      }
      continue;
    }

    /* key unacceptable.  beep ? */
  }

  /* clean up after ourselves */
  sdlgl_enable_cursor(win, 0);

  Sdlgl_clear_nhwindow(WIN_MESSAGE);
}

/*
 * Read a line closed with '\n' into the array char bufp[BUFSZ].
 * The '\n' is not stored. The string is closed with a '\0'.
 * Reading can be interrupted by an escape ('\033') - now the
 * resulting string is "\033".
 */
void Sdlgl_getlin(const char *query, char *bufp)
{
  struct TextWindow *win;

  if (WIN_MESSAGE == WIN_ERR)
  {
    strcpy(bufp, "\033");
    return;
  }

  win = text_wins[WIN_MESSAGE];
  assert(win);
  assert(win->base);

  if (sdlgl_alt_prev)
    sdlgl_remove_scrollback();

  if (win->fresh_lines > 0)
    sdlgl_more(win);

  Sdlgl_clear_nhwindow(WIN_MESSAGE);

  /* disable scrollback while we control the message win */
  win->scrollback_enable = 0;
  
  do_getlin(win, query, bufp, 0);

  win->scrollback_enable = 1;
}

/* ---------------------------------------------------------------- */


#define VALID_EXT_CH(ch)  ('a' <= (ch) && (ch) <= 'z')

/* fills in `comp_tex' with an "[abcd]" completion string.  If there
 * was a single match, returns the extcmdlist index for it, otherwise
 * returns -1.
 */
static int make_completion_text(const char *word, int len,
    char *comp_tex)
{
  int i, pos, last = -1;
  int letters[26] = { 0, };
  int matches;

  /* special case for zero length: use stock string */
  if (len == 0)
  {
    strcpy(comp_tex, "[a-z?]");
    return -1;
  }
  
  /* default to empty string */
  comp_tex[0] = 0;
    
  for (i=matches=0; extcmdlist[i].ef_txt; i++)
  {
    const char *cmd = extcmdlist[i].ef_txt;

    if (! VALID_EXT_CH(cmd[0]))
      continue;

    if (strlen(cmd) <= len)
      continue;

    if (strncmp(word, cmd, len) != 0)
      continue;

    if (! VALID_EXT_CH(cmd[len]))
      continue;

    letters[cmd[len] - 'a'] = 1;
    matches++;
    last = i;
  }
  
  if (matches == 0)
    return -1;
  else if (matches == 1)
    return last;

  /* build match list string */
  pos = 0;
  
  comp_tex[pos++] = '[';

  for (i=0; i < 26; i++)
    if (letters[i])
      comp_tex[pos++] = 'a' + i;

  comp_tex[pos++] = ']';
  comp_tex[pos] = 0;

  return -1;
}

/* -AJA- I think it's cleaner to have a whole new routine rather than
 *       try to shoe-horn the extended command completion stuff into
 *       the existing do_getlin() routine.
 */
static int do_get_ext_cmd(struct TextWindow *win)
{
  char buffer[BUFSZ + 1 /* for NUL */];
  char comp_buf[BUFSZ];

  int len = 0;
  int i, ch;
  int use_menu = 0;

  sdlgl_enable_cursor(win, 1);
  win->write_col = L_GREY;

  /* loop invariant: buffer[len] == 0 */
  buffer[0] = 0;
  comp_buf[0] = 0;
  
  (void) make_completion_text(buffer, len, comp_buf);

  for (;;)
  {
    /* redraw the line, then flush */
    sdlgl_home(win);
    sdlgl_clear_end(win);

    sdlgl_putc(win, '#');
    sdlgl_putc(win, ' ');
    sdlgl_puts(win, buffer); 
    sdlgl_puts(win, comp_buf);

    sdlgl_flush();
    
    /* process a key */
    ch = sdlgl_get_key(POSKEY_ALLOW_REPEAT);

    if (ch == C('p'))
    {
      Sdlgl_doprev_message();
      continue;
    }

    if (sdlgl_alt_prev)
      sdlgl_remove_scrollback();

    if (ch == '\033')  /* escape key ? */
    {
      len = 0;
      break;
    }

    if (ch == '\n' || ch == '\r')  /* return ? */
        break;

    if (ch == '\b')  /* backspace ? */
    {
      if (len == 0)
        break;

      assert(len > 0);

      buffer[--len] = 0;

      (void) make_completion_text(buffer, len, comp_buf);
      continue;
    }

    if (ch == '?' && len == 0)
    {
      len = 0;
      use_menu = 1;
      break;
    }
    
    /* only lowercase letters please */
    if (! VALID_EXT_CH(ch) || len >= MAX_EXT_CMD_LEN)
    {
      /* beep ? */
      continue;
    }

    buffer[len++] = ch;
    buffer[len] = 0;

    /* do completion */
    i = make_completion_text(buffer, len, comp_buf);

    if (i >= 0)
    {
      strcpy(buffer, extcmdlist[i].ef_txt);
      len = strlen(buffer);
    }
  }

  sdlgl_enable_cursor(win, 0);
  sdlgl_clear(win);
  sdlgl_home(win);

  if (use_menu)
    return extcmd_via_menu();

  if (len == 0)
    return -1;

  /* find command, show message if not found */
  for (i=0; extcmdlist[i].ef_txt; i++)
    if (strcmp(extcmdlist[i].ef_txt, buffer) == 0)
      return i;

  pline("%s: unknown extended command.", buffer);
  return -1;
}

/*
 * Read in an extended command, doing command line completion.  We
 * stop when we have found enough characters to make a unique command.
 */
int Sdlgl_get_ext_cmd(void)
{
  struct TextWindow *win;

  int cmd;

#if 0
  if (iflags.extmenu)
    return extcmd_via_menu();
#endif
   
  if (WIN_MESSAGE == WIN_ERR)
    return -1;

  win = text_wins[WIN_MESSAGE];
  assert(win);
  assert(win->base);

  if (sdlgl_alt_prev)
    sdlgl_remove_scrollback();

  if (win->fresh_lines > 0)
    sdlgl_more(win);

  Sdlgl_clear_nhwindow(WIN_MESSAGE);

  /* disable scrollback while we control the message win */
  win->scrollback_enable = 0;
  
  cmd = do_get_ext_cmd(win);

  win->scrollback_enable = 1;

  return cmd;
}

/*
 * plname is filled either by an option (-u Player  or  -uPlayer) or
 * explicitly (by being the wizard) or by askname.
 * It may still contain a suffix denoting the role, etc.
 * Always called after init_nhwindows() and before display_gamewindows().
 */
void Sdlgl_askname(void)
{
  struct TextWindow *win;
  int pixel_h;

  char buffer[BUFSZ];

  /* create text & tile windows */
  win = sdlgl_new_textwin(NHW_TEXT);  /* type unimportant */
   
  win->show_w = sdlgl_width / sdlgl_font_message->tile_w;
  win->show_h = HEIGHT_ASKNAME;

  pixel_h = sdlgl_font_message->tile_h * win->show_h;
  
  win->base = sdlgl_new_tilewin(sdlgl_font_message, win->show_w, 
      win->show_h, 1,0);
        
  sdlgl_map_tilewin(win->base, 0, sdlgl_height - pixel_h,
      sdlgl_width, pixel_h, DEPTH_ASKNAME);

  /* do the query */
  do_getlin(win, "Who are you?", buffer, 1);

  sdlgl_unmap_tilewin(win->base);
  sdlgl_free_textwin(win);

  if (buffer[0] == '\033')
    sdlgl_error("Quit from who-are-you prompt.\n");

  strncpy(plname, buffer, sizeof(plname) - 1);
  plname[sizeof(plname) - 1] = 0;
}


#endif /* GL_GRAPHICS */
/*gl_emul.c*/