00001
00002 #include <signal.h>
00003
00004
00005 #include <iostream>
00006 #include <cstdlib>
00007 using namespace std;
00008
00009
00010 #include "util-x11.h"
00011 #include "util-xv.h"
00012
00013
00014 #include <X11/Xatom.h>
00015 #include <X11/Xutil.h>
00016 #include <X11/extensions/XShm.h>
00017 #include <X11/extensions/Xv.h>
00018 #include <X11/extensions/Xvlib.h>
00019
00020 QMap<int,port_info> open_xv_ports;
00021
00022 void close_all_xv_ports_signal_handler(int sig)
00023 {
00024 cerr<<"Signal: "<<sys_siglist[sig]<<endl;
00025 QMap<int,port_info>::iterator it;
00026 for (it = open_xv_ports.begin(); it != open_xv_ports.end(); ++it)
00027 {
00028 cerr<<"Ungrabbing XVideo port: "<<(*it).port<<endl;
00029 XvUngrabPort((*it).disp, (*it).port, CurrentTime);
00030 }
00031 exit(GENERIC_EXIT_NOT_OK);
00032 }
00033
00034 void add_open_xv_port(Display *disp, int port)
00035 {
00036 if (port >= 0)
00037 {
00038 open_xv_ports[port].disp = disp;
00039 open_xv_ports[port].port = port;
00040
00041 signal(SIGINT, close_all_xv_ports_signal_handler);
00042 }
00043 }
00044
00045 void del_open_xv_port(int port)
00046 {
00047 if (port >= 0)
00048 {
00049 open_xv_ports.remove(port);
00050
00051 if (!open_xv_ports.count())
00052 {
00053
00054 signal(SIGINT, SIG_DFL);
00055 }
00056 }
00057 }
00058
00059 bool has_open_xv_port(int port)
00060 {
00061 return open_xv_ports.find(port) != open_xv_ports.end();
00062 }
00063
00064 uint cnt_open_xv_port(void)
00065 {
00066 return open_xv_ports.count();
00067 }
00068
00069 QString xvflags2str(int flags)
00070 {
00071 QString str("");
00072 if (XvInputMask == (flags & XvInputMask))
00073 str.append("XvInputMask ");
00074 if (XvOutputMask == (flags & XvOutputMask))
00075 str.append("XvOutputMask ");
00076 if (XvVideoMask == (flags & XvVideoMask))
00077 str.append("XvVideoMask ");
00078 if (XvStillMask == (flags & XvStillMask))
00079 str.append("XvStillMask ");
00080 if (XvImageMask == (flags & XvImageMask))
00081 str.append("XvImageMask ");
00082 return str;
00083 }
00084
00085 bool xv_is_attrib_supported(
00086 Display *disp, int port, const char *name,
00087 int *current_value, int *min_value, int *max_value)
00088 {
00089 Atom xv_atom;
00090 XvAttribute *attributes;
00091 int attrib_count;
00092 int ret;
00093
00094 int dummy;
00095 int *xv_val = (current_value) ? current_value : &dummy;
00096
00097 X11S(attributes = XvQueryPortAttributes(disp, port, &attrib_count));
00098 for (int i = (attributes) ? 0 : attrib_count; i < attrib_count; i++)
00099 {
00100 if (strcmp(attributes[i].name, name))
00101 continue;
00102
00103 if (min_value)
00104 *min_value = attributes[i].min_value;
00105
00106 if (max_value)
00107 *max_value = attributes[i].max_value;
00108
00109 X11S(xv_atom = XInternAtom(disp, name, False));
00110 if (None == xv_atom)
00111 continue;
00112
00113 X11S(ret = XvGetPortAttribute(disp, port, xv_atom, xv_val));
00114 if (Success == ret)
00115 {
00116 X11S(XFree(attributes));
00117 return true;
00118 }
00119 }
00120
00121 if (attributes)
00122 X11S(XFree(attributes));
00123
00124 return false;
00125 }
00126
00127 bool xv_set_attrib(Display *disp, int port, const char *name, int val)
00128 {
00129 Atom xv_atom;
00130 X11S(xv_atom = XInternAtom(disp, name, False));
00131 if (xv_atom == None)
00132 return false;
00133
00134 int ret;
00135 X11S(ret = XvSetPortAttribute(disp, port, xv_atom, val));
00136 if (Success != ret)
00137 return false;
00138
00139 return true;
00140 }
00141
00142 bool xv_get_attrib(Display *disp, int port, const char *name, int &val)
00143 {
00144 Atom xv_atom;
00145 X11S(xv_atom = XInternAtom(disp, name, False));
00146 if (xv_atom == None)
00147 return false;
00148
00149 int ret;
00150 X11S(ret = XvGetPortAttribute(disp, port, xv_atom, &val));
00151 if (Success != ret)
00152 return false;
00153
00154 return true;
00155 }