3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #ifdef ARCH_i386 /* i386 specific things */
28 extern int gets (void);
29 extern unsigned short *textmemptr
;
32 extern int csr_x
, csr_y
;
33 extern unsigned char vgagui
;
35 static char con_screen
[TTY_CON_BUF
]; /* console buffer for image of screen */
36 static unsigned short tty_count
; /* number of created ttys */
37 tty_font_t tty_font
; /* global font settings */
38 tty_char_t tty_char
; /* global character array settings */
40 tty_t tty_list
; /* list for all created ttys */
41 tty_t
*tty0
, *tty1
; /* structures for default ttys (0, 1) */
43 static unsigned task_tty ();
46 bool tty_font_set (char *c
, unsigned char x
, unsigned char y
)
58 /* setup of character array */
59 bool tty_char_set (unsigned short x
, unsigned short y
)
61 if ((x
* y
* 2) > TTY_CON_BUF
)
70 /* clear selected screen */
71 bool tty_cls (tty_t
*tty
)
79 memset (tty
->screen
, 0, tty_char
.x
* tty_char
.y
* 2);
107 /* lock manipulation with selected tty */
108 bool tty_lock (tty_t
*tty
)
118 /* unlock manipulation with selected tty */
119 bool tty_unlock (tty_t
*tty
)
129 /* startup function for tty init */
139 strcpy (currtty
->pwd
, (char *) env_get ("PWD"));
143 env_set ("PWD", tty
->pwd
);
145 /* always default colors */
154 /* change current tty to selected one */
155 bool tty_change (tty_t
*tty
)
164 /* redraw screen with selected tty */
165 memcpy (textmemptr
, tty
->screen
, tty_char
.x
* tty_char
.y
* 2);
167 csr_x
= tty
->screen_x
;
168 csr_y
= tty
->screen_y
;
172 strcpy (currtty
->pwd
, (char *) env_get ("PWD"));
174 if (currtty
->user
&& currtty
->user
->name
)
175 strcpy (currtty
->user
->name
, (char *) env_get ("USER"));
179 env_set ("PWD", tty
->pwd
);
182 env_set ("USER", tty
->user
->name
);
187 /* move cursor position to selected place */
188 bool tty_gotoxy (tty_t
*tty
, unsigned char x
, unsigned char y
)
193 if (x
>= tty_char
.x
|| y
>= tty_char
.y
)
204 /* find wanted tty by name */
205 tty_t
*tty_find (char *name
)
209 for (tty
= tty_list
.next
; tty
!= &tty_list
; tty
= tty
->next
) {
210 if (!strcmp (tty
->name
, name
))
217 /* find tty by task */
218 tty_t
*tty_findbytask (task_t
*task
)
222 for (tty
= tty_list
.next
; tty
!= &tty_list
; tty
= tty
->next
) {
223 if (tty
->task
== task
)
230 /* display list of created tty consoles */
233 unsigned short id
= 1;
237 for (tty
= tty_list
.next
; tty
!= &tty_list
; tty
= tty
->next
) {
238 printf ("%d.\t%s\t%s\n", id
, tty
->name
, currtty
== tty
? "<- current" : "");
244 /* main function for print strings into selected tty */
245 bool tty_write (tty_t
*tty
, char *str
, unsigned len
)
252 if (!tty_putnch (tty
, str
[l
]))
261 /* read strings from selected tty */
262 int tty_read (tty_t
*tty
, char *str
, unsigned len
)
267 char *p
= (char *) tty
->screen
;
275 if (len
+tty
->read_len
> n
)
276 len
= n
-tty
->read_len
;
278 for (l
= 0; l
< len
; l
++) {
279 if (p
[((l
+tty
->read_len
)*2)+1])
280 str
[l
] = p
[((l
+tty
->read_len
)*2)+1];
284 if (x
>= tty_char
.x
) {
298 /* print character to current tty */
299 bool tty_putch (char c
)
301 tty_putnch (currtty
, c
);
306 /* print character to selected tty */
307 bool tty_putnch (tty_t
*tty
, char c
)
309 /* we wont null character */
322 /* save font attributes (colors) */
323 tty
->screen
[(tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2] = ' ';
324 /* save character into buffer */
325 tty
->screen
[((tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2)+1] = attrib
;
328 tty
->screen_x
= (tty
->screen_x
+ 8) & ~(8 - 1);
331 /* save font attributes (colors) */
332 tty
->screen
[(tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2] = c
;
333 /* save character into buffer */
334 tty
->screen
[((tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2)+1] = attrib
;
339 if (tty
->screen_x
>= tty_char
.x
) {
340 tty
->screen_x
-= tty_char
.x
;
344 if (tty
->screen_y
>= tty_char
.y
) {
346 /* save old screen, but with one line offset */
347 for (i
= 0; i
< (tty_char
.x
* tty_char
.y
* 2) - (tty_char
.x
* 2); i
++)
348 con_screen
[i
] = tty
->screen
[i
+(tty_char
.x
* 2)];
349 /* copy new image into tty screen */
350 for (i
= 0; i
< (tty_char
.x
* tty_char
.y
* 2); i
++)
351 tty
->screen
[i
] = con_screen
[i
];
353 tty
->read_len
+= tty_char
.x
;
355 tty
->screen_y
= tty_char
.y
- 1;
365 /* create Message Of The Day in /etc/motd */
368 vfs_list_add ("motd", VFS_FILEATTR_FILE
| VFS_FILEATTR_READ
| VFS_FILEATTR_SYSTEM
, "/etc/");
370 char *motd
= "\t\t\t\tWelcome !\n"
371 "This is ZeX/OS - operating system created by Tomas 'ZeXx86' Jedrzejek\n"
372 "Please visit web page www.zexos.org for more information ..\n"
373 "Latest source code is available on git repository: git://repo.or.cz/ZeXOS.git\n"
374 "You can display list of the built-in commands by \"help\".\n"
375 "Default login name is \"root\" secured by password \"root\".\n";
377 vfs_mmap ("/etc/motd", 9, motd
, strlen (motd
));
381 vfs_cat ("/etc/motd", 9);
388 /* create new tty console */
391 /* Software limit - max. 1K of ttys */
392 if (tty_count
> TTY_COUNT_MAX
)
396 sprintf (name
, "tty%d", tty_count
);
398 unsigned name_len
= strlen (name
);
400 /* alloc and init context */
401 tty_t
*tty
= (tty_t
*) kmalloc (sizeof (tty_t
));
406 memset (tty
, 0, sizeof (tty_t
));
408 tty
->screen
= (char *) kmalloc (tty_char
.x
* tty_char
.y
* 2);
415 tty
->name
= (char *) kmalloc (sizeof (char) * name_len
+ 1);
423 memcpy (tty
->name
, name
, name_len
);
424 tty
->name
[name_len
] = '\0';
426 memset (tty
->screen
, 0, tty_char
.x
* tty_char
.y
* 2);
437 strcpy (tty
->pwd
, (char *) env_get ("PWD"));
439 tty
->task
= (task_t
*) task_create (name
, (unsigned) task_tty
, 32);
442 tty
->next
= &tty_list
;
443 tty
->prev
= tty_list
.prev
;
444 tty
->prev
->next
= tty
;
445 tty
->next
->prev
= tty
;
452 /* thread for running tty consoles - handle selected console */
453 void task_thread (tty_t
*tty
)
464 /* handle console on kernel level */
473 /* VESA mode - graphical font */
478 unsigned m
= tty_char
.x
* tty_char
.y
;
481 gputch (1+(j
*tty_font
.x
), 11+(k
*(tty_font
.y
+1)), vesa_16cto16b (tty
->screen
[i
*2+1] & 0x0F), tty
->screen
[i
*2]);
485 if (j
>= tty_char
.x
) {
498 /* startup function for created tty console */
499 static unsigned task_tty ()
501 tty_t
*tty
= tty_findbytask (_curr_task
);
510 /* change tty console to next (+) or previous (-) in list */
511 void tty_switch (char act
)
516 tty_t
*tty
= currtty
->next
;
518 if (tty
== &tty_list
)
528 tty_t
*tty
= currtty
->prev
;
530 if (tty
== &tty_list
)
541 /* special function for initialization of graphical tty */
544 tty_t
*tty
= tty_create ();
551 /* initialization of tty */
552 unsigned int init_tty ()
554 /* pre-set tty list */
555 tty_list
.next
= &tty_list
;
556 tty_list
.prev
= &tty_list
;
561 tty_font_set ((char *) font5x8
, 5, 8);
564 case 0: /* VGA-Textual console */
565 tty_char_set (80, 25);
567 case 1: /* VGA console */
568 tty_char_set (53, 18);
570 case 2: /* VESA concole */
571 tty_char_set (80, 65);
577 /* create consoles */
578 tty0
= tty_create ();
584 tty1
= tty_create ();
591 /* printf MOTD into TTY0 console */
594 /* then print login messages */
595 tty_write (tty0
, "\nlogin: ", 8);
596 tty_write (tty1
, "\nlogin: ", 8);