-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.c
283 lines (241 loc) · 7.52 KB
/
main.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Simple framebuffer testing program
Set the screen to a given color. For use in developing Linux
display drivers.
Copyright (c) 2014, Jumpnow Technologies, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ideas taken from the Yocto Project psplash program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <getopt.h>
struct fb_config
{
int fd;
int width;
int height;
int height_virtual;
int bpp;
int stride;
int red_offset;
int red_length;
int green_offset;
int green_length;
int blue_offset;
int blue_length;
int transp_offset;
int transp_length;
int buffer_num;
char *data;
char *base;
};
void dump_vscreeninfo(struct fb_var_screeninfo *fvsi)
{
printf("======= FB VAR SCREENINFO =======\n");
printf("xres: %d\n", fvsi->xres);
printf("yres: %d\n", fvsi->yres);
printf("yres_virtual: %d\n", fvsi->yres_virtual);
printf("buffer number: %d\n", fvsi->yres_virtual / fvsi->yres);
printf("bpp : %d\n", fvsi->bits_per_pixel);
printf("red bits :\n");
printf(" offset : %d\n", fvsi->red.offset);
printf(" length : %d\n", fvsi->red.length);
printf(" msb_right: %d\n", fvsi->red.msb_right);
printf("green bits :\n");
printf(" offset : %d\n", fvsi->green.offset);
printf(" length : %d\n", fvsi->green.length);
printf(" msb_right: %d\n", fvsi->green.msb_right);
printf("blue bits :\n");
printf(" offset : %d\n", fvsi->blue.offset);
printf(" length : %d\n", fvsi->blue.length);
printf(" msb_right: %d\n", fvsi->blue.msb_right);
printf("transp bits :\n");
printf(" offset : %d\n", fvsi->transp.offset);
printf(" length : %d\n", fvsi->transp.length);
printf(" msb_right: %d\n", fvsi->transp.msb_right);
printf("=================================\n");
}
void dump_fscreeninfo(struct fb_fix_screeninfo *ffsi)
{
printf("======= FB FIX SCREENINFO =======\n");
printf("id : %s\n", ffsi->id);
printf("smem_start : 0x%08lX\n", ffsi->smem_start);
printf("smem_len : %u\n", ffsi->smem_len);
printf("line_length : %u\n", ffsi->line_length);
printf("=================================\n");
}
void plot_pixel(struct fb_config *fb, int x, int y, int r, int g, int b)
{
int offset = (y * fb->stride) + (x * (fb->bpp >> 3));
int r_o = fb->red_offset/fb->red_length;
int g_o = fb->green_offset/fb->green_length;
int b_o = fb->blue_offset/fb->blue_length;
*(fb->data + offset + r_o) = r;
*(fb->data + offset + g_o) = g;
*(fb->data + offset + b_o) = b;
if (fb->transp_length != 0)
*(fb->data + offset + fb->transp_offset/fb->transp_length) = 255;
}
void draw_rect(struct fb_config *fb, int x, int y, int w, int h, int r, int g, int b)
{
int dx, dy;
for (dy = 0; dy < h; dy++) {
for (dx = 0; dx < w; dx++) {
plot_pixel(fb, x + dx, y + dy, r, g, b);
}
}
}
void clear_screen(struct fb_config *fb, int r, int g, int b)
{
draw_rect(fb, 0, 0, fb->width, fb->height, r, g, b);
}
void usage(const char *argv_0)
{
printf("\nUsage %s: [-r<red>] [-g<green>] [-b<blue>] [-B<border>] [-i<buffer index>]\n", argv_0);
printf(" All colors default to 0xff\n");
printf(" The border color applies to all rgb and is 10 pixels wide\n");
printf(" If border is not provided, none is drawn.\n");
printf(" The index defaults to 0, if your FB has more than 1 buffers, you can set it.\n");
printf(" If set the index to -1, we will use all buffers for 20 times.\n");
exit(1);
}
int main(int argc, char **argv)
{
int fd;
unsigned long offset;
struct fb_var_screeninfo fvsi;
struct fb_fix_screeninfo ffsi;
struct fb_config fb;
int red = 0xff;
int green = 0xff;
int blue = 0xff;
int border = -1;
int opt;
int index = 0;
int loop = 0;
while ((opt = getopt(argc, argv, "r:g:b:B:i:h")) != -1) {
switch (opt) {
case 'r':
red = 0xff & strtol(optarg, NULL, 0);
break;
case 'g':
green = 0xff & strtol(optarg, NULL, 0);
break;
case 'b':
blue = 0xff & strtol(optarg, NULL, 0);
break;
case 'B':
border = 0xff & strtol(optarg, NULL, 0);
break;
case 'i':
index = strtol(optarg, NULL, 0);
break;
default:
usage(argv[0]);
break;
}
}
memset(&fb, 0, sizeof(fb));
if ((fd = open("/dev/fb0", O_RDWR)) < 0) {
perror("open");
exit(1);
}
if (ioctl(fd, FBIOGET_VSCREENINFO, &fvsi) < 0) {
perror("ioctl(FBIOGET_VSCREENINFO)");
close(fd);
exit(1);
}
dump_vscreeninfo(&fvsi);
if (ioctl(fd, FBIOGET_FSCREENINFO, &ffsi) < 0) {
perror("ioctl(FBIOGET_FSCREENINFO)");
close(fd);
exit(1);
}
dump_fscreeninfo(&ffsi);
fb.fd = fd;
fb.width = fvsi.xres;
fb.height = fvsi.yres;
fb.height_virtual = fvsi.yres_virtual;
fb.bpp = fvsi.bits_per_pixel;
fb.stride = ffsi.line_length;
fb.red_offset = fvsi.red.offset;
fb.red_length = fvsi.red.length;
fb.green_offset = fvsi.green.offset;
fb.green_length = fvsi.green.length;
fb.blue_offset = fvsi.blue.offset;
fb.blue_length = fvsi.blue.length;
fb.transp_offset = fvsi.transp.offset;
fb.transp_length = fvsi.transp.length;
fb.buffer_num = fb.height_virtual / fb.height;
if (index == -1) {
index = 0;
loop = 20;
}
if (index > fb.buffer_num - 1 || index < 0) {
printf("Invalid index.\n");
exit(1);
}
if (fb.red_length != 8 || fb.green_length != 8 || fb.blue_length != 8) {
printf("Don't support this color format\n");
goto SKIP_DRAW;
}
fb.base = (char *)mmap((caddr_t) NULL, ffsi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (fb.base == (char *)-1) {
perror("mmap");
close(fd);
exit(1);
}
offset = (unsigned long) ffsi.smem_start % (unsigned long) getpagesize();
do {
fb.data = fb.base + offset + (fb.stride * fb.height) * index;
if (border == -1) {
clear_screen(&fb, red, green, blue);
}
else {
clear_screen(&fb, border, border, border);
// draw a rect 10 pixels in from each border in the color chosen
draw_rect(&fb, 10, 10, fb.width - 20, fb.height - 20, red, green, blue);
}
fvsi.yoffset = index * fvsi.yres;
printf("data addr = %p, yoffset = %d\n", fb.data, fvsi.yoffset);
ioctl(fb.fd, FBIOPAN_DISPLAY, &fvsi);
if (fb.buffer_num > 1)
index = (index + 1) % fb.buffer_num;
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
}while(loop--);
SKIP_DRAW:
close(fd);
return 0;
}