// Code generated by "./generator ./system/org.lingmo.keyevent1"; DO NOT EDIT.

// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package keyevent1

import (
	"errors"
	"fmt"
	"unsafe"

	"github.com/godbus/dbus/v5"
	"github.com/LingmoOS/golang-github-lingmo-go-lib/dbusutil"
	"github.com/LingmoOS/golang-github-lingmo-go-lib/dbusutil/proxy"
)

type KeyEvent interface {
	keyEvent // interface org.lingmo.KeyEvent1
	proxy.Object
}

type objectKeyEvent struct {
	interfaceKeyEvent // interface org.lingmo.KeyEvent1
	proxy.ImplObject
}

func NewKeyEvent(conn *dbus.Conn) KeyEvent {
	obj := new(objectKeyEvent)
	obj.ImplObject.Init_(conn, "org.lingmo.KeyEvent1", "/org/lingmo/KeyEvent1")
	return obj
}

type keyEvent interface {
	ConnectKeyEvent(cb func(keycode uint32, pressed bool, ctrlPressed bool, shiftPressed bool, altPressed bool, superPressed bool)) (dbusutil.SignalHandlerId, error)
}

type interfaceKeyEvent struct{}

func (v *interfaceKeyEvent) GetObject_() *proxy.ImplObject {
	return (*proxy.ImplObject)(unsafe.Pointer(v))
}

func (*interfaceKeyEvent) GetInterfaceName_() string {
	return "org.lingmo.KeyEvent1"
}

// signal KeyEvent

func (v *interfaceKeyEvent) ConnectKeyEvent(cb func(keycode uint32, pressed bool, ctrlPressed bool, shiftPressed bool, altPressed bool, superPressed bool)) (dbusutil.SignalHandlerId, error) {
	if cb == nil {
		return 0, errors.New("nil callback")
	}
	obj := v.GetObject_()
	rule := fmt.Sprintf(
		"type='signal',interface='%s',member='%s',path='%s',sender='%s'",
		v.GetInterfaceName_(), "KeyEvent", obj.Path_(), obj.ServiceName_())

	sigRule := &dbusutil.SignalRule{
		Path: obj.Path_(),
		Name: v.GetInterfaceName_() + ".KeyEvent",
	}
	handlerFunc := func(sig *dbus.Signal) {
		var keycode uint32
		var pressed bool
		var ctrlPressed bool
		var shiftPressed bool
		var altPressed bool
		var superPressed bool
		err := dbus.Store(sig.Body, &keycode, &pressed, &ctrlPressed, &shiftPressed, &altPressed, &superPressed)
		if err == nil {
			cb(keycode, pressed, ctrlPressed, shiftPressed, altPressed, superPressed)
		}
	}

	return obj.ConnectSignal_(rule, sigRule, handlerFunc)
}
