Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

console.c

Go to the documentation of this file.
00001 /*!     \file kernel/console.c
00002  *      \brief Virtual consoles.
00003  *      \author Andrea Righi <drizzt@inwind.it>
00004  *      \date Last update: 2003-11-01
00005  *      \note Copyright (&copy;) 2003 Andrea Righi
00006  */
00007 
00008 #include <const.h>
00009 #include <stdarg.h>
00010 #include <stdio.h>
00011 
00012 #include <arch/mem.h>
00013 
00014 #include <kernel/keyboard.h>
00015 #include <kernel/kmalloc.h>
00016 #include <kernel/task.h>
00017 #include <kernel/video.h>
00018 
00019 #include <kernel/console.h>
00020 
00021 //! The address of the video buffer in memory.
00022 #define VIDEO_BUFFER    VIDEO_MEM_ADDRESS
00023 //! The size of the video buffer (in standard text-mode).
00024 #define VIDEO_BUF_DIM   (80*25)
00025 
00026 //! Virual consoles buffer (console #0 is the special console).
00027 static console_t vir_cons[K_TOT_VIR_CONS+1];
00028 //! Index of the current console.
00029 static int curr_cons=1;
00030 
00031 // --- Consoles managemet operators ---                                 //
00032 
00033 //! \brief Get the address of a console structure.
00034 //! \param c The console id.
00035 //! \return The address of the console structure.
00036 //! \exception NULL Console \p c does not exists.
00037 console_t *get_console_addr(int c)
00038 {
00039         if ( (c>=0) && (c<=K_TOT_VIR_CONS) )
00040                 return( (console_t *)(&vir_cons[c]) );
00041 
00042         return((console_t *)NULL);
00043 }
00044 
00045 //! \brief Get the current console id.
00046 //! \return The current console id.
00047 int get_curr_console()
00048 {
00049         return(curr_cons);
00050 }
00051 
00052 //! \brief Set the current console.
00053 //! \param c The console id to set as current console.
00054 //! \return
00055 //!     \li #TRUE if \p c is a valid console id.
00056 //!     \li #FALSE if  \p c is not a valid console id.
00057 bool set_curr_console(int c)
00058 {
00059         if ( (c > 0) && (c <= K_TOT_VIR_CONS) )
00060                 curr_cons = c;
00061         else
00062                 return(FALSE);
00063 
00064         return(TRUE);
00065 }
00066 
00067 //! \brief Change the current console and refresh the screen.
00068 //! \param c The console to switch to.
00069 //! \return
00070 //!     \li #TRUE if \p c is a valid console id.
00071 //!     \li #FALSE if  \p c is not a valid console id.
00072 bool switch_to_console(int c)
00073 {
00074         if ( c == curr_cons)
00075                 // Attempt to switch to the current console             //
00076                 return(TRUE);
00077 
00078         // Copy old console parameters from video memory                //
00079         memcpyw(vir_cons[curr_cons].vid_buffer, vir_cons[0].vid_buffer, VIDEO_BUF_DIM);
00080         vir_cons[curr_cons].cur_pos = vir_cons[0].cur_pos;
00081         vir_cons[curr_cons].cur_color = vir_cons[0].cur_color;
00082 
00083         if ( set_curr_console(c) )
00084         {
00085                 // Set new console parameters to the video memory       //
00086                 memcpyw(vir_cons[0].vid_buffer, vir_cons[c].vid_buffer, VIDEO_BUF_DIM);
00087                 k_set_cur_pos( (console_t *)(&vir_cons[0]), vir_cons[c].cur_pos );
00088                 k_set_color( (console_t *)(&vir_cons[0]), vir_cons[c].cur_color );
00089 
00090                 // Set keyboard parameters of the new selected console  //
00091                 update_leds();
00092 
00093                 return(TRUE);
00094         }
00095         return(FALSE);
00096 }
00097 
00098 // --- Video operators ---                                              //
00099 
00100 //! \brief Clear the screen.
00101 void clrscr()
00102 {
00103         task_t *p;
00104 
00105         if ( !(p=get_curr_task()) )
00106         {
00107                 // If there is no task clear the main console           //
00108                 k_clrscr( (console_t *)(&vir_cons[0]) );
00109                 return;
00110         }
00111 
00112         if ( p->console==curr_cons )
00113                 // The task is running in the current console.          //
00114                 // So clear the main console                            //
00115                 k_clrscr( (console_t *)(&vir_cons[0]) );
00116         else
00117                 // The task is not running in the current console.      //
00118                 // Clear the task console                               //
00119                 k_clrscr( (console_t *)(&vir_cons[p->console]) );
00120 }
00121 
00122 //! \brief Get the current text color of the console.
00123 //! \return The value of the color.
00124 byte get_color()
00125 {
00126         task_t *p;
00127 
00128         if ( !(p=get_curr_task()) )
00129         {
00130                 // If there is no task return the main console color    //
00131                 return( k_get_color((console_t *)(&vir_cons[0])) );
00132         }
00133 
00134         if ( p->console==curr_cons )
00135                 // The task is running in the current console.          //
00136                 // So return the main console color                     //
00137                 return( k_get_color((console_t *)(&vir_cons[0])) );
00138         else
00139                 // The task is not running in the current console.      //
00140                 // Return the task console color                        //
00141                 return( k_get_color((console_t *)(&vir_cons[p->console])) );
00142 }
00143 
00144 //! \brief Set the text color for the current console.
00145 //! \param attrib The color value.
00146 void set_color(byte attrib)
00147 {
00148         task_t *p;
00149 
00150         if ( !(p=get_curr_task()) )
00151         {
00152                 // If there is no task set the main console color       //
00153                 k_set_color( (console_t *)(&vir_cons[0]), attrib );
00154                 return;
00155         }
00156 
00157         if ( p->console==curr_cons )
00158                 // The task is running in the current console.          //
00159                 // So set the main console color                        //
00160                 k_set_color( (console_t *)(&vir_cons[0]), attrib );
00161         else
00162                 // The task is not running in the current console.      //
00163                 // Set the task console color                           //
00164                 k_set_color( (console_t *)(&vir_cons[p->console]), attrib );
00165 }
00166 
00167 //! \brief Scroll the console up.
00168 void scroll_up()
00169 {
00170         task_t *p;
00171 
00172         if ( !(p=get_curr_task()) )
00173         {
00174                 // If there is no task execute in the video buffer      //
00175                 k_scroll_up( (console_t *)(&vir_cons[0]) );
00176                 return;
00177         }
00178 
00179         if ( p->console==curr_cons )
00180                 // The task is running in the current console.          //
00181                 // So execute directly to the video memory              //
00182                 k_scroll_up( (console_t *)(&vir_cons[0]) );
00183         else
00184                 // The task is not running in the current console.      //
00185                 // Execute into its video buffer                        //
00186                 k_scroll_up( (console_t *)(&vir_cons[p->console]) );
00187 }
00188 
00189 //! \brief Place the cursor at the (\p x, \p y) coordinates.
00190 //! \note
00191 //!     If \p x or \p y get a value of -1 they does not change
00192 //!     their value.
00193 void gotoxy(int x, int y)
00194 {
00195         task_t *p;
00196 
00197         if ( !(p=get_curr_task()) )
00198         {
00199                 // If there is no task print to the video buffer        //
00200                 k_gotoxy((console_t *)(&vir_cons[0]), x, y);
00201                 return;
00202         }
00203 
00204         if ( p->console==curr_cons )
00205                 // The task is running in the current console.          //
00206                 // So execute directly to the video memory              //
00207                 k_gotoxy((console_t *)(&vir_cons[0]), x, y);
00208         else
00209                 // The task is not running in the current console.      //
00210                 // Execute into its video buffer                        //
00211                 k_gotoxy((console_t *)(&vir_cons[p->console]), x, y);
00212 }
00213 
00214 //! \brief Put a character on the current console.
00215 //! \param c The ASCII value of the character.
00216 void kputchar( int c )
00217 {
00218         task_t *p = get_curr_task();
00219         uint32_t IF = GET_IF();
00220 
00221         if ( !p )
00222         {
00223                 // If there is no task print to the video buffer.
00224                 _kputchar((console_t *)(&vir_cons[0]), c);
00225                 return;
00226         }
00227 
00228         disable();
00229 
00230         if ( p->console==curr_cons )
00231                 // The task is running in the current console.
00232                 // So print directly to the video memory.
00233                 _kputchar((console_t *)(&vir_cons[0]), c);
00234         else
00235                 // The task is not running in the current console.
00236                 // Print into its video buffer.
00237                 _kputchar((console_t *)(&vir_cons[p->console]), c);
00238 
00239         SET_IF( IF );
00240 }
00241 
00242 //! \brief Print a string to the current console.
00243 //! \param message The format of the message.
00244 //! \param ... The variables to print.
00245 int kprintf(const char *fmt, ...)
00246 {
00247         char buf[1024];
00248         va_list args;
00249         int i;
00250 
00251         va_start( args, fmt );
00252 
00253         vsnprintf( buf, sizeof(buf), fmt, args );
00254         va_end( args );
00255 
00256         for( i=0; i<1024; i++ )
00257         {
00258                 if( !buf[i] ) break;
00259                 kputchar( buf[i] );
00260         }
00261 
00262         return( i );
00263 }
00264 
00265 // --- Initialization routines ---------------------------------------- //
00266 
00267 //! \brief Initialize the special console #0 as the video memory buffer.
00268 void init_main_console()
00269 {
00270         vir_cons[0].vid_buffer = (word *)VIDEO_BUFFER;
00271         vir_cons[0].cur_pos = 0;
00272         vir_cons[0].cur_color = DEFAULT_COLOR;
00273 
00274         // Clear the screen                                             //
00275         k_clrscr( (console_t *)(&vir_cons[0]) );
00276 }
00277 
00278 //! \brief Allocate and initialize memory space for virtual consoles.
00279 //! \note
00280 //!     Before you can run this procedure you have to initialize the
00281 //!     virtual memory manager.
00282 void create_virtual_console()
00283 {
00284         int i;
00285 
00286         for (i=1; i < (K_TOT_VIR_CONS+1); i++)
00287         {
00288                 vir_cons[i].vid_buffer = kmalloc( VIDEO_BUF_DIM*sizeof(word) );
00289                 memsetw(vir_cons[i].vid_buffer, 0, VIDEO_BUF_DIM);
00290                 vir_cons[i].cur_pos = 0;
00291                 vir_cons[i].cur_color = DEFAULT_COLOR;
00292         }
00293 
00294         // Copy the current video parameters into the first     //
00295         // virtual console.                                     //
00296         memcpyw(vir_cons[1].vid_buffer, vir_cons[0].vid_buffer, VIDEO_BUF_DIM);
00297         vir_cons[1].cur_pos = vir_cons[0].cur_pos;
00298         vir_cons[1].cur_color = vir_cons[0].cur_color;
00299 
00300         // Automatic switch to the first virtual console        //
00301         curr_cons = 1;
00302 }

Generated on Fri Feb 20 15:32:15 2004 for Minirighi by doxygen1.2.18