Recuento/src/windows/launcher.c

112 lines
3.4 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <windows.h>
#include <glib.h>
#include <owlpath.h>
const char *keyboard_layouts_key = "System\\CurrentControlSet\\Control\\Keyboard Layouts\\00000809";
BYTE *wineGetKdbusContents (void) {
HKEY keyboard_key;
BYTE *keyboard_file = NULL;
DWORD keyboard_file_length = 0;
RegCreateKeyEx (HKEY_LOCAL_MACHINE, TEXT(keyboard_layouts_key), 0,
NULL, 0, KEY_READ, NULL, &keyboard_key, NULL);
LSTATUS status_search_keyboard_layout = RegGetValue (
keyboard_key, NULL, TEXT ("Layout File"), RRF_RT_ANY, NULL, keyboard_file, &keyboard_file_length);
if (status_search_keyboard_layout != ERROR_MORE_DATA || !keyboard_file_length) {
RegCloseKey (keyboard_key);
return NULL;
}
keyboard_file = g_malloc (sizeof *keyboard_file * keyboard_file_length);
status_search_keyboard_layout = RegGetValue (
keyboard_key, NULL, TEXT ("Layout File"), RRF_RT_ANY, NULL, keyboard_file, &keyboard_file_length);
if (status_search_keyboard_layout != ERROR_SUCCESS) {
RegCloseKey (keyboard_key);
return NULL;
}
status_search_keyboard_layout = RegQueryValueEx (
keyboard_key, TEXT ("Layout File"), 0, NULL, NULL, &keyboard_file_length);
if (keyboard_file[keyboard_file_length-1] == '\0') {
RegCloseKey (keyboard_key);
return keyboard_file;
}
printf ("The registry key HKEY_LOCAL_MACHINE\\%s is not null terminated\n", keyboard_layouts_key);
RegCloseKey (keyboard_key);
return NULL;
}
bool wineHasKbdus (void) {
OwlPath *system32 = owl_path_new_from_path ("C:\\\\Windows\\System32");
BYTE *kbdus = wineGetKdbusContents ();
if (kbdus == NULL) {
return false;
}
OwlPath *kbdus_path = owl_path_child (system32, (char *)kbdus, NULL);
if (owl_path_exists (kbdus_path)) {
return true;
}
printf ("KBDUS path %s does not exists\n", owl_path_get_path (kbdus_path));
return false;
}
bool isWine (void) {
HKEY wine_key;
LSTATUS status_search_wine = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
TEXT("Software\\Wine"), 0, KEY_READ, &wine_key);
RegCloseKey (wine_key);
if (status_search_wine == ERROR_SUCCESS) {
return true;
}
return false;
}
bool isInstallingKbdusNeeded (void) {
if (isWine ()) {
printf ("%s\n", "This is wine");
return !wineHasKbdus ();
}
return false;
}
void installKbdus (void) {
HKEY keyboard_key;
BYTE *kbdus_value = (BYTE *) "KBDUS_RECUENTO.DLL";
RegCreateKeyEx (HKEY_LOCAL_MACHINE, TEXT(keyboard_layouts_key), 0,
NULL, 0, KEY_SET_VALUE, NULL, &keyboard_key, NULL);
RegSetValueEx (keyboard_key, TEXT ("Layout File"), 0, REG_SZ, kbdus_value, strlen ((char *)kbdus_value) + 1);
OwlPath *kbdus_path = owl_path_new_from_path ("recuento\\\\libKBDUS.dll");
OwlPath *system32 = owl_path_new_from_path ("C:\\\\Windows\\System32");
OwlPath *system32_kbdus = owl_path_child (system32, (char *)kbdus_value, NULL);
owl_path_copy (kbdus_path, system32_kbdus, NULL);
}
int
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{
char* argument_list[] = { _fullpath(NULL, "recuento\\\\recuento_installer_windows.exe", 3000), NULL };
if (isInstallingKbdusNeeded ()) {
installKbdus ();
}
_putenv_s("DATADIR_RECUENTO", "recuento\\\\resources");
_putenv_s("GSETTINGS_SCHEMA_DIR", "share\\\\glib-2.0\\\\schemas");
_putenv_s("XDG_DATA_HOME", "share");
_putenv_s("XDG_CONFIG_DIRS", "etc");
int return_value = execvp(argument_list[0], argument_list);
if (return_value == -1) {
printf ("%s\n", strerror(errno));
return 1;
}
return 0;
}