<commit_before>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of mhome.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <QApplication>
#include <QX11Info>

#include "homewindowmonitor.h"
#include "xtools/xatomcache.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>


QSharedPointer<HomeWindowMonitor> HomeWindowMonitor::windowMonitorInstance = QSharedPointer<HomeWindowMonitor>();

const HomeWindowMonitor *HomeWindowMonitor::instance()
{
    if (windowMonitorInstance.isNull()) {
        windowMonitorInstance = QSharedPointer<HomeWindowMonitor>(new HomeWindowMonitor());
    }

    return windowMonitorInstance.data();
}

HomeWindowMonitor::HomeWindowMonitor() :
        nonFullscreenApplicationWindowTypes(QSet<Atom>() << AtomCache::WindowTypeNotificationAtom <<
                                                            AtomCache::WindowTypeDialogAtom <<
                                                            AtomCache::WindowTypeMenuAtom),
        netClientListStacking(XInternAtom(QX11Info::display(), "_NET_CLIENT_LIST_STACKING", False))
{
}

HomeWindowMonitor::~HomeWindowMonitor()
{
}

bool HomeWindowMonitor::isOwnWindow(WId wid) const
{
    foreach (QWidget *window, QApplication::topLevelWidgets()) {
        if (window->winId() == wid) {
            return true;
        }
    }

    return false;
}

bool HomeWindowMonitor::handleXEvent(const XEvent& event)
{
    bool eventHandled = false;

    if (event.type == PropertyNotify && event.xproperty.atom == netClientListStacking && event.xproperty.window == DefaultRootWindow(QX11Info::display())) {
        int numWindowStackingOrderReceivers = receivers(SIGNAL(windowStackingOrderChanged(QList<WindowInfo>)));
        int numFullscreenWindowReceivers = receivers(SIGNAL(fullscreenWindowOnTopOfOwnWindow()));
        int numAnyWindowReceivers = receivers(SIGNAL(anyWindowOnTopOfOwnWindow(WindowInfo)));

        if (numWindowStackingOrderReceivers + numFullscreenWindowReceivers + numAnyWindowReceivers > 0) {
            QList<Window> windowOrder = windowStackingOrder();

            if (numWindowStackingOrderReceivers > 0) {
                QList<WindowInfo *> windowStackingList;
                foreach (Window wid, windowOrder) {
                    windowStackingList.append(WindowInfo::windowFor(wid));
                }

                emit windowStackingOrderChanged(windowStackingList);
            }

            if (numFullscreenWindowReceivers + numAnyWindowReceivers > 0) {
                if (!windowOrder.isEmpty()) {
                    QListIterator<Window> iter(windowOrder);
                    iter.toBack();
                    bool anyWindowSignalEmitted = false;
                    while (iter.hasPrevious()) {
                        WindowInfo *windowInfo = WindowInfo::windowFor(iter.previous());
                        if (isOwnWindow(windowInfo->window())) {
                            break;
                        }
                        if (numAnyWindowReceivers > 0 && !anyWindowSignalEmitted) {
                            emit anyWindowOnTopOfOwnWindow(windowInfo);
                            // signal is sent only once per each XEvent, mark signal sent
                            anyWindowSignalEmitted = true;
                            if (numFullscreenWindowReceivers < 1) {
                                // no listeners for fullscreenWindowOnTopOfOwnWindow() signal
                                break;
                            }
                        }
                        if (windowInfo->types().toSet().intersect(nonFullscreenApplicationWindowTypes).isEmpty()) {
                            emit fullscreenWindowOnTopOfOwnWindow();
                            break;
                        }
                    }
                }
            }
        }
        eventHandled = true;

        if (isOwnWindow(windowStackingOrder().last()))
            emit this->isHomeWindowOnTopChanged();
    }

    return eventHandled;<commit_msg>HomeWindowMonitor: fixed a bug with a signal<commit_after>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of mhome.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <QApplication>
#include <QX11Info>

#include "homewindowmonitor.h"
#include "xtools/xatomcache.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>


QSharedPointer<HomeWindowMonitor> HomeWindowMonitor::windowMonitorInstance = QSharedPointer<HomeWindowMonitor>();

const HomeWindowMonitor *HomeWindowMonitor::instance()
{
    if (windowMonitorInstance.isNull()) {
        windowMonitorInstance = QSharedPointer<HomeWindowMonitor>(new HomeWindowMonitor());
    }

    return windowMonitorInstance.data();
}

HomeWindowMonitor::HomeWindowMonitor() :
        nonFullscreenApplicationWindowTypes(QSet<Atom>() << AtomCache::WindowTypeNotificationAtom <<
                                                            AtomCache::WindowTypeDialogAtom <<
                                                            AtomCache::WindowTypeMenuAtom),
        netClientListStacking(XInternAtom(QX11Info::display(), "_NET_CLIENT_LIST_STACKING", False))
{
}

HomeWindowMonitor::~HomeWindowMonitor()
{
}

bool HomeWindowMonitor::isOwnWindow(WId wid) const
{
    foreach (QWidget *window, QApplication::topLevelWidgets()) {
        if (window->winId() == wid) {
            return true;
        }
    }

    return false;
}

bool HomeWindowMonitor::handleXEvent(const XEvent& event)
{
    bool eventHandled = false;

    if (event.type == PropertyNotify && event.xproperty.atom == netClientListStacking && event.xproperty.window == DefaultRootWindow(QX11Info::display())) {
        int numWindowStackingOrderReceivers = receivers(SIGNAL(windowStackingOrderChanged(QList<WindowInfo>)));
        int numFullscreenWindowReceivers = receivers(SIGNAL(fullscreenWindowOnTopOfOwnWindow()));
        int numAnyWindowReceivers = receivers(SIGNAL(anyWindowOnTopOfOwnWindow(WindowInfo)));

        if (numWindowStackingOrderReceivers + numFullscreenWindowReceivers + numAnyWindowReceivers > 0) {
            QList<Window> windowOrder = windowStackingOrder();

            if (numWindowStackingOrderReceivers > 0) {
                QList<WindowInfo *> windowStackingList;
                foreach (Window wid, windowOrder) {
                    windowStackingList.append(WindowInfo::windowFor(wid));
                }

                emit windowStackingOrderChanged(windowStackingList);
            }

            if (numFullscreenWindowReceivers + numAnyWindowReceivers > 0) {
                if (!windowOrder.isEmpty()) {
                    QListIterator<Window> iter(windowOrder);
                    iter.toBack();
                    bool anyWindowSignalEmitted = false;
                    while (iter.hasPrevious()) {
                        WindowInfo *windowInfo = WindowInfo::windowFor(iter.previous());
                        if (isOwnWindow(windowInfo->window())) {
                            break;
                        }
                        if (numAnyWindowReceivers > 0 && !anyWindowSignalEmitted) {
                            emit anyWindowOnTopOfOwnWindow(windowInfo);
                            // signal is sent only once per each XEvent, mark signal sent
                            anyWindowSignalEmitted = true;
                            if (numFullscreenWindowReceivers < 1) {
                                // no listeners for fullscreenWindowOnTopOfOwnWindow() signal
                                break;
                            }
                        }
                        if (windowInfo->types().toSet().intersect(nonFullscreenApplicationWindowTypes).isEmpty()) {
                            emit fullscreenWindowOnTopOfOwnWindow();
                            break;
                        }
                    }
                }
            }
        }
        eventHandled = true;
        emit this->isHomeWindowOnTopChanged();
    }

    return eventHandled;
{'fraction_non_alphanumeric': 0.12244461093867066, 'fraction_numerical': 0.005779728138713475, 'mean_word_length': 2.328820805130032, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '906793', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2245, 'n_words': 483}