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

Last change on this file since 4052 was 4052, checked in by Jean-Yves Lamoureux, 11 years ago
  • Server's mainloop now repeatedly calls server_iteration()
  • Property svn:keywords set to Id
File size: 4.7 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 4052 2009-11-27 12:46:25Z 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    screensaver_flying_toasters_init(screen_list);
36}
37
38void screensaver_kill(struct screen_list *screen_list)
39{
40    screensaver_flying_toasters_kill(screen_list);
41}
42
43void draw_screensaver(struct screen_list *screen_list)
44{
45    screensaver_flying_toasters(screen_list);
46}
47
48
49
50
51/* Flying Toasters */
52
53#define COUNT 15
54#define PRECISION 100
55
56char toaster_text[3][99] = { {
57 "      __._ \n"
58 "   .-'== _',\n"
59 "  <|_= .-'  |\n"
60 "   | --|   \\'.-_ \n"
61 "   |   | \\  \" _.\n"
62 "    `-_|.-\\_.-\n"},
63{
64 "      __._ \n"
65 "   .-'== _',\n"
66 "  \\|_= .-'  |\n"
67 "   | --|  __'-.\n"
68 "   |   | ___.-\n"
69 "    `-_|.-\n"},
70{
71 "   _- __._\n"
72 "  /.-'== _',_.-.\n"
73 "  \\|_= .-'/  _.'\n"
74 "   | --| / .-\n"
75 "   |   |  _.|\n"
76 "    `-_|.-\n"}
77};
78
79char toaster_mask[3][99] = { {
80                             
81 "      __._ \n"
82 "   .-'== _',\n"
83 "  <|_=X.-'XX|\n"
84 "   |X--|XXX\\'.-_ \n"
85 "   |XXX|X\\XX\"X_.\n"
86 "    `-_|.-\\_.-\n"},
87    {
88
89 "      __._ \n"
90 "   .-'== _',\n"
91 "  \\|_= .-'XX|\n"
92 "   |X--|XX__'-.\n"
93 "   |XXX|X___.-\n"
94 "    `-_|.-\n"},
95{
96 "   _- __._\n"
97 "  /.-'== _',_.-.\n"
98 "  \\|_= .-'/XX_.'\n"
99 "   |X--|X/X.-\n"
100 "   |XXX|XX_.|\n"
101 "    `-_|.-\n"}
102};
103
104struct flying_toaster
105{
106    int x[COUNT], y[COUNT], s[COUNT];
107    caca_canvas_t **toaster;
108    caca_canvas_t **mask;
109};
110
111void screensaver_flying_toasters_init(struct screen_list *screen_list)
112{
113    struct flying_toaster *flying_toaster;
114    int w = caca_get_canvas_width(screen_list->cv);
115    int h = caca_get_canvas_height(screen_list->cv);
116    int i;
117
118    flying_toaster = malloc(sizeof(struct flying_toaster));
119    flying_toaster->toaster =
120        (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3);
121    flying_toaster->mask =
122        (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3);
123
124    for (i = 0; i < 3; i++)
125    {
126        flying_toaster->toaster[i] = caca_create_canvas(0, 0);
127        flying_toaster->mask[i] = caca_create_canvas(0, 0);
128        caca_import_canvas_from_memory(flying_toaster->toaster[i],
129                                       toaster_text[i],
130                                       strlen(toaster_text[i]), "utf8");
131        caca_import_canvas_from_memory(flying_toaster->mask[i],
132                                       toaster_mask[i],
133                                       strlen(toaster_mask[i]), "utf8");
134    }
135
136    for (i = 0; i < COUNT; i++)
137    {
138        flying_toaster->x[i] = (rand() % w) * PRECISION;
139        flying_toaster->y[i] = (rand() % h) * PRECISION;
140        flying_toaster->s[i] = (rand() % 3) * PRECISION;
141    }
142
143
144    screen_list->screensaver_data = flying_toaster;
145}
146
147void screensaver_flying_toasters_kill(struct screen_list *screen_list)
148{
149    struct flying_toaster *flying_toaster = screen_list->screensaver_data;
150
151    caca_free_canvas(flying_toaster->toaster[0]);
152    caca_free_canvas(flying_toaster->toaster[1]);
153    caca_free_canvas(flying_toaster->toaster[2]);
154    free(flying_toaster->toaster);
155    free(flying_toaster);
156    screen_list->screensaver_data = NULL;
157}
158
159void screensaver_flying_toasters(struct screen_list *screen_list)
160{
161    struct flying_toaster *d = screen_list->screensaver_data;
162    int i, w, h, x, y, s;
163    if (!d)
164        return;
165
166    w = caca_get_canvas_width(screen_list->cv);
167    h = caca_get_canvas_height(screen_list->cv);
168
169    caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLACK);
170    caca_clear_canvas(screen_list->cv);
171
172    for (i = 0; i < COUNT; i++)
173    {
174        x = d->x[i] / PRECISION;
175        y = d->y[i] / PRECISION;
176        s = d->s[i] / PRECISION;
177
178        caca_blit(screen_list->cv, x, y, d->toaster[s], d->mask[s]);
179
180        d->x[i] -= 40;
181        d->y[i] += 10;
182
183        if (d->x[i] / PRECISION + caca_get_canvas_width(d->toaster[s]) <= 0)
184            d->x[i] = ((rand() % w) / 3 + w) * PRECISION;
185        if (d->y[i] / PRECISION >= h)
186            d->y[i] = ((rand() % h) / 2 - h) * PRECISION;
187
188        d->s[i] = ((d->s[i] + 24) % (3 * PRECISION));
189    }
190}
Note: See TracBrowser for help on using the repository browser.