81 lines
3.1 KiB
C++
81 lines
3.1 KiB
C++
#include <winsock2.h>
|
|
#include "files_text_window.h"
|
|
#include "lan_files.h"
|
|
#include "platform.h"
|
|
#include <shellapi.h>
|
|
#include <thread>
|
|
|
|
int WINAPI wWinMain(HINSTANCE instance,HINSTANCE,PWSTR,int)
|
|
{
|
|
int count{};
|
|
auto args=CommandLineToArgvW(GetCommandLineW(),&count);
|
|
if(!args || count!=2)
|
|
{
|
|
if(args) LocalFree(args);
|
|
return 1;
|
|
}
|
|
const std::filesystem::path document=args[1];
|
|
LocalFree(args);
|
|
if(FAILED(CoInitializeEx(nullptr,COINIT_APARTMENTTHREADED))) return 1;
|
|
WSADATA network{};
|
|
if(WSAStartup(MAKEWORD(2,2),&network))
|
|
{
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
int result=0;
|
|
try
|
|
{
|
|
using namespace ks;
|
|
using namespace ks::lan;
|
|
const auto serverId=randomHex(16), clientId=randomHex(16), key=randomHex(32);
|
|
FilesListener listener(serverId,[&](const auto &id){return id==clientId?key:std::string{};},0);
|
|
if(!listener.port()) throw std::runtime_error("Could not bind the LAN test listener.");
|
|
const auto pairing=Json{{"enabled",true},{"id",clientId},
|
|
{"trusted",{{serverId,{{"key",key}}}}}}.dump();
|
|
atomicWrite(dataDirectory()/L"lan-pairing.bin",protect(Bytes(pairing.begin(),pairing.end())));
|
|
const FilesPeer peer{serverId,"127.0.0.1",L"Loopback peer",listener.port()};
|
|
std::jthread server([&](std::stop_token stop) {
|
|
while(!stop.stop_requested()) { listener.poll(); Sleep(5); }
|
|
});
|
|
WNDCLASSW type{};
|
|
type.hInstance=instance;
|
|
type.lpszClassName=L"KSwitcher.Files.MarkdownFixture";
|
|
type.lpfnWndProc=[](HWND window,UINT message,WPARAM wparam,LPARAM lparam)->LRESULT {
|
|
if(message==WM_DESTROY) { files::textWindowOwnerClosed(); return 0; }
|
|
return DefWindowProcW(window,message,wparam,lparam);
|
|
};
|
|
if(!RegisterClassW(&type)) throw std::runtime_error("Could not register the LAN fixture.");
|
|
const auto owner=CreateWindowW(type.lpszClassName,L"LAN Markdown fixture",WS_OVERLAPPEDWINDOW,
|
|
20,20,960,720,nullptr,nullptr,instance,nullptr);
|
|
if(!owner) throw std::runtime_error("Could not open the LAN fixture.");
|
|
files::TextDialogs dialogs{};
|
|
dialogs.readFont=[] {
|
|
files::TextWindowFont font{};
|
|
wcscpy_s(font.font.lfFaceName,L"Consolas");
|
|
font.tenthsOfPoint=100;
|
|
return font;
|
|
};
|
|
dialogs.confirmExisting=[](HWND){return true;};
|
|
dialogs.confirmSave=[](HWND)->INT_PTR {return IDYES;};
|
|
dialogs.message=[](HWND,const wchar_t *message){log(message);};
|
|
dialogs.dismissOwned=[](HWND){return false;};
|
|
files::openTextWindow(owner,document,false,dialogs,&peer);
|
|
DestroyWindow(owner);
|
|
MSG message{};
|
|
while(GetMessageW(&message,nullptr,0,0)>0)
|
|
{
|
|
TranslateMessage(&message);
|
|
DispatchMessageW(&message);
|
|
}
|
|
}
|
|
catch(const std::exception &error)
|
|
{
|
|
ks::log(L"LAN Markdown fixture: "+ks::wide(error.what()));
|
|
result=1;
|
|
}
|
|
WSACleanup();
|
|
CoUninitialize();
|
|
return result;
|
|
}
|