File: epgview

package info (click to toggle)
libvideo-capture-v4l-perl 0.224-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 572 kB
  • ctags: 563
  • sloc: ansic: 4,924; perl: 2,746; makefile: 57; sh: 22
file content (210 lines) | stat: -rwxr-xr-x 5,592 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl

use Storable;
use POSIX 'strftime';
use Carp;
use Curses;

my $pair = 0;

my $db_name = $ARGV[0];

END { endwin };

initscr; start_color;
cbreak; noecho;
nonl; intrflush 0; keypad 1;
immedok 0; idlok 1; scrollok 0; leaveok 1;
clear;

sub cattr($$) {
   return $cattr{$_[0],$_[1]} if defined $cattr{$_[0], $_[1]};
   $pair++;
   init_pair ($pair, $_[0], $_[1]);
   $cattr{$_[0],$_[1]} = COLOR_PAIR($pair);
}

sub date2unix {
   my($date,$time,$lto)=@_;
   381283200
   + ($date-45000) * 86400
   + ($time >> 12     ) * 10 * 60 * 60
   + ($time >>  8 & 15) * 60 * 60
   + ($time >>  4 & 15) * 10 * 60
   + ($time       & 15) * 60
   + $lto * 15;
}

sub date2text {
   sprintf "{%04x}", $_[0];
}

sub time2text {
   sprintf "%02x:%02x", $_[0] >> 8, $_[0] & 0xff;
}

my $wofs = -5*60;
my $to;
my $mtime = -1;
my @pi;
my $current = 1;
my $shortinfo = 1;

sub load_db {
   return if -M $db_name == $mtime;
   $mtime = -M $db_name;
   *db = eval { Storable::retrieve($db_name) } || { };
   @pi = map values %$_, values %{$db{pi}};
   for (@pi) {
      $_->{start_time} = date2unix($_->{start_date}, $_->{start_time}, $db{ai}{networks}[$_->{netwop_no}]{LTO});
      $_->{stop_time}  = date2unix($_->{start_date}, $_->{stop_time} , $db{ai}{networks}[$_->{netwop_no}]{LTO});
      $_->{stop_time} += 86400 if $_->{stop_time} < $_->{start_time};
   }
   @pi = sort { $a->{start_time} <=> $b->{start_time} ||
                $a->{netwop_no}  <=> $b->{netwop_no} } @pi;
}

move (1,0); standout; addch "=" for 1..$COLS; standend;

for(;;) {
   load_db;
   $now = time;
   $to = $now + 60;
   move (0,0); addstr sprintf "%s (window offset %5d)\n", strftime("%H:%M:%S", localtime $now), int($wofs / 60);
   show_current();
   move (0, 40); addstr ($to-$now);
   refresh;
   while (time < $to) {
      my $r = ""; vec ($r, fileno STDIN, 1) = 1;
      select $r,undef,undef,1;
      if (vec ($r, fileno STDIN, 1)) {
         my $key = getch;
         exit                     if $key eq "q" || $key eq "\x1b";
         $current   = !$current   if $key eq "c";
         $shortinfo = !$shortinfo if $key eq "s";
         $wofs  = -300            if $key eq "." || $key == KEY_HOME;
         $wofs -=  300            if $key eq "k" || $key == KEY_UP;
         $wofs +=  300            if $key eq "j" || $key == KEY_DOWN;
         $wofs -= 3600            if $key eq ""|| $key == KEY_PPAGE;
         $wofs += 3600            if $key eq ""|| $key == KEY_NPAGE;
         last;
      } elsif (-M $db_name != $mtime) {
         last;
      }
   }
}

my ($y,$x);

sub outstr {
   my $s = shift;
   my($l,$r);
   for (;;) {
      $l = length $s;
      $r = $COLS - $x;
      if ($l < $r) {
         addstr ($s);
         $x += $l;
         last;
      }
      addstr $1 if $s =~ s/^(.{1,$r})([\x00-\x20]+|$)//;
      $y++;
      move $y, 35;
      $x = 35;
   }
}

sub addttstr($$) {
   my ($fg, $bg) = (COLOR_WHITE, COLOR_BLACK);
   my $y1;
   local $_ = shift;
   y/~{|}[]//; # buggy as hell :(
   s/\s*([\x00-\x20])\s*/$1/g; # wipe away superflous spaces
   getyx $y, $x; $y1 = $y;
   for(;;) {
      if (/\G([\x20-\xff]+)/gc) {
         attrset (cattr ($fg, $bg));
         outstr $1;
      } elsif (/\G([\x00-\x07])/gc) {
         $fg = COLOR_BLACK	if $1 eq "\x00";
         $fg = COLOR_RED	if $1 eq "\x01";
         $fg = COLOR_GREEN	if $1 eq "\x02";
         $fg = COLOR_YELLOW	if $1 eq "\x03";
         $fg = COLOR_BLUE	if $1 eq "\x04";
         $fg = COLOR_MAGENTA	if $1 eq "\x05";
         $fg = COLOR_CYAN	if $1 eq "\x06";
         $fg = COLOR_WHITE	if $1 eq "\x07";
         outstr " ";
      } elsif (/\G\x1d/gc) {
         $bg = $fg;
         outstr " ";
      } elsif (/\G./gc) {
         # nop
      } else {
         last;
      }
   }
   attrset (cattr (COLOR_WHITE, COLOR_BLACK));
   #s/([\x00-\x07])/sprintf " [%dm", ord($1)+30/ge;
   #s/([\x00-\x09\x0b-\x1a\x1c-\x1f])/sprintf "[%02x]",ord $1/ge;
   #s/^ //g;
   #$_."";
   $_[1] += $y-$y1;
}

sub show_current {
   my $trenn = 2;
   my $lines = $LINES - 2;
   move (2, 0);
   clrtobot;
   for (@pi) {
      if ($_->{start_time} < $now + $wofs) {
         next if !$current || $_->{stop_time} < $now;
         $to = $_->{stop_time}  if $to > $_->{stop_time}  && $_->{stop_time}  >= $now;
      } else {
         $to = $_->{start_time} if $to > $_->{start_time} && $_->{start_time} > $now;
      }
      my $start = $_->{start_time};
      my $stop  = $_->{stop_time};
      if ($start > $now) {
         if ($trenn == 1) {
            addch (ACS_HLINE) for 1..$COLS;
            $lines--;
         }
         $trenn = 0;
      } elsif ($trenn == 2) {
         $trenn = 1;
      }

      addstr sprintf "%s-%s (%+4d) %-10.10s ",
             strftime("%H:%M", localtime $start), strftime("%H:%M (%d)", localtime $stop),
             int (($start-$now)/60),
             $db{ai}{networks}[$_->{netwop_no}]{netwop_name};

      my $si;
      if ($shortinfo) {
         unless ($_->{sinfo}) {
            $si = delete $_->{shortinfo};
            my $li = delete $_->{longinfo};
            for ($si, $li) {
               s/(.{40})/$1 /g;
               s/\s\s+/ /g;
            }
            for (my $x = length($si); $x; $x--) {
               if (substr ($si, -$x) eq substr ($li, 0, $x)) {
                  substr ($si, -$x) = $li;
                  last;
               }
            }
            $si =~ s/[\x00-\x20]+$//;
            $_->{sinfo} = $si;
         }
         $si = $_->{sinfo};
      }
      addttstr ($_->{title}.$si, $lines);

      addch ("\n");
      last unless --$lines;
   }
   clrtobot;
}