Skip to content

Commit

Permalink
Merge pull request #173 from Code-Hex/add/graphic-display-rich
Browse files Browse the repository at this point in the history
make more rich graphical window
  • Loading branch information
AkihiroSuda authored Nov 19, 2024
2 parents e20c903 + 8dcce62 commit 179f6bb
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 25 deletions.
2 changes: 1 addition & 1 deletion example/gui-linux/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func run(ctx context.Context) error {
}

runtime.LockOSThread()
vm.StartGraphicApplication(960, 600)
vm.StartGraphicApplication(960, 600, vz.WithWindowTitle("Linux"), vz.WithController(true))
runtime.UnlockOSThread()

cleanup()
Expand Down
2 changes: 1 addition & 1 deletion example/macOS/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func runVM(ctx context.Context) error {
log.Println("finished cleanup")
}

vm.StartGraphicApplication(960, 600)
vm.StartGraphicApplication(960, 600, vz.WithWindowTitle("macOS"), vz.WithController(true))

cleanup()

Expand Down
43 changes: 41 additions & 2 deletions virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,55 @@ func (v *VirtualMachine) Stop() error {
return <-errCh
}

type startGraphicApplicationOptions struct {
title string
enableController bool
}

// StartGraphicApplicationOption is an option for display graphics start.
type StartGraphicApplicationOption func(*startGraphicApplicationOptions) error

// WithWindowTitle is an option to set window title of display graphics window.
func WithWindowTitle(title string) StartGraphicApplicationOption {
return func(sgao *startGraphicApplicationOptions) error {
sgao.title = title
return nil
}
}

// WithController is an option to set virtual machine controller on graphics window toolbar.
func WithController(enable bool) StartGraphicApplicationOption {
return func(sgao *startGraphicApplicationOptions) error {
sgao.enableController = enable
return nil
}
}

// StartGraphicApplication starts an application to display graphics of the VM.
//
// You must to call runtime.LockOSThread before calling this method.
//
// This is only supported on macOS 12 and newer, error will be returned on older versions.
func (v *VirtualMachine) StartGraphicApplication(width, height float64) error {
func (v *VirtualMachine) StartGraphicApplication(width, height float64, opts ...StartGraphicApplicationOption) error {
if err := macOSAvailable(12); err != nil {
return err
}
C.startVirtualMachineWindow(objc.Ptr(v), C.double(width), C.double(height))
defaultOpts := &startGraphicApplicationOptions{}
for _, opt := range opts {
if err := opt(defaultOpts); err != nil {
return err
}
}
windowTitle := charWithGoString(defaultOpts.title)
defer windowTitle.Free()
C.startVirtualMachineWindow(
objc.Ptr(v),
v.dispatchQueue,
C.double(width),
C.double(height),
windowTitle.CString(),
C.bool(defaultOpts.enableController),
)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion virtualization_12.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ void setKeyboardsVZVirtualMachineConfiguration(void *config,
void setAudioDevicesVZVirtualMachineConfiguration(void *config,
void *audioDevices);

void startVirtualMachineWindow(void *machine, double width, double height);
void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController);
8 changes: 6 additions & 2 deletions virtualization_12.m
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,22 @@ void setVZVirtioFileSystemDeviceConfigurationShare(void *config, void *share)
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

void startVirtualMachineWindow(void *machine, double width, double height)
void startVirtualMachineWindow(void *machine, void *queue, double width, double height, const char *title, bool enableController)
{
// Create a shared app instance.
// This will initialize the global variable
// 'NSApp' with the application instance.
[VZApplication sharedApplication];
if (@available(macOS 12, *)) {
@autoreleasepool {
NSString *windowTitle = [NSString stringWithUTF8String:title];
AppDelegate *appDelegate = [[[AppDelegate alloc]
initWithVirtualMachine:(VZVirtualMachine *)machine
queue:(dispatch_queue_t)queue
windowWidth:(CGFloat)width
windowHeight:(CGFloat)height] autorelease];
windowHeight:(CGFloat)height
windowTitle:windowTitle
enableController:enableController] autorelease];

NSApp.delegate = appDelegate;
[NSApp run];
Expand Down
7 changes: 5 additions & 2 deletions virtualization_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
@end

API_AVAILABLE(macos(12.0))
@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate, VZVirtualMachineDelegate>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate, VZVirtualMachineDelegate, NSToolbarDelegate>
- (instancetype)initWithVirtualMachine:(VZVirtualMachine *)virtualMachine
queue:(dispatch_queue_t)queue
windowWidth:(CGFloat)windowWidth
windowHeight:(CGFloat)windowHeight;
windowHeight:(CGFloat)windowHeight
windowTitle:(NSString *)windowTitle
enableController:(BOOL)enableController;
@end
Loading

0 comments on commit 179f6bb

Please sign in to comment.