source: neercs/trunk/src/screensaver.c

Last change on this file was 4366, checked in by Sam Hocevar, 11 years ago

Clean up source code, copyright information, author names, SVN keywords...

  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  This program is free software. It comes without any warranty, to
8 *  the extent permitted by applicable law. You can redistribute it
9 *  and/or modify it under the terms of the Do What The Fuck You Want
10 *  To Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <sys/types.h>
20#include <signal.h>
21#include <sys/wait.h>
22#include <errno.h>
23#include <unistd.h>
24
25#include <caca.h>
26
27#include "neercs.h"
28
29
30void screensaver_init(struct screen_list *screen_list)
31{
32    screensaver_flying_toasters_init(screen_list);
33}
34
35void screensaver_kill(struct screen_list *screen_list)
36{
37    screensaver_flying_toasters_kill(screen_list);
38}
39
40void draw_screensaver(struct screen_list *screen_list)
41{
42    screensaver_flying_toasters(screen_list);
43}
44
45
46
47
48/* Flying Toasters */
49
50#define COUNT 15
51#define PRECISION 100
52
53char toaster_text[3][99] = { {
54 "      __._ \n"
55 "   .-'== _',\n"
56 "  <|_= .-'  |\n"
57 "   | --|   \\'.-_ \n"
58 "   |   | \\  \" _.\n"
59 "    `-_|.-\\_.-\n"},
60{
61 "      __._ \n"
62 "   .-'== _',\n"
63 "  \\|_= .-'  |\n"
64 "   | --|  __'-.\n"
65 "   |   | ___.-\n"
66 "    `-_|.-\n"},
67{
68 "   _- __._\n"
69 "  /.-'== _',_.-.\n"
70 "  \\|_= .-'/  _.'\n"
71 "   | --| / .-\n"
72 "   |   |  _.|\n"
73 "    `-_|.-\n"}
74};
75
76char toaster_mask[3][99] = { {
77
78 "      __._ \n"
79 "   .-'== _',\n"
80 "  <|_=X.-'XX|\n"
81 "   |X--|XXX\\'.-_ \n"
82 "   |XXX|X\\XX\"X_.\n"
83 "    `-_|.-\\_.-\n"},
84    {
85
86 "      __._ \n"
87 "   .-'== _',\n"
88 "  \\|_= .-'XX|\n"
89 "   |X--|XX__'-.\n"
90 "   |XXX|X___.-\n"
91 "    `-_|.-\n"},
92{
93 "   _- __._\n"
94 "  /.-'== _',_.-.\n"
95 "  \\|_= .-'/XX_.'\n"
96 "   |X--|X/X.-\n"
97 "   |XXX|XX_.|\n"
98 "    `-_|.-\n"}
99};
100
101struct flying_toaster
102{
103    int x[COUNT], y[COUNT], s[COUNT];
104    caca_canvas_t **toaster;
105    caca_canvas_t **mask;
106};
107
108void screensaver_flying_toasters_init(struct screen_list *screen_list)
109{
110    struct flying_toaster *flying_toaster;
111    int w = caca_get_canvas_width(screen_list->cv);
112    int h = caca_get_canvas_height(screen_list->cv);
113    int i;
114
115    flying_toaster = malloc(sizeof(struct flying_toaster));
116    flying_toaster->toaster =
117        (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3);
118    flying_toaster->mask =
119        (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3);
120
121    for (i = 0; i < 3; i++)
122    {
123        flying_toaster->toaster[i] = caca_create_canvas(0, 0);
124        flying_toaster->mask[i] = caca_create_canvas(0, 0);
125        caca_import_canvas_from_memory(flying_toaster->toaster[i],
126                                       toaster_text[i],
127                                       strlen(toaster_text[i]), "utf8");
128        caca_import_canvas_from_memory(flying_toaster->mask[i],
129                                       toaster_mask[i],
130                                       strlen(toaster_mask[i]), "utf8");
131    }
132
133    for (i = 0; i < COUNT; i++)
134    {
135        flying_toaster->x[i] = (rand() % w) * PRECISION;
136        flying_toaster->y[i] = (rand() % h) * PRECISION;
137        flying_toaster->s[i] = (rand() % 3) * PRECISION;
138    }
139
140
141    screen_list->screensaver.data = flying_toaster;
142}
143
144void screensaver_flying_toasters_kill(struct screen_list *screen_list)
145{
146    struct flying_toaster *flying_toaster = screen_list->screensaver.data;
147
148    caca_free_canvas(flying_toaster->toaster[0]);
149    caca_free_canvas(flying_toaster->toaster[1]);
150    caca_free_canvas(flying_toaster->toaster[2]);
151    free(flying_toaster->toaster);
152    free(flying_toaster);
153    screen_list->screensaver.data = NULL;
154}
155
156void screensaver_flying_toasters(struct screen_list *screen_list)
157{
158    struct flying_toaster *d = screen_list->screensaver.data;
159    int i, w, h, x, y, s;
160    if (!d)
161        return;
162
163    w = caca_get_canvas_width(screen_list->cv);
164    h = caca_get_canvas_height(screen_list->cv);
165
166    caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLACK);
167    caca_clear_canvas(screen_list->cv);
168
169    for (i = 0; i < COUNT; i++)
170    {
171        x = d->x[i] / PRECISION;
172        y = d->y[i] / PRECISION;
173        s = d->s[i] / PRECISION;
174
175        caca_blit(screen_list->cv, x, y, d->toaster[s], d->mask[s]);
176
177        d->x[i] -= 40;
178        d->y[i] += 10;
179
180        if (d->x[i] / PRECISION + caca_get_canvas_width(d->toaster[s]) <= 0)
181            d->x[i] = ((rand() % w) / 3 + w) * PRECISION;
182        if (d->y[i] / PRECISION >= h)
183            d->y[i] = ((rand() % h) / 2 - h) * PRECISION;
184
185        d->s[i] = ((d->s[i] + 24) % (3 * PRECISION));
186    }
187}
Note: See TracBrowser for help on using the repository browser.