Merge branch 'dev' into 'dev'

添加电源管理模块禁止锁屏,取消锁屏接口

See merge request kysdk/kysdk-system!4
This commit is contained in:
liu yunhe 2022-05-17 10:04:17 +00:00
commit ef73b21f5b
12 changed files with 348 additions and 1 deletions

19
debian/control vendored
View File

@ -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: 电源管理 - 开发库

View File

@ -0,0 +1,2 @@
src/powermanagement/libkylockscreen.h usr/include/kysdk/kysdk-system/
development-files/kysdk-powermanagement.pc usr/share/pkgconfig/

View File

@ -0,0 +1 @@
usr/lib/kysdk/kysdk-system/libkypowermanagement.so

View File

@ -0,0 +1,6 @@
Name: libkysdk-powermanagement
Description: kysdk system layer powermanagement component
Requires: kysdk-log
Version: 1.0.0
Libs: -L/usr/lib/kysdk/kysdk-system/ -lkypowermanagement -Wl,-rpath=/usr/lib/kysdk/kysdk-system/
Cflags: -I/usr/include/kysdk/kysdk-system/

View File

@ -5,3 +5,4 @@ add_subdirectory(hardware)
add_subdirectory(proc)
add_subdirectory(packages)
add_subdirectory(filesystem)
add_subdirectory(powermanagement)

View File

@ -0,0 +1,30 @@
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})
pkg_check_modules(KYLOG kysdk-log)
target_include_directories(${PROJECT_NAME} PRIVATE ${KYLOG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYLOG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${KYLOG_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)

View File

@ -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;
}

View File

@ -0,0 +1,35 @@
#ifndef POWERMANAGEMENT_LIBKYLOCKSCREEN_H_
#define POWERMANAGEMENT_LIBKYLOCKSCREEN_H_
#include <stdint.h>
#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

View File

@ -0,0 +1,161 @@
#include <iostream>
#include <dbus-1.0/dbus/dbus.h>
#include <libkylog.h>
#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)) {
klog_err("kdk : d-bus connect fail !\n");
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)) {
klog_err("kdk : d-bus append args fail !\n");
return 0;
}
if (!dbus_connection_send_with_reply(conn , sendMsg , &sendMsgPending , -1)) {
klog_err("kdk : d-bus send message fail !\n");
return 0;
}
if (sendMsgPending == NULL) {
klog_err("kdk : d-bus pending message is NULL !\n");
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) {
klog_err("kdk : get reply message fail !\n");
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);
klog_err("kdk : d-bus reply message fail !\n");
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)) {
klog_err("kdk : d-bus connect fail !\n");
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)) {
klog_err("kdk : d-bus append args fail !\n");
return false;
}
if (!dbus_connection_send_with_reply(conn , sendMsg , &sendMsgPending , -1)) {
std::cout << "kdk : d-bus send message fail" << std::endl;
klog_err("kdk : d-bus send message fail !\n");
return false;
}
if (sendMsgPending == NULL) {
klog_err("kdk : d-bus pending message is NULL !\n");
return false;
}
dbus_connection_flush(conn);
if (sendMsg) {
dbus_message_unref(sendMsg);
}
if (sendMsgPending) {
dbus_pending_call_unref(sendMsgPending);
}
return true;
}
}

View File

@ -0,0 +1,33 @@
#include <stdint.h>
#include <string>
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);
};
}

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.5)
project(testlockscreen LANGUAGES C)
set(TESTLOCKSCREEN_TOP_DIR ${CMAKE_CURRENT_LIST_DIR})
add_executable(${PROJECT_NAME})
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -g)
find_package(PkgConfig REQUIRED)
pkg_check_modules(KYPOWERMANAGEMENT kysdk-powermanagement)
target_include_directories(${PROJECT_NAME} PRIVATE ${KYPOWERMANAGEMENT_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYPOWERMANAGEMENT_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${KYPOWERMANAGEMENT_LIBRARIES})
set(SRCS
"${TESTLOCKSCREEN_TOP_DIR}/main.c")
target_sources(${PROJECT_NAME} PRIVATE ${SRCS})

View File

@ -0,0 +1,21 @@
#include <libkylockscreen.h>
#include <stdio.h>
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;
}