- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / YFrameBuffer.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YFrameBuffer.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "framebuffer"
21 #include <ycp/y2log.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include "YFrameBuffer.h"
31
32
33 YFrameBuffer::YFrameBuffer( int width, int height, int depth )
34 {
35     _width      = width;
36     _height     = height;
37     _depth      = depth;
38
39     _fb         = 0;
40     _fb_fd      = -1;
41     _fb_len     = 0;
42
43     switch ( _depth )
44     {
45         case 15:
46         case 16: _bytesPerPixel = 2;
47             break;
48
49         case 24:
50         case 32: _bytesPerPixel = 4;
51             break;
52
53         default:
54             y2warning( "Pixel depth %d not supported", _depth );
55             _bytesPerPixel = 0;
56             break;
57     }
58
59     if ( _bytesPerPixel > 0 )
60         mmapFB();
61 }
62
63
64 YFrameBuffer::~YFrameBuffer()
65 {
66     munmapFB();
67 }
68
69
70 void YFrameBuffer::mmapFB()
71 {
72     if ( _fb )
73     {
74         y2error( "Framebuffer already mmap()'ed" );
75         return;
76     }
77
78     _fb_fd = open( "/dev/fb0", O_RDWR );
79
80     if ( _fb_fd < 0 )
81     {
82         y2error( "Can't open /dev/fb0 - errno %d: %s", errno, strerror( errno ) );
83         return;
84     }
85
86     _fb_len     = _width * _height * _bytesPerPixel;
87     _fb         = mmap( 0,                      // start
88                         _fb_len,                // length
89                         PROT_READ | PROT_WRITE, // prot
90                         MAP_SHARED,             // flags
91                         _fb_fd,                 // file descriptor
92                         0 );                    // offset
93
94     if ( _fb == MAP_FAILED )
95     {
96         y2error( "mmap() failed for /dev/fb0 - errno %d: %s", errno, strerror( errno ) );
97         _fb = 0;
98         close( _fb_fd );
99     }
100
101     _fb16 = (UINT16 *) _fb;
102     _fb32 = (UINT32 *) _fb;
103 }
104
105
106 void YFrameBuffer::munmapFB()
107 {
108     if ( ! _fb )
109         return;
110
111     munmap( _fb, _fb_len );
112     close( _fb_fd );
113
114     _fb         = 0;
115     _fb16       = 0;
116     _fb32       = 0;
117     _fb_fd      = -1;
118 }
119
120
121 void YFrameBuffer::setPixel( int x, int y, FBPixel pixel )
122 {
123     if ( ! _fb )
124         return;
125
126     if ( x < _width && y < _height )
127     {
128         if ( _bytesPerPixel == 2 )
129         {
130             pixel &= 0xFFFF;
131             _fb16[ y * _width + x ] = (UINT16) pixel;
132         }
133         else if ( _bytesPerPixel == 4 )
134         {
135             _fb32[ y * _width + x ] = (UINT32) pixel;
136         }
137     }
138 }
139
140
141 YFrameBuffer::FBPixel
142 YFrameBuffer::pixel( int x, int y )
143 {
144     if ( ! _fb )
145         return 0;
146
147     if ( x < _width && y < _height )
148     {
149         if ( _bytesPerPixel == 2 )
150         {
151             return (FBPixel) _fb16[ y * _width + x ];
152         }
153         else if ( _bytesPerPixel == 4 )
154         {
155             return (FBPixel) _fb32[ y * _width + x ];
156         }
157     }
158
159     return 0;
160 }
161
162
163
164