summaryrefslogtreecommitdiff
path: root/dvbapi.c
blob: 484a22e565b9f6a0bbff5eb2f853d536df465c8f (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
/*
 * dvbapi.c: Interface to the DVB driver
 *
 * See the main source file 'osm.c' for copyright information and
 * how to reach the author.
 *
 * $Id: dvbapi.c 1.2 2000/03/11 10:39:09 kls Exp $
 */

#include "dvbapi.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "interface.h"
#include "tools.h"

#define VIDEODEVICE "/dev/video"

const char *DvbQuality = "LMH"; // Low, Medium, High

bool DvbSetChannel(int FrequencyMHz, char Polarization, int Diseqc, int Srate, int Vpid, int Apid)
{
  int v = open(VIDEODEVICE, O_RDWR);
  
  if (v >= 0) {
     struct frontend front;
     ioctl(v, VIDIOCGFRONTEND, &front);
     unsigned int freq = FrequencyMHz;
     front.ttk = (freq < 11800UL) ? 0 : 1;
     if (freq < 11800UL)
        freq -=  9750UL;
     else
        freq -= 10600UL;
     front.freq      = freq * 1000000UL;
     front.diseqc    = Diseqc;
     front.srate     = Srate * 1000;
     front.volt      = (Polarization == 'v') ? 0 : 1;
     front.video_pid = Vpid;
     front.audio_pid = Apid;
     front.AFC       = 1;
     ioctl(v, VIDIOCSFRONTEND, &front);
     close(v);
     if (front.sync & 0x1F == 0x1F)
        return true;
     esyslog(LOG_ERR, "channel not sync'ed (front.sync=%X)!", front.sync);
     }
  else
     Interface.Error("can't open VIDEODEVICE");//XXX
  return false;
}

// -- cDvbRecorder -----------------------------------------------------------

cDvbRecorder::cDvbRecorder(void)
{
  recording = false;
}

cDvbRecorder::~cDvbRecorder()
{
  Stop();
}

bool cDvbRecorder::Recording(void)
{
  return recording;
}

bool cDvbRecorder::Record(const char *FileName, char Quality)
{
  isyslog(LOG_INFO, "record %s (%c)", FileName, Quality);
  if (MakeDirs(FileName)) {
     FILE *f = fopen(FileName, "a");
     if (f) {
        fprintf(f, "%s, %c\n", FileName, Quality);
        fclose(f);
        recording = true;
        // TODO
        Interface.Error("Recording not yet implemented!");
        return true;
        }
     else {
        Interface.Error("Can't write to file!");
        return false;
        }
     }
  // TODO
  return false;
}

bool cDvbRecorder::Play(const char *FileName, int Frame)
{
  if (!recording) {
     isyslog(LOG_INFO, "play %s (%d)", FileName, Frame);
     // TODO
     Interface.Error("Playback not yet implemented!");
     return true;
     }
  return false;
}

bool cDvbRecorder::FastForward(void)
{
  isyslog(LOG_INFO, "fast forward");
  // TODO
  return false;
}

bool cDvbRecorder::FastRewind(void)
{
  isyslog(LOG_INFO, "fast rewind");
  // TODO
  return false;
}

bool cDvbRecorder::Pause(void)
{
  isyslog(LOG_INFO, "pause");
  // TODO
  return false;
}

void cDvbRecorder::Stop(void)
{
  isyslog(LOG_INFO, "stop");
  recording = false;
  // TODO
}

int cDvbRecorder::Frame(void)
{
  isyslog(LOG_INFO, "frame");
  // TODO
  return 0;
}

// --- cDvbOsd ---------------------------------------------------------------

cDvbOsd::cDvbOsd(void)
{
  cols = rows = 0;
#ifdef DEBUG_OSD
  memset(&colorPairs, 0, sizeof(colorPairs));
  initscr();
  start_color();
  keypad(stdscr, TRUE);
  nonl();
  cbreak();
  noecho();
  timeout(1000);
  leaveok(stdscr, TRUE);
  window = stdscr;
#endif
#ifdef DEBUG_REMOTE
  initscr();
  keypad(stdscr, TRUE);
  nonl();
  cbreak();
  noecho();
  timeout(1000);
#endif
}

cDvbOsd::~cDvbOsd()
{
  Close();
}

#ifdef DEBUG_OSD
void cDvbOsd::SetColor(eDvbColor colorFg, eDvbColor colorBg)
{
  int color = (colorBg << 16) | colorFg | 0x80000000;
  for (int i = 0; i < MaxColorPairs; i++) {
      if (!colorPairs[i]) {
         colorPairs[i] = color;
         init_pair(i + 1, colorFg, colorBg);
         wattrset(window, COLOR_PAIR(i + 1));
         break;
         }
      else if (color == colorPairs[i]) {
         wattrset(window, COLOR_PAIR(i + 1));
         break;
         }
      }
}
#else
void cDvbOsd::Cmd(OSD_Command cmd, int color, int x0, int y0, int x1, int y1, const void *data)
{
  int v = open(VIDEODEVICE, O_RDWR);

  if (v >= 0) {
     struct drawcmd dc;
     dc.cmd   = cmd;
     dc.color = color;
     dc.x0    = x0;
     dc.y0    = y0;
     dc.x1    = x1;
     dc.y1    = y1;
     dc.data  = (void *)data;
     ioctl(v, VIDIOCSOSDCOMMAND, &dc);
     close(v);
     }
  else
     Interface.Error("can't open VIDEODEVICE");//XXX
}
#endif

void cDvbOsd::Open(int w, int h)
{
  cols = w;
  rows = h;
#ifdef DEBUG_OSD
  //XXX size...
  #define B2C(b) (((b) * 1000) / 255)
  #define SETCOLOR(n, r, g, b, o) init_color(n, B2C(r), B2C(g), B2C(b))
#else
  w *= charWidth;
  h *= lineHeight;
  int x = (720 - w) / 2; //TODO PAL vs. NTSC???
  int y = (576 - h) / 2;
  Cmd(OSD_Open, 4, x, y, x + w - 1, y + h - 1);
  #define SETCOLOR(n, r, g, b, o) Cmd(OSD_SetColor, n, r, g, b, o)
#endif
  SETCOLOR(clrBackground, 0x00, 0x00, 0x00, 127); // background 50% gray
  SETCOLOR(clrBlack,      0x00, 0x00, 0x00, 255);
  SETCOLOR(clrRed,        0xFC, 0x14, 0x14, 255);
  SETCOLOR(clrGreen,      0x24, 0xFC, 0x24, 255);
  SETCOLOR(clrYellow,     0xFC, 0xC0, 0x24, 255);
  SETCOLOR(clrBlue,       0x00, 0x00, 0xFC, 255);
  SETCOLOR(clrCyan,       0x00, 0xFC, 0xFC, 255);
  SETCOLOR(clrMagenta,    0xB0, 0x00, 0xFC, 255);
  SETCOLOR(clrWhite,      0xFC, 0xFC, 0xFC, 255);
}

void cDvbOsd::Close(void)
{
#ifndef DEBUG_OSD
  Cmd(OSD_Close);
#endif
}

void cDvbOsd::Clear(void)
{
#ifdef DEBUG_OSD
  SetColor(clrBackground, clrBackground);
  Fill(0, 0, cols, rows, clrBackground);
#else
  Cmd(OSD_Clear);
#endif
}

void cDvbOsd::Fill(int x, int y, int w, int h, eDvbColor color)
{
  if (x < 0) x = cols + x;
  if (y < 0) y = rows + y;
#ifdef DEBUG_OSD
  SetColor(color, color);
  for (int r = 0; r < h; r++) {
      wmove(window, y + r, x); // ncurses wants 'y' before 'x'!
      whline(window, ' ', w);
      }
#else
  Cmd(OSD_FillBlock, color, x * charWidth, y * lineHeight, (x + w) * charWidth - 1, (y + h) * lineHeight - 1);
#endif
}

void cDvbOsd::ClrEol(int x, int y, eDvbColor color)
{
  Fill(x, y, cols - x, 1, color);
}

void cDvbOsd::Text(int x, int y, const char *s, eDvbColor colorFg, eDvbColor colorBg)
{
  if (x < 0) x = cols + x;
  if (y < 0) y = rows + y;
#ifdef DEBUG_OSD
  SetColor(colorFg, colorBg);
  wmove(window, y, x); // ncurses wants 'y' before 'x'!
  waddstr(window, s);
#else
  Cmd(OSD_Text, (int(colorBg) << 16) | colorFg, x * charWidth, y * lineHeight, 1, 0, s);
#endif
}