summaryrefslogtreecommitdiff
path: root/src/libply-splash-core/ply-boot-splash.c
blob: d82d610c2d81e6c8087fdcfd7e315c395a7b2ecf (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/* ply-boot-splash.h - APIs for putting up a splash screen
 *
 * Copyright (C) 2007 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>
 *             Soeren Sandmann <sandmann@redhat.com>
 */
#include "config.h"
#include "ply-boot-splash.h"

#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <wchar.h>

#include "ply-boot-splash-plugin.h"
#include "ply-terminal.h"
#include "ply-event-loop.h"
#include "ply-list.h"
#include "ply-logger.h"
#include "ply-trigger.h"
#include "ply-utils.h"
#include "ply-progress.h"
#include "ply-keyboard.h"
#include "ply-key-file.h"

#ifndef UPDATES_PER_SECOND
#define UPDATES_PER_SECOND 30
#endif

#define KEY_CTRL_L ('\100' ^'L')
#define KEY_CTRL_T ('\100' ^'T')
#define KEY_CTRL_V ('\100' ^'V')

struct _ply_boot_splash
{
  ply_event_loop_t *loop;
  ply_module_handle_t *module_handle;
  const ply_boot_splash_plugin_interface_t *plugin_interface;
  ply_boot_splash_plugin_t *plugin;
  ply_boot_splash_mode_t mode;
  ply_keyboard_t *keyboard;
  ply_buffer_t *boot_buffer;
  ply_trigger_t *idle_trigger;
  ply_list_t *pixel_displays;
  ply_list_t *text_displays;

  char *theme_path;
  char *plugin_dir;
  char *status;

  ply_progress_t *progress;
  ply_boot_splash_on_idle_handler_t idle_handler;
  void *idle_handler_user_data;

  uint32_t is_loaded : 1;
  uint32_t should_force_text_mode : 1;
};

typedef const ply_boot_splash_plugin_interface_t *
        (* get_plugin_interface_function_t) (void);

static void ply_boot_splash_update_progress (ply_boot_splash_t *splash);
static void ply_boot_splash_detach_from_event_loop (ply_boot_splash_t *splash);

ply_boot_splash_t *
ply_boot_splash_new (const char     *theme_path,
                     const char     *plugin_dir,
                     ply_buffer_t   *boot_buffer)
{
  ply_boot_splash_t *splash;

  assert (theme_path != NULL);

  splash = calloc (1, sizeof (ply_boot_splash_t));
  splash->loop = NULL;
  splash->theme_path = strdup (theme_path);
  splash->plugin_dir = strdup (plugin_dir);
  splash->module_handle = NULL;
  splash->mode = PLY_BOOT_SPLASH_MODE_INVALID;

  splash->boot_buffer = boot_buffer;
  splash->pixel_displays = ply_list_new ();
  splash->text_displays = ply_list_new ();

  return splash;
}

static void
refresh_displays (ply_boot_splash_t *splash)
{
  ply_list_node_t *node;

  node = ply_list_get_first_node (splash->pixel_displays);
  while (node != NULL)
    {
      ply_pixel_display_t *display;
      ply_list_node_t *next_node;
      unsigned long width, height;

      display = ply_list_node_get_data (node);
      next_node = ply_list_get_next_node (splash->pixel_displays, node);

      width = ply_pixel_display_get_width (display);
      height = ply_pixel_display_get_height (display);

      ply_pixel_display_draw_area (display, 0, 0, width, height);
      node = next_node;
    }

  node = ply_list_get_first_node (splash->text_displays);
  while (node != NULL)
    {
      ply_text_display_t *display;
      ply_list_node_t *next_node;
      int number_of_columns, number_of_rows;

      display = ply_list_node_get_data (node);
      next_node = ply_list_get_next_node (splash->text_displays, node);

      number_of_columns = ply_text_display_get_number_of_columns (display);
      number_of_rows = ply_text_display_get_number_of_rows (display);

      ply_text_display_draw_area (display, 0, 0,
                                  number_of_columns,
                                  number_of_rows);
      node = next_node;
    }
}

static ply_terminal_t *
find_local_console_terminal (ply_boot_splash_t *splash)
{
  ply_list_node_t *node;
  node = ply_list_get_first_node (splash->text_displays);

  while (node != NULL)
    {
      ply_text_display_t *display;
      ply_terminal_t *terminal;
      ply_list_node_t *next_node;

      display = ply_list_node_get_data (node);
      next_node = ply_list_get_next_node (splash->text_displays, node);

      terminal = ply_text_display_get_terminal (display);

      if (terminal != NULL && ply_terminal_is_vt (terminal))
        return terminal;

      node = next_node;
    }

  return NULL;
}

static void
on_keyboard_input (ply_boot_splash_t *splash,
                   const char        *keyboard_input,
                   size_t             character_size)
{
  wchar_t key;

  if ((ssize_t) mbrtowc (&key, keyboard_input, character_size, NULL) > 0)
    {
      switch (key)
        {
          case KEY_CTRL_L:
            refresh_displays (splash);
          return;

          case KEY_CTRL_T:
            ply_trace ("toggle text mode!");
            splash->should_force_text_mode = !splash->should_force_text_mode;

            if (ply_list_get_length (splash->pixel_displays) >= 1)
              {
                ply_terminal_t *terminal;

                terminal = find_local_console_terminal (splash);

                if (terminal != NULL)
                  {
                    if (splash->should_force_text_mode)
                      {
                        ply_terminal_set_mode (terminal, PLY_TERMINAL_MODE_TEXT);
                        ply_terminal_ignore_mode_changes (terminal, true);
                      }
                    else
                      ply_terminal_ignore_mode_changes (terminal, false);
                  }
              }
            ply_trace ("text mode toggled!");
          return;

          case KEY_CTRL_V:
            ply_trace ("toggle verbose mode!");
            ply_toggle_tracing ();
            ply_trace ("verbose mode toggled!");
          return;
        }
    }
}

void
ply_boot_splash_set_keyboard (ply_boot_splash_t *splash,
                              ply_keyboard_t    *keyboard)
{
  splash->keyboard = keyboard;

  ply_keyboard_add_input_handler (keyboard,
                                  (ply_keyboard_input_handler_t)
                                  on_keyboard_input, splash);

  if (splash->plugin_interface->set_keyboard == NULL)
    return;

  splash->plugin_interface->set_keyboard (splash->plugin, keyboard);
}

void
ply_boot_splash_unset_keyboard (ply_boot_splash_t *splash)
{
  ply_keyboard_remove_input_handler (splash->keyboard,
                                     (ply_keyboard_input_handler_t)
                                     on_keyboard_input);

  if (splash->plugin_interface->set_keyboard == NULL)
    return;

  splash->plugin_interface->unset_keyboard (splash->plugin, splash->keyboard);
}

void
ply_boot_splash_add_pixel_display (ply_boot_splash_t   *splash,
                                   ply_pixel_display_t *display)
{
  ply_list_append_data (splash->pixel_displays, display);

  if (splash->plugin_interface->add_pixel_display == NULL)
    return;

  splash->plugin_interface->add_pixel_display (splash->plugin, display);
}

void
ply_boot_splash_remove_pixel_display (ply_boot_splash_t   *splash,
                                      ply_pixel_display_t *display)
{
  ply_list_remove_data (splash->pixel_displays, display);

  if (splash->plugin_interface->remove_pixel_display == NULL)
    return;

  splash->plugin_interface->remove_pixel_display (splash->plugin, display);
}

void
ply_boot_splash_add_text_display (ply_boot_splash_t   *splash,
                                  ply_text_display_t *display)
{
  ply_list_append_data (splash->text_displays, display);

  if (splash->plugin_interface->add_text_display == NULL)
    return;

  splash->plugin_interface->add_text_display (splash->plugin, display);
}

void
ply_boot_splash_remove_text_display (ply_boot_splash_t   *splash,
                                     ply_text_display_t *display)
{
  ply_list_remove_data (splash->text_displays, display);

  if (splash->plugin_interface->remove_pixel_display == NULL)
    return;

  splash->plugin_interface->remove_text_display (splash->plugin, display);
}

bool
ply_boot_splash_load (ply_boot_splash_t *splash)
{
  ply_key_file_t *key_file;
  char *module_name;
  char *module_path;

  assert (splash != NULL);

  get_plugin_interface_function_t get_boot_splash_plugin_interface;

  key_file = ply_key_file_new (splash->theme_path);

  if (!ply_key_file_load (key_file))
    return false;

  module_name = ply_key_file_get_value (key_file, "Plymouth Theme", "ModuleName");

  asprintf (&module_path, "%s%s.so",
            splash->plugin_dir, module_name);
  free (module_name);

  splash->module_handle = ply_open_module (module_path);

  free (module_path);

  if (splash->module_handle == NULL)
    {
      ply_key_file_free (key_file);
      return false;
    }

  get_boot_splash_plugin_interface = (get_plugin_interface_function_t)
      ply_module_look_up_function (splash->module_handle,
                                   "ply_boot_splash_plugin_get_interface");

  if (get_boot_splash_plugin_interface == NULL)
    {
      ply_save_errno ();
      ply_close_module (splash->module_handle);
      splash->module_handle = NULL;
      ply_key_file_free (key_file);
      ply_restore_errno ();
      return false;
    }

  splash->plugin_interface = get_boot_splash_plugin_interface ();

  if (splash->plugin_interface == NULL)
    {
      ply_save_errno ();
      ply_close_module (splash->module_handle);
      splash->module_handle = NULL;
      ply_key_file_free (key_file);
      ply_restore_errno ();
      return false;
    }

  splash->plugin = splash->plugin_interface->create_plugin (key_file);

  ply_key_file_free (key_file);

  assert (splash->plugin != NULL);

  splash->is_loaded = true;

  return true;
}

bool
ply_boot_splash_load_built_in (ply_boot_splash_t *splash)
{
  get_plugin_interface_function_t get_boot_splash_plugin_interface;

  assert (splash != NULL);

  splash->module_handle = ply_open_built_in_module ();

  if (splash->module_handle == NULL)
    return false;

  get_boot_splash_plugin_interface = (get_plugin_interface_function_t)
      ply_module_look_up_function (splash->module_handle,
                                   "ply_boot_splash_plugin_get_interface");

  if (get_boot_splash_plugin_interface == NULL)
    {
      ply_save_errno ();
      ply_close_module (splash->module_handle);
      splash->module_handle = NULL;
      ply_restore_errno ();
      return false;
    }

  splash->plugin_interface = get_boot_splash_plugin_interface ();

  if (splash->plugin_interface == NULL)
    {
      ply_save_errno ();
      ply_close_module (splash->module_handle);
      splash->module_handle = NULL;
      ply_restore_errno ();
      return false;
    }

  splash->plugin = splash->plugin_interface->create_plugin (NULL);

  assert (splash->plugin != NULL);

  splash->is_loaded = true;

  return true;
}

void
ply_boot_splash_unload (ply_boot_splash_t *splash)
{
  assert (splash != NULL);
  assert (splash->plugin != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->module_handle != NULL);

  splash->plugin_interface->destroy_plugin (splash->plugin);
  splash->plugin = NULL;

  ply_close_module (splash->module_handle);
  splash->plugin_interface = NULL;
  splash->module_handle = NULL;

  splash->is_loaded = false;
}

static void
remove_displays (ply_boot_splash_t *splash)
{
  ply_list_node_t *node, *next_node;

  ply_trace ("removing pixel displays");

  node = ply_list_get_first_node (splash->pixel_displays);
  while (node != NULL)
    {
      ply_pixel_display_t *display;
      ply_list_node_t *next_node;
      unsigned long width, height;

      display = ply_list_node_get_data (node);
      next_node = ply_list_get_next_node (splash->pixel_displays, node);

      width = ply_pixel_display_get_width (display);
      height = ply_pixel_display_get_height (display);

      ply_trace ("Removing %lux%lu pixel display", width, height);

      if (splash->plugin_interface->remove_pixel_display != NULL)
        splash->plugin_interface->remove_pixel_display (splash->plugin, display);

      ply_trace ("Removing node");
      ply_list_remove_node (splash->pixel_displays, node);

      node = next_node;
    }

  ply_trace ("removing text displays");
  node = ply_list_get_first_node (splash->text_displays);
  while (node != NULL)
    {
      ply_text_display_t *display;
      int number_of_columns, number_of_rows;

      display = ply_list_node_get_data (node);
      next_node = ply_list_get_next_node (splash->text_displays, node);

      number_of_columns = ply_text_display_get_number_of_columns (display);
      number_of_rows = ply_text_display_get_number_of_rows (display);

      ply_trace ("Removing %dx%d text display", number_of_columns, number_of_rows);

      if (splash->plugin_interface->remove_text_display != NULL)
        splash->plugin_interface->remove_text_display (splash->plugin, display);

      ply_trace ("Removing node");
      ply_list_remove_node (splash->text_displays, node);

      node = next_node;
    }
}

void
ply_boot_splash_free (ply_boot_splash_t *splash)
{
  ply_trace ("freeing splash");
  if (splash == NULL)
    return;

  if (splash->loop != NULL)
    {
      if (splash->plugin_interface->on_boot_progress != NULL)
        {
          ply_event_loop_stop_watching_for_timeout (splash->loop,
                                                    (ply_event_loop_timeout_handler_t)
                                                    ply_boot_splash_update_progress, splash);
        }

      ply_event_loop_stop_watching_for_exit (splash->loop, (ply_event_loop_exit_handler_t)
                                             ply_boot_splash_detach_from_event_loop,
                                             splash);
    }

  remove_displays (splash);
  ply_list_free (splash->pixel_displays);
  ply_list_free (splash->text_displays);

  if (splash->module_handle != NULL)
    ply_boot_splash_unload (splash);

  if (splash->idle_trigger != NULL)
    ply_trigger_free (splash->idle_trigger);

  free (splash->theme_path);
  free (splash->plugin_dir);
  free (splash);
}

static void
ply_boot_splash_update_progress (ply_boot_splash_t *splash)
{
  double percentage=0.0;
  double time=0.0;

  assert (splash != NULL);

  if (splash->progress)
    {
      percentage = ply_progress_get_percentage(splash->progress);
      time = ply_progress_get_time(splash->progress);
    }

  if (splash->plugin_interface->on_boot_progress != NULL)
    splash->plugin_interface->on_boot_progress (splash->plugin,
                                                time,
                                                percentage);

  ply_event_loop_watch_for_timeout (splash->loop,
                                   1.0 / UPDATES_PER_SECOND,
                                   (ply_event_loop_timeout_handler_t)
                                   ply_boot_splash_update_progress, splash);
}

void
ply_boot_splash_attach_progress (ply_boot_splash_t *splash,
                                      ply_progress_t    *progress)
{
  assert (splash != NULL);
  assert (progress != NULL);
  assert (splash->progress == NULL);
  splash->progress = progress;
}


bool
ply_boot_splash_show (ply_boot_splash_t *splash,
                      ply_boot_splash_mode_t mode)
{
  assert (splash != NULL);
  assert (mode != PLY_BOOT_SPLASH_MODE_INVALID);
  assert (splash->module_handle != NULL);
  assert (splash->loop != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  assert (splash->plugin_interface->show_splash_screen != NULL);

  if (splash->mode == mode)
    {
      ply_trace ("already set same splash screen mode");
      return true;
    }
  else if (splash->mode != PLY_BOOT_SPLASH_MODE_INVALID)
    {
      splash->plugin_interface->hide_splash_screen (splash->plugin,
                                                    splash->loop);
    }

  ply_trace ("showing splash screen");
  if (!splash->plugin_interface->show_splash_screen (splash->plugin,
                                                     splash->loop,
                                                     splash->boot_buffer,
                                                     mode))
    {

      ply_save_errno ();
      ply_trace ("can't show splash: %m");
      ply_restore_errno ();
      return false;
    }

  if (splash->plugin_interface->on_boot_progress != NULL)
    {
      ply_boot_splash_update_progress (splash);
    }

  splash->mode = mode;
  return true;
}

bool
ply_boot_splash_system_update (ply_boot_splash_t *splash,
                               int                progress)
{
  assert (splash != NULL);
  assert (splash->module_handle != NULL);
  assert (splash->loop != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  assert (splash->plugin_interface->system_update != NULL);

  ply_trace ("updating system %i%%", progress);
  splash->plugin_interface->system_update (splash->plugin,
                                           progress);
  return true;
}

void
ply_boot_splash_update_status (ply_boot_splash_t *splash,
                               const char        *status)
{
  assert (splash != NULL);
  assert (status != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  assert (splash->plugin_interface->update_status != NULL);
  assert (splash->mode != PLY_BOOT_SPLASH_MODE_INVALID);

  splash->plugin_interface->update_status (splash->plugin, status);
}

void
ply_boot_splash_update_output (ply_boot_splash_t *splash,
                               const char        *output,
                               size_t             size)
{
  assert (splash != NULL);
  assert (output != NULL);

  if (splash->plugin_interface->on_boot_output != NULL)
    splash->plugin_interface->on_boot_output (splash->plugin, output, size);
}

void
ply_boot_splash_root_mounted (ply_boot_splash_t *splash)
{
  assert (splash != NULL);

  if (splash->plugin_interface->on_root_mounted != NULL)
    splash->plugin_interface->on_root_mounted (splash->plugin);
}

static void
ply_boot_splash_detach_from_event_loop (ply_boot_splash_t *splash)
{
  assert (splash != NULL);
  splash->loop = NULL;
}

void
ply_boot_splash_hide (ply_boot_splash_t *splash)
{
  assert (splash != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  assert (splash->plugin_interface->hide_splash_screen != NULL);

  splash->plugin_interface->hide_splash_screen (splash->plugin,
                                                splash->loop);

  if (ply_list_get_length (splash->pixel_displays) >= 1)
    {
      ply_terminal_t *terminal;

      terminal = find_local_console_terminal (splash);

      if (terminal != NULL)
        ply_terminal_set_mode (terminal, PLY_TERMINAL_MODE_TEXT);
    }

  splash->mode = PLY_BOOT_SPLASH_MODE_INVALID;

  if (splash->loop != NULL)
    {
      if (splash->plugin_interface->on_boot_progress != NULL)
        {
          ply_event_loop_stop_watching_for_timeout (splash->loop,
                                                    (ply_event_loop_timeout_handler_t)
                                                    ply_boot_splash_update_progress, splash);
        }

      ply_event_loop_stop_watching_for_exit (splash->loop, (ply_event_loop_exit_handler_t)
                                             ply_boot_splash_detach_from_event_loop,
                                             splash);
    }
}

void ply_boot_splash_display_message (ply_boot_splash_t             *splash,
                                      const char                    *message)
{
  assert (splash != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);

  if (splash->plugin_interface->display_message != NULL)
    splash->plugin_interface->display_message (splash->plugin, message);
}

void ply_boot_splash_hide_message (ply_boot_splash_t             *splash,
                                      const char                 *message)
{
  assert (splash != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  if (splash->plugin_interface->hide_message != NULL)
    splash->plugin_interface->hide_message (splash->plugin, message);
}

void ply_boot_splash_display_normal  (ply_boot_splash_t              *splash)
{
  assert (splash != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  if (splash->plugin_interface->display_normal != NULL)
    splash->plugin_interface->display_normal (splash->plugin);
}

void ply_boot_splash_display_password (ply_boot_splash_t             *splash,
                                       const char                    *prompt,
                                       int                            bullets)
{
  assert (splash != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  if (splash->plugin_interface->display_password != NULL)
    splash->plugin_interface->display_password (splash->plugin, prompt, bullets);
}

void ply_boot_splash_display_question (ply_boot_splash_t             *splash,
                                       const char                    *prompt,
                                       const char                    *entry_text)
{
  assert (splash != NULL);
  assert (splash->plugin_interface != NULL);
  assert (splash->plugin != NULL);
  if (splash->plugin_interface->display_question != NULL)
    splash->plugin_interface->display_question (splash->plugin, prompt, entry_text);
}



void
ply_boot_splash_attach_to_event_loop (ply_boot_splash_t *splash,
                                      ply_event_loop_t  *loop)
{
  assert (splash != NULL);
  assert (loop != NULL);
  assert (splash->loop == NULL);

  splash->loop = loop;

  ply_event_loop_watch_for_exit (loop, (ply_event_loop_exit_handler_t) 
                                 ply_boot_splash_detach_from_event_loop,
                                 splash); 
}

static void
on_idle (ply_boot_splash_t *splash)
{

  ply_trace ("splash now idle");
  ply_event_loop_watch_for_timeout (splash->loop, 0.01,
                                    (ply_event_loop_timeout_handler_t)
                                    splash->idle_handler,
                                    splash->idle_handler_user_data);
  splash->idle_handler = NULL;
  splash->idle_handler_user_data = NULL;
}

void
ply_boot_splash_become_idle (ply_boot_splash_t                  *splash,
                             ply_boot_splash_on_idle_handler_t  idle_handler,
                             void                              *user_data)
{
  assert (splash->idle_trigger == NULL);

  if (splash->progress != NULL)
    {
      ply_progress_set_percentage (splash->progress, 1.0);
      ply_boot_splash_update_progress (splash);
    }

  ply_trace ("telling splash to become idle");
  if (splash->plugin_interface->become_idle == NULL)
    {
      ply_event_loop_watch_for_timeout (splash->loop, 0.01,
                                        (ply_event_loop_timeout_handler_t)
                                        idle_handler,
                                        user_data);

      return;
    }

  splash->idle_handler = idle_handler;
  splash->idle_handler_user_data = user_data;

  splash->idle_trigger = ply_trigger_new (&splash->idle_trigger);
  ply_trigger_add_handler (splash->idle_trigger,
                           (ply_trigger_handler_t) on_idle,
                           splash);

  splash->plugin_interface->become_idle (splash->plugin, splash->idle_trigger);
}

#ifdef PLY_BOOT_SPLASH_ENABLE_TEST

#include <stdio.h>

#include "ply-event-loop.h"
#include "ply-boot-splash.h"

typedef struct test_state test_state_t;
struct test_state {
  ply_event_loop_t *loop;
  ply_boot_splash_t *splash;
  ply_buffer_t *buffer;
};

static void
on_timeout (ply_boot_splash_t *splash)
{
  ply_boot_splash_update_status (splash, "foo");
  ply_event_loop_watch_for_timeout (splash->loop, 
                                    5.0,
                                   (ply_event_loop_timeout_handler_t)
                                   on_timeout,
                                   splash);
}

static void
on_quit (test_state_t *state)
{
    ply_boot_splash_hide (state->splash);
    ply_event_loop_exit (state->loop, 0);
}

static void
add_displays_to_splash_from_renderer (test_state_t   *state,
                                      ply_renderer_t *renderer)
{
  ply_list_t *heads;
  ply_list_node_t *node;

  heads = ply_renderer_get_heads (renderer);

  node = ply_list_get_first_node (heads);
  while (node != NULL)
    {
      ply_list_node_t *next_node;
      ply_renderer_head_t *head;
      ply_pixel_display_t *display;

      head = ply_list_node_get_data (node);
      next_node = ply_list_get_next_node (heads, node);

      display = ply_pixel_display_new (renderer, head);

      ply_boot_splash_add_pixel_display (state->splash, display);

      node = next_node;
    }
}

int
main (int    argc,
      char **argv)
{
  int exit_code;
  test_state_t state;
  char *tty_name;
  const char *theme_path;
  ply_text_display_t *text_display;
  ply_renderer_t *renderer;
  ply_terminal_t *terminal;
  ply_keyboard_t *keyboard;

  exit_code = 0;

  state.loop = ply_event_loop_new ();

  if (argc > 1)
    theme_path = argv[1];
  else
    theme_path = PLYMOUTH_THEME_PATH "/fade-in/fade-in.plymouth";

  if (argc > 2)
    asprintf(&tty_name, "tty%s", argv[2]);
  else
    tty_name = strdup("tty0");

  terminal = ply_terminal_new (tty_name);

  if (!ply_terminal_open (terminal))
    {
      perror ("could not open tty");
      return errno;
    }

  renderer = ply_renderer_new (NULL, terminal);
  free(tty_name);

  if (!ply_renderer_open (renderer))
    {
      perror ("could not open renderer /dev/fb");
      ply_renderer_free (renderer);
      return errno;
    }

  keyboard = ply_keyboard_new_for_renderer (renderer);
  ply_keyboard_add_escape_handler (keyboard,
                                   (ply_keyboard_escape_handler_t) on_quit, &state);

  state.buffer = ply_buffer_new ();
  state.splash = ply_boot_splash_new (theme_path, PLYMOUTH_PLUGIN_PATH, state.buffer);

  if (!ply_boot_splash_load (state.splash))
    {
      perror ("could not load splash screen");
      return errno;
    }

  ply_boot_splash_set_keyboard (state.splash, keyboard);
  add_displays_to_splash_from_renderer (&state, renderer);

  text_display = ply_text_display_new (terminal);
  ply_boot_splash_add_text_display (state.splash, text_display);

  ply_boot_splash_attach_to_event_loop (state.splash, state.loop);

  if (!ply_boot_splash_show (state.splash, PLY_BOOT_SPLASH_MODE_BOOT_UP))
    {
      perror ("could not show splash screen");
      return errno;
    }

  ply_event_loop_watch_for_timeout (state.loop, 
                                    1.0,
                                   (ply_event_loop_timeout_handler_t)
                                   on_timeout,
                                   state.splash);
  exit_code = ply_event_loop_run (state.loop);
  ply_boot_splash_free (state.splash);
  ply_buffer_free (state.buffer);

  return exit_code;
}

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