Blob Blame History Raw
From da3febed1796f3ee81a8a623f535c2496539094e Mon Sep 17 00:00:00 2001
From: hubenchang0515 <hubenchang@uniontech.com>
Date: Mon, 21 Sep 2020 15:32:21 +0800
Subject: [PATCH 3/9] fix(backlight): remove DDCCI
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

DDCCI设置亮度太慢,导致了很多问题,现在删除

Log: 删除DDCCI
Bug: https://pms.uniontech.com/zentao/bug-view-48540.html
Change-Id: I87474b13743f68c3a39ecf33e1f3a97ab934a78c
Reviewed-on: http://gerrit.uniontech.com/c/dde-daemon/+/5696
Reviewed-by: <mailman@uniontech.com>
Reviewed-by: quezhiyong <quezhiyong@uniontech.com>
Tested-by: <mailman@uniontech.com>
---
 bin/backlight_helper/ddcci/ddcci.go   | 180 --------------------------
 bin/backlight_helper/ddcci/manager.go |  96 --------------
 bin/backlight_helper/main.go          |  13 +-
 3 files changed, 1 insertion(+), 288 deletions(-)
 delete mode 100644 bin/backlight_helper/ddcci/ddcci.go
 delete mode 100644 bin/backlight_helper/ddcci/manager.go

diff --git a/bin/backlight_helper/ddcci/ddcci.go b/bin/backlight_helper/ddcci/ddcci.go
deleted file mode 100644
index cd4a66da..00000000
--- a/bin/backlight_helper/ddcci/ddcci.go
+++ /dev/null
@@ -1,180 +0,0 @@
-package ddcci
-
-// #cgo pkg-config: ddcutil
-// #include <ddcutil_c_api.h>
-import "C"
-import (
-	"fmt"
-	"sync"
-	"unsafe"
-
-	"pkg.deepin.io/lib/utils"
-)
-
-type ddcci struct {
-	listPointer *C.DDCA_Display_Info_List
-	listMu      sync.Mutex
-
-	displayMap map[string]int
-}
-
-const (
-	brightnessVCP = 0x10
-)
-
-func newDDCCI() (*ddcci, error) {
-	ddc := &ddcci{
-		displayMap: make(map[string]int),
-	}
-
-	status := C.ddca_set_max_tries(C.DDCA_MULTI_PART_TRIES, 5)
-	if status < C.int(0) {
-		return nil, fmt.Errorf("Error setting retries: %d", status)
-	}
-
-	err := ddc.RefreshDisplays()
-	if err != nil {
-		return nil, err
-	}
-
-	return ddc, nil
-}
-
-func (d *ddcci) freeList() {
-	if d.listPointer != nil {
-		C.ddca_free_display_info_list(d.listPointer)
-		d.listPointer = nil
-	}
-}
-
-func (d *ddcci) RefreshDisplays() error {
-	d.listMu.Lock()
-	defer d.listMu.Unlock()
-
-	d.freeList()
-
-	status := C.ddca_get_display_info_list2(C.bool(true), &d.listPointer)
-	if status != C.int(0) {
-		return fmt.Errorf("failed to get display info list: %d", status)
-	}
-
-	for i := 0; i < int(d.listPointer.ct); i++ {
-		err := d.initDisplay(i)
-		if err != nil {
-			logger.Warning(err)
-		}
-	}
-
-	return nil
-}
-
-func (d *ddcci) initDisplay(idx int) error {
-	item := d.getDisplayInfoByIdx(idx)
-
-	var handle C.DDCA_Display_Handle
-	status := C.ddca_open_display2(item.dref, C.bool(true), &handle)
-	if status != C.int(0) {
-		return fmt.Errorf("failed to open monitor: %d", status)
-	}
-
-	defer C.ddca_close_display(handle)
-
-	var val C.DDCA_Non_Table_Vcp_Value
-	status = C.ddca_get_non_table_vcp_value(handle, brightnessVCP, &val)
-	if status != C.int(0) {
-		return fmt.Errorf("failed to check DDC/CI support: %d", status)
-	}
-
-	edid := C.GoBytes(unsafe.Pointer(&item.edid_bytes), 128)
-	edidChecksum := getEDIDChecksum(edid)
-
-	d.displayMap[string(edidChecksum)] = idx
-	return nil
-}
-
-func (d *ddcci) SupportBrightness(edidChecksum string) bool {
-	d.listMu.Lock()
-	_, ok := d.displayMap[edidChecksum]
-	d.listMu.Unlock()
-
-	return ok
-}
-
-func (d *ddcci) GetBrightness(edidChecksum string) (brightness int, err error) {
-	d.listMu.Lock()
-	defer d.listMu.Unlock()
-
-	idx, ok := d.displayMap[edidChecksum]
-	if !ok {
-		err = fmt.Errorf("monitor not support DDC/CI")
-		return
-	}
-
-	item := d.getDisplayInfoByIdx(idx)
-
-	var handle C.DDCA_Display_Handle
-	status := C.ddca_open_display2(item.dref, C.bool(true), &handle)
-	if status != C.int(0) {
-		err = fmt.Errorf("failed to open monitor: %d", status)
-		return
-	}
-
-	defer C.ddca_close_display(handle)
-
-	var val C.DDCA_Non_Table_Vcp_Value
-	status = C.ddca_get_non_table_vcp_value(handle, brightnessVCP, &val)
-	if status != C.int(0) {
-		err = fmt.Errorf("failed to get brightness: %d", status)
-		return
-	}
-
-	brightness = int(val.sl)
-	return
-}
-
-func (d *ddcci) SetBrightness(edidChecksum string, percent int) error {
-	d.listMu.Lock()
-	defer d.listMu.Unlock()
-
-	idx, ok := d.displayMap[edidChecksum]
-	if !ok {
-		return fmt.Errorf("monitor not support DDC/CI")
-	}
-
-	item := d.getDisplayInfoByIdx(idx)
-
-	var handle C.DDCA_Display_Handle
-	status := C.ddca_open_display2(item.dref, C.bool(true), &handle)
-	if status != C.int(0) {
-		return fmt.Errorf("failed to open monitor: %d", status)
-	}
-
-	defer C.ddca_close_display(handle)
-
-	// 开启结果验证,防止返回设置成功,但实际上没有生效的情况
-	// 此方法仅对当前线程生效
-	C.ddca_enable_verify(true)
-
-	status = C.ddca_set_non_table_vcp_value(handle, brightnessVCP, 0, C.uchar(percent))
-	if status != C.int(0) {
-		return fmt.Errorf("failed to set brightness via DDC/CI: %d", status)
-	}
-
-	return nil
-}
-
-func (d *ddcci) getDisplayInfoByIdx(idx int) *C.DDCA_Display_Info {
-	start := unsafe.Pointer(uintptr(unsafe.Pointer(d.listPointer)) + uintptr(C.sizeof_DDCA_Display_Info_List))
-	size := uintptr(C.sizeof_DDCA_Display_Info)
-
-	return (*C.DDCA_Display_Info)(unsafe.Pointer(uintptr(start) + size*uintptr(idx)))
-}
-
-func getEDIDChecksum(edid []byte) string {
-	if len(edid) < 128 {
-		return ""
-	}
-
-	id, _ := utils.SumStrMd5(string(edid[:128]))
-	return id
-}
diff --git a/bin/backlight_helper/ddcci/manager.go b/bin/backlight_helper/ddcci/manager.go
deleted file mode 100644
index e87083d2..00000000
--- a/bin/backlight_helper/ddcci/manager.go
+++ /dev/null
@@ -1,96 +0,0 @@
-package ddcci
-
-import (
-	"fmt"
-	"runtime"
-	"strings"
-	"sync"
-
-	dbus "github.com/godbus/dbus"
-	"pkg.deepin.io/lib/dbusutil"
-	"pkg.deepin.io/lib/log"
-)
-
-const (
-	DbusPath      = "/com/deepin/daemon/helper/Backlight/DDCCI"
-	dbusInterface = "com.deepin.daemon.helper.Backlight.DDCCI"
-)
-
-var logger = log.NewLogger("backlight_helper/ddcci")
-
-type Manager struct {
-	ddcci   *ddcci
-	PropsMu sync.RWMutex
-
-	methods *struct { //nolint
-		CheckSupport    func() `in:"edidChecksum" out:"support"`
-		GetBrightness   func() `in:"edidChecksum" out:"value"`
-		SetBrightness   func() `in:"edidChecksum,value"`
-		RefreshDisplays func()
-	}
-}
-
-func NewManager() (*Manager, error) {
-	m := &Manager{}
-
-	// 在 arm 和 mips 架构下,调用 i2c 的接口会导致待机后无法唤醒,
-	// 所以不在 arm 和 mips 架构下使用 DDC/CI 功能。
-	if !strings.HasPrefix(runtime.GOARCH, "arm") && !strings.HasPrefix(runtime.GOARCH, "mips") {
-		var err error
-		m.ddcci, err = newDDCCI()
-		if err != nil {
-			return nil, fmt.Errorf("failed to init ddc/ci: %s", err)
-		}
-	}
-
-	return m, nil
-}
-
-func (*Manager) GetInterfaceName() string {
-	return dbusInterface
-}
-
-func (m *Manager) CheckSupport(edidChecksum string) (bool, *dbus.Error) {
-	if m.ddcci == nil {
-		return false, nil
-	}
-
-	return m.ddcci.SupportBrightness(edidChecksum), nil
-}
-
-func (m *Manager) GetBrightness(edidChecksum string) (int32, *dbus.Error) {
-	if m.ddcci == nil {
-		return 0, nil
-	}
-
-	if !m.ddcci.SupportBrightness(edidChecksum) {
-		err := fmt.Errorf("not support ddc/ci: %s", edidChecksum)
-		return 0, dbusutil.ToError(err)
-	}
-
-	brightness, err := m.ddcci.GetBrightness(edidChecksum)
-	return int32(brightness), dbusutil.ToError(err)
-}
-
-func (m *Manager) SetBrightness(edidChecksum string, value int32) *dbus.Error {
-	if m.ddcci == nil {
-		return nil
-	}
-
-	if !m.ddcci.SupportBrightness(edidChecksum) {
-		err := fmt.Errorf("not support ddc/ci: %s", edidChecksum)
-		return dbusutil.ToError(err)
-	}
-
-	err := m.ddcci.SetBrightness(edidChecksum, int(value))
-	return dbusutil.ToError(err)
-}
-
-func (m *Manager) RefreshDisplays() *dbus.Error {
-	if m.ddcci == nil {
-		return nil
-	}
-
-	err := m.ddcci.RefreshDisplays()
-	return dbusutil.ToError(err)
-}
diff --git a/bin/backlight_helper/main.go b/bin/backlight_helper/main.go
index c8e6e7e1..84f13805 100644
--- a/bin/backlight_helper/main.go
+++ b/bin/backlight_helper/main.go
@@ -27,7 +27,6 @@ import (
 	"strings"
 
 	dbus "github.com/godbus/dbus"
-	"pkg.deepin.io/dde/daemon/bin/backlight_helper/ddcci"
 	"pkg.deepin.io/lib/dbusutil"
 	"pkg.deepin.io/lib/log"
 )
@@ -45,7 +44,7 @@ const (
 
 type Manager struct {
 	service *dbusutil.Service
-	methods *struct {		//nolint
+	methods *struct { //nolint
 		SetBrightness func() `in:"type,name,value"`
 	}
 }
@@ -111,16 +110,6 @@ func main() {
 		logger.Fatal("failed to export:", err)
 	}
 
-	ddcciManager, err := ddcci.NewManager()
-	if err != nil {
-		logger.Warning(err)
-	} else {
-		err = service.Export(ddcci.DbusPath, ddcciManager)
-		if err != nil {
-			logger.Warning("failed to export:", err)
-		}
-	}
-
 	err = service.RequestName(dbusServiceName)
 	if err != nil {
 		logger.Fatal("failed to request name:", err)
-- 
2.26.2