1# 使用OH_DisplayManager实现屏幕基础信息查询和状态监听 (C/C++) 2<!--Kit: ArkUI--> 3<!--Subsystem: Window--> 4<!--Owner: @oh_wangxk; @logn--> 5<!--Designer: @hejunfei1991--> 6<!--Tester: @qinliwen0417--> 7<!--Adviser: @ge-yafang--> 8 9## 场景介绍 10 11[OH_DisplayManager](../reference/apis-arkui/capi-oh-displaymanager.md)屏幕管理模块用于提供屏幕的信息查询、屏幕状态变化监听、折叠设备的折叠状态变化监听等能力,应用可根据对应的屏幕信息、屏幕状态变化、屏幕折叠状态适配不同的UI界面显示。 12 13- 支持查询的屏幕信息,包括屏幕的分辨率、物理像素密度、逻辑像素密度、刷新率、屏幕尺寸、屏幕旋转方向、屏幕旋转角度等。 14 15- 支持屏幕状态变化的监听,包括屏幕旋转变化,屏幕分辨率变化、屏幕刷新率变化等。 16 17- 支持查询当前设备是否为可折叠设备,同时支持折叠状态(展开/折叠)变化的监听。 18 19## 基本概念 20 21- 屏幕的物理像素密度(densityDPI):代表每英寸屏幕所拥有的物理像素点数。 22 23- 屏幕的逻辑像素的密度(densityPixels):代表物理像素与逻辑像素的缩放系数比,计算方法为物理像素密度除以160。 24 25## 接口说明 26 27常用接口如下表所示。更多API说明请参考[OH_DisplayManager](../reference/apis-arkui/capi-oh-displaymanager.md)。 28 29| 接口名 | 描述 | 30| -------- | -------- | 31| OH_NativeDisplayManager_GetDefaultDisplayRotation(NativeDisplayManager_Rotation *displayRotation) | 获取默认屏幕的旋转角度。 | 32| OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo **cutoutInfo) | 获取挖孔屏、刘海屏、瀑布屏等不可用屏幕区域信息。 | 33| OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo *cutoutInfo) | 销毁挖孔屏、刘海屏、瀑布屏等不可用屏幕区域信息。| 34| OH_NativeDisplayManager_IsFoldable()|查询设备是否可折叠。| 35| OH_NativeDisplayManager_RegisterDisplayChangeListener( OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback, uint32_t *listenerIndex)|注册屏幕状态变化监听(如旋转变化、刷新率、DPI、分辨率等)。| 36|OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)|取消屏幕状态变化监听。| 37|OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener( OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback, uint32_t *listenerIndex)|注册屏幕展开、折叠状态变化监听。| 38|OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)|取消屏幕展开、折叠状态变化监听。| 39 40## 在CMake脚本中链接动态库 41 42``` 43target_link_libraries(entry PUBLIC libhilog_ndk.z.so) 44target_link_libraries(entry PUBLIC libnative_display_manager.so ) 45``` 46 47## 添加头文件 48 49```c++ 50#include <window_manager/oh_display_info.h> 51#include <window_manager/oh_display_manager.h> 52#include <hilog/log.h> 53``` 54 55## 获取屏幕状态 56 571. 可以通过OH_NativeDisplayManager_GetDefaultDisplayRotation获取默认屏幕的旋转角度。 58 59 ```c++ 60 #include "napi/native_api.h" 61 #include <window_manager/oh_display_info.h> 62 #include <window_manager/oh_display_manager.h> 63 #include <hilog/log.h> 64 65 static napi_value GetDefaultDisplayRotation(napi_env env, napi_callback_info info) 66 { 67 NativeDisplayManager_Rotation displayRotation; 68 NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_GetDefaultDisplayRotation(&displayRotation); 69 if (errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK) { 70 napi_value rotation; 71 napi_create_int32(env, displayRotation, &rotation); 72 return rotation; 73 } else { 74 napi_value errorCode; 75 napi_create_int32(env, errCode, &errorCode); 76 return errorCode; 77 } 78 } 79 80 EXTERN_C_START 81 static napi_value Init(napi_env env, napi_value exports) { 82 napi_property_descriptor desc[] = { 83 {"getDisplayRotation", nullptr, GetDefaultDisplayRotation, nullptr, nullptr, nullptr, napi_default, nullptr}, 84 }; 85 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 86 return exports; 87 } 88 EXTERN_C_END 89 ``` 90 912. 可以通过OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo获取挖孔屏、刘海屏、瀑布屏等不可用屏幕区域信息。 可通过OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo销毁挖孔屏、刘海屏、瀑布屏等不可用屏幕区域信息。 92 93 ```c++ 94 #include "napi/native_api.h" 95 #include <window_manager/oh_display_info.h> 96 #include <window_manager/oh_display_manager.h> 97 #include <hilog/log.h> 98 99 static napi_value CreateDefaultDisplayCutoutInfo(napi_env env, napi_callback_info info) 100 { 101 NativeDisplayManager_CutoutInfo *cutOutInfo = NULL; 102 NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(&cutOutInfo); 103 OH_LOG_INFO(LOG_APP, "GetDefaultCutoutInfo errCode=%{public}d", errCode); 104 if (errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK) { 105 if (cutOutInfo != NULL && cutOutInfo->boundingRectsLength != 0) { 106 OH_LOG_INFO(LOG_APP, "GetDefaultCutoutInfo cutOutInfo length=%{public}d", cutOutInfo->boundingRectsLength); 107 for (int i = 0; i < cutOutInfo->boundingRectsLength; i++) { 108 OH_LOG_INFO(LOG_APP, "cutOutInfo[%{public}d]=[%{public}d %{public}d %{public}d %{public}d]", 109 i, cutOutInfo->boundingRects[i].left, cutOutInfo->boundingRects[i].top, 110 cutOutInfo->boundingRects[i].width, cutOutInfo->boundingRects[i].height); 111 } 112 OH_LOG_INFO(LOG_APP, "cutOutInfo waterfall left rect=[%{public}d %{public}d %{public}d %{public}d]", 113 cutOutInfo->waterfallDisplayAreaRects.left.left, cutOutInfo->waterfallDisplayAreaRects.left.top, 114 cutOutInfo->waterfallDisplayAreaRects.left.width, cutOutInfo->waterfallDisplayAreaRects.left.height); 115 OH_LOG_INFO(LOG_APP, "cutOutInfo waterfall top rect=[%{public}d %{public}d %{public}d %{public}d]", 116 cutOutInfo->waterfallDisplayAreaRects.top.left, cutOutInfo->waterfallDisplayAreaRects.top.top, 117 cutOutInfo->waterfallDisplayAreaRects.top.width, cutOutInfo->waterfallDisplayAreaRects.top.height); 118 OH_LOG_INFO(LOG_APP, "cutOutInfo waterfall right rect=[%{public}d %{public}d %{public}d %{public}d]", 119 cutOutInfo->waterfallDisplayAreaRects.right.left, cutOutInfo->waterfallDisplayAreaRects.right.top, 120 cutOutInfo->waterfallDisplayAreaRects.right.width, cutOutInfo->waterfallDisplayAreaRects.right.height); 121 OH_LOG_INFO(LOG_APP, "cutOutInfo waterfall bottom rect=[%{public}d %{public}d %{public}d %{public}d]", 122 cutOutInfo->waterfallDisplayAreaRects.bottom.left, cutOutInfo->waterfallDisplayAreaRects.bottom.top, 123 cutOutInfo->waterfallDisplayAreaRects.bottom.width, cutOutInfo->waterfallDisplayAreaRects.bottom.height); 124 } 125 napi_value boundingRectsLength; 126 napi_create_int32(env, cutOutInfo->boundingRectsLength, &boundingRectsLength); 127 OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(cutOutInfo); 128 return boundingRectsLength; 129 } else { 130 napi_value errorCode; 131 napi_create_int32(env, errCode, &errorCode); 132 return errorCode; 133 } 134 } 135 136 EXTERN_C_START 137 static napi_value Init(napi_env env, napi_value exports) { 138 napi_property_descriptor desc[] = { 139 {"getCutoutInfo", nullptr, CreateDefaultDisplayCutoutInfo, nullptr, nullptr, nullptr, napi_default, nullptr}, 140 }; 141 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 142 return exports; 143 } 144 EXTERN_C_END 145 ``` 146 147 148 149## 监听屏幕状态变化 150 151可以通过OH_NativeDisplayManager_RegisterDisplayChangeListener接口注册屏幕变化的监听,包括屏幕旋转、分辨率变化、刷新率变化、DPI变化等。 通过OH_NativeDisplayManager_UnregisterDisplayChangeListener接口取消屏幕状态变化的监听。 152 153```c++ 154#include "napi/native_api.h" 155#include <window_manager/oh_display_info.h> 156#include <window_manager/oh_display_manager.h> 157#include <hilog/log.h> 158 159void DisplayChangeCallback(uint64_t displayId) 160{ 161 OH_LOG_INFO(LOG_APP, "DisplayChangeCallback displayId=%{public}lu.", displayId); 162} 163 164static napi_value RegisterDisplayChangeListener(napi_env env, napi_callback_info info) 165{ 166 uint32_t listenerIndex; 167 NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_RegisterDisplayChangeListener( 168 DisplayChangeCallback, &listenerIndex); 169 OH_LOG_INFO(LOG_APP, "RegisterDisplayChangeListener listenerIndex =%{public}d errCode=%{public}d.", 170 listenerIndex, errCode); 171 if (errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK) { 172 napi_value registerIndex; 173 napi_create_int32(env, listenerIndex, ®isterIndex); 174 return registerIndex; 175 } else { 176 napi_value errorCode; 177 napi_create_int32(env, errCode, &errorCode); 178 return errorCode; 179 } 180} 181 182static napi_value UnregisterDisplayChangeListener(napi_env env, napi_callback_info info) 183{ 184 size_t argc = 1; 185 napi_value args[1] = { nullptr }; 186 187 uint32_t listenerIndex; 188 napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 189 napi_get_value_uint32(env, args[0], &listenerIndex); 190 OH_LOG_INFO(LOG_APP, "UnregisterDisplayChangeListener listenerIndex =%{public}d.", listenerIndex); 191 NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_UnregisterDisplayChangeListener(listenerIndex); 192 OH_LOG_INFO(LOG_APP, "UnregisterDisplayChangeListener errCode=%{public}d.", errCode); 193 napi_value errorCode; 194 napi_create_int32(env, errCode, &errorCode); 195 return errorCode; 196} 197 198EXTERN_C_START 199static napi_value Init(napi_env env, napi_value exports) { 200 napi_property_descriptor desc[] = { 201 {"registerDisplayChange", nullptr, RegisterDisplayChangeListener, nullptr, nullptr, nullptr, napi_default, nullptr}, 202 {"unregisterDisplayChange", nullptr, UnregisterDisplayChangeListener, nullptr, nullptr, nullptr, 203 napi_default, nullptr}, 204 }; 205 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 206 return exports; 207} 208EXTERN_C_END 209 210``` 211 212## 监听折叠设备状态变化 213 2141. 可以通过OH_NativeDisplayManager_IsFoldable接口查询设备是不是折叠设备。 215 216 ```c++ 217 #include "napi/native_api.h" 218 #include <window_manager/oh_display_manager.h> 219 #include <hilog/log.h> 220 221 static napi_value IsFoldable(napi_env env, napi_callback_info info) 222 { 223 bool isFoldDevice = OH_NativeDisplayManager_IsFoldable(); 224 OH_LOG_INFO(LOG_APP, "IsFoldable isFoldDevice =%{public}d.", isFoldDevice); 225 napi_value isFold; 226 napi_get_boolean(env, isFoldDevice, &isFold); 227 return isFold; 228 } 229 230 EXTERN_C_START 231 static napi_value Init(napi_env env, napi_value exports) { 232 napi_property_descriptor desc[] = { 233 {"checkIsFoldDevice", nullptr, IsFoldable, nullptr, nullptr, nullptr, napi_default, nullptr}, 234 }; 235 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 236 return exports; 237 } 238 EXTERN_C_END 239 ``` 2402. 可以通过OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener注册屏幕展开/折叠状态变化的监听。 通过OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener接口取消屏幕展开/折叠状态变化的监听。 241 242 ```c++ 243 #include "napi/native_api.h" 244 #include <window_manager/oh_display_info.h> 245 #include <window_manager/oh_display_manager.h> 246 #include <hilog/log.h> 247 248 void FoldDisplayModeChangeCallback(NativeDisplayManager_FoldDisplayMode displayMode) 249 { 250 OH_LOG_INFO(LOG_APP, "displayMode=%{public}d.", displayMode); 251 } 252 253 static napi_value RegisterFoldDisplayModeChangeListener(napi_env env, napi_callback_info info) 254 { 255 uint32_t listenerIndex = 0; 256 NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener( 257 FoldDisplayModeChangeCallback, &listenerIndex); 258 OH_LOG_INFO(LOG_APP, "listenerIndex =%{public}d errCode=%{public}d.", 259 listenerIndex, errCode); 260 if (errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK) { 261 napi_value registerIndex; 262 napi_create_int32(env, listenerIndex, ®isterIndex); 263 return registerIndex; 264 } else { 265 napi_value errorCode; 266 napi_create_int32(env, errCode, &errorCode); 267 return errorCode; 268 } 269 } 270 271 static napi_value UnregisterFoldDisplayModeChangeListener(napi_env env, napi_callback_info info) 272 { 273 size_t argc = 1; 274 napi_value args[1] = { nullptr }; 275 uint32_t listenerIndex; 276 napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 277 napi_get_value_uint32(env, args[0], &listenerIndex); 278 OH_LOG_INFO(LOG_APP, "listenerIndex =%{public}d.", listenerIndex); 279 NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(listenerIndex); 280 OH_LOG_INFO(LOG_APP, "errorCode=%{public}d", errCode); 281 napi_value errorCode; 282 napi_create_int32(env, errCode, &errorCode); 283 return errorCode; 284 } 285 286 EXTERN_C_START 287 static napi_value Init(napi_env env, napi_value exports) { 288 napi_property_descriptor desc[] = { 289 { "registerFoldDisplayModeChange", nullptr, RegisterFoldDisplayModeChangeListener, nullptr, nullptr, nullptr, 290 napi_default, nullptr }, 291 { "unregisterFoldDisplayModeChange", nullptr, UnregisterFoldDisplayModeChangeListener, nullptr, nullptr, 292 nullptr, napi_default, nullptr }, 293 }; 294 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 295 return exports; 296 } 297 EXTERN_C_END 298 ```