-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbtools.c
247 lines (213 loc) · 6.1 KB
/
fbtools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* some generic framebuffer device stuff
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/kd.h>
#include <linux/vt.h>
#include <linux/fb.h>
#include <cairo.h>
#include "fbtools.h"
#include "logind.h"
/* -------------------------------------------------------------------- */
/* internal variables */
struct fb_fix_screeninfo fb_fix;
struct fb_var_screeninfo fb_var;
unsigned char *fb_mem;
int fb_mem_offset = 0;
cairo_format_t fb_format = CAIRO_FORMAT_INVALID;
static int fb;
static int kd_mode;
static bool kd_mode_restore = false;
static struct termios term;
static struct fb_var_screeninfo fb_ovar;
static unsigned short ored[256], ogreen[256], oblue[256];
static struct fb_cmap ocmap = { 0, 256, ored, ogreen, oblue };
static unsigned short p_red[256], p_green[256], p_blue[256];
static struct fb_cmap p_cmap = { 0, 256, p_red, p_green, p_blue };
/* -------------------------------------------------------------------- */
/* palette handling */
static unsigned short color_scale(int n, int max)
{
int ret = 65535.0 * (float)n/(max);
if (ret > 65535) ret = 65535;
if (ret < 0) ret = 0;
return ret;
}
static void fb_linear_palette(int r, int g, int b)
{
int i, size;
size = 256 >> (8 - r);
for (i = 0; i < size; i++)
p_red[i] = color_scale(i,size);
p_cmap.len = size;
size = 256 >> (8 - g);
for (i = 0; i < size; i++)
p_green[i] = color_scale(i,size);
if (p_cmap.len < size)
p_cmap.len = size;
size = 256 >> (8 - b);
for (i = 0; i < size; i++)
p_blue[i] = color_scale(i,size);
if (p_cmap.len < size)
p_cmap.len = size;
}
static void fb_set_palette(void)
{
if (fb_fix.visual != FB_VISUAL_DIRECTCOLOR && fb_var.bits_per_pixel != 8)
return;
if (-1 == ioctl(fb,FBIOPUTCMAP,&p_cmap)) {
perror("ioctl FBIOPUTCMAP");
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* initialisation & cleanup */
static void
fb_memset (void *addr, int c, size_t len)
{
#if 1 /* defined(__powerpc__) */
unsigned int i, *p;
i = (c & 0xff) << 8;
i |= i << 16;
len >>= 2;
for (p = addr; len--; p++)
*p = i;
#else
memset(addr, c, len);
#endif
}
void fb_fini(void)
{
/* restore console */
if (-1 == ioctl(fb, FBIOPUT_VSCREENINFO, &fb_ovar))
perror("ioctl FBIOPUT_VSCREENINFO");
if (-1 == ioctl(fb, FBIOGET_FSCREENINFO, &fb_fix))
perror("ioctl FBIOGET_FSCREENINFO");
if (fb_ovar.bits_per_pixel == 8 ||
fb_fix.visual == FB_VISUAL_DIRECTCOLOR) {
if (-1 == ioctl(fb, FBIOPUTCMAP, &ocmap))
perror("ioctl FBIOPUTCMAP");
}
close(fb);
if (kd_mode_restore) {
if (-1 == ioctl(STDIN_FILENO, KDSETMODE, kd_mode))
perror("ioctl KDSETMODE");
}
tcsetattr(STDIN_FILENO, TCSANOW, &term);
}
/* -------------------------------------------------------------------- */
void fb_query(int cardno)
{
char device[64];
snprintf(device, sizeof(device), "/dev/fb%d", cardno);
fb = device_open(device);
if (-1 == ioctl(fb,FBIOGET_FSCREENINFO,&fb_fix)) {
perror("ioctl FBIOGET_FSCREENINFO");
exit(1);
}
if (-1 == ioctl(fb,FBIOGET_VSCREENINFO,&fb_var)) {
perror("ioctl FBIOGET_VSCREENINFO");
exit(1);
}
}
void fb_init(int cardno)
{
unsigned long page_mask;
char device[64];
snprintf(device, sizeof(device), "/dev/fb%d", cardno);
if (-1 == ioctl(STDIN_FILENO, KDGETMODE, &kd_mode)) {
perror("ioctl KDGETMODE");
} else {
kd_mode_restore = true;
}
/* get current settings (which we have to restore) */
fb = device_open(device);
if (-1 == ioctl(fb,FBIOGET_VSCREENINFO,&fb_ovar)) {
perror("ioctl FBIOGET_VSCREENINFO");
exit(1);
}
if (-1 == ioctl(fb,FBIOGET_FSCREENINFO,&fb_fix)) {
perror("ioctl FBIOGET_FSCREENINFO");
exit(1);
}
if (-1 == ioctl(fb,FBIOGET_VSCREENINFO,&fb_var)) {
perror("ioctl FBIOGET_VSCREENINFO");
exit(1);
}
if (fb_ovar.bits_per_pixel == 8 ||
fb_fix.visual == FB_VISUAL_DIRECTCOLOR) {
if (-1 == ioctl(fb,FBIOGETCMAP,&ocmap)) {
perror("ioctl FBIOGETCMAP");
exit(1);
}
}
tcgetattr(STDIN_FILENO, &term);
/* checks & initialisation */
if (kd_mode_restore) {
if (-1 == ioctl(STDIN_FILENO, KDSETMODE, KD_GRAPHICS))
perror("ioctl KDSETMODE");
}
if (-1 == ioctl(fb,FBIOGET_FSCREENINFO,&fb_fix)) {
perror("ioctl FBIOGET_FSCREENINFO");
exit(1);
}
if (fb_fix.type != FB_TYPE_PACKED_PIXELS) {
fprintf(stderr,"can handle only packed pixel frame buffers\n");
goto err;
}
page_mask = getpagesize()-1;
fb_mem_offset = (unsigned long)(fb_fix.smem_start) & page_mask;
fb_mem = mmap(NULL,fb_fix.smem_len+fb_mem_offset,
PROT_READ|PROT_WRITE,MAP_SHARED,fb,0);
if (-1L == (long)fb_mem) {
perror("mmap");
goto err;
}
/* move viewport to upper left corner */
if (fb_var.xoffset != 0 || fb_var.yoffset != 0) {
fb_var.xoffset = 0;
fb_var.yoffset = 0;
if (-1 == ioctl(fb,FBIOPAN_DISPLAY,&fb_var)) {
perror("ioctl FBIOPAN_DISPLAY");
goto err;
}
}
/* cls */
fb_memset(fb_mem+fb_mem_offset, 0, fb_fix.line_length * fb_var.yres);
/* init palette */
switch (fb_var.bits_per_pixel) {
case 16:
fb_format = CAIRO_FORMAT_RGB16_565;
fb_linear_palette(5,6,5);
break;
case 32:
fb_format = CAIRO_FORMAT_RGB24;
fb_linear_palette(8,8,8);
break;
default:
fprintf(stderr, "unsupported framebuffer format (%d bpp)\n",
fb_var.bits_per_pixel);
goto err;
}
fb_set_palette();
return;
err:
fb_fini();
exit(1);
}