summaryrefslogtreecommitdiffstats
path: root/src/libplybootsplash/ply-throbber.c
blob: 0dd6dc56a838bb23d4dd9c38b6fcea21c876c674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* throbber.c - boot throbber
 *
 * Copyright (C) 2007, 2008 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written by: Ray Strode <rstrode@redhat.com>
 */
#include "config.h"

#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <values.h>
#include <unistd.h>
#include <wchar.h>

#include "ply-throbber.h"
#include "ply-event-loop.h"
#include "ply-array.h"
#include "ply-logger.h"
#include "ply-frame-buffer.h"
#include "ply-image.h"
#include "ply-utils.h"
#include "ply-window.h"

#include <linux/kd.h>

#ifndef FRAMES_PER_SECOND
#define FRAMES_PER_SECOND 30
#endif

struct _ply_throbber
{
  ply_array_t *frames;
  ply_event_loop_t *loop;
  char *image_dir;
  char *frames_prefix;

  ply_window_t            *window;
  ply_frame_buffer_t      *frame_buffer;
  ply_frame_buffer_area_t  frame_area;

  long x, y;
  long width, height;
  double start_time, now;
};

ply_throbber_t *
ply_throbber_new (const char *image_dir,
              const char *frames_prefix)
{
  ply_throbber_t *throbber;

  assert (image_dir != NULL);
  assert (frames_prefix != NULL);

  throbber = calloc (1, sizeof (ply_throbber_t));

  throbber->frames = ply_array_new ();
  throbber->frames_prefix = strdup (frames_prefix);
  throbber->image_dir = strdup (image_dir);
  throbber->width = 0;
  throbber->height = 0;
  throbber->frame_area.width = 0;
  throbber->frame_area.height = 0;
  throbber->frame_area.x = 0;
  throbber->frame_area.y = 0;

  return throbber;
}

static void
ply_throbber_remove_frames (ply_throbber_t *throbber)
{
  int i;
  ply_image_t **frames;

  frames = (ply_image_t **) ply_array_steal_elements (throbber->frames);
  for (i = 0; frames[i] != NULL; i++)
    ply_image_free (frames[i]);
  free (frames);
}

void
ply_throbber_free (ply_throbber_t *throbber)
{
  if (throbber == NULL)
    return;

  ply_throbber_remove_frames (throbber);
  ply_array_free (throbber->frames);

  free (throbber->frames_prefix);
  free (throbber->image_dir);
  free (throbber);
}

static void
draw_background (ply_throbber_t *throbber)
{
  ply_frame_buffer_fill_with_gradient (throbber->frame_buffer, &throbber->frame_area,
                                       PLYMOUTH_BACKGROUND_START_COLOR,
                                       PLYMOUTH_BACKGROUND_END_COLOR);
}

static void
animate_at_time (ply_throbber_t *throbber,
                 double      time)
{
  int number_of_frames;
  int frame_number;
  ply_image_t * const * frames;
  uint32_t *frame_data;

  ply_window_set_mode (throbber->window, PLY_WINDOW_MODE_GRAPHICS);

  number_of_frames = ply_array_get_size (throbber->frames);

  if (number_of_frames == 0)
    return;

  frame_number = (.5 * sin (time) + .5) * number_of_frames;

  ply_frame_buffer_pause_updates (throbber->frame_buffer);
  if (throbber->frame_area.width > 0)
    draw_background (throbber);

  frames = (ply_image_t * const *) ply_array_get_elements (throbber->frames);

  throbber->frame_area.x = throbber->x;
  throbber->frame_area.y = throbber->y;
  throbber->frame_area.width = ply_image_get_width (frames[frame_number]);
  throbber->frame_area.height = ply_image_get_height (frames[frame_number]);
  frame_data = ply_image_get_data (frames[frame_number]);

  ply_frame_buffer_fill_with_argb32_data (throbber->frame_buffer,
                                          &throbber->frame_area, 0, 0,
                                          frame_data);
  ply_frame_buffer_unpause_updates (throbber->frame_buffer);
}

static void
on_timeout (ply_throbber_t *throbber)
{
  double sleep_time;
  throbber->now = ply_get_timestamp ();

#ifdef REAL_TIME_ANIMATION
  animate_at_time (throbber,
                   throbber->now - throbber->start_time);
#else
  static double time = 0.0;
  time += 1.0 / FRAMES_PER_SECOND;
  animate_at_time (throbber, time);
#endif

  sleep_time = 1.0 / FRAMES_PER_SECOND;
  sleep_time = MAX (sleep_time - (ply_get_timestamp () - throbber->now),
                    0.005);

  ply_event_loop_watch_for_timeout (throbber->loop,
                                    sleep_time,
                                    (ply_event_loop_timeout_handler_t)
                                    on_timeout, throbber);
}

static bool
ply_throbber_add_frame (ply_throbber_t *throbber,
                    const char *filename)
{
  ply_image_t *image;

  image = ply_image_new (filename);

  if (!ply_image_load (image))
    {
      ply_image_free (image);
      return false;
    }

  ply_array_add_element (throbber->frames, image);

  throbber->width = MAX (throbber->width, ply_image_get_width (image));
  throbber->height = MAX (throbber->width, ply_image_get_height (image));

  return true;
}

static bool
ply_throbber_add_frames (ply_throbber_t *throbber)
{
  struct dirent **entries;
  int number_of_entries;
  int i;
  bool load_finished;

  entries = NULL;

  number_of_entries = scandir (throbber->image_dir, &entries, NULL, versionsort);

  if (number_of_entries < 0)
    return false;

  load_finished = false;
  for (i = 0; i < number_of_entries; i++)
    {
      if (strncmp (entries[i]->d_name,
                   throbber->frames_prefix,
                   strlen (throbber->frames_prefix)) == 0
          && (strlen (entries[i]->d_name) > 4)
          && strcmp (entries[i]->d_name + strlen (entries[i]->d_name) - 4, ".png") == 0)
        {
          char *filename;

          filename = NULL;
          asprintf (&filename, "%s/%s", throbber->image_dir, entries[i]->d_name);

          if (!ply_throbber_add_frame (throbber, filename))
            goto out;

          free (filename);
        }

      free (entries[i]);
      entries[i] = NULL;
    }
  load_finished = true;

out:
  if (!load_finished)
    {
      ply_throbber_remove_frames (throbber);

      while (entries[i] != NULL)
        {
          free (entries[i]);
          i++;
        }
    }
  free (entries);

  return load_finished;
}

bool
ply_throbber_load (ply_throbber_t *throbber)
{
  if (ply_array_get_size (throbber->frames) != 0)
    ply_throbber_remove_frames (throbber->frames);

  if (!ply_throbber_add_frames (throbber))
    return false;

  return true;
}

bool
ply_throbber_start (ply_throbber_t         *throbber,
                ply_event_loop_t   *loop,
                ply_window_t       *window,
                long                x,
                long                y)
{
  assert (throbber != NULL);
  assert (throbber->loop == NULL);

  throbber->loop = loop;
  throbber->window = window;
  throbber->frame_buffer = ply_window_get_frame_buffer (window);;

  throbber->x = x;
  throbber->y = y;

  throbber->start_time = ply_get_timestamp ();

  ply_event_loop_watch_for_timeout (throbber->loop,
                                    1.0 / FRAMES_PER_SECOND,
                                    (ply_event_loop_timeout_handler_t)
                                    on_timeout, throbber);

  return true;
}

void
ply_throbber_stop (ply_throbber_t *throbber)
{
  if (throbber->frame_area.width > 0)
    draw_background (throbber);

  throbber->frame_buffer = NULL;
  throbber->window = NULL;

  if (throbber->loop != NULL)
    {
      ply_event_loop_stop_watching_for_timeout (throbber->loop,
                                                (ply_event_loop_timeout_handler_t)
                                                on_timeout, throbber);
      throbber->loop = NULL;
    }
}

long
ply_throbber_get_width (ply_throbber_t *throbber)
{
  return throbber->width;
}

long
ply_throbber_get_height (ply_throbber_t *throbber)
{
  return throbber->height;
}

/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */