Skip to content

Commit

Permalink
Initial implementation of the --no-dbus option.
Browse files Browse the repository at this point in the history
  • Loading branch information
jehutting committed May 10, 2016
1 parent fb8261c commit 6d54e66
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 175 deletions.
148 changes: 11 additions & 137 deletions Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dbus/dbus.h>
#include <errno.h>

#include "utils/log.h"
#include "Keyboard.h"
#include "KeyConfig.h"


Keyboard::Keyboard()
{
Expand All @@ -32,19 +33,6 @@ Keyboard::Keyboard()
orig_fl = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, orig_fl | O_NONBLOCK);
}

if (dbus_connect() < 0)
{
CLog::Log(LOGWARNING, "Keyboard: DBus connection failed");
}
else
{
CLog::Log(LOGDEBUG, "Keyboard: DBus connection succeeded");
}

dbus_threads_init_default();
Create();
m_action = -1;
}

Keyboard::~Keyboard()
Expand All @@ -54,11 +42,6 @@ Keyboard::~Keyboard()

void Keyboard::Close()
{
if (ThreadHandle())
{
StopThread();
}
dbus_disconnect();
restore_term();
}

Expand All @@ -74,136 +57,27 @@ void Keyboard::restore_term()
}
}

void Keyboard::Sleep(unsigned int dwMilliSeconds)
{
struct timespec req;
req.tv_sec = dwMilliSeconds / 1000;
req.tv_nsec = (dwMilliSeconds % 1000) * 1000000;

while ( nanosleep(&req, &req) == -1 && errno == EINTR && (req.tv_nsec > 0 || req.tv_sec > 0));
}

void Keyboard::Process()
{
while(!m_bStop)
{
if (conn)
dbus_connection_read_write_dispatch(conn, 0);
int ch[8];
int chnum = 0;

while ((ch[chnum] = getchar()) != EOF) chnum++;

if (chnum > 1) ch[0] = ch[chnum - 1] | (ch[chnum - 2] << 8);

if (chnum > 0)
CLog::Log(LOGDEBUG, "Keyboard: character %c (0x%x)", ch[0], ch[0]);

if (m_keymap[ch[0]] != 0)
send_action(m_keymap[ch[0]]);
else
Sleep(20);
}
}

int Keyboard::getEvent()
{
int ret = m_action;
m_action = -1;
return ret;
}
int ch[8];
int chnum = 0;

void Keyboard::send_action(int action)
{
DBusMessage *message = NULL, *reply = NULL;
DBusError error;
m_action = action;
if (!conn)
return;

dbus_error_init(&error);

if (!(message = dbus_message_new_method_call(m_dbus_name.c_str(),
OMXPLAYER_DBUS_PATH_SERVER,
OMXPLAYER_DBUS_INTERFACE_PLAYER,
"Action")))
{
CLog::Log(LOGWARNING, "Keyboard: DBus error 1");
goto fail;
}

dbus_message_append_args(message, DBUS_TYPE_INT32, &action, DBUS_TYPE_INVALID);

reply = dbus_connection_send_with_reply_and_block(conn, message, -1, &error);
while ((ch[chnum] = getchar()) != EOF) chnum++;

if (!reply || dbus_error_is_set(&error))
goto fail;
if (chnum > 1) ch[0] = ch[chnum - 1] | (ch[chnum - 2] << 8);

dbus_message_unref(message);
dbus_message_unref(reply);

return;

fail:
if (dbus_error_is_set(&error))
if (chnum > 0)
{
printf("%s", error.message);
dbus_error_free(&error);
}
CLog::Log(LOGDEBUG, "Keyboard: character %c (0x%x)", ch[0], ch[0]);

if (message)
dbus_message_unref(message);
return m_keymap[ch[0]];
}

if (reply)
dbus_message_unref(reply);
return -1;
}

void Keyboard::setKeymap(std::map<int,int> keymap)
{
m_keymap = keymap;
}

void Keyboard::setDbusName(std::string dbus_name)
{
m_dbus_name = dbus_name;
}

int Keyboard::dbus_connect()
{
DBusError error;

dbus_error_init(&error);
if (!(conn = dbus_bus_get_private(DBUS_BUS_SESSION, &error)))
{
CLog::Log(LOGWARNING, "dbus_bus_get_private(): %s", error.message);
goto fail;
}

dbus_connection_set_exit_on_disconnect(conn, FALSE);

return 0;

fail:
if (dbus_error_is_set(&error))
dbus_error_free(&error);

if (conn)
{
dbus_connection_close(conn);
dbus_connection_unref(conn);
conn = NULL;
}

return -1;

}

void Keyboard::dbus_disconnect()
{
if (conn)
{
dbus_connection_close(conn);
dbus_connection_unref(conn);
conn = NULL;
}
}
30 changes: 8 additions & 22 deletions Keyboard.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
#define OMXPLAYER_DBUS_PATH_SERVER "/org/mpris/MediaPlayer2"
#define OMXPLAYER_DBUS_INTERFACE_ROOT "org.mpris.MediaPlayer2"
#define OMXPLAYER_DBUS_INTERFACE_PLAYER "org.mpris.MediaPlayer2.Player"

#include "OMXThread.h"
#include <map>

class Keyboard : public OMXThread
{
protected:
struct termios orig_termios;
int orig_fl;
int m_action;
DBusConnection *conn;
std::map<int,int> m_keymap;
std::string m_dbus_name;
public:
class Keyboard
{
public:
Keyboard();
~Keyboard();
void Close();
void Process();
void setKeymap(std::map<int,int> keymap);
void setDbusName(std::string dbus_name);
void Sleep(unsigned int dwMilliSeconds);
int getEvent();
private:
private:
struct termios orig_termios;
int orig_fl;
std::map<int,int> m_keymap;
void restore_term();
void send_action(int action);
int dbus_connect();
void dbus_disconnect();
};
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Usage: omxplayer [OPTIONS] [FILE]
--user-agent 'ua' Send specified User-Agent as part of HTTP requests
--lavfdopts 'opts' Options passed to libavformat, e.g. 'probesize:250000,...'
--avdict 'opts' Options passed to demuxer, e.g., 'rtsp_transport:tcp,...'
--no-dbus No omxplayer control through the D-Bus interface

For example:

Expand Down
Loading

0 comments on commit 6d54e66

Please sign in to comment.