Skip to content

Commit

Permalink
xcb: Refactor align of xcb events
Browse files Browse the repository at this point in the history
Use C++11 alignas instead of a union to pad the
xcb_*_event. It allows using the struct directly
without accessing a union member.

Change-Id: I221a6708ef7af844bd6b71a57dcbab75e1319c72
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
  • Loading branch information
msvetkin committed Jul 5, 2018
1 parent 4b10298 commit 4170552
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/plugins/platforms/xcb/qxcbclipboard.cpp
Expand Up @@ -581,7 +581,7 @@ void QXcbClipboard::handleSelectionRequest(xcb_selection_request_event_t *req)
return;
}

Q_DECLARE_XCB_EVENT(event, xcb_selection_notify_event_t);
q_padded_xcb_event<xcb_selection_notify_event_t> event = {};
event.response_type = XCB_SELECTION_NOTIFY;
event.requestor = req->requestor;
event.selection = req->selection;
Expand Down
11 changes: 2 additions & 9 deletions src/plugins/platforms/xcb/qxcbconnection.h
Expand Up @@ -772,18 +772,11 @@ struct QStdFreeDeleter {
call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call##_unchecked(__VA_ARGS__), nullptr) \
)

template <typename T>
union q_padded_xcb_event {
T event;
char padding[32];
};

// The xcb_send_event() requires all events to have 32 bytes. It calls memcpy() on the
// passed in event. If the passed in event is less than 32 bytes, memcpy() reaches into
// unrelated memory.
#define Q_DECLARE_XCB_EVENT(event_var, event_type) \
q_padded_xcb_event<event_type> store = {}; \
auto &event_var = store.event;
template <typename T>
struct alignas(32) q_padded_xcb_event : T { };

QT_END_NAMESPACE

Expand Down
3 changes: 1 addition & 2 deletions src/plugins/platforms/xcb/qxcbdrag.cpp
Expand Up @@ -1112,8 +1112,7 @@ static xcb_window_t findXdndAwareParent(QXcbConnection *c, xcb_window_t window)
void QXcbDrag::handleSelectionRequest(const xcb_selection_request_event_t *event)
{
qCDebug(lcQpaXDnd) << "handle selection request from target:" << event->requestor;

Q_DECLARE_XCB_EVENT(notify, xcb_selection_notify_event_t);
q_padded_xcb_event<xcb_selection_notify_event_t> notify = {};
notify.response_type = XCB_SELECTION_NOTIFY;
notify.requestor = event->requestor;
notify.selection = event->selection;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/platforms/xcb/qxcbwindow.cpp
Expand Up @@ -800,7 +800,7 @@ void QXcbWindow::hide()
xcb_unmap_window(xcb_connection(), m_window);

// send synthetic UnmapNotify event according to icccm 4.1.4
Q_DECLARE_XCB_EVENT(event, xcb_unmap_notify_event_t);
q_padded_xcb_event<xcb_unmap_notify_event_t> event = {};
event.response_type = XCB_UNMAP_NOTIFY;
event.event = xcbScreen()->root();
event.window = m_window;
Expand Down

0 comments on commit 4170552

Please sign in to comment.