source: neercs/trunk/src/screensaver.c @ 3969

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