/[hurd]/hurd/console-client/ncursesw.c
ViewVC logotype

Diff of /hurd/console-client/ncursesw.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by marcus, Wed Sep 18 02:47:01 2002 UTC revision 1.3 by marcus, Fri Aug 15 21:07:31 2003 UTC
# Line 1  Line 1 
1  /* ncursesw.c - The ncursesw console driver.  /* ncursesw.c - The ncursesw console driver.
2     Copyright (C) 2002 Free Software Foundation, Inc.     Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3     Written by Marcus Brinkmann.     Written by Marcus Brinkmann.
4    
5     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
# Line 34  Line 34 
34    
35  /* ncurses is not thread-safe.  This lock protects all calls into the  /* ncurses is not thread-safe.  This lock protects all calls into the
36     ncurses library.  */     ncurses library.  */
37  struct mutex ncurses_lock;  static struct mutex ncurses_lock;
38    
39    /* The current width and height the ncursesw driver is using.  */
40    static int current_width;
41    static int current_height;
42    
43    /* The window on which the console is shown.  */
44    static WINDOW *conspad;
45    
46    /* The upper left corner shown in the pad.  */
47    static int padx;
48    static int pady;
49    
50    /* Autoscroll is on or off.  Autoscroll makes scrolling dependant on
51       the cursor position.  */
52    static int autoscroll;
53    
54  /* Forward declaration.  */  /* Forward declaration.  */
55  static struct display_ops ncursesw_display_ops;  static struct display_ops ncursesw_display_ops;
# Line 258  ucs4_to_altchar (wchar_t chr, chtype *ac Line 273  ucs4_to_altchar (wchar_t chr, chtype *ac
273    return 1;    return 1;
274  }  }
275    
276    static error_t
277    refresh_screen (void)
278    {
279      /* It is possible */
280      if (!current_width && !current_height)
281        return 0;
282      return prefresh (conspad, pady, padx, 0, 0,
283                       (current_height <= LINES ? current_height : LINES) - 1,
284                       (current_width <= COLS ? current_width : COLS) - 1);
285    }
286    
287  static any_t  static any_t
288  input_loop (any_t unused)  input_loop (any_t unused)
289  {  {
# Line 282  input_loop (any_t unused) Line 308  input_loop (any_t unused)
308            size_t size = 0;            size_t size = 0;
309    
310            mutex_lock (&ncurses_lock);            mutex_lock (&ncurses_lock);
311            while ((ret = getch ()) != ERR)            while ((ret = wgetch (conspad)) != ERR)
312              {              {
313                int i;                int i;
314                int found;                int found;
# Line 292  input_loop (any_t unused) Line 318  input_loop (any_t unused)
318                    switch (ret)                    switch (ret)
319                      {                      {
320                      case 'x':                      case 'x':
321                        endwin ();                        mutex_unlock (&ncurses_lock);
322                        exit (0);                        console_exit ();
323                        break;                        break;
324                      case 23:    /* ^W */                      case 23:    /* ^W */
325                        assert (size < 100);                        assert (size < 100);
# Line 313  input_loop (any_t unused) Line 339  input_loop (any_t unused)
339                        console_switch (1 + (ret - '1'), 0);                        console_switch (1 + (ret - '1'), 0);
340                        mutex_lock (&ncurses_lock);                        mutex_lock (&ncurses_lock);
341                        break;                        break;
342                        case 'j':
343                          /* Scroll pad to left.  */
344                          if (padx > 0)
345                            {
346                              padx--;
347                              refresh_screen ();
348                            }
349                          break;
350                        case 'k':
351                          /* Scroll pad down.  */
352                          if (pady < current_height - LINES)
353                            {
354                              pady++;
355                              refresh_screen ();
356                            }
357                          break;
358                        case 'l':
359                          /* Scroll pad to right.  */
360                          if (padx < current_width - COLS)
361                            {
362                              padx++;
363                              ncurses_refresh ();
364                            }  
365                          break;
366                        case 'i':
367                          /* Scroll pad up.  */
368                          if (pady > 0)
369                            {
370                              pady--;
371                              refresh_screen ();
372                            }
373                          break;
374                        case 'a':
375                          /* Switch autoscroll on/off.  */
376                          autoscroll = !autoscroll;
377                          break;
378                      default:                      default:
379                        break;                        break;
380                      }                      }
# Line 326  input_loop (any_t unused) Line 388  input_loop (any_t unused)
388                      break;                      break;
389                    default:                    default:
390                      found = 0;                      found = 0;
391                      for (i =0; i < sizeof(keycodes) / sizeof(keycodes[0]); i++)                      for (i = 0; i < sizeof (keycodes) / sizeof (keycodes[0]);
392                             i++)
393                        {                        {
394                          if (keycodes[i].curses == ret)                          if (keycodes[i].curses == ret)
395                            {                                {    
396                              if (keycodes[i].cons)                              if (keycodes[i].cons)
397                                {                                {
398                                  assert (size < 101 - strlen(keycodes[i].cons));                                  assert (size
399                                            < 101 - strlen (keycodes[i].cons));
400                                  strcpy (&buf[size], keycodes[i].cons);                                  strcpy (&buf[size], keycodes[i].cons);
401                                  size += strlen (keycodes[i].cons);                                  size += strlen (keycodes[i].cons);
402                                }                                }
# Line 382  mvwputsn (conchar_t *str, size_t len, of Line 446  mvwputsn (conchar_t *str, size_t len, of
446    attr_t attr = conchar_attr_to_attr (str->attr);    attr_t attr = conchar_attr_to_attr (str->attr);
447    short color_pair = conchar_attr_to_color_pair (str->attr);    short color_pair = conchar_attr_to_color_pair (str->attr);
448    
449    move (y, x);    wmove (conspad, y, x);
450    while (len)    while (len)
451      {      {
452        int ret;        int ret;
# Line 396  mvwputsn (conchar_t *str, size_t len, of Line 460  mvwputsn (conchar_t *str, size_t len, of
460          }          }
461    
462        if (ucs4_to_altchar (str->chr, &ac))        if (ucs4_to_altchar (str->chr, &ac))
463          addch (ac | attr | color_pair);          waddch (conspad, ac | attr | color_pair);
464        else        else
465          {                {      
466            wch[0] = str->chr;            wch[0] = str->chr;
# Line 409  mvwputsn (conchar_t *str, size_t len, of Line 473  mvwputsn (conchar_t *str, size_t len, of
473                assert (!"Do something if setcchar fails.");                assert (!"Do something if setcchar fails.");
474              }              }
475  #endif  #endif
476            ret = add_wch (&chr);            ret = wadd_wch (conspad, &chr);
477  #if 0  #if 0
478            if (ret == ERR)            if (ret == ERR)
479              {              {
# Line 429  static error_t Line 493  static error_t
493  ncursesw_update (void *handle)  ncursesw_update (void *handle)
494  {  {
495    mutex_lock (&ncurses_lock);    mutex_lock (&ncurses_lock);
496    refresh ();    refresh_screen ();
497    mutex_unlock (&ncurses_lock);    mutex_unlock (&ncurses_lock);
498    return 0;    return 0;
499  }  }
# Line 439  static error_t Line 503  static error_t
503  ncursesw_set_cursor_pos (void *handle, uint32_t col, uint32_t row)  ncursesw_set_cursor_pos (void *handle, uint32_t col, uint32_t row)
504  {  {
505    mutex_lock (&ncurses_lock);    mutex_lock (&ncurses_lock);
506    move (row, col);    assert (current_width && current_height);
507      if (autoscroll)
508        {
509          /* Autoscroll to the right.  */
510          if (col > COLS + padx)
511            {
512              padx += COLS / 2;
513              if (padx > COLS + current_width)
514                padx = current_width - COLS;
515              ncurses_refresh ();
516            }
517          /* Autoscroll to the left.  */
518          else if (col < padx)
519            {
520              padx -= COLS / 2;
521              if (padx < 0)
522                padx = 0;
523              ncurses_refresh ();
524            }
525          /* Autoscroll down.  */
526          if (row > LINES + pady)
527            {
528              pady += LINES / 2;
529              if (pady > LINES + current_height)
530                pady = current_height - LINES;
531              ncurses_refresh ();
532            }
533          /* Autoscroll up.  */
534          else if (row < pady)
535            {
536              pady -= LINES / 2;
537              if (pady < 0)
538                pady = 0;
539              ncurses_refresh ();
540            }
541        }
542    
543      wmove (conspad, row, col);
544    
545    mutex_unlock (&ncurses_lock);    mutex_unlock (&ncurses_lock);
546    return 0;    return 0;
547  }  }
# Line 449  static error_t Line 551  static error_t
551  ncursesw_set_cursor_status (void *handle, uint32_t status)  ncursesw_set_cursor_status (void *handle, uint32_t status)
552  {  {
553    mutex_lock (&ncurses_lock);    mutex_lock (&ncurses_lock);
554    curs_set (status ? (status == 1 ? 1 : 2) : 0);  
555      /* If the cursor is invisible and switching to one visible state is
556         impossible, switch to the other visible state or else the cursor
557         will not be shown at all.  */
558      if (curs_set (status) == -1 && status)
559        curs_set (status == 1 ? 2 : 1);
560    
561    mutex_unlock (&ncurses_lock);    mutex_unlock (&ncurses_lock);
562    return 0;    return 0;
563  }  }
# Line 462  ncursesw_scroll (void *handle, int delta Line 570  ncursesw_scroll (void *handle, int delta
570    assert (delta >= 0);    assert (delta >= 0);
571    
572    mutex_lock (&ncurses_lock);    mutex_lock (&ncurses_lock);
573    idlok (stdscr, TRUE);    idlok (conspad, TRUE);
574    scrollok (stdscr, TRUE);    scrollok (conspad, TRUE);
575    scrl (delta);    wscrl (conspad, delta);
576    idlok (stdscr, FALSE);    idlok (conspad, FALSE);
577    scrollok (stdscr, FALSE);    scrollok (conspad, FALSE);
578    mutex_unlock (&ncurses_lock);    mutex_unlock (&ncurses_lock);
579    return 0;    return 0;
580  }  }
# Line 480  ncursesw_write (void *handle, conchar_t Line 588  ncursesw_write (void *handle, conchar_t
588    int y;    int y;
589    
590    mutex_lock (&ncurses_lock);    mutex_lock (&ncurses_lock);
591    getyx (stdscr, y, x);    getyx (conspad, y, x);
592    mvwputsn (str, length, col, row);    mvwputsn (str, length, col, row);
593    wmove (stdscr, y, x);    wmove (conspad, y, x);
594    mutex_unlock (&ncurses_lock);    mutex_unlock (&ncurses_lock);
595    return 0;    return 0;
596  }  }
# Line 531  ncursesw_driver_start (void *handle) Line 639  ncursesw_driver_start (void *handle)
639    raw ();    raw ();
640    noecho ();    noecho ();
641    nonl ();    nonl ();
642    intrflush (stdscr, FALSE);  
643    nodelay (stdscr, TRUE);    /* Create a new pad with the size minimal size.  This pad will be
644    timeout (1);       resized by ncursesw_set_dimension.  */
645    keypad (stdscr, TRUE);    conspad = newpad (1, 1);
646      if (!conspad)
647        return errno;
648    
649      intrflush (conspad, FALSE);
650      nodelay (conspad, TRUE);
651      wtimeout (conspad, 1);
652      keypad (conspad, TRUE);
653    
654    err = driver_add_display (&ncursesw_display_ops, NULL);    err = driver_add_display (&ncursesw_display_ops, NULL);
655    if (err)    if (err)
# Line 559  ncursesw_driver_start (void *handle) Line 674  ncursesw_driver_start (void *handle)
674      }      }
675    
676    cthread_detach (cthread_fork (input_loop, NULL));    cthread_detach (cthread_fork (input_loop, NULL));
   endwin ();  
677    
678    return err;    return 0;
679  }  }
680    
681  /* Destroy the display HANDLE.  */  /* Destroy the display HANDLE.  */
# Line 579  ncursesw_driver_fini (void *handle, int Line 693  ncursesw_driver_fini (void *handle, int
693    return 0;    return 0;
694  }  }
695    
696    static error_t
697    ncursesw_set_dimension (void *handle, int width, int height)
698    {
699      mutex_lock (&ncurses_lock);
700      if (width != current_width || height != current_height)
701        {
702          wresize (conspad, height, width);
703          padx = 0;
704          pady = 0;
705        }
706      current_width = width;
707      current_height = height;
708      mutex_unlock(&ncurses_lock);
709      return 0;
710    }
711    
712    
713  struct driver_ops driver_ncursesw_ops =  struct driver_ops driver_ncursesw_ops =
714    {    {
# Line 596  static struct display_ops ncursesw_displ Line 726  static struct display_ops ncursesw_displ
726      ncursesw_write,      ncursesw_write,
727      ncursesw_update,      ncursesw_update,
728      ncursesw_flash,      ncursesw_flash,
729      NULL      NULL,
730        ncursesw_set_dimension
731    };    };
732    
733  static struct input_ops ncursesw_input_ops =  static struct input_ops ncursesw_input_ops =

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26