00001 #include <qapplication.h>
00002 #include <qevent.h>
00003 #include <qkeysequence.h>
00004 #include <cstdio>
00005 #include <cerrno>
00006 #include <sys/wait.h>
00007 #include <sys/types.h>
00008 #include <unistd.h>
00009 #include <stdlib.h>
00010
00011 #include "mythcontext.h"
00012
00013 #include "lirc.h"
00014 #include "lircevent.h"
00015 #include "util.h"
00016
00017 #if (QT_VERSION < 0x030100)
00018 #error Native LIRC support requires Qt 3.1 or greater.
00019 #endif
00020
00028 LircClient::LircClient(QObject *main_window)
00029 {
00030 mainWindow = main_window;
00031 }
00032
00033 int LircClient::Init(const QString &config_file, const QString &program,
00034 bool ignoreExtApp)
00035 {
00036 int fd;
00037
00038
00039 fd = lirc_init((char *)program.latin1(), 1);
00040 if (fd == -1)
00041 {
00042 VERBOSE(VB_IMPORTANT,
00043 QString("lirc_init failed for %1, see preceding messages")
00044 .arg(program));
00045 return -1;
00046 }
00047
00048
00049 if (lirc_readconfig((char *)config_file.latin1(), &lircConfig, NULL))
00050 {
00051 VERBOSE(VB_IMPORTANT,
00052 QString("Failed to read lirc config %1 for %2")
00053 .arg(config_file).arg(program));
00054 lirc_deinit();
00055 return -1;
00056 }
00057
00058 if (!ignoreExtApp)
00059 external_app = gContext->GetSetting("LircKeyPressedApp", "");
00060
00061 VERBOSE(VB_GENERAL,
00062 QString("lirc init success using configuration file: %1")
00063 .arg(config_file));
00064
00065 return 0;
00066 }
00067
00068 LircClient::~LircClient()
00069 {
00070 lirc_deinit();
00071 lirc_freeconfig(lircConfig);
00072 }
00073
00074 void LircClient::Process(void)
00075 {
00076 char *code = 0;
00077 char *ir = 0;
00078 int ret;
00079
00080
00081 while (lirc_nextcode(&ir) == 0)
00082 {
00083 if (!ir)
00084 continue;
00085 while ((ret = lirc_code2char(lircConfig, ir, &code)) == 0 &&
00086 code != NULL)
00087 {
00088 QKeySequence a(code);
00089
00090 int keycode = 0;
00091
00092
00093
00094
00095 if (!a.count())
00096 QApplication::postEvent(mainWindow, new LircKeycodeEvent(code,
00097 keycode, true));
00098
00099 for (unsigned int i = 0; i < a.count(); i++)
00100 {
00101 keycode = a[i];
00102
00103 QApplication::postEvent(mainWindow, new LircKeycodeEvent(code,
00104 keycode, true));
00105 QApplication::postEvent(mainWindow, new LircKeycodeEvent(code,
00106 keycode, false));
00107
00108 SpawnApp();
00109 }
00110 }
00111
00112 free(ir);
00113 if (ret == -1)
00114 break;
00115 }
00116 }
00117
00118
00119 void LircClient::SpawnApp(void)
00120 {
00121
00122
00123 if (external_app.isEmpty())
00124 return;
00125
00126 QString command = external_app + " &";
00127
00128 int status = myth_system(command);
00129
00130 if (status > 0)
00131 {
00132 VERBOSE(VB_IMPORTANT,
00133 QString("External key pressed command exited with status %1")
00134 .arg(status));
00135 }
00136 }
00137