summaryrefslogtreecommitdiff
path: root/xineplayer.c
blob: b3b07cb53c254284d390ca7f4383b7542aa2b4dc (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

#include "xineCommon.h"

#include "xineExternal.h"

static int fdControl = -1;
static int fdResult = -1;

bool writeString(const char *s)
{
  int l = ::strlen(s);

  while (l)
  {
    int r = ::write(fdControl, s, l);
    if (r < 0)
    {
      ::perror("xineplayer: writeString() failed");
      return false;
    }

    l -= r;
    s += r;
  }

  return true;
}

bool cmdPlay(char *const mrl)
{
  if (!::writeString("play "))
    return false;

  if (!::writeString(mrl))
    return false;

  if (!::writeString("\n"))
    return false;

  return true;
}

bool waitResult()
{
  char s;
  
  int r = ::read(fdResult, &s, 1);
  if (r < 0)
  {
    ::perror("xineplayer: waitResult() failed");
    return false;
  }

  return (1 == r);
}

bool communicate(char *const mrl)
{
  if (!::cmdPlay(mrl))
    return false;

  if (!::waitResult())
    return false;
  
  return true;
}

#define ARG_VDR_XINE_INSTANCE "--vdr-xine-instance="

int main(int argc, char *argv[])
{
  if (argc < 2)
  {
  usage:    
    ::fprintf(stderr, "usage: xineplayer [ " ARG_VDR_XINE_INSTANCE "N ] [ options ] mrl\n");
    return 1;
  }

  int instanceNo = -1;
  
  if (0 == ::strncmp(argv[ 1 ], ARG_VDR_XINE_INSTANCE, ::strlen(ARG_VDR_XINE_INSTANCE)))
  {
    instanceNo = ::atoi(argv[ 1 ] + ::strlen(ARG_VDR_XINE_INSTANCE));

    if (instanceNo < 0)
      goto usage;

    if (argc < 3)
      goto usage;
  }

  string fifoDir = FIFO_DIR;

  if (instanceNo >= 0)
  {
    char s[ 20 ];
    ::sprintf(s, "%d", instanceNo);
    
    fifoDir += s;
  }

  string fifoNameExtControl = fifoDir + FIFO_NAME_EXT_CONTROL;
  string fifoNameExtResult  = fifoDir + FIFO_NAME_EXT_RESULT;
  
  fdResult = ::open(fifoNameExtResult.c_str(), O_RDONLY);
  if (-1 == fdResult)
  {
    ::perror(("xineplayer: opening '" + fifoNameExtResult + "' failed").c_str());

    ::close(fdControl);
    return 1;
  }
  
  fdControl = ::open(fifoNameExtControl.c_str(), O_WRONLY);
  if (-1 == fdControl)
  {
    ::perror(("xineplayer: opening '" + fifoNameExtControl + "' failed").c_str());
    return 1;
  }
  
  bool result = ::communicate(argv[ argc - 1 ]);
  
  ::close(fdControl);
  ::close(fdResult);
  
  return !result;
}