# tty.c

Go to the documentation of this file.

    00001 /*****************************************************************************************
    00002  Copyright (c) 2002-2004 The UbixOS Project
    00003  All rights reserved.
    00004
    00005  Redistribution and use in source and binary forms, with or without modification, are
    00006  permitted provided that the following conditions are met:
    00007
    00008  Redistributions of source code must retain the above copyright notice, this list of
    00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
    00010  form must reproduce the above copyright notice, this list of conditions, the following
    00011  disclaimer and the list of authors in the documentation and/or other materials provided
    00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
    00013  contributors may be used to endorse or promote products derived from this software
    00014  without specific prior written permission.
    00015
    00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00025
    00026  $Id: tty_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
    00027
    00028 *****************************************************************************************/
    00029
    00030 #include <ubixos/tty.h>
    00031 #include <ubixos/kpanic.h>
    00032 #include <ubixos/spinlock.h>
    00033 #include <lib/kprintf.h>
    00034 #include <lib/kmalloc.h>
    00035 #include <sys/io.h>
    00036 #include <string.h>
    00037
    00038 static tty_term  *terms          = 0x0;
    00039 tty_term         *tty_foreground = 0x0;
    00040 static spinLock_t tty_spinLock   = SPIN_LOCK_INITIALIZER;
    00041
    00042 int tty_init() {
    00043   int i = 0x0;
    00044
    00045   /* Allocate memory for terminals */
    00046   terms = (tty_term *)kmalloc(sizeof(tty_term)*TTY_MAX_TERMS);
    00047   if (terms == 0x0)
    00048     kpanic("tty_init: Failed to allocate memory. File: %s, Line: %i\n",__FILE__,__LINE__);
    00049
    00050   /* Set up all default terminal information */
    00051   for (i = 0;i < TTY_MAX_TERMS;i++) {
    00052     terms[i].tty_buffer = (char *)kmalloc(80*60*2);
    00053     if (terms[i].tty_buffer == 0x0)
    00054       kpanic("tty_init: Failed to allocate buffer memory. File: %s, Line: %i\n",__FILE__,__LINE__);
    00055
    00056     terms[i].tty_pointer = terms[i].tty_buffer;   /* Set up tty pointer to internal buffer */
    00057     terms[i].tty_x       = 0x0;                   /* Set up default X position             */
    00058     terms[i].tty_y       = 0x0;                   /* Set up default Y position             */
    00059     terms[i].tty_colour  = 0x0A + i;              /* Set up default tty text colour        */
    00060     }
    00061
    00062   /* Read tty0 current position (to migrate from kprintf). */
    00063   outportByte(0x3D4, 0x0e);
    00064   terms[0].tty_y = inportByte(0x3D5);
    00065   outportByte(0x3D4, 0x0f);
    00066   terms[0].tty_x = inportByte(0x3D5);
    00067
    00068
    00069   /* Set up pointer for the foreground tty */
    00070   tty_foreground = &terms[0];
    00071
    00072   /* Set up the foreground ttys information */
    00073   tty_foreground->tty_pointer = (char *)0xB8000;
    00074
    00075   /* Return to let kernel know initialization is complete */
    00076   kprintf("tty0 - Initialized\n");
    00077
    00078   return(0x0);
    00079   }
    00080
    00081
    00082  /*
    00083   This will change the specified tty. It ultimately copies the screen
    00084   to the foreground buffer copies the new ttys buffer to the screen and
    00085   adjusts a couple pointers and we are good to go.
    00086  */
    00087 int tty_change(uInt16 tty) {
    00088
    00089   if (tty > TTY_MAX_TERMS)
    00090     kpanic("Error: Changing to an invalid tty. File: %s, Line: %i\n",__FILE__,__LINE__);
    00091
    00092   /* Copy display buffer to tty buffer */
    00093   memcpy(tty_foreground->tty_buffer,(char *)0xB8000,(80*60*2));
    00094
    00095   /* Copy new tty buffer to display buffer */
    00096   memcpy((char *)0xB8000,terms[tty].tty_buffer,(80*60*2));
    00097
    00098   /*
    00099    Set the tty_pointer to the internal buffer so I can continue
    00100    writing to what it believes is the screen
    00101   */
    00102   tty_foreground->tty_pointer = tty_foreground->tty_buffer;
    00103
    00104   terms[tty].tty_pointer = (char *)0xB8000;
    00105
    00106   /* set new foreground tty */
    00107   tty_foreground = &terms[tty];
    00108
    00109   /* Adjust cursor when we change consoles */
    00110   outportByte(0x3D4, 0x0F);
    00111   outportByte(0x3D5, tty_foreground->tty_x);
    00112   outportByte(0x3D4, 0x0E);
    00113   outportByte(0x3D5, tty_foreground->tty_y);
    00114
    00115   return(0x0);
    00116   }
    00117
    00118 int tty_print(char *string,tty_term *term) {
    00119   unsigned int    bufferOffset = 0x0, character = 0x0, i = 0x0;
    00120   spinLock(&tty_spinLock);
    00121
    00122   /* We Need To Get The Y Position */
    00123   bufferOffset = term->tty_y;
    00124   bufferOffset <<= 8;
    00125
    00126   /* Then We Need To Add The X Position */
    00127   bufferOffset += term->tty_x;
    00128   bufferOffset <<= 1;
    00129
    00130   while ((character = *string++)) {
    00131     switch (character) {
    00132     case '\n':
    00133       bufferOffset = (bufferOffset / 160) * 160 + 160;
    00134       break;
    00135     default:
    00136       term->tty_pointer[bufferOffset++] = character;
    00137       term->tty_pointer[bufferOffset++] = term->tty_colour;
    00138       break;
    00139     }  /* switch */
    00140
    00141     /* Check To See If We Are Out Of Bounds */
    00142     if (bufferOffset >= 160 * 25) {
    00143       for (i = 0; i < 160 * 24; i++) {
    00144         term->tty_pointer[i] = term->tty_pointer[i + 160];
    00145         }
    00146       for (i = 0; i < 80; i++) {
    00147         term->tty_pointer[(160 * 24) + (i * 2)] = 0x20;
    00148         term->tty_pointer[(160 * 24) + (i * 2) + 1] = term->tty_colour;
    00149         }
    00150       bufferOffset -= 160;
    00151       }
    00152     }
    00153
    00154   bufferOffset >>= 1;  /* Set the new cursor position  */
    00155   term->tty_x = (bufferOffset &  0xFF);
    00156   term->tty_y = (bufferOffset >> 0x8);
    00157
    00158   if (term == tty_foreground) {
    00159     outportByte(0x3D4, 0x0f);
    00160     outportByte(0x3D5, term->tty_x);
    00161     outportByte(0x3D4, 0x0e);
    00162     outportByte(0x3D5, term->tty_y);
    00163     }
    00164
    00165   spinUnlock(&tty_spinLock);
    00166
    00167   return(0x0);
    00168   }
    00169
    00170 tty_term *tty_find(uInt16 tty) {
    00171   return(&terms[tty]);
    00172   }
    00173
    00174 /***
    00175  END
    00176  ***/