3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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);
106 /* lock manipulation with selected tty */
107 bool tty_lock (tty_t
*tty
)
117 /* unlock manipulation with selected tty */
118 bool tty_unlock (tty_t
*tty
)
128 /* startup function for tty init */
138 strcpy (currtty
->pwd
, (char *) env_get ("PWD"));
142 env_set ("PWD", tty
->pwd
);
144 /* always default colors */
153 /* change current tty to selected one */
154 bool tty_change (tty_t
*tty
)
163 /* redraw screen with selected tty */
164 memcpy (textmemptr
, tty
->screen
, tty_char
.x
* tty_char
.y
* 2);
166 csr_x
= tty
->screen_x
;
167 csr_y
= tty
->screen_y
;
171 strcpy (currtty
->pwd
, (char *) env_get ("PWD"));
173 if (currtty
->user
&& currtty
->user
->name
)
174 strcpy (currtty
->user
->name
, (char *) env_get ("USER"));
178 env_set ("PWD", tty
->pwd
);
181 env_set ("USER", tty
->user
->name
);
186 /* move cursor position to selected place */
187 bool tty_gotoxy (tty_t
*tty
, unsigned char x
, unsigned char y
)
192 if (x
>= tty_char
.x
|| y
>= tty_char
.y
)
203 /* find wanted tty by name */
204 tty_t
*tty_find (char *name
)
208 for (tty
= tty_list
.next
; tty
!= &tty_list
; tty
= tty
->next
) {
209 if (!strcmp (tty
->name
, name
))
216 /* find tty by task */
217 tty_t
*tty_findbytask (task_t
*task
)
221 for (tty
= tty_list
.next
; tty
!= &tty_list
; tty
= tty
->next
) {
222 if (tty
->task
== task
)
229 /* display list of created tty consoles */
232 unsigned short id
= 1;
236 for (tty
= tty_list
.next
; tty
!= &tty_list
; tty
= tty
->next
) {
237 printf ("%d.\t%s\t%s\n", id
, tty
->name
, currtty
== tty
? "<- current" : "");
243 /* enable logging for TTY */
244 int tty_log_create (tty_t
*tty
, unsigned short len
)
249 tty
->log
= (tty_log_t
*) kmalloc (sizeof (tty_log_t
));
254 tty
->log
->buf
= (char *) kmalloc (sizeof (char) * len
);
256 if (!tty
->log
->buf
) {
267 /* main function for print strings into selected tty */
268 bool tty_write (tty_t
*tty
, char *str
, unsigned len
)
275 if (!tty_putnch (tty
, str
[l
]))
284 /* read strings from selected tty */
285 int tty_read (tty_t
*tty
, char *str
, unsigned len
)
290 /* create log buffer for current tty when not exist */
292 if (!tty_log_create (tty
, 512))
297 if (l
> tty
->log
->pos
)
300 memcpy (str
, tty
->log
->buf
, l
);
307 /* print character to current tty */
308 bool tty_putch (char c
)
310 tty_putnch (currtty
, c
);
315 /* print character to selected tty */
316 bool tty_putnch (tty_t
*tty
, char c
)
318 /* we wont null character */
331 /* save font attributes (colors) */
332 tty
->screen
[(tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2] = ' ';
333 /* save character into buffer */
334 tty
->screen
[((tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2)+1] = attrib
;
338 tty
->screen_x
= (tty
->screen_x
+ 8) & ~7;
341 /* save font attributes (colors) */
342 tty
->screen
[(tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2] = c
;
343 /* save character into buffer */
344 tty
->screen
[((tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2)+1] = attrib
;
349 if (tty
->screen_x
>= tty_char
.x
) {
350 tty
->screen_x
-= tty_char
.x
;
354 if (tty
->screen_y
>= tty_char
.y
) {
356 /* save old screen, but with one line offset */
357 for (i
= 0; i
< (tty_char
.x
* tty_char
.y
* 2) - (tty_char
.x
* 2); i
++)
358 con_screen
[i
] = tty
->screen
[i
+(tty_char
.x
* 2)];
359 /* copy new image into tty screen */
360 for (i
= 0; i
< (tty_char
.x
* tty_char
.y
* 2); i
++)
361 tty
->screen
[i
] = con_screen
[i
];
363 tty
->screen_y
= tty_char
.y
- 1;
366 if (tty
== currtty
) {
372 if (tty
->log
->pos
+1 == tty
->log
->len
) {
373 memcpy (tty
->log
->buf
, tty
->log
->buf
+1, tty
->log
->len
-1);
374 tty
->log
->buf
[tty
->log
->pos
] = c
;
376 tty
->log
->buf
[tty
->log
->pos
++] = c
;
382 /* create Message Of The Day in /etc/motd */
385 vfs_list_add ("motd", VFS_FILEATTR_FILE
| VFS_FILEATTR_READ
| VFS_FILEATTR_SYSTEM
, "/etc/");
387 char *motd
= "\t\t\t\tWelcome !\n"
388 "This is ZeX/OS - operating system created by Tomas 'ZeXx86' Jedrzejek\n"
389 "Please visit web page www.zexos.org for more information ..\n"
390 "Latest source code is available on git repository: git://repo.or.cz/ZeXOS.git\n"
391 "You can display list of the built-in commands by \"help\".\n"
392 "Default login name is \"root\" secured by password \"root\".\n";
394 vfs_content_t content
;
396 content
.len
= strlen (motd
);
398 vfs_mmap ("/etc/motd", 9, &content
);
402 vfs_cat ("/etc/motd", 9);
409 /* virtual terminal (device handler) */
410 bool vterm_acthandler (unsigned act
, char *block
, unsigned block_len
)
415 dev_flags_t
*flags
= (dev_flags_t
*) block
;
420 if (block_len
!= sizeof (dev_flags_t
))
423 tty_t
*tty
= tty_find ((char *) flags
->iomem
);
428 flags
->iomem
= (void *) tty
->screen
;
429 flags
->iolen
= tty_char
.x
* tty_char
.y
* 2;
439 /* create new tty console */
442 /* Software limit - max. 1K of ttys */
443 if (tty_count
> TTY_COUNT_MAX
)
447 sprintf (name
, "tty%d", tty_count
);
449 unsigned name_len
= strlen (name
);
451 /* alloc and init context */
452 tty_t
*tty
= (tty_t
*) kmalloc (sizeof (tty_t
));
457 memset (tty
, 0, sizeof (tty_t
));
459 tty
->screen
= (char *) kmalloc (tty_char
.x
* tty_char
.y
* 2);
466 tty
->name
= (char *) kmalloc (sizeof (char) * name_len
+ 1);
474 memcpy (tty
->name
, name
, name_len
);
475 tty
->name
[name_len
] = '\0';
477 memset (tty
->screen
, 0, tty_char
.x
* tty_char
.y
* 2);
488 strcpy (tty
->pwd
, (char *) env_get ("PWD"));
490 tty
->task
= (task_t
*) task_create (name
, (unsigned) task_tty
, 32);
493 tty
->next
= &tty_list
;
494 tty
->prev
= tty_list
.prev
;
495 tty
->prev
->next
= tty
;
496 tty
->next
->prev
= tty
;
498 /* create device which correspnd with current TTY */
499 dev_register (name
, "Virtual terminal", DEV_ATTR_CHAR
, (dev_handler_t
*) &vterm_acthandler
);
506 /* thread for running tty consoles - handle selected console */
507 void task_thread (tty_t
*tty
)
518 /* handle console on kernel level */
527 /* VESA mode - graphical font */
532 unsigned m
= tty_char
.x
* tty_char
.y
;
535 gputch (1+(j
*tty_font
.x
), 11+(k
*(tty_font
.y
+1)), vesa_16cto16b (tty
->screen
[i
*2+1] & 0x0F), tty
->screen
[i
*2]);
539 if (j
>= tty_char
.x
) {
552 /* startup function for created tty console */
553 static unsigned task_tty ()
555 tty_t
*tty
= tty_findbytask (_curr_task
);
564 /* change tty console to next (+) or previous (-) in list */
565 void tty_switch (char act
)
570 tty_t
*tty
= currtty
->next
;
572 if (tty
== &tty_list
)
582 tty_t
*tty
= currtty
->prev
;
584 if (tty
== &tty_list
)
595 /* special function for initialization of graphical tty */
598 tty_t
*tty
= tty_create ();
605 /* initialization of tty */
606 unsigned int init_tty ()
608 /* pre-set tty list */
609 tty_list
.next
= &tty_list
;
610 tty_list
.prev
= &tty_list
;
615 tty_font_set ((char *) font5x8
, 5, 8);
618 case 0: /* VGA-Textual console */
619 tty_char_set (80, 25);
621 case 1: /* VGA console */
622 tty_char_set (53, 18);
624 case 2: /* VESA concole */
625 tty_char_set (80, 65);
631 /* create consoles */
632 tty0
= tty_create ();
638 tty1
= tty_create ();
645 /* printf MOTD into TTY0 console */
648 /* then print login messages */
649 tty_write (tty0
, "\nlogin: ", 8);
651 tty_write (tty1
, "\nlogin: ", 8);