blob: c4834e9d5379018cb766d7089e5f472347a488ff [file] [log] [blame]
adamdunkels228efad2003-08-20 19:49:44 +00001/*
adamdunkels0a08fda2004-07-04 18:33:07 +00002 * Copyright (c) 2002-2004, Adam Dunkels.
adamdunkels228efad2003-08-20 19:49:44 +00003 * 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 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
adamdunkels0a08fda2004-07-04 18:33:07 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkels228efad2003-08-20 19:49:44 +000015 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the Contiki desktop environment
31 *
oliverschmidtb04f65f2005-05-12 21:07:22 +000032 * $Id: plasma.c,v 1.4 2005/05/12 21:07:22 oliverschmidt Exp $
adamdunkels228efad2003-08-20 19:49:44 +000033 *
34 */
35
36#include <stdlib.h>
37
38#include "ctk.h"
39#include "ctk-draw.h"
40#include "ctk-mouse.h"
adamdunkels0a08fda2004-07-04 18:33:07 +000041#include "ek.h"
adamdunkels228efad2003-08-20 19:49:44 +000042#include "loader.h"
43
44static unsigned char sinetab1[256] = {
45 128, 131, 134, 137, 140, 143, 146, 149,
46 152, 156, 159, 162, 165, 168, 171, 174,
47 176, 179, 182, 185, 188, 191, 193, 196,
48 199, 201, 204, 206, 209, 211, 213, 216,
49 218, 220, 222, 224, 226, 228, 230, 232,
50 234, 236, 237, 239, 240, 242, 243, 245,
51 246, 247, 248, 249, 250, 251, 252, 252,
52 253, 254, 254, 255, 255, 255, 255, 255,
53 255, 255, 255, 255, 255, 255, 254, 254,
54 253, 252, 252, 251, 250, 249, 248, 247,
55 246, 245, 243, 242, 240, 239, 237, 236,
56 234, 232, 230, 228, 226, 224, 222, 220,
57 218, 216, 213, 211, 209, 206, 204, 201,
58 199, 196, 193, 191, 188, 185, 182, 179,
59 176, 174, 171, 168, 165, 162, 159, 156,
60 152, 149, 146, 143, 140, 137, 134, 131,
61 128, 124, 121, 118, 115, 112, 109, 106,
oliverschmidtb04f65f2005-05-12 21:07:22 +000062 103, 99, 96, 93, 90, 87, 84, 81,
63 79, 76, 73, 70, 67, 64, 62, 59,
64 56, 54, 51, 49, 46, 44, 42, 39,
65 37, 35, 33, 31, 29, 27, 25, 23,
66 21, 19, 18, 16, 15, 13, 12, 10,
67 9, 8, 7, 6, 5, 4, 3, 3,
68 2, 1, 1, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 1, 1,
70 2, 3, 3, 4, 5, 6, 7, 8,
71 9, 10, 12, 13, 15, 16, 18, 19,
72 21, 23, 25, 27, 29, 31, 33, 35,
73 37, 39, 42, 44, 46, 49, 51, 54,
74 56, 59, 62, 64, 67, 70, 73, 76,
75 78, 81, 84, 87, 90, 93, 96, 99,
76 103, 106, 109, 112, 115, 118, 121, 124,
adamdunkels228efad2003-08-20 19:49:44 +000077};
78
79static unsigned char sinetab2[256];
80static unsigned char sinetab3[256];
81
82static unsigned char colortab[256];
83
84static unsigned char colors[16] =
85 {
86 0x00, 0x06, 0x0b, 0x04,
87 0x0a, 0x0f, 0x07, 0x01,
88 0x07, 0x0f, 0x0a, 0x08,
89 0x02, 0x09, 0x00, 0x00,
90 };
91
oliverschmidtb04f65f2005-05-12 21:07:22 +000092#define XSIZE 25
adamdunkels228efad2003-08-20 19:49:44 +000093#define YSIZE 25
94
oliverschmidtb04f65f2005-05-12 21:07:22 +000095#define XADD01 0xfe
96#define YADD01 0x05
97
98#define XADD1 0x04
99#define YADD1 0x02
100#define XADD2 0xfc
101#define YADD2 0xf9
102
103#define XADD 0x03
104#define YADD 0xfe
105
106#define MOVADD 0xfb
107
adamdunkels228efad2003-08-20 19:49:44 +0000108static unsigned char xplasma[XSIZE], yplasma[YSIZE];
109static unsigned char xcnt, ycnt;
110static unsigned char xcnt01, xcnt02, xcnt1, xcnt2;
111static unsigned char ycnt01, ycnt02, ycnt1, ycnt2;
112
adamdunkels228efad2003-08-20 19:49:44 +0000113static unsigned char xadd02 = 0x01;
114static unsigned char yadd02 = 0xfb;
115
adamdunkels228efad2003-08-20 19:49:44 +0000116static unsigned char movcnt;
adamdunkels228efad2003-08-20 19:49:44 +0000117
adamdunkels0a08fda2004-07-04 18:33:07 +0000118/*static DISPATCHER_SIGHANDLER(sighandler, s, data);
adamdunkels228efad2003-08-20 19:49:44 +0000119static void idle(void);
120static struct dispatcher_proc p =
121 {DISPATCHER_PROC("Plasma screensaver", idle,
122 sighandler,
123 NULL)};
adamdunkels0a08fda2004-07-04 18:33:07 +0000124 static ek_id_t id;*/
125EK_EVENTHANDLER(eventhandler, ev, data);
126EK_POLLHANDLER(pollhandler);
127EK_PROCESS(p, "Plasma screensaver", EK_PRIO_LOWEST,
128 eventhandler, pollhandler, NULL);
129static ek_id_t id = EK_ID_NONE;
adamdunkels228efad2003-08-20 19:49:44 +0000130
131/*-----------------------------------------------------------------------------------*/
132static void
133quit(void)
134{
adamdunkels0a08fda2004-07-04 18:33:07 +0000135 ek_exit();
adamdunkels228efad2003-08-20 19:49:44 +0000136 id = EK_ID_NONE;
137 LOADER_UNLOAD();
138}
139/*-----------------------------------------------------------------------------------*/
140static void
141scrninit(void)
142{
143 unsigned char *ptr, *cptr;
144 static int i;
145
146 /* Make sine tables */
147 for(i = 0; i < 256; ++i) {
148 sinetab2[(unsigned char)i] = sinetab1[(unsigned char)i] / 2;
149 sinetab3[(unsigned char)i] = sinetab1[(unsigned char)i] / 4;
150 }
151
152 /* Make color table */
153 for(i = 0; i < 256; ++i) {
154 colortab[(unsigned char)i] = colors[(unsigned char)i / 16];
155 }
156
157
158 /* Fill screen with inverted spaces. */
159 cptr = COLOR_RAM;
160 for(ptr = (unsigned char *)0x0400;
161 ptr != (unsigned char *)0x07e8;
162 ++ptr) {
163 *ptr = 0xa0;
164 *cptr++ = 0x00;
165 }
166
167 VIC.ctrl1 = 0x1b; /* $D011 */
168 VIC.addr = 0x17; /* $D018 */
169 VIC.ctrl2 = 0xc8; /* $D016 */
170 VIC.bordercolor = 0x00; /* $D020 */
171 VIC.bgcolor0 = 0x00; /* $D021 */
172 CIA2.pra = 0x03; /* $DD00 */
173
174}
175/*-----------------------------------------------------------------------------------*/
adamdunkels0a08fda2004-07-04 18:33:07 +0000176EK_POLLHANDLER(pollhandler)
adamdunkels228efad2003-08-20 19:49:44 +0000177{
178 static unsigned char i, x, y;
179 register unsigned char *cptr;
180
181 if(ctk_mode_get() == CTK_MODE_SCREENSAVER) {
182
183
184 xcnt1 = xcnt01;
185 xcnt2 = xcnt02;
186
187 for(i = 0; i < XSIZE; ++i) {
188 xplasma[i] = sinetab1[xcnt1] + sinetab2[xcnt2];
oliverschmidtb04f65f2005-05-12 21:07:22 +0000189 xcnt1 += XADD1;
190 xcnt2 += XADD2;
adamdunkels228efad2003-08-20 19:49:44 +0000191 }
192
193 ycnt1 = ycnt01;
194 ycnt2 = ycnt02;
195
196 for(i = 0; i < YSIZE; ++i) {
197 yplasma[i] = sinetab1[ycnt1] + sinetab3[ycnt2];
oliverschmidtb04f65f2005-05-12 21:07:22 +0000198 ycnt1 += YADD1;
199 ycnt2 += YADD2;
adamdunkels228efad2003-08-20 19:49:44 +0000200 }
201
oliverschmidtb04f65f2005-05-12 21:07:22 +0000202 xcnt01 += XADD01;
adamdunkels228efad2003-08-20 19:49:44 +0000203 xcnt02 += xadd02;
oliverschmidtb04f65f2005-05-12 21:07:22 +0000204 ycnt01 += YADD01;
adamdunkels228efad2003-08-20 19:49:44 +0000205 ycnt02 += yadd02;
206
207
208 yadd02 = sinetab3[ycnt] / 4;
209 xadd02 = sinetab3[xcnt] / 4;
210
oliverschmidtb04f65f2005-05-12 21:07:22 +0000211 ycnt += YADD;
212 xcnt += XADD;
adamdunkels228efad2003-08-20 19:49:44 +0000213
oliverschmidtb04f65f2005-05-12 21:07:22 +0000214 movcnt += MOVADD;
215 cptr = (unsigned char *)(COLOR_RAM + sinetab1[movcnt]/16);
adamdunkels228efad2003-08-20 19:49:44 +0000216 for(y = 0; y < YSIZE; ++y) {
217 for(x = 0; x < XSIZE; ++x) {
218 *cptr = colortab[(xplasma[x] + yplasma[y]) & 0xff];
219 ++cptr;
220 }
221 cptr += 40 - XSIZE;
222 }
223 }
224}
225/*-----------------------------------------------------------------------------------*/
adamdunkels0a08fda2004-07-04 18:33:07 +0000226/*static
227 DISPATCHER_SIGHANDLER(sighandler, s, data)*/
228EK_EVENTHANDLER(eventhandler, ev, data)
adamdunkels228efad2003-08-20 19:49:44 +0000229{
adamdunkels0a08fda2004-07-04 18:33:07 +0000230 EK_EVENTHANDLER_ARGS(ev, data);
231
232 if(ev == EK_EVENT_INIT) {
233 ctk_mode_set(CTK_MODE_SCREENSAVER);
234 ctk_mouse_hide();
235
236 scrninit();
237
238 } else if(ev == ctk_signal_screensaver_stop ||
239 ev == EK_EVENT_REQUEST_EXIT) {
adamdunkels228efad2003-08-20 19:49:44 +0000240 ctk_draw_init();
241 ctk_desktop_redraw(NULL);
adamdunkels0a08fda2004-07-04 18:33:07 +0000242 /* ctk_mode_set(CTK_MODE_NORMAL);*/
adamdunkels228efad2003-08-20 19:49:44 +0000243 quit();
244 }
245}
246/*-----------------------------------------------------------------------------------*/
adamdunkels7cde6092003-08-24 22:35:22 +0000247LOADER_INIT_FUNC(plasma_init, arg)
adamdunkels228efad2003-08-20 19:49:44 +0000248{
adamdunkels7cde6092003-08-24 22:35:22 +0000249 arg_free(arg);
250
adamdunkels228efad2003-08-20 19:49:44 +0000251 if(id == EK_ID_NONE) {
adamdunkels0a08fda2004-07-04 18:33:07 +0000252 id = ek_start(&p);
adamdunkels228efad2003-08-20 19:49:44 +0000253 }
254}
255/*-----------------------------------------------------------------------------------*/
256