Skip to content

add graphics event handling and undefine flags support#2

Merged
rafaelgieschke merged 3 commits into
virtnextfrom
VirGraphicsStuff
Nov 6, 2025
Merged

add graphics event handling and undefine flags support#2
rafaelgieschke merged 3 commits into
virtnextfrom
VirGraphicsStuff

Conversation

@z0Kng

@z0Kng z0Kng commented Oct 27, 2025

Copy link
Copy Markdown

No description provided.

@rafaelgieschke

Copy link
Copy Markdown
Member

@copilot review!

Copilot AI commented Nov 6, 2025

Copy link
Copy Markdown

@rafaelgieschke I've opened a new pull request, #3, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for additional libvirt domain operations and events, targeting .NET 9.0 compatibility. The changes include new native bindings for domain undefine flags and graphics events, improvements to the QEMU monitor command interface, and updated project configuration.

Key changes:

  • Added native bindings for domain undefine flags and graphics event callbacks
  • Refactored NativeVirQemu to use proper P/Invoke memory management with out parameters
  • Added .NET 9.0 as a target framework and updated package dependencies

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
virDomainUndefineFlagsValues.cs New enum defining flags for domain undefine operations
VirDomainEventGraphicsPhase.cs New enum and structs for graphics event handling
VirConnectDomainEventCallback.cs Added callback delegate for graphics events
NativeVirQemu.cs Refactored to static class with improved memory management
NativeVirDomain.cs Added UndefineFlags method with flags support
NativeVirConnect.cs Added DomainEventGraphicsRegisterAny method
LibvirtDomain.cs Updated to use new MonitorCommand signature
IDNT.AppBasics.Virtualization.Libvirt.csproj Added .NET 9.0 target and updated dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2 to +4
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imports System.Collections.Generic, System.Linq, and System.Threading.Tasks are unused in this file. Only System is needed for the [Flags] attribute. Remove the unused imports to keep the code clean.

Suggested change
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Copilot uses AI. Check for mistakes.
Comment on lines +2 to +4
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imports System.Collections.Generic, System.Linq, and System.Threading.Tasks are unused in this file. Only System and System.Runtime.InteropServices are required. Remove the unused imports.

Suggested change
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Copilot uses AI. Check for mistakes.
/// The callback signature to use when registering for an event of type VIR_DOMAIN_EVENT_ID_GRAPHICS with virConnectDomainEventRegisterAny()
/// </summary>
/// <param name="conn">virConnect connection </param>
/// <param name="dom">The domain on which the event occured</param>

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'occured' to 'occurred'.

Suggested change
/// <param name="dom">The domain on which the event occured</param>
/// <param name="dom">The domain on which the event occurred</param>

Copilot uses AI. Check for mistakes.
result = Marshal.PtrToStringUTF8(resultPtr);

// Free memory allocated by libvirt
NativeFunctions.Free(resultPtr);

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory allocated by libvirt should be freed using virFree (imported at line 43-44), not NativeFunctions.Free which calls the Windows C runtime's free(). Using the wrong free function can cause crashes or memory corruption. Replace with virFree(resultPtr);

Suggested change
NativeFunctions.Free(resultPtr);
virFree(resultPtr);

Copilot uses AI. Check for mistakes.
/// </summary>
/// <param name="domain">pointer to a defined domain</param>
/// <param name="flags">an OR'ed set of virDomainUndefineFlags</param>
/// <returns>0 in case of success, -1 in case of error

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XML documentation comment for the return value is incomplete - it's missing the closing </returns> tag. Add </returns> at the end of line 752.

Suggested change
/// <returns>0 in case of success, -1 in case of error
/// <returns>0 in case of success, -1 in case of error</returns>

Copilot uses AI. Check for mistakes.
result = Marshal.PtrToStringUTF8(resultPtr);

// Free memory allocated by libvirt
NativeFunctions.Free(resultPtr);

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this call with a call to managed code if possible.

Suggested change
NativeFunctions.Free(resultPtr);
virFree(resultPtr);

Copilot uses AI. Check for mistakes.
/// <param name="ff">optional function to deallocate opaque when not used anymore</param>
/// <returns>t shall take a reference to it, by calling virDomainRef. The reference can be released once the object is no longer required by calling virDomainFree. Returns 0 on success, -1 on failure</returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virConnectDomainEventRegisterAny")]
public static extern int DomainEventGraphicsRegisterAny(IntPtr conn, IntPtr pool, VirDomainEventID eventId,

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimise the use of unmanaged code.

Copilot uses AI. Check for mistakes.
Comment on lines +755 to 757
public static extern int UndefineFlags(IntPtr domain, VirDomainUndefineFlagsValues flags);

/// <summary>

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimise the use of unmanaged code.

Suggested change
public static extern int UndefineFlags(IntPtr domain, VirDomainUndefineFlagsValues flags);
/// <summary>
private static extern int UndefineFlags(IntPtr domain, VirDomainUndefineFlagsValues flags);
/// <summary>
/// Managed wrapper for UndefineFlags. Undefines a domain with the specified flags.
/// </summary>
/// <param name="domain">Pointer to a defined domain</param>
/// <param name="flags">An OR'ed set of virDomainUndefineFlags</param>
/// <returns>True if successful, false otherwise</returns>
public static bool UndefineDomainWithFlags(IntPtr domain, VirDomainUndefineFlagsValues flags)
{
int result = UndefineFlags(domain, flags);
return result == 0;
}
/// <summary>

Copilot uses AI. Check for mistakes.
{
private const int MaxStringLength = 1024;
[DllImport("libvirt-qemu.so.0", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virDomainQemuMonitorCommand")]
private static extern int virDomainQemuMonitorCommand(

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimise the use of unmanaged code.

Copilot uses AI. Check for mistakes.
Comment on lines +43 to +44
[DllImport("libvirt.so.0", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virFree")]
private static extern int virFree(IntPtr ptr);

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimise the use of unmanaged code.

Suggested change
[DllImport("libvirt.so.0", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virFree")]
private static extern int virFree(IntPtr ptr);

Copilot uses AI. Check for mistakes.
@rafaelgieschke rafaelgieschke merged commit 111978e into virtnext Nov 6, 2025
6 checks passed
@rafaelgieschke rafaelgieschke deleted the VirGraphicsStuff branch November 6, 2025 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants