Ver código fonte

all window primitives implemented

tags/v0.9.3
Dan Lynch 18 anos atrás
pai
commit
2814681d20
2 arquivos alterados com 116 adições e 87 exclusões
  1. 107
    57
      src/core/curses.c
  2. 9
    30
      src/include/curses.h

+ 107
- 57
src/core/curses.c Ver arquivo

@@ -8,6 +8,106 @@
8 8
  *
9 9
  */
10 10
 
11
+#define WRAP 0
12
+#define NOWRAP 1
13
+
14
+struct cursor_pos {
15
+	unsigned int y, x;
16
+};
17
+
18
+/*
19
+  Primitives
20
+*/
21
+
22
+/**
23
+ * Write a single character rendition to a window
24
+ *
25
+ * @v *win	window in which to write
26
+ * @v ch	character rendition to write
27
+ */
28
+static void _wputch ( WINDOW *win, chtype ch, int wrap ) {
29
+	win->scr->putc(win->scr, ch);
30
+	if ( ++(win->curs_x) > win->width ) {
31
+		if ( wrap == WRAP ) {
32
+			win->curs_x = 0;
33
+			(win->curs_y)++;
34
+		} else {
35
+			(win->curs_x)--;
36
+		}
37
+	}
38
+}
39
+
40
+/**
41
+ * Write a chtype string to a window
42
+ *
43
+ * @v *win	window in which to write
44
+ * @v *chstr	chtype string
45
+ * @v wrap	wrap "switch"
46
+ * @v n		write at most n chtypes
47
+ */
48
+static void _wputchstr ( WINDOW *win, chtype *chstr, int wrap, int n ) {
49
+	for ( ; *chstr && n-- ; chstr++ ) {
50
+		_wputch(win,*chstr,wrap);
51
+	}
52
+}
53
+
54
+/**
55
+ * Write a standard c-style string to a window
56
+ * @v *win	window in which to write
57
+ * @v *str	string
58
+ * @v wrap	wrap "switch"
59
+ * @v n		write at most n chars from *str
60
+ */
61
+static void _wputstr ( WINDOW *win, char *str, int wrap, int n ) {
62
+	for ( ; *str && n-- ; str++ ) {
63
+		_wputch( win, *str | win->attrs, wrap );
64
+	}
65
+}
66
+
67
+/**
68
+ * Restore cursor position from encoded backup variable
69
+ *
70
+ * @v *win	window on which to operate
71
+ * @v *pos	pointer to struct in which original cursor position is stored
72
+ */
73
+static void _restore_curs_pos ( WINDOW *win, struct cursor_pos *pos ){
74
+	win->curs_y = pos->y;
75
+	win->curs_x = pos->x;
76
+}
77
+
78
+/**
79
+ * Store cursor position for later restoration
80
+ *
81
+ * @v *win	window on which to operate
82
+ * @v *pos	pointer to struct in which to store cursor position
83
+ */
84
+static void _store_curs_pos ( WINDOW *win, struct cursor_pos *pos ) {
85
+	pos->y = win->curs_y;
86
+	pos->x = win->curs_x;
87
+}
88
+
89
+/**
90
+ * Move a window's cursor to the specified position
91
+ *
92
+ * @v *win	window to be operated on
93
+ * @v y		Y position
94
+ * @v x		X position
95
+ * @ret rc	return status code
96
+ */
97
+int wmove ( WINDOW *win, int y, int x ) {
98
+	/* chech for out-of-bounds errors */
99
+	if ( ( ( (unsigned)x - win->ori_x ) > win->width ) ||
100
+	     ( ( (unsigned)y - win->ori_y ) > win->height ) ) {
101
+		return ERR;
102
+	}
103
+
104
+	win->scr->movetoyx( win->scr, y, x );
105
+	return OK;
106
+}
107
+
108
+
109
+
110
+
11 111
 WINDOW _stdscr = {
12 112
 	.attrs = A_DEFAULT,
13 113
 	.ori_y = 0,
@@ -146,8 +246,6 @@ WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
146 246
  * @ret rc	return status code
147 247
  */
148 248
 int waddch ( WINDOW *win, const chtype ch ) {
149
-	_putc( win->scr, ch & A_CHARTEXT );
150
-	_advcurs_wrap( win );
151 249
 	return OK;
152 250
  err:
153 251
 	return ERR;
@@ -162,21 +260,12 @@ int waddch ( WINDOW *win, const chtype ch ) {
162 260
  * @ret rc	return status code
163 261
  */
164 262
 int waddchnstr ( WINDOW *win, const chtype *chstr, int n ) {
165
-	unsigned int ch, pos, count = 0;
166
-	chtype *chptr = chstr;
263
+	struct cursor_pos pos;	
167 264
 
168
-	pos = _store_curs_pos ( win );
169
-	while ( ( ( ( ch = ( *chptr & A_CHARTEXT ) ) ) != '\0' )
170
-		&& ( count++ < (unsigned)n ) ) {
171
-		_putc( win, ch );
172
-		_advcurs_nowrap( win );
173
-		/* set rendition code here */
174
-	}
175
-	_restore_curs_pos( win, pos );
265
+	_store_curs_pos( win, &pos );
266
+	_wputchstr( win, chstr, NOWRAP, n );
267
+	_restore_curs_pos( win, &pos );
176 268
 	return OK;
177
- err:
178
-	_restore_curs_pos( win, pos );
179
-	return ERR;
180 269
 }
181 270
 
182 271
 /**
@@ -193,8 +282,6 @@ int waddnstr ( WINDOW *win, const char *str, int n ) {
193 282
 
194 283
 	while ( ( ( ch = *strptr ) != '\0' )
195 284
 		&& ( count++ < (unsigned)n ) ) {
196
-		_putc( win, ch );
197
-		_advcurs_wrap( win );
198 285
 	}
199 286
 
200 287
 	return OK;
@@ -238,29 +325,6 @@ int wattrset ( WINDOW *win, int attrs ) {
238 325
 	return OK;
239 326
 }
240 327
 
241
-/**
242
- * Set background rendition attributes for a window and apply to
243
- * contents
244
- *
245
- * @v *win	window to be operated on
246
- * @v ch	chtype containing rendition attributes
247
- * @ret rc	return status code
248
- */
249
-int wbkgd ( WINDOW *win, chtype ch ) {
250
-	return OK;
251
- err:
252
-	return ERR;
253
-}
254
-
255
-/**
256
- * Set background rendition attributes for a window
257
- *
258
- * @v *win	window to be operated on
259
- * @v ch	chtype containing rendition attributes
260
- */
261
-void wbkgdset ( WINDOW *win, chtype ch ) {
262
-}
263
-
264 328
 /**
265 329
  * Draw borders from single-byte characters and renditions around a
266 330
  * window
@@ -276,25 +340,11 @@ void wbkgdset ( WINDOW *win, chtype ch ) {
276 340
  * @v br	bottom right corner
277 341
  * @ret rc	return status code
278 342
  */
279
-int wborder ( WINDOW *win, chtype ls, chtype rs, 
280
-			   chtype ts, chtype bs, chtype tl,
281
-			   chtype tr, chtype bl, chtype br ) {
343
+int wborder ( WINDOW *win, chtype ls, chtype rs,
344
+	      chtype ts, chtype bs, chtype tl,
345
+	      chtype tr, chtype bl, chtype br ) {
282 346
 	return OK;
283 347
  err:
284 348
 	return ERR;
285 349
 }
286 350
 
287
-/**
288
- * Move a window's cursor to the specified position
289
- *
290
- * @v *win	window to be operated on
291
- * @v y		Y position
292
- * @v x		X position
293
- * @ret rc	return status code
294
- */
295
-int wmove ( WINDOW *win, int y, int x ) {
296
-	_movetoyx( win->scr, y, x );
297
-	return OK;
298
- err:
299
-	return ERR;
300
-}

+ 9
- 30
src/include/curses.h Ver arquivo

@@ -477,7 +477,7 @@ extern int wattr_off ( WINDOW *, attr_t, void * );
477 477
 extern int wattr_on ( WINDOW *, attr_t, void * );
478 478
 extern int wattr_set ( WINDOW *, attr_t, short, void * );
479 479
 extern int wbkgd ( WINDOW *, chtype );
480
-extern void wbkgdset ( WINDOW *, chtype );
480
+/*extern void wbkgdset ( WINDOW *, chtype );*/
481 481
 extern int wborder ( WINDOW *, chtype, chtype, chtype, chtype, chtype, chtype,
482 482
 		   chtype, chtype );
483 483
 extern int wchgat ( WINDOW *, int, attr_t, short, const void * );
@@ -628,7 +628,7 @@ static inline int bkgd ( chtype ch ) {
628 628
  * Set background rendition attributes for stdscr
629 629
  */
630 630
 static inline void bkgdset ( chtype ch ) {
631
-	wbkgdset ( stdscr, ch );
631
+	wattrset ( stdscr, ch );
632 632
 }
633 633
 
634 634
 /**
@@ -832,37 +832,16 @@ static inline int waddstr ( WINDOW *win, const char *str ) {
832 832
 	return waddnstr ( win, str, -1 );
833 833
 }
834 834
 
835
-/*
836
- * Primitives
837
- */
838
-/**
839
- * Advance cursor (wrap text)
840
- *
841
- * @v *win	window in which to advance
842
- * @ret rc	void function
843
- */
844
-void _advcurs_wrap ( struct _curses_window *win );
845
-/**
846
- * Advance cursor (no wrap text)
847
- *
848
- * @v *win	window in which to advance
849
- * @ret rc	void function
850
- */
851
-void _advcurs_nowrap ( struct _curses_window *win );
852
-/**
853
- * Store cursor position for later restoration
854
- *
855
- * @v *win	window on which to operate
856
- * @ret rc	return encoded position
857
- */
858
-unsigned int _store_curs_pos ( WINDOW *win );
859 835
 /**
860
- * Restore cursor position from encoded backup variable
836
+ * Set background rendition attributes for a window and apply to
837
+ * contents
861 838
  *
862
- * @v *win	window on which to operate
863
- * @v pos	encoded position
839
+ * @v *win	window to be operated on
840
+ * @v ch	chtype containing rendition attributes
864 841
  * @ret rc	return status code
865 842
  */
866
-int _restore_curs_pos ( WINDOW *win, unsigned int pos );
843
+static inline int wbkgdset ( WINDOW *win, chtype ch ) {
844
+	return wattrset( win, ch );
845
+}
867 846
 
868 847
 #endif /* CURSES_H */

Carregando…
Cancelar
Salvar