mucurses.c

Go to the documentation of this file.
00001 #include <console.h>
00002 #include <curses.h>
00003 #include "mucurses.h"
00004 
00005 /** @file
00006  *
00007  * MuCurses core functions
00008  *
00009  */
00010 
00011 FILE_LICENCE ( GPL2_OR_LATER );
00012 
00013 static void _wupdcurs ( WINDOW *win ) __nonnull;
00014 void _wputch ( WINDOW *win, chtype ch, int wrap ) __nonnull;
00015 void _wputc ( WINDOW *win, char c, int wrap ) __nonnull;
00016 void _wcursback ( WINDOW *win ) __nonnull;
00017 void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n ) __nonnull;
00018 void _wputstr ( WINDOW *win, const char *str, int wrap, int n ) __nonnull;
00019 int wmove ( WINDOW *win, int y, int x ) __nonnull;
00020 
00021 WINDOW _stdscr = {
00022         .attrs = A_DEFAULT,
00023         .ori_y = 0,
00024         .ori_x = 0,
00025         .curs_y = 0,
00026         .curs_x = 0,
00027         .scr = &_ansi_screen,
00028 };
00029 
00030 /*
00031  *  Primitives
00032  */
00033 
00034 /**
00035  * Update cursor position
00036  *
00037  * @v *win      window in which to update position
00038  */
00039 static void _wupdcurs ( WINDOW *win ) {
00040         win->scr->movetoyx ( win->scr, win->ori_y + win->curs_y,
00041                              win->ori_x + win->curs_x );
00042 }
00043 
00044 /**
00045  * Write a single character rendition to a window
00046  *
00047  * @v *win      window in which to write
00048  * @v ch        character rendition to write
00049  * @v wrap      wrap "switch"
00050  */
00051 void _wputch ( WINDOW *win, chtype ch, int wrap ) {
00052         /* make sure we set the screen cursor to the right position
00053            first! */
00054         _wupdcurs(win);
00055         win->scr->putc(win->scr, ch);
00056         if ( ++(win->curs_x) - win->width == 0 ) {
00057                 if ( wrap == WRAP ) {
00058                         win->curs_x = 0;
00059                         /* specification says we should really scroll,
00060                            but we have no buffer to scroll with, so we
00061                            can only overwrite back at the beginning of
00062                            the window */
00063                         if ( ++(win->curs_y) - win->height == 0 )
00064                                 win->curs_y = 0;
00065                 } else {
00066                         (win->curs_x)--;
00067                 }
00068         }
00069 }
00070 
00071 /**
00072  * Write a single character to a window
00073  *
00074  * @v *win      window in which to write
00075  * @v c         character rendition to write
00076  * @v wrap      wrap "switch"
00077  */
00078 void _wputc ( WINDOW *win, char c, int wrap ) {
00079         _wputch ( win, ( c | win->attrs ), wrap );
00080 }
00081 
00082 /**
00083  * Retreat the cursor back one position (useful for a whole host of
00084  * ops)
00085  *
00086  * @v *win      window in which to retreat
00087  */
00088 void _wcursback ( WINDOW *win ) {
00089         if ( win->curs_x == 0 ) {
00090                 if ( win->curs_y == 0 )
00091                         win->curs_y = win->height - 1;
00092                 win->curs_x = win->width = 1;
00093         } else {
00094                 win->curs_x--;
00095         }
00096 
00097         _wupdcurs(win);
00098 }
00099 
00100 /**
00101  * Write a chtype string to a window
00102  *
00103  * @v *win      window in which to write
00104  * @v *chstr    chtype string
00105  * @v wrap      wrap "switch"
00106  * @v n         write at most n chtypes
00107  */
00108 void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n ) {
00109         for ( ; *chstr && n-- ; chstr++ ) {
00110                 _wputch(win,*chstr,wrap);
00111         }
00112 }
00113 
00114 /**
00115  * Write a standard c-style string to a window
00116  *
00117  * @v *win      window in which to write
00118  * @v *str      string
00119  * @v wrap      wrap "switch"
00120  * @v n         write at most n chars from *str
00121  */
00122 void _wputstr ( WINDOW *win, const char *str, int wrap, int n ) {
00123         for ( ; *str && n-- ; str++ ) {
00124                 _wputc ( win, *str, wrap );
00125         }
00126 }
00127 
00128 /**
00129  * Move a window's cursor to the specified position
00130  *
00131  * @v *win      window to be operated on
00132  * @v y         Y position
00133  * @v x         X position
00134  * @ret rc      return status code
00135  */
00136 int wmove ( WINDOW *win, int y, int x ) {
00137         /* chech for out-of-bounds errors */
00138         if ( ( (unsigned)y >= win->height ) ||
00139              ( (unsigned)x >= win->width ) ) {
00140                 return ERR;
00141         }
00142 
00143         win->curs_y = y;
00144         win->curs_x = x;
00145         _wupdcurs(win);
00146         return OK;
00147 }

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1