feat(): 添加电源管理禁止锁屏,取消锁屏接口
This commit is contained in:
parent
db7e843cb3
commit
211156f4d2
|
@ -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: 电源管理 - 开发库
|
|
@ -0,0 +1,2 @@
|
|||
src/powermanagement/libkylockscreen.h /usr/include/kysdk/kysdk-system/
|
||||
development-files/kysdk-powermanagement.pc /usr/share/pkgconfig/
|
|
@ -0,0 +1 @@
|
|||
usr/lib/kysdk/kysdk-system/libkypowermanagement.so
|
|
@ -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/
|
|
@ -5,3 +5,4 @@ add_subdirectory(hardware)
|
|||
add_subdirectory(proc)
|
||||
add_subdirectory(packages)
|
||||
add_subdirectory(filesystem)
|
||||
add_subdirectory(powermanagement)
|
||||
|
|
|
@ -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)
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,159 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <dbus-1.0/dbus/dbus.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)) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <libkylockscreen.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;
|
||||
}
|
Loading…
Reference in New Issue