From 1ccc258455bebc02c38cc358f6905c3874ad91e4 Mon Sep 17 00:00:00 2001 From: "Jerry (Xinyu Hou)" Date: Mon, 19 Oct 2015 11:18:08 -0700 Subject: [PATCH] Add DPI helper #5030 --- src/lib/synergy/DpiHelper.cpp | 49 +++++++++++++++++++++++++++++++++++ src/lib/synergy/DpiHelper.h | 36 +++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/lib/synergy/DpiHelper.cpp create mode 100644 src/lib/synergy/DpiHelper.h diff --git a/src/lib/synergy/DpiHelper.cpp b/src/lib/synergy/DpiHelper.cpp new file mode 100644 index 00000000..7b8effe7 --- /dev/null +++ b/src/lib/synergy/DpiHelper.cpp @@ -0,0 +1,49 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2015 Synergy Seamless Inc. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "synergy/DpiHelper.h" +#include "base/Log.h" + +#include + +size_t DpiHelper::s_dpi = kDefaultDpi; +bool DpiHelper::s_dpiScaled = false; +size_t DpiHelper::s_resolutionWidth = 0; +size_t DpiHelper::s_resolutionHeight = 0; +size_t DpiHelper::s_primaryWidthCenter = 0; +size_t DpiHelper::s_primaryHeightCenter = 0; + +void DpiHelper::calculateDpi(size_t width, size_t height) +{ + if (s_resolutionWidth == 0 || s_resolutionHeight == 0) { + return; + } + + size_t dpiTest1 = s_resolutionWidth * 100 / width; + size_t dpiTest2 = s_resolutionHeight * 100 / height; + + assert(dpiTest1 == dpiTest2); + + s_dpi = dpiTest1; + + if (s_dpi != kDefaultDpi) { + s_dpiScaled = true; + + LOG((CLOG_DEBUG "DPI: %d%%", s_dpi)); + LOG((CLOG_DEBUG "physical resolution: %d, %d scaled resolution: %d, %d", s_resolutionWidth, s_resolutionHeight, width, height)); + } +} diff --git a/src/lib/synergy/DpiHelper.h b/src/lib/synergy/DpiHelper.h new file mode 100644 index 00000000..8f37de96 --- /dev/null +++ b/src/lib/synergy/DpiHelper.h @@ -0,0 +1,36 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2015 Synergy Seamless Inc. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +class DpiHelper { +public: + enum EDpi { + kDefaultDpi = 100 + }; + + static void calculateDpi(size_t width, size_t height); + static float getDpi() { return (float)(s_dpi / 100.0f); } + +public: + static size_t s_dpi; + static bool s_dpiScaled; + static size_t s_resolutionWidth; + static size_t s_resolutionHeight; + static size_t s_primaryWidthCenter; + static size_t s_primaryHeightCenter; +};