<commit_before>// TheJIKLLite.cpp : Defines the entry point for the application.
//

// http://www.lotushints.com/2013/04/win32-hide-to-system-tray-part-3/

#include "stdafx.h"
#include "TheJIKLLite.h"
#include "NTKbdLites.h"

LayoutInfo layoutInfo;

HHOOK hookHandle;
UINT shellHookMessage;
BOOL hooked;
BOOL modified;

// *****************


int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// Check another instance
	HANDLE mutex = CreateMutex(NULL, FALSE, TEXT("TheJIKL"));
	if (GetLastError() == ERROR_ALREADY_EXISTS || GetLastError() == ERROR_ACCESS_DENIED)
	{
		return 1;
	}

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_THEJIKLLITE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_THEJIKLLITE));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	DeinitInstance();

	return (int) msg.wParam;
}


void TrayDrawIcon(HWND hWnd) {
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
	nid.hWnd = hWnd;
	nid.uID = TRAY_ICONUID;
	nid.uVersion = NOTIFYICON_VERSION;
	nid.uCallbackMessage = WM_TRAYMESSAGE;
	nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SMALL));
	LoadString(hInst, IDS_APP_TITLE, nid.szTip, 128);
	nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	Shell_NotifyIcon(NIM_ADD, &nid);
}

void TrayDeleteIcon(HWND hWnd) {
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
	nid.hWnd = hWnd;
	nid.uID = TRAY_ICONUID;
	Shell_NotifyIcon(NIM_DELETE, &nid);
}

void TrayLoadPopupMenu(HWND hWnd) {
	POINT cursor;
	HMENU hMenu;
	GetCursorPos(&cursor);
	hMenu = (HMENU) GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAYMENU)), 0);
	// hMenu = (HMENU) LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAYMENU));
	SetMenuDefaultItem(hMenu, IDM_TRAY_EXIT, FALSE);
	TrackPopupMenu(hMenu, TPM_LEFTALIGN, cursor.x, cursor.y, 0, hWnd, NULL);
}

BOOL ReadLayouts()
{
	const int MAX_LAYOUTS = 2;

	int nElements = GetKeyboardLayoutList(0, nullptr);

	if (nElements < MAX_LAYOUTS)
	{
		return FALSE;
	}

	// var ids = new IntPtr[nElements];
	layoutInfo.lpList = (HKL*)LocalAlloc(LPTR, (nElements * sizeof(HKL)));
	// layoutInfo.lpList = new HKL[MAX_LAYOUTS];

	nElements = GetKeyboardLayoutList(nElements, layoutInfo.lpList);
	if (MAX_LAYOUTS > nElements) {
		return FALSE;
	}

	layoutInfo.first = TRUE;

	return TRUE;
}

void ChangeLayout(HKL hLayout)
{
	GUITHREADINFO threadInfo;
	threadInfo.cbSize = sizeof(threadInfo);
    GetGUIThreadInfo(0, &threadInfo);

    HWND hWnd = (threadInfo.hwndFocus != NULL) ? threadInfo.hwndFocus : threadInfo.hwndActive;
    // PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, (LPARAM)hLayout);
    PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, reinterpret_cast<LPARAM>(hLayout));

	/*
	DWORD recipients = BSM_APPLICATIONS;
	return (0 < ::BroadcastSystemMessage(BSF_POSTMESSAGE, &recipients, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, reinterpret_cast<LPARAM>(mozc_hkl)));
	*/
}

void SetHook(HWND hWnd)
{
	hooked = false;
	modified = false;
	hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHandler, hInst, 0);

	shellHookMessage = RegisterWindowMessage(TEXT("SHELLHOOK"));
	RegisterShellHookWindow(hWnd);
}

void UnSetHook()
{
	if (hookHandle != NULL) {
		UnhookWindowsHookEx(hookHandle);
	}
}<commit_msg>Improving handle selection<commit_after>// TheJIKLLite.cpp : Defines the entry point for the application.
//

// http://www.lotushints.com/2013/04/win32-hide-to-system-tray-part-3/

#include "stdafx.h"
#include "TheJIKLLite.h"
#include "NTKbdLites.h"

LayoutInfo layoutInfo;

HHOOK hookHandle;
UINT shellHookMessage;
BOOL hooked;
BOOL modified;

// *****************


int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// Check another instance
	HANDLE mutex = CreateMutex(NULL, FALSE, TEXT("TheJIKL"));
	if (GetLastError() == ERROR_ALREADY_EXISTS || GetLastError() == ERROR_ACCESS_DENIED)
	{
		return 1;
	}

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_THEJIKLLITE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_THEJIKLLITE));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	DeinitInstance();

	return (int) msg.wParam;
}


void TrayDrawIcon(HWND hWnd) {
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
	nid.hWnd = hWnd;
	nid.uID = TRAY_ICONUID;
	nid.uVersion = NOTIFYICON_VERSION;
	nid.uCallbackMessage = WM_TRAYMESSAGE;
	nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SMALL));
	LoadString(hInst, IDS_APP_TITLE, nid.szTip, 128);
	nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	Shell_NotifyIcon(NIM_ADD, &nid);
}

void TrayDeleteIcon(HWND hWnd) {
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
	nid.hWnd = hWnd;
	nid.uID = TRAY_ICONUID;
	Shell_NotifyIcon(NIM_DELETE, &nid);
}

void TrayLoadPopupMenu(HWND hWnd) {
	POINT cursor;
	HMENU hMenu;
	GetCursorPos(&cursor);
	hMenu = (HMENU) GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAYMENU)), 0);
	// hMenu = (HMENU) LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAYMENU));
	SetMenuDefaultItem(hMenu, IDM_TRAY_EXIT, FALSE);
	TrackPopupMenu(hMenu, TPM_LEFTALIGN, cursor.x, cursor.y, 0, hWnd, NULL);
}

BOOL ReadLayouts()
{
	const int MAX_LAYOUTS = 2;
	int nElements = GetKeyboardLayoutList(0, nullptr);
	if (nElements < MAX_LAYOUTS)
	{
		return FALSE;
	}

	// var ids = new IntPtr[nElements];
	layoutInfo.lpList = (HKL*)LocalAlloc(LPTR, (nElements * sizeof(HKL)));
	// layoutInfo.lpList = new HKL[MAX_LAYOUTS];

	nElements = GetKeyboardLayoutList(nElements, layoutInfo.lpList);
	if (MAX_LAYOUTS > nElements) {
		return FALSE;
	}

	layoutInfo.first = TRUE;

	return TRUE;
}

void ChangeLayout(HKL hLayout)
{
	GUITHREADINFO threadInfo;
	threadInfo.cbSize = sizeof(threadInfo);
    GetGUIThreadInfo(0, &threadInfo);

	/*
    HWND hWnd = (threadInfo.hwndFocus != NULL) ? threadInfo.hwndFocus : threadInfo.hwndActive;
    PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, reinterpret_cast<LPARAM>(hLayout));
	*/

	HWND hWnd = NULL;
	if (threadInfo.hwndCaret != NULL) {
		hWnd = threadInfo.hwndCaret;
	}
	else if (threadInfo.hwndFocus != NULL) {
		hWnd = threadInfo.hwndFocus;
	}
	else if (threadInfo.hwndActive != NULL) {
		hWnd = threadInfo.hwndActive;
	}

	if (hWnd != NULL) {
		PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, reinterpret_cast<LPARAM>(hLayout));
	}

	/*
    PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, (LPARAM)hLayout);

	DWORD recipients = BSM_APPLICATIONS;
	return (0 < ::BroadcastSystemMessage(BSF_POSTMESSAGE, &recipients, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_SYSCHARSET, reinterpret_cast<LPARAM>(mozc_hkl)));
	*/
}

void SetHook(HWND hWnd)
{
	hooked = false;
	modified = false;
	hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHandler, hInst, 0);

	shellHookMessage = RegisterWindowMessage(TEXT("SHELLHOOK"));
	RegisterShellHookWindow(hWnd);
}

void UnSetHook()
{
	if (hookHandle != NULL) {
		UnhookWindowsHookEx(hookHandle);
	}
}
{'fraction_non_alphanumeric': 0.12090063631913853, 'fraction_numerical': 0.0053842388644150755, 'mean_word_length': 5.04959289415248, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3714513', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3170, 'n_words': 563}