<commit_before>// Copyright 2015-2020 <NAME>. All rights reserved.

#include "../Setup.h"

#include <stdexcept>
#include <X11/extensions/Xrandr.h> 
#include "NativeWindowLinux.hpp"
#include "EngineLinux.hpp"

#if OUZEL_SUPPORTS_X11
namespace
{
    constexpr long NET_WM_STATE_REMOVE = 0L;
    constexpr long NET_WM_STATE_ADD = 1L;
    constexpr long NET_WM_STATE_TOGGLE = 2L;
}
#endif

namespace ouzel::core::linux
{
    NativeWindow::NativeWindow(const std::function<void(const Event&)>& initCallback,
                               const Size2U& newSize,
                               bool newResizable,
                               bool newFullscreen,
                               bool newExclusiveFullscreen,
                               const std::string& newTitle):
        core::NativeWindow(initCallback,
                           newSize,
                           newResizable,
                           newFullscreen,
                           newExclusiveFullscreen,
                           newTitle,
                           true)
    {
#if OUZEL_SUPPORTS_X11
        const auto engineLinux = static_cast<Engine*>(engine);
        display = engineLinux->getDisplay();

        const auto screen = XDefaultScreenOfDisplay(display);
        screenNumber = XScreenNumberOfScreen(screen);

        const auto rootWindow = RootWindow(display, screenNumber);


        int monitorCount;
        XRRMonitorInfo* monitors = XRRGetMonitors(display, rootWindow, True, &monitorCount);
        std::unique_ptr<XRRMonitorInfo, decltype(&XRRFreeMonitors)> monitorsPtr(monitors, XRRFreeMonitors);

        XRRMonitorInfo* primaryMonitor = (monitorCount > 0) ? &monitors[0] : nullptr;
        for (int i = 0; i < monitorCount; ++i)
            if (monitors[i].primary)
            {
                primaryMonitor = &monitors[i];
                break;
            }

        constexpr std::uint32_t defaultWidth = 800U;
        constexpr std::uint32_t defaultHeight = 600U;

        if (size.v[0] <= 0.0F) size.v[0] = primaryMonitor ? static_cast<std::uint32_t>(primaryMonitor->width * 0.8F) : defaultWidth;
        if (size.v[1] <= 0.0F) size.v[1] = primaryMonitor ? static_cast<std::uint32_t>(primaryMonitor->height * 0.8F) : defaultHeight;

        resolution = size;

        const int x = primaryMonitor ? monitors[0].x + primaryMonitor->width / 2 - static_cast<int>(size.v[0] / 2) : 0;
        const int y = primaryMonitor ? monitors[0].y + primaryMonitor->height / 2 - static_cast<int>(size.v[1] / 2) : 0;

        XSetWindowAttributes attributes;
        attributes.background_pixel = XWhitePixel(display, screenNumber);
        attributes.border_pixel = 0;
        attributes.event_mask = FocusChangeMask | KeyPressMask | KeyRelease | ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;

        window = XCreateWindow(display, rootWindow, x, y,<commit_msg>Fix window position on Linux<commit_after>// Copyright 2015-2020 <NAME>. All rights reserved.

#include "../Setup.h"

#include <stdexcept>
#include <X11/extensions/Xrandr.h> 
#include "NativeWindowLinux.hpp"
#include "EngineLinux.hpp"

#if OUZEL_SUPPORTS_X11
namespace
{
    constexpr long NET_WM_STATE_REMOVE = 0L;
    constexpr long NET_WM_STATE_ADD = 1L;
    constexpr long NET_WM_STATE_TOGGLE = 2L;
}
#endif

namespace ouzel::core::linux
{
    NativeWindow::NativeWindow(const std::function<void(const Event&)>& initCallback,
                               const Size2U& newSize,
                               bool newResizable,
                               bool newFullscreen,
                               bool newExclusiveFullscreen,
                               const std::string& newTitle):
        core::NativeWindow(initCallback,
                           newSize,
                           newResizable,
                           newFullscreen,
                           newExclusiveFullscreen,
                           newTitle,
                           true)
    {
#if OUZEL_SUPPORTS_X11
        const auto engineLinux = static_cast<Engine*>(engine);
        display = engineLinux->getDisplay();

        const auto screen = XDefaultScreenOfDisplay(display);
        screenNumber = XScreenNumberOfScreen(screen);

        const auto rootWindow = RootWindow(display, screenNumber);


        int monitorCount;
        std::unique_ptr<XRRMonitorInfo, decltype(&XRRFreeMonitors)> monitors(XRRGetMonitors(display, rootWindow, True, &monitorCount), XRRFreeMonitors);

        XRRMonitorInfo* primaryMonitor = (monitorCount > 0) ? &monitors.get()[0] : nullptr;
        for (int i = 0; i < monitorCount; ++i)
            if (monitors.get()[i].primary)
            {
                primaryMonitor = &monitors.get()[i];
                break;
            }

        constexpr std::uint32_t defaultWidth = 800U;
        constexpr std::uint32_t defaultHeight = 600U;

        if (size.v[0] <= 0.0F) size.v[0] = primaryMonitor ? static_cast<std::uint32_t>(primaryMonitor->width * 0.8F) : defaultWidth;
        if (size.v[1] <= 0.0F) size.v[1] = primaryMonitor ? static_cast<std::uint32_t>(primaryMonitor->height * 0.8F) : defaultHeight;

        resolution = size;

        const int x = primaryMonitor ? primaryMonitor->x + primaryMonitor->width / 2 - static_cast<int>(size.v[0] / 2) : 0;
        const int y = primaryMonitor ? primaryMonitor->y + primaryMonitor->height / 2 - static_cast<int>(size.v[1] / 2) : 0;

        XSetWindowAttributes attributes;
        attributes.background_pixel = XWhitePixel(display, screenNumber);
        attributes.border_pixel = 0;
        attributes.event_mask = FocusChangeMask | KeyPressMask | KeyRelease | ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;

        window = XCreateWindow(display, rootWindow, x, y,
{'fraction_non_alphanumeric': 0.0997087544971732, 'fraction_numerical': 0.019530580777796812, 'mean_word_length': 2.3283922462941846, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4426471', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1631, 'n_words': 348}