1/*-
2 * Copyright (c) 1998 Dag-Erling Co��dan Sm��rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: releng/10.3/sys/dev/syscons/rain/rain_saver.c 230132 2012-01-15 13:23:18Z uqs $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35#include <sys/syslog.h>
36#include <sys/consio.h>
37#include <sys/fbio.h>
38
39#include <dev/fb/fbreg.h>
40#include <dev/fb/splashreg.h>
41#include <dev/syscons/syscons.h>
42
43#define SAVER_NAME	 "rain_saver"
44#ifdef MAX
45#undef MAX
46#endif
47#define MAX		 63	/* number of colors (in addition to black) */
48#define INCREMENT	 4	/* increment between colors */
49
50#define RED(n)		 ((n) * 3 + 0)
51#define GREEN(n)	 ((n) * 3 + 1)
52#define BLUE(n)		 ((n) * 3 + 2)
53
54#define SET_ORIGIN(adp, o) do {				\
55	int oo = o;					\
56	if (oo != last_origin)				\
57	    vidd_set_win_org(adp, last_origin = oo);	\
58	} while (0)
59
60static u_char		*vid;
61static int		 banksize, scrmode, bpsl, scrw, scrh;
62static u_char		 rain_pal[768];
63static int		 blanked;
64
65static void
66rain_update(video_adapter_t *adp)
67{
68	int i, t;
69
70	t = rain_pal[BLUE(MAX)];
71	for (i = MAX; i > 1; i--)
72		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)];
73	rain_pal[BLUE(1)] = t;
74	vidd_load_palette(adp, rain_pal);
75}
76
77static int
78rain_saver(video_adapter_t *adp, int blank)
79{
80	int i, j, o, p, pl;
81	u_char temp;
82	int last_origin = -1;
83
84	if (blank) {
85		/* switch to graphics mode */
86		if (blanked <= 0) {
87			pl = splhigh();
88			vidd_set_mode(adp, scrmode);
89			vidd_load_palette(adp, rain_pal);
90			vidd_set_border(adp, 0);
91			blanked++;
92			vid = (u_char *)adp->va_window;
93			banksize = adp->va_window_size;
94			bpsl = adp->va_line_width;
95			splx(pl);
96			for (i = 0; i < bpsl*scrh; i += banksize) {
97				SET_ORIGIN(adp, i);
98				if ((bpsl * scrh - i) < banksize)
99					bzero(vid, bpsl * scrh - i);
100				else
101					bzero(vid, banksize);
102			}
103			SET_ORIGIN(adp, 0);
104			for (i = 0, o = 0, p = 0; i < scrw; i += 2, p += 2) {
105				if (p > banksize) {
106					p -= banksize;
107					o += banksize;
108					SET_ORIGIN(adp, o);
109				}
110				vid[p] = 1 + (random() % MAX);
111			}
112			o = 0; p = 0;
113			for (j = 1; j < scrh; j++)
114			  for (i = 0, p = bpsl * (j - 1) - o; i < scrw; i += 2, p+= 2) {
115			  	while (p > banksize) {
116					p -= banksize;
117					o += banksize;
118				}
119				SET_ORIGIN(adp, o);
120				temp = (vid[p] < MAX) ? 1 + vid[p] : 1;
121				if (p + bpsl < banksize) {
122					vid[p + bpsl] = temp;
123				} else {
124					SET_ORIGIN(adp, o + banksize);
125					vid[p + bpsl - banksize] = temp;
126				}
127			  }
128		}
129
130		/* update display */
131		rain_update(adp);
132	} else {
133		blanked = 0;
134	}
135	return (0);
136}
137
138static int
139rain_init(video_adapter_t *adp)
140{
141	video_info_t info;
142	int i;
143
144	if (!vidd_get_info(adp, M_VGA_CG320, &info)) {
145		scrmode = M_VGA_CG320;
146	} else if (!vidd_get_info(adp, M_PC98_PEGC640x480, &info)) {
147		scrmode = M_PC98_PEGC640x480;
148	} else if (!vidd_get_info(adp, M_PC98_PEGC640x400, &info)) {
149		scrmode = M_PC98_PEGC640x400;
150	} else {
151		log(LOG_NOTICE,
152		    "%s: the console does not support M_VGA_CG320\n",
153		    SAVER_NAME);
154		return (ENODEV);
155	}
156
157	scrw = info.vi_width;
158	scrh = info.vi_height;
159
160	/* intialize the palette */
161	for (i = 1; i < MAX; i++)
162		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)] + INCREMENT;
163
164	return (0);
165}
166
167static int
168rain_term(video_adapter_t *adp)
169{
170	return (0);
171}
172
173static scrn_saver_t rain_module = {
174	SAVER_NAME,
175	rain_init,
176	rain_term,
177	rain_saver,
178	NULL
179};
180
181SAVER_MODULE(rain_saver, rain_module);
182