aboutsummaryrefslogtreecommitdiff
path: root/lumina-desktop/panel-plugins/systemtray/LSysTray.cpp
blob: 2472d0640d08b4f8f3a027ac5596b6fee54023c9 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2012, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "LSysTray.h"
#include "../../LSession.h"

/*#include <LuminaX11.h>
//X includes (these need to be last due to Qt compile issues)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xdamage.h>*/

//Static variables for damage detection (tray update notifications)
//static int dmgEvent = 0;
//static int dmgError = 0;

LSysTray::LSysTray(QWidget *parent, QString id, bool horizontal) : LPPlugin(parent, id, horizontal){
  frame = new QFrame(this);
  frame->setContentsMargins(0,0,0,0);
  frame->setStyleSheet("QFrame{ background: transparent; border: 1px solid transparent; border-radius: 3px; }");
  LI = new QBoxLayout( this->layout()->direction());
    frame->setLayout(LI);
    LI->setAlignment(Qt::AlignCenter);
    LI->setSpacing(1);
    LI->setContentsMargins(0,0,0,0);
  this->layout()->addWidget(frame);
  this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  //TrayID=0;
  upTimer = new QTimer(this);
    upTimer->setInterval(300000); //maximum time between refreshes is 5 minutes
    connect(upTimer, SIGNAL(timeout()), this, SLOT(checkAll()) );
  isRunning = false; stopping = false; checking = false;
  QTimer::singleShot(100, this, SLOT(start()) );
	
  connect(LSession::handle(), SIGNAL(TrayListChanged()), this, SLOT(checkAll()) );
  connect(LSession::handle(), SIGNAL(TrayIconChanged(WId)), this, SLOT(UpdateTrayWindow(WId)) );
  connect(LSession::handle(), SIGNAL(VisualTrayAvailable()), this, SLOT(start()) );
}

LSysTray::~LSysTray(){
 if(isRunning){
   this->stop();
 }
}

void LSysTray::start(){
  if(isRunning || stopping){ return; } //already running
  isRunning = LSession::handle()->registerVisualTray(this->winId());
  qDebug() << "Visual Tray Started:" << this->type() << isRunning;
  if(isRunning){ 
    //upTimer->start();
    QTimer::singleShot(0,this, SLOT(checkAll()) ); 
  }
  //Make sure we catch all events right away
  /*connect(LSession::instance(),SIGNAL(aboutToQuit()),this,SLOT(closeAll()) );
  connect(LSession::instance(),SIGNAL(TrayEvent(XEvent*)), this, SLOT(checkXEvent(XEvent*)) );
  isRunning = true;
  TrayID = LX11::startSystemTray(0); //LSession::desktop()->screenNumber(this));
  if(TrayID!=0){
    XSelectInput(QX11Info::display(), TrayID, InputOutput); //make sure TrayID events get forwarded here
    XDamageQueryExtension( QX11Info::display(), &dmgEvent, &dmgError);
    //Now connect the session logout signal to the close function
    qDebug() << "System Tray Started Successfully";
    upTimer->start();
    //QTimer::singleShot(100, this, SLOT(initialTrayIconDetect()) );
  }else{
    disconnect(this);
  }
  isRunning = (TrayID!=0);*/
}

void LSysTray::stop(){
  if(!isRunning){ return; }
  stopping = true;
  upTimer->stop();
  //Now close down the system tray registry
  qDebug() << "Stop system Tray:" << this->type();
  //LX11::closeSystemTray(TrayID);
  //TrayID = 0;
  disconnect(this); //remove any signals/slots
  isRunning = false;
  //Release all the tray applications and delete the containers
  qDebug() << " - Remove tray applications";
  for(int i=(trayIcons.length()-1); i>=0; i--){
    trayIcons[i]->detachApp();
    TrayIcon *cont = trayIcons.takeAt(i);
      LI->removeWidget(cont);
      delete cont;
  }
  //Now let some other visual tray take over
  LSession::handle()->unregisterVisualTray(this->winId());
  qDebug() << "Done stopping system tray";
}

// ========================
//    PRIVATE FUNCTIONS
// ========================
/*void LSysTray::checkXEvent(XEvent *event){
  if(!isRunning){ return; }
  switch(event->type){
  // -------------------------
    case ClientMessage:
    	//Only check if the client is the system tray, otherwise ignore
    	if(event->xany.window == TrayID){
    	  //qDebug() << "SysTray: ClientMessage";
	    switch(event->xclient.data.l[1]){
		case SYSTEM_TRAY_REQUEST_DOCK:
		  addTrayIcon(event->xclient.data.l[2]); //Window ID
		  break;
		//case SYSTEM_TRAY_BEGIN_MESSAGE:
		  //Let the window manager handle the pop-up messages for now
		  //break;    	    
		//case SYSTEM_TRAY_CANCEL_MESSAGE:
		  //Let the window manager handle the pop-up messages for now
		  //break;
	    }
    	}
    	break;
    case SelectionClear:
    	if(event->xany.window == TrayID){
    	  //qDebug() << "SysTray: Selection Clear";
    	  this->stop(); //de-activate this system tray (release all embeds)
    	}
    	break;
    case DestroyNotify:
	//qDebug() << "SysTray: DestroyNotify";
        removeTrayIcon(event->xany.window); //Check for removing an icon
        break;
    
    case ConfigureNotify:
	for(int i=0; i<trayIcons.length(); i++){
	  if(event->xany.window==trayIcons[i]->appID()){
	    //qDebug() << "SysTray: Configure Event" << trayIcons[i]->appID();
	    trayIcons[i]->update(); //trigger a repaint event
	    break;
	  }
	}
    default:
	if(event->type == dmgEvent+XDamageNotify){
	  WId ID = reinterpret_cast<XDamageNotifyEvent*>(event)->drawable;	
	  //qDebug() << "SysTray: Damage Event";
	  for(int i=0; i<trayIcons.length(); i++){
	    if(ID==trayIcons[i]->appID()){ 
	      //qDebug() << "SysTray: Damage Event" << ID;
	      trayIcons[i]->update(); //trigger a repaint event
	      break;
	    }
	  }
        }

  }//end of switch over event type
}	

void LSysTray::closeAll(){
  //Actually close all the tray apps (not just unembed)
    //This is used when the desktop is shutting everything down
  for(int i=0; i<trayIcons.length(); i++){
    LX11::CloseWindow(trayIcons[i]->appID());
  }
  
}
*/
void LSysTray::checkAll(){
  if(!isRunning || stopping || checking){ return; } //Don't check if not running at the moment
  checking = true;
  //qDebug() << "System Tray: Check tray apps";
  bool listChanged = false;
  QList<WId> wins = LSession::handle()->currentTrayApps(this->winId());
  for(int i=0; i<trayIcons.length(); i++){
    int index = wins.indexOf(trayIcons[i]->appID());
    if(index < 0){
      //Tray Icon no longer exists: remove it
      //qDebug() << " - SysTray: Remove Icon";
      TrayIcon *cont = trayIcons.takeAt(i);
      LI->removeWidget(cont);
      delete cont;
      i--; //List size changed
      listChanged = true;
      //Re-adjust the maximum widget size to account for what is left
      if(this->layout()->direction()==QBoxLayout::LeftToRight){
        this->setMaximumSize( trayIcons.length()*this->height(), 10000);
      }else{
        this->setMaximumSize(10000, trayIcons.length()*this->width());
      }
    }else{
      //Tray Icon already exists
      //qDebug() << " - SysTray: Update Icon";
      //trayIcons[i]->update();
      wins.removeAt(index); //Already found - remove from the list
    }
  }
  //Now go through any remaining windows and add them
  for(int i=0; i<wins.length(); i++){
    //qDebug() << " - SysTray: Add Icon";
    TrayIcon *cont = new TrayIcon(this);
      LSession::processEvents();
      trayIcons << cont;
      LI->addWidget(cont);
      //qDebug() << " - Update tray layout";
      if(this->layout()->direction()==QBoxLayout::LeftToRight){
        cont->setSizeSquare(this->height()-2*frame->frameWidth()); //horizontal tray
	this->setMaximumSize( trayIcons.length()*this->height(), 10000);
      }else{
	cont->setSizeSquare(this->width()-2*frame->frameWidth()); //vertical tray
	this->setMaximumSize(10000, trayIcons.length()*this->width());
      }
      LSession::processEvents();
      //qDebug() << " - Attach tray app";
      cont->attachApp(wins[i]);
      if(cont->appID()==0){ 
	//could not attach window - remove the widget
	qDebug() << "Invalid Tray Container:"; 
	trayIcons.takeAt(trayIcons.length()-1); //Always at the end
	LI->removeWidget(cont);
	delete cont;
	continue;
      }else{
	listChanged = true;
      }
    LI->update(); //make sure there is no blank space in the layout
  }
  /*if(listChanged){
    //Icons got moved around: be sure to re-draw all of them to fix visuals
    for(int i=0; i<trayIcons.length(); i++){
      trayIcons[i]->update();
    }
  }*/
  //qDebug() << " - System Tray: check done";
  checking = false;
}

void LSysTray::UpdateTrayWindow(WId win){
  if(!isRunning || stopping || checking){ return; }
  for(int i=0; i<trayIcons.length(); i++){
    if(trayIcons[i]->appID()==win){
      //qDebug() << "System Tray: Update Window " << win;
      trayIcons[i]->update(); 
      break;
    }
  }	 
}

/*void LSysTray::initialTrayIconDetect(){
  // WARNING: This is still experimental and should be disabled by default!!
  QList<WId> wins = LX11::findOrphanTrayWindows();
  for(int i=0; i<wins.length(); i++){
    //addTrayIcon(wins[i]);
    qDebug() << "Initial Tray Window:" << wins[i] << LX11::WindowClass(wins[i]);
  }
}*/

/*void LSysTray::addTrayIcon(WId win){
  if(win == 0 || !isRunning){ return; }
  //qDebug() << "System Tray: Add Tray Icon:" << win;
  bool exists = false;
  for(int i=0; i<trayIcons.length(); i++){
    if(trayIcons[i]->appID() == win){ exists=true; break; }
  }
  if(!exists){
    //qDebug() << " - New Icon Window:" << win;
    TrayIcon *cont = new TrayIcon(this);
      QCoreApplication::processEvents();
      connect(cont, SIGNAL(AppClosed()), this, SLOT(trayAppClosed()) );
      connect(cont, SIGNAL(AppAttached()), this, SLOT(updateStatus()) );
      trayIcons << cont;
      LI->addWidget(cont);
      //qDebug() << " - Update tray layout";
      if(this->layout()->direction()==QBoxLayout::LeftToRight){
        cont->setSizeSquare(this->height()-2*frame->frameWidth()); //horizontal tray
	this->setMaximumSize( trayIcons.length()*this->height(), 10000);
      }else{
	cont->setSizeSquare(this->width()-2*frame->frameWidth()); //vertical tray
	this->setMaximumSize(10000, trayIcons.length()*this->width());
      }
      LSession::processEvents();
      //qDebug() << " - Attach tray app";
      cont->attachApp(win);
    LI->update(); //make sure there is no blank space
  }
}*/

/*void LSysTray::removeTrayIcon(WId win){
  //This function only runs when the tray app was closed externally
  if(win==0 || !isRunning){ return; }
  for(int i=0; i<trayIcons.length(); i++){
    if(trayIcons[i]->appID()==win){
      //qDebug() << " - Remove Icon Window:" << win;
      //Remove it from the layout and keep going
      TrayIcon *cont = trayIcons.takeAt(i);
      LI->removeWidget(cont);
      delete cont;
      i--; //make sure we don't miss an item when we continue
      QCoreApplication::processEvents();
    }
  }
  //Re-adjust the maximum widget size to account for what is left
  if(this->layout()->direction()==QBoxLayout::LeftToRight){
    this->setMaximumSize( trayIcons.length()*this->height(), 10000);
  }else{
    this->setMaximumSize(10000, trayIcons.length()*this->width());
  }
  LI->update(); //update the layout (no gaps)
  this->update(); //update the main widget appearance
}*/

/*void LSysTray::updateStatus(){
  qDebug() << "System Tray: Client Attached";
  LI->update(); //make sure there is no blank space
  //qDebug() << " - Items:" << trayIcons.length();
}

void LSysTray::trayAppClosed(){
  if(!isRunning){ return; }
  for(int i=0;  i<trayIcons.length(); i++){
    if(trayIcons[i]->appID() == 0){
      qDebug() << "System Tray: Removing icon";
      TrayIcon *cont = trayIcons.takeAt(i);
      LI->removeWidget(cont);
      delete cont;
      QCoreApplication::processEvents();
    }
  }
  //Re-adjust the maximum widget size
  if(this->layout()->direction()==QBoxLayout::LeftToRight){
    this->setMaximumSize( trayIcons.length()*this->height(), 10000);
  }else{
    this->setMaximumSize(10000, trayIcons.length()*this->width());
  }
  LI->update(); //update the layout (no gaps)
  this->update();	
}
*/
bgstack15