File: acceltest.c

package info (click to toggle)
joystick 20051019-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 200 kB
  • ctags: 153
  • sloc: ansic: 2,441; makefile: 24; sh: 15
file content (170 lines) | stat: -rw-r--r-- 3,940 bytes parent folder | download | duplicates (3)
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
/* $Header: /cvsroot/linuxconsole/ruby/utils/acceltest.c,v 1.11 2004/10/19 07:51:52 vojtech Exp $ */
/*
 * Copyright (C) 2001 Romain Dolbeau <dolbeau@irisa.fr>
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License. See the file COPYING in the main directory of this archive for
 *  more details.
 *
 */

#include <stdio.h>

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
extern char *optarg;
extern int optind, opterr, optopt;

#include <errno.h>
int errno;

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <setjmp.h>
#include <sys/mman.h>
#include <asm/page.h>
#include <asm/byteorder.h>

#include <linux/fb.h>
#include <linux/fbvid.h>



int
main(int argc, char **argv)
{
	char fbdevice[256];
	int fbnum = 0, opnu = 0, sx = 128, sy = 128, wi = 64, he = 64, color = 0xFFFFFFFF, nsx, nsy, count = 0;
	unsigned char *fb_mem = NULL;
	int fb_mem_offset = 0, fbd, r;
	char *tmpoptarg;
	struct fb_var_screeninfo fb_var;
	struct fb_fix_screeninfo fb_fix;
	struct fb_fillrect fbfr;
	struct fb_copyarea fbca;
	
	while ((opnu = getopt(argc, argv, "f:x:y:w:h:c:?")) != EOF)
	{
		switch(opnu)
		{
		case 'f':
			tmpoptarg = optarg;
			if (!(strncmp(tmpoptarg, "/dev/", 5)))
				tmpoptarg += 5;
			if (!(strncmp(tmpoptarg, "fb", 2)))
				tmpoptarg += 2;
			fbnum = atoi(tmpoptarg);
			break;
		case 'x':
			sx = atoi(optarg);
			break;
		case 'y':
			sy = atoi(optarg);
			break;
		case 'w':
			wi = atoi(optarg);
			break;
		case 'h':
			he = atoi(optarg);
			break;
		case 'c':
			color = atoi(optarg);
			break;
		case '?':
			printf("Usage: %s [-f <fb_device>] [-x <initial_X>] [-y <initial_Y>] [-w <width>] [-h <height>] [-c <colour>]\n", argv[0]);
			return(0);
		default:
			fprintf(stderr, "Warning: Unknown option \"%c\", try %s -?\n", opnu, argv[0]);
			exit(1);
		}
	}

	fprintf(stderr, "Opening /dev/fb%d\n", fbnum);
	
	sprintf(fbdevice, "/dev/fb%d", fbnum);
	fbd = open(fbdevice, O_RDWR);
	
	if (fbd < 0)
	{
		fprintf(stderr, "Couldn't open /dev/fb%d; errno: %d (%s)\n", fbnum, errno, strerror(errno));
		exit(1);
	}
	r = ioctl(fbd, FBIOGET_VSCREENINFO, &fb_var);
	if (r < 0)
	{
		fprintf(stderr, "IOCTL FBIOGET_VSCREENINFO error: %d errno: %d (%s)\n", r, errno, strerror(errno));
		exit(1);
	}
	r = ioctl(fbd, FBIOGET_FSCREENINFO, &fb_fix);
	if (r < 0)
	{
		fprintf(stderr, "IOCTL FBIOGET_FSCREENINFO error: %d errno: %d (%s)\n", r, errno, strerror(errno));
		exit(1);
	}
	/* map all FB memory */
	fb_mem_offset = (unsigned long)(fb_fix.smem_start) & (~PAGE_MASK);
	fb_mem = mmap(NULL,fb_fix.smem_len+fb_mem_offset,PROT_WRITE,MAP_SHARED,fbd,0);
	if (!fb_mem)
	{
		fprintf(stderr, "MMap of /dev/fb%d failed\n", fbnum);
		exit(1);
	}
	if (((sx + wi) > fb_var.xres) || ((sy + he) > fb_var.yres))
	{
		fprintf(stderr, "Rectangle too big for given offset (%dx%d+%d+%d, fb is %dx%d)",
			he, wi, sx, sy,
			fb_var.xres, fb_var.xres);
		exit(1);
	}
	fprintf(stderr, "Filling rect with color 0x%x\n", color);
	fbfr.dx = sx;
	fbfr.dy = sy;
	fbfr.width = wi;
	fbfr.height = he;
	fbfr.color = color;
	fbfr.rop = ROP_COPY;
	r = ioctl(fbd, FBIOPUT_FILLRECT, &fbfr);
	if (r < 0)
	{
		fprintf(stderr, "IOCTL FBIOPUT_FILLRECT error: %d errno: %d (%s)\n", r, errno, strerror(errno));
		exit(1);
	}
	if ((sx == 0) || (sy == 0))
	{
		fprintf(stderr, "Can't CopyArea, stop\n");
		return(0);
	}	
	nsx = sx; nsy = sy;
	fbca.width = wi + 1;
	fbca.height = he + 1;
	while (((nsx + wi) < fb_var.xres) ||
	       ((nsy + he) < fb_var.yres))
	{
		count++;
		if ((nsx + wi) < fb_var.xres)
		{
			sx = nsx;
			nsx++;
		}
		if ((nsy + he) < fb_var.yres)
		{
			sy = nsy;
			nsy++;
		}
		fbca.sx = sx - 1;
		fbca.sy = sy - 1;
		fbca.dx = nsx;
		fbca.dy = nsy;
		r = ioctl(fbd, FBIOPUT_COPYAREA, &fbca);
		if (r < 0)
		{
			fprintf(stderr, "IOCTL FBIOPUT_FILLRECT #%d error: %d errno: %d (%s)\n",
				count, r, errno, strerror(errno));
			exit(1);
		}
	}
	return(0);
}