<commit_before>
/*
 * Copyright (c) 2011-2012, <NAME>
 * Copyright (c) 2012, <NAME> <<EMAIL>>
 * Copyright (c) 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */

#include <QDir>
#include <QFileInfoList>
#include <QFileSystemWatcher>
#include <QRegExp>
#include <QX11Info>

#include "switchermodel.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>

// Define this if you'd like to see debug messages from the switcher
#ifdef DEBUG_SWITCHER
#define SWITCHER_DEBUG(things) qDebug() << Q_FUNC_INFO << things
#else
#define SWITCHER_DEBUG(things)
#endif

SwitcherModel::SwitcherModel(QObject *parent)
    : QObjectListModel(parent)
{
}

SwitcherModel::~SwitcherModel()
{
}

bool SwitcherModel::handleXEvent(const XEvent &event)
{
    if (event.type == PropertyNotify &&
        event.xproperty.window == DefaultRootWindow(QX11Info::display()) &&
        event.xproperty.atom == AtomCache::atom("_NET_CLIENT_LIST"))
    {
        updateWindowList();
        return true;
    }
    else if (event.type == ClientMessage &&
             event.xclient.message_type == AtomCache::atom("_NET_CLOSE_WINDOW"))
    {
        SWITCHER_DEBUG("Got close WindowInfo event for " << event.xclient.window);
        WindowInfo *wi = WindowInfo::windowFor(event.xclient.window);
        delete wi;
        return true;
    }

    return false;
}

void SwitcherModel::updateWindowList()
{
    SWITCHER_DEBUG("Updating window list");

    Display *dpy = QX11Info::display();
    Atom actualType;
    int actualFormat;
    unsigned long numWindowItems, bytesLeft;
    unsigned char *windowData = NULL;

    int result = XGetWindowProperty(dpy,
                                    DefaultRootWindow(dpy),
                                    AtomCache::atom("_NET_CLIENT_LIST"),
                                    0, 0x7fffffff,
                                    false, XA_WINDOW,
                                    &actualType,
                                    &actualFormat,
                                    &numWindowItems,
                                    &bytesLeft,
                                    &windowData);

    if (result != Success || windowData == None)
        return;

    SWITCHER_DEBUG("Read list of " << numWindowItems << " windows");
    QSet<WindowInfo *> oldWindowSet;
    QSet<WindowInfo *> newWindowSet;

    // XXX: we can only get a QList<QObject*>, we want a QList<T>
    QList<QObject *> *temporaryList = getList();
    oldWindowSet.reserve(temporaryList->size());
    for (int i = 0; i < temporaryList->count(); ++i)
        oldWindowSet.insert(static_cast<WindowInfo *>(temporaryList->at(i)));

    newWindowSet.reserve(numWindowItems);

    Window *wins = (Window *)windowData;

    for (unsigned int i = 0; i < numWindowItems; i++) {
        WindowInfo *wi = WindowInfo::windowFor(wins[i]);
        if (wi->visibleInSwitcher())
            newWindowSet.insert(wi);
    }

    XFree(windowData);

    QSet<WindowInfo *> closedWindows = oldWindowSet - newWindowSet;
    QSet<WindowInfo *> openedWindows = newWindowSet - oldWindowSet;

    foreach (WindowInfo *wi, closedWindows)
        delete wi;

    foreach (WindowInfo *wi, openedWindows)
        addItem(wi);

    emit itemCountChanged();
}<commit_msg>Revert "handle differencing between switcher content instead of doing full model resets"<commit_after>
/*
 * Copyright (c) 2011-2012, <NAME>
 * Copyright (c) 2012, <NAME> <<EMAIL>>
 * Copyright (c) 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */

#include <QDir>
#include <QFileInfoList>
#include <QFileSystemWatcher>
#include <QRegExp>
#include <QX11Info>

#include "switchermodel.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>

// Define this if you'd like to see debug messages from the switcher
#ifdef DEBUG_SWITCHER
#define SWITCHER_DEBUG(things) qDebug() << Q_FUNC_INFO << things
#else
#define SWITCHER_DEBUG(things)
#endif

SwitcherModel::SwitcherModel(QObject *parent)
    : QObjectListModel(parent)
{
}

SwitcherModel::~SwitcherModel()
{
}

bool SwitcherModel::handleXEvent(const XEvent &event)
{
    if (event.type == PropertyNotify &&
        event.xproperty.window == DefaultRootWindow(QX11Info::display()) &&
        event.xproperty.atom == AtomCache::atom("_NET_CLIENT_LIST"))
    {
        updateWindowList();
        return true;
    }
    else if (event.type == ClientMessage &&
             event.xclient.message_type == AtomCache::atom("_NET_CLOSE_WINDOW"))
    {
        SWITCHER_DEBUG("Got close WindowInfo event for " << event.xclient.window);
        WindowInfo *wi = WindowInfo::windowFor(event.xclient.window);
        delete wi;
        return true;
    }

    return false;
}

void SwitcherModel::updateWindowList()
{
    SWITCHER_DEBUG("Updating window list");

    Display *dpy = QX11Info::display();
    Atom actualType;
    int actualFormat;
    unsigned long numWindowItems, bytesLeft;
    unsigned char *windowData = NULL;

    int result = XGetWindowProperty(dpy,
                                    DefaultRootWindow(dpy),
                                    AtomCache::atom("_NET_CLIENT_LIST"),
                                    0, 0x7fffffff,
                                    false, XA_WINDOW,
                                    &actualType,
                                    &actualFormat,
                                    &numWindowItems,
                                    &bytesLeft,
                                    &windowData);

    if (result != Success || windowData == None)
        return;

    SWITCHER_DEBUG("Read list of " << numWindowItems << " windows");
    QList<WindowInfo*> *windowList = new QList<WindowInfo*>();
    Window *wins = (Window *)windowData;

    for (unsigned int i = 0; i < numWindowItems; i++) {
        WindowInfo *wi = WindowInfo::windowFor(wins[i]);
        if (wi->visibleInSwitcher())
            windowList->append(wi);
    }

    XFree(windowData);

    setList(windowList);
    emit itemCountChanged();
}
{'fraction_non_alphanumeric': 0.11625057436054526, 'fraction_numerical': 0.012406187777607597, 'mean_word_length': 2.5278227984873043, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6518360', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1962, 'n_words': 448}