# mhwd Driver Config

NAME="video-virtualmachine"
INFO="X.org virtual machine support and open-vm-tools/virtualbox tools"
VERSION="2025.07.24"
FREEDRIVER="true"
PRIORITY="2"

CLASSIDS="0300"
# Virtualbox version 6.0 uses VMSVGA on Linux guests by default, which has VMWare's VENDORID.
# VENDOR VMWare=80ee Virtualbox=15AD Redhat(QXL)=1af4 Redhat(VirtIO)=1b36 cirrus=1013
VENDORIDS="80ee 15AD 1af4 1b36 1013"
DEVICEIDS="*"

# Dependencies, gtkmm3 is needed to enable copy/paste support with vmware, xf86-video-qxl is left out on purpose
DEPENDS="virtualbox-guest-utils open-vm-tools xf86-input-vmmouse spice-vdagent qemu-guest-agent"
OPTREMDEPENDS="gtkmm3"

gdm_disable_wayland()
{
	if [[ -e /etc/gdm/custom.conf ]]; then
		sed -i -e 's|#WaylandEnable=false|WaylandEnable=false|g' /etc/gdm/custom.conf
	fi
}

post_install()
{
	if [[ "$(systemd-detect-virt)" == "oracle" ]]; then
		# Virtualbox detected

		if [[ ! -d /run/openrc ]]; then
			# Load kernel modules and sync clock
			systemctl enable --now --no-block vboxservice.service
			systemctl enable --now --no-block vboxclient.service
		fi
		# We have to make /dev/vboxuser read-write, otherwise VBoxClient won't be able to connect. This is not done automatically until the next reboot.
		chmod 666 /dev/vboxuser
	else

		if [[ "$(systemd-detect-virt)" == "vmware" ]]; then
			# Vmware detected

			if [[ ! -d /run/openrc ]]; then
				systemctl enable --now --no-block vmtoolsd.service
			fi
		else
			# Not virtualbox or vmware. Start spice-vdagentd.socket to auto detect if spice is in use. Otherwise, do nothing.
			systemctl enable --now --no-block spice-vdagentd.socket
			systemctl enable --now --no-block qemu-guest-agent.service
			sed -i 's/MODULES=.*/MODULES=(qxl virtio-gpu virtio virtio_scsi virtio_blk virtio_pci virtio_net virtio_ring)/g' /etc/mkinitcpio.conf
			mkinitcpio -P
		fi
	fi
	gdm_disable_wayland
	mhwd-gpu --check
}

post_remove()
{
	if [[ "$(systemd-detect-virt)" == "oracle" ]]; then
		# Virtualbox detected

		if [[ ! -d /run/openrc ]]; then
			systemctl disable --now vboxservice.service
		fi
	elif [[ "$(systemd-detect-virt)" == "vmware" ]]; then
		# Vmware detected

		if [[ ! -d /run/openrc ]]; then
			systemctl disable --now vmtoolsd.service
		fi
	fi
	sed -i 's/MODULES=.*/MODULES=()/g' /etc/mkinitcpio.conf
	mkinitcpio -P
	mhwd-gpu --check
}
