optimize: basic operations for macOS

This commit is contained in:
XZB-1248
2022-07-09 08:11:12 +08:00
parent bb0712ba2a
commit 29380197de
5 changed files with 107 additions and 3 deletions

View File

@@ -54,6 +54,7 @@ func Start() {
var err error
if common.WSConn != nil {
common.WSConn.Close()
common.WSConn = nil
}
common.WSConn, err = connectWS()
if err != nil && !stop {
@@ -209,6 +210,11 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) {
if act, ok := handlers[pack.Act]; !ok {
common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|actionNotImplemented}`}, pack, wsConn)
} else {
defer func() {
if r := recover(); r != nil {
golog.Error(`Panic: `, r)
}
}()
act(pack, wsConn)
}
}

View File

@@ -0,0 +1,96 @@
//go:build darwin
// +build darwin
package basic
/*
#cgo LDFLAGS: -framework CoreServices -framework Carbon
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>
static OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend);
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend)
{
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
AppleEvent eventReply = {typeNull, NULL};
AppleEvent appleEventToSend = {typeNull, NULL};
OSStatus error = noErr;
error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);
if (error != noErr) {
return(error);
}
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
AEDisposeDesc(&targetDesc);
if (error != noErr) {
return(error);
}
error = AESend(&appleEventToSend, &eventReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
AEDisposeDesc(&appleEventToSend);
if (error != noErr) {
return(error);
}
AEDisposeDesc(&eventReply);
return(error);
}
*/
import "C"
import (
"errors"
"os/exec"
)
// I'm not familiar with macOS, that's all I can do.
func init() {
}
func Lock() error {
return errors.New(`${i18n|operationNotSupported}`)
}
func Logoff() error {
if C.SendAppleEventToSystemProcess(C.kAEReallyLogOut) == C.noErr {
return nil
} else {
return errors.New(`${i18n|operationNotSupported}`)
}
}
func Hibernate() error {
if C.SendAppleEventToSystemProcess(C.kAESleep) == C.noErr {
return nil
} else {
return errors.New(`${i18n|operationNotSupported}`)
}
}
func Suspend() error {
return errors.New(`${i18n|operationNotSupported}`)
}
func Restart() error {
if C.SendAppleEventToSystemProcess(C.kAERestart) == C.noErr {
return nil
} else {
return exec.Command(`reboot`).Run()
}
}
func Shutdown() error {
if C.SendAppleEventToSystemProcess(C.kAEShutDown) == C.noErr {
return nil
} else {
return exec.Command(`shutdown`).Run()
}
}

View File

@@ -1,4 +1,4 @@
//go:build !linux && !windows
//go:build !linux && !windows && !darwin
package basic

View File

@@ -76,8 +76,8 @@ func InputTerminal(pack modules.Packet) error {
return nil
}
terminal := val.(*terminal)
terminal.lastPack = time.Now().Unix()
terminal.pty.Write(data)
terminal.lastPack = time.Now().Unix()
return nil
}
@@ -123,6 +123,7 @@ func KillTerminal(pack modules.Packet) error {
return nil
}
terminal := val.(*terminal)
terminals.Remove(termUUID)
doKillTerminal(terminal)
return nil
}

View File

@@ -107,13 +107,13 @@ func InputTerminal(pack modules.Packet) error {
return nil
}
terminal := val.(*terminal)
terminal.lastPack = time.Now().Unix()
if len(data) == 1 && data[0] == '\x03' {
terminal.cmd.Process.Signal(os.Interrupt)
return nil
}
data, _ = decodeUTF8(data)
(*terminal.stdin).Write(data)
terminal.lastPack = time.Now().Unix()
return nil
}
@@ -133,6 +133,7 @@ func KillTerminal(pack modules.Packet) error {
return nil
}
terminal := val.(*terminal)
terminals.Remove(termUUID)
doKillTerminal(terminal)
return nil
}