aboutsummaryrefslogtreecommitdiff
path: root/lumina-desktop/LXcbEventFilter.h
blob: 390a2c144b82bbd809499834d2c8541f128a6ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2012, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
// This class provides the XCB ->Xlib conversion necessary for Qt5 usage
//===========================================
#ifndef _LUMINA_DESKTOP_XCB_FILTER_H
#define _LUMINA_DESKTOP_XCB_FILTER_H

#include <QAbstractNativeEventFilter>
#include <QList>
#include <QStringList>
#include <QDebug>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <xcb/damage.h>
#include <xcb/xcb_atom.h>
#include "LSession.h"

/*
List of XCB response types (since almost impossible to find good docs on XCB)
switch (xcb_generic_event_t*->response_type  & ~0x80)
case values:
XCB_KEY_[PRESS | RELEASE]
XCB_BUTTON_[PRESS | RELEASE]
XCB_MOTION_NOTIFY
XCB_ENTER_NOTIFY
XCB_LEAVE_NOTIFY
XCB_FOCUS_[IN | OUT]
XCB_KEYMAP_NOTIFY
XCB_EXPOSE
XCB_GRAPHICS_EXPOSURE
XCB_VISIBILITY_NOTIFY
XCB_CREATE_NOTIFY
XCB_DESTROY_NOTIFY
XCB_UNMAP_NOTIFY
XCB_MAP_[NOTIFY | REQUEST]
XCB_REPARENT_NOTIFY
XCB_CONFIGURE_[NOTIFY | REQUEST]
XCB_GRAVITY_NOTIFY
XCB_RESIZE_REQUEST
XCB_CIRCULATE_[NOTIFY | REQUEST]
XCB_PROPERTY_NOTIFY
XCB_SELECTION_[CLEAR | REQUEST | NOTIFY]
XCB_COLORMAP_NOTIFY
XCB_CLIENT_MESSAGE

Also: it appears that the Xlib "Window" is identical to an XCB "xcb_window_t"
*/

//SYSTEM TRAY STANDARD DEFINITIONS
#define SYSTEM_TRAY_REQUEST_DOCK 0
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
#define SYSTEM_TRAY_CANCEL_MESSAGE 2

class XCBEventFilter : public QAbstractNativeEventFilter{
private:
	LSession *session;
	QList<xcb_atom_t> atoms;
	xcb_atom_t _NET_SYSTEM_TRAY_OPCODE;
	int TrayDmgFlag; //internal damage event offset value for the system tray

	void checkClientMessage(xcb_client_message_event_t* event){
	   if(event->type == _NET_SYSTEM_TRAY_OPCODE && event->format == 32){
		//data32[0] is timestamp, [1] is opcode, [2] is  window handle
		uint32_t opcode = event->data.data32[1];
		if(opcode==SYSTEM_TRAY_REQUEST_DOCK){
		      session->SysTrayDockRequest(event->data.data32[2]);
		}
		//Ignore the System Tray messages at the moment (let the WM handle it)
	   }
	}
	
	void InitAtoms(){
	   atoms.clear();
	   QStringList names;
	    //List all the atoms that we want to detect for proprty changes
	    names << "_NET_CLIENT_LIST" << "_NET_ACTIVE_WINDOW" << "_NET_WM_NAME" << "_NET_WM_VISIBLE_NAME" \
			<< "_NET_WM_ICON_NAME" << "_NET_WM_VISIBLE_ICON_NAME" << "_NET_WM_STATE";
	   
	  xcb_connection_t *c = xcb_connect (NULL, NULL);
	  xcb_intern_atom_cookie_t *cs = (xcb_intern_atom_cookie_t *) malloc (names.length() * sizeof(xcb_intern_atom_cookie_t));
	  for(int i = 0; i < names.length(); ++i)
	    cs[i] = xcb_intern_atom (c, 0, names[i].length(), names[i].toStdString().c_str());

	  for(int i = 0; i < names.length(); ++i) {
	    xcb_intern_atom_reply_t *r = xcb_intern_atom_reply(c, cs[i], 0);
	    if(r){ atoms << r->atom; }
	    free(r);
	  }
	  //Also need the _net_system_tray_opcode atom as well
	  xcb_intern_atom_cookie_t cookie = xcb_intern_atom(c, 0, 23,"_NET_SYSTEM_TRAY_OPCODE");
	    xcb_intern_atom_reply_t *r = xcb_intern_atom_reply(c, cookie, 0);
	    if(r){ _NET_SYSTEM_TRAY_OPCODE = r->atom; }
	    free(r);
	}
	
public:
	XCBEventFilter(LSession *sessionhandle = 0) : QAbstractNativeEventFilter(){
	  session = sessionhandle; //save this for interaction with the session later
	  TrayDmgFlag = 0;
	  InitAtoms();
	}
	void setTrayDamageFlag(int flag){
	  //Special flag for system tray damage events
	  TrayDmgFlag = flag + XCB_DAMAGE_NOTIFY; //save the whole flag (no calculations later)
	}
	//This function format taken directly from the Qt5.3 documentation
	virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE
	{
		//qDebug() << "New Event";
		if(eventType=="xcb_generic_event_t"){
		  //Convert to known event type (for X11 systems)
		   xcb_generic_event_t *ev = static_cast<xcb_generic_event_t *>(message);
		  //Now parse the event and emit signals as necessary
		  switch( ev->response_type & ~0x80){
		    case XCB_PROPERTY_NOTIFY:
			//qDebug() << "Property Notify Event:";
			if( atoms.contains( ((xcb_property_notify_event_t*)ev)->atom) ){
			  //qDebug() << " - launch session property event";
			  session->WindowPropertyEvent();		
			}
			break;
		    
		    case XCB_CLIENT_MESSAGE:
			//qDebug() << "Client Message Event";
		        checkClientMessage( (xcb_client_message_event_t*)ev );
		        break;
		    
		    case XCB_DESTROY_NOTIFY:
			//qDebug() << "Window Closed Event";
			session->WindowClosedEvent( ( (xcb_destroy_notify_event_t*)ev )->window );
		        break;
		    
		    case XCB_CONFIGURE_NOTIFY:
			//qDebug() << "Configure Notify Event";
			session->WindowConfigureEvent( ((xcb_configure_notify_event_t*)ev)->window );
		        break;
		    
		    case XCB_SELECTION_CLEAR:
			//qDebug() << "Selection Clear Event";
			session->WindowSelectionClearEvent( ((xcb_selection_clear_event_t*)ev)->owner );  
		        break;
		    
		    default:
			if( (ev->response_type & ~0x80)==TrayDmgFlag){
			  session->WindowDamageEvent( ((xcb_damage_notify_event_t*)ev)->drawable );
			}/*else{
		          qDebug() << "Default Event:" << (ev->response_type & ~0x80);
		        }*/
		  }
		}
		//qDebug() << " - finished event";
		return false; //make sure the handling keeps going (transparent watching of events)
	}
	
};

#endif
bgstack15