111d7827des/*-
211d7827des * Copyright (c) 2015 Dag-Erling Sm��rgrav
311d7827des * All rights reserved.
411d7827des *
511d7827des * Redistribution and use in source and binary forms, with or without
611d7827des * modification, are permitted provided that the following conditions
711d7827des * are met:
811d7827des * 1. Redistributions of source code must retain the above copyright
911d7827des *    notice, this list of conditions and the following disclaimer.
1011d7827des * 2. Redistributions in binary form must reproduce the above copyright
1111d7827des *    notice, this list of conditions and the following disclaimer in the
1211d7827des *    documentation and/or other materials provided with the distribution.
1311d7827des *
1411d7827des * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1511d7827des * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1611d7827des * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1711d7827des * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1811d7827des * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1911d7827des * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2011d7827des * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2111d7827des * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2211d7827des * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2311d7827des * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2411d7827des * SUCH DAMAGE.
2511d7827des *
2611d7827des * $FreeBSD$
2711d7827des *
2811d7827des * To CJA, in appreciation of Nighthawk brunches past and future.
2911d7827des */
3011d7827des
3111d7827des#include <sys/param.h>
3211d7827des#include <sys/systm.h>
3311d7827des#include <sys/kernel.h>
3411d7827des#include <sys/module.h>
3511d7827des#include <sys/syslog.h>
3611d7827des#include <sys/consio.h>
3711d7827des#include <sys/fbio.h>
3811d7827des
3911d7827des#include <dev/fb/fbreg.h>
4011d7827des#include <dev/fb/splashreg.h>
4111d7827des#include <dev/syscons/syscons.h>
4211d7827des
4311d7827des#define SAVER_NAME	 "plasma_saver"
4411d7827des
4511d7827des#include "fp16.h"
4611d7827des
4711d7827des/*
4811d7827des * Preferred video modes
4911d7827des */
5011d7827desstatic int modes[] = {
5111d7827des	M_VGA_CG640,
5211d7827des	M_VGA_CG320,
5311d7827des	-1
5411d7827des};
5511d7827des
5611d7827des/*
5711d7827des * Display parameters
5811d7827des */
5911d7827desstatic unsigned char *vid;
6011d7827desstatic unsigned int banksize, scrmode, scrw, scrh;
6111d7827desstatic unsigned int blanked;
6211d7827des
6311d7827des/*
6411d7827des * List of foci
6511d7827des */
6611d7827des#define FOCI 3
6711d7827desstatic struct {
6811d7827des	int x, y;		/* coordinates */
6911d7827des	int vx, vy;		/* velocity */
7011d7827des} plasma_foci[FOCI];
7111d7827des
7211d7827des/*
7311d7827des * Palette
7411d7827des */
7511d7827desstatic struct {
7611d7827des	unsigned char r, g, b;
7711d7827des} plasma_pal[256];
7811d7827des
7911d7827des/*
8011d7827des * Draw a new frame
8111d7827des */
8211d7827desstatic void
8311d7827desplasma_update(video_adapter_t *adp)
8411d7827des{
8511d7827des	unsigned int x, y;	/* coordinates */
8611d7827des	signed int dx, dy;	/* horizontal / vertical distance */
8711d7827des	fp16_t sqd, d;		/* square of distance and distance */
8811d7827des	fp16_t m;		/* magnitude */
8911d7827des	unsigned int org, off;	/* origin and offset */
9011d7827des	unsigned int i;		/* loop index */
9111d7827des
9211d7827des	/* switch to bank 0 */
9311d7827des	vidd_set_win_org(adp, 0);
9411d7827des	/* for each scan line */
9511d7827des	for (y = org = off = 0; y < scrh; ++y) {
9611d7827des		/* for each pixel on scan line */
9711d7827des		for (x = 0; x < scrw; ++x, ++off) {
9811d7827des			/* for each focus */
9911d7827des			for (i = m = 0; i < FOCI; ++i) {
10011d7827des				dx = x - plasma_foci[i].x;
10111d7827des				dy = y - plasma_foci[i].y;
10211d7827des				sqd = ItoFP16(dx * dx + dy * dy);
10311d7827des				d = fp16_sqrt(sqd);
10411d7827des				/* divide by 4 to stretch out the pattern */
105bebb40fdes				m = fp16_sub(m, fp16_cos(d / 4));
10611d7827des			}
10711d7827des			/*
10811d7827des			 * m is now in the range +/- FOCI, but we need a
10911d7827des			 * value between 0 and 255.  We scale to +/- 127
11011d7827des			 * and add 127, which moves it into the range [0,
11111d7827des			 * 254].
11211d7827des			 */
11311d7827des			m = fp16_mul(m, ItoFP16(127));
11411d7827des			m = fp16_div(m, ItoFP16(FOCI));
11511d7827des			m = fp16_add(m, ItoFP16(127));
11611d7827des			/* switch banks if necessary */
11711d7827des			if (off > banksize) {
11811d7827des				off -= banksize;
11911d7827des				org += banksize;
12011d7827des				vidd_set_win_org(adp, org);
12111d7827des			}
12211d7827des			/* plot */
12311d7827des			vid[off] = FP16toI(m);
12411d7827des		}
12511d7827des	}
12611d7827des	/* now move the foci */
12711d7827des	for (i = 0; i < FOCI; ++i) {
12811d7827des		plasma_foci[i].x += plasma_foci[i].vx;
12911d7827des		if (plasma_foci[i].x < 0) {
13011d7827des			/* bounce against left wall */
13111d7827des			plasma_foci[i].vx = -plasma_foci[i].vx;
13211d7827des			plasma_foci[i].x = -plasma_foci[i].x;
13311d7827des		} else if (plasma_foci[i].x >= scrw) {
13411d7827des			/* bounce against right wall */
13511d7827des			plasma_foci[i].vx = -plasma_foci[i].vx;
13611d7827des			plasma_foci[i].x = scrw - (plasma_foci[i].x - scrw);
13711d7827des		}
13811d7827des		plasma_foci[i].y += plasma_foci[i].vy;
13911d7827des		if (plasma_foci[i].y < 0) {
14011d7827des			/* bounce against ceiling */
14111d7827des			plasma_foci[i].vy = -plasma_foci[i].vy;
14211d7827des			plasma_foci[i].y = -plasma_foci[i].y;
14311d7827des		} else if (plasma_foci[i].y >= scrh) {
14411d7827des			/* bounce against floor */
14511d7827des			plasma_foci[i].vy = -plasma_foci[i].vy;
14611d7827des			plasma_foci[i].y = scrh - (plasma_foci[i].y - scrh);
14711d7827des		}
14811d7827des	}
14911d7827des}
15011d7827des
15111d7827des/*
15211d7827des * Start or stop the screensaver
15311d7827des */
15411d7827desstatic int
15511d7827desplasma_saver(video_adapter_t *adp, int blank)
15611d7827des{
15711d7827des	int pl;
15811d7827des
15911d7827des	if (blank) {
16011d7827des		/* switch to graphics mode */
16111d7827des		if (blanked <= 0) {
16211d7827des			pl = splhigh();
16311d7827des			vidd_set_mode(adp, scrmode);
16411d7827des			vidd_load_palette(adp, (unsigned char *)plasma_pal);
16511d7827des			vidd_set_border(adp, 0);
16611d7827des			blanked++;
16711d7827des			vid = (unsigned char *)adp->va_window;
16811d7827des			banksize = adp->va_window_size;
16911d7827des			splx(pl);
17011d7827des			vidd_clear(adp);
17111d7827des		}
17211d7827des		/* update display */
17311d7827des		plasma_update(adp);
17411d7827des	} else {
17511d7827des		blanked = 0;
17611d7827des	}
17711d7827des	return (0);
17811d7827des}
17911d7827des
18011d7827des/*
18111d7827des * Initialize on module load
18211d7827des */
18311d7827desstatic int
18411d7827desplasma_init(video_adapter_t *adp)
18511d7827des{
18611d7827des	video_info_t info;
18711d7827des	int i;
18811d7827des
18911d7827des	/* select video mode */
19011d7827des	for (i = 0; modes[i] >= 0; ++i)
19111d7827des		if (vidd_get_info(adp, modes[i], &info) == 0)
19211d7827des			break;
19311d7827des	if (modes[i] < 0) {
19411d7827des		log(LOG_NOTICE, "%s: no supported video modes\n", SAVER_NAME);
19511d7827des		return (ENODEV);
19611d7827des	}
19711d7827des	scrmode = modes[i];
19811d7827des	scrw = info.vi_width;
19911d7827des	scrh = info.vi_height;
20011d7827des
20111d7827des	/* initialize the palette */
20211d7827des	for (i = 0; i < 256; ++i)
20311d7827des		plasma_pal[i].r = plasma_pal[i].g = plasma_pal[i].b = i;
20411d7827des
20511d7827des	/* randomize the foci */
20611d7827des	for (i = 0; i < FOCI; i++) {
20711d7827des		plasma_foci[i].x = random() % scrw;
20811d7827des		plasma_foci[i].y = random() % scrh;
20911d7827des		plasma_foci[i].vx = random() % 5 - 2;
21011d7827des		plasma_foci[i].vy = random() % 5 - 2;
21111d7827des	}
21211d7827des
21311d7827des	return (0);
21411d7827des}
21511d7827des
21611d7827des/*
21711d7827des * Clean up before module unload
21811d7827des */
21911d7827desstatic int
22011d7827desplasma_term(video_adapter_t *adp)
22111d7827des{
22211d7827des
22311d7827des	return (0);
22411d7827des}
22511d7827des
22611d7827des/*
22711d7827des * Boilerplate
22811d7827des */
22911d7827desstatic scrn_saver_t plasma_module = {
23011d7827des	SAVER_NAME,
23111d7827des	plasma_init,
23211d7827des	plasma_term,
23311d7827des	plasma_saver,
23411d7827des	NULL
23511d7827des};
23611d7827des
23711d7827desSAVER_MODULE(plasma_saver, plasma_module);
238