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
) {
266 /* main function for print strings into selected tty */
267 bool tty_write (tty_t
*tty
, char *str
, unsigned len
)
274 if (!tty_putnch (tty
, str
[l
]))
283 /* read strings from selected tty */
284 int tty_read (tty_t
*tty
, char *str
, unsigned len
)
289 char *p
= (char *) tty
->screen
;
297 //if (tty->read_len > n)
298 // len = n-tty->read_len;
299 //printf ("len: %d : %d\n", len, tty->read_len);
302 /* for (l = 0; l < len; l ++) {
303 if (p[(l+tty->read_len)*2])
304 str[i ++] = p[(l+tty->read_len)*2];
308 if (x >= tty_char.x) {
319 tty->read_len += l;*/
321 /*if (len > tty->read_len)
324 for (l = 0; l < len; l ++) {
325 char c = p[((tty->screen_y * tty_char.x) + tty->screen_x - len + l)*2];
336 tty->read_len -= len;*/
341 /* print character to current tty */
342 bool tty_putch (char c
)
344 tty_putnch (currtty
, c
);
349 /* print character to selected tty */
350 bool tty_putnch (tty_t
*tty
, char c
)
352 /* we wont null character */
365 /* save font attributes (colors) */
366 tty
->screen
[(tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2] = ' ';
367 /* save character into buffer */
368 tty
->screen
[((tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2)+1] = attrib
;
372 tty
->screen_x
= (tty
->screen_x
+ 8) & ~7;
375 /* save font attributes (colors) */
376 tty
->screen
[(tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2] = c
;
377 /* save character into buffer */
378 tty
->screen
[((tty
->screen_x
+(tty
->screen_y
*tty_char
.x
))*2)+1] = attrib
;
383 if (tty
->screen_x
>= tty_char
.x
) {
384 tty
->screen_x
-= tty_char
.x
;
388 if (tty
->screen_y
>= tty_char
.y
) {
390 /* save old screen, but with one line offset */
391 for (i
= 0; i
< (tty_char
.x
* tty_char
.y
* 2) - (tty_char
.x
* 2); i
++)
392 con_screen
[i
] = tty
->screen
[i
+(tty_char
.x
* 2)];
393 /* copy new image into tty screen */
394 for (i
= 0; i
< (tty_char
.x
* tty_char
.y
* 2); i
++)
395 tty
->screen
[i
] = con_screen
[i
];
397 tty
->screen_y
= tty_char
.y
- 1;
407 /* create Message Of The Day in /etc/motd */
410 vfs_list_add ("motd", VFS_FILEATTR_FILE
| VFS_FILEATTR_READ
| VFS_FILEATTR_SYSTEM
, "/etc/");
412 char *motd
= "\t\t\t\tWelcome !\n"
413 "This is ZeX/OS - operating system created by Tomas 'ZeXx86' Jedrzejek\n"
414 "Please visit web page www.zexos.org for more information ..\n"
415 "Latest source code is available on git repository: git://repo.or.cz/ZeXOS.git\n"
416 "You can display list of the built-in commands by \"help\".\n"
417 "Default login name is \"root\" secured by password \"root\".\n";
419 vfs_content_t content
;
421 content
.len
= strlen (motd
);
423 vfs_mmap ("/etc/motd", 9, &content
);
427 vfs_cat ("/etc/motd", 9);
434 /* virtual terminal (device handler) */
435 bool vterm_acthandler (unsigned act
, char *block
, unsigned block_len
)
440 dev_flags_t
*flags
= (dev_flags_t
*) block
;
445 if (block_len
!= sizeof (dev_flags_t
))
448 tty_t
*tty
= tty_find ((char *) flags
->iomem
);
453 flags
->iomem
= (void *) tty
->screen
;
454 flags
->iolen
= tty_char
.x
* tty_char
.y
* 2;
464 /* create new tty console */
467 /* Software limit - max. 1K of ttys */
468 if (tty_count
> TTY_COUNT_MAX
)
472 sprintf (name
, "tty%d", tty_count
);
474 unsigned name_len
= strlen (name
);
476 /* alloc and init context */
477 tty_t
*tty
= (tty_t
*) kmalloc (sizeof (tty_t
));
482 memset (tty
, 0, sizeof (tty_t
));
484 tty
->screen
= (char *) kmalloc (tty_char
.x
* tty_char
.y
* 2);
491 tty
->name
= (char *) kmalloc (sizeof (char) * name_len
+ 1);
499 memcpy (tty
->name
, name
, name_len
);
500 tty
->name
[name_len
] = '\0';
502 memset (tty
->screen
, 0, tty_char
.x
* tty_char
.y
* 2);
513 strcpy (tty
->pwd
, (char *) env_get ("PWD"));
515 tty
->task
= (task_t
*) task_create (name
, (unsigned) task_tty
, 32);
518 tty
->next
= &tty_list
;
519 tty
->prev
= tty_list
.prev
;
520 tty
->prev
->next
= tty
;
521 tty
->next
->prev
= tty
;
523 /* create device which correspnd with current TTY */
524 dev_register (name
, "Virtual terminal", DEV_ATTR_CHAR
, (dev_handler_t
*) &vterm_acthandler
);
531 /* thread for running tty consoles - handle selected console */
532 void task_thread (tty_t
*tty
)
543 /* handle console on kernel level */
552 /* VESA mode - graphical font */
557 unsigned m
= tty_char
.x
* tty_char
.y
;
560 gputch (1+(j
*tty_font
.x
), 11+(k
*(tty_font
.y
+1)), vesa_16cto16b (tty
->screen
[i
*2+1] & 0x0F), tty
->screen
[i
*2]);
564 if (j
>= tty_char
.x
) {
577 /* startup function for created tty console */
578 static unsigned task_tty ()
580 tty_t
*tty
= tty_findbytask (_curr_task
);
589 /* change tty console to next (+) or previous (-) in list */
590 void tty_switch (char act
)
595 tty_t
*tty
= currtty
->next
;
597 if (tty
== &tty_list
)
607 tty_t
*tty
= currtty
->prev
;
609 if (tty
== &tty_list
)
620 /* special function for initialization of graphical tty */
623 tty_t
*tty
= tty_create ();
630 /* initialization of tty */
631 unsigned int init_tty ()
633 /* pre-set tty list */
634 tty_list
.next
= &tty_list
;
635 tty_list
.prev
= &tty_list
;
640 tty_font_set ((char *) font5x8
, 5, 8);
643 case 0: /* VGA-Textual console */
644 tty_char_set (80, 25);
646 case 1: /* VGA console */
647 tty_char_set (53, 18);
649 case 2: /* VESA concole */
650 tty_char_set (80, 65);
656 /* create consoles */
657 tty0
= tty_create ();
663 tty1
= tty_create ();
670 /* printf MOTD into TTY0 console */
673 /* then print login messages */
674 tty_write (tty0
, "\nlogin: ", 8);
676 tty_write (tty1
, "\nlogin: ", 8);