MythTV  master
util-xv.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <iostream>
3 #include <cstdlib>
4 using namespace std;
5 
6 // Qt headers
7 #include <QCoreApplication>
8 
9 // MythTV headers
10 #include "signalhandling.h"
11 #include "mythlogging.h"
12 #include "mythxdisplay.h"
13 #include "util-xv.h"
14 
15 // X11 headers
16 #include <X11/Xatom.h>
17 #include <X11/Xutil.h>
18 #include <X11/extensions/XShm.h>
19 #include <X11/extensions/Xv.h>
20 #include <X11/extensions/Xvlib.h>
21 
22 QMap<int,port_info> open_xv_ports;
23 
25 {
26  LOG(VB_GENERAL, LOG_CRIT, QString("Signal: %1").arg(sys_siglist[sig]));
27  QMap<int,port_info>::iterator it;
28  for (it = open_xv_ports.begin(); it != open_xv_ports.end(); ++it)
29  {
30  restore_port_attributes((*it).port);
31  LOG(VB_GENERAL, LOG_CRIT, QString("Ungrabbing XVideo port: %1")
32  .arg((*it).port));
33  XvUngrabPort((*it).disp->GetDisplay(), (*it).port, CurrentTime);
34  }
35  QCoreApplication::exit(GENERIC_EXIT_NOT_OK);
36 }
38 {
40 }
42 {
44 }
45 
46 void save_port_attributes(int port)
47 {
48  if (!open_xv_ports.count(port))
49  return;
50 
51  open_xv_ports[port].attribs.clear();
52 
53  int attrib_count = 0;
54 
55  MythXDisplay *disp = open_xv_ports[port].disp;
56  MythXLocker lock(disp);
57  XvAttribute *attributes = XvQueryPortAttributes(disp->GetDisplay(),
58  port, &attrib_count);
59  if (!attributes || !attrib_count)
60  return;
61 
62  for (int i = 0; i < attrib_count; i++)
63  {
64  if (!(attributes[i].flags & XvGettable))
65  continue;
66 
67  int current;
68  if (xv_get_attrib(disp, port, attributes[i].name, current))
69  open_xv_ports[port].attribs[QString(attributes[i].name)] = current;
70  }
71 }
72 
73 void restore_port_attributes(int port, bool clear)
74 {
75  if (!open_xv_ports.count(port))
76  return;
77  if (open_xv_ports[port].attribs.empty())
78  return;
79 
80  MythXDisplay *disp = open_xv_ports[port].disp;
81  MythXLocker lock(disp);
82 
83  QMap<QString,int>::iterator it;
84  for (it = open_xv_ports[port].attribs.begin();
85  it != open_xv_ports[port].attribs.end(); ++it)
86  {
87  QByteArray ascii_name = it.key().toLatin1();
88  const char *cname = ascii_name.constData();
89  xv_set_attrib(disp, port, cname, it.value());
90  }
91 
92  if (clear)
93  open_xv_ports[port].attribs.clear();
94 }
95 
96 bool add_open_xv_port(MythXDisplay *disp, int port)
97 {
98  bool ret = false;
99  if (port >= 0)
100  {
101  open_xv_ports[port].disp = disp;
102  open_xv_ports[port].port = port;
103  QByteArray ascii_name = "XV_SET_DEFAULTS";
104  const char *name = ascii_name.constData();
105  ret = xv_is_attrib_supported(disp, port, name);
110  }
111  return ret;
112 }
113 
114 void del_open_xv_port(int port)
115 {
116  if (port >= 0)
117  {
118  open_xv_ports.remove(port);
119 
120  if (open_xv_ports.isEmpty())
121  {
122  SignalHandler::SetHandler(SIGINT, nullptr);
123  SignalHandler::SetHandler(SIGTERM, nullptr);
124  }
125  }
126 }
127 
128 bool has_open_xv_port(int port)
129 {
130  return open_xv_ports.find(port) != open_xv_ports.end();
131 }
132 
134 {
135  return open_xv_ports.count();
136 }
137 
138 QString xvflags2str(int flags)
139 {
140  QString str("");
141  if (XvInputMask == (flags & XvInputMask))
142  str.append("XvInputMask ");
143  if (XvOutputMask == (flags & XvOutputMask))
144  str.append("XvOutputMask ");
145  if (XvVideoMask == (flags & XvVideoMask))
146  str.append("XvVideoMask ");
147  if (XvStillMask == (flags & XvStillMask))
148  str.append("XvStillMask ");
149  if (XvImageMask == (flags & XvImageMask))
150  str.append("XvImageMask ");
151  return str;
152 }
153 
155  MythXDisplay *disp, int port, const char *name,
156  int *current_value, int *min_value, int *max_value)
157 {
158  Atom xv_atom;
159  XvAttribute *attributes;
160  int attrib_count;
161  int ret;
162 
163  int dummy;
164  int *xv_val = (current_value) ? current_value : &dummy;
165 
166  MythXLocker lock(disp);
167  attributes = XvQueryPortAttributes(disp->GetDisplay(),
168  port, &attrib_count);
169  for (int i = (attributes) ? 0 : attrib_count; i < attrib_count; i++)
170  {
171  if (strcmp(attributes[i].name, name) != 0)
172  continue;
173 
174  if (min_value)
175  *min_value = attributes[i].min_value;
176 
177  if (max_value)
178  *max_value = attributes[i].max_value;
179 
180  if (!(attributes[i].flags & XvGettable))
181  {
182  XFree(attributes);
183  return true;
184  }
185 
186  xv_atom = XInternAtom(disp->GetDisplay(), name, False);
187  if (None == xv_atom)
188  continue;
189 
190  ret = XvGetPortAttribute(disp->GetDisplay(), port, xv_atom, xv_val);
191  if (Success == ret)
192  {
193  XFree(attributes);
194  return true;
195  }
196  }
197 
198  if (attributes)
199  XFree(attributes);
200 
201  return false;
202 }
203 
204 bool xv_set_attrib(MythXDisplay *disp, int port, const char *name, int val)
205 {
206  Atom xv_atom;
207  XLOCK(disp, xv_atom = XInternAtom(disp->GetDisplay(), name, False));
208  if (xv_atom == None)
209  return false;
210 
211  int ret;
212  XLOCK(disp, ret = XvSetPortAttribute(disp->GetDisplay(),
213  port, xv_atom, val));
214  return Success == ret;
215 }
216 
217 bool xv_get_attrib(MythXDisplay *disp, int port, const char *name, int &val)
218 {
219  Atom xv_atom;
220  XLOCK(disp, xv_atom = XInternAtom(disp->GetDisplay(), name, False));
221  if (xv_atom == None)
222  return false;
223 
224  int ret;
225  XLOCK(disp, ret = XvGetPortAttribute(disp->GetDisplay(),
226  port, xv_atom, &val));
227  return Success == ret;
228 }
void save_port_attributes(int port)
Definition: util-xv.cpp:46
Display * GetDisplay(void)
Definition: mythxdisplay.h:21
static void close_all_xv_ports_signal_handler_SIGINT(void)
Definition: util-xv.cpp:37
#define XLOCK(dpy, arg)
Definition: mythxdisplay.h:75
QMap< int, port_info > open_xv_ports
Definition: util-xv.cpp:22
void del_open_xv_port(int port)
Definition: util-xv.cpp:114
This file is intended to hold X11 specific utility functions.
Definition: mythxdisplay.h:16
unsigned int uint
Definition: compat.h:140
QString xvflags2str(int flags)
Definition: util-xv.cpp:138
void restore_port_attributes(int port, bool clear)
Definition: util-xv.cpp:73
bool xv_is_attrib_supported(MythXDisplay *disp, int port, const char *name, int *current_value, int *min_value, int *max_value)
Definition: util-xv.cpp:154
uint cnt_open_xv_port(void)
Definition: util-xv.cpp:133
static void clear(SettingsMap &cache, SettingsMap &overrides, const QString &myKey)
Definition: mythdb.cpp:830
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
static void close_all_xv_ports_signal_handler_SIGTERM(void)
Definition: util-xv.cpp:41
void close_all_xv_ports_signal_handler(int sig)
Definition: util-xv.cpp:24
bool xv_get_attrib(MythXDisplay *disp, int port, const char *name, int &val)
Definition: util-xv.cpp:217
static void SetHandler(int signum, SigHandlerFunc handler)
const char * name
Definition: ParseText.cpp:328
bool has_open_xv_port(int port)
Definition: util-xv.cpp:128
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool xv_set_attrib(MythXDisplay *disp, int port, const char *name, int val)
Definition: util-xv.cpp:204
bool add_open_xv_port(MythXDisplay *disp, int port)
Definition: util-xv.cpp:96
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11