-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mainloop: add optional C implementation
This uses a specific build tag so as not to be on by default.
- Loading branch information
Showing
4 changed files
with
109 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2019 The Walk Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows,walk_use_cgo | ||
|
||
package walk | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/lxn/win" | ||
) | ||
|
||
// #include <windows.h> | ||
// | ||
// extern void shimRunSynchronized(void); | ||
// extern unsigned char shimHandleKeyDown(uintptr_t fb, uintptr_t m); | ||
// | ||
// static int mainloop(uintptr_t handle_ptr, uintptr_t fb_ptr) | ||
// { | ||
// HANDLE *hwnd = (HANDLE *)handle_ptr; | ||
// MSG m; | ||
// int r; | ||
// | ||
// while (*hwnd) { | ||
// r = GetMessage(&m, NULL, 0, 0); | ||
// if (!r) | ||
// return m.wParam; | ||
// else if (r < 0) | ||
// return -1; | ||
// if (m.message == WM_KEYDOWN && shimHandleKeyDown(fb_ptr, (uintptr_t)&m)) | ||
// continue; | ||
// if (!IsDialogMessage(*hwnd, &m)) { | ||
// TranslateMessage(&m); | ||
// DispatchMessage(&m); | ||
// } | ||
// shimRunSynchronized(); | ||
// } | ||
// return 0; | ||
// } | ||
import "C" | ||
|
||
//export shimHandleKeyDown | ||
func shimHandleKeyDown(fb uintptr, msg uintptr) bool { | ||
return (*FormBase)(unsafe.Pointer(fb)).handleKeyDown((*win.MSG)(unsafe.Pointer(msg))) | ||
} | ||
|
||
//export shimRunSynchronized | ||
func shimRunSynchronized() { | ||
runSynchronized() | ||
} | ||
|
||
func (fb *FormBase) mainLoop() int { | ||
return int(C.mainloop(C.uintptr_t(uintptr(unsafe.Pointer(&fb.hWnd))), C.uintptr_t(uintptr(unsafe.Pointer(fb))))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2019 The Walk Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows,!walk_use_cgo | ||
|
||
package walk | ||
|
||
import ( | ||
"github.com/lxn/win" | ||
"unsafe" | ||
) | ||
|
||
func (fb *FormBase) mainLoop() int { | ||
msg := (*win.MSG)(unsafe.Pointer(win.GlobalAlloc(0, unsafe.Sizeof(win.MSG{})))) | ||
defer win.GlobalFree(win.HGLOBAL(unsafe.Pointer(msg))) | ||
|
||
for fb.hWnd != 0 { | ||
switch win.GetMessage(msg, 0, 0, 0) { | ||
case 0: | ||
return int(msg.WParam) | ||
|
||
case -1: | ||
return -1 | ||
} | ||
|
||
switch msg.Message { | ||
case win.WM_KEYDOWN: | ||
if fb.handleKeyDown(msg) { | ||
continue | ||
} | ||
} | ||
|
||
if !win.IsDialogMessage(fb.hWnd, msg) { | ||
win.TranslateMessage(msg) | ||
win.DispatchMessage(msg) | ||
} | ||
|
||
runSynchronized() | ||
} | ||
|
||
return 0 | ||
} |