<commit_before>// Ewmh.cc for fluxbox
// Copyright (c) 2002-2003 <NAME> (fluxgen at user.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: Ewmh.cc,v 1.16 2003/04/15 00:14:24 fluxgen Exp $

#include "Ewmh.hh" 

#include "Screen.hh"
#include "Window.hh"
#include "fluxbox.hh"
#include "WinClient.hh"

#include <iostream>
#include <algorithm>
#include <new>
using namespace std;

Ewmh::Ewmh() {
    createAtoms();
    enableUpdate();
}

Ewmh::~Ewmh() {
    while (!m_windows.empty()) {
        XDestroyWindow(FbTk::App::instance()->display(), m_windows.back());
        m_windows.pop_back();
    }
}

void Ewmh::initForScreen(BScreen &screen) {
    Display *disp = FbTk::App::instance()->display();


    Window wincheck = XCreateSimpleWindow(disp,
                                          screen.getRootWindow(), 0, 0, 5, 5, 0, 0, 0);

    if (wincheck != None) {
        m_windows.push_back(wincheck);
		
        XChangeProperty(disp, screen.getRootWindow(), m_net_supporting_wm_check, XA_WINDOW, 32,
			PropModeReplace, (unsigned char *) &wincheck, 1);
        XChangeProperty(disp, wincheck, m_net_supporting_wm_check, XA_WINDOW, 32,
			PropModeReplace, (unsigned char *) &wincheck, 1);

        XChangeProperty(disp, wincheck, m_net_wm_name, XA_STRING, 8,
			PropModeReplace, (unsigned char *) "Fluxbox", strlen("Fluxbox"));
    }
	
    //set supported atoms
    Atom atomsupported[] = {
        // window properties
        m_net_wm_state,
        // states that we support:
        m_net_wm_state_sticky,
        m_net_wm_state_shaded,
		
        m_net_wm_desktop,
				
        // root properties
        m_net_client_list,
        m_net_number_of_desktops,
        m_net_current_desktop,
        m_net_active_window,
        m_net_close_window,
        m_net_moveresize_window,
        m_net_desktop_names,
        m_net_supporting_wm_check		
    };

    XChangeProperty(disp, screen.getRootWindow(), 
                    m_net_supported, XA_ATOM, 32,
                    PropModeReplace, 
                    (unsigned char *) &atomsupported, 
                    (sizeof atomsupported)/sizeof atomsupported[0]);

	
}

void Ewmh::setupWindow(FluxboxWindow &win) {

    Display *disp = FbTk::App::instance()->display();
    Atom ret_type;
    int fmt;
    unsigned long nitems, bytes_after;
    long *data = 0;
/*	
	if (XGetWindowProperty(disp, win.getClientWindow(), 
        m_net_wm_state, 0, 1, False, XA_CARDINAL, 
        &ret_type, &fmt, &nitems, &bytes_after, 
        (unsigned char **) &data) ==  Success && data) {
        flags = *data;
        setState(win, flags);
        XFree(data);
	}
*/
    if (XGetWindowProperty(disp, win.getClientWindow(), 
                           m_net_wm_desktop, 0, 1, False, XA_CARDINAL, 
                           &ret_type, &fmt, &nitems, &bytes_after, 
                           (unsigned char **) &data) ==  Success && data) {
        unsigned int desktop = static_cast<unsigned int>(*data);
        if (desktop == 0xFFFFFFFF && !win.isStuck())
            win.stick();
        else
            win.getScreen().sendToWorkspace(desktop, &win, false);

        XFree(data);
    }
}

void Ewmh::updateClientList(BScreen &screen) {
    size_t num=0;

    BScreen::Workspaces::const_iterator workspace_it = 
        screen.getWorkspacesList().begin();
    BScreen::Workspaces::const_iterator workspace_it_end = 
        screen.getWorkspacesList().end();
    for (; workspace_it != workspace_it_end; ++workspace_it) {
        Workspace::Windows::iterator win_it = 
            (*workspace_it)->getWindowList().begin();
        Workspace::Windows::iterator win_it_end = 
            (*workspace_it)->getWindowList().end();
        for (; win_it != win_it_end; ++win_it) {
            num += (*win_it)->numClients();
        }

    }
    //int num = getCurrentWorkspace()->getWindowList().size();
	
    Window *wl = new (nothrow) Window[num];
    if (wl == 0) {
        cerr<<"Fatal: Out of memory, can't allocate for Ewmh client list"<<endl;
        return;
    }
    //start the iterator from begining
    workspace_it = screen.getWorkspacesList().begin();
    int win=0;
    for (; workspace_it != workspace_it_end; ++workspace_it) {
	
        // Fill in array of window ID's
        Workspace::Windows::const_iterator it = 
            (*workspace_it)->getWindowList().begin();
        Workspace::Windows::const_iterator it_end = 
            (*workspace_it)->getWindowList().end();		
        for (; it != it_end; ++it) {
            if ((*it)->numClients() == 1)
                wl[win++] = (*it)->getClientWindow();
            else {
                // add every client in fluxboxwindow to list window list
                std::list<WinClient *>::iterator client_it = 
                    (*it)->clientList().begin();
                std::list<WinClient *>::iterator client_it_end = 
                    (*it)->clientList().end();
                for (; client_it != client_it_end; ++client_it)
                    wl[win++] = (*client_it)->window();
            }
        }
    }

    // plus iconified windows
    BScreen::Icons::const_iterator it = screen.getIconList().begin();
    BScreen::Icons::const_iterator it_end = screen.getIconList().end();		
    for (; it != it_end; ++it) {
        wl[win++] = (*it)->getClientWindow();
    }
    
    //number of windows to show in client list
    num = win;
    XChangeProperty(FbTk::App::instance()->display(), 
                    screen.getRootWindow(), 
                    m_net_client_list, 
                    XA_CARDINAL, 32,
                    PropModeReplace, (unsigned char *)wl, num);
	
    delete [] wl;
}

void Ewmh::updateWorkspaceNames(BScreen &screen) {
    XTextProperty text;
    const size_t number_of_desks = screen.getWorkspaceNames().size();
	
    char *names[number_of_desks];		
	
    for (size_t i = 0; i < number_of_desks; i++) {		
        names[i] = new char[screen.getWorkspaceNames()[i].size()];
        strcpy(names[i], screen.getWorkspaceNames()[i].c_str());
    }
	
    if (XStringListToTextProperty(names, number_of_desks, &text)) {
        XSetTextProperty(FbTk::App::instance()->display(), screen.getRootWindow(),<commit_msg>fixed client and icon count<commit_after>// Ewmh.cc for fluxbox
// Copyright (c) 2002-2003 <NAME> (fluxgen at user.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: Ewmh.cc,v 1.17 2003/04/15 12:22:06 fluxgen Exp $

#include "Ewmh.hh" 

#include "Screen.hh"
#include "Window.hh"
#include "fluxbox.hh"
#include "WinClient.hh"

#include <iostream>
#include <algorithm>
#include <new>
using namespace std;

Ewmh::Ewmh() {
    createAtoms();
    enableUpdate();
}

Ewmh::~Ewmh() {
    while (!m_windows.empty()) {
        XDestroyWindow(FbTk::App::instance()->display(), m_windows.back());
        m_windows.pop_back();
    }
}

void Ewmh::initForScreen(BScreen &screen) {
    Display *disp = FbTk::App::instance()->display();


    Window wincheck = XCreateSimpleWindow(disp,
                                          screen.getRootWindow(), 
                                          0, 0, 5, 5, 0, 0, 0);

    if (wincheck != None) {
        m_windows.push_back(wincheck);
		
        XChangeProperty(disp, screen.getRootWindow(), m_net_supporting_wm_check, XA_WINDOW, 32,
			PropModeReplace, (unsigned char *) &wincheck, 1);
        XChangeProperty(disp, wincheck, m_net_supporting_wm_check, XA_WINDOW, 32,
			PropModeReplace, (unsigned char *) &wincheck, 1);

        XChangeProperty(disp, wincheck, m_net_wm_name, XA_STRING, 8,
			PropModeReplace, (unsigned char *) "Fluxbox", strlen("Fluxbox"));
    }
	
    //set supported atoms
    Atom atomsupported[] = {
        // window properties
        m_net_wm_state,
        // states that we support:
        m_net_wm_state_sticky,
        m_net_wm_state_shaded,
		
        m_net_wm_desktop,
				
        // root properties
        m_net_client_list,
        m_net_number_of_desktops,
        m_net_current_desktop,
        m_net_active_window,
        m_net_close_window,
        m_net_moveresize_window,
        m_net_desktop_names,
        m_net_supporting_wm_check		
    };

    XChangeProperty(disp, screen.getRootWindow(), 
                    m_net_supported, XA_ATOM, 32,
                    PropModeReplace, 
                    (unsigned char *) &atomsupported, 
                    (sizeof atomsupported)/sizeof atomsupported[0]);

	
}

void Ewmh::setupWindow(FluxboxWindow &win) {

    Display *disp = FbTk::App::instance()->display();
    Atom ret_type;
    int fmt;
    unsigned long nitems, bytes_after;
    long *data = 0;
/*	
	if (XGetWindowProperty(disp, win.getClientWindow(), 
        m_net_wm_state, 0, 1, False, XA_CARDINAL, 
        &ret_type, &fmt, &nitems, &bytes_after, 
        (unsigned char **) &data) ==  Success && data) {
        flags = *data;
        setState(win, flags);
        XFree(data);
	}
*/
    if (XGetWindowProperty(disp, win.getClientWindow(), 
                           m_net_wm_desktop, 0, 1, False, XA_CARDINAL, 
                           &ret_type, &fmt, &nitems, &bytes_after, 
                           (unsigned char **) &data) ==  Success && data) {
        unsigned int desktop = static_cast<unsigned int>(*data);
        if (desktop == 0xFFFFFFFF && !win.isStuck())
            win.stick();
        else
            win.getScreen().sendToWorkspace(desktop, &win, false);

        XFree(data);
    }
}

void Ewmh::updateClientList(BScreen &screen) {
    size_t num=0;

    BScreen::Workspaces::const_iterator workspace_it = 
        screen.getWorkspacesList().begin();
    BScreen::Workspaces::const_iterator workspace_it_end = 
        screen.getWorkspacesList().end();
    for (; workspace_it != workspace_it_end; ++workspace_it) {
        Workspace::Windows::iterator win_it = 
            (*workspace_it)->getWindowList().begin();
        Workspace::Windows::iterator win_it_end = 
            (*workspace_it)->getWindowList().end();
        for (; win_it != win_it_end; ++win_it) {
            num += (*win_it)->numClients();
        }

    }
    // and count icons
    BScreen::Icons::const_iterator icon_it = screen.getIconList().begin();
    BScreen::Icons::const_iterator icon_it_end = screen.getIconList().end();		
    for (; icon_it != icon_it_end; ++icon_it) {
        num += (*icon_it)->numClients();
    }
	
    Window *wl = new (nothrow) Window[num];
    if (wl == 0) {
        cerr<<"Fatal: Out of memory, can't allocate for Ewmh client list"<<endl;
        return;
    }

    //start the iterator from begining
    workspace_it = screen.getWorkspacesList().begin();
    int win=0;
    for (; workspace_it != workspace_it_end; ++workspace_it) {
	
        // Fill in array of window ID's
        Workspace::Windows::const_iterator it = 
            (*workspace_it)->getWindowList().begin();
        Workspace::Windows::const_iterator it_end = 
            (*workspace_it)->getWindowList().end();		
        for (; it != it_end; ++it) {
            if ((*it)->numClients() == 1)
                wl[win++] = (*it)->getClientWindow();
            else {
                // add every client in fluxboxwindow to list window list
                std::list<WinClient *>::iterator client_it = 
                    (*it)->clientList().begin();
                std::list<WinClient *>::iterator client_it_end = 
                    (*it)->clientList().end();
                for (; client_it != client_it_end; ++client_it)
                    wl[win++] = (*client_it)->window();
            }
        }
    }

    // plus iconified windows
    icon_it = screen.getIconList().begin();
    for (; icon_it != icon_it_end; ++icon_it) {
        FluxboxWindow::ClientList::iterator client_it = (*icon_it)->clientList().begin();
        FluxboxWindow::ClientList::iterator client_it_end = (*icon_it)->clientList().end();
        for (; client_it != client_it_end; ++client_it)
            wl[win++] = (*client_it)->window();
    }
    
    //number of windows to show in client list
    num = win;
    XChangeProperty(FbTk::App::instance()->display(), 
                    screen.getRootWindow(), 
                    m_net_client_list, 
                    XA_CARDINAL, 32,
                    PropModeReplace, (unsigned char *)wl, num);
	
    delete [] wl;
}

void Ewmh::updateWorkspaceNames(BScreen &screen) {
    XTextProperty text;
    const size_t number_of_desks = screen.getWorkspaceNames().size();
	
    char *names[number_of_desks];		
	
    for (size_t i = 0; i < number_of_desks; i++) {		
        names[i] = new char[screen.getWorkspaceNames()[i].size()];
        strcpy(names[i], screen.getWorkspaceNames()[i].c_str());
    }
	
    if (XStringListToTextProperty(names, number_of_desks, &text)) {
        XSetTextProperty(FbTk::App::instance()->display(), screen.getRootWindow(),
{'fraction_non_alphanumeric': 0.12616067823980623, 'fraction_numerical': 0.007401426456735298, 'mean_word_length': 2.56940441882805, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1396041', 'n_tokens_mistral': 4780, 'n_tokens_neox': 4455, 'n_words': 1101}