Skip to content

Commit 097bc2b

Browse files
committed
Moved the syscall to openApplicationAtURL inside Systray
1 parent 1c0d413 commit 097bc2b

File tree

4 files changed

+88
-28
lines changed

4 files changed

+88
-28
lines changed

systray/exec_darwin.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package systray
2+
3+
/*
4+
#cgo CFLAGS: -x objective-c
5+
#cgo LDFLAGS: -framework Cocoa
6+
#import <Cocoa/Cocoa.h>
7+
8+
char **makeCharArray(int size) {
9+
return calloc(sizeof(char*), size);
10+
}
11+
12+
void setCharArray(char **a, int n, char *s) {
13+
a[n] = s;
14+
}
15+
16+
void freeCharArray(char **a, int size) {
17+
int i;
18+
for (i = 0; i < size; i++) {
19+
free(a[i]);
20+
}
21+
free(a);
22+
}
23+
24+
void runApplication(const char *path, const char **argv, int argc) {
25+
NSMutableArray<NSString *> *stringArray = [NSMutableArray array];
26+
for (int i=0; i<argc; i++) {
27+
NSString *arg = [NSString stringWithCString:argv[i] encoding:NSUTF8StringEncoding];
28+
[stringArray addObject:arg];
29+
}
30+
NSArray<NSString *> *arguments = [NSArray arrayWithArray:stringArray];
31+
32+
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
33+
NSURL *url = [NSURL fileURLWithPath:@(path) isDirectory:NO];
34+
35+
NSWorkspaceOpenConfiguration* configuration = [NSWorkspaceOpenConfiguration new];
36+
//[configuration setEnvironment:env];
37+
[configuration setPromptsUserIfNeeded:YES];
38+
[configuration setCreatesNewApplicationInstance:YES];
39+
[configuration setArguments:arguments];
40+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
41+
[ws openApplicationAtURL:url configuration:configuration completionHandler:^(NSRunningApplication* app, NSError* error) {
42+
dispatch_semaphore_signal(semaphore);
43+
}];
44+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
45+
}
46+
*/
47+
import "C"
48+
49+
func execApp(path string, args ...string) error {
50+
argc := C.int(len(args))
51+
argv := C.makeCharArray(argc)
52+
for i, arg := range args {
53+
C.setCharArray(argv, C.int(i), C.CString(arg))
54+
}
55+
56+
C.runApplication(C.CString(path), argv, argc)
57+
58+
C.freeCharArray(argv, argc)
59+
return nil
60+
}

systray/exec_default.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2022 Arduino SA
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published
5+
// by the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
//go:build !darwin
17+
18+
package systray
19+
20+
import "os/exec"
21+
22+
// default execApp from golang
23+
func execApp(path string, args ...string) error {
24+
cmd := exec.Command(path, args...)
25+
return cmd.Start()
26+
}

systray/systray.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package systray
1818
import (
1919
"fmt"
2020
"os"
21-
"os/exec"
2221
"strings"
2322

2423
"github.com/arduino/go-paths-helper"
@@ -69,8 +68,7 @@ func (s *Systray) Restart() {
6968
}
7069

7170
// Launch executable
72-
cmd := exec.Command(s.path, args...)
73-
err := cmd.Start()
71+
err := execApp(s.path, args...)
7472
if err != nil {
7573
log.Printf("Error restarting process: %v\n", err)
7674
return

updater/updater_darwin.go

+1-25
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,6 @@
1515

1616
package updater
1717

18-
/*
19-
#cgo CFLAGS: -x objective-c
20-
#cgo LDFLAGS: -framework Cocoa
21-
#import <Cocoa/Cocoa.h>
22-
23-
void runApplication(const char *path) {
24-
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
25-
NSURL *url = [NSURL fileURLWithPath:@(path) isDirectory:NO];
26-
27-
NSWorkspaceOpenConfiguration* configuration = [NSWorkspaceOpenConfiguration new];
28-
//[configuration setEnvironment:env];
29-
[configuration setPromptsUserIfNeeded:YES];
30-
[configuration setCreatesNewApplicationInstance:YES];
31-
32-
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
33-
[ws openApplicationAtURL:url configuration:configuration completionHandler:^(NSRunningApplication* app, NSError* error) {
34-
dispatch_semaphore_signal(semaphore);
35-
}];
36-
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
37-
}
38-
*/
39-
import "C"
40-
4118
import (
4219
"bytes"
4320
"context"
@@ -179,8 +156,7 @@ func checkForUpdates(currentVersion string, updateURL string, cmdName string) (s
179156

180157
// Restart agent
181158
logrus.WithField("path", currentAppPath).Info("Running new app")
182-
C.runApplication(C.CString(currentAppPath.String()))
183159

184160
// Close old agent
185-
return "quit", nil
161+
return currentAppPath.String(), nil
186162
}

0 commit comments

Comments
 (0)