From 544863b2d38adf0f538e2704ab749e0ad7fe6ba0 Mon Sep 17 00:00:00 2001 From: fuleyi Date: Thu, 5 Dec 2024 20:27:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20treeland=E7=8E=AF=E5=A2=83=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E5=88=87=E6=8D=A2=E4=B8=BApipewire=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit treeland环境强制切换为pipewire框架 Log: treeland环境强制切换为pipewire框架 pms: TASK-369471 --- audio1/audio.go | 65 ++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/audio1/audio.go b/audio1/audio.go index 3dbad4818..2e007816c 100644 --- a/audio1/audio.go +++ b/audio1/audio.go @@ -378,17 +378,18 @@ func sendNotify(icon, summary, body string) { func startAudioServer(service *dbusutil.Service) error { //var pulseaudioState string - var activeServices, deactiveServices []string + var activeServices, deactiveServices, needMaskedServices []string audioServers := map[string][]string{ pulseaudioService: pulseaudioServices, pipewireService: pipewireServices, } // 默认音频服务 - var defaultAudioService = pulseaudioService + var defaultAudioService = pipewireService // 如果是treeland,则默认使用pipewire服务 + var hasTreeland = false if os.Getenv("XDG_SESSION_TYPE") == "wayland" { - defaultAudioService = pipewireService + hasTreeland = true } // 查询已安装的音频服务状态 @@ -417,38 +418,36 @@ func startAudioServer(service *dbusutil.Service) error { } } } - - // 如果可激活列表中有默认音频服务,则激活该服务 + logger.Infof("get audio service, actived: %v, deactive: %v", activeServices, deactiveServices) var activeService string - if len(activeServices) > 0 { - // 如果存在默认音频服务,则优先使用默认 - if strv.Strv(activeServices).Contains(defaultAudioService) { - activeService = defaultAudioService + var found bool + activeService = defaultAudioService + needMaskedServices, found = strv.Strv(activeServices).Delete(defaultAudioService) + if !found { + if hasTreeland { + // 如果是treeland环境,只支持pipewire,需要强制切换 + // 如果不存在,则在masked 服务中查找 + // 场景:X11环境切换到treeland环境,需要强制切换音频服务为pipewire + found = strv.Strv(deactiveServices).Contains(defaultAudioService) + if !found { + err := fmt.Errorf("not found supported audio services") + return err + } + logger.Warning("ready to unmask service:", audioServers[defaultAudioService]) } else { - // 不是默认的,说明是手动改过配置 - activeService = activeServices[0] - } - } else { - // 如果可加载服务列表是空的,可能存在问题,为了能够正常启动服务,则到disable列表中查找 - if len(deactiveServices) > 0 { - if strv.Strv(deactiveServices).Contains(defaultAudioService) { - activeService = defaultAudioService + if len(activeServices) > 0 { + activeService = activeServices[0] } else { - // 异常场景,如果都是masked状态,应该选取其中一个激活 - activeService = deactiveServices[0] + err := fmt.Errorf("no active services found") + return err } - } else { - err := errors.New("no available audioservice found") - logger.Errorf(err.Error()) - return err } } - logger.Debug("load audio service:", activeService) // 将剩余可选的audio服务都mask - tmpDeactiveServices, _ := strv.Strv(append(activeServices, deactiveServices...)).Delete(activeService) - logger.Debug("deactive audio service:", tmpDeactiveServices) - for _, server := range tmpDeactiveServices { + logger.Infof("need to active audio service:", activeService) + logger.Infof("ready to deactive audio service:", needMaskedServices) + for _, server := range needMaskedServices { for _, deactiveService := range audioServers[server] { deactiveServicePath, err := systemd.GetUnit(0, deactiveService) if err == nil { @@ -485,6 +484,16 @@ func startAudioServer(service *dbusutil.Service) error { } } } + _, err := systemd.UnmaskUnitFiles(0, audioServers[activeService], false) + if err != nil { + logger.Warning("Failed to unmask unit files", activeServices, "\nError:", err) + return err + } + err = systemd.Reload(0) + if err != nil { + logger.Warning("Failed to reload unit files. Error:", err) + return err + } for _, activeService := range audioServers[activeService] { activeServicePath, err := systemd.GetUnit(0, activeService) @@ -503,7 +512,7 @@ func startAudioServer(service *dbusutil.Service) error { logger.Warning("failed to get audio server active state", err) return err } - logger.Warning("start audio service", activeService, state) + logger.Info("start audio service", activeService, state) if state != "active" { go func() {