Implemented entry into power saving state when application is in background,
[situare] / src / application.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QX11Info>
23 #include <QDebug>
24
25 #include "application.h"
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <X11/Xutil.h>
30
31 #ifdef Q_WS_HILDON
32     #define ACTIVE_APP_ATOM "_MB_CURRENT_APP_WINDOW"
33 #else
34     #define ACTIVE_APP_ATOM "_NET_ACTIVE_WINDOW"
35 #endif
36
37 Application::Application(int &argc, char **argv)
38     : QApplication(argc, argv),
39     m_topmost(false)
40 {
41     qDebug() << __PRETTY_FUNCTION__;
42 }
43
44 void Application::registerWindow(WId window)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48    m_windows.append(window);
49 }
50
51 void Application::unregisterWindow(WId window)
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     m_windows.removeOne(window);
56 }
57
58 WId Application::activeDesktopWindow(Display *display, WId rootWindow)
59 {
60     qDebug() << __PRETTY_FUNCTION__;
61
62     Atom realType;
63     int format, status;
64     unsigned long n, extra;
65     unsigned char *data = 0;
66     WId activeWindow;
67     Atom atom = XInternAtom(display, ACTIVE_APP_ATOM, false);
68
69     status = XGetWindowProperty(display, rootWindow, atom, 0L, 16L,
70                         0, XA_WINDOW, &realType, &format,
71                         &n, &extra, &data);
72     if (status == Success && realType == XA_WINDOW &&
73         format == 32 && n == 1 && data != NULL)
74         activeWindow = ((WId*)data)[0];
75     else
76         activeWindow = None;
77
78     if (data != 0)
79         XFree(data);
80
81     return activeWindow;
82 }
83
84 bool Application::x11EventFilter(XEvent *event)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     static Qt::HANDLE rootWindow = QX11Info::appRootWindow();
89     static Atom activeAppAtom = XInternAtom(QX11Info::display(),
90                                             ACTIVE_APP_ATOM, false);
91
92     if (event->type == PropertyNotify) {
93         XPropertyEvent *pevent = (XPropertyEvent*)event;
94         if (pevent->window == rootWindow) {
95             if (pevent->atom == activeAppAtom) {
96                 WId activeWindow = activeDesktopWindow(pevent->display,
97                                                        rootWindow);
98
99                 if(m_topmost != m_windows.contains(activeWindow)) {
100                     m_topmost = !m_topmost;
101                     qWarning() << "Application is topmost " << m_topmost;
102                     emit topmostChanged(!m_topmost);
103                 }
104             }
105         }
106     }
107     return false;
108 }