Kernel/VTerm - Replace offset WritePos with Row,Col pair
[tpg/acess2.git] / KernelLand / Kernel / drv / vterm_output.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/vterm_input.c
6  * - Virtual Terminal - Input code
7  */
8 #include "vterm.h"
9 #include <api_drv_video.h>
10 #define DEBUG   0
11
12 // === CODE ===
13 /**
14  * \fn void VT_InitOutput()
15  * \brief Initialise Video Output
16  */
17 void VT_InitOutput()
18 {
19         giVT_OutputDevHandle = VFS_Open(gsVT_OutputDevice, VFS_OPENFLAG_WRITE);
20         if(giVT_OutputDevHandle == -1) {
21                 Log_Warning("VTerm", "Oh F**k, I can't open the video device '%s'", gsVT_OutputDevice);
22                 return ;
23         }
24         VT_SetResolution( giVT_RealWidth, giVT_RealHeight );
25         VT_SetTerminal( 0 );
26         VT_SetMode( VIDEO_BUFFMT_TEXT );
27         LOG("VTerm output initialised");
28 }
29
30 /**
31  * \brief Set video output buffer mode
32  */
33 void VT_SetMode(int Mode)
34 {
35         VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &Mode );
36 }
37
38 /**
39  * \fn void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
40  * \note Scrolls the framebuffer down by \a Count text lines
41  */
42 void VT_int_ScrollFramebuffer( tVTerm *Term, int Count )
43 {
44          int    tmp;
45         struct {
46                 Uint8   Op;
47                 Uint16  DstX, DstY;
48                 Uint16  SrcX, SrcY;
49                 Uint16  W, H;
50         } PACKED        buf;
51         
52         // Only update if this is the current terminal
53         if( Term != gpVT_CurTerm )      return;
54         
55         if( Count > Term->ScrollHeight )        Count = Term->ScrollHeight;
56         if( Count < -Term->ScrollHeight )       Count = -Term->ScrollHeight;
57         
58         // Switch to 2D Command Stream
59         tmp = VIDEO_BUFFMT_2DSTREAM;
60         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
61         
62         // BLIT to 0,0 from 0,giVT_CharHeight
63         buf.Op = VIDEO_2DOP_BLIT;
64         buf.SrcX = 0;   buf.DstX = 0;
65         // TODO: Don't assume character dimensions
66         buf.W = Term->TextWidth * giVT_CharWidth;
67         if( Count > 0 )
68         {
69                 buf.SrcY = (Term->ScrollTop+Count) * giVT_CharHeight;
70                 buf.DstY = Term->ScrollTop * giVT_CharHeight;
71         }
72         else    // Scroll up, move text down
73         {
74                 Count = -Count;
75                 buf.SrcY = Term->ScrollTop * giVT_CharHeight;
76                 buf.DstY = (Term->ScrollTop+Count) * giVT_CharHeight;
77         }
78         buf.H = (Term->ScrollHeight-Count) * giVT_CharHeight;
79         VFS_WriteAt(giVT_OutputDevHandle, 0, sizeof(buf), &buf);
80         
81         // Restore old mode (this function is only called during text mode)
82         tmp = VIDEO_BUFFMT_TEXT;
83         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETBUFFORMAT, &tmp);
84 }
85
86 void VT_int_UpdateCursor( tVTerm *Term, int bShow )
87 {
88         tVideo_IOCtl_Pos        csr_pos;
89
90         if( Term != gpVT_CurTerm )      return ;
91
92         if( !bShow )
93         {
94                 csr_pos.x = -1; 
95                 csr_pos.y = -1; 
96         }
97         else if( Term->Mode == TERM_MODE_TEXT )
98         {
99 //              if( !(Term->Flags & VT_FLAG_SHOWCSR)
100 //               && ( (Term->Flags & VT_FLAG_HIDECSR) || !Term->Node.ReadThreads)
101 //                )
102                 if( !Term->Text || Term->Flags & VT_FLAG_HIDECSR )
103                 {
104                         csr_pos.x = -1;
105                         csr_pos.y = -1;
106                 }
107                 else
108                 {
109                         const tVT_Pos   *wrpos = VT_int_GetWritePosPtr(Term);
110                         csr_pos.x = wrpos->Col;
111                         csr_pos.y = wrpos->Row - (Term->Flags & VT_FLAG_ALTBUF ? 0 : Term->ViewTopRow);
112                         if( 0 > csr_pos.y || csr_pos.y >= Term->TextHeight )
113                                 csr_pos.y = -1, csr_pos.x = -1;
114                 }
115         }
116         else
117         {
118                 csr_pos.x = Term->VideoCursorX;
119                 csr_pos.y = Term->VideoCursorY;
120         }
121         VFS_IOCtl(giVT_OutputDevHandle, VIDEO_IOCTL_SETCURSOR, &csr_pos);
122 }       
123
124 /**
125  * \fn void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
126  * \brief Updates the video framebuffer
127  */
128 void VT_int_UpdateScreen( tVTerm *Term, int UpdateAll )
129 {
130         // Only update if this is the current terminal
131         if( Term != gpVT_CurTerm )      return;
132         
133         switch( Term->Mode )
134         {
135         case TERM_MODE_TEXT: {
136                 size_t view_pos = (Term->Flags & VT_FLAG_ALTBUF) ? 0 : Term->ViewTopRow*Term->TextWidth;
137                 const tVT_Pos *wrpos = VT_int_GetWritePosPtr(Term);
138                 const tVT_Char *buffer = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltBuf : Term->Text;
139                 // Re copy the entire screen?
140                 if(UpdateAll) {
141                         VFS_WriteAt(
142                                 giVT_OutputDevHandle,
143                                 0,
144                                 Term->TextWidth*Term->TextHeight*sizeof(tVT_Char),
145                                 &buffer[view_pos]
146                                 );
147                 }
148                 // Only copy the current line
149                 else {
150                         size_t  ofs = wrpos->Row * Term->TextWidth;
151                         VFS_WriteAt(
152                                 giVT_OutputDevHandle,
153                                 (ofs - view_pos)*sizeof(tVT_Char),
154                                 Term->TextWidth*sizeof(tVT_Char),
155                                 &buffer[ofs]
156                                 );
157                 }
158                 break; }
159         case TERM_MODE_FB:
160                 break;
161         }
162         
163         VT_int_UpdateCursor(Term, 1);
164 }
165

UCC git Repository :: git.ucc.asn.au