Blame 0003-fix-backlight-remove-DDCCI.patch

79f4da1
From da3febed1796f3ee81a8a623f535c2496539094e Mon Sep 17 00:00:00 2001
79f4da1
From: hubenchang0515 <hubenchang@uniontech.com>
79f4da1
Date: Mon, 21 Sep 2020 15:32:21 +0800
75c19b7
Subject: [PATCH 3/8] fix(backlight): remove DDCCI
79f4da1
MIME-Version: 1.0
79f4da1
Content-Type: text/plain; charset=UTF-8
79f4da1
Content-Transfer-Encoding: 8bit
79f4da1
79f4da1
DDCCI设置亮度太慢,导致了很多问题,现在删除
79f4da1
79f4da1
Log: 删除DDCCI
79f4da1
Bug: https://pms.uniontech.com/zentao/bug-view-48540.html
79f4da1
Change-Id: I87474b13743f68c3a39ecf33e1f3a97ab934a78c
79f4da1
Reviewed-on: http://gerrit.uniontech.com/c/dde-daemon/+/5696
79f4da1
Reviewed-by: <mailman@uniontech.com>
79f4da1
Reviewed-by: quezhiyong <quezhiyong@uniontech.com>
79f4da1
Tested-by: <mailman@uniontech.com>
79f4da1
---
79f4da1
 bin/backlight_helper/ddcci/ddcci.go   | 180 --------------------------
79f4da1
 bin/backlight_helper/ddcci/manager.go |  96 --------------
79f4da1
 bin/backlight_helper/main.go          |  13 +-
79f4da1
 3 files changed, 1 insertion(+), 288 deletions(-)
79f4da1
 delete mode 100644 bin/backlight_helper/ddcci/ddcci.go
79f4da1
 delete mode 100644 bin/backlight_helper/ddcci/manager.go
79f4da1
79f4da1
diff --git a/bin/backlight_helper/ddcci/ddcci.go b/bin/backlight_helper/ddcci/ddcci.go
79f4da1
deleted file mode 100644
79f4da1
index cd4a66da..00000000
79f4da1
--- a/bin/backlight_helper/ddcci/ddcci.go
79f4da1
+++ /dev/null
79f4da1
@@ -1,180 +0,0 @@
79f4da1
-package ddcci
79f4da1
-
79f4da1
-// #cgo pkg-config: ddcutil
79f4da1
-// #include <ddcutil_c_api.h>
79f4da1
-import "C"
79f4da1
-import (
79f4da1
-	"fmt"
79f4da1
-	"sync"
79f4da1
-	"unsafe"
79f4da1
-
79f4da1
-	"pkg.deepin.io/lib/utils"
79f4da1
-)
79f4da1
-
79f4da1
-type ddcci struct {
79f4da1
-	listPointer *C.DDCA_Display_Info_List
79f4da1
-	listMu      sync.Mutex
79f4da1
-
79f4da1
-	displayMap map[string]int
79f4da1
-}
79f4da1
-
79f4da1
-const (
79f4da1
-	brightnessVCP = 0x10
79f4da1
-)
79f4da1
-
79f4da1
-func newDDCCI() (*ddcci, error) {
79f4da1
-	ddc := &ddcci{
79f4da1
-		displayMap: make(map[string]int),
79f4da1
-	}
79f4da1
-
79f4da1
-	status := C.ddca_set_max_tries(C.DDCA_MULTI_PART_TRIES, 5)
79f4da1
-	if status < C.int(0) {
79f4da1
-		return nil, fmt.Errorf("Error setting retries: %d", status)
79f4da1
-	}
79f4da1
-
79f4da1
-	err := ddc.RefreshDisplays()
79f4da1
-	if err != nil {
79f4da1
-		return nil, err
79f4da1
-	}
79f4da1
-
79f4da1
-	return ddc, nil
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) freeList() {
79f4da1
-	if d.listPointer != nil {
79f4da1
-		C.ddca_free_display_info_list(d.listPointer)
79f4da1
-		d.listPointer = nil
79f4da1
-	}
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) RefreshDisplays() error {
79f4da1
-	d.listMu.Lock()
79f4da1
-	defer d.listMu.Unlock()
79f4da1
-
79f4da1
-	d.freeList()
79f4da1
-
79f4da1
-	status := C.ddca_get_display_info_list2(C.bool(true), &d.listPointer)
79f4da1
-	if status != C.int(0) {
79f4da1
-		return fmt.Errorf("failed to get display info list: %d", status)
79f4da1
-	}
79f4da1
-
79f4da1
-	for i := 0; i < int(d.listPointer.ct); i++ {
79f4da1
-		err := d.initDisplay(i)
79f4da1
-		if err != nil {
79f4da1
-			logger.Warning(err)
79f4da1
-		}
79f4da1
-	}
79f4da1
-
79f4da1
-	return nil
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) initDisplay(idx int) error {
79f4da1
-	item := d.getDisplayInfoByIdx(idx)
79f4da1
-
79f4da1
-	var handle C.DDCA_Display_Handle
79f4da1
-	status := C.ddca_open_display2(item.dref, C.bool(true), &handle)
79f4da1
-	if status != C.int(0) {
79f4da1
-		return fmt.Errorf("failed to open monitor: %d", status)
79f4da1
-	}
79f4da1
-
79f4da1
-	defer C.ddca_close_display(handle)
79f4da1
-
79f4da1
-	var val C.DDCA_Non_Table_Vcp_Value
79f4da1
-	status = C.ddca_get_non_table_vcp_value(handle, brightnessVCP, &val)
79f4da1
-	if status != C.int(0) {
79f4da1
-		return fmt.Errorf("failed to check DDC/CI support: %d", status)
79f4da1
-	}
79f4da1
-
79f4da1
-	edid := C.GoBytes(unsafe.Pointer(&item.edid_bytes), 128)
79f4da1
-	edidChecksum := getEDIDChecksum(edid)
79f4da1
-
79f4da1
-	d.displayMap[string(edidChecksum)] = idx
79f4da1
-	return nil
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) SupportBrightness(edidChecksum string) bool {
79f4da1
-	d.listMu.Lock()
79f4da1
-	_, ok := d.displayMap[edidChecksum]
79f4da1
-	d.listMu.Unlock()
79f4da1
-
79f4da1
-	return ok
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) GetBrightness(edidChecksum string) (brightness int, err error) {
79f4da1
-	d.listMu.Lock()
79f4da1
-	defer d.listMu.Unlock()
79f4da1
-
79f4da1
-	idx, ok := d.displayMap[edidChecksum]
79f4da1
-	if !ok {
79f4da1
-		err = fmt.Errorf("monitor not support DDC/CI")
79f4da1
-		return
79f4da1
-	}
79f4da1
-
79f4da1
-	item := d.getDisplayInfoByIdx(idx)
79f4da1
-
79f4da1
-	var handle C.DDCA_Display_Handle
79f4da1
-	status := C.ddca_open_display2(item.dref, C.bool(true), &handle)
79f4da1
-	if status != C.int(0) {
79f4da1
-		err = fmt.Errorf("failed to open monitor: %d", status)
79f4da1
-		return
79f4da1
-	}
79f4da1
-
79f4da1
-	defer C.ddca_close_display(handle)
79f4da1
-
79f4da1
-	var val C.DDCA_Non_Table_Vcp_Value
79f4da1
-	status = C.ddca_get_non_table_vcp_value(handle, brightnessVCP, &val)
79f4da1
-	if status != C.int(0) {
79f4da1
-		err = fmt.Errorf("failed to get brightness: %d", status)
79f4da1
-		return
79f4da1
-	}
79f4da1
-
79f4da1
-	brightness = int(val.sl)
79f4da1
-	return
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) SetBrightness(edidChecksum string, percent int) error {
79f4da1
-	d.listMu.Lock()
79f4da1
-	defer d.listMu.Unlock()
79f4da1
-
79f4da1
-	idx, ok := d.displayMap[edidChecksum]
79f4da1
-	if !ok {
79f4da1
-		return fmt.Errorf("monitor not support DDC/CI")
79f4da1
-	}
79f4da1
-
79f4da1
-	item := d.getDisplayInfoByIdx(idx)
79f4da1
-
79f4da1
-	var handle C.DDCA_Display_Handle
79f4da1
-	status := C.ddca_open_display2(item.dref, C.bool(true), &handle)
79f4da1
-	if status != C.int(0) {
79f4da1
-		return fmt.Errorf("failed to open monitor: %d", status)
79f4da1
-	}
79f4da1
-
79f4da1
-	defer C.ddca_close_display(handle)
79f4da1
-
79f4da1
-	// 开启结果验证,防止返回设置成功,但实际上没有生效的情况
79f4da1
-	// 此方法仅对当前线程生效
79f4da1
-	C.ddca_enable_verify(true)
79f4da1
-
79f4da1
-	status = C.ddca_set_non_table_vcp_value(handle, brightnessVCP, 0, C.uchar(percent))
79f4da1
-	if status != C.int(0) {
79f4da1
-		return fmt.Errorf("failed to set brightness via DDC/CI: %d", status)
79f4da1
-	}
79f4da1
-
79f4da1
-	return nil
79f4da1
-}
79f4da1
-
79f4da1
-func (d *ddcci) getDisplayInfoByIdx(idx int) *C.DDCA_Display_Info {
79f4da1
-	start := unsafe.Pointer(uintptr(unsafe.Pointer(d.listPointer)) + uintptr(C.sizeof_DDCA_Display_Info_List))
79f4da1
-	size := uintptr(C.sizeof_DDCA_Display_Info)
79f4da1
-
79f4da1
-	return (*C.DDCA_Display_Info)(unsafe.Pointer(uintptr(start) + size*uintptr(idx)))
79f4da1
-}
79f4da1
-
79f4da1
-func getEDIDChecksum(edid []byte) string {
79f4da1
-	if len(edid) < 128 {
79f4da1
-		return ""
79f4da1
-	}
79f4da1
-
79f4da1
-	id, _ := utils.SumStrMd5(string(edid[:128]))
79f4da1
-	return id
79f4da1
-}
79f4da1
diff --git a/bin/backlight_helper/ddcci/manager.go b/bin/backlight_helper/ddcci/manager.go
79f4da1
deleted file mode 100644
79f4da1
index e87083d2..00000000
79f4da1
--- a/bin/backlight_helper/ddcci/manager.go
79f4da1
+++ /dev/null
79f4da1
@@ -1,96 +0,0 @@
79f4da1
-package ddcci
79f4da1
-
79f4da1
-import (
79f4da1
-	"fmt"
79f4da1
-	"runtime"
79f4da1
-	"strings"
79f4da1
-	"sync"
79f4da1
-
79f4da1
-	dbus "github.com/godbus/dbus"
79f4da1
-	"pkg.deepin.io/lib/dbusutil"
79f4da1
-	"pkg.deepin.io/lib/log"
79f4da1
-)
79f4da1
-
79f4da1
-const (
79f4da1
-	DbusPath      = "/com/deepin/daemon/helper/Backlight/DDCCI"
79f4da1
-	dbusInterface = "com.deepin.daemon.helper.Backlight.DDCCI"
79f4da1
-)
79f4da1
-
79f4da1
-var logger = log.NewLogger("backlight_helper/ddcci")
79f4da1
-
79f4da1
-type Manager struct {
79f4da1
-	ddcci   *ddcci
79f4da1
-	PropsMu sync.RWMutex
79f4da1
-
79f4da1
-	methods *struct { //nolint
79f4da1
-		CheckSupport    func() `in:"edidChecksum" out:"support"`
79f4da1
-		GetBrightness   func() `in:"edidChecksum" out:"value"`
79f4da1
-		SetBrightness   func() `in:"edidChecksum,value"`
79f4da1
-		RefreshDisplays func()
79f4da1
-	}
79f4da1
-}
79f4da1
-
79f4da1
-func NewManager() (*Manager, error) {
79f4da1
-	m := &Manager{}
79f4da1
-
79f4da1
-	// 在 arm 和 mips 架构下,调用 i2c 的接口会导致待机后无法唤醒,
79f4da1
-	// 所以不在 arm 和 mips 架构下使用 DDC/CI 功能。
79f4da1
-	if !strings.HasPrefix(runtime.GOARCH, "arm") && !strings.HasPrefix(runtime.GOARCH, "mips") {
79f4da1
-		var err error
79f4da1
-		m.ddcci, err = newDDCCI()
79f4da1
-		if err != nil {
79f4da1
-			return nil, fmt.Errorf("failed to init ddc/ci: %s", err)
79f4da1
-		}
79f4da1
-	}
79f4da1
-
79f4da1
-	return m, nil
79f4da1
-}
79f4da1
-
79f4da1
-func (*Manager) GetInterfaceName() string {
79f4da1
-	return dbusInterface
79f4da1
-}
79f4da1
-
79f4da1
-func (m *Manager) CheckSupport(edidChecksum string) (bool, *dbus.Error) {
79f4da1
-	if m.ddcci == nil {
79f4da1
-		return false, nil
79f4da1
-	}
79f4da1
-
79f4da1
-	return m.ddcci.SupportBrightness(edidChecksum), nil
79f4da1
-}
79f4da1
-
79f4da1
-func (m *Manager) GetBrightness(edidChecksum string) (int32, *dbus.Error) {
79f4da1
-	if m.ddcci == nil {
79f4da1
-		return 0, nil
79f4da1
-	}
79f4da1
-
79f4da1
-	if !m.ddcci.SupportBrightness(edidChecksum) {
79f4da1
-		err := fmt.Errorf("not support ddc/ci: %s", edidChecksum)
79f4da1
-		return 0, dbusutil.ToError(err)
79f4da1
-	}
79f4da1
-
79f4da1
-	brightness, err := m.ddcci.GetBrightness(edidChecksum)
79f4da1
-	return int32(brightness), dbusutil.ToError(err)
79f4da1
-}
79f4da1
-
79f4da1
-func (m *Manager) SetBrightness(edidChecksum string, value int32) *dbus.Error {
79f4da1
-	if m.ddcci == nil {
79f4da1
-		return nil
79f4da1
-	}
79f4da1
-
79f4da1
-	if !m.ddcci.SupportBrightness(edidChecksum) {
79f4da1
-		err := fmt.Errorf("not support ddc/ci: %s", edidChecksum)
79f4da1
-		return dbusutil.ToError(err)
79f4da1
-	}
79f4da1
-
79f4da1
-	err := m.ddcci.SetBrightness(edidChecksum, int(value))
79f4da1
-	return dbusutil.ToError(err)
79f4da1
-}
79f4da1
-
79f4da1
-func (m *Manager) RefreshDisplays() *dbus.Error {
79f4da1
-	if m.ddcci == nil {
79f4da1
-		return nil
79f4da1
-	}
79f4da1
-
79f4da1
-	err := m.ddcci.RefreshDisplays()
79f4da1
-	return dbusutil.ToError(err)
79f4da1
-}
79f4da1
diff --git a/bin/backlight_helper/main.go b/bin/backlight_helper/main.go
79f4da1
index c8e6e7e1..84f13805 100644
79f4da1
--- a/bin/backlight_helper/main.go
79f4da1
+++ b/bin/backlight_helper/main.go
79f4da1
@@ -27,7 +27,6 @@ import (
79f4da1
 	"strings"
79f4da1
 
79f4da1
 	dbus "github.com/godbus/dbus"
79f4da1
-	"pkg.deepin.io/dde/daemon/bin/backlight_helper/ddcci"
79f4da1
 	"pkg.deepin.io/lib/dbusutil"
79f4da1
 	"pkg.deepin.io/lib/log"
79f4da1
 )
79f4da1
@@ -45,7 +44,7 @@ const (
79f4da1
 
79f4da1
 type Manager struct {
79f4da1
 	service *dbusutil.Service
79f4da1
-	methods *struct {		//nolint
79f4da1
+	methods *struct { //nolint
79f4da1
 		SetBrightness func() `in:"type,name,value"`
79f4da1
 	}
79f4da1
 }
79f4da1
@@ -111,16 +110,6 @@ func main() {
79f4da1
 		logger.Fatal("failed to export:", err)
79f4da1
 	}
79f4da1
 
79f4da1
-	ddcciManager, err := ddcci.NewManager()
79f4da1
-	if err != nil {
79f4da1
-		logger.Warning(err)
79f4da1
-	} else {
79f4da1
-		err = service.Export(ddcci.DbusPath, ddcciManager)
79f4da1
-		if err != nil {
79f4da1
-			logger.Warning("failed to export:", err)
79f4da1
-		}
79f4da1
-	}
79f4da1
-
79f4da1
 	err = service.RequestName(dbusServiceName)
79f4da1
 	if err != nil {
79f4da1
 		logger.Fatal("failed to request name:", err)
79f4da1
-- 
79f4da1
2.26.2
79f4da1