1 /*-
2  * (MPSAFE)
3  *
4  * Copyright (c) 1995-1998 S�ren Schmidt
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  *    without modification, immediately at the beginning of the file.
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: src/sys/modules/syscons/snake/snake_saver.c,v 1.26.2.2 2001/05/06 05:44:29 nyan Exp $
31  * $DragonFly: src/sys/dev/misc/syscons/snake/snake_saver.c,v 1.8 2006/12/20 18:14:39 dillon Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/consio.h>
41 #include <sys/fbio.h>
42 #include <sys/thread.h>
43 
44 #include <machine/pc/display.h>
45 
46 #include <dev/video/fb/fbreg.h>
47 #include <dev/video/fb/splashreg.h>
48 #include "../syscons.h"
49 
50 static u_char	*message;
51 static int	*messagep;
52 static int	messagelen;
53 static int	blanked;
54 
55 static int
56 snake_saver(video_adapter_t *adp, int blank)
57 {
58 	static int	dirx, diry;
59 	int		f;
60 	sc_softc_t	*sc;
61 	scr_stat	*scp;
62 
63 /* XXX hack for minimal changes. */
64 #define	save	message
65 #define	savs	messagep
66 
67 
68 	sc = sc_find_softc(adp, NULL);
69 	if (sc == NULL) {
70 		return EAGAIN;
71 	}
72 	scp = sc->cur_scp;
73 
74 	if (blank) {
75 		if (adp->va_info.vi_flags & V_INFO_GRAPHICS) {
76 			return EAGAIN;
77 		}
78 		if (blanked <= 0) {
79 			sc_vtb_clear(&scp->scr, sc->scr_map[0x20],
80 				     (FG_LIGHTGREY | BG_BLACK) << 8);
81 			(*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
82 			sc_set_border(scp, 0);
83 			dirx = (scp->xpos ? 1 : -1);
84 			diry = (scp->ypos ?
85 				scp->xsize : -scp->xsize);
86 			for (f=0; f< messagelen; f++)
87 				savs[f] = scp->xpos + scp->ypos*scp->xsize;
88 			sc_vtb_putc(&scp->scr, savs[0], sc->scr_map[*save],
89 				    (FG_LIGHTGREY | BG_BLACK) << 8);
90 			blanked = 1;
91 		}
92 		if (blanked++ < 4) {
93 			return 0;
94 		}
95 		blanked = 1;
96 		sc_vtb_putc(&scp->scr, savs[messagelen - 1], sc->scr_map[0x20],
97 			    (FG_LIGHTGREY | BG_BLACK) << 8);
98 		for (f=messagelen-1; f > 0; f--)
99 			savs[f] = savs[f-1];
100 		f = savs[0];
101 		if ((f % scp->xsize) == 0 ||
102 		    (f % scp->xsize) == scp->xsize - 1 ||
103 		    (krandom() % 50) == 0)
104 			dirx = -dirx;
105 		if ((f / scp->xsize) == 0 ||
106 		    (f / scp->xsize) == scp->ysize - 1 ||
107 		    (krandom() % 20) == 0)
108 			diry = -diry;
109 		savs[0] += dirx + diry;
110 		for (f=messagelen-1; f>=0; f--)
111 			sc_vtb_putc(&scp->scr, savs[f], sc->scr_map[save[f]],
112 				    (FG_LIGHTGREY | BG_BLACK) << 8);
113 	} else {
114 		blanked = 0;
115 	}
116 
117 	return 0;
118 }
119 
120 static int
121 snake_init(video_adapter_t *adp)
122 {
123 	messagelen = strlen(ostype) + 1 + strlen(osrelease);
124 	message = kmalloc(messagelen + 1, M_SYSCONS, M_WAITOK);
125 	ksprintf(message, "%s %s", ostype, osrelease);
126 	messagep = kmalloc(messagelen * sizeof *messagep, M_SYSCONS, M_WAITOK);
127 	return 0;
128 }
129 
130 static int
131 snake_term(video_adapter_t *adp)
132 {
133 	kfree(message, M_SYSCONS);
134 	kfree(messagep, M_SYSCONS);
135 	return 0;
136 }
137 
138 static scrn_saver_t snake_module = {
139 	"snake_saver", snake_init, snake_term, snake_saver, NULL,
140 };
141 
142 SAVER_MODULE(snake_saver, snake_module);
143