Skip to content

Commit f5fb506

Browse files
committed
MouseSim now supports static and dynamic builds
NSIS packaging is also available
1 parent 68bbb06 commit f5fb506

25 files changed

+1337
-68
lines changed

AboutBox.hpp.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MOUSESIM_ABOUTBOX
2+
3+
#define MOUSESIM_ABOUTBOX \
4+
"<h2>MouseSim</h2> \
5+
<p>Version: ${CMAKE_VERSION_MAJOR}.${CMAKE_VERSION_MINOR}.${CMAKE_VERSION_PATCH}</p> \
6+
<p>Build Number: ${CMAKE_VERSION_BUILD}</p> \
7+
<p>Copyright &copy; 2014 Alex Swindler</p> \
8+
<p><a href='https://github.com/axelstudios/MouseSim'>MouseSim</a> is a simple tool to simulate mouse movement and avoid computer inactivity.</p>"
9+
10+
#endif // MOUSESIM_ABOUTBOX

CMakeLists.txt

Lines changed: 141 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,151 @@
1-
cmake_minimum_required(VERSION 2.8.9)
1+
cmake_minimum_required(VERSION 3.0)
2+
cmake_policy(SET CMP0020 NEW)
3+
cmake_policy(SET CMP0043 NEW)
24

35
project(MouseSim)
46

5-
cmake_policy(SET CMP0020 NEW)
7+
set(CMAKE_VERSION_MAJOR 1)
8+
set(CMAKE_VERSION_MINOR 1)
9+
set(CMAKE_VERSION_PATCH 0)
10+
11+
set(CMAKE_VERSION_BUILD "Unknown" CACHE STRING "Build number")
12+
find_package(Git)
13+
if(GIT_FOUND)
14+
execute_process(COMMAND "${GIT_EXECUTABLE}" "rev-parse" "--short=10" "HEAD"
15+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
16+
TIMEOUT 10
17+
RESULT_VARIABLE RESULT
18+
OUTPUT_VARIABLE GIT_VERSION
19+
ERROR_QUIET
20+
OUTPUT_STRIP_TRAILING_WHITESPACE)
21+
if(${RESULT} EQUAL 0 AND NOT "${GIT_VERSION}" EQUAL "${CMAKE_VERSION_BUILD}")
22+
set(CMAKE_VERSION_BUILD ${GIT_VERSION} CACHE STRING "Build number" FORCE) # git sha
23+
endif()
24+
25+
get_filename_component(GIT_DIR "${GIT_EXECUTABLE}" PATH)
26+
endif()
27+
628
mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
29+
option(BUILD_STATIC "Build with static Qt libs" OFF)
30+
option(BUILD_PACKAGE "Package with NSIS" ON)
31+
32+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
33+
34+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
735

836
set(CMAKE_AUTOMOC ON)
937

1038
find_package(Qt5Widgets REQUIRED)
11-
mark_as_advanced(Qt5Core_DIR)
12-
mark_as_advanced(Qt5Gui_DIR)
13-
mark_as_advanced(Qt5Widgets_DIR)
39+
mark_as_advanced(
40+
Qt5Core_DIR
41+
Qt5Gui_DIR
42+
Qt5Widgets_DIR
43+
)
1444

1545
qt5_add_resources(MouseSim_qrc MouseSim.qrc)
16-
add_executable(MouseSim WIN32 main.cpp MouseSim.cpp ${MouseSim_qrc} MouseSim.rc)
17-
qt5_use_modules(MouseSim Widgets)
46+
configure_file(AboutBox.hpp.in "${CMAKE_CURRENT_BINARY_DIR}/AboutBox.hpp")
47+
configure_file(MouseSim.rc.in "${CMAKE_CURRENT_BINARY_DIR}/MouseSim.rc")
48+
add_executable(MouseSim WIN32 main.cpp MouseSim.cpp ${MouseSim_qrc} "${CMAKE_CURRENT_BINARY_DIR}/MouseSim.rc")
49+
50+
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build Configuration" FORCE)
51+
if(BUILD_STATIC)
52+
message(STATUS "Note: Static builds require a custom static build of Qt")
53+
54+
find_package(Qt5OpenGL REQUIRED)
55+
mark_as_advanced(Qt5OpenGL_DIR)
56+
57+
add_definitions(-DBUILD_STATIC)
58+
59+
target_link_libraries(MouseSim Qt5::OpenGL OpenGL32 imm32 winmm ws2_32)
60+
61+
get_target_property(QtCore_LOCATION Qt5::Core LOCATION)
62+
get_filename_component(QT_LIB_DIR ${QtCore_LOCATION} PATH)
63+
64+
find_library(PLATFORM_SUPPORT Qt5PlatformSupport ${QT_LIB_DIR})
65+
find_library(WIN_PLATFORM qwindows ${QT_LIB_DIR}/../plugins/platforms/)
66+
mark_as_advanced(PLATFORM_SUPPORT WIN_PLATFORM)
67+
target_link_libraries(MouseSim optimized ${PLATFORM_SUPPORT} optimized ${WIN_PLATFORM})
68+
69+
find_library(PLATFORM_SUPPORT_D Qt5PlatformSupportd ${QT_LIB_DIR})
70+
find_library(WIN_PLATFORM_D qwindowsd ${QT_LIB_DIR}/../plugins/platforms/)
71+
mark_as_advanced(PLATFORM_SUPPORT_D WIN_PLATFORM_D)
72+
if(NOT ${PLATFORM_SUPPORT_D} STREQUAL "PLATFORM_SUPPORT_D-NOTFOUND" AND NOT ${WIN_PLATFORM_D} STREQUAL "WIN_PLATFORM_D-NOTFOUND")
73+
target_link_libraries(MouseSim debug ${PLATFORM_SUPPORT_D} debug ${WIN_PLATFORM_D})
74+
else()
75+
set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "Build Configuration" FORCE)
76+
endif()
77+
else()
78+
qt5_use_modules(MouseSim Widgets)
79+
80+
get_target_property(QPA_PLUGIN Qt5::QWindowsIntegrationPlugin LOCATION)
81+
get_filename_component(QPA_PLUGIN_NAME "${QPA_PLUGIN}" NAME)
82+
install(PROGRAMS "${QPA_PLUGIN}"
83+
DESTINATION "platforms"
84+
)
85+
endif()
86+
87+
install(TARGETS MouseSim
88+
RUNTIME
89+
DESTINATION /
90+
)
91+
92+
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP ON)
93+
include(InstallRequiredSystemLibraries)
94+
if(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS)
95+
install(FILES ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION "/")
96+
97+
if(NOT BUILD_STATIC)
98+
set(LIBRARY_SEARCH_DIRECTORY "${CMAKE_BINARY_DIR}" "${CMAKE_BINARY_DIR}/Release" "${CMAKE_BINARY_DIR}/Debug")
99+
install(CODE "
100+
include(BundleUtilities)
101+
fixup_bundle(\"\${CMAKE_INSTALL_PREFIX}/MouseSim.exe\" \"\${CMAKE_INSTALL_PREFIX}/platforms/${QPA_PLUGIN_NAME}\" \"${LIBRARY_SEARCH_DIRECTORY}\")
102+
")
103+
endif()
104+
endif()
105+
106+
if(BUILD_PACKAGE)
107+
configure_file("${CMAKE_SOURCE_DIR}/icons/nsis.bmp" "${CMAKE_BINARY_DIR}/nsis.bmp" COPYONLY)
108+
109+
set(FULL_VERSION_STRING "${CMAKE_VERSION_MAJOR}.${CMAKE_VERSION_MINOR}.${CMAKE_VERSION_PATCH}")
110+
111+
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "MouseSim")
112+
set(CPACK_PACKAGE_VENDOR "Alex Swindler")
113+
set(CPACK_PACKAGE_CONTACT "${CPACK_PACKAGE_VENDOR} <[email protected]>")
114+
115+
set(CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_VERSION_MAJOR})
116+
set(CPACK_PACKAGE_VERSION_MINOR ${CMAKE_VERSION_MINOR})
117+
set(CPACK_PACKAGE_VERSION_PATCH ${CMAKE_VERSION_PATCH})
118+
119+
set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${FULL_VERSION_STRING}")
120+
if(CMAKE_CL_64)
121+
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-Win64")
122+
set(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
123+
endif()
124+
125+
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
126+
127+
set(CPACK_PACKAGE_EXECUTABLES "MouseSim" "MouseSim")
128+
129+
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MouseSim is a simple tool to simulate mouse movement and avoid computer inactivity.")
130+
131+
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/CPack.Description.txt")
132+
set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/CPack.Description.txt")
133+
set(CPACK_RESOURCE_FILE_WELCOME "${CMAKE_SOURCE_DIR}/CPack.Welcome.txt")
134+
135+
set(CPACK_NSIS_MUI_ICON "${CMAKE_SOURCE_DIR}/icons/mouse.ico")
136+
set(CPACK_NSIS_INSTALLED_ICON_NAME "MouseSim.exe")
137+
set(CPACK_NSIS_URL_INFO_ABOUT "https://github.com/axelstudios/MouseSim")
138+
set(CPACK_NSIS_HELP_LINK "https://github.com/axelstudios/MouseSim")
139+
set(CPACK_NSIS_CREATE_ICONS "CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\MouseSim.lnk\\\" \\\"$INSTDIR\\\\MouseSim.exe\\\"")
140+
141+
set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "
142+
WriteRegStr HKCU 'Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run' 'MouseSim' '\\\"$INSTDIR\\\\MouseSim.exe\\\"'
143+
")
144+
set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "
145+
DeleteRegValue HKCU 'Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run' 'MouseSim'
146+
")
147+
148+
set(CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR};Required;ALL;/")
149+
150+
include(CPack)
151+
endif()

CPack.Description.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DESCRIPTION
2+
===========
3+
4+
MouseSim is a simple tool to simulate mouse movement and avoid computer inactivity.
5+

CPack.Welcome.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This program will guide you through the installation of MouseSim.

MouseSim.cpp

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,77 @@
11
#include "MouseSim.hpp"
2+
#include <AboutBox.hpp>
23

34
#include <QAction>
45
#include <QCoreApplication>
56
#include <QMenu>
7+
#include <QMessageBox>
8+
#include <QSettings>
9+
#include <QSharedMemory>
610
#include <QTimer>
711

812
#include <Windows.h>
913

1014
MouseSim::MouseSim()
1115
{
12-
enableAction = new QAction(tr("&Enable"), this);
13-
connect(enableAction, SIGNAL(triggered()), this, SLOT(enable()));
16+
m_mem = new QSharedMemory("MouseSimRunning", this);
17+
if (!m_mem->create(1)) QTimer::singleShot(0, qApp, SLOT(quit()));
1418

15-
disableAction = new QAction(tr("&Disable"), this);
16-
connect(disableAction, SIGNAL(triggered()), this, SLOT(disable()));
19+
QImage img = QImage(":/icons/mouse_16.png");
20+
m_activated = new QIcon(QPixmap::fromImage(img));
21+
QSize size = img.size();
22+
int width = size.width(),
23+
height = size.height();
24+
25+
for (int i = 0; i < width; ++i) {
26+
for (int j = 0; j < height; ++j) {
27+
QRgb pixel = img.pixel(i, j);
28+
int gray = qGray(pixel),
29+
alpha = qAlpha(pixel);
30+
img.setPixel(i, j, qRgba(gray, gray, gray, alpha));
31+
}
32+
}
33+
m_deactivated = new QIcon(QPixmap::fromImage(img));
34+
35+
m_settings = new QSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
36+
bool checked = !m_settings->value("MouseSim", QVariant("")).toString().isEmpty();
37+
38+
m_enableAction = new QAction("&Enable", this);
39+
m_disableAction = new QAction("&Disable", this);
40+
m_autostartAction = new QAction("Autostart &MouseSim", this);
41+
m_autostartAction->setCheckable(true);
42+
m_autostartAction->setChecked(checked);
43+
m_aboutAction = new QAction("&About MouseSim", this);
44+
m_quitAction = new QAction("&Quit", this);
1745

18-
quitAction = new QAction(tr("&Quit"), this);
19-
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
46+
connect(m_enableAction, &QAction::triggered, this, &MouseSim::enable);
47+
connect(m_disableAction, &QAction::triggered, this, &MouseSim::disable);
48+
connect(m_autostartAction, &QAction::triggered, this, &MouseSim::autostart);
49+
connect(m_aboutAction, &QAction::triggered, this, &MouseSim::about);
50+
connect(m_quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
2051

21-
trayIconMenu = new QMenu(this);
22-
trayIconMenu->addAction(enableAction);
23-
trayIconMenu->addAction(disableAction);
24-
trayIconMenu->addSeparator();
25-
trayIconMenu->addAction(quitAction);
52+
m_trayIconMenu = new QMenu(this);
53+
m_trayIconMenu->addAction(m_enableAction);
54+
m_trayIconMenu->addAction(m_disableAction);
55+
m_trayIconMenu->addAction(m_autostartAction);
56+
m_trayIconMenu->addAction(m_aboutAction);
57+
m_trayIconMenu->addSeparator();
58+
m_trayIconMenu->addAction(m_quitAction);
2659

27-
trayIcon = new QSystemTrayIcon(this);
28-
trayIcon->setContextMenu(trayIconMenu);
60+
m_trayIcon = new QSystemTrayIcon(this);
61+
m_trayIcon->setContextMenu(m_trayIconMenu);
2962

30-
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
31-
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
63+
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MouseSim::iconActivated);
3264

3365
m_timer = new QTimer(this);
34-
connect(m_timer, SIGNAL(timeout()), this, SLOT(sendInput()));
66+
connect(m_timer, &QTimer::timeout, this, &MouseSim::sendInput);
3567

3668
enable();
37-
trayIcon->show();
69+
m_trayIcon->show();
70+
}
71+
72+
MouseSim::~MouseSim()
73+
{
74+
m_mem->detach();
3875
}
3976

4077
void MouseSim::iconActivated(QSystemTrayIcon::ActivationReason reason)
@@ -65,18 +102,37 @@ void MouseSim::sendInput()
65102
void MouseSim::enable()
66103
{
67104
m_timer->start(1000);
68-
trayIcon->setIcon(QIcon(":/images/mouse.png"));
69-
trayIcon->setToolTip("MouseSim: Enabled");
105+
m_trayIcon->setIcon(*m_activated);
106+
m_trayIcon->setToolTip("MouseSim: Enabled");
70107
sendInput();
71-
enableAction->setDisabled(true);
72-
disableAction->setEnabled(true);
108+
m_enableAction->setDisabled(true);
109+
m_disableAction->setEnabled(true);
73110
}
74111

75112
void MouseSim::disable()
76113
{
77114
m_timer->stop();
78-
trayIcon->setIcon(QIcon(":/images/mouse-gray.png"));
79-
trayIcon->setToolTip("MouseSim: Disabled");
80-
disableAction->setDisabled(true);
81-
enableAction->setEnabled(true);
115+
116+
m_trayIcon->setIcon(*m_deactivated);
117+
m_trayIcon->setToolTip("MouseSim: Disabled");
118+
m_disableAction->setDisabled(true);
119+
m_enableAction->setEnabled(true);
120+
}
121+
122+
void MouseSim::autostart(bool enabled)
123+
{
124+
if (enabled) {
125+
m_settings->setValue("MouseSim", "\"" + QCoreApplication::applicationFilePath().replace("/", "\\") + "\"");
126+
} else {
127+
m_settings->remove("MouseSim");
128+
}
129+
}
130+
131+
void MouseSim::about()
132+
{
133+
QMessageBox about(this);
134+
about.setText(MOUSESIM_ABOUTBOX);
135+
about.setStyleSheet("qproperty-alignment: AlignCenter;");
136+
about.setWindowTitle("About MouseSim");
137+
about.exec();
82138
}

MouseSim.hpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,42 @@
66

77
class QAction;
88
class QMenu;
9+
class QSharedMemory;
10+
class QSettings;
911

1012
class MouseSim : public QWidget
1113
{
1214
Q_OBJECT
1315

14-
public:
16+
public:
1517
MouseSim();
18+
~MouseSim();
1619

17-
private slots:
20+
private slots:
1821
void iconActivated(QSystemTrayIcon::ActivationReason reason);
1922
void sendInput();
2023
void enable();
2124
void disable();
25+
void autostart(bool enabled);
26+
void about();
27+
28+
private:
29+
QIcon *m_activated;
30+
QIcon *m_deactivated;
2231

23-
private:
2432
QTimer *m_timer;
2533

26-
QAction *enableAction;
27-
QAction *disableAction;
28-
QAction *quitAction;
34+
QAction *m_enableAction;
35+
QAction *m_disableAction;
36+
QAction *m_autostartAction;
37+
QAction *m_aboutAction;
38+
QAction *m_quitAction;
39+
40+
QSystemTrayIcon *m_trayIcon;
41+
QMenu *m_trayIconMenu;
2942

30-
QSystemTrayIcon *trayIcon;
31-
QMenu *trayIconMenu;
43+
QSettings *m_settings;
44+
QSharedMemory *m_mem;
3245
};
3346

34-
#endif MOUSESIM_HPP
47+
#endif // MOUSESIM_HPP

MouseSim.qrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<!DOCTYPE RCC><RCC version="1.0">
22
<qresource>
3-
<file>images/mouse.png</file>
4-
<file>images/mouse-gray.png</file>
3+
<file>icons/mouse_16.png</file>
54
</qresource>
65
</RCC>

0 commit comments

Comments
 (0)