Skip to content

Commit

Permalink
* Use embedable version of python 3.5 (32-bit) to launch our server.
Browse files Browse the repository at this point in the history
* Add /debug command line argument to PIMELauncher.
  • Loading branch information
PCMan committed Feb 1, 2016
1 parent 572b0b2 commit c0ac279
Show file tree
Hide file tree
Showing 29 changed files with 32 additions and 24 deletions.
22 changes: 18 additions & 4 deletions PIMELauncher/PIMELauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <Windows.h>
#include <ShlObj.h>
#include <cstring>
#include <string>

using namespace std;

Expand All @@ -28,29 +29,42 @@ static const int MAX_FAILURES = 10;

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmd, int show) {
// FIXME: we should only allow running one instance of PIMELauncher
bool debug = false;
if (cmd && strstr(cmd, "/debug"))
debug = true;

int n_failures = 0;
int n_crashes = 0;
wchar_t dir_path[MAX_PATH];
wchar_t server_path[MAX_PATH];
DWORD len = GetModuleFileNameW(NULL, dir_path, MAX_PATH);
dir_path[len] = '\0';
wchar_t* p = wcsrchr(dir_path, '\\');
if (!p)
return 1;
*p = '\0';

// when debugging, we use the console version of python
wchar_t python_path[MAX_PATH];
wcscpy(python_path, dir_path);
wcscat(python_path, debug ? L"\\python\\python.exe" : L"\\python\\pythonw.exe");
// build the full path of the server directory
wcscat(dir_path, L"\\server");
wcscpy(server_path, dir_path);
wcscat(server_path, L"\\server.py");

// build the full path of the server.py file and make it quoted
wchar_t server_path[MAX_PATH + 3];
wsprintf(server_path, L"\"%s\\server.py\"", dir_path);

// launch the python server again if it crashes
for (;;) {
SHELLEXECUTEINFOW info = { 0 };
info.cbSize = sizeof(info);
info.fMask = SEE_MASK_NOCLOSEPROCESS;
info.lpVerb = L"open";
info.lpFile = server_path;
info.lpFile = python_path;
info.lpParameters = server_path;
info.lpDirectory = dir_path;
info.nShow = SW_SHOWNORMAL;

if (ShellExecuteExW(&info)) {
// wait for the python server process to terminate
WaitForSingleObject(info.hProcess, INFINITE);
Expand Down
22 changes: 6 additions & 16 deletions installer/installer.nsi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;
; Copyright (C) 2013 - 2015 Hong Jen Yee (PCMan) <[email protected]>
; Copyright (C) 2013 - 2016 Hong Jen Yee (PCMan) <[email protected]>
;
; This library is free software; you can redistribute it and/or
; modify it under the terms of the GNU Library General Public
Expand Down Expand Up @@ -119,12 +119,6 @@ Function .onInstFailed
MessageBox MB_ICONSTOP|MB_OK "安裝發生錯誤,無法完成。$\n$\n可能有檔案正在使用中,暫時無法刪除或覆寫$\n$\n建議重新開機後,再次執行安裝程式。"
FunctionEnd

; called to show an error message when errors happen
;Function onInstError
; MessageBox MB_ICONSTOP|MB_OK "安裝發生錯誤,舊版可能有檔案正在使用中,暫時無法覆寫$\n$\n請重開機後,再次執行安裝程式。"
; Abort
;FunctionEnd

;Installer Sections
Section "PIME 輸入法" SecMain

Expand All @@ -135,16 +129,12 @@ Section "PIME 輸入法" SecMain

SetOverwrite on ; overwrite existing files
SetOutPath "$INSTDIR"
; FIXME: install python and pywin32 automatically as needed
; Download and install python 3.4.3
; nsisdl::download https://www.python.org/ftp/python/3.4.3/python-3.4.3.msi $0
; ExecWait '"msiexec" /i "$0"'

; Download and install pywin32
; nsisdl::download http://downloads.sourceforge.net/project/pywin32/pywin32/Build%20219/pywin32-219.win32-py3.4.exe?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpywin32%2Ffiles%2Fpywin32%2FBuild%2520219%2F&ts=1439740165 $0
; ExecWait "$0"

; Install an embedable version of python 3.
File /r "..\python"

; Install the python server and input method modules
; FIXME: maybe we should install the pyc files later?
File /r /x "__pycache__" "..\server"

; Install the launcher and monitor of the server
Expand All @@ -163,7 +153,6 @@ Section "PIME 輸入法" SecMain
ExecWait '"$SYSDIR\regsvr32.exe" /s "$INSTDIR\x86\PIMETextService.dll"'

; Launch the python server on startup
; TODO: write the PIMELauncher program
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "PIMELauncher" "$INSTDIR\PIMELauncher.exe"

;Store installation folder in the registry
Expand Down Expand Up @@ -199,6 +188,7 @@ Section "Uninstall"

RMDir /r "$INSTDIR\x86"
RMDir /r "$INSTDIR\server"
RMDIR /r "$INSTDIR\python"
Delete "$INSTDIR\PIMELauncher.exe"

DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\PIME"
Expand Down
8 changes: 4 additions & 4 deletions libpipe/libpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ void close_pipe(HANDLE pipe) {

int read_pipe(HANDLE pipe, char* buf, unsigned long len, unsigned long* error) {
DWORD read_len = 0;
ReadFile(pipe, buf, len, &read_len, NULL);
BOOL success = ReadFile(pipe, buf, len, &read_len, NULL);
if (error != nullptr)
*error = (unsigned long)GetLastError();
*error = success ? 0 : (unsigned long)GetLastError();
return (int)read_len;
}

int write_pipe(HANDLE pipe, const char* data, unsigned long len, unsigned long* error) {
DWORD write_len = 0;
WriteFile(pipe, data, len, &write_len, NULL);
BOOL success = WriteFile(pipe, data, len, &write_len, NULL);
if (error != nullptr)
*error = (unsigned long)GetLastError();
*error = success ? 0 : (unsigned long)GetLastError();
return (int)write_len;
}

Expand Down
Binary file added python/_bz2.pyd
Binary file not shown.
Binary file added python/_ctypes.pyd
Binary file not shown.
Binary file added python/_decimal.pyd
Binary file not shown.
Binary file added python/_elementtree.pyd
Binary file not shown.
Binary file added python/_hashlib.pyd
Binary file not shown.
Binary file added python/_lzma.pyd
Binary file not shown.
Binary file added python/_msi.pyd
Binary file not shown.
Binary file added python/_multiprocessing.pyd
Binary file not shown.
Binary file added python/_overlapped.pyd
Binary file not shown.
Binary file added python/_socket.pyd
Binary file not shown.
Binary file added python/_sqlite3.pyd
Binary file not shown.
Binary file added python/_ssl.pyd
Binary file not shown.
Binary file added python/pyexpat.pyd
Binary file not shown.
Binary file added python/python.exe
Binary file not shown.
Binary file added python/python3.dll
Binary file not shown.
Binary file added python/python35.dll
Binary file not shown.
Binary file added python/python35.zip
Binary file not shown.
Binary file added python/pythonw.exe
Binary file not shown.
1 change: 1 addition & 0 deletions python/pyvenv.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
applocal = true
Binary file added python/select.pyd
Binary file not shown.
Binary file added python/sqlite3.dll
Binary file not shown.
Binary file added python/unicodedata.pyd
Binary file not shown.
Binary file added python/vcruntime140.dll
Binary file not shown.
Binary file added python/winsound.pyd
Binary file not shown.
Binary file modified server/libpipe.dll
Binary file not shown.
3 changes: 3 additions & 0 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def run(self):
buf_len = c_ulong(512)
read_len = libpipe.read_pipe(pipe, self.buf, buf_len, pointer(error))
error = error.value
print("read: ", read_len, "error:", error)

# convert content in the read buffer to unicode
if read_len > 0:
Expand Down Expand Up @@ -147,7 +148,9 @@ def run(self):

data = bytes(reply, "UTF-8") # convert to UTF-8
data_len = c_ulong(len(data))
print("write reply:", data_len)
libpipe.write_pipe(pipe, data, data_len, None)
print("written!!")
except:
import traceback
# print callstatck to know where the exceptions is
Expand Down

0 comments on commit c0ac279

Please sign in to comment.