Skip to content

Commit

Permalink
Change development environment from VSCode to neovim + neovide + clangd.
Browse files Browse the repository at this point in the history
Yeah, and add some powershell scripts so that I can compile and build
this project manually. And fix some header file path problems.
  • Loading branch information
fanlumaster committed Oct 10, 2023
1 parent c3b07e6 commit 43eb004
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
BasedOnStyle: Google
IndentWidth: 4
14 changes: 0 additions & 14 deletions DeclareDPIAware.manifest
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<!-- <security>
<requestedPrivileges>
<requestedExecutionLevel
level="highestAvailable"
uiAccess="true" />
</requestedPrivileges>
</security> -->
<!-- <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="true" />
</requestedPrivileges>
</security>
</trustInfo> -->
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
Expand Down
28 changes: 28 additions & 0 deletions compile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# generate and compile exe files

$currentDirectory = Get-Location
$cmakeListsPath = Join-Path -Path $currentDirectory -ChildPath "CMakeLists.txt"

if (-not (Test-Path $cmakeListsPath))
{
Write-Host("No CMakeLists.txt in current directory, please check.")
return
}

Write-Host "Start generating and compiling..."

$buildFolderPath = ".\build"

if (-not (Test-Path $buildFolderPath))
{
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null
Write-Host "build folder created."
}

cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/

if ($LASTEXITCODE -eq 0)
{
cmake --build ./build/ --config DEBUG
}

46 changes: 46 additions & 0 deletions launch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# generate, compile and run exe files

$currentDirectory = Get-Location
$cmakeListsPath = Join-Path -Path $currentDirectory -ChildPath "CMakeLists.txt"

if (-not (Test-Path $cmakeListsPath))
{
Write-Host("No CMakeLists.txt in current directory, please check.")
return
}

Write-Host "Start generating and compiling..."

$buildFolderPath = ".\build"

if (-not (Test-Path $buildFolderPath))
{
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null
Write-Host "build folder created."
}

cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/

if ($LASTEXITCODE -eq 0)
{
cmake --build ./build/ --config DEBUG
if ($LASTEXITCODE -eq 0)
{
$content = Get-Content -Path "./CMakeLists.txt"
foreach($line in $content)
{
if ($line.StartsWith("add_executable"))
{
$pattern = "\((.*?)\)"
if ($line -match $pattern)
{
$contentInParentheses = $Matches[1]
$result = -split $contentInParentheses
$exePath = "./build/DEBUG/" + $result[0] + ".exe"
Invoke-Expression $exePath
}
}
}
}
}

78 changes: 52 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
注意!!!Caution!!!
这个输入法一开始的状态是英文!需要使用 Ctrl + Space 切换一下状态才可以正常输入中文!!!
这个输入法一开始的状态是英文!需要使用 Ctrl + Space
切换一下状态才可以正常输入中文!!!
*/
#ifndef UNICODE
#define UNICODE
Expand All @@ -17,17 +18,18 @@

#include "./InitConsole.h"
#include "./flypytblsqlite.h"
#include "./hook/ime_hook.h"
#include "./hook/key_handle_func_lib.h"
#include "./sqlite/sqlite_wrapper.h"
#include "./ui/cand_ui.h"
#include "./src/hook/ime_hook.h"
#include "./src/hook/key_handle_func_lib.h"
#include "./src/sqlite/sqlite_wrapper.h"
#include "./src/ui/cand_ui.h"
#include "./src/utils/caret_helper.h"
#include "./uiaccess.h"
#include "./utils/caret_helper.h"

#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,
int iCmdShow) {
// Windos topmost
PrepareForUIAccess();
// 测试是否有内存泄露
Expand All @@ -43,7 +45,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,
freopen_s(&pCout, "CONOUT$", "w", stdout);
// SetConsoleTitleW("Debug Console");
SetConsoleTitleW(L"Fany Debug");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
SetConsoleTextAttribute(
GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
}
// set std::cout to use my custom streambuf
outbuf ob;
Expand All @@ -66,7 +70,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,
// 初始化小鹤双拼的码表,纯双拼二码
// std::string dbPath = "../../src/flyciku.db";
std::string dbPath = "./db/flyciku.db";
// sqlPageMap = transTableToMap(dbPath, 8); // 如果把这个放到钩子函数里面会导致程序很慢的
// sqlPageMap = transTableToMap(dbPath, 8); //
// 如果把这个放到钩子函数里面会导致程序很慢的
db = openSqlite(dbPath);
// 初始化 COM
CoInitialize(nullptr); // <-- add this to init COM
Expand All @@ -88,14 +93,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,

// 注册窗口
if (!RegisterClassEx(&winClass)) {
MessageBox(NULL, TEXT("This program requires Windows NT!"), L"error", MB_ICONERROR);
MessageBox(NULL, TEXT("This program requires Windows NT!"), L"error",
MB_ICONERROR);
return 0;
}

// 创建窗口
// 这个 WS_EX_TOOLWINDOW 是为了去掉任务栏区的图标的,这样它就更像一个输入法窗口了
// WS_EX_NOACTIVATE 程序执行之后,窗口不会自动获取焦点
gHwnd = CreateWindowEx(WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE,
// 这个 WS_EX_TOOLWINDOW
// 是为了去掉任务栏区的图标的,这样它就更像一个输入法窗口了 WS_EX_NOACTIVATE
// 程序执行之后,窗口不会自动获取焦点
gHwnd = CreateWindowEx(WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOPMOST |
WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE,
L"Direct2D", // window class name
L"Draw Rectangle", // window caption
WS_POPUP, // window style
Expand Down Expand Up @@ -126,25 +134,43 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_FANY_HIDEWINDOW) {
// ShowWindow(gHwnd, SW_HIDE);
SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_HIDEWINDOW | SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW |
// SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
} else if (msg.message == WM_FANY_SHOWWINDOW) {
// ShowWindow(gHwnd, SW_SHOW);
// SetWindowPos(gHwnd, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
// SetWindowPos(gHwnd, HWND_DESKTOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE
// | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW |
// SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
// SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW |
// SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
SetWindowPos(gHwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
// SetWindowPos(gHwnd, HWND_DESKTOP, 0, 0, 0, 0, SWP_SHOWWINDOW |
// SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
} else if (msg.message == WM_FANY_REDRAW) {
// wText = L"ni'hc\n1.还行\n2.世界\n3.毛笔\n4.量子\n5.笔画\n6.竟然\n7.什么\n8.可是";
// wText =
// L"ni'hc\n1.还行\n2.世界\n3.毛笔\n4.量子\n5.笔画\n6.竟然\n7.什么\n8.可是";
std::pair<int, int> candSize = calcCandSize(17, 2);
// std::pair<int, int> caretPos = fanyGetCaretPos();
std::pair<int, int> caretPos = getGeneralCaretPos();
// SetWindowPos(gHwnd, NULL, caretPos.first, caretPos.second, candSize.first, candSize.second, SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS);
// SetWindowPos(gHwnd, HWND_TOPMOST, caretPos.first, caretPos.second, candSize.first, candSize.second, SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS);
// SetWindowPos(gHwnd, HWND_TOPMOST, caretPos.first, caretPos.second, candSize.first, candSize.second, SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS);
SetWindowPos(gHwnd, HWND_TOPMOST, caretPos.first, caretPos.second, candSize.first, candSize.second, SWP_SHOWWINDOW | SWP_ASYNCWINDOWPOS);
// SetWindowPos(gHwnd, HWND_DESKTOP, caretPos.first, caretPos.second, candSize.first, candSize.second, SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS);
// SetWindowPos(gHwnd, NULL, caretPos.first, caretPos.second,
// candSize.first, candSize.second, SWP_SHOWWINDOW | SWP_NOZORDER |
// SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS); SetWindowPos(gHwnd,
// HWND_TOPMOST, caretPos.first, caretPos.second, candSize.first,
// candSize.second, SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE |
// SWP_ASYNCWINDOWPOS); SetWindowPos(gHwnd, HWND_TOPMOST,
// caretPos.first, caretPos.second, candSize.first, candSize.second,
// SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS);
SetWindowPos(gHwnd, HWND_TOPMOST, caretPos.first, caretPos.second,
candSize.first, candSize.second,
SWP_SHOWWINDOW | SWP_ASYNCWINDOWPOS);
// SetWindowPos(gHwnd, HWND_DESKTOP, caretPos.first,
// caretPos.second, candSize.first, candSize.second, SWP_SHOWWINDOW
// | SWP_NOZORDER | SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS);
FanyDrawText(gHwnd, wText);
} else if (msg.message == WM_HOTKEY) {
if (1 == msg.wParam) {
Expand Down
3 changes: 3 additions & 0 deletions src/hook/ime_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ LRESULT CALLBACK KBDHook(int nCode, WPARAM wParam, LPARAM lParam) {

// 顿号
if (s->vkCode == VK_OEM_5) {
if (fCtrlDown) {
break; // 这里的作用是不干扰其他快捷键,比如 notion 中的 Ctrl + \ 快捷键
}
if (fShiftDown) {
sendStringToCursor(converter.from_bytes("|"));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/hook/ime_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <unordered_map>
#include <vector>

#include "../libs/sqlite/sqlite3.h"
#include "../../libs/sqlite/sqlite3.h"

/*
全局变量部分
Expand Down
48 changes: 35 additions & 13 deletions uiaccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <tchar.h>
#include <tlhelp32.h>

static DWORD DuplicateWinloginToken(DWORD dwSessionId, DWORD dwDesiredAccess, PHANDLE phToken) {
static DWORD DuplicateWinloginToken(DWORD dwSessionId, DWORD dwDesiredAccess,
PHANDLE phToken) {
DWORD dwErr;
PRIVILEGE_SET ps;

Expand All @@ -19,25 +20,34 @@ static DWORD DuplicateWinloginToken(DWORD dwSessionId, DWORD dwDesiredAccess, PH
pe.dwSize = sizeof(pe);
dwErr = ERROR_NOT_FOUND;

for (bCont = Process32First(hSnapshot, &pe); bCont; bCont = Process32Next(hSnapshot, &pe)) {
for (bCont = Process32First(hSnapshot, &pe); bCont;
bCont = Process32Next(hSnapshot, &pe)) {
HANDLE hProcess;

if (0 != _tcsicmp(pe.szExeFile, TEXT("winlogon.exe"))) {
continue;
}

hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pe.th32ProcessID);
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
pe.th32ProcessID);
if (hProcess) {
HANDLE hToken;
DWORD dwRetLen, sid;

if (OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_DUPLICATE, &hToken)) {
if (OpenProcessToken(
hProcess, TOKEN_QUERY | TOKEN_DUPLICATE, &hToken)) {
BOOL fTcb;

if (PrivilegeCheck(hToken, &ps, &fTcb) && fTcb) {
if (GetTokenInformation(hToken, TokenSessionId, &sid, sizeof(sid), &dwRetLen) && sid == dwSessionId) {
if (GetTokenInformation(hToken, TokenSessionId,
&sid, sizeof(sid),
&dwRetLen) &&
sid == dwSessionId) {
bFound = TRUE;
if (DuplicateTokenEx(hToken, dwDesiredAccess, NULL, SecurityImpersonation, TokenImpersonation, phToken)) {
if (DuplicateTokenEx(
hToken, dwDesiredAccess, NULL,
SecurityImpersonation,
TokenImpersonation, phToken)) {
dwErr = ERROR_SUCCESS;
} else {
dwErr = GetLastError();
Expand Down Expand Up @@ -67,19 +77,28 @@ static DWORD CreateUIAccessToken(PHANDLE phToken) {
DWORD dwErr;
HANDLE hTokenSelf;

if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &hTokenSelf)) {
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE,
&hTokenSelf)) {
DWORD dwSessionId, dwRetLen;

if (GetTokenInformation(hTokenSelf, TokenSessionId, &dwSessionId, sizeof(dwSessionId), &dwRetLen)) {
if (GetTokenInformation(hTokenSelf, TokenSessionId, &dwSessionId,
sizeof(dwSessionId), &dwRetLen)) {
HANDLE hTokenSystem;

dwErr = DuplicateWinloginToken(dwSessionId, TOKEN_IMPERSONATE, &hTokenSystem);
dwErr = DuplicateWinloginToken(dwSessionId, TOKEN_IMPERSONATE,
&hTokenSystem);
if (ERROR_SUCCESS == dwErr) {
if (SetThreadToken(NULL, hTokenSystem)) {
if (DuplicateTokenEx(hTokenSelf, TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_DEFAULT, NULL, SecurityAnonymous, TokenPrimary, phToken)) {
if (DuplicateTokenEx(
hTokenSelf,
TOKEN_QUERY | TOKEN_DUPLICATE |
TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_DEFAULT,
NULL, SecurityAnonymous, TokenPrimary, phToken)) {
BOOL bUIAccess = TRUE;

if (!SetTokenInformation(*phToken, TokenUIAccess, &bUIAccess, sizeof(bUIAccess))) {
if (!SetTokenInformation(*phToken, TokenUIAccess,
&bUIAccess,
sizeof(bUIAccess))) {
dwErr = GetLastError();
CloseHandle(*phToken);
}
Expand Down Expand Up @@ -111,7 +130,8 @@ static BOOL CheckForUIAccess(DWORD *pdwErr, DWORD *pfUIAccess) {
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
DWORD dwRetLen;

if (GetTokenInformation(hToken, TokenUIAccess, pfUIAccess, sizeof(*pfUIAccess), &dwRetLen)) {
if (GetTokenInformation(hToken, TokenUIAccess, pfUIAccess,
sizeof(*pfUIAccess), &dwRetLen)) {
result = TRUE;
} else {
*pdwErr = GetLastError();
Expand Down Expand Up @@ -140,7 +160,9 @@ DWORD PrepareForUIAccess() {
PROCESS_INFORMATION pi;

GetStartupInfo(&si);
if (CreateProcessAsUser(hTokenUIAccess, NULL, GetCommandLine(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
if (CreateProcessAsUser(hTokenUIAccess, NULL, GetCommandLine(),
NULL, NULL, FALSE, 0, NULL, NULL, &si,
&pi)) {
CloseHandle(pi.hProcess), CloseHandle(pi.hThread);
ExitProcess(0);
} else {
Expand Down

0 comments on commit 43eb004

Please sign in to comment.