d2eb26a919638b3fc1610d2d492f3497a6405ad4
[tpg/acess2.git] / Usermode / Applications / axwin4_src / Server / video.cpp
1 /*
2  * Acess2 GUI v4
3  * - By John Hodge (thePowersGang)
4  *
5  * video.cpp
6  * - Graphics output
7  */
8 #include <cstddef>
9 #include <video.hpp>
10 #include <common.hpp>
11
12 extern "C" {
13 #include <acess/sys.h>
14 #include <acess/devices/pty.h>
15 #include <acess/devices/pty_cmds.h>
16 #include "resources/cursor.h"
17 }
18
19 namespace AxWin {
20
21 CVideo::CVideo(const CConfigVideo& config):
22         m_fd(1),
23         m_width(0),
24         m_height(0),
25         m_bufferFormat(PTYBUFFMT_TEXT)
26 {
27         // Obtain dimensions
28         {
29                 if( _SysIOCtl(m_fd, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL )
30                         throw AxWin::InitFailure("stdin isn't a terminal");
31                 struct ptydims  dims;
32                 if( _SysIOCtl(m_fd, PTY_IOCTL_GETDIMS, &dims) == -1 )
33                         throw AxWin::InitFailure("Failed to get dimensions from stdin");
34                 m_width = dims.PW;
35                 m_height = dims.PH;
36                 if( m_width == 0 || m_height == 0 )
37                         throw AxWin::InitFailure("Terminal not capable of graphics");
38         }
39         
40         SetCursorBitmap();
41         
42         SetCursorPos( m_width/2, m_height/2 );
43         
44         SetBufFormat(PTYBUFFMT_FB);
45 }
46
47 void CVideo::GetDims(unsigned int& w, unsigned int& h)
48 {
49         w = m_width;
50         h = m_height;
51 }
52
53 void CVideo::BlitLine(const uint32_t* src, unsigned int dst_y, unsigned int dst_x, unsigned int width)
54 {
55         //_SysWriteAt(m_fd, (dst_y * m_width + dst_x) * 4, width * 4, src);
56         SetBufFormat(PTYBUFFMT_FB);
57         _SysSeek(m_fd, (dst_y * m_width + dst_x) * 4, SEEK_SET);
58         _SysWrite(m_fd, src, width * 4);
59 }
60
61 void CVideo::Flush()
62 {
63         // TODO: Write to a local copy of the framebuffer in BlitLine, and then flush out in this function
64 }
65
66 void CVideo::SetBufFormat(unsigned int FormatID)
67 {
68         if( m_bufferFormat != FormatID )
69         {
70                 
71                 struct ptymode mode = {.OutputMode = FormatID, .InputMode = 0};
72                 int rv = _SysIOCtl( m_fd, PTY_IOCTL_SETMODE, &mode );
73                 if( rv ) {
74                         throw ::AxWin::InitFailure("Can't set PTY to framebuffer mode");
75                 }
76                 
77                 m_bufferFormat = FormatID;
78         }
79 }
80
81 void CVideo::SetCursorBitmap()
82 {
83         // Set cursor position and bitmap
84         struct ptycmd_header    hdr = {PTY2D_CMD_SETCURSORBMP,0,0};
85         size_t size = sizeof(hdr) + sizeof(cCursorBitmap) + cCursorBitmap.W*cCursorBitmap.H*4;
86         hdr.len_low = size / 4;
87         hdr.len_hi = size / (256*4);
88         
89         SetBufFormat(PTYBUFFMT_2DCMD);
90         _SysWrite(m_fd, &hdr, sizeof(hdr));
91         _SysDebug("SetCursorBitmap - size = %i (%04x:%02x * 4)", size, hdr.len_hi, hdr.len_low);
92         _SysWrite(m_fd, &cCursorBitmap, size-sizeof(hdr));
93 }
94
95 void CVideo::SetCursorPos(int X, int Y)
96 {
97         struct ptycmd_setcursorpos      cmd;
98         cmd.hdr.cmd = PTY2D_CMD_SETCURSORPOS;
99         cmd.hdr.len_low = sizeof(cmd)/4;
100         cmd.hdr.len_hi = 0;
101         cmd.x = X;
102         cmd.y = Y;
103
104         SetBufFormat(PTYBUFFMT_2DCMD);  
105         _SysWrite(m_fd, &cmd, sizeof(cmd));
106 }
107
108 };      // namespace AxWin
109

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