<commit_before>
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2011 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//


#include "SDLWindow.h"
#include "../avgconfigwrapper.h"

#include "Player.h"
#include "MouseEvent.h"
#include "KeyEvent.h"
#if defined(HAVE_XI2_1) || defined(HAVE_XI2_2) 
#include "XInputMTInputDevice.h" 
#endif

#include "../base/Exception.h"
#include "../base/Logger.h"
#include "../base/ScopeTimer.h"
#include "../base/OSHelper.h"
#include "../base/StringHelper.h"

#include "../graphics/GLContext.h"
#include "../graphics/GLContextManager.h"
#include "../graphics/Filterflip.h"
#include "../graphics/Filterfliprgb.h"

#ifdef WIN32
#undef WIN32_LEAN_AND_MEAN
#endif
#include <SDL2/SDL_syswm.h>

#include <iostream>

using namespace std;

namespace avg {

//vector<long> SDLWindow::s_KeyCodeTranslationTable(SDLK_LAST, key::KEY_UNKNOWN);

SDLWindow::SDLWindow(const DisplayParams& dp, GLConfig glConfig)
    : Window(dp.getWindowParams(0), dp.isFullscreen()),
      m_pSDLWindow(0),
      m_SDLGLContext(0),
      m_LastMousePos(IntPoint(-1, -1))
{
    initTranslationTable();

    const WindowParams& wp = dp.getWindowParams(0);

    IntPoint pos = getPos();
    if (pos.x == -1) {
        pos.x = SDL_WINDOWPOS_UNDEFINED;
        pos.y = SDL_WINDOWPOS_UNDEFINED;
    }

    unsigned int flags = SDL_WINDOW_OPENGL;
    if (dp.isFullscreen()) {
        flags |= SDL_WINDOW_FULLSCREEN;
    }
    if (!wp.m_bHasWindowFrame) {
        flags |= SDL_WINDOW_BORDERLESS;
    }

    switch (dp.getBPP()) {
        case 24:
            SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 24);
            break;
        case 16:
            SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
            SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
            SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
            SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);
            break;
        default:
            AVG_LOG_ERROR("Unsupported bpp " << dp.getBPP() <<
                    "in Window::init()");
            exit(-1);
    }
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    while (glConfig.m_MultiSampleSamples && !m_SDLGLContext) {
        if (glConfig.m_MultiSampleSamples > 1) {
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,
                    glConfig.m_MultiSampleSamples);
        } else {
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
        }
        m_pSDLWindow = SDL_CreateWindow("libavg", pos.x, pos.y, wp.m_Size.x, wp.m_Size.y,
            flags);
        m_SDLGLContext = SDL_GL_CreateContext(m_pSDLWindow);
        if (!m_SDLGLContext) {
            glConfig.m_MultiSampleSamples = GLContext::nextMultiSampleValue(
                    glConfig.m_MultiSampleSamples);
        }
    }

#ifndef __linux__
    glConfig.m_bUseDebugContext = false;
#endif

    if (!m_SDLGLContext) {
        throw Exception(AVG_ERR_UNSUPPORTED, string("Creating window failed: ")
                + SDL_GetError() + ". (size=" + toString(wp.m_Size) + ", bpp=" +
                toString(dp.getBPP()) + ").");
    }
    SDL_GL_MakeCurrent(m_pSDLWindow, m_SDLGLContext);
    SDL_SysWMinfo info;
    SDL_VERSION(&info.version);
    int rc = SDL_GetWindowWMInfo(m_pSDLWindow, &info);
    AVG_ASSERT(rc != -1);
    GLContext* pGLContext = GLContextManager::get()->createContext(glConfig, wp.m_Size,
            &info);
    setGLContext(pGLContext);
    pGLContext->logConfig();

/*
#if defined(HAVE_XI2_1) || defined(HAVE_XI2_2) 
    SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
    m_pXIMTInputDevice = 0;
#endif
*/
}

SDLWindow::~SDLWindow()
{
}

void SDLWindow::setTitle(const string& sTitle)
{
    SDL_SetWindowTitle(m_pSDLWindow, sTitle.c_str());
}

static ProfilingZoneID SwapBufferProfilingZone("Render - SDL swap buffers");

void SDLWindow::swapBuffers() const
{
    ScopeTimer timer(SwapBufferProfilingZone);
#ifdef __linux__    
    getGLContext()->swapBuffers();
#else
    SDL_GL_SwapBuffers();
#endif
    GLContext::checkError("swapBuffers()");
}

vector<EventPtr> SDLWindow::pollEvents()
{
    SDL_Event sdlEvent;
    vector<EventPtr> events;

    int numEvents = 0;<commit_msg>Fixed window cleanup.<commit_after>
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2011 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//


#include "SDLWindow.h"
#include "../avgconfigwrapper.h"

#include "Player.h"
#include "MouseEvent.h"
#include "KeyEvent.h"
#if defined(HAVE_XI2_1) || defined(HAVE_XI2_2) 
#include "XInputMTInputDevice.h" 
#endif

#include "../base/Exception.h"
#include "../base/Logger.h"
#include "../base/ScopeTimer.h"
#include "../base/OSHelper.h"
#include "../base/StringHelper.h"

#include "../graphics/GLContext.h"
#include "../graphics/GLContextManager.h"
#include "../graphics/Filterflip.h"
#include "../graphics/Filterfliprgb.h"

#ifdef WIN32
#undef WIN32_LEAN_AND_MEAN
#endif
#include <SDL2/SDL_syswm.h>

#include <iostream>

using namespace std;

namespace avg {

//vector<long> SDLWindow::s_KeyCodeTranslationTable(SDLK_LAST, key::KEY_UNKNOWN);

SDLWindow::SDLWindow(const DisplayParams& dp, GLConfig glConfig)
    : Window(dp.getWindowParams(0), dp.isFullscreen()),
      m_pSDLWindow(0),
      m_SDLGLContext(0),
      m_LastMousePos(IntPoint(-1, -1))
{
    initTranslationTable();

    const WindowParams& wp = dp.getWindowParams(0);

    IntPoint pos = getPos();
    if (pos.x == -1) {
        pos.x = SDL_WINDOWPOS_UNDEFINED;
        pos.y = SDL_WINDOWPOS_UNDEFINED;
    }

    unsigned int flags = SDL_WINDOW_OPENGL;
    if (dp.isFullscreen()) {
        flags |= SDL_WINDOW_FULLSCREEN;
    }
    if (!wp.m_bHasWindowFrame) {
        flags |= SDL_WINDOW_BORDERLESS;
    }

    switch (dp.getBPP()) {
        case 24:
            SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
            SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 24);
            break;
        case 16:
            SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
            SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
            SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
            SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);
            break;
        default:
            AVG_LOG_ERROR("Unsupported bpp " << dp.getBPP() <<
                    "in Window::init()");
            exit(-1);
    }
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    while (glConfig.m_MultiSampleSamples && !m_SDLGLContext) {
        if (glConfig.m_MultiSampleSamples > 1) {
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,
                    glConfig.m_MultiSampleSamples);
        } else {
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
        }
        m_pSDLWindow = SDL_CreateWindow("libavg", pos.x, pos.y, wp.m_Size.x, wp.m_Size.y,
            flags);
        m_SDLGLContext = SDL_GL_CreateContext(m_pSDLWindow);
        if (!m_SDLGLContext) {
            glConfig.m_MultiSampleSamples = GLContext::nextMultiSampleValue(
                    glConfig.m_MultiSampleSamples);
        }
    }

#ifndef __linux__
    glConfig.m_bUseDebugContext = false;
#endif

    if (!m_SDLGLContext) {
        throw Exception(AVG_ERR_UNSUPPORTED, string("Creating window failed: ")
                + SDL_GetError() + ". (size=" + toString(wp.m_Size) + ", bpp=" +
                toString(dp.getBPP()) + ").");
    }
    SDL_GL_MakeCurrent(m_pSDLWindow, m_SDLGLContext);
    SDL_SysWMinfo info;
    SDL_VERSION(&info.version);
    int rc = SDL_GetWindowWMInfo(m_pSDLWindow, &info);
    AVG_ASSERT(rc != -1);
    GLContext* pGLContext = GLContextManager::get()->createContext(glConfig, wp.m_Size,
            &info);
    setGLContext(pGLContext);
    pGLContext->logConfig();

/*
#if defined(HAVE_XI2_1) || defined(HAVE_XI2_2) 
    SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
    m_pXIMTInputDevice = 0;
#endif
*/
}

SDLWindow::~SDLWindow()
{
    if (m_pSDLWindow) {
        SDL_DestroyWindow(m_pSDLWindow);
    }
}

void SDLWindow::setTitle(const string& sTitle)
{
    SDL_SetWindowTitle(m_pSDLWindow, sTitle.c_str());
}

static ProfilingZoneID SwapBufferProfilingZone("Render - SDL swap buffers");

void SDLWindow::swapBuffers() const
{
    ScopeTimer timer(SwapBufferProfilingZone);
#ifdef __linux__    
    getGLContext()->swapBuffers();
#else
    SDL_GL_SwapBuffers();
#endif
    GLContext::checkError("swapBuffers()");
}

vector<EventPtr> SDLWindow::pollEvents()
{
    SDL_Event sdlEvent;
    vector<EventPtr> events;

    int numEvents = 0;
{'fraction_non_alphanumeric': 0.11585193557502119, 'fraction_numerical': 0.012809644909108034, 'mean_word_length': 3.1622892983143864, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1518644', 'n_tokens_mistral': 3851, 'n_tokens_neox': 3552, 'n_words': 732}