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