xref: /freebsd/sys/dev/syscons/rain/rain_saver.c (revision 71fe318b852b8dfb3e799cb12ef184750f7f8eac)
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$
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 #define MAX		 63	/* number of colors (in addition to black) */
45 #define INCREMENT	 4	/* increment between colors */
46 
47 #define RED(n)		 ((n) * 3 + 0)
48 #define GREEN(n)	 ((n) * 3 + 1)
49 #define BLUE(n)		 ((n) * 3 + 2)
50 
51 static u_char		*vid;
52 static int		 banksize, scrmode, bpsl, scrw, scrh;
53 static u_char		 rain_pal[768];
54 static int		 blanked;
55 
56 static void
57 rain_update(video_adapter_t *adp)
58 {
59 	int i, t;
60 
61 	t = rain_pal[BLUE(MAX)];
62 	for (i = MAX; i > 1; i--)
63 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)];
64 	rain_pal[BLUE(1)] = t;
65 	load_palette(adp, rain_pal);
66 }
67 
68 static int
69 rain_saver(video_adapter_t *adp, int blank)
70 {
71 	int i, j, o, p, pl;
72 	u_char temp;
73 
74 	if (blank) {
75 		/* switch to graphics mode */
76 		if (blanked <= 0) {
77 			pl = splhigh();
78 			set_video_mode(adp, scrmode);
79 			load_palette(adp, rain_pal);
80 			set_border(adp, 0);
81 			blanked++;
82 			vid = (u_char *)adp->va_window;
83 			banksize = adp->va_window_size;
84 			bpsl = adp->va_line_width;
85 			splx(pl);
86 			for (i = 0; i < bpsl*scrh; i += banksize) {
87 				set_origin(adp, i);
88 				if ((bpsl * scrh - i) < banksize)
89 					bzero(vid, bpsl * scrh - i);
90 				else
91 					bzero(vid, banksize);
92 			}
93 			set_origin(adp, 0);
94 			for (i = 0, o = 0, p = 0; i < scrw; i += 2, p += 2) {
95 				if (p > banksize) {
96 					p -= banksize;
97 					o += banksize;
98 					set_origin(adp, o);
99 				}
100 				vid[p] = 1 + (random() % MAX);
101 			}
102 			o = 0; p = 0;
103 			for (j = 1; j < scrh; j++)
104 			  for (i = 0, p = bpsl * (j - 1) - o; i < scrw; i += 2, p+= 2) {
105 			  	while (p > banksize) {
106 					p -= banksize;
107 					o += banksize;
108 				}
109 				set_origin(adp, o);
110 				temp = (vid[p] < MAX) ? 1 + vid[p] : 1;
111 				if (p + bpsl < banksize) {
112 					vid[p + bpsl] = temp;
113 				} else {
114 					set_origin(adp, o + banksize);
115 					vid[p + bpsl - banksize] = temp;
116 				}
117 			  }
118 		}
119 
120 		/* update display */
121 		rain_update(adp);
122 	} else {
123 		blanked = 0;
124 	}
125 	return (0);
126 }
127 
128 static int
129 rain_init(video_adapter_t *adp)
130 {
131 	video_info_t info;
132 	int i;
133 
134 	if (!get_mode_info(adp, M_VGA_CG320, &info)) {
135 		scrmode = M_VGA_CG320;
136 	} else if (!get_mode_info(adp, M_PC98_PEGC640x480, &info)) {
137 		scrmode = M_PC98_PEGC640x480;
138 	} else if (!get_mode_info(adp, M_PC98_PEGC640x400, &info)) {
139 		scrmode = M_PC98_PEGC640x400;
140 	} else {
141 		log(LOG_NOTICE,
142 		    "%s: the console does not support M_VGA_CG320\n",
143 		    SAVER_NAME);
144 		return (ENODEV);
145 	}
146 
147 	scrw = info.vi_width;
148 	scrh = info.vi_height;
149 
150 	/* intialize the palette */
151 	for (i = 1; i < MAX; i++)
152 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)] + INCREMENT;
153 
154 	return (0);
155 }
156 
157 static int
158 rain_term(video_adapter_t *adp)
159 {
160 	return (0);
161 }
162 
163 static scrn_saver_t rain_module = {
164 	SAVER_NAME,
165 	rain_init,
166 	rain_term,
167 	rain_saver,
168 	NULL
169 };
170 
171 SAVER_MODULE(rain_saver, rain_module);
172