2015-08-18 19:40:26 +00:00
|
|
|
#ifndef APPLICATION_UNIX_CPP
|
2015-05-06 00:50:57 +00:00
|
|
|
#error This file may only be included from CApplicationUnix.cpp
|
|
|
|
#endif
|
|
|
|
|
2015-08-18 22:43:30 +00:00
|
|
|
#include "boo/IApplication.hpp"
|
2015-05-06 00:50:57 +00:00
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
#define explicit explicit_c
|
|
|
|
#include <xcb/xcb.h>
|
2015-05-10 07:02:18 +00:00
|
|
|
#include <xcb/xcb_event.h>
|
2015-05-09 05:33:48 +00:00
|
|
|
#include <xkbcommon/xkbcommon-x11.h>
|
|
|
|
#include <xcb/xkb.h>
|
2015-05-12 09:38:37 +00:00
|
|
|
#include <xcb/xinput.h>
|
2015-05-09 05:33:48 +00:00
|
|
|
#undef explicit
|
|
|
|
|
2015-05-13 22:21:13 +00:00
|
|
|
#include <dbus/dbus.h>
|
|
|
|
DBusConnection* registerDBus(const char* appName, bool& isFirst);
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
2015-05-09 05:33:48 +00:00
|
|
|
|
2015-05-12 09:38:37 +00:00
|
|
|
int XINPUT_OPCODE = 0;
|
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
static xcb_window_t getWindowOfEvent(xcb_generic_event_t* event, bool& windowEvent)
|
|
|
|
{
|
2015-05-10 07:02:18 +00:00
|
|
|
switch (XCB_EVENT_RESPONSE_TYPE(event))
|
2015-05-09 05:33:48 +00:00
|
|
|
{
|
2015-05-12 09:38:37 +00:00
|
|
|
case XCB_CLIENT_MESSAGE:
|
|
|
|
{
|
|
|
|
xcb_client_message_event_t* ev = (xcb_client_message_event_t*)event;
|
|
|
|
windowEvent = true;
|
|
|
|
return ev->window;
|
|
|
|
}
|
2015-05-09 05:33:48 +00:00
|
|
|
case XCB_EXPOSE:
|
|
|
|
{
|
|
|
|
xcb_expose_event_t* ev = (xcb_expose_event_t*)event;
|
|
|
|
windowEvent = true;
|
|
|
|
return ev->window;
|
|
|
|
}
|
|
|
|
case XCB_CONFIGURE_NOTIFY:
|
|
|
|
{
|
|
|
|
xcb_configure_notify_event_t* ev = (xcb_configure_notify_event_t*)event;
|
|
|
|
windowEvent = true;
|
|
|
|
return ev->window;
|
|
|
|
}
|
|
|
|
case XCB_KEY_PRESS:
|
|
|
|
{
|
|
|
|
xcb_key_press_event_t* ev = (xcb_key_press_event_t*)event;
|
|
|
|
windowEvent = true;
|
2015-05-12 09:38:37 +00:00
|
|
|
return ev->event;
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
case XCB_KEY_RELEASE:
|
|
|
|
{
|
|
|
|
xcb_key_release_event_t* ev = (xcb_key_release_event_t*)event;
|
|
|
|
windowEvent = true;
|
2015-05-12 09:38:37 +00:00
|
|
|
return ev->event;
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
case XCB_BUTTON_PRESS:
|
|
|
|
{
|
|
|
|
xcb_button_press_event_t* ev = (xcb_button_press_event_t*)event;
|
|
|
|
windowEvent = true;
|
2015-05-12 09:38:37 +00:00
|
|
|
return ev->event;
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
case XCB_BUTTON_RELEASE:
|
|
|
|
{
|
|
|
|
xcb_button_release_event_t* ev = (xcb_button_release_event_t*)event;
|
|
|
|
windowEvent = true;
|
2015-05-12 09:38:37 +00:00
|
|
|
return ev->event;
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
case XCB_MOTION_NOTIFY:
|
|
|
|
{
|
|
|
|
xcb_motion_notify_event_t* ev = (xcb_motion_notify_event_t*)event;
|
|
|
|
windowEvent = true;
|
2015-05-12 09:38:37 +00:00
|
|
|
return ev->event;
|
|
|
|
}
|
|
|
|
case XCB_GE_GENERIC:
|
|
|
|
{
|
|
|
|
xcb_ge_event_t* gev = (xcb_ge_event_t*)event;
|
|
|
|
if (gev->pad0 == XINPUT_OPCODE)
|
|
|
|
{
|
2015-05-13 08:51:18 +00:00
|
|
|
switch (gev->event_type)
|
2015-05-12 09:38:37 +00:00
|
|
|
{
|
2015-05-13 08:51:18 +00:00
|
|
|
case XCB_INPUT_MOTION:
|
2015-05-12 09:38:37 +00:00
|
|
|
{
|
2015-05-13 08:51:18 +00:00
|
|
|
xcb_input_motion_event_t* ev = (xcb_input_motion_event_t*)event;
|
|
|
|
windowEvent = true;
|
|
|
|
return ev->event;
|
2015-05-12 09:38:37 +00:00
|
|
|
}
|
2015-05-13 08:51:18 +00:00
|
|
|
case XCB_INPUT_TOUCH_BEGIN:
|
2015-05-12 09:38:37 +00:00
|
|
|
{
|
2015-05-13 08:51:18 +00:00
|
|
|
xcb_input_touch_begin_event_t* ev = (xcb_input_touch_begin_event_t*)event;
|
|
|
|
windowEvent = true;
|
|
|
|
return ev->event;
|
|
|
|
}
|
|
|
|
case XCB_INPUT_TOUCH_UPDATE:
|
|
|
|
{
|
|
|
|
xcb_input_touch_update_event_t* ev = (xcb_input_touch_update_event_t*)event;
|
|
|
|
windowEvent = true;
|
|
|
|
return ev->event;
|
|
|
|
}
|
|
|
|
case XCB_INPUT_TOUCH_END:
|
|
|
|
{
|
|
|
|
xcb_input_touch_end_event_t* ev = (xcb_input_touch_end_event_t*)event;
|
2015-05-12 09:38:37 +00:00
|
|
|
windowEvent = true;
|
|
|
|
return ev->event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 08:51:18 +00:00
|
|
|
windowEvent = false;
|
|
|
|
return 0;
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
2015-05-06 00:50:57 +00:00
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
IWindow* _CWindowXCBNew(const std::string& title, xcb_connection_t* conn);
|
2015-05-06 00:50:57 +00:00
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
class ApplicationXCB final : public IApplication
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
2015-05-10 07:02:18 +00:00
|
|
|
IApplicationCallback& m_callback;
|
2015-05-13 22:21:13 +00:00
|
|
|
const std::string m_uniqueName;
|
2015-05-06 00:50:57 +00:00
|
|
|
const std::string m_friendlyName;
|
|
|
|
const std::string m_pname;
|
|
|
|
const std::vector<std::string> m_args;
|
2015-05-09 05:33:48 +00:00
|
|
|
|
2015-05-13 22:21:13 +00:00
|
|
|
/* DBus single-instance */
|
|
|
|
bool m_singleInstance;
|
|
|
|
DBusConnection* m_dbus = NULL;
|
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
/* All windows */
|
|
|
|
std::unordered_map<xcb_window_t, IWindow*> m_windows;
|
|
|
|
|
2015-05-13 22:21:13 +00:00
|
|
|
xcb_connection_t* m_xcbConn = NULL;
|
2015-05-09 05:33:48 +00:00
|
|
|
bool m_running;
|
2015-05-06 00:50:57 +00:00
|
|
|
|
|
|
|
void _deletedWindow(IWindow* window)
|
|
|
|
{
|
2015-05-09 05:33:48 +00:00
|
|
|
m_windows.erase((xcb_window_t)window->getPlatformHandle());
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-08-18 19:40:26 +00:00
|
|
|
ApplicationXCB(IApplicationCallback& callback,
|
2015-05-13 22:21:13 +00:00
|
|
|
const std::string& uniqueName,
|
2015-05-06 00:50:57 +00:00
|
|
|
const std::string& friendlyName,
|
|
|
|
const std::string& pname,
|
2015-05-13 22:21:13 +00:00
|
|
|
const std::vector<std::string>& args,
|
|
|
|
bool singleInstance)
|
2015-05-06 00:50:57 +00:00
|
|
|
: m_callback(callback),
|
2015-05-13 22:21:13 +00:00
|
|
|
m_uniqueName(uniqueName),
|
2015-05-06 00:50:57 +00:00
|
|
|
m_friendlyName(friendlyName),
|
|
|
|
m_pname(pname),
|
2015-05-13 22:21:13 +00:00
|
|
|
m_args(args),
|
|
|
|
m_singleInstance(singleInstance)
|
2015-05-09 05:33:48 +00:00
|
|
|
{
|
2015-05-13 22:21:13 +00:00
|
|
|
/* DBus single instance registration */
|
|
|
|
bool isFirst;
|
|
|
|
m_dbus = registerDBus(uniqueName.c_str(), isFirst);
|
|
|
|
if (m_singleInstance)
|
|
|
|
{
|
|
|
|
if (!isFirst)
|
|
|
|
{
|
|
|
|
/* This is a duplicate instance, send signal and return */
|
|
|
|
if (args.size())
|
|
|
|
{
|
|
|
|
/* create a signal & check for errors */
|
|
|
|
DBusMessage*
|
|
|
|
msg = dbus_message_new_signal("/boo/signal/FileHandler",
|
|
|
|
"boo.signal.FileHandling",
|
|
|
|
"Open");
|
|
|
|
|
|
|
|
/* append arguments onto signal */
|
|
|
|
DBusMessageIter argsIter;
|
|
|
|
dbus_message_iter_init_append(msg, &argsIter);
|
|
|
|
for (const std::string& arg : args)
|
|
|
|
{
|
|
|
|
const char* sigvalue = arg.c_str();
|
|
|
|
dbus_message_iter_append_basic(&argsIter, DBUS_TYPE_STRING, &sigvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the message and flush the connection */
|
|
|
|
dbus_uint32_t serial;
|
|
|
|
dbus_connection_send(m_dbus, msg, &serial);
|
|
|
|
dbus_connection_flush(m_dbus);
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is the first instance, register for signal */
|
|
|
|
// add a rule for which messages we want to see
|
|
|
|
DBusError err = {};
|
|
|
|
dbus_bus_add_match(m_dbus, "type='signal',interface='boo.signal.FileHandling'", &err);
|
|
|
|
dbus_connection_flush(m_dbus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open X connection */
|
2015-05-09 05:33:48 +00:00
|
|
|
m_xcbConn = xcb_connect(NULL, NULL);
|
|
|
|
|
2015-05-13 08:51:18 +00:00
|
|
|
/* The xkb extension requests that the X server does not
|
2015-05-09 05:33:48 +00:00
|
|
|
* send repeated keydown events when a key is held */
|
|
|
|
xkb_x11_setup_xkb_extension(m_xcbConn,
|
|
|
|
XKB_X11_MIN_MAJOR_XKB_VERSION,
|
|
|
|
XKB_X11_MIN_MINOR_XKB_VERSION,
|
|
|
|
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
xcb_xkb_per_client_flags(m_xcbConn, XCB_XKB_ID_USE_CORE_KBD,
|
|
|
|
XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
|
|
|
|
XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, 0, 0, 0);
|
2015-05-12 09:38:37 +00:00
|
|
|
|
2015-05-13 08:51:18 +00:00
|
|
|
/* Xinput major opcode */
|
|
|
|
const xcb_query_extension_reply_t* xiReply =
|
|
|
|
xcb_get_extension_data(m_xcbConn, &xcb_input_id);
|
|
|
|
if (xiReply)
|
|
|
|
XINPUT_OPCODE = xiReply->major_opcode;
|
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
~ApplicationXCB()
|
2015-05-09 05:33:48 +00:00
|
|
|
{
|
|
|
|
xcb_disconnect(m_xcbConn);
|
|
|
|
}
|
2015-05-06 00:50:57 +00:00
|
|
|
|
|
|
|
EPlatformType getPlatformType() const
|
|
|
|
{
|
|
|
|
return PLAT_XCB;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2015-05-13 22:21:13 +00:00
|
|
|
if (!m_xcbConn)
|
|
|
|
return;
|
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
xcb_generic_event_t* event;
|
2015-05-10 07:02:18 +00:00
|
|
|
m_running = true;
|
|
|
|
m_callback.appLaunched(this);
|
2015-05-12 09:38:37 +00:00
|
|
|
xcb_flush(m_xcbConn);
|
2015-05-13 08:51:18 +00:00
|
|
|
|
2015-05-13 22:21:13 +00:00
|
|
|
int xcbFd = xcb_get_file_descriptor(m_xcbConn);
|
|
|
|
int dbusFd;
|
|
|
|
dbus_connection_get_unix_fd(m_dbus, &dbusFd);
|
|
|
|
int maxFd = MAX(xcbFd, dbusFd);
|
|
|
|
|
|
|
|
while (m_running)
|
2015-05-09 05:33:48 +00:00
|
|
|
{
|
2015-05-13 22:21:13 +00:00
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(xcbFd, &fds);
|
|
|
|
FD_SET(dbusFd, &fds);
|
|
|
|
select(maxFd+1, &fds, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (FD_ISSET(xcbFd, &fds))
|
|
|
|
{
|
|
|
|
event = xcb_poll_for_event(m_xcbConn);
|
|
|
|
if (!event)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bool windowEvent;
|
|
|
|
xcb_window_t evWindow = getWindowOfEvent(event, windowEvent);
|
|
|
|
//fprintf(stderr, "EVENT %d\n", XCB_EVENT_RESPONSE_TYPE(event));
|
|
|
|
if (windowEvent)
|
|
|
|
{
|
|
|
|
auto window = m_windows.find(evWindow);
|
|
|
|
if (window != m_windows.end())
|
|
|
|
window->second->_incomingEvent(event);
|
|
|
|
}
|
|
|
|
free(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(dbusFd, &fds))
|
2015-05-09 05:33:48 +00:00
|
|
|
{
|
2015-05-13 22:21:13 +00:00
|
|
|
DBusMessage* msg;
|
|
|
|
dbus_connection_read_write(m_dbus, 0);
|
|
|
|
while ((msg = dbus_connection_pop_message(m_dbus)))
|
|
|
|
{
|
|
|
|
/* check if the message is a signal from the correct interface and with the correct name */
|
|
|
|
if (dbus_message_is_signal(msg, "boo.signal.FileHandling", "Open"))
|
|
|
|
{
|
|
|
|
/* read the parameters */
|
2015-08-18 19:40:26 +00:00
|
|
|
std::vector<std::string> paths;
|
2015-05-13 22:21:13 +00:00
|
|
|
DBusMessageIter iter;
|
|
|
|
dbus_message_iter_init(msg, &iter);
|
|
|
|
while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID)
|
|
|
|
{
|
|
|
|
const char* argVal;
|
|
|
|
dbus_message_iter_get_basic(&iter, &argVal);
|
|
|
|
paths.push_back(argVal);
|
|
|
|
dbus_message_iter_next(&iter);
|
|
|
|
}
|
|
|
|
m_callback.appFilesOpen(this, paths);
|
|
|
|
}
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
}
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 22:21:13 +00:00
|
|
|
|
2015-05-10 07:02:18 +00:00
|
|
|
m_callback.appQuitting(this);
|
2015-05-09 05:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void quit()
|
|
|
|
{
|
|
|
|
m_running = false;
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
2015-05-13 22:21:13 +00:00
|
|
|
|
|
|
|
const std::string& getUniqueName() const
|
|
|
|
{
|
|
|
|
return m_uniqueName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& getFriendlyName() const
|
|
|
|
{
|
|
|
|
return m_friendlyName;
|
|
|
|
}
|
2015-05-06 00:50:57 +00:00
|
|
|
|
|
|
|
const std::string& getProcessName() const
|
|
|
|
{
|
|
|
|
return m_pname;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string>& getArgs() const
|
|
|
|
{
|
|
|
|
return m_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
IWindow* newWindow(const std::string& title)
|
|
|
|
{
|
2015-05-09 05:33:48 +00:00
|
|
|
IWindow* newWindow = _CWindowXCBNew(title, m_xcbConn);
|
|
|
|
m_windows[(xcb_window_t)newWindow->getPlatformHandle()] = newWindow;
|
|
|
|
return newWindow;
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|