summaryrefslogtreecommitdiffstats
path: root/src/libplybootsplash/ply-window.c
blob: 1e1f8c05801460aa2d15e954bd2b0b3f20fc168c (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
/* ply-window.h - APIs for putting up a window 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>
 */
#include "config.h"
#include "ply-window.h"

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

#include <linux/kd.h>
#include <linux/vt.h>

#include "ply-buffer.h"
#include "ply-event-loop.h"
#include "ply-frame-buffer.h"
#include "ply-logger.h"
#include "ply-utils.h"

#define KEY_CTRL_T ('\100' ^'T')
#define KEY_CTRL_U ('\100' ^'U')
#define KEY_CTRL_W ('\100' ^'W')
#define KEY_CTRL_V ('\100' ^'V')
#define KEY_ESCAPE ('\100' ^'[')
#define KEY_RETURN '\r'
#define KEY_BACKSPACE '\177'

#ifndef CLEAR_SCREEN_SEQUENCE
#define CLEAR_SCREEN_SEQUENCE "\033[2J"
#endif

#ifndef MOVE_CURSOR_SEQUENCE
#define MOVE_CURSOR_SEQUENCE "\033[%d;%df"
#endif

#ifndef HIDE_CURSOR_SEQUENCE
#define HIDE_CURSOR_SEQUENCE "\033[?25l"
#endif

#ifndef SHOW_CURSOR_SEQUENCE
#define SHOW_CURSOR_SEQUENCE "\033[?25h"
#endif

#ifndef COLOR_SEQUENCE_FORMAT
#define COLOR_SEQUENCE_FORMAT "\033[%dm"
#endif

#ifndef FOREGROUND_COLOR_BASE
#define FOREGROUND_COLOR_BASE 30
#endif

#ifndef BACKGROUND_COLOR_BASE
#define BACKGROUND_COLOR_BASE 40
#endif

struct _ply_window
{
  ply_event_loop_t *loop;
  ply_buffer_t     *keyboard_input_buffer;
  ply_buffer_t     *line_buffer;

  struct termios    original_term_attributes;

  ply_frame_buffer_t *frame_buffer;

  char *tty_name;
  int   tty_fd;
  int   vt_number;

  ply_fd_watch_t *tty_fd_watch;
  ply_window_mode_t mode;
  ply_window_color_t foreground_color;
  ply_window_color_t background_color;

  int number_of_text_rows;
  int number_of_text_columns;

  uint32_t should_force_text_mode : 1;
  uint32_t original_term_attributes_saved : 1;

  ply_window_keyboard_input_handler_t keyboard_input_handler;
  void *keyboard_input_handler_user_data;

  ply_window_backspace_handler_t backspace_handler;
  void *backspace_handler_user_data;

  ply_window_escape_handler_t escape_handler;
  void *escape_handler_user_data;

  ply_window_enter_handler_t enter_handler;
  void *enter_handler_user_data;
};

ply_window_t *
ply_window_new (int vt_number)
{
  ply_window_t *window;

  window = calloc (1, sizeof (ply_window_t));
  window->keyboard_input_buffer = ply_buffer_new ();
  window->line_buffer = ply_buffer_new ();
  window->frame_buffer = ply_frame_buffer_new (NULL);
  window->loop = NULL;
  asprintf (&window->tty_name, "/dev/tty%d", vt_number);
  window->tty_fd = -1;
  window->vt_number = vt_number;

  return window;
}

static void
process_backspace (ply_window_t *window)
{
  ssize_t bytes_to_remove;
  ssize_t previous_character_size;
  const char *bytes;
  size_t size;

  bytes = ply_buffer_get_bytes (window->line_buffer);
  size = ply_buffer_get_size (window->line_buffer);

  bytes_to_remove = MB_CUR_MAX;
  while ((previous_character_size = mbrlen (bytes + size - bytes_to_remove, bytes_to_remove, NULL)) < bytes_to_remove &&
         previous_character_size > 0)
    bytes_to_remove -= previous_character_size;

  if (bytes_to_remove <= size)
    {
      ply_buffer_remove_bytes_at_end (window->line_buffer, bytes_to_remove);

      if (window->backspace_handler != NULL)
        window->backspace_handler (window->backspace_handler_user_data);
    }
}

static void
process_line_erase (ply_window_t *window)
{
  size_t size;

  while ((size = ply_buffer_get_size (window->line_buffer)) > 0)
    process_backspace (window);
}

static void
process_keyboard_input (ply_window_t *window,
                        const char   *keyboard_input,
                        size_t        character_size)
{
  wchar_t key;

  if (mbrtowc (&key, keyboard_input, 1, NULL) > 0)
    {
      switch (key)
        {
          case KEY_CTRL_T:
            ply_trace ("toggle text mode!");
            window->should_force_text_mode = !window->should_force_text_mode;
            ply_window_set_mode (window, window->mode);
            ply_trace ("text mode toggled!");
          return;

          case KEY_CTRL_U:
          case KEY_CTRL_W:
            ply_trace ("erase line!");
            process_line_erase (window);
          return;

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

          case KEY_ESCAPE:
            ply_trace ("escape key!");
            if (window->escape_handler != NULL)
              window->escape_handler (window->escape_handler_user_data);
            ply_trace ("end escape key handler");
          return;

          case KEY_BACKSPACE:
            ply_trace ("backspace key!");
            process_backspace (window);
          return;

          case KEY_RETURN:
            ply_trace ("return key!");

            if (window->enter_handler != NULL)
              window->enter_handler (window->enter_handler_user_data,
                                     ply_buffer_get_bytes (window->line_buffer));

            ply_buffer_clear (window->line_buffer);
          return;

          default:
            ply_buffer_append_bytes (window->line_buffer,
                                     keyboard_input, character_size);
          break;
        }
    }

  if (window->keyboard_input_handler != NULL)
    window->keyboard_input_handler (window->keyboard_input_handler_user_data,
                                    keyboard_input, character_size);
}

static void
check_buffer_for_key_events (ply_window_t *window)
{
  const char *bytes;
  size_t size, i;

  bytes = ply_buffer_get_bytes (window->keyboard_input_buffer);
  size = ply_buffer_get_size (window->keyboard_input_buffer);

  i = 0;
  while (i < size)
    {
      ssize_t character_size;
      char *keyboard_input;

      character_size = (ssize_t) mbrlen (bytes + i, size - i, NULL);

      if (character_size < 0)
        break;

      keyboard_input = strndup (bytes + i, character_size);

      process_keyboard_input (window, keyboard_input, character_size);

      free (keyboard_input);

      i += character_size;
    }

  if (i > 0)
    ply_buffer_remove_bytes (window->keyboard_input_buffer, i);
}

static void
on_key_event (ply_window_t *window)
{
  ply_buffer_append_from_fd (window->keyboard_input_buffer, window->tty_fd);

  check_buffer_for_key_events (window);
}

static bool
ply_window_set_unbuffered_input (ply_window_t *window)
{
  struct termios term_attributes;

  tcgetattr (window->tty_fd, &term_attributes);

  if (!window->original_term_attributes_saved)
    {
      window->original_term_attributes = term_attributes;
      window->original_term_attributes_saved = true;
    }

  cfmakeraw (&term_attributes);

  /* Make \n return go to the beginning of the next line */
  term_attributes.c_oflag |= ONLCR;

  if (tcsetattr (window->tty_fd, TCSAFLUSH, &term_attributes) != 0)
    return false;

  return true;
}

static bool
ply_window_set_buffered_input (ply_window_t *window)
{
  if (!window->original_term_attributes_saved)
    return false;

  if (tcsetattr (window->tty_fd, TCSAFLUSH, &window->original_term_attributes) != 0)
    return false;

  return true;
}

static int
get_active_vt (void)
{
  int console_fd;
  struct vt_stat console_state = { 0 };

  console_fd = open ("/dev/tty0", O_RDONLY | O_NOCTTY);

  if (console_fd < 0)
    goto out;

  if (ioctl (console_fd, VT_GETSTATE, &console_state) < 0)
    goto out;

out:
  if (console_fd >= 0)
    close (console_fd);

  return console_state.v_active;
}

bool
ply_window_look_up_geometry (ply_window_t *window)
{
    struct winsize window_size;

    ply_trace ("looking up window text geometry");

    if (ioctl (window->tty_fd, TIOCGWINSZ, &window_size) < 0)
      {
        ply_trace ("could not read window text geometry: %m");
        return false;
      }

    window->number_of_text_rows = window_size.ws_row;
    window->number_of_text_columns = window_size.ws_col;

    ply_trace ("window is now %dx%d text cells",
               window->number_of_text_columns,
               window->number_of_text_rows);

    return true;
}

bool
ply_window_open (ply_window_t *window)
{
  assert (window != NULL);
  assert (window->tty_name != NULL);
  assert (window->tty_fd < 0);

  if (window->vt_number == 0)
    {
      window->vt_number = get_active_vt ();
      free (window->tty_name);
      asprintf (&window->tty_name, "/dev/tty%d", window->vt_number);
    }

  window->tty_fd = open (window->tty_name, O_RDWR | O_NOCTTY);

  if (window->tty_fd < 0)
    return false;

  if (!ply_window_set_unbuffered_input (window))
    return false;

  if (!ply_window_set_mode (window, PLY_WINDOW_MODE_TEXT))
    return false;

  if (!ply_window_look_up_geometry (window))
    return false;

  ply_window_hide_text_cursor (window);
  ply_window_set_text_cursor_position (window, 0, 0);

  ply_event_loop_watch_signal (window->loop,
                               SIGWINCH,
                               (ply_event_handler_t)
                               ply_window_look_up_geometry,
                               window);

  if (window->loop != NULL)
    window->tty_fd_watch = ply_event_loop_watch_fd (window->loop, window->tty_fd,
                                                    PLY_EVENT_LOOP_FD_STATUS_HAS_DATA,
                                                    (ply_event_handler_t) on_key_event,
                                                    NULL, window);

  /* We try to open the frame buffer, but it may fail. splash plugins can check
   * to see if it's open and react accordingly
   */
  ply_frame_buffer_open (window->frame_buffer);

  return true;
}

void
ply_window_close (ply_window_t *window)
{
  ply_window_set_text_cursor_position (window, 0, 0);

  if (ply_frame_buffer_device_is_open (window->frame_buffer))
    ply_frame_buffer_close (window->frame_buffer);

  if (window->tty_fd_watch != NULL)
    {
      ply_event_loop_stop_watching_fd (window->loop, window->tty_fd_watch);
      window->tty_fd_watch = NULL;
    }

  if (window->loop != NULL)
    ply_event_loop_stop_watching_signal (window->loop, SIGWINCH);

  ply_window_set_buffered_input (window);

  close (window->tty_fd);
  window->tty_fd = -1;
}

bool
ply_window_set_mode (ply_window_t      *window,
                     ply_window_mode_t  mode)
{
  assert (window != NULL);
  assert (mode == PLY_WINDOW_MODE_TEXT || mode == PLY_WINDOW_MODE_GRAPHICS);

  if (ply_is_tracing ())
    window->should_force_text_mode = true;

  switch (mode)
    {
      case PLY_WINDOW_MODE_TEXT:
        if (ioctl (window->tty_fd, KDSETMODE, KD_TEXT) < 0)
          return false;
        break;

      case PLY_WINDOW_MODE_GRAPHICS:
        if (!ply_frame_buffer_device_is_open (window->frame_buffer)
            && !ply_frame_buffer_open (window->frame_buffer))
          return false;

        if (ioctl (window->tty_fd, KDSETMODE,
                   window->should_force_text_mode? KD_TEXT : KD_GRAPHICS) < 0)
          return false;
        break;
    }
  ply_window_set_unbuffered_input (window);

  window->mode = mode;
  return true;
}

int
ply_window_get_number_of_text_rows (ply_window_t *window)
{
  return window->number_of_text_rows;
}

int
ply_window_get_number_of_text_columns (ply_window_t *window)
{
  return window->number_of_text_columns;
}

void
ply_window_set_text_cursor_position (ply_window_t *window,
                                     int           column,
                                     int           row)
{
  char *sequence;

  sequence = NULL;
  asprintf (&sequence, MOVE_CURSOR_SEQUENCE, row, column);
  write (window->tty_fd, sequence, strlen (sequence));
  free (sequence);
}

void
ply_window_clear_screen (ply_window_t *window)
{
  write (window->tty_fd, CLEAR_SCREEN_SEQUENCE, strlen (CLEAR_SCREEN_SEQUENCE));

  ply_window_set_text_cursor_position (window, 0, 0);

  if (ply_frame_buffer_device_is_open (window->frame_buffer))
    ply_frame_buffer_fill_with_color (window->frame_buffer, NULL, 0.0, 0.0, 0.0, 1.0);
}

void
ply_window_set_background_color (ply_window_t       *window,
                                 ply_window_color_t  color)
{
  char *sequence;

  sequence = NULL;
  asprintf (&sequence, COLOR_SEQUENCE_FORMAT,
            BACKGROUND_COLOR_BASE + color);
  write (window->tty_fd, sequence, strlen (sequence));
  free (sequence);

  window->background_color = color;
}

void
ply_window_set_foreground_color (ply_window_t       *window,
                                 ply_window_color_t  color)
{
  char *sequence;

  sequence = NULL;
  asprintf (&sequence, COLOR_SEQUENCE_FORMAT,
            FOREGROUND_COLOR_BASE + color);
  write (window->tty_fd, sequence, strlen (sequence));
  free (sequence);

  window->foreground_color = color;
}

ply_window_color_t
ply_window_get_background_color (ply_window_t *window)
{
  return window->background_color;
}

ply_window_color_t
ply_window_get_foreground_color (ply_window_t *window)
{
  return window->foreground_color;
}

void
ply_window_hide_text_cursor (ply_window_t *window)
{
  write (window->tty_fd, HIDE_CURSOR_SEQUENCE, strlen (HIDE_CURSOR_SEQUENCE));
}

void
ply_window_show_text_cursor (ply_window_t *window)
{
  write (window->tty_fd, SHOW_CURSOR_SEQUENCE, strlen (SHOW_CURSOR_SEQUENCE));
}

static void
ply_window_detach_from_event_loop (ply_window_t *window)
{
  assert (window != NULL);
  window->loop = NULL;
  window->tty_fd_watch = NULL;
}

void
ply_window_free (ply_window_t *window)
{
  if (window == NULL)
    return;

  if (window->loop != NULL)
    ply_event_loop_stop_watching_for_exit (window->loop,
                                           (ply_event_loop_exit_handler_t)
                                           ply_window_detach_from_event_loop,
                                           window);

  ply_window_close (window);

  ply_buffer_free (window->keyboard_input_buffer);
  ply_buffer_free (window->line_buffer);

  ply_frame_buffer_free (window->frame_buffer);

  free (window);
}

void
ply_window_set_keyboard_input_handler (ply_window_t *window,
                                       ply_window_keyboard_input_handler_t input_handler,
                                       void       *user_data)
{
  assert (window != NULL);

  window->keyboard_input_handler = input_handler;
  window->keyboard_input_handler_user_data = user_data;
}

void
ply_window_set_backspace_handler (ply_window_t *window,
                                  ply_window_backspace_handler_t backspace_handler,
                                  void         *user_data)
{
  assert (window != NULL);

  window->backspace_handler = backspace_handler;
  window->backspace_handler_user_data = user_data;
}

void
ply_window_set_escape_handler (ply_window_t *window,
                               ply_window_escape_handler_t escape_handler,
                               void       *user_data)
{
  assert (window != NULL);

  window->escape_handler = escape_handler;
  window->escape_handler_user_data = user_data;
}

void
ply_window_set_enter_handler (ply_window_t *window,
                              ply_window_enter_handler_t enter_handler,
                              void         *user_data)
{
  assert (window != NULL);

  window->enter_handler = enter_handler;
  window->enter_handler_user_data = user_data;
}

void
ply_window_attach_to_event_loop (ply_window_t     *window,
                                 ply_event_loop_t *loop)
{
  assert (window != NULL);
  assert (loop != NULL);
  assert (window->loop == NULL);

  window->loop = loop;

  if (window->tty_fd >= 0)
    window->tty_fd_watch = ply_event_loop_watch_fd (window->loop, window->tty_fd,
                                                    PLY_EVENT_LOOP_FD_STATUS_HAS_DATA,
                                                    (ply_event_handler_t) on_key_event,
                                                    NULL, window);

  ply_event_loop_watch_for_exit (loop, (ply_event_loop_exit_handler_t)
                                 ply_window_detach_from_event_loop,
                                 window);
}

ply_frame_buffer_t *
ply_window_get_frame_buffer (ply_window_t *window)
{
  return window->frame_buffer;
}

static bool
switch_to_vt (int tty_fd,
              int vt_number)
{
  if (ioctl (tty_fd, VT_ACTIVATE, vt_number) < 0)
    return false;

  ioctl (tty_fd, VT_WAITACTIVE, vt_number);

  return get_active_vt () == vt_number;
}

bool
ply_window_take_console (ply_window_t *window)
{
  assert (window != NULL);

  return switch_to_vt (window->tty_fd, window->vt_number);
}

bool
ply_window_give_console (ply_window_t *window,
                         int           vt_number)
{
  char *tty_name;
  int   tty_fd;
  bool vt_switched;

  assert (window != NULL);
  assert (vt_number > 0);
  assert (vt_number != window->vt_number);

  tty_name = NULL;
  asprintf (&tty_name, "/dev/tty%d", vt_number);
  tty_fd = open (tty_name, O_RDONLY | O_NOCTTY);

  if (tty_fd < 0)
    {
      free (tty_name);
      return false;
    }
  free (tty_name);

  vt_switched = switch_to_vt (tty_fd, vt_number);

  close (tty_fd);

  return vt_switched;
}

#ifdef PLY_WINDOW_ENABLE_TEST

#include <stdio.h>

#include "ply-event-loop.h"
#include "ply-window.h"

static void
on_timeout (ply_window_t     *window,
            ply_event_loop_t *loop)
{
  ply_event_loop_exit (loop, 0);
}

static void
on_keypress (ply_window_t *window,
             const char   *keyboard_input)
{
  printf ("key '%c' (0x%x) was pressed\n",
          keyboard_input[0], (unsigned int) keyboard_input[0]);
}

int
main (int    argc,
      char **argv)
{
  ply_event_loop_t *loop;
  ply_window_t *window;
  int exit_code;
  int vt_number;

  exit_code = 0;

  loop = ply_event_loop_new ();

  vt_number = 0;
  if (argc > 1)
    vt_number = atoi (argv[1]);

  if (vt_number <= 0)
    vt_number = 1;

  window = ply_window_new (vt_number);
  ply_window_attach_to_event_loop (window, loop);
  ply_window_set_keyboard_input_handler (window,
                                         (ply_window_keyboard_input_handler_t)
                                         on_keypress, window);

  if (!ply_window_open (window))
    {
      ply_save_errno ();
      perror ("could not open window");
      ply_restore_errno ();
      return errno;
    }

  if (!ply_window_take_console (window))
    {
      ply_save_errno ();
      ply_error ("could not move console to vt %d: %m", vt_number);
      ply_restore_errno ();
      return errno;
    }

  if (!ply_window_set_mode (window, PLY_WINDOW_MODE_TEXT))
    {
      ply_save_errno ();
      perror ("could not set window for graphics mode");
      ply_restore_errno ();
    }

  ply_event_loop_watch_for_timeout (loop,
                                    15.0,
                                   (ply_event_loop_timeout_handler_t)
                                   on_timeout,
                                   window);
  exit_code = ply_event_loop_run (loop);

  ply_window_close (window);
  ply_window_free (window);

  return exit_code;
}

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