From 1c94dda072ffb23ffa0d22652c26300df663f081 Mon Sep 17 00:00:00 2001 From: David Capello Date: Wed, 20 Apr 2016 12:48:49 -0300 Subject: [PATCH] Merge pen library into she library This is the first step to add pen information to mouse events (e.g. to known if a event came from the eraser or the regular tip). --- src/CMakeLists.txt | 1 - src/app/CMakeLists.txt | 1 - src/app/app.cpp | 4 -- src/she/CMakeLists.txt | 1 + src/she/common/system.h | 1 + src/she/skia/she.cpp | 2 - src/she/skia/skia_system.h | 7 ++- src/she/win/pen.cpp | 96 ++++++++++++++++++++++++++++++++++++++ src/she/win/pen.h | 34 ++++++++++++++ src/she/win/system.h | 31 ++++++++++++ src/she/win/window.h | 15 +++++- 11 files changed, 183 insertions(+), 10 deletions(-) create mode 100644 src/she/win/pen.cpp create mode 100644 src/she/win/pen.h create mode 100644 src/she/win/system.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 03aa0fd44..afc8714b8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -102,7 +102,6 @@ add_subdirectory(flic) add_subdirectory(gen) add_subdirectory(gfx) add_subdirectory(net) -add_subdirectory(pen) add_subdirectory(render) add_subdirectory(script) add_subdirectory(she) diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 6688700e6..74e2e6605 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -445,7 +445,6 @@ target_link_libraries(app-lib flic-lib gfx-lib net-lib - pen-lib render-lib script-lib she diff --git a/src/app/app.cpp b/src/app/app.cpp index 97a5fce98..dfe0e45a0 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -67,7 +67,6 @@ #include "doc/palette.h" #include "doc/site.h" #include "doc/sprite.h" -#include "pen/pen.h" #include "render/render.h" #include "script/engine_delegate.h" #include "she/display.h" @@ -666,9 +665,6 @@ void App::run() { // Run the GUI if (isGui()) { - // Initialize Wacom tablet API - pen::PenAPI pen(she::instance()->defaultDisplay()->nativeHandle()); - // Initialize Steam API #ifdef ENABLE_STEAM steam::SteamAPI steam; diff --git a/src/she/CMakeLists.txt b/src/she/CMakeLists.txt index 727c39b8e..cddf5bc44 100644 --- a/src/she/CMakeLists.txt +++ b/src/she/CMakeLists.txt @@ -187,6 +187,7 @@ if(USE_SKIA_BACKEND) if(WIN32) list(APPEND SHE_SOURCES skia/skia_window_win.cpp + win/pen.cpp win/vk.cpp win/window_dde.cpp) elseif(APPLE) diff --git a/src/she/common/system.h b/src/she/common/system.h index 273d445dd..f03a0ce20 100644 --- a/src/she/common/system.h +++ b/src/she/common/system.h @@ -24,6 +24,7 @@ #include "she/common/freetype_font.h" #include "she/common/sprite_sheet_font.h" +#include "she/system.h" namespace she { diff --git a/src/she/skia/she.cpp b/src/she/skia/she.cpp index e45ac5d21..833a0dab2 100644 --- a/src/she/skia/she.cpp +++ b/src/she/skia/she.cpp @@ -13,8 +13,6 @@ #include "gfx/size.h" #include "she/she.h" -#include "she/common/system.h" - #include "she/skia/skia_system.h" #if __APPLE__ diff --git a/src/she/skia/skia_system.h b/src/she/skia/skia_system.h index a8a6d618a..e40e4d60c 100644 --- a/src/she/skia/skia_system.h +++ b/src/she/skia/skia_system.h @@ -15,23 +15,28 @@ #include "SkPixelRef.h" #include "SkStream.h" +#include "she/common/system.h" #include "she/skia/skia_display.h" #include "she/skia/skia_surface.h" #ifdef _WIN32 #include "she/win/event_queue.h" + #include "she/win/system.h" + #define SkiaSystemBase WindowSystem #elif __APPLE__ #include "she/osx/app.h" #include "she/osx/event_queue.h" + #define SkiaSystemBase CommonSystem #else #include "she/x11/event_queue.h" + #define SkiaSystemBase CommonSystem #endif namespace she { EventQueueImpl g_queue; -class SkiaSystem : public CommonSystem { +class SkiaSystem : public SkiaSystemBase { public: SkiaSystem() : m_defaultDisplay(nullptr) diff --git a/src/she/win/pen.cpp b/src/she/win/pen.cpp new file mode 100644 index 000000000..343d08c4d --- /dev/null +++ b/src/she/win/pen.cpp @@ -0,0 +1,96 @@ +// SHE library +// Copyright (C) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "she/win/pen.h" + +#include "base/debug.h" +#include "base/fs.h" +#include "base/log.h" +#include "base/path.h" +#include "base/string.h" + +typedef UINT (API* WTInfoW_Func)(UINT, UINT, LPVOID); +typedef HCTX (API* WTOpenW_Func)(HWND, LPLOGCONTEXTW, BOOL); +typedef BOOL (API* WTClose_Func)(HCTX); + +namespace she { + +static WTInfoW_Func WTInfo; +static WTOpenW_Func WTOpen; +static WTClose_Func WTClose; + +PenAPI::PenAPI() + : m_wintabLib(nullptr) +{ +} + +PenAPI::~PenAPI() +{ + if (!m_wintabLib) + return; + + base::unload_dll(m_wintabLib); + m_wintabLib = nullptr; +} + +HCTX PenAPI::attachDisplay(HWND hwnd) +{ + if (!m_wintabLib && !loadWintab()) + return nullptr; + + LOGCONTEXTW logctx; + memset(&logctx, 0, sizeof(LOGCONTEXTW)); + logctx.lcOptions |= CXO_SYSTEM; + UINT infoRes = WTInfo(WTI_DEFSYSCTX, 0, &logctx); + ASSERT(infoRes == sizeof(LOGCONTEXTW)); + ASSERT(logctx.lcOptions & CXO_SYSTEM); + logctx.lcOptions |= CXO_SYSTEM; + HCTX ctx = WTOpen(hwnd, &logctx, TRUE); + if (!ctx) { + LOG("Error attaching pen to display\n"); + return nullptr; + } + + LOG("Pen attached to display\n"); + return ctx; +} + +void PenAPI::detachDisplay(HCTX ctx) +{ + if (ctx) { + ASSERT(m_wintabLib); + LOG("Pen detached from window\n"); + WTClose(ctx); + } +} + +bool PenAPI::loadWintab() +{ + ASSERT(!m_wintabLib); + + m_wintabLib = base::load_dll("wintab32.dll"); + if (!m_wintabLib) { + LOG("wintab32.dll is not present\n"); + return false; + } + + WTInfo = base::get_dll_proc(m_wintabLib, "WTInfoW"); + WTOpen = base::get_dll_proc(m_wintabLib, "WTOpenW"); + WTClose = base::get_dll_proc(m_wintabLib, "WTClose"); + if (!WTInfo || !WTOpen || !WTClose) { + LOG("wintab32.dll does not contain all required functions\n"); + return false; + } + + LOG("Pen initialized\n"); + return true; +} + +} // namespace she diff --git a/src/she/win/pen.h b/src/she/win/pen.h new file mode 100644 index 000000000..d9ac9f6fb --- /dev/null +++ b/src/she/win/pen.h @@ -0,0 +1,34 @@ +// SHE library +// Copyright (C) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef SHE_WIN_PEN_H_INCLUDED +#define SHE_WIN_PEN_H_INCLUDED +#pragma once + +#include "base/dll.h" + +#include +#include "wacom/wintab.h" + +namespace she { + + class PenAPI { + public: + PenAPI(); + ~PenAPI(); + + HCTX attachDisplay(HWND hwnd); + void detachDisplay(HCTX ctx); + + private: + bool loadWintab(); + + base::dll m_wintabLib; + }; + +} // namespace she + +#endif diff --git a/src/she/win/system.h b/src/she/win/system.h new file mode 100644 index 000000000..48414127f --- /dev/null +++ b/src/she/win/system.h @@ -0,0 +1,31 @@ +// SHE library +// Copyright (C) 2012-2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef SHE_WIN_SYSTEM_H +#define SHE_WIN_SYSTEM_H +#pragma once + +#include "she/common/system.h" +#include "she/win/pen.h" + +namespace she { + +class WindowSystem : public CommonSystem { +public: + WindowSystem() { } + ~WindowSystem() { } + + PenAPI& penApi() { + return m_penApi; + } + +private: + PenAPI m_penApi; +}; + +} // namespace she + +#endif diff --git a/src/she/win/window.h b/src/she/win/window.h index be29adf80..216ccd537 100644 --- a/src/she/win/window.h +++ b/src/she/win/window.h @@ -19,6 +19,7 @@ #include "she/event.h" #include "she/keys.h" #include "she/native_cursor.h" +#include "she/win/system.h" #include "she/win/window_dde.h" #ifndef WM_MOUSEHWHEEL @@ -43,13 +44,24 @@ namespace she { , m_captureMouse(false) { registerClass(); m_hwnd = createHwnd(this, width, height); - m_hcursor = NULL; + m_hcursor = nullptr; + m_hpenctx = nullptr; m_scale = scale; // This flag is used to avoid calling T::resizeImpl() when we // add the scrollbars to the window. (As the T type could not be // fully initialized yet.) m_isCreated = true; + + // Attach Wacom context + m_hpenctx = static_cast(she::instance()) + ->penApi().attachDisplay(m_hwnd); + } + + ~WinWindow() { + if (m_hpenctx) + static_cast(she::instance()) + ->penApi().detachDisplay(m_hpenctx); } void queueEvent(Event& ev) { @@ -667,6 +679,7 @@ namespace she { mutable HWND m_hwnd; HCURSOR m_hcursor; + HCTX m_hpenctx; // Wacom Pen context gfx::Size m_clientSize; gfx::Size m_restoredSize; int m_scale;