From 211156f4d295c6084bcd02ad69a9343ad6c0b954 Mon Sep 17 00:00:00 2001 From: jishengjie <348240162@qq.com> Date: Mon, 16 May 2022 14:18:37 +0800 Subject: [PATCH] =?UTF-8?q?feat():=20=E6=B7=BB=E5=8A=A0=E7=94=B5=E6=BA=90?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=A6=81=E6=AD=A2=E9=94=81=E5=B1=8F,?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E9=94=81=E5=B1=8F=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian/control | 19 ++- debian/libkysdk-powermanagement-dev.install | 2 + debian/libkysdk-powermanagement.install | 1 + development-files/kysdk-powermanagement.pc | 6 + src/CMakeLists.txt | 1 + src/powermanagement/CMakeLists.txt | 25 +++ src/powermanagement/libkylockscreen.cpp | 20 +++ src/powermanagement/libkylockscreen.h | 35 ++++ src/powermanagement/lockscreen.cpp | 159 ++++++++++++++++++ src/powermanagement/lockscreen.h | 33 ++++ .../test/testlockscreen/CMakeLists.txt | 0 .../test/testlockscreen/main.c | 20 +++ 12 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 debian/libkysdk-powermanagement-dev.install create mode 100644 debian/libkysdk-powermanagement.install create mode 100644 development-files/kysdk-powermanagement.pc create mode 100644 src/powermanagement/CMakeLists.txt create mode 100644 src/powermanagement/libkylockscreen.cpp create mode 100644 src/powermanagement/libkylockscreen.h create mode 100644 src/powermanagement/lockscreen.cpp create mode 100644 src/powermanagement/lockscreen.h create mode 100644 src/powermanagement/test/testlockscreen/CMakeLists.txt create mode 100644 src/powermanagement/test/testlockscreen/main.c diff --git a/debian/control b/debian/control index 50089f1..3a53f1e 100644 --- a/debian/control +++ b/debian/control @@ -28,6 +28,7 @@ Depends: ${shlibs:Depends}, libkysdk-disk, libkysdk-sysinfo, libkysdk-systime, + libkysdk-powermanagement, systemd, libkylin-activation, libglib2.0-0 @@ -44,7 +45,8 @@ Depends: ${shlibs:Depends}, libkysdk-sysinfo-dev, libkysdk-systime-dev, libkysdk-sysinfo-dev, - libkysdk-filesystem-dev + libkysdk-filesystem-dev, + libkysdk-powermanagement-dev Multi-Arch: same Description: 麒麟开发者套件 - 系统层套件 - 开发库,提供系统信息、磁盘信息、系统时间等API与服务 @@ -205,4 +207,19 @@ Depends: ${shlibs:Depends}, Multi-Arch: same Description: 运行时信息获取库 - 开发库 +Package: libkysdk-powermanagement +Architecture: any +Section: utils +Depends: ${shlibs:Depends}, + ${misc:Depends} +Multi-Arch: same +Description: 电源管理库 +Package: libkysdk-powermanagement-dev +Architecture: any +Section: utils +Depends: ${shlibs:Depends}, + ${misc:Depends}, + libkysdk-powermanagement +Multi-Arch: same +Description: 电源管理 - 开发库 \ No newline at end of file diff --git a/debian/libkysdk-powermanagement-dev.install b/debian/libkysdk-powermanagement-dev.install new file mode 100644 index 0000000..7c53a5d --- /dev/null +++ b/debian/libkysdk-powermanagement-dev.install @@ -0,0 +1,2 @@ +src/powermanagement/libkylockscreen.h /usr/include/kysdk/kysdk-system/ +development-files/kysdk-powermanagement.pc /usr/share/pkgconfig/ \ No newline at end of file diff --git a/debian/libkysdk-powermanagement.install b/debian/libkysdk-powermanagement.install new file mode 100644 index 0000000..93a56e2 --- /dev/null +++ b/debian/libkysdk-powermanagement.install @@ -0,0 +1 @@ +usr/lib/kysdk/kysdk-system/libkypowermanagement.so \ No newline at end of file diff --git a/development-files/kysdk-powermanagement.pc b/development-files/kysdk-powermanagement.pc new file mode 100644 index 0000000..085bb26 --- /dev/null +++ b/development-files/kysdk-powermanagement.pc @@ -0,0 +1,6 @@ +Name: libkysdk-powermanagement +Description: kysdk system layer powermanagement component +Requires: kysdk-log kysdk-utils libudev blkid +Version: 1.0.0 +Libs: -L/usr/lib/kysdk/kysdk-system/ -libkypowermanagement -Wl,-rpath=/usr/lib/kysdk/kysdk-system/ +Cflags: -I/usr/include/kysdk/kysdk-system/ \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2492062..05a2e50 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory(hardware) add_subdirectory(proc) add_subdirectory(packages) add_subdirectory(filesystem) +add_subdirectory(powermanagement) diff --git a/src/powermanagement/CMakeLists.txt b/src/powermanagement/CMakeLists.txt new file mode 100644 index 0000000..27c9911 --- /dev/null +++ b/src/powermanagement/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.5) + +project(kypowermanagement LANGUAGES CXX) + +set(POWERMANAGEMENT_TOP_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_library(${PROJECT_NAME} SHARED) + +target_compile_options(${PROJECT_NAME} PRIVATE -Wall -g) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(DBUS-1 dbus-1) +target_include_directories(${PROJECT_NAME} PRIVATE ${DBUS-1_INCLUDE_DIRS}) +target_link_directories(${PROJECT_NAME} PRIVATE ${DBUS-1_LIBRARY_DIRS}) +target_link_libraries(${PROJECT_NAME} PRIVATE ${DBUS-1_LIBRARIES}) + +set(SRCS + "${POWERMANAGEMENT_TOP_DIR}/lockscreen.cpp" + "${POWERMANAGEMENT_TOP_DIR}/libkylockscreen.cpp") + +target_include_directories(${PROJECT_NAME} PRIVATE ${POWERMANAGEMENT_TOP_DIR}) +target_sources(${PROJECT_NAME} PRIVATE ${SRCS}) + +install(TARGETS ${PROJECT_NAME} DESTINATION lib/kysdk/kysdk-system) \ No newline at end of file diff --git a/src/powermanagement/libkylockscreen.cpp b/src/powermanagement/libkylockscreen.cpp new file mode 100644 index 0000000..ae29246 --- /dev/null +++ b/src/powermanagement/libkylockscreen.cpp @@ -0,0 +1,20 @@ +#include "lockscreen.h" +#include "libkylockscreen.h" + +uint32_t kdk_set_inhibit_lockscreen(const char *appName , const char *reason) +{ + kdk::LockScreen lockScreen; + return lockScreen.setInhibitLockScreen(appName , reason); +} + +int kdk_un_inhibit_lockscreen(uint32_t flag) +{ + kdk::LockScreen lockScreen; + if (lockScreen.unInhibitLockScreen(flag)) { + return 0; + } else { + return -1; + } + + return -1; +} \ No newline at end of file diff --git a/src/powermanagement/libkylockscreen.h b/src/powermanagement/libkylockscreen.h new file mode 100644 index 0000000..77632d7 --- /dev/null +++ b/src/powermanagement/libkylockscreen.h @@ -0,0 +1,35 @@ +#ifndef POWERMANAGEMENT_LIBKYLOCKSCREEN_H_ +#define POWERMANAGEMENT_LIBKYLOCKSCREEN_H_ + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * @brief 设置禁止锁屏 + * + * @param appName : 应用名 + * @param reason : 禁止锁屏原因 + * + * @return 0 : 失败 , >0 : 成功 + */ +extern uint32_t kdk_set_inhibit_lockscreen(const char *appName , const char *reason); + +/** + * @brief 取消禁止锁屏 + * + * @param flag : 禁止锁屏接口的返回值 + * + * @retval 0 : 成功 + * @retval -1 : 失败 + */ +extern int kdk_un_inhibit_lockscreen(uint32_t flag); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/powermanagement/lockscreen.cpp b/src/powermanagement/lockscreen.cpp new file mode 100644 index 0000000..cf53048 --- /dev/null +++ b/src/powermanagement/lockscreen.cpp @@ -0,0 +1,159 @@ +#include + +#include + +#include "lockscreen.h" + +namespace kdk +{ +namespace +{ +constexpr char dbusServiceName[] = "org.gnome.SessionManager"; +constexpr char dbusObjectPath[] = "/org/gnome/SessionManager"; +constexpr char dbusInterfaceName[] = "org.gnome.SessionManager"; +constexpr char dbusInhibitLockScreenMethod[] = "Inhibit"; +constexpr char dbusUnInhibitLockScreenMethod[] = "Uninhibit"; +} + +LockScreen::LockScreen() = default; + +LockScreen::~LockScreen() = default; + +uint32_t LockScreen::setInhibitLockScreen(std::string appName , std::string reason) +{ + if (!appName.size() || !reason.size()) { + return 0; + } + + DBusConnection *conn; + DBusError error; + + dbus_error_init(&error); + + conn = dbus_bus_get(DBUS_BUS_SESSION , &error); + if (dbus_error_is_set(&error)) { + std::cout << "kdk : d-bus connect fail !"; + return 0; + } + + if (conn == NULL) { + return 0; + } + + DBusMessage *sendMsg = NULL; + DBusPendingCall *sendMsgPending = NULL; + DBusMessage *replyMsg = NULL; + + sendMsg = dbus_message_new_method_call(dbusServiceName , dbusObjectPath , dbusInterfaceName , dbusInhibitLockScreenMethod); + const char *tmpAppName = appName.c_str(); + dbus_int32_t topLevel = 0; + const char *tmpReason = reason.c_str(); + dbus_int32_t flags = 8; + if (!dbus_message_append_args(sendMsg , DBUS_TYPE_STRING , &tmpAppName , DBUS_TYPE_UINT32 , &topLevel , DBUS_TYPE_STRING , &tmpReason , DBUS_TYPE_UINT32 , &flags , DBUS_TYPE_INVALID)) { + std::cout << "kdk : d-bus append args fail " << std::endl; + return 0; + } + + if (!dbus_connection_send_with_reply(conn , sendMsg , &sendMsgPending , -1)) { + std::cout << "kdk : d-bus send message fail" << std::endl; + return 0; + } + + if (sendMsgPending == NULL) { + std::cout << "kdk : d-bus pending message is NULL" << std::endl; + return 0; + } + + dbus_connection_flush(conn); + + if (sendMsg) { + dbus_message_unref(sendMsg); + } + + dbus_pending_call_block(sendMsgPending); + replyMsg = dbus_pending_call_steal_reply(sendMsgPending); + if (replyMsg == NULL) { + std::cout << "kdk : get reply message fail" << std::endl; + return 0; + } + + if (sendMsgPending) { + dbus_pending_call_unref(sendMsgPending); + } + + DBusMessageIter args; + uint32_t ret = 0; + + if (!dbus_message_iter_init(replyMsg , &args)) { + dbus_message_unref(replyMsg); + std::cout << "kdk : d-bus reply message fail!" << std::endl; + return 0; + } else { + dbus_message_iter_get_basic(&args , &ret); + } + + if (replyMsg) { + dbus_message_unref(replyMsg); + } + + if (ret) { + return ret; + } + + return 0; +} + +bool LockScreen::unInhibitLockScreen(uint32_t flag) +{ + if (!flag) { + return false; + } + + DBusConnection *conn; + DBusError error; + + dbus_error_init(&error); + + conn = dbus_bus_get(DBUS_BUS_SESSION , &error); + if (dbus_error_is_set(&error)) { + std::cout << "kdk : d-bus connect fail !"; + return false; + } + + if (conn == NULL) { + return false; + } + + DBusMessage *sendMsg = NULL; + DBusPendingCall *sendMsgPending = NULL; + + sendMsg = dbus_message_new_method_call(dbusServiceName , dbusObjectPath , dbusInterfaceName , dbusUnInhibitLockScreenMethod); + if (!dbus_message_append_args(sendMsg , DBUS_TYPE_UINT32 , &flag)) { + std::cout << "kdk : d-bus append args fail " << std::endl; + return false; + } + + if (!dbus_connection_send_with_reply(conn , sendMsg , &sendMsgPending , -1)) { + std::cout << "kdk : d-bus send message fail" << std::endl; + return false; + } + + if (sendMsgPending == NULL) { + std::cout << "kdk : d-bus pending message is NULL" << std::endl; + return false; + } + + dbus_connection_flush(conn); + + if (sendMsg) { + dbus_message_unref(sendMsg); + } + + if (sendMsgPending) { + dbus_pending_call_unref(sendMsgPending); + } + + return true; +} + +} \ No newline at end of file diff --git a/src/powermanagement/lockscreen.h b/src/powermanagement/lockscreen.h new file mode 100644 index 0000000..7d64418 --- /dev/null +++ b/src/powermanagement/lockscreen.h @@ -0,0 +1,33 @@ +#include +#include + +namespace kdk +{ + +class LockScreen +{ +public: + LockScreen(); + ~LockScreen(); + + /** + * @brief 设置禁止锁屏 + * + * @param appName : 应用名 + * @param reason : 原因描述 + * + * @return 标记 + */ + uint32_t setInhibitLockScreen(std::string appName , std::string reason); + + /** + * @brief 取消禁止锁屏 + * + * @param flag : 禁止锁屏接口返回来的标记 + * + * @return true : 成功 , false : 失败 + */ + bool unInhibitLockScreen(uint32_t flag); +}; + +} \ No newline at end of file diff --git a/src/powermanagement/test/testlockscreen/CMakeLists.txt b/src/powermanagement/test/testlockscreen/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/powermanagement/test/testlockscreen/main.c b/src/powermanagement/test/testlockscreen/main.c new file mode 100644 index 0000000..63e8a93 --- /dev/null +++ b/src/powermanagement/test/testlockscreen/main.c @@ -0,0 +1,20 @@ +#include + +int main(void) +{ + char appName[] = "test-package"; + char reason[] = "test-reason"; + + uint32_t flag; + if ((flag = kdk_set_inhibit_lockscreen(appName , reason)) == 0) { + printf("set inhibit lock screen fail !\n"); + return -1; + } + + if (kdk_un_inhibit_lockscreen(flag)) { + printf("un inhibit lock screen fail !\n"); + return -1; + } + + return 0; +} \ No newline at end of file