parent
19eb56997d
commit
f548825faf
@ -0,0 +1 @@
|
||||
Color = {}
|
@ -0,0 +1 @@
|
||||
Point = {}
|
@ -0,0 +1 @@
|
||||
Rect = {}
|
@ -0,0 +1 @@
|
||||
Size = {}
|
@ -0,0 +1,16 @@
|
||||
OTClient = { }
|
||||
|
||||
-- TODO: load and save configurations
|
||||
function OTClient.init()
|
||||
g_window.move({ x=220, y=220 })
|
||||
g_window.resize({ width=800, height=600 })
|
||||
g_window.setTitle('OTClient')
|
||||
g_window.setIcon('otcicon.png')
|
||||
|
||||
addEvent(g_window.show)
|
||||
return true
|
||||
end
|
||||
|
||||
function OTClient.terminate()
|
||||
g_window.hide()
|
||||
end
|
@ -0,0 +1,207 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "application.h"
|
||||
#include <framework/core/clock.h>
|
||||
#include <framework/core/resourcemanager.h>
|
||||
#include <framework/core/modulemanager.h>
|
||||
#include <framework/core/eventdispatcher.h>
|
||||
#include <framework/core/configmanager.h>
|
||||
#include <framework/net/connection.h>
|
||||
#include <framework/platform/platformwindow.h>
|
||||
#include <framework/ui/uimanager.h>
|
||||
#include <framework/ui/uiwidget.h>
|
||||
#include <framework/graphics/graphics.h>
|
||||
#include <framework/luascript/luainterface.h>
|
||||
|
||||
void exitSignalHandler(int sig)
|
||||
{
|
||||
static bool signaled = false;
|
||||
switch(sig) {
|
||||
case SIGTERM:
|
||||
case SIGINT:
|
||||
if(!signaled) {
|
||||
signaled = true;
|
||||
g_dispatcher.addEvent(std::bind(&Application::close, &g_app));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Application::init(const std::string& appName, const std::vector<std::string>& args)
|
||||
{
|
||||
logInfo("Starting application...");
|
||||
|
||||
m_pollCycleDelay = POLL_CYCLE_DELAY;
|
||||
m_appName = appName;
|
||||
|
||||
// capture exit signals
|
||||
signal(SIGTERM, exitSignalHandler);
|
||||
signal(SIGINT, exitSignalHandler);
|
||||
|
||||
// initialize lua
|
||||
g_lua.init();
|
||||
registerLuaFunctions();
|
||||
|
||||
// initialize resources
|
||||
g_resources.init(args[0].c_str());
|
||||
|
||||
// loads user configuration
|
||||
if(!g_configs.load("config.otml"))
|
||||
logInfo("Using default configurations.");
|
||||
|
||||
// initialize the ui
|
||||
g_ui.init();
|
||||
|
||||
// setup platform window
|
||||
g_window.init();
|
||||
g_window.setOnResize(std::bind(&Application::resize, this, _1));
|
||||
g_window.setOnInputEvent(std::bind(&Application::inputEvent, this, _1));
|
||||
g_window.setOnClose(std::bind(&Application::close, this));
|
||||
|
||||
// initialize graphics
|
||||
g_graphics.init();
|
||||
|
||||
// fire first resize
|
||||
resize(g_window.getSize());
|
||||
|
||||
// auto load lua modules
|
||||
g_modules.discoverAndLoadModules();
|
||||
}
|
||||
|
||||
|
||||
void Application::terminate()
|
||||
{
|
||||
// hide the window because there is no render anymore
|
||||
g_window.hide();
|
||||
|
||||
// run modules unload events
|
||||
g_modules.unloadModules();
|
||||
|
||||
// release remaining lua object references
|
||||
g_lua.collectGarbage();
|
||||
|
||||
// poll remaining events
|
||||
poll();
|
||||
|
||||
// terminate ui
|
||||
g_ui.terminate();
|
||||
|
||||
// terminate network
|
||||
Connection::terminate();
|
||||
|
||||
// flush remaining dispatcher events
|
||||
g_dispatcher.flush();
|
||||
|
||||
// terminate graphics
|
||||
g_graphics.terminate();
|
||||
|
||||
// save configurations
|
||||
g_configs.save();
|
||||
|
||||
// release resources
|
||||
g_resources.terminate();
|
||||
|
||||
// terminate script environment
|
||||
g_lua.terminate();
|
||||
|
||||
// release platform window
|
||||
g_window.terminate();
|
||||
|
||||
logInfo("Application ended successfully.");
|
||||
}
|
||||
|
||||
void Application::run()
|
||||
{
|
||||
ticks_t lastPollTicks = g_clock.updateTicks();
|
||||
m_stopping = false;
|
||||
m_running = true;
|
||||
|
||||
// run the first poll
|
||||
poll();
|
||||
|
||||
if(g_ui.getRootWidget()->getChildCount() == 0) {
|
||||
logError("There is no root widgets to display, the app will close");
|
||||
m_stopping = true;
|
||||
}
|
||||
|
||||
while(!m_stopping) {
|
||||
g_clock.updateTicks();
|
||||
|
||||
// poll events every POLL_CYCLE_DELAY
|
||||
// this delay exists to avoid massive polling thus increasing framerate
|
||||
if(g_clock.ticksElapsed(lastPollTicks) >= m_pollCycleDelay) {
|
||||
poll();
|
||||
lastPollTicks = g_clock.ticks();
|
||||
}
|
||||
|
||||
if(g_window.isVisible()) {
|
||||
g_graphics.beginRender();
|
||||
render();
|
||||
g_graphics.endRender();
|
||||
|
||||
// update screen pixels
|
||||
g_window.swapBuffers();
|
||||
} else {
|
||||
// sleeps until next poll to avoid massive cpu usage
|
||||
g_clock.sleep(POLL_CYCLE_DELAY+1);
|
||||
}
|
||||
}
|
||||
|
||||
m_stopping = false;
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
void Application::exit()
|
||||
{
|
||||
logInfo("Exiting application..");
|
||||
m_stopping = true;
|
||||
}
|
||||
|
||||
void Application::poll()
|
||||
{
|
||||
// poll input events
|
||||
g_window.poll();
|
||||
|
||||
// poll network events
|
||||
Connection::poll();
|
||||
|
||||
// poll application genareted events
|
||||
g_dispatcher.poll();
|
||||
}
|
||||
|
||||
void Application::render()
|
||||
{
|
||||
// everything is rendered by UI components
|
||||
g_ui.render();
|
||||
}
|
||||
|
||||
void Application::resize(const Size& size)
|
||||
{
|
||||
g_graphics.resize(size);
|
||||
g_ui.resize(size);
|
||||
}
|
||||
|
||||
void Application::inputEvent(const InputEvent& event)
|
||||
{
|
||||
g_ui.inputEvent(event);
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef APPLICATION_H
|
||||
#define APPLICATION_H
|
||||
|
||||
#include <framework/core/inputevent.h>
|
||||
|
||||
class Application
|
||||
{
|
||||
enum {
|
||||
POLL_CYCLE_DELAY = 10
|
||||
};
|
||||
|
||||
public:
|
||||
virtual void init(const std::string& appName, const std::vector<std::string>& args);
|
||||
virtual void registerLuaFunctions();
|
||||
virtual void terminate();
|
||||
virtual void run();
|
||||
virtual void exit();
|
||||
virtual void poll();
|
||||
virtual void close() { exit(); }
|
||||
|
||||
void setPollCycleDelay(int delay) { m_pollCycleDelay = delay; }
|
||||
|
||||
bool isRunning() { return m_running; }
|
||||
bool isStopping() { return m_stopping; }
|
||||
std::string getAppName() { return m_appName; }
|
||||
|
||||
protected:
|
||||
virtual void render();
|
||||
virtual void resize(const Size& size);
|
||||
virtual void inputEvent(const InputEvent& event);
|
||||
|
||||
private:
|
||||
std::string m_appName;
|
||||
int m_pollCycleDelay;
|
||||
Boolean<false> m_running;
|
||||
Boolean<false> m_stopping;
|
||||
};
|
||||
|
||||
extern Application& g_app;
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GLUTIL_H
|
||||
#define GLUTIL_H
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
|
||||
#ifndef OPENGL_ES2
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#else
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HANDLE_EXCEPTIONS
|
||||
|
||||
#include <csignal>
|
||||
#include <execinfo.h>
|
||||
|
||||
#define MAX_BACKTRACE_DEPTH 128
|
||||
#define DEMANGLE_BACKTRACE_SYMBOLS
|
||||
|
||||
void crashHandler(int signum, siginfo_t* info, void* secret)
|
||||
{
|
||||
logError("Application crashed");
|
||||
|
||||
ucontext_t context = *(ucontext_t*)secret;
|
||||
time_t tnow;
|
||||
char fileName[128];
|
||||
time(&tnow);
|
||||
tm *ts = localtime(&tnow);
|
||||
strftime(fileName, 128, (x11.appName + "-crash_-%d-%m-%Y_%H:%M:%S.txt").c_str(), ts);
|
||||
|
||||
std::stringstream ss;
|
||||
ss.flags(std::ios::hex | std::ios::showbase);
|
||||
#if __WORDSIZE == 32
|
||||
ss <<
|
||||
ss << " at eip = " << context.uc_mcontext.gregs[REG_EIP] << std::endl;
|
||||
ss << " eax = " << context.uc_mcontext.gregs[REG_EAX] << std::endl;
|
||||
ss << " ebx = " << context.uc_mcontext.gregs[REG_EBX] << std::endl;
|
||||
ss << " ecx = " << context.uc_mcontext.gregs[REG_ECX] << std::endl;
|
||||
ss << " edx = " << context.uc_mcontext.gregs[REG_EDX] << std::endl;
|
||||
ss << " esi = " << context.uc_mcontext.gregs[REG_ESI] << std::endl;
|
||||
ss << " edi = " << context.uc_mcontext.gregs[REG_EDI] << std::endl;
|
||||
ss << " ebp = " << context.uc_mcontext.gregs[REG_EBP] << std::endl;
|
||||
ss << " esp = " << context.uc_mcontext.gregs[REG_ESP] << std::endl;
|
||||
ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
|
||||
#else // 64-bit
|
||||
ss << " at rip = " << context.uc_mcontext.gregs[REG_RIP] << std::endl;
|
||||
ss << " rax = " << context.uc_mcontext.gregs[REG_RAX] << std::endl;
|
||||
ss << " rbx = " << context.uc_mcontext.gregs[REG_RBX] << std::endl;
|
||||
ss << " rcx = " << context.uc_mcontext.gregs[REG_RCX] << std::endl;
|
||||
ss << " rdx = " << context.uc_mcontext.gregs[REG_RDX] << std::endl;
|
||||
ss << " rsi = " << context.uc_mcontext.gregs[REG_RSI] << std::endl;
|
||||
ss << " rdi = " << context.uc_mcontext.gregs[REG_RDI] << std::endl;
|
||||
ss << " rbp = " << context.uc_mcontext.gregs[REG_RBP] << std::endl;
|
||||
ss << " rsp = " << context.uc_mcontext.gregs[REG_RSP] << std::endl;
|
||||
ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
|
||||
#endif
|
||||
ss << std::endl;
|
||||
ss.flags(std::ios::dec);
|
||||
ss << " backtrace:" << std::endl;
|
||||
|
||||
void* buffer[MAX_BACKTRACE_DEPTH];
|
||||
int numLevels = backtrace(buffer, MAX_BACKTRACE_DEPTH);
|
||||
char **tracebackBuffer = backtrace_symbols(buffer, numLevels);
|
||||
if(tracebackBuffer) {
|
||||
for(int i = 2; i < numLevels; i++) {
|
||||
std::string line = tracebackBuffer[i];
|
||||
if(line.find("__libc_start_main") != std::string::npos)
|
||||
break;
|
||||
#ifdef DEMANGLE_BACKTRACE_SYMBOLS
|
||||
std::size_t demanglePos = line.find("(_Z");
|
||||
if(demanglePos != std::string::npos) {
|
||||
demanglePos++;
|
||||
int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
|
||||
std::string funcName = line.substr(demanglePos, len);
|
||||
line.replace(demanglePos, len, Fw::demangleName(funcName.c_str()));
|
||||
}
|
||||
#endif
|
||||
ss << " " << i-1 << ": " << line << std::endl;
|
||||
}
|
||||
free(tracebackBuffer);
|
||||
}
|
||||
|
||||
logInfo(ss.str());
|
||||
|
||||
std::ofstream out(fileName);
|
||||
out << ss.str();
|
||||
out.close();
|
||||
logInfo("Crash report saved to file ", fileName);
|
||||
|
||||
signal(SIGILL, SIG_DFL);
|
||||
signal(SIGSEGV, SIG_DFL);
|
||||
signal(SIGFPE, SIG_DFL);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef HANDLE_EXCEPTIONS
|
||||
struct sigaction sa;
|
||||
sa.sa_sigaction = &crashHandler;
|
||||
sigemptyset (&sa.sa_mask);
|
||||
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
||||
|
||||
sigaction(SIGILL, &sa, NULL); // illegal instruction
|
||||
sigaction(SIGSEGV, &sa, NULL); // segmentation fault
|
||||
sigaction(SIGFPE, &sa, NULL); // floating-point exception
|
||||
#endif
|
@ -1,86 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef PLATFORM_H
|
||||
#define PLATFORM_H
|
||||
|
||||
#include <framework/global.h>
|
||||
|
||||
class PlatformListener;
|
||||
|
||||
class Platform
|
||||
{
|
||||
public:
|
||||
void init(PlatformListener* platformListener, const char* appName);
|
||||
void terminate();
|
||||
|
||||
/// Poll platform input/window events
|
||||
void poll();
|
||||
|
||||
/// Sleep in current thread
|
||||
void sleep(ulong ms);
|
||||
|
||||
bool createWindow(int x, int y, int width, int height, int minWidth, int minHeight, bool maximized);
|
||||
void setWindowIcon(const std::string& pngIcon);
|
||||
void destroyWindow();
|
||||
void showWindow();
|
||||
void hideWindow();
|
||||
void setWindowTitle(const char* title);
|
||||
bool isWindowFocused();
|
||||
bool isWindowVisible();
|
||||
int getWindowX();
|
||||
int getWindowY();
|
||||
int getWindowWidth();
|
||||
int getWindowHeight();
|
||||
bool isWindowMaximized();
|
||||
|
||||
int getDisplayHeight();
|
||||
int getDisplayWidth();
|
||||
|
||||
/// Get GL extension function address
|
||||
void* getExtensionProcAddress(const char* ext);
|
||||
/// Check if GLX/WGL extension is supported
|
||||
bool isExtensionSupported(const char* ext);
|
||||
|
||||
/// Get text from Ctrl+c
|
||||
const char* getClipboardText();
|
||||
/// Set text for Ctrl+v
|
||||
void setClipboardText(const char* text);
|
||||
|
||||
void hideMouseCursor();
|
||||
void showMouseCursor();
|
||||
Point getMouseCursorPos();
|
||||
|
||||
/// Enable or disable vertical synchronization
|
||||
void setVerticalSync(bool enable);
|
||||
/// Swap GL buffers
|
||||
void swapBuffers();
|
||||
|
||||
/// Get the app user directory, the place to save files configurations files
|
||||
std::string getAppUserDir();
|
||||
|
||||
void displayFatalError(const std::string& message);
|
||||
};
|
||||
|
||||
extern Platform g_platform;
|
||||
|
||||
#endif
|
@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef INPUTEVENT_H
|
||||
#define INPUTEVENT_H
|
||||
|
||||
#include <framework/global.h>
|
||||
|
||||
enum PlatformEventType {
|
||||
EventNone = 0,
|
||||
EventMouseAction = 1,
|
||||
EventKeyboardAction = 2,
|
||||
EventDown = 4,
|
||||
EventUp = 8,
|
||||
EventMouseWheel = 16,
|
||||
EventMouseLeftButton = 32,
|
||||
EventMouseRightButton = 64,
|
||||
EventMouseMidButton = 128,
|
||||
EventKeyDown = EventKeyboardAction | EventDown,
|
||||
EventKeyUp = EventKeyboardAction | EventUp,
|
||||
EventMouseMove = EventMouseAction | 512,
|
||||
EventMouseLeftButtonDown = EventMouseAction | EventMouseLeftButton | EventDown,
|
||||
EventMouseLeftButtonUp = EventMouseAction | EventMouseLeftButton | EventUp,
|
||||
EventMouseMiddleButtonDown = EventMouseAction | EventMouseMidButton | EventDown,
|
||||
EventMouseMiddleButtonUp = EventMouseAction | EventMouseMidButton | EventUp,
|
||||
EventMouseRightButtonDown = EventMouseAction | EventMouseRightButton | EventDown,
|
||||
EventMouseRightButtonUp = EventMouseAction | EventMouseRightButton | EventUp,
|
||||
EventMouseWheelUp = EventMouseAction | EventMouseWheel | EventUp,
|
||||
EventMouseWheelDown = EventMouseAction | EventMouseWheel | EventDown
|
||||
};
|
||||
|
||||
//TODO: rework platform events, make more compatible with UI events
|
||||
struct PlatformEvent {
|
||||
int type;
|
||||
Point mousePos;
|
||||
Point mouseMoved;
|
||||
char keychar;
|
||||
Fw::Key keycode;
|
||||
bool ctrl;
|
||||
bool shift;
|
||||
bool alt;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef PLATFORMWINDOW_H
|
||||
#define PLATFORMWINDOW_H
|
||||
|
||||
#include <framework/global.h>
|
||||
#include <framework/core/inputevent.h>
|
||||
|
||||
class PlatformWindow
|
||||
{
|
||||
typedef std::function<void(const Size&)> OnResizeCallback;
|
||||
typedef std::function<void(const InputEvent&)> OnInputEventCallback;
|
||||
|
||||
public:
|
||||
virtual void init() = 0;
|
||||
virtual void terminate() = 0;
|
||||
|
||||
virtual void move(const Point& pos) = 0;
|
||||
virtual void resize(const Size& size) = 0;
|
||||
virtual void show() = 0;
|
||||
virtual void hide() = 0;
|
||||
virtual void maximize() = 0;
|
||||
virtual void poll() = 0;
|
||||
virtual void swapBuffers() = 0;
|
||||
virtual void showMouse() = 0;
|
||||
virtual void hideMouse() = 0;
|
||||
|
||||
virtual void setTitle(const std::string& title) = 0;
|
||||
virtual void setMinimumSize(const Size& minimumSize) = 0;
|
||||
virtual void setFullscreen(bool fullscreen) = 0;
|
||||
virtual void setVerticalSync(bool enable) = 0;
|
||||
virtual void setIcon(const std::string& iconFile) = 0;
|
||||
virtual void setClipboardText(const std::string& text) = 0;
|
||||
|
||||
virtual Size getDisplaySize() = 0;
|
||||
virtual std::string getClipboardText() = 0;
|
||||
|
||||
int getDisplayWidth() { return getDisplaySize().width(); }
|
||||
int getDisplayHeight() { return getDisplaySize().width(); }
|
||||
|
||||
Size getSize() { return m_size; }
|
||||
int getWidth() { return m_size.width(); }
|
||||
int getHeight() { return m_size.height(); }
|
||||
Point getPos() { return m_pos; }
|
||||
int getX() { return m_pos.x; }
|
||||
int getY() { return m_pos.y; }
|
||||
Point getMousePos() { return m_inputEvent.mousePos; }
|
||||
int getKeyboardModifiers() { return m_inputEvent.keyboardModifiers; }
|
||||
|
||||
bool isVisible() { return m_visible; }
|
||||
bool isFullscreen() { return m_fullscreen; }
|
||||
bool hasFocus() { return m_focused; }
|
||||
|
||||
void setOnClose(const SimpleCallback& onClose) { m_onClose = onClose; }
|
||||
void setOnResize(const OnResizeCallback& onResize) { m_onResize = onResize; }
|
||||
void setOnInputEvent(const OnInputEventCallback& onInputEvent) { m_onInputEvent = onInputEvent; }
|
||||
|
||||
protected:
|
||||
Size m_size;
|
||||
Point m_pos;
|
||||
InputEvent m_inputEvent;
|
||||
|
||||
Boolean<false> m_created;
|
||||
Boolean<false> m_visible;
|
||||
Boolean<false> m_focused;
|
||||
Boolean<false> m_fullscreen;
|
||||
|
||||
SimpleCallback m_onClose;
|
||||
OnResizeCallback m_onResize;
|
||||
OnInputEventCallback m_onInputEvent;
|
||||
};
|
||||
|
||||
extern PlatformWindow& g_window;
|
||||
|
||||
#endif
|
@ -1,989 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "platform.h"
|
||||
#include "platformlistener.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <physfs.h>
|
||||
|
||||
#include <GL/glx.h>
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xatom.h>
|
||||
|
||||
#include <framework/thirdparty/apngloader.h>
|
||||
#include <framework/core/resourcemanager.h>
|
||||
|
||||
struct X11PlatformPrivate {
|
||||
Display *display;
|
||||
XVisualInfo *visual;
|
||||
GLXContext glxContext;
|
||||
XIM xim;
|
||||
XIC xic;
|
||||
Colormap colormap;
|
||||
Window window;
|
||||
Cursor cursor;
|
||||
Atom atomDeleteWindow;
|
||||
Atom atomClipboard;
|
||||
Atom atomTargets;
|
||||
Atom atomText;
|
||||
Atom atomCompoundText;
|
||||
Atom atomUTF8String;
|
||||
Atom atomWindowState;
|
||||
Atom atomWindowMaximizedVert;
|
||||
Atom atomWindowMaximizedHorz;
|
||||
bool visible;
|
||||
bool focused;
|
||||
bool maximizeOnFirstShow;
|
||||
std::string appName;
|
||||
int width;
|
||||
int height;
|
||||
int x;
|
||||
int y;
|
||||
int lastTicks;
|
||||
std::string clipboardText;
|
||||
std::map<int, Fw::Key> keyMap;
|
||||
PlatformListener* listener;
|
||||
PlatformEvent platformEvent;
|
||||
} x11;
|
||||
|
||||
Platform g_platform;
|
||||
|
||||
#ifdef HANDLE_EXCEPTIONS
|
||||
|
||||
#include <csignal>
|
||||
#include <execinfo.h>
|
||||
|
||||
#define MAX_BACKTRACE_DEPTH 128
|
||||
#define DEMANGLE_BACKTRACE_SYMBOLS
|
||||
|
||||
void crashHandler(int signum, siginfo_t* info, void* secret)
|
||||
{
|
||||
logError("Application crashed");
|
||||
|
||||
ucontext_t context = *(ucontext_t*)secret;
|
||||
time_t tnow;
|
||||
char fileName[128];
|
||||
time(&tnow);
|
||||
tm *ts = localtime(&tnow);
|
||||
strftime(fileName, 128, (x11.appName + "-crash_-%d-%m-%Y_%H:%M:%S.txt").c_str(), ts);
|
||||
|
||||
std::stringstream ss;
|
||||
ss.flags(std::ios::hex | std::ios::showbase);
|
||||
#if __WORDSIZE == 32
|
||||
ss <<
|
||||
ss << " at eip = " << context.uc_mcontext.gregs[REG_EIP] << std::endl;
|
||||
ss << " eax = " << context.uc_mcontext.gregs[REG_EAX] << std::endl;
|
||||
ss << " ebx = " << context.uc_mcontext.gregs[REG_EBX] << std::endl;
|
||||
ss << " ecx = " << context.uc_mcontext.gregs[REG_ECX] << std::endl;
|
||||
ss << " edx = " << context.uc_mcontext.gregs[REG_EDX] << std::endl;
|
||||
ss << " esi = " << context.uc_mcontext.gregs[REG_ESI] << std::endl;
|
||||
ss << " edi = " << context.uc_mcontext.gregs[REG_EDI] << std::endl;
|
||||
ss << " ebp = " << context.uc_mcontext.gregs[REG_EBP] << std::endl;
|
||||
ss << " esp = " << context.uc_mcontext.gregs[REG_ESP] << std::endl;
|
||||
ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
|
||||
#else // 64-bit
|
||||
ss << " at rip = " << context.uc_mcontext.gregs[REG_RIP] << std::endl;
|
||||
ss << " rax = " << context.uc_mcontext.gregs[REG_RAX] << std::endl;
|
||||
ss << " rbx = " << context.uc_mcontext.gregs[REG_RBX] << std::endl;
|
||||
ss << " rcx = " << context.uc_mcontext.gregs[REG_RCX] << std::endl;
|
||||
ss << " rdx = " << context.uc_mcontext.gregs[REG_RDX] << std::endl;
|
||||
ss << " rsi = " << context.uc_mcontext.gregs[REG_RSI] << std::endl;
|
||||
ss << " rdi = " << context.uc_mcontext.gregs[REG_RDI] << std::endl;
|
||||
ss << " rbp = " << context.uc_mcontext.gregs[REG_RBP] << std::endl;
|
||||
ss << " rsp = " << context.uc_mcontext.gregs[REG_RSP] << std::endl;
|
||||
ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
|
||||
#endif
|
||||
ss << std::endl;
|
||||
ss.flags(std::ios::dec);
|
||||
ss << " backtrace:" << std::endl;
|
||||
|
||||
void* buffer[MAX_BACKTRACE_DEPTH];
|
||||
int numLevels = backtrace(buffer, MAX_BACKTRACE_DEPTH);
|
||||
char **tracebackBuffer = backtrace_symbols(buffer, numLevels);
|
||||
if(tracebackBuffer) {
|
||||
for(int i = 2; i < numLevels; i++) {
|
||||
std::string line = tracebackBuffer[i];
|
||||
if(line.find("__libc_start_main") != std::string::npos)
|
||||
break;
|
||||
#ifdef DEMANGLE_BACKTRACE_SYMBOLS
|
||||
std::size_t demanglePos = line.find("(_Z");
|
||||
if(demanglePos != std::string::npos) {
|
||||
demanglePos++;
|
||||
int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
|
||||
std::string funcName = line.substr(demanglePos, len);
|
||||
line.replace(demanglePos, len, Fw::demangleName(funcName.c_str()));
|
||||
}
|
||||
#endif
|
||||
ss << " " << i-1 << ": " << line << std::endl;
|
||||
}
|
||||
free(tracebackBuffer);
|
||||
}
|
||||
|
||||
logInfo(ss.str());
|
||||
|
||||
std::ofstream out(fileName);
|
||||
out << ss.str();
|
||||
out.close();
|
||||
logInfo("Crash report saved to file ", fileName);
|
||||
|
||||
signal(SIGILL, SIG_DFL);
|
||||
signal(SIGSEGV, SIG_DFL);
|
||||
signal(SIGFPE, SIG_DFL);
|
||||
}
|
||||
#endif
|
||||
|
||||
void Platform::init(PlatformListener* platformListener, const char *appName)
|
||||
{
|
||||
#ifdef HANDLE_EXCEPTIONS
|
||||
struct sigaction sa;
|
||||
sa.sa_sigaction = &crashHandler;
|
||||
sigemptyset (&sa.sa_mask);
|
||||
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
||||
|
||||
sigaction(SIGILL, &sa, NULL); // illegal instruction
|
||||
sigaction(SIGSEGV, &sa, NULL); // segmentation fault
|
||||
sigaction(SIGFPE, &sa, NULL); // floating-point exception
|
||||
#endif
|
||||
|
||||
// seend random numbers
|
||||
srand(time(NULL));
|
||||
|
||||
x11.appName = appName;
|
||||
x11.display = NULL;
|
||||
x11.visual = NULL;
|
||||
x11.glxContext = NULL;
|
||||
x11.xim = NULL;
|
||||
x11.xic = NULL;
|
||||
x11.colormap = None;
|
||||
x11.window = None;
|
||||
x11.cursor = None;
|
||||
x11.visible = false;
|
||||
x11.focused = false;
|
||||
x11.width = 0;
|
||||
x11.height = 0;
|
||||
x11.maximizeOnFirstShow = false;
|
||||
x11.listener = platformListener;
|
||||
|
||||
x11.keyMap[XK_Escape] = Fw::KeyEscape;
|
||||
x11.keyMap[XK_Tab] = Fw::KeyTab;
|
||||
x11.keyMap[XK_Return] = Fw::KeyReturn;
|
||||
x11.keyMap[XK_BackSpace] = Fw::KeyBackspace;
|
||||
|
||||
x11.keyMap[XK_Page_Up] = Fw::KeyPageUp;
|
||||
x11.keyMap[XK_Page_Down] = Fw::KeyPageDown;
|
||||
x11.keyMap[XK_Home] = Fw::KeyHome;
|
||||
x11.keyMap[XK_End] = Fw::KeyEnd;
|
||||
x11.keyMap[XK_Insert] = Fw::KeyInsert;
|
||||
x11.keyMap[XK_Delete] = Fw::KeyDelete;
|
||||
|
||||
x11.keyMap[XK_Up] = Fw::KeyUp;
|
||||
x11.keyMap[XK_Down] = Fw::KeyDown;
|
||||
x11.keyMap[XK_Left] = Fw::KeyLeft;
|
||||
x11.keyMap[XK_Right] = Fw::KeyRight;
|
||||
|
||||
x11.keyMap[XK_Num_Lock] = Fw::KeyNumLock;
|
||||
x11.keyMap[XK_Scroll_Lock] = Fw::KeyScrollLock;
|
||||
x11.keyMap[XK_Caps_Lock] = Fw::KeyCapsLock;
|
||||
x11.keyMap[XK_Print] = Fw::KeyPrintScreen;
|
||||
x11.keyMap[XK_Pause] = Fw::KeyPause;
|
||||
|
||||
x11.keyMap[XK_Control_L] = Fw::KeyCtrl;
|
||||
x11.keyMap[XK_Control_R] = Fw::KeyCtrl;
|
||||
x11.keyMap[XK_Shift_R] = Fw::KeyShift;
|
||||
x11.keyMap[XK_Shift_L] = Fw::KeyShift;
|
||||
x11.keyMap[XK_Alt_R] = Fw::KeyAlt;
|
||||
x11.keyMap[XK_Alt_L] = Fw::KeyAltGr;
|
||||
x11.keyMap[XK_Meta_L] = Fw::KeyMeta;
|
||||
x11.keyMap[XK_Meta_R] = Fw::KeyMeta;
|
||||
x11.keyMap[XK_Menu] = Fw::KeyMenu;
|
||||
|
||||
// ascii characters
|
||||
x11.keyMap[XK_space] = Fw::KeySpace;
|
||||
x11.keyMap[XK_exclam] = Fw::KeyExclamation;
|
||||
x11.keyMap[XK_quotedbl] = Fw::KeyQuote;
|
||||
x11.keyMap[XK_numbersign] = Fw::KeyNumberSign;
|
||||
x11.keyMap[XK_dollar] = Fw::KeyDollar;
|
||||
x11.keyMap[XK_percent] = Fw::KeyPercent;
|
||||
x11.keyMap[XK_ampersand] = Fw::KeyAmpersand;
|
||||
x11.keyMap[XK_apostrophe] = Fw::KeyApostrophe;
|
||||
x11.keyMap[XK_parenleft] = Fw::KeyLeftParen;
|
||||
x11.keyMap[XK_parenright] = Fw::KeyRightParen;
|
||||
x11.keyMap[XK_asterisk] = Fw::KeyAsterisk;
|
||||
x11.keyMap[XK_plus] = Fw::KeyPlus;
|
||||
x11.keyMap[XK_comma] = Fw::KeyComma;
|
||||
x11.keyMap[XK_minus] = Fw::KeyMinus;
|
||||
x11.keyMap[XK_period] = Fw::KeyPeriod;
|
||||
x11.keyMap[XK_slash] = Fw::KeySlash;
|
||||
x11.keyMap[XK_1] = Fw::Key1;
|
||||
x11.keyMap[XK_2] = Fw::Key2;
|
||||
x11.keyMap[XK_3] = Fw::Key3;
|
||||
x11.keyMap[XK_4] = Fw::Key4;
|
||||
x11.keyMap[XK_5] = Fw::Key5;
|
||||
x11.keyMap[XK_6] = Fw::Key6;
|
||||
x11.keyMap[XK_7] = Fw::Key7;
|
||||
x11.keyMap[XK_8] = Fw::Key8;
|
||||
x11.keyMap[XK_9] = Fw::Key9;
|
||||
x11.keyMap[XK_0] = Fw::Key0;
|
||||
x11.keyMap[XK_colon] = Fw::KeyColon;
|
||||
x11.keyMap[XK_semicolon] = Fw::KeySemicolon;
|
||||
x11.keyMap[XK_less] = Fw::KeyLess;
|
||||
x11.keyMap[XK_equal] = Fw::KeyEqual;
|
||||
x11.keyMap[XK_greater] = Fw::KeyGreater;
|
||||
x11.keyMap[XK_question] = Fw::KeyQuestion;
|
||||
x11.keyMap[XK_at] = Fw::KeyAtSign;
|
||||
x11.keyMap[XK_a] = Fw::KeyA;
|
||||
x11.keyMap[XK_b] = Fw::KeyB;
|
||||
x11.keyMap[XK_c] = Fw::KeyC;
|
||||
x11.keyMap[XK_d] = Fw::KeyD;
|
||||
x11.keyMap[XK_e] = Fw::KeyE;
|
||||
x11.keyMap[XK_f] = Fw::KeyF;
|
||||
x11.keyMap[XK_g] = Fw::KeyG;
|
||||
x11.keyMap[XK_h] = Fw::KeyH;
|
||||
x11.keyMap[XK_i] = Fw::KeyI;
|
||||
x11.keyMap[XK_j] = Fw::KeyJ;
|
||||
x11.keyMap[XK_k] = Fw::KeyK;
|
||||
x11.keyMap[XK_l] = Fw::KeyL;
|
||||
x11.keyMap[XK_m] = Fw::KeyM;
|
||||
x11.keyMap[XK_n] = Fw::KeyN;
|
||||
x11.keyMap[XK_o] = Fw::KeyO;
|
||||
x11.keyMap[XK_p] = Fw::KeyP;
|
||||
x11.keyMap[XK_q] = Fw::KeyQ;
|
||||
x11.keyMap[XK_r] = Fw::KeyR;
|
||||
x11.keyMap[XK_s] = Fw::KeyS;
|
||||
x11.keyMap[XK_t] = Fw::KeyT;
|
||||
x11.keyMap[XK_u] = Fw::KeyU;
|
||||
x11.keyMap[XK_v] = Fw::KeyV;
|
||||
x11.keyMap[XK_w] = Fw::KeyW;
|
||||
x11.keyMap[XK_x] = Fw::KeyX;
|
||||
x11.keyMap[XK_y] = Fw::KeyY;
|
||||
x11.keyMap[XK_z] = Fw::KeyZ;
|
||||
x11.keyMap[XK_bracketleft] = Fw::KeyLeftBracket;
|
||||
x11.keyMap[XK_backslash] = Fw::KeyBackslash;
|
||||
x11.keyMap[XK_bracketright] = Fw::KeyRightBracket;
|
||||
x11.keyMap[XK_asciicircum] = Fw::KeyCaret;
|
||||
x11.keyMap[XK_underscore] = Fw::KeyUnderscore;
|
||||
x11.keyMap[XK_grave] = Fw::KeyGrave;
|
||||
x11.keyMap[XK_braceleft] = Fw::KeyLeftCurly;
|
||||
x11.keyMap[XK_bar] = Fw::KeyBar;
|
||||
x11.keyMap[XK_braceright] = Fw::KeyRightCurly;
|
||||
x11.keyMap[XK_asciitilde] = Fw::KeyTilde;
|
||||
|
||||
// keypad
|
||||
x11.keyMap[XK_KP_Add] = Fw::KeyPlus;
|
||||
x11.keyMap[XK_KP_Subtract] = Fw::KeyMinus;
|
||||
x11.keyMap[XK_KP_Decimal] = Fw::KeyPeriod;
|
||||
x11.keyMap[XK_KP_Divide] = Fw::KeySlash;
|
||||
x11.keyMap[XK_KP_Multiply] = Fw::KeyAsterisk;
|
||||
x11.keyMap[XK_KP_Enter] = Fw::KeyEnter;
|
||||
|
||||
// keypad with numlock off
|
||||
x11.keyMap[XK_KP_Insert] = Fw::KeyNumpad0;
|
||||
x11.keyMap[XK_KP_End] = Fw::KeyNumpad1;
|
||||
x11.keyMap[XK_KP_Down] = Fw::KeyNumpad2;
|
||||
x11.keyMap[XK_KP_Page_Down] = Fw::KeyNumpad3;
|
||||
x11.keyMap[XK_KP_Left] = Fw::KeyNumpad4;
|
||||
x11.keyMap[XK_KP_Begin] = Fw::KeyNumpad5;
|
||||
x11.keyMap[XK_KP_Right] = Fw::KeyNumpad6;
|
||||
x11.keyMap[XK_KP_Home] = Fw::KeyNumpad7;
|
||||
x11.keyMap[XK_KP_Up] = Fw::KeyNumpad8;
|
||||
x11.keyMap[XK_KP_Page_Up] = Fw::KeyNumpad9;
|
||||
x11.keyMap[XK_KP_Delete] = Fw::KeyDelete;
|
||||
|
||||
// keypad with numlock on
|
||||
x11.keyMap[XK_KP_0] = Fw::Key0;
|
||||
x11.keyMap[XK_KP_1] = Fw::Key1;
|
||||
x11.keyMap[XK_KP_2] = Fw::Key2;
|
||||
x11.keyMap[XK_KP_3] = Fw::Key3;
|
||||
x11.keyMap[XK_KP_4] = Fw::Key4;
|
||||
x11.keyMap[XK_KP_5] = Fw::Key5;
|
||||
x11.keyMap[XK_KP_6] = Fw::Key6;
|
||||
x11.keyMap[XK_KP_7] = Fw::Key7;
|
||||
x11.keyMap[XK_KP_8] = Fw::Key8;
|
||||
x11.keyMap[XK_KP_9] = Fw::Key9;
|
||||
|
||||
x11.keyMap[XK_F1] = Fw::KeyF1;
|
||||
x11.keyMap[XK_F2] = Fw::KeyF2;
|
||||
x11.keyMap[XK_F3] = Fw::KeyF3;
|
||||
x11.keyMap[XK_F4] = Fw::KeyF4;
|
||||
x11.keyMap[XK_F5] = Fw::KeyF5;
|
||||
x11.keyMap[XK_F6] = Fw::KeyF6;
|
||||
x11.keyMap[XK_F7] = Fw::KeyF7;
|
||||
x11.keyMap[XK_F8] = Fw::KeyF8;
|
||||
x11.keyMap[XK_F9] = Fw::KeyF9;
|
||||
x11.keyMap[XK_F10] = Fw::KeyF10;
|
||||
x11.keyMap[XK_F11] = Fw::KeyF11;
|
||||
x11.keyMap[XK_F12] = Fw::KeyF12;
|
||||
|
||||
// try to set a latin1 locales otherwise fallback to standard C locale
|
||||
static char locales[][32] = { "en_US.iso88591", "iso88591", "en_US", "C" };
|
||||
for(int i=0;i<4;++i) {
|
||||
if(setlocale(LC_ALL, locales[i]))
|
||||
break;
|
||||
}
|
||||
|
||||
// open display
|
||||
x11.display = XOpenDisplay(0);
|
||||
if(!x11.display)
|
||||
logFatal("Failed to open X display");
|
||||
|
||||
// check if GLX is supported on this display
|
||||
if(!glXQueryExtension(x11.display, 0, 0))
|
||||
logFatal("GLX not supported");
|
||||
|
||||
// retrieve GLX version
|
||||
int glxMajor;
|
||||
int glxMinor;
|
||||
if(!glXQueryVersion(x11.display, &glxMajor, &glxMinor))
|
||||
logFatal("Unable to query GLX version");
|
||||
logInfo("GLX version ",glxMajor,".",glxMinor);
|
||||
|
||||
// clipboard related atoms
|
||||
x11.atomClipboard = XInternAtom(x11.display, "CLIPBOARD", False);
|
||||
x11.atomTargets = XInternAtom(x11.display, "TARGETS", False);
|
||||
x11.atomUTF8String = XInternAtom(x11.display, "UTF8_STRING", False);
|
||||
x11.atomText = XInternAtom(x11.display, "TEXT", False);
|
||||
x11.atomCompoundText = XInternAtom(x11.display, "COMPOUND_TEXT", False);
|
||||
x11.atomWindowState = XInternAtom(x11.display, "_NET_WM_STATE", False);
|
||||
x11.atomWindowMaximizedVert = XInternAtom(x11.display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
|
||||
x11.atomWindowMaximizedHorz = XInternAtom(x11.display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
|
||||
}
|
||||
|
||||
void Platform::terminate()
|
||||
{
|
||||
if(x11.window) {
|
||||
destroyWindow();
|
||||
x11.window = None;
|
||||
}
|
||||
|
||||
// close display
|
||||
if(x11.display) {
|
||||
XCloseDisplay(x11.display);
|
||||
x11.display = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void Platform::poll()
|
||||
{
|
||||
XEvent event, peekevent;
|
||||
PlatformEvent& platformEvent = x11.platformEvent;
|
||||
static int oldWidth = -1;
|
||||
static int oldHeight = -1;
|
||||
while(XPending(x11.display) > 0) {
|
||||
XNextEvent(x11.display, &event);
|
||||
|
||||
// call filter because xim will discard KeyPress events when keys still composing
|
||||
if(XFilterEvent(&event, x11.window))
|
||||
continue;
|
||||
|
||||
// discard events of repeated key releases
|
||||
if(event.type == KeyRelease && XPending(x11.display)) {
|
||||
XPeekEvent(x11.display, &peekevent);
|
||||
if((peekevent.type == KeyPress) &&
|
||||
(peekevent.xkey.keycode == event.xkey.keycode) &&
|
||||
((peekevent.xkey.time-event.xkey.time) < 2))
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(event.type) {
|
||||
case Expose:
|
||||
// needs redraw
|
||||
break;
|
||||
case ConfigureNotify:
|
||||
// window resize
|
||||
if(oldWidth != event.xconfigure.width || oldHeight != event.xconfigure.height) {
|
||||
x11.listener->onResize(Size(event.xconfigure.width, event.xconfigure.height));
|
||||
oldWidth = event.xconfigure.width;
|
||||
oldHeight = event.xconfigure.height;
|
||||
}
|
||||
|
||||
if(!isWindowMaximized()) {
|
||||
x11.width = event.xconfigure.width;
|
||||
x11.height = event.xconfigure.height;
|
||||
|
||||
// hack to fix x11 windows move gaps
|
||||
static int gap_x = -1, gap_y = -1;
|
||||
if(gap_x == -1 && gap_y == -1) {
|
||||
gap_x = event.xconfigure.x;
|
||||
gap_y = event.xconfigure.y;
|
||||
}
|
||||
x11.x = event.xconfigure.x - gap_x;
|
||||
x11.y = event.xconfigure.y - gap_y;
|
||||
}
|
||||
break;
|
||||
|
||||
case KeyPress:
|
||||
case KeyRelease: {
|
||||
KeySym keysym;
|
||||
char buf[32];
|
||||
int len;
|
||||
|
||||
platformEvent.ctrl = (event.xkey.state & ControlMask);
|
||||
platformEvent.shift = (event.xkey.state & ShiftMask);
|
||||
platformEvent.alt = (event.xkey.state & Mod1Mask);
|
||||
platformEvent.keychar = 0;
|
||||
|
||||
// fire enter text event
|
||||
if(event.type == KeyPress && !platformEvent.ctrl && !platformEvent.alt) {
|
||||
if(x11.xic) { // with xim we can get latin1 input correctly
|
||||
Status status;
|
||||
len = XmbLookupString(x11.xic, &event.xkey, buf, sizeof(buf), &keysym, &status);
|
||||
} else { // otherwise use XLookupString, but it doesn't work right with dead keys often
|
||||
static XComposeStatus compose = {NULL, 0};
|
||||
len = XLookupString(&event.xkey, buf, sizeof(buf), &keysym, &compose);
|
||||
}
|
||||
|
||||
if(len > 0 &&
|
||||
// these keys produces characters that we don't want to capture
|
||||
keysym != XK_BackSpace &&
|
||||
keysym != XK_Return &&
|
||||
keysym != XK_Delete &&
|
||||
keysym != XK_Escape &&
|
||||
(uchar)(buf[0]) >= 32
|
||||
) {
|
||||
//logDebug("char: ", buf[0], " code: ", (uint)buf[0]);
|
||||
platformEvent.keychar = buf[0];
|
||||
}
|
||||
} else {
|
||||
//event.xkey.state &= ~(ShiftMask | LockMask);
|
||||
len = XLookupString(&event.xkey, buf, sizeof(buf), &keysym, 0);
|
||||
|
||||
if(len > 0 && (uchar)platformEvent.keychar >= 32)
|
||||
platformEvent.keychar = (len > 0) ? buf[0] : 0;
|
||||
}
|
||||
|
||||
if(x11.keyMap.find(keysym) != x11.keyMap.end())
|
||||
platformEvent.keycode = x11.keyMap[keysym];
|
||||
else
|
||||
platformEvent.keycode = Fw::KeyUnknown;
|
||||
|
||||
platformEvent.keycode = x11.keyMap[keysym];
|
||||
platformEvent.type = (event.type == KeyPress) ? EventKeyDown : EventKeyUp;
|
||||
|
||||
if(platformEvent.keycode != Fw::KeyUnknown || platformEvent.keychar != 0)
|
||||
x11.listener->onPlatformEvent(platformEvent);
|
||||
break;
|
||||
}
|
||||
case ButtonPress:
|
||||
case ButtonRelease:
|
||||
switch(event.xbutton.button) {
|
||||
case Button1:
|
||||
platformEvent.type = (event.type == ButtonPress) ? EventMouseLeftButtonDown : EventMouseLeftButtonUp;
|
||||
break;
|
||||
case Button3:
|
||||
platformEvent.type = (event.type == ButtonPress) ? EventMouseRightButtonDown : EventMouseRightButtonUp;
|
||||
break;
|
||||
case Button2:
|
||||
platformEvent.type = (event.type == ButtonPress) ? EventMouseMiddleButtonDown : EventMouseMiddleButtonUp;
|
||||
break;
|
||||
case Button4:
|
||||
platformEvent.type = EventMouseWheelUp;
|
||||
break;
|
||||
case Button5:
|
||||
platformEvent.type = EventMouseWheelDown;
|
||||
break;
|
||||
}
|
||||
x11.listener->onPlatformEvent(platformEvent);
|
||||
break;
|
||||
|
||||
case MotionNotify:
|
||||
{
|
||||
platformEvent.type = EventMouseMove;
|
||||
Point newMousePos(event.xbutton.x, event.xbutton.y);
|
||||
platformEvent.mouseMoved = newMousePos - platformEvent.mousePos;
|
||||
platformEvent.mousePos = newMousePos;
|
||||
x11.listener->onPlatformEvent(platformEvent);
|
||||
break;
|
||||
}
|
||||
|
||||
case MapNotify:
|
||||
x11.visible = true;
|
||||
break;
|
||||
|
||||
case UnmapNotify:
|
||||
x11.visible = false;
|
||||
break;
|
||||
|
||||
case FocusIn:
|
||||
x11.focused = true;
|
||||
break;
|
||||
|
||||
case FocusOut:
|
||||
x11.focused = false;
|
||||
break;
|
||||
|
||||
// clipboard data request
|
||||
case SelectionRequest:
|
||||
{
|
||||
XEvent respond;
|
||||
XSelectionRequestEvent *req = &(event.xselectionrequest);
|
||||
|
||||
if(req->target == x11.atomTargets ) {
|
||||
Atom typeList[] = {x11.atomText, x11.atomCompoundText, x11.atomUTF8String, XA_STRING};
|
||||
|
||||
XChangeProperty(x11.display, req->requestor,
|
||||
req->property, req->target,
|
||||
8, PropModeReplace,
|
||||
(uchar *) &typeList,
|
||||
sizeof(typeList));
|
||||
respond.xselection.property = req->property;
|
||||
} else {
|
||||
XChangeProperty(x11.display,
|
||||
req->requestor,
|
||||
req->property, req->target,
|
||||
8,
|
||||
PropModeReplace,
|
||||
(uchar*) x11.clipboardText.c_str(),
|
||||
x11.clipboardText.size());
|
||||
respond.xselection.property = req->property;
|
||||
}
|
||||
|
||||
respond.xselection.type = SelectionNotify;
|
||||
respond.xselection.display = req->display;
|
||||
respond.xselection.requestor = req->requestor;
|
||||
respond.xselection.selection = req->selection;
|
||||
respond.xselection.target = req->target;
|
||||
respond.xselection.time = req->time;
|
||||
XSendEvent(x11.display, req->requestor, 0, 0, &respond);
|
||||
XFlush(x11.display);
|
||||
break;
|
||||
}
|
||||
|
||||
case ClientMessage:
|
||||
{
|
||||
if((Atom)event.xclient.data.l[0] == x11.atomDeleteWindow)
|
||||
x11.listener->onClose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Platform::createWindow(int x, int y, int width, int height, int minWidth, int minHeight, bool maximized)
|
||||
{
|
||||
static int attrList[] = {
|
||||
GLX_USE_GL,
|
||||
GLX_RGBA,
|
||||
GLX_DOUBLEBUFFER,
|
||||
None
|
||||
};
|
||||
|
||||
// choose OpenGL, RGBA, double buffered, visual
|
||||
x11.visual = glXChooseVisual(x11.display, DefaultScreen(x11.display), attrList);
|
||||
if(!x11.visual)
|
||||
logFatal("RGBA/Double buffered visual not supported");
|
||||
|
||||
// create GLX context
|
||||
x11.glxContext = glXCreateContext(x11.display, x11.visual, NULL, True);
|
||||
if(!x11.glxContext)
|
||||
logFatal("Unable to create GLX context");
|
||||
|
||||
logInfo("Direct rendering: ", glXIsDirect(x11.display, x11.glxContext) ? "Yes" : "No");
|
||||
|
||||
// color map
|
||||
x11.colormap = XCreateColormap(x11.display,
|
||||
RootWindow(x11.display, x11.visual->screen),
|
||||
x11.visual->visual,
|
||||
AllocNone);
|
||||
|
||||
// setup window type
|
||||
XSetWindowAttributes attr;
|
||||
attr.colormap = x11.colormap;
|
||||
attr.border_pixel = 0;
|
||||
attr.event_mask = KeyPressMask | KeyReleaseMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
|
||||
ExposureMask | VisibilityChangeMask |
|
||||
StructureNotifyMask | FocusChangeMask;
|
||||
unsigned int mask = CWBorderPixel | CWColormap | CWEventMask;
|
||||
|
||||
x11.x = x;
|
||||
x11.y = y;
|
||||
|
||||
// create the window
|
||||
x11.window = XCreateWindow(x11.display,
|
||||
RootWindow(x11.display, x11.visual->screen),
|
||||
0, 0,
|
||||
width, height,
|
||||
0,
|
||||
x11.visual->depth,
|
||||
InputOutput,
|
||||
x11.visual->visual,
|
||||
mask,
|
||||
&attr);
|
||||
if(!x11.window)
|
||||
logFatal("XCreateWindow failed");
|
||||
|
||||
// create input context (to have better key input handling)
|
||||
if(XSupportsLocale()) {
|
||||
XSetLocaleModifiers("");
|
||||
x11.xim = XOpenIM(x11.display, NULL, NULL, NULL);
|
||||
if(x11.xim) {
|
||||
x11.xic = XCreateIC(x11.xim,
|
||||
XNInputStyle,
|
||||
XIMPreeditNothing | XIMStatusNothing,
|
||||
XNClientWindow, x11.window, NULL);
|
||||
if(!x11.xic)
|
||||
logError("Unable to create the input context");
|
||||
} else
|
||||
logError("Failed to open an input method");
|
||||
} else
|
||||
logError("X11 does not support the current locale");
|
||||
|
||||
if(!x11.xic)
|
||||
logWarning("Input of special keys maybe messed up because we couldn't create an input context");
|
||||
|
||||
// set window minimum size
|
||||
XSizeHints xsizehints;
|
||||
xsizehints.flags = PMinSize;
|
||||
xsizehints.min_width = minWidth;
|
||||
xsizehints.min_height= minHeight;
|
||||
XSetWMSizeHints(x11.display, x11.window, &xsizehints, XA_WM_NORMAL_HINTS);
|
||||
|
||||
// handle delete window event
|
||||
x11.atomDeleteWindow = XInternAtom(x11.display, "WM_DELETE_WINDOW", True);
|
||||
XSetWMProtocols(x11.display, x11.window, &x11.atomDeleteWindow , 1);
|
||||
|
||||
// connect the GLX-context to the window
|
||||
if(!glXMakeCurrent(x11.display, x11.window, x11.glxContext))
|
||||
logFatal("glXMakeCurrent failed");
|
||||
|
||||
x11.width = width;
|
||||
x11.height = height;
|
||||
x11.maximizeOnFirstShow = maximized;
|
||||
|
||||
// call first onResize
|
||||
x11.listener->onResize(Size(width, height));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Platform::setWindowIcon(const std::string& pngIcon)
|
||||
{
|
||||
apng_data apng;
|
||||
std::stringstream fin;
|
||||
g_resources.loadFile(pngIcon, fin);
|
||||
if(load_apng(fin, &apng) == 0) {
|
||||
if(apng.bpp != 4) {
|
||||
logError("could not set app icon, icon image must have 4 channels");
|
||||
free_apng(&apng);
|
||||
return;
|
||||
}
|
||||
|
||||
int n = apng.width * apng.height;
|
||||
std::vector<unsigned long int> iconData(n + 2);
|
||||
iconData[0] = apng.width;
|
||||
iconData[1] = apng.height;
|
||||
for(int i=0; i < n;++i) {
|
||||
uint8 *pixel = (uint8*)&iconData[2 + i];
|
||||
pixel[2] = *(apng.pdata + (i * 4) + 0);
|
||||
pixel[1] = *(apng.pdata + (i * 4) + 1);
|
||||
pixel[0] = *(apng.pdata + (i * 4) + 2);
|
||||
pixel[3] = *(apng.pdata + (i * 4) + 3);
|
||||
}
|
||||
|
||||
Atom property = XInternAtom(x11.display, "_NET_WM_ICON", 0);
|
||||
if(!XChangeProperty(x11.display, x11.window, property, XA_CARDINAL, 32, PropModeReplace, (const unsigned char*)&iconData[0], iconData.size()))
|
||||
logError("Couldn't set app icon");
|
||||
|
||||
free_apng(&apng);
|
||||
} else
|
||||
logError("Couldn't load app icon");
|
||||
}
|
||||
|
||||
void Platform::destroyWindow()
|
||||
{
|
||||
if(x11.glxContext) {
|
||||
glXMakeCurrent(x11.display, None, NULL);
|
||||
glXDestroyContext(x11.display, x11.glxContext);
|
||||
x11.glxContext = NULL;
|
||||
}
|
||||
|
||||
if(x11.visual) {
|
||||
XFree(x11.visual);
|
||||
x11.visual = NULL;
|
||||
}
|
||||
|
||||
if(x11.colormap != None) {
|
||||
XFreeColormap(x11.display, x11.colormap);
|
||||
x11.colormap = 0;
|
||||
}
|
||||
|
||||
if(x11.window != None) {
|
||||
XDestroyWindow(x11.display, x11.window);
|
||||
x11.window = None;
|
||||
}
|
||||
|
||||
if(x11.xic) {
|
||||
XDestroyIC(x11.xic);
|
||||
x11.xic = NULL;
|
||||
}
|
||||
|
||||
if(x11.xim) {
|
||||
XCloseIM(x11.xim);
|
||||
x11.xim = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void Platform::showWindow()
|
||||
{
|
||||
XMapWindow(x11.display, x11.window);
|
||||
|
||||
static bool firstShow = true;
|
||||
if(firstShow) {
|
||||
// move window
|
||||
XMoveWindow(x11.display, x11.window, x11.x, x11.y);
|
||||
|
||||
// set window maximized if needed
|
||||
if(x11.maximizeOnFirstShow) {
|
||||
XEvent e;
|
||||
memset(&e, 0, sizeof(XEvent));
|
||||
e.xany.type = ClientMessage;
|
||||
e.xclient.message_type = x11.atomWindowState;
|
||||
e.xclient.format = 32;
|
||||
e.xclient.window = x11.window;
|
||||
e.xclient.data.l[0] = 1l;
|
||||
e.xclient.data.l[1] = x11.atomWindowMaximizedVert;
|
||||
e.xclient.data.l[2] = x11.atomWindowMaximizedHorz;
|
||||
e.xclient.data.l[3] = 0l;
|
||||
|
||||
XSendEvent(x11.display, RootWindow(x11.display, x11.visual->screen), 0,
|
||||
SubstructureNotifyMask | SubstructureRedirectMask, &e);
|
||||
}
|
||||
firstShow = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Platform::hideWindow()
|
||||
{
|
||||
XUnmapWindow(x11.display, x11.window);
|
||||
}
|
||||
|
||||
void Platform::setWindowTitle(const char *title)
|
||||
{
|
||||
XStoreName(x11.display, x11.window, title);
|
||||
XSetIconName(x11.display, x11.window, title);
|
||||
}
|
||||
|
||||
void *Platform::getExtensionProcAddress(const char *ext)
|
||||
{
|
||||
return (void*)glXGetProcAddressARB((const GLubyte*)ext);
|
||||
}
|
||||
|
||||
bool Platform::isExtensionSupported(const char *ext)
|
||||
{
|
||||
const char *exts = glXQueryExtensionsString(x11.display, DefaultScreen(x11.display));
|
||||
if(strstr(exts, ext))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *Platform::getClipboardText()
|
||||
{
|
||||
Window ownerWindow = XGetSelectionOwner(x11.display, x11.atomClipboard);
|
||||
if(ownerWindow == x11.window)
|
||||
return x11.clipboardText.c_str();
|
||||
|
||||
std::string clipboard = "";
|
||||
if(ownerWindow != None) {
|
||||
XConvertSelection(x11.display, x11.atomClipboard, XA_STRING, 0, ownerWindow, CurrentTime);
|
||||
XFlush(x11.display);
|
||||
|
||||
// hack to wait SelectioNotify event, otherwise we will get wrong clipboard pastes
|
||||
usleep(100 * 1000);
|
||||
|
||||
// check for data
|
||||
Atom type;
|
||||
int format;
|
||||
ulong numItems, bytesLeft, dummy;
|
||||
uchar *data;
|
||||
XGetWindowProperty(x11.display, ownerWindow,
|
||||
XA_STRING,
|
||||
0, 0, 0,
|
||||
AnyPropertyType,
|
||||
&type,
|
||||
&format,
|
||||
&numItems,
|
||||
&bytesLeft,
|
||||
&data);
|
||||
if(bytesLeft > 0) {
|
||||
// get the data get
|
||||
int result = XGetWindowProperty(x11.display, ownerWindow,
|
||||
XA_STRING,
|
||||
0,
|
||||
bytesLeft,
|
||||
0,
|
||||
AnyPropertyType,
|
||||
&type,
|
||||
&format,
|
||||
&numItems,
|
||||
&dummy,
|
||||
&data);
|
||||
if(result == Success)
|
||||
clipboard = (const char*)data;
|
||||
XFree(data);
|
||||
}
|
||||
}
|
||||
return clipboard.c_str();
|
||||
}
|
||||
|
||||
void Platform::setClipboardText(const char *text)
|
||||
{
|
||||
x11.clipboardText = text;
|
||||
XSetSelectionOwner(x11.display, x11.atomClipboard, x11.window, CurrentTime);
|
||||
XFlush(x11.display);
|
||||
}
|
||||
|
||||
void Platform::hideMouseCursor()
|
||||
{
|
||||
if(x11.cursor == None) {
|
||||
char bm[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
Pixmap pix = XCreateBitmapFromData(x11.display, x11.window, bm, 8, 8);
|
||||
XColor black;
|
||||
memset(&black, 0, sizeof(XColor));
|
||||
black.flags = DoRed | DoGreen | DoBlue;
|
||||
x11.cursor = XCreatePixmapCursor(x11.display, pix, pix, &black, &black, 0, 0);
|
||||
XFreePixmap(x11.display, pix);
|
||||
}
|
||||
XDefineCursor(x11.display, x11.window, x11.cursor);
|
||||
}
|
||||
|
||||
void Platform::showMouseCursor()
|
||||
{
|
||||
XUndefineCursor(x11.display, x11.window);
|
||||
if(x11.cursor != None) {
|
||||
XFreeCursor(x11.display, x11.cursor);
|
||||
x11.cursor = None;
|
||||
}
|
||||
}
|
||||
|
||||
Point Platform::getMouseCursorPos()
|
||||
{
|
||||
return x11.platformEvent.mousePos;
|
||||
}
|
||||
|
||||
void Platform::setVerticalSync(bool enable)
|
||||
{
|
||||
typedef GLint (*glSwapIntervalProc)(GLint);
|
||||
glSwapIntervalProc glSwapInterval = NULL;
|
||||
|
||||
if(isExtensionSupported("GLX_MESA_swap_control"))
|
||||
glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalMESA");
|
||||
else if(isExtensionSupported("GLX_SGI_swap_control"))
|
||||
glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalSGI");
|
||||
|
||||
if(glSwapInterval)
|
||||
glSwapInterval(enable ? 1 : 0);
|
||||
}
|
||||
|
||||
void Platform::swapBuffers()
|
||||
{
|
||||
//glFlush();
|
||||
//glFinish();
|
||||
glXSwapBuffers(x11.display, x11.window);
|
||||
}
|
||||
|
||||
bool Platform::isWindowFocused()
|
||||
{
|
||||
return x11.focused;
|
||||
}
|
||||
|
||||
bool Platform::isWindowVisible()
|
||||
{
|
||||
return x11.visible;
|
||||
}
|
||||
|
||||
int Platform::getWindowX()
|
||||
{
|
||||
return x11.x;
|
||||
}
|
||||
|
||||
int Platform::getWindowY()
|
||||
{
|
||||
return x11.y;
|
||||
}
|
||||
|
||||
int Platform::getWindowWidth()
|
||||
{
|
||||
return x11.width;
|
||||
}
|
||||
|
||||
int Platform::getWindowHeight()
|
||||
{
|
||||
return x11.height;
|
||||
}
|
||||
|
||||
int Platform::getDisplayWidth()
|
||||
{
|
||||
return XDisplayWidth(x11.display, DefaultScreen(x11.display));
|
||||
}
|
||||
|
||||
int Platform::getDisplayHeight()
|
||||
{
|
||||
return XDisplayHeight(x11.display, DefaultScreen(x11.display));
|
||||
}
|
||||
|
||||
bool Platform::isWindowMaximized()
|
||||
{
|
||||
bool ret = false;
|
||||
Atom actualType;
|
||||
int actualFormat;
|
||||
ulong i, numItems, bytesAfter;
|
||||
uchar *propertyValue = NULL;
|
||||
long maxLength = 1024;
|
||||
|
||||
if(XGetWindowProperty(x11.display, x11.window, x11.atomWindowState,
|
||||
0l, maxLength, False, XA_ATOM, &actualType,
|
||||
&actualFormat, &numItems, &bytesAfter,
|
||||
&propertyValue) == Success) {
|
||||
Atom *atoms = (Atom *)propertyValue;
|
||||
int maximized = 0;
|
||||
|
||||
for(i=0; i<numItems; ++i) {
|
||||
if(atoms[i] == x11.atomWindowMaximizedVert)
|
||||
maximized |= 1;
|
||||
else if(atoms[i] == x11.atomWindowMaximizedHorz)
|
||||
maximized |= 2;
|
||||
}
|
||||
if(maximized == 3)
|
||||
ret = true;
|
||||
XFree(propertyValue);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string Platform::getAppUserDir()
|
||||
{
|
||||
std::stringstream sdir;
|
||||
sdir << PHYSFS_getUserDir() << "." << x11.appName;
|
||||
if((mkdir(sdir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) && (errno != EEXIST))
|
||||
logError("Couldn't create directory for saving configuration file. (",sdir.str(),")");
|
||||
return sdir.str();
|
||||
}
|
||||
|
||||
void Platform::displayFatalError(const std::string& message)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
@ -0,0 +1,887 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "x11window.h"
|
||||
#include <framework/core/resourcemanager.h>
|
||||
#include <framework/thirdparty/apngloader.h>
|
||||
|
||||
X11Window::X11Window()
|
||||
{
|
||||
m_cursor = None;
|
||||
m_visual = 0;
|
||||
m_colormap = 0;
|
||||
|
||||
m_keyMap[XK_Escape] = Fw::KeyEscape;
|
||||
m_keyMap[XK_Tab] = Fw::KeyTab;
|
||||
m_keyMap[XK_Return] = Fw::KeyReturn;
|
||||
m_keyMap[XK_BackSpace] = Fw::KeyBackspace;
|
||||
|
||||
m_keyMap[XK_Page_Up] = Fw::KeyPageUp;
|
||||
m_keyMap[XK_Page_Down] = Fw::KeyPageDown;
|
||||
m_keyMap[XK_Home] = Fw::KeyHome;
|
||||
m_keyMap[XK_End] = Fw::KeyEnd;
|
||||
m_keyMap[XK_Insert] = Fw::KeyInsert;
|
||||
m_keyMap[XK_Delete] = Fw::KeyDelete;
|
||||
|
||||
m_keyMap[XK_Up] = Fw::KeyUp;
|
||||
m_keyMap[XK_Down] = Fw::KeyDown;
|
||||
m_keyMap[XK_Left] = Fw::KeyLeft;
|
||||
m_keyMap[XK_Right] = Fw::KeyRight;
|
||||
|
||||
m_keyMap[XK_Num_Lock] = Fw::KeyNumLock;
|
||||
m_keyMap[XK_Scroll_Lock] = Fw::KeyScrollLock;
|
||||
m_keyMap[XK_Caps_Lock] = Fw::KeyCapsLock;
|
||||
m_keyMap[XK_Print] = Fw::KeyPrintScreen;
|
||||
m_keyMap[XK_Pause] = Fw::KeyPause;
|
||||
|
||||
m_keyMap[XK_Control_L] = Fw::KeyCtrl;
|
||||
m_keyMap[XK_Control_R] = Fw::KeyCtrl;
|
||||
m_keyMap[XK_Shift_R] = Fw::KeyShift;
|
||||
m_keyMap[XK_Shift_L] = Fw::KeyShift;
|
||||
m_keyMap[XK_Alt_R] = Fw::KeyAlt;
|
||||
m_keyMap[XK_Alt_L] = Fw::KeyAltGr;
|
||||
m_keyMap[XK_Meta_L] = Fw::KeyMeta;
|
||||
m_keyMap[XK_Meta_R] = Fw::KeyMeta;
|
||||
m_keyMap[XK_Menu] = Fw::KeyMenu;
|
||||
|
||||
// ascii characters
|
||||
m_keyMap[XK_space] = Fw::KeySpace;
|
||||
m_keyMap[XK_exclam] = Fw::KeyExclamation;
|
||||
m_keyMap[XK_quotedbl] = Fw::KeyQuote;
|
||||
m_keyMap[XK_numbersign] = Fw::KeyNumberSign;
|
||||
m_keyMap[XK_dollar] = Fw::KeyDollar;
|
||||
m_keyMap[XK_percent] = Fw::KeyPercent;
|
||||
m_keyMap[XK_ampersand] = Fw::KeyAmpersand;
|
||||
m_keyMap[XK_apostrophe] = Fw::KeyApostrophe;
|
||||
m_keyMap[XK_parenleft] = Fw::KeyLeftParen;
|
||||
m_keyMap[XK_parenright] = Fw::KeyRightParen;
|
||||
m_keyMap[XK_asterisk] = Fw::KeyAsterisk;
|
||||
m_keyMap[XK_plus] = Fw::KeyPlus;
|
||||
m_keyMap[XK_comma] = Fw::KeyComma;
|
||||
m_keyMap[XK_minus] = Fw::KeyMinus;
|
||||
m_keyMap[XK_period] = Fw::KeyPeriod;
|
||||
m_keyMap[XK_slash] = Fw::KeySlash;
|
||||
m_keyMap[XK_1] = Fw::Key1;
|
||||
m_keyMap[XK_2] = Fw::Key2;
|
||||
m_keyMap[XK_3] = Fw::Key3;
|
||||
m_keyMap[XK_4] = Fw::Key4;
|
||||
m_keyMap[XK_5] = Fw::Key5;
|
||||
m_keyMap[XK_6] = Fw::Key6;
|
||||
m_keyMap[XK_7] = Fw::Key7;
|
||||
m_keyMap[XK_8] = Fw::Key8;
|
||||
m_keyMap[XK_9] = Fw::Key9;
|
||||
m_keyMap[XK_0] = Fw::Key0;
|
||||
m_keyMap[XK_colon] = Fw::KeyColon;
|
||||
m_keyMap[XK_semicolon] = Fw::KeySemicolon;
|
||||
m_keyMap[XK_less] = Fw::KeyLess;
|
||||
m_keyMap[XK_equal] = Fw::KeyEqual;
|
||||
m_keyMap[XK_greater] = Fw::KeyGreater;
|
||||
m_keyMap[XK_question] = Fw::KeyQuestion;
|
||||
m_keyMap[XK_at] = Fw::KeyAtSign;
|
||||
m_keyMap[XK_a] = Fw::KeyA;
|
||||
m_keyMap[XK_b] = Fw::KeyB;
|
||||
m_keyMap[XK_c] = Fw::KeyC;
|
||||
m_keyMap[XK_d] = Fw::KeyD;
|
||||
m_keyMap[XK_e] = Fw::KeyE;
|
||||
m_keyMap[XK_f] = Fw::KeyF;
|
||||
m_keyMap[XK_g] = Fw::KeyG;
|
||||
m_keyMap[XK_h] = Fw::KeyH;
|
||||
m_keyMap[XK_i] = Fw::KeyI;
|
||||
m_keyMap[XK_j] = Fw::KeyJ;
|
||||
m_keyMap[XK_k] = Fw::KeyK;
|
||||
m_keyMap[XK_l] = Fw::KeyL;
|
||||
m_keyMap[XK_m] = Fw::KeyM;
|
||||
m_keyMap[XK_n] = Fw::KeyN;
|
||||
m_keyMap[XK_o] = Fw::KeyO;
|
||||
m_keyMap[XK_p] = Fw::KeyP;
|
||||
m_keyMap[XK_q] = Fw::KeyQ;
|
||||
m_keyMap[XK_r] = Fw::KeyR;
|
||||
m_keyMap[XK_s] = Fw::KeyS;
|
||||
m_keyMap[XK_t] = Fw::KeyT;
|
||||
m_keyMap[XK_u] = Fw::KeyU;
|
||||
m_keyMap[XK_v] = Fw::KeyV;
|
||||
m_keyMap[XK_w] = Fw::KeyW;
|
||||
m_keyMap[XK_x] = Fw::KeyX;
|
||||
m_keyMap[XK_y] = Fw::KeyY;
|
||||
m_keyMap[XK_z] = Fw::KeyZ;
|
||||
m_keyMap[XK_bracketleft] = Fw::KeyLeftBracket;
|
||||
m_keyMap[XK_backslash] = Fw::KeyBackslash;
|
||||
m_keyMap[XK_bracketright] = Fw::KeyRightBracket;
|
||||
m_keyMap[XK_asciicircum] = Fw::KeyCaret;
|
||||
m_keyMap[XK_underscore] = Fw::KeyUnderscore;
|
||||
m_keyMap[XK_grave] = Fw::KeyGrave;
|
||||
m_keyMap[XK_braceleft] = Fw::KeyLeftCurly;
|
||||
m_keyMap[XK_bar] = Fw::KeyBar;
|
||||
m_keyMap[XK_braceright] = Fw::KeyRightCurly;
|
||||
m_keyMap[XK_asciitilde] = Fw::KeyTilde;
|
||||
|
||||
// keypad
|
||||
m_keyMap[XK_KP_Add] = Fw::KeyPlus;
|
||||
m_keyMap[XK_KP_Subtract] = Fw::KeyMinus;
|
||||
m_keyMap[XK_KP_Decimal] = Fw::KeyPeriod;
|
||||
m_keyMap[XK_KP_Divide] = Fw::KeySlash;
|
||||
m_keyMap[XK_KP_Multiply] = Fw::KeyAsterisk;
|
||||
m_keyMap[XK_KP_Enter] = Fw::KeyEnter;
|
||||
|
||||
// keypad with numlock off
|
||||
m_keyMap[XK_KP_Insert] = Fw::KeyNumpad0;
|
||||
m_keyMap[XK_KP_End] = Fw::KeyNumpad1;
|
||||
m_keyMap[XK_KP_Down] = Fw::KeyNumpad2;
|
||||
m_keyMap[XK_KP_Page_Down] = Fw::KeyNumpad3;
|
||||
m_keyMap[XK_KP_Left] = Fw::KeyNumpad4;
|
||||
m_keyMap[XK_KP_Begin] = Fw::KeyNumpad5;
|
||||
m_keyMap[XK_KP_Right] = Fw::KeyNumpad6;
|
||||
m_keyMap[XK_KP_Home] = Fw::KeyNumpad7;
|
||||
m_keyMap[XK_KP_Up] = Fw::KeyNumpad8;
|
||||
m_keyMap[XK_KP_Page_Up] = Fw::KeyNumpad9;
|
||||
m_keyMap[XK_KP_Delete] = Fw::KeyDelete;
|
||||
|
||||
// keypad with numlock on
|
||||
m_keyMap[XK_KP_0] = Fw::Key0;
|
||||
m_keyMap[XK_KP_1] = Fw::Key1;
|
||||
m_keyMap[XK_KP_2] = Fw::Key2;
|
||||
m_keyMap[XK_KP_3] = Fw::Key3;
|
||||
m_keyMap[XK_KP_4] = Fw::Key4;
|
||||
m_keyMap[XK_KP_5] = Fw::Key5;
|
||||
m_keyMap[XK_KP_6] = Fw::Key6;
|
||||
m_keyMap[XK_KP_7] = Fw::Key7;
|
||||
m_keyMap[XK_KP_8] = Fw::Key8;
|
||||
m_keyMap[XK_KP_9] = Fw::Key9;
|
||||
|
||||
m_keyMap[XK_F1] = Fw::KeyF1;
|
||||
m_keyMap[XK_F2] = Fw::KeyF2;
|
||||
m_keyMap[XK_F3] = Fw::KeyF3;
|
||||
m_keyMap[XK_F4] = Fw::KeyF4;
|
||||
m_keyMap[XK_F5] = Fw::KeyF5;
|
||||
m_keyMap[XK_F6] = Fw::KeyF6;
|
||||
m_keyMap[XK_F7] = Fw::KeyF7;
|
||||
m_keyMap[XK_F8] = Fw::KeyF8;
|
||||
m_keyMap[XK_F9] = Fw::KeyF9;
|
||||
m_keyMap[XK_F10] = Fw::KeyF10;
|
||||
m_keyMap[XK_F11] = Fw::KeyF11;
|
||||
m_keyMap[XK_F12] = Fw::KeyF12;
|
||||
}
|
||||
|
||||
void X11Window::init()
|
||||
{
|
||||
m_size = Size(200, 200);
|
||||
internalOpenDisplay();
|
||||
internalCheckGL();
|
||||
internalChooseGLVisual();
|
||||
internalCreateGLContext();
|
||||
internalCreateWindow();
|
||||
}
|
||||
|
||||
void X11Window::terminate()
|
||||
{
|
||||
XDestroyWindow(m_display, m_window);
|
||||
|
||||
if(m_colormap)
|
||||
XFreeColormap(m_display, m_colormap);
|
||||
|
||||
internalDestroyGLContext();
|
||||
|
||||
if(m_visual)
|
||||
XFree(m_visual);
|
||||
|
||||
if(m_xic)
|
||||
XDestroyIC(m_xic);
|
||||
|
||||
if(m_xim)
|
||||
XCloseIM(m_xim);
|
||||
|
||||
XCloseDisplay(m_display);
|
||||
|
||||
m_visible = false;
|
||||
}
|
||||
|
||||
void X11Window::internalOpenDisplay()
|
||||
{
|
||||
m_display = XOpenDisplay(NULL);
|
||||
if(!m_display)
|
||||
logFatal("Unable to open X11 display");
|
||||
m_screen = DefaultScreen(m_display);
|
||||
}
|
||||
|
||||
void X11Window::internalCreateWindow()
|
||||
{
|
||||
Visual *vis;
|
||||
int depth;
|
||||
unsigned int attrsMask = CWEventMask;
|
||||
XSetWindowAttributes attrs = {0};
|
||||
|
||||
attrs.event_mask = KeyPressMask | KeyReleaseMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
|
||||
ExposureMask | VisibilityChangeMask |
|
||||
StructureNotifyMask | FocusChangeMask;
|
||||
|
||||
if(m_visual) {
|
||||
m_colormap = XCreateColormap(m_display, m_rootWindow, m_visual->visual, AllocNone);
|
||||
attrs.colormap = m_colormap;
|
||||
attrs.border_pixel = 0;
|
||||
attrsMask |= CWBorderPixel | CWColormap;
|
||||
vis = m_visual->visual;
|
||||
depth = m_visual->depth;
|
||||
} else {
|
||||
attrs.override_redirect = False;
|
||||
attrsMask |= CWOverrideRedirect;
|
||||
depth = CopyFromParent;
|
||||
vis = CopyFromParent;
|
||||
}
|
||||
|
||||
m_window = XCreateWindow(m_display, m_rootWindow,
|
||||
m_pos.x, m_pos.y, m_size.width(), m_size.height(),
|
||||
0,
|
||||
depth,
|
||||
InputOutput,
|
||||
vis,
|
||||
attrsMask, &attrs);
|
||||
if(!m_window)
|
||||
logFatal("Unable to create X11 window!");
|
||||
|
||||
// ensure window input focus
|
||||
XWMHints hints;
|
||||
hints.input = True;
|
||||
hints.flags = InputHint;
|
||||
XSetWMHints(m_display, m_window, &hints);
|
||||
|
||||
// ensure window position
|
||||
XMoveWindow(m_display, m_window, m_pos.x, m_pos.y);
|
||||
|
||||
// handle wm_delete events
|
||||
m_wmDelete = XInternAtom(m_display, "WM_DELETE_WINDOW", True);
|
||||
XSetWMProtocols(m_display, m_window, &m_wmDelete , 1);
|
||||
|
||||
if(!internalSetupWindowInput())
|
||||
logWarning("Input of special keys may be messed up, because window input initialization failed");
|
||||
|
||||
internalConnectGLContext();
|
||||
}
|
||||
|
||||
bool X11Window::internalSetupWindowInput()
|
||||
{
|
||||
// try to set a latin1 locales, otherwise fallback to standard C locale
|
||||
static char locales[4][32] = { "en_US.iso88591", "iso88591", "en_US", "C" };
|
||||
for(int i=0;i<4;++i) {
|
||||
if(setlocale(LC_ALL, locales[i]))
|
||||
break;
|
||||
}
|
||||
|
||||
// create input context (to have better key input handling)
|
||||
if(!XSupportsLocale()) {
|
||||
logError("X11 doesn't support the current locale");
|
||||
return false;
|
||||
}
|
||||
|
||||
XSetLocaleModifiers("");
|
||||
m_xim = XOpenIM(m_display, NULL, NULL, NULL);
|
||||
if(!m_xim) {
|
||||
logError("XOpenIM failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_xic = XCreateIC(m_xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, m_window, NULL);
|
||||
if(!m_xic) {
|
||||
logError("Unable to create the input context");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void X11Window::internalCheckGL()
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
if(!glXQueryExtension(m_display, NULL, NULL))
|
||||
logFatal("GLX not supported");
|
||||
#else
|
||||
m_eglDisplay = eglGetDisplay((EGLNativeDisplayType)m_display);
|
||||
if(m_eglDisplay == EGL_NO_DISPLAY)
|
||||
logFatal("EGL not supported");
|
||||
|
||||
if(!eglInitialize(m_eglDisplay, NULL, NULL))
|
||||
logFatal("Unable to initialize EGL");
|
||||
#endif
|
||||
}
|
||||
|
||||
void X11Window::internalChooseGLVisual()
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
static int attrList[] = {
|
||||
GLX_USE_GL,
|
||||
GLX_RGBA,
|
||||
GLX_DOUBLEBUFFER,
|
||||
None
|
||||
};
|
||||
|
||||
m_visual = glXChooseVisual(m_display, m_screen, attrList);
|
||||
if(!m_visual)
|
||||
logFatal("Couldn't choose RGBA, double buffered visual");
|
||||
|
||||
m_rootWindow = RootWindow(m_display, m_visual->screen);
|
||||
#else
|
||||
static int attrList[] = {
|
||||
EGL_BUFFER_SIZE, 16,
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
EGLint numConfig;
|
||||
if(!eglChooseConfig(m_eglDisplay, attrList, &m_eglConfig, 1, &numConfig))
|
||||
logFatal("Failed to choose EGL config");
|
||||
|
||||
if(numConfig != 1)
|
||||
logWarning("Didn't got the exact EGL config");
|
||||
|
||||
m_rootWindow = DefaultRootWindow(m_display);
|
||||
#endif
|
||||
}
|
||||
|
||||
void X11Window::internalCreateGLContext()
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
m_glxContext = glXCreateContext(m_display, m_visual, NULL, True);
|
||||
if(!m_glxContext)
|
||||
logFatal("Unable to create GLX context");
|
||||
|
||||
if(!glXIsDirect(m_display, m_glxContext))
|
||||
logWarning("GL direct rendering is not possible");
|
||||
#else
|
||||
EGLint attrList[] = {
|
||||
EGL_CONTEXT_CLIENT_VERSION, 2,
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, EGL_NO_CONTEXT, attrList);
|
||||
if(m_eglContext == EGL_NO_CONTEXT )
|
||||
logFatal("Unable to create EGL context: ", eglGetError());
|
||||
#endif
|
||||
}
|
||||
|
||||
void X11Window::internalDestroyGLContext()
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
glXMakeCurrent(m_display, None, NULL);
|
||||
glXDestroyContext(m_display, m_glxContext);
|
||||
#else
|
||||
eglDestroyContext(m_eglDisplay, m_eglContext);
|
||||
eglDestroySurface(m_eglDisplay, m_eglSurface);
|
||||
eglTerminate(m_eglDisplay);
|
||||
#endif
|
||||
}
|
||||
|
||||
void X11Window::internalConnectGLContext()
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
if(!glXMakeCurrent(m_display, m_window, m_glxContext))
|
||||
logFatal("Unable to set GLX context on X11 window");
|
||||
#else
|
||||
m_eglSurface = eglCreateWindowSurface(m_eglDisplay, m_eglConfig, m_window, NULL);
|
||||
if(m_eglSurface == EGL_NO_SURFACE)
|
||||
logFatal("Unable to create EGL surface: ", eglGetError());
|
||||
if(!eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext))
|
||||
logFatal("Unable to connect EGL context into X11 window");
|
||||
#endif
|
||||
}
|
||||
|
||||
void *X11Window::getExtensionProcAddress(const char *ext)
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
return (void *)glXGetProcAddressARB((const GLubyte*)ext);
|
||||
#else
|
||||
//TODO
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool X11Window::isExtensionSupported(const char *ext)
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
const char *exts = glXQueryExtensionsString(m_display, m_screen);
|
||||
if(strstr(exts, ext))
|
||||
return true;
|
||||
#else
|
||||
//TODO
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
void X11Window::move(const Point& pos)
|
||||
{
|
||||
XMoveWindow(m_display, m_window, pos.x, pos.y);
|
||||
}
|
||||
|
||||
void X11Window::resize(const Size& size)
|
||||
{
|
||||
XResizeWindow(m_display, m_window, size.width(), size.height());
|
||||
}
|
||||
|
||||
void X11Window::show()
|
||||
{
|
||||
XMapWindow(m_display, m_window);
|
||||
}
|
||||
|
||||
void X11Window::hide()
|
||||
{
|
||||
XUnmapWindow(m_display, m_window);
|
||||
}
|
||||
|
||||
void X11Window::maximize()
|
||||
{
|
||||
Atom wmState = XInternAtom(m_display, "_NET_WM_STATE", False);
|
||||
Atom wmStateMaximizedVert = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
|
||||
Atom wmStateMaximizedHorz = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
|
||||
|
||||
XEvent e = {0};
|
||||
e.xany.type = ClientMessage;
|
||||
e.xclient.send_event = True;
|
||||
e.xclient.message_type = wmState;
|
||||
e.xclient.format = 32;
|
||||
e.xclient.window = m_window;
|
||||
e.xclient.data.l[0] = 1;
|
||||
e.xclient.data.l[1] = wmStateMaximizedVert;
|
||||
e.xclient.data.l[2] = wmStateMaximizedHorz;
|
||||
e.xclient.data.l[3] = 0;
|
||||
|
||||
XSendEvent(m_display, m_rootWindow, 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
|
||||
XFlush(m_display);
|
||||
}
|
||||
|
||||
void X11Window::poll()
|
||||
{
|
||||
bool needsResizeUpdate = false;
|
||||
|
||||
XEvent event, peekEvent;
|
||||
while(XPending(m_display) > 0) {
|
||||
XNextEvent(m_display, &event);
|
||||
|
||||
// call filter because xim will discard KeyPress events when keys still composing
|
||||
if(XFilterEvent(&event, m_window))
|
||||
continue;
|
||||
|
||||
// discard events of repeated key releases
|
||||
if(event.type == KeyRelease && XPending(m_display)) {
|
||||
XPeekEvent(m_display, &peekEvent);
|
||||
if((peekEvent.type == KeyPress) &&
|
||||
(peekEvent.xkey.keycode == event.xkey.keycode) &&
|
||||
((peekEvent.xkey.time-event.xkey.time) < 2))
|
||||
continue;
|
||||
}
|
||||
|
||||
// reset inputEvent values, except keyboardModifiers and mousePos
|
||||
m_inputEvent.type = Fw::NoInputEvent;
|
||||
m_inputEvent.mouseButton = Fw::MouseNoButton;
|
||||
m_inputEvent.keyCode = Fw::KeyUnknown;
|
||||
m_inputEvent.keyText = "";
|
||||
m_inputEvent.mouseMoved = Point();
|
||||
m_inputEvent.wheelDirection = Fw::MouseNoWheel;
|
||||
|
||||
switch(event.type) {
|
||||
case ClientMessage: {
|
||||
// close event
|
||||
if((Atom)event.xclient.data.l[0] == m_wmDelete && m_onClose)
|
||||
m_onClose();
|
||||
break;
|
||||
}
|
||||
case ConfigureNotify: {
|
||||
Size newSize(event.xconfigure.width, event.xconfigure.height);
|
||||
Point newPos(event.xconfigure.x, event.xconfigure.y);
|
||||
|
||||
// updates window size
|
||||
if(m_size != newSize) {
|
||||
m_size = newSize;
|
||||
needsResizeUpdate = true;
|
||||
}
|
||||
|
||||
// updates window pos
|
||||
if(m_pos != newPos && !isMaximized())
|
||||
m_pos = newPos;
|
||||
break;
|
||||
}
|
||||
case SelectionRequest: {
|
||||
XEvent respond;
|
||||
XSelectionRequestEvent *req = &(event.xselectionrequest);
|
||||
|
||||
Atom targets = XInternAtom(m_display, "TARGETS", False);
|
||||
if(req->target == targets) {
|
||||
Atom typeList[] = { XInternAtom(m_display, "UTF8_STRING", False),
|
||||
XInternAtom(m_display, "TEXT", False),
|
||||
XInternAtom(m_display, "COMPOUND_TEXT", False),
|
||||
XA_STRING };
|
||||
|
||||
XChangeProperty(m_display, req->requestor,
|
||||
req->property, req->target,
|
||||
8, PropModeReplace,
|
||||
(uchar *)&typeList,
|
||||
sizeof(typeList));
|
||||
respond.xselection.property = req->property;
|
||||
} else {
|
||||
XChangeProperty(m_display,
|
||||
req->requestor,
|
||||
req->property, req->target,
|
||||
8,
|
||||
PropModeReplace,
|
||||
(uchar *)m_clipboardText.c_str(),
|
||||
m_clipboardText.length());
|
||||
respond.xselection.property = req->property;
|
||||
}
|
||||
|
||||
respond.xselection.type = SelectionNotify;
|
||||
respond.xselection.display = req->display;
|
||||
respond.xselection.requestor = req->requestor;
|
||||
respond.xselection.selection = req->selection;
|
||||
respond.xselection.target = req->target;
|
||||
respond.xselection.time = req->time;
|
||||
XSendEvent(m_display, req->requestor, 0, 0, &respond);
|
||||
XFlush(m_display);
|
||||
break;
|
||||
}
|
||||
case KeyPress:
|
||||
case KeyRelease: {
|
||||
KeySym keysym;
|
||||
char buf[32];
|
||||
int len;
|
||||
|
||||
m_inputEvent.keyboardModifiers = 0;
|
||||
if(event.xkey.state & ControlMask)
|
||||
m_inputEvent.keyboardModifiers |= Fw::KeyboardCtrlModifier;
|
||||
if(event.xkey.state & ShiftMask)
|
||||
m_inputEvent.keyboardModifiers |= Fw::KeyboardShiftModifier;
|
||||
if(event.xkey.state & Mod1Mask)
|
||||
m_inputEvent.keyboardModifiers |= Fw::KeyboardAltModifier;
|
||||
|
||||
// lookup for keyText
|
||||
if(event.type == KeyPress && !(event.xkey.state & ControlMask) && !(event.xkey.state & Mod1Mask)) {
|
||||
if(m_xic) { // with xim we can get latin1 input correctly
|
||||
Status status;
|
||||
len = XmbLookupString(m_xic, &event.xkey, buf, sizeof(buf), &keysym, &status);
|
||||
} else { // otherwise use XLookupString, but often it doesn't work right with dead keys
|
||||
static XComposeStatus compose = {NULL, 0};
|
||||
len = XLookupString(&event.xkey, buf, sizeof(buf), &keysym, &compose);
|
||||
}
|
||||
|
||||
if(len > 0 &&
|
||||
// these keys produces characters that we don't want to capture
|
||||
keysym != XK_BackSpace &&
|
||||
keysym != XK_Return &&
|
||||
keysym != XK_Delete &&
|
||||
keysym != XK_Escape &&
|
||||
(uchar)(buf[0]) >= 32
|
||||
) {
|
||||
//logDebug("char: ", buf[0], " code: ", (uint)buf[0]);
|
||||
m_inputEvent.keyText = buf;
|
||||
}
|
||||
}
|
||||
|
||||
if(m_keyMap.find(keysym) != m_keyMap.end())
|
||||
m_inputEvent.keyCode = m_keyMap[keysym];
|
||||
|
||||
m_inputEvent.type = (event.type == KeyPress) ? Fw::KeyPressInputEvent : Fw::KeyReleaseInputEvent;
|
||||
if(m_inputEvent.keyCode != Fw::KeyUnknown || !m_inputEvent.keyText.empty())
|
||||
m_onInputEvent(m_inputEvent);
|
||||
break;
|
||||
}
|
||||
case ButtonPress:
|
||||
case ButtonRelease: {
|
||||
m_inputEvent.type = (event.type == ButtonPress) ? Fw::MousePressInputEvent : Fw::MouseReleaseInputEvent;
|
||||
switch(event.xbutton.button) {
|
||||
case Button1:
|
||||
m_inputEvent.mouseButton = Fw::MouseLeftButton;
|
||||
break;
|
||||
case Button3:
|
||||
m_inputEvent.mouseButton = Fw::MouseRightButton;
|
||||
break;
|
||||
case Button2:
|
||||
m_inputEvent.mouseButton = Fw::MouseMidButton;
|
||||
break;
|
||||
case Button4:
|
||||
m_inputEvent.type = Fw::MouseWheelInputEvent;
|
||||
m_inputEvent.mouseButton = Fw::MouseMidButton;
|
||||
m_inputEvent.wheelDirection = Fw::MouseWheelUp;
|
||||
break;
|
||||
case Button5:
|
||||
m_inputEvent.type = Fw::MouseWheelInputEvent;
|
||||
m_inputEvent.mouseButton = Fw::MouseMidButton;
|
||||
m_inputEvent.wheelDirection = Fw::MouseWheelDown;
|
||||
break;
|
||||
default:
|
||||
m_inputEvent.type = Fw::NoInputEvent;
|
||||
break;
|
||||
}
|
||||
if(m_inputEvent.type != Fw::NoInputEvent && m_onInputEvent)
|
||||
m_onInputEvent(m_inputEvent);
|
||||
break;
|
||||
}
|
||||
|
||||
case MotionNotify: {
|
||||
m_inputEvent.type = Fw::MouseMoveInputEvent;
|
||||
Point newMousePos(event.xbutton.x, event.xbutton.y);
|
||||
m_inputEvent.mouseMoved = newMousePos - m_inputEvent.mousePos;
|
||||
m_inputEvent.mousePos = newMousePos;
|
||||
if(m_onInputEvent)
|
||||
m_onInputEvent(m_inputEvent);
|
||||
break;
|
||||
}
|
||||
case MapNotify:
|
||||
m_visible = true;
|
||||
break;
|
||||
case UnmapNotify:
|
||||
m_visible = false;
|
||||
break;
|
||||
case FocusIn:
|
||||
m_focused = true;
|
||||
break;
|
||||
case FocusOut:
|
||||
m_focused = false;
|
||||
break;
|
||||
case Expose:
|
||||
// window needs redraw
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(needsResizeUpdate && m_onResize)
|
||||
m_onResize(m_size);
|
||||
}
|
||||
|
||||
void X11Window::swapBuffers()
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
glXSwapBuffers(m_display, m_window);
|
||||
#else
|
||||
eglSwapBuffers(m_eglDisplay, m_eglSurface);
|
||||
#endif
|
||||
}
|
||||
|
||||
void X11Window::showMouse()
|
||||
{
|
||||
XUndefineCursor(m_display, m_window);
|
||||
if(m_cursor != None) {
|
||||
XFreeCursor(m_display, m_cursor);
|
||||
m_cursor = None;
|
||||
}
|
||||
}
|
||||
|
||||
void X11Window::hideMouse()
|
||||
{
|
||||
if(m_cursor == None) {
|
||||
char bm[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
Pixmap pix = XCreateBitmapFromData(m_display, m_window, bm, 8, 8);
|
||||
XColor black = {0};
|
||||
black.flags = DoRed | DoGreen | DoBlue;
|
||||
m_cursor = XCreatePixmapCursor(m_display, pix, pix, &black, &black, 0, 0);
|
||||
XFreePixmap(m_display, pix);
|
||||
}
|
||||
XDefineCursor(m_display, m_window, m_cursor);
|
||||
}
|
||||
|
||||
void X11Window::setTitle(const std::string& title)
|
||||
{
|
||||
XStoreName(m_display, m_window, title.c_str());
|
||||
XSetIconName(m_display, m_window, title.c_str());
|
||||
}
|
||||
|
||||
void X11Window::setMinimumSize(const Size& minimumSize)
|
||||
{
|
||||
XSizeHints sizeHints = {0};
|
||||
sizeHints.flags = PMinSize;
|
||||
sizeHints.min_width = minimumSize.width();
|
||||
sizeHints.min_height= minimumSize.height();
|
||||
XSetWMSizeHints(m_display, m_window, &sizeHints, XA_WM_NORMAL_HINTS);
|
||||
}
|
||||
|
||||
void X11Window::setFullscreen(bool fullscreen)
|
||||
{
|
||||
if(m_fullscreen != fullscreen) {
|
||||
Atom wmState = XInternAtom(m_display, "_NET_WM_STATE", False);
|
||||
Atom wmStateFullscreen = XInternAtom(m_display, "_NET_WM_STATE_FULLSCREEN", False);
|
||||
|
||||
XEvent xev;
|
||||
xev.xclient.type = ClientMessage;
|
||||
xev.xclient.serial = 0;
|
||||
xev.xclient.send_event = True;
|
||||
xev.xclient.window = m_window;
|
||||
xev.xclient.message_type = wmState;
|
||||
xev.xclient.format = 32;
|
||||
xev.xclient.data.l[0] = fullscreen ? 1 : 0;
|
||||
xev.xclient.data.l[1] = wmStateFullscreen;
|
||||
xev.xclient.data.l[2] = 0;
|
||||
|
||||
XSendEvent(m_display, m_rootWindow, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
|
||||
XFlush(m_display);
|
||||
|
||||
m_fullscreen = fullscreen;
|
||||
}
|
||||
}
|
||||
|
||||
void X11Window::setVerticalSync(bool enable)
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
typedef GLint (*glSwapIntervalProc)(GLint);
|
||||
glSwapIntervalProc glSwapInterval = NULL;
|
||||
|
||||
if(isExtensionSupported("GLX_MESA_swap_control"))
|
||||
glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalMESA");
|
||||
else if(isExtensionSupported("GLX_SGI_swap_control"))
|
||||
glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalSGI");
|
||||
|
||||
if(glSwapInterval)
|
||||
glSwapInterval(enable ? 1 : 0);
|
||||
#else
|
||||
//TODO
|
||||
#endif
|
||||
}
|
||||
|
||||
void X11Window::setIcon(const std::string& iconFile)
|
||||
{
|
||||
std::stringstream fin;
|
||||
g_resources.loadFile(iconFile, fin);
|
||||
|
||||
apng_data apng;
|
||||
if(load_apng(fin, &apng) != 0) {
|
||||
logError("Unable to load window icon");
|
||||
return;
|
||||
}
|
||||
|
||||
if(apng.bpp != 4) {
|
||||
logError("Could not set window icon, icon image must have 4 channels");
|
||||
free_apng(&apng);
|
||||
return;
|
||||
}
|
||||
|
||||
int n = apng.width * apng.height;
|
||||
std::vector<unsigned long int> iconData(n + 2);
|
||||
iconData[0] = apng.width;
|
||||
iconData[1] = apng.height;
|
||||
for(int i=0; i < n;++i) {
|
||||
uint8 *pixel = (uint8*)&iconData[2 + i];
|
||||
pixel[2] = *(apng.pdata + (i * 4) + 0);
|
||||
pixel[1] = *(apng.pdata + (i * 4) + 1);
|
||||
pixel[0] = *(apng.pdata + (i * 4) + 2);
|
||||
pixel[3] = *(apng.pdata + (i * 4) + 3);
|
||||
}
|
||||
|
||||
Atom property = XInternAtom(m_display, "_NET_WM_ICON", 0);
|
||||
if(!XChangeProperty(m_display, m_window, property, XA_CARDINAL, 32, PropModeReplace, (const unsigned char*)&iconData[0], iconData.size()))
|
||||
logError("Couldn't set app icon");
|
||||
|
||||
free_apng(&apng);
|
||||
}
|
||||
|
||||
void X11Window::setClipboardText(const std::string& text)
|
||||
{
|
||||
m_clipboardText = text;
|
||||
Atom clipboard = XInternAtom(m_display, "CLIPBOARD", False);
|
||||
XSetSelectionOwner(m_display, clipboard, m_window, CurrentTime);
|
||||
XFlush(m_display);
|
||||
}
|
||||
|
||||
Size X11Window::getDisplaySize()
|
||||
{
|
||||
return Size(XDisplayWidth(m_display, m_screen), XDisplayHeight(m_display, m_screen));
|
||||
}
|
||||
|
||||
std::string X11Window::getClipboardText()
|
||||
{
|
||||
Atom clipboard = XInternAtom(m_display, "CLIPBOARD", False);
|
||||
Window ownerWindow = XGetSelectionOwner(m_display, clipboard);
|
||||
if(ownerWindow == m_window)
|
||||
return m_clipboardText;
|
||||
|
||||
std::string clipboardText;
|
||||
if(ownerWindow != None) {
|
||||
XConvertSelection(m_display, clipboard, XA_STRING, 0, ownerWindow, CurrentTime);
|
||||
XFlush(m_display);
|
||||
|
||||
// hack to wait SelectioNotify event, otherwise we will get wrong clipboard pastes
|
||||
// TODO: fix this in a correct way
|
||||
usleep(100 * 1000);
|
||||
|
||||
// check for data
|
||||
Atom type;
|
||||
int format;
|
||||
ulong numItems, bytesLeft, dummy;
|
||||
uchar *data;
|
||||
XGetWindowProperty(m_display, ownerWindow,
|
||||
XA_STRING,
|
||||
0, 0, 0,
|
||||
AnyPropertyType,
|
||||
&type,
|
||||
&format,
|
||||
&numItems,
|
||||
&bytesLeft,
|
||||
&data);
|
||||
if(bytesLeft > 0) {
|
||||
// get the data get
|
||||
int result = XGetWindowProperty(m_display, ownerWindow,
|
||||
XA_STRING,
|
||||
0,
|
||||
bytesLeft,
|
||||
0,
|
||||
AnyPropertyType,
|
||||
&type,
|
||||
&format,
|
||||
&numItems,
|
||||
&dummy,
|
||||
&data);
|
||||
if(result == Success)
|
||||
clipboardText = (const char*)data;
|
||||
XFree(data);
|
||||
}
|
||||
}
|
||||
|
||||
return clipboardText;
|
||||
}
|
||||
|
||||
bool X11Window::isMaximized()
|
||||
{
|
||||
Atom wmState = XInternAtom(m_display, "_NET_WM_STATE", False);
|
||||
Atom wmStateMaximizedVert = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
|
||||
Atom wmStateMaximizedHorz = XInternAtom(m_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
|
||||
Atom actualType;
|
||||
ulong i, numItems, bytesAfter;
|
||||
uchar *propertyValue = NULL;
|
||||
int actualFormat;
|
||||
bool maximized = false;
|
||||
|
||||
if(XGetWindowProperty(m_display, m_window, wmState,
|
||||
0, 1024, False, XA_ATOM, &actualType,
|
||||
&actualFormat, &numItems, &bytesAfter,
|
||||
&propertyValue) == Success) {
|
||||
Atom *atoms = (Atom*)propertyValue;
|
||||
int maximizedMask = 0;
|
||||
|
||||
for(i=0; i<numItems; ++i) {
|
||||
if(atoms[i] == wmStateMaximizedVert)
|
||||
maximizedMask |= 1;
|
||||
else if(atoms[i] == wmStateMaximizedHorz)
|
||||
maximizedMask |= 2;
|
||||
}
|
||||
|
||||
if(maximizedMask == 3)
|
||||
maximizedMask = true;
|
||||
|
||||
XFree(propertyValue);
|
||||
}
|
||||
|
||||
return maximized;
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef X11WINDOW_H
|
||||
#define X11WINDOW_H
|
||||
|
||||
#include "platformwindow.h"
|
||||
#include <framework/graphics/glutil.h>
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xatom.h>
|
||||
|
||||
#ifndef OPENGL_ES2
|
||||
#include <GL/glx.h>
|
||||
#else
|
||||
#include <EGL/egl.h>
|
||||
#endif
|
||||
|
||||
class X11Window : public PlatformWindow
|
||||
{
|
||||
void internalOpenDisplay();
|
||||
void internalCreateWindow();
|
||||
bool internalSetupWindowInput();
|
||||
|
||||
void internalCheckGL();
|
||||
void internalChooseGLVisual();
|
||||
void internalCreateGLContext();
|
||||
void internalDestroyGLContext();
|
||||
void internalConnectGLContext();
|
||||
|
||||
void *getExtensionProcAddress(const char *ext);
|
||||
bool isExtensionSupported(const char *ext);
|
||||
|
||||
public:
|
||||
X11Window();
|
||||
|
||||
void init();
|
||||
void terminate();
|
||||
|
||||
void move(const Point& pos);
|
||||
void resize(const Size& size);
|
||||
void show();
|
||||
void hide();
|
||||
void maximize();
|
||||
void poll();
|
||||
void swapBuffers();
|
||||
void showMouse();
|
||||
void hideMouse();
|
||||
|
||||
void setTitle(const std::string& title);
|
||||
void setMinimumSize(const Size& minimumSize);
|
||||
void setFullscreen(bool fullscreen);
|
||||
void setVerticalSync(bool enable);
|
||||
void setIcon(const std::string& iconFile);
|
||||
void setClipboardText(const std::string& text);
|
||||
|
||||
Size getDisplaySize();
|
||||
std::string getClipboardText();
|
||||
|
||||
bool isMaximized();
|
||||
|
||||
private:
|
||||
Display *m_display;
|
||||
XVisualInfo *m_visual;
|
||||
Window m_window;
|
||||
Window m_rootWindow;
|
||||
Colormap m_colormap;
|
||||
Cursor m_cursor;
|
||||
XIM m_xim;
|
||||
XIC m_xic;
|
||||
int m_screen;
|
||||
Atom m_wmDelete;
|
||||
std::string m_clipboardText;
|
||||
std::map<int, Fw::Key> m_keyMap;
|
||||
|
||||
#ifndef OPENGL_ES2
|
||||
GLXContext m_glxContext;
|
||||
#else
|
||||
EGLConfig m_eglConfig;
|
||||
EGLContext m_eglContext;
|
||||
EGLDisplay m_eglDisplay;
|
||||
EGLSurface m_eglSurface;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,304 +1,10 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "otclient.h"
|
||||
|
||||
#include <framework/core/modulemanager.h>
|
||||
#include <framework/core/configmanager.h>
|
||||
#include <framework/core/resourcemanager.h>
|
||||
#include <framework/core/eventdispatcher.h>
|
||||
#include <framework/core/clock.h>
|
||||
#include <framework/luascript/luainterface.h>
|
||||
#include <framework/graphics/graphics.h>
|
||||
#include <framework/graphics/fontmanager.h>
|
||||
#include <framework/ui/uimanager.h>
|
||||
#include <framework/ui/uiwidget.h>
|
||||
#include <framework/net/connection.h>
|
||||
#include <framework/platform/platform.h>
|
||||
#include <otclient/net/protocolgame.h>
|
||||
#include <otclient/core/game.h>
|
||||
#include <otclient/core/map.h>
|
||||
|
||||
OTClient g_client;
|
||||
OTClient g_otclient;
|
||||
Application& g_app = g_otclient;
|
||||
|
||||
void OTClient::init(std::vector<std::string> args)
|
||||
void OTClient::init(const std::vector<std::string>& args)
|
||||
{
|
||||
m_running = false;
|
||||
m_stopping = false;
|
||||
|
||||
// print client information
|
||||
logInfo(Otc::AppName, " ", Otc::AppVersion);
|
||||
|
||||
// initialize platform related stuff
|
||||
g_platform.init(this, Otc::AppName);
|
||||
|
||||
// initialize script environment
|
||||
g_lua.init();
|
||||
|
||||
// register otclient lua functions
|
||||
registerLuaFunctions();
|
||||
|
||||
// initialize resources
|
||||
g_resources.init(args[0].c_str(), Otc::AppPathName);
|
||||
|
||||
// load configurations
|
||||
loadConfigurations();
|
||||
|
||||
// create the client window
|
||||
//TODO: bind these functions and move to otclient module
|
||||
int minWidth = 550;
|
||||
int minHeight = 450;
|
||||
int windowX = Fw::fromstring(g_configs.get("window x"), 0);
|
||||
int windowY = Fw::fromstring(g_configs.get("window y"), 0);
|
||||
int windowWidth = Fw::fromstring(g_configs.get("window width"), minWidth);
|
||||
int windowHeight = Fw::fromstring(g_configs.get("window height"), minHeight);
|
||||
bool maximized = Fw::fromstring(g_configs.get("window maximized"), false);
|
||||
g_platform.createWindow(windowX, windowY, windowWidth, windowHeight, minWidth, minHeight, maximized);
|
||||
g_platform.setWindowTitle(Otc::AppName);
|
||||
g_platform.setWindowIcon("otclient/otcicon.png");
|
||||
|
||||
// initialize graphics
|
||||
g_graphics.init();
|
||||
|
||||
// initialize the ui
|
||||
g_ui.init();
|
||||
|
||||
g_game.init();
|
||||
|
||||
// discover and load modules
|
||||
g_modules.discoverAndLoadModules();
|
||||
g_modules.getModule("otclient")->load();
|
||||
ModulePtr tibiaFilesModule = g_modules.getModule("tibiafiles");
|
||||
if(!tibiaFilesModule || !tibiaFilesModule->isLoaded()) {
|
||||
logFatal("Could not find or load 'tibiafiles' module, this module contains SPR and DAT files.\n"
|
||||
"Please download it and place in modules directory, then try running again.");
|
||||
}
|
||||
|
||||
// now that everything is initialized, setup configurations
|
||||
setupConfigurations();
|
||||
|
||||
// now that everything is loaded, show the window
|
||||
g_platform.showWindow();
|
||||
}
|
||||
|
||||
void OTClient::run()
|
||||
{
|
||||
int frameTicks = g_clock.ticks();
|
||||
int lastPollTicks = frameTicks;
|
||||
int frameCount = 0;
|
||||
|
||||
m_stopping = false;
|
||||
m_running = true;
|
||||
|
||||
if(g_ui.getRootWidget()->getChildCount() == 0) {
|
||||
logError("There is no root widgets to display, the app will close");
|
||||
m_stopping = true;
|
||||
}
|
||||
|
||||
// run the first poll
|
||||
poll();
|
||||
|
||||
while(!m_stopping) {
|
||||
//g_platform.sleep(150);
|
||||
frameTicks = g_clock.updateTicks();
|
||||
|
||||
// poll events every POLL_CYCLE_DELAY
|
||||
// this delay exists to avoid massive polling thus increasing framerate
|
||||
if(frameTicks - lastPollTicks >= POLL_CYCLE_DELAY) {
|
||||
poll();
|
||||
lastPollTicks = frameTicks;
|
||||
}
|
||||
|
||||
// only render when the windows is visible
|
||||
if(g_platform.isWindowVisible()) {
|
||||
// render begin
|
||||
g_graphics.beginRender();
|
||||
|
||||
// render everything
|
||||
render();
|
||||
|
||||
// render end
|
||||
g_graphics.endRender();
|
||||
|
||||
// swap the old buffer with the new rendered
|
||||
g_platform.swapBuffers();
|
||||
} else {
|
||||
// sleeps until next poll to avoid massive cpu usage
|
||||
g_clock.sleep(POLL_CYCLE_DELAY+1);
|
||||
}
|
||||
}
|
||||
|
||||
m_stopping = false;
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
void OTClient::terminate()
|
||||
{
|
||||
// hide the window because there is no render anymore
|
||||
g_platform.hideWindow();
|
||||
|
||||
g_game.terminate();
|
||||
|
||||
// run modules unload event
|
||||
g_modules.unloadModules();
|
||||
|
||||
// terminate ui
|
||||
g_ui.terminate();
|
||||
|
||||
// release remaining lua object references
|
||||
g_lua.collectGarbage();
|
||||
|
||||
// poll remaining events
|
||||
poll();
|
||||
|
||||
// terminate network
|
||||
Connection::terminate();
|
||||
|
||||
// flush remaining dispatcher events
|
||||
g_dispatcher.flush();
|
||||
|
||||
// terminate graphics
|
||||
g_graphics.terminate();
|
||||
|
||||
// save configurations
|
||||
saveConfigurations();
|
||||
|
||||
// release resources
|
||||
g_resources.terminate();
|
||||
|
||||
// terminate script environment
|
||||
g_lua.terminate();
|
||||
|
||||
// end platform related stuff
|
||||
g_platform.terminate();
|
||||
|
||||
}
|
||||
|
||||
void OTClient::exit()
|
||||
{
|
||||
// stops the main loop
|
||||
m_stopping = true;
|
||||
}
|
||||
|
||||
void OTClient::poll()
|
||||
{
|
||||
// poll platform events
|
||||
g_platform.poll();
|
||||
|
||||
// poll network events
|
||||
Connection::poll();
|
||||
|
||||
// poll dispatcher events
|
||||
g_dispatcher.poll();
|
||||
}
|
||||
|
||||
void OTClient::render()
|
||||
{
|
||||
// everything is rendered by UI components
|
||||
g_ui.render();
|
||||
}
|
||||
|
||||
void OTClient::loadConfigurations()
|
||||
{
|
||||
// default window size
|
||||
int defWidth = 550;
|
||||
int defHeight = 450;
|
||||
|
||||
// sets default window configuration
|
||||
g_configs.set("window x", Fw::tostring((g_platform.getDisplayWidth() - defWidth)/2));
|
||||
g_configs.set("window y", Fw::tostring((g_platform.getDisplayHeight() - defHeight)/2));
|
||||
g_configs.set("window width", Fw::tostring(defWidth));
|
||||
g_configs.set("window height", Fw::tostring(defHeight));
|
||||
g_configs.set("window maximized", Fw::tostring(false));
|
||||
g_configs.set("vsync", Fw::tostring(true));
|
||||
|
||||
// loads user configuration
|
||||
if(!g_configs.load("config.otml"))
|
||||
logInfo("Using default configurations.");
|
||||
}
|
||||
|
||||
void OTClient::setupConfigurations()
|
||||
{
|
||||
// activate vertical synchronization?
|
||||
bool vsync = Fw::fromstring(g_configs.get("vsync"), true);
|
||||
g_platform.setVerticalSync(vsync);
|
||||
}
|
||||
|
||||
void OTClient::saveConfigurations()
|
||||
{
|
||||
g_configs.set("window x", Fw::tostring(g_platform.getWindowX()));
|
||||
g_configs.set("window y", Fw::tostring(g_platform.getWindowY()));
|
||||
g_configs.set("window width", Fw::tostring(g_platform.getWindowWidth()));
|
||||
g_configs.set("window height", Fw::tostring(g_platform.getWindowHeight()));
|
||||
g_configs.set("window maximized", Fw::tostring(g_platform.isWindowMaximized()));
|
||||
|
||||
// saves user configuration
|
||||
if(!g_configs.save())
|
||||
logError("Configurations are lost because it couldn't be saved");
|
||||
}
|
||||
|
||||
void OTClient::onClose()
|
||||
{
|
||||
//TODO: make and use g_lua.callGlobalField
|
||||
if(m_onCloseCallback)
|
||||
m_onCloseCallback();
|
||||
else
|
||||
exit();
|
||||
}
|
||||
|
||||
void OTClient::onResize(const Size& size)
|
||||
{
|
||||
static bool eventScheduled = false;
|
||||
static Size newSize;
|
||||
newSize = size;
|
||||
|
||||
// avoid massive resize updates
|
||||
if(!eventScheduled) {
|
||||
g_dispatcher.addEvent([&] {
|
||||
g_graphics.resize(newSize);
|
||||
g_ui.resize(newSize);
|
||||
eventScheduled = false;
|
||||
});
|
||||
eventScheduled = true;
|
||||
}
|
||||
}
|
||||
|
||||
void OTClient::onPlatformEvent(const PlatformEvent& event)
|
||||
{
|
||||
bool fireUi = true;
|
||||
|
||||
if(event.type == EventKeyDown && event.ctrl && !event.alt && !event.shift && event.keycode == Fw::KeyF8) {
|
||||
// TODO: move this events to lua
|
||||
UIWidgetPtr console = g_ui.getRootWidget()->getChildById("consolePanel");
|
||||
if(!console->isExplicitlyVisible()) {
|
||||
console->lock();
|
||||
console->show();
|
||||
} else {
|
||||
console->unlock();
|
||||
console->hide();
|
||||
}
|
||||
fireUi = false;
|
||||
}
|
||||
|
||||
if(fireUi)
|
||||
g_ui.inputEvent(event);
|
||||
Application::init(Otc::AppName, args);
|
||||
}
|
||||
|
@ -1,79 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef OTCLIENT_H
|
||||
#define OTCLIENT_H
|
||||
|
||||
#include <framework/ui/declarations.h>
|
||||
#include <framework/platform/platformlistener.h>
|
||||
#include <framework/application.h>
|
||||
#include <otclient/global.h>
|
||||
|
||||
class OTClient : public PlatformListener
|
||||
class OTClient : public Application
|
||||
{
|
||||
enum {
|
||||
POLL_CYCLE_DELAY = 10
|
||||
};
|
||||
|
||||
public:
|
||||
/// Where everything begins...
|
||||
void init(std::vector<std::string> args);
|
||||
/// Main loop
|
||||
void run();
|
||||
/// Called when the client is terminating
|
||||
void terminate();
|
||||
|
||||
/// Stop running
|
||||
void exit();
|
||||
/// Poll platform, dispatcher and network events
|
||||
void poll();
|
||||
/// Render each frame
|
||||
void render();
|
||||
|
||||
/// Fired when user tryes to close the window
|
||||
void onClose();
|
||||
/// Fired when user resize the window
|
||||
void onResize(const Size& size);
|
||||
/// Fired on an user input event
|
||||
void onPlatformEvent(const PlatformEvent& event);
|
||||
|
||||
bool isRunning() const { return m_running; }
|
||||
|
||||
void setOnClose(const SimpleCallback& onCloseCallback) { m_onCloseCallback = onCloseCallback; }
|
||||
SimpleCallback getOnClose() const { return m_onCloseCallback; }
|
||||
|
||||
static void registerLuaFunctions();
|
||||
|
||||
private:
|
||||
/// Set default configurations and load the user configurations
|
||||
void loadConfigurations();
|
||||
/// Use the loaded configurations to setup other classes
|
||||
void setupConfigurations();
|
||||
void saveConfigurations();
|
||||
|
||||
bool m_running;
|
||||
bool m_stopping;
|
||||
|
||||
SimpleCallback m_onCloseCallback;
|
||||
void init(const std::vector<std::string>& args);
|
||||
void registerLuaFunctions();
|
||||
};
|
||||
|
||||
extern OTClient g_client;
|
||||
extern OTClient g_otclient;
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in new issue