/**
 * @file hv_app_OsdDialog.c
 * @brief osd MiddleWare init c file.
 * @details This file provides the following functions: \n

 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-09-01
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author       <th>Date            <th>Change Description
 * <tr><td>shanpeng     <td>2022-09-01      <td>create
 * </table>
 */

#include "hv_app_Osd.h"
#include "hv_app_OsdDialog.h"
#include "hv_app_OsdMenu.h"
#include "hv_mw_OsdComm.h"
#include "hv_mw_OsdLog.h"
#include "hv_mw_Channel_Manager.h"
#include "hv_mw_OsdDraw.h"
#include "hv_mw_OsdEvent.h"
#include "hv_mw_OsdOperation.h"
#include "hv_app_OsdDynamicUpdate.h"
#include "hv_drv_Osd.h"

static OsdTimingInfo s_stCurTiming;

static const USHORT16 s_ausPipSubPositionWithRotation[4][4][2] =
{
    /* 0 degree */
    {{1, 99}, {1, 1}, {99, 99}, {99, 1}},
    /* 90 degree */
    {{1, 1}, {99, 1}, {1, 99}, {99, 99}},
    /* 180 degree */
    {{99, 1}, {99, 99}, {1, 1}, {1, 99}},
    /* 270 degree */
    {{99, 99}, {1, 99}, {99, 1}, {1, 1}},
};

static const USHORT16 s_ausPbpSubPositionWithRotation[4][2] =
{
    {75, 50}, {50, 75}, {25, 50}, {50, 25},
};

static const USHORT16 s_ausPbpMainPositionWithRotation[4][2] =
{
    {25, 50}, {50, 25}, {75, 50}, {50, 75},
};

OsdTimingInfo *Hv_App_OsdGetCurrentTimingInfo(VOID)
{
    //Hv_App_OsdSourceUpdateTimingInfo();
    //Hv_App_OsdSourceUpdateSubTimingInfo();

    return &s_stCurTiming;
}

VOID Hv_App_OsdSetBypassDialog(VOID)
{
    s_stCurTiming.bBypassDialog = HV_TRUE;
}

VOID Hv_App_OsdSignalUnsupportEnter(USHORT16 usValue)
{
    USHORT16 usChannelType = (usValue >> 8) & 0x000F;
    USHORT16 usPortIndex = (usValue >> 12) & 0x000F;
    USHORT16 usUnsupportReason = usValue & 0x00FF;      //ERROR_RX_TIMING_UNSUPPORT or ERROR_RX_TIMING_FREQ_OUT_OF_RANGE

    OSD_LOG_INFO("OSD receive signal unsupport, ChannelType = %d, Port = %d, UnsupportReason = %d", usChannelType, usPortIndex, usUnsupportReason);

    Hv_Mw_OsdSetAllKeyEnable(HV_TRUE);
    Hv_App_OsdHideAll();
    Hv_Mw_OsdSceneInit(SCENE_SIGNAL_UNSUPPORT);
    Hv_Mw_OsdSceneShow(SCENE_SIGNAL_UNSUPPORT);
    Hv_Mw_OsdItemShow(ITEM_UNSUPPORT_SCENE_SIGNAL_UNSUPPORT);
    if (usChannelType == CHANNEL_TYPE_MAIN)
    {
        s_stCurTiming.bSignalSync = HV_FALSE;
        Hv_Mw_OsdComponentUpdateByUserdata(COMPONENT(OSD_CP_TYPE_ICON_LIST, ICONLIST_UNSUPPORT_SCENE_SIGNAL_UNSUPPORT_NORMAL), E_USERDATA_VIDEOSOURCE);
    }
    else
    {
        s_stCurTiming.bSubSignalSync = HV_FALSE;
        Hv_Mw_OsdComponentUpdateByUserdata(COMPONENT(OSD_CP_TYPE_ICON_LIST, ICONLIST_UNSUPPORT_SCENE_SIGNAL_UNSUPPORT_NORMAL), E_USERDATA_PXPVIDEOSOURCE);
    }
    s_stCurTiming.bBypassDialog = HV_FALSE;

    //Hv_Mw_OsdStartAutoCloseTimer(SCENE_SIGNAL_UNSUPPORT, HV_PROJECT_CONFIG_OSD_DIALOG_SHOW_TIME);
    OSD_LOG_INFO("Show signal unsupport dialog");
}

VOID Hv_App_OsdSignalUnsupportExit(VOID)
{
    Hv_Mw_OsdSceneHide(SCENE_SIGNAL_UNSUPPORT);
    Hv_Mw_OsdSceneClear(SCENE_SIGNAL_UNSUPPORT);
}

VOID Hv_App_OsdNoSignalEnter(LinkPortIndex enPort)
{
    UCHAR8 ucSource = Hv_App_GetOsdDataValue(E_USERDATA_VIDEOSOURCE);
    USHORT16 usX = 0;
    USHORT16 usY = 0;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_OSD_ROTATION)
    UCHAR8 ucRotate = Hv_App_GetOsdDataValue(E_USERDATA_ROTATION);
#else
    UCHAR8 ucRotate = 0;
#endif

    Hv_Mw_OsdSetAllKeyEnable(HV_TRUE);
    if ((!Hv_Mw_OsdSceneIsShowing(SCENE_NOSIGNAL_SUB)) && (!Hv_Mw_OsdSceneIsShowing(SCENE_INPUT_SOURCE_SUB)))
    {
        Hv_App_OsdHideAll();
    }
    Hv_Mw_OsdSceneInit(SCENE_NOSIGNAL);
    Hv_Mw_OsdSceneShow(SCENE_NOSIGNAL);

    if (UD_PXPMODE_V_PBP == Hv_App_GetOsdDataValue(E_USERDATA_PXPMODE))
    {
        usX = s_ausPbpMainPositionWithRotation[ucRotate][0];
        usY = s_ausPbpMainPositionWithRotation[ucRotate][1];
    }
    else
    {
        usX = 50;
        usY = 50;
    }
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_NOSIGNAL, HV_TRUE, usX, 0, INVALID_PARAM_USHORT16);
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_NOSIGNAL, HV_FALSE, usY, 0, INVALID_PARAM_USHORT16);

    Hv_Mw_OsdItemShow(ITEM_NO_SIGNAL_SCENE_NOSIGNAL);

    Hv_Mw_OsdStartAutoCloseTimer(SCENE_NOSIGNAL, HV_PROJECT_CONFIG_OSD_DIALOG_SHOW_TIME);
    OSD_LOG_INFO("Show no signal dialog");
}

VOID Hv_App_OsdNoSignalExit(VOID)
{
    Hv_Mw_OsdSceneHide(SCENE_NOSIGNAL);
    Hv_Mw_OsdSceneClear(SCENE_NOSIGNAL);
}

VOID Hv_App_OsdSubNoSignalEnter(LinkPortIndex enPort)
{
    UCHAR8 ucSource = Hv_App_GetOsdDataValue(E_USERDATA_PXPVIDEOSOURCE);
    USHORT16 usX = 0;
    USHORT16 usY = 0;
    UCHAR8 ucPipPostion = Hv_App_GetOsdDataValue(E_USERDATA_PIP_POSITION);
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_OSD_ROTATION)
    UCHAR8 ucRotate = Hv_App_GetOsdDataValue(E_USERDATA_ROTATION);
#else
    UCHAR8 ucRotate = 0;
#endif

    Hv_Mw_OsdSetAllKeyEnable(HV_TRUE);
    if ((!Hv_Mw_OsdSceneIsShowing(SCENE_NOSIGNAL)) && (!Hv_Mw_OsdSceneIsShowing(SCENE_INPUT_SOURCE)))
    {
        Hv_App_OsdHideAll();
    }
    Hv_Mw_OsdSceneInit(SCENE_NOSIGNAL_SUB);
    Hv_Mw_OsdSceneShow(SCENE_NOSIGNAL_SUB);

    if (UD_PXPMODE_V_PIP == Hv_App_GetOsdDataValue(E_USERDATA_PXPMODE))
    {
        usX = s_ausPipSubPositionWithRotation[ucRotate][ucPipPostion][0];
        usY = s_ausPipSubPositionWithRotation[ucRotate][ucPipPostion][1];
    }
    else if (UD_PXPMODE_V_PBP == Hv_App_GetOsdDataValue(E_USERDATA_PXPMODE))
    {
        usX = s_ausPbpSubPositionWithRotation[ucRotate][0];
        usY = s_ausPbpSubPositionWithRotation[ucRotate][1];
    }
    else
    {
        usX = 50;
        usY = 50;
    }
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_NOSIGNAL_SUB, HV_TRUE, usX, 0, INVALID_PARAM_USHORT16);
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_NOSIGNAL_SUB, HV_FALSE, usY, 0, INVALID_PARAM_USHORT16);

    Hv_Mw_OsdItemShow(ITEM_NO_SIGNAL_SUB_SCENE_NOSIGNAL_SUB);

    Hv_Mw_OsdStartAutoCloseTimer(SCENE_NOSIGNAL_SUB, HV_PROJECT_CONFIG_OSD_DIALOG_SHOW_TIME);
    OSD_LOG_INFO("Show sub no signal dialog");
}

VOID Hv_App_OsdSubNoSignalExit(VOID)
{
    Hv_Mw_OsdSceneHide(SCENE_NOSIGNAL_SUB);
    Hv_Mw_OsdSceneClear(SCENE_NOSIGNAL_SUB);
}

static VOID Hv_App_OsdSourceUpdateTimingInfo(LinkPortIndex enPort)
{
    ChannelData stChannelData = {0};

    Hv_Mw_InputPort_GetVideoPara(enPort, &stChannelData);
    s_stCurTiming.enPort = enPort;
    s_stCurTiming.bInterlacedMode = stChannelData.stRxVideoTiming.bInterlacedMode;
    s_stCurTiming.usHActive = stChannelData.stRxVideoTiming.usHActive;
    s_stCurTiming.usVActive = stChannelData.stRxVideoTiming.bInterlacedMode ? stChannelData.stRxVideoTiming.usVActive * 2 : stChannelData.stRxVideoTiming.usVActive;
    s_stCurTiming.enColorFmt = stChannelData.stVideoConfigParams.stColor.eClrFormat;
    s_stCurTiming.bIsVrr = (stChannelData.stVideoConfigParams.stVRRInfoParam.bVRREnable);
    s_stCurTiming.bIsFreesync = (stChannelData.stVideoConfigParams.stVRRInfoParam.bVRREnable &&
                                 ((stChannelData.stVideoConfigParams.stVRRInfoParam.eVendor == VendorType_AMD) ||
                                  (stChannelData.stVideoConfigParams.stVRRInfoParam.eVendor == VendorType_AMD_AdaptSync)));

    s_stCurTiming.bIsHdr = (stChannelData.stVideoConfigParams.stHdrParam.xType != SDR_TYPE);

    UINT32 uiRefreshRate = Hv_Mw_InputPort_GetCurrentFrameRate(enPort);
    s_stCurTiming.usFrameRate = (uiRefreshRate + 500) / 1000;
    if (s_stCurTiming.bInterlacedMode)
    {
        s_stCurTiming.usFrameRate /= 2;
    }

    /* 判断是否支持ALLM */
    s_stCurTiming.bAllmSupport = Hv_Mw_Video_SetMainALLMStatus();
}

static VOID Hv_App_OsdSourceUpdateSubTimingInfo(LinkPortIndex enPort)
{
    ChannelData stChannelData = {0};

    Hv_Mw_InputPort_GetVideoPara(enPort, &stChannelData);
    s_stCurTiming.enSubPort = enPort;
    s_stCurTiming.usSubHActive = stChannelData.stRxVideoTiming.usHActive;
    s_stCurTiming.usSubVActive = stChannelData.stRxVideoTiming.bInterlacedMode ? stChannelData.stRxVideoTiming.usVActive * 2 : stChannelData.stRxVideoTiming.usVActive;

    UINT32 uiRefreshRate = Hv_Mw_InputPort_GetCurrentFrameRate(enPort);
    s_stCurTiming.usSubFrameRate = (uiRefreshRate + 500) / 1000;
    s_stCurTiming.enSubColorFmt = stChannelData.stVideoConfigParams.stColor.eClrFormat;
}

VOID Hv_App_OsdSourcePlugInUpdate(VOID)
{
    if (!Hv_Mw_OsdSceneIsShowing(SCENE_INPUT_SOURCE))
    {
        return;
    }

    OsdTimingInfo *pstTiming = Hv_App_OsdGetCurrentTimingInfo();

    LinkPortIndex enPortIndex = Hv_App_GetOsdDataValue(E_USERDATA_VIDEOSOURCE);
    UINT32 uiRefreshRate = Hv_Mw_InputPort_GetCurrentFrameRate(enPortIndex);

    if (pstTiming->bInterlacedMode)
    {
        uiRefreshRate /= 2;
    }
    uiRefreshRate = (uiRefreshRate + 500) / 1000;

    UCHAR8 aucString[OSD_ASCII_STRING_LENGTH_MAX] = { 0 };
    USHORT16 usStringLength = Hv_App_OsdSetTimingInfoString(aucString, s_stCurTiming.usHActive, s_stCurTiming.usVActive,
                                                   uiRefreshRate, HV_FALSE, s_stCurTiming.bInterlacedMode);
    Hv_Mw_OsdComponentShow(COMPONENT(OSD_CP_TYPE_ASCII_STRING, CUSTOMSTRING_TIMING_SCENE_INPUT_SOURCE_NORMAL));
    Hv_Mw_OsdDynamicAsciiStringUpdate(CUSTOMSTRING_TIMING_SCENE_INPUT_SOURCE_NORMAL, aucString, usStringLength);

    OSD_LOG_DEBUG("Update signal plug in dialog, port = %d, string: %s", enPortIndex, aucString);

    Hv_Mw_OsdStartAutoRefreshTimer(SCENE_INPUT_SOURCE, HV_PROJECT_CONFIG_OSD_DEFAULT_REFRESH_PERIOD_MS);
}

VOID Hv_App_OsdSourcePlugInEnter(LinkPortIndex enPort)
{
    UserdataValueVideoSource enSourceID = 0;
    UCHAR8 aucString[OSD_ASCII_STRING_LENGTH_MAX] = { 0 };
    USHORT16 usStringLength = 0;
    USHORT16 usX = 0;
    USHORT16 usY = 0;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_OSD_ROTATION)
    UCHAR8 ucRotate = Hv_App_GetOsdDataValue(E_USERDATA_ROTATION);
#else
    UCHAR8 ucRotate = 0;
#endif

    Hv_Mw_OsdSetAllKeyEnable(HV_TRUE);
    if ((!Hv_Mw_OsdSceneIsShowing(SCENE_NOSIGNAL_SUB)) && (!Hv_Mw_OsdSceneIsShowing(SCENE_INPUT_SOURCE_SUB)))
    {
        Hv_App_OsdHideAll();
    }
    Hv_Mw_OsdSceneInit(SCENE_INPUT_SOURCE);
    Hv_Mw_OsdSceneShow(SCENE_INPUT_SOURCE);
    //Hv_Mw_OsdComponentShow(COMPONENT(OSD_CP_TYPE_ASCII_STRING, ASCIISTRING_SOURCE_TYPE_SCENE_INPUT_SOURCE_NORMAL));

    if (UD_PXPMODE_V_PBP == Hv_App_GetOsdDataValue(E_USERDATA_PXPMODE))
    {
        usX = s_ausPbpMainPositionWithRotation[ucRotate][0];
        usY = s_ausPbpMainPositionWithRotation[ucRotate][1];
    }
    else
    {
        usX = 50;
        usY = 50;
    }
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_INPUT_SOURCE, HV_TRUE, usX, 0, INVALID_PARAM_USHORT16);
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_INPUT_SOURCE, HV_FALSE, usY, 0, INVALID_PARAM_USHORT16);

    switch (enPort)
    {
        case LINK_PORT_INDEX_HDMI_RX0:
        {
            enSourceID = UD_VIDEOSOURCE_V_HDMI1;
            break;
        }
        case LINK_PORT_INDEX_HDMI_RX1:
        {
            enSourceID = UD_VIDEOSOURCE_V_HDMI2;
            break;
        }
        case LINK_PORT_INDEX_DP_RX0:
        {
            enSourceID = UD_VIDEOSOURCE_V_USB_C;
            break;
        }
        case LINK_PORT_INDEX_DP_RX1:
        {
            enSourceID = UD_VIDEOSOURCE_V_DP;
            break;
        }
        default:
        {
            return;
        }
    }
    Hv_Mw_OsdAdjustFunction(E_USERDATA_VIDEOSOURCE, ACT_SET, enSourceID, SET_OSDDATA | SET_RELATED);
    Hv_Mw_OsdItemShow(ITEM_INPUT_TIMING_SCENE_INPUT_SOURCE);

    usStringLength = Hv_App_OsdSetTimingInfoString(aucString, s_stCurTiming.usHActive, s_stCurTiming.usVActive,
                                                   s_stCurTiming.usFrameRate, HV_FALSE, s_stCurTiming.bInterlacedMode);
    Hv_Mw_OsdDynamicAsciiStringUpdate(CUSTOMSTRING_TIMING_SCENE_INPUT_SOURCE_NORMAL, aucString, usStringLength);


    if (s_stCurTiming.bIsVrr)
    {
        Hv_Mw_OsdStartAutoRefreshTimer(SCENE_INPUT_SOURCE, HV_PROJECT_CONFIG_OSD_DEFAULT_REFRESH_PERIOD_MS);
    }
    else
    {
        Hv_Mw_OsdStopAutoRefreshTimer(SCENE_INPUT_SOURCE);
    }

    Hv_Mw_OsdStartAutoCloseTimer(SCENE_INPUT_SOURCE, HV_PROJECT_CONFIG_OSD_DIALOG_SHOW_TIME);
    OSD_LOG_INFO("Show signal plug in dialog, port = %d, string: %s", enPort, aucString);
}

VOID Hv_App_OsdSourcePlugInExit(VOID)
{
    Hv_Mw_OsdSceneHide(SCENE_INPUT_SOURCE);
    Hv_Mw_OsdSceneClear(SCENE_INPUT_SOURCE);
}

VOID Hv_App_OsdSubSourcePlugInEnter(LinkPortIndex enPort)
{
    UserdataValueVideoSource enSourceID = 0;
    UCHAR8 aucString[OSD_ASCII_STRING_LENGTH_MAX] = { 0 };
    USHORT16 usStringLength = 0;
    USHORT16 usX = 0;
    USHORT16 usY = 0;
    UCHAR8 ucPipPostion = Hv_App_GetOsdDataValue(E_USERDATA_PIP_POSITION);
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_OSD_ROTATION)
    UCHAR8 ucRotate = Hv_App_GetOsdDataValue(E_USERDATA_ROTATION);
#else
    UCHAR8 ucRotate = 0;
#endif

    if (UD_PXPMODE_V_PIP == Hv_App_GetOsdDataValue(E_USERDATA_PXPMODE))
    {
        usX = s_ausPipSubPositionWithRotation[ucRotate][ucPipPostion][0];
        usY = s_ausPipSubPositionWithRotation[ucRotate][ucPipPostion][1];
    }
    else if (UD_PXPMODE_V_PBP == Hv_App_GetOsdDataValue(E_USERDATA_PXPMODE))
    {
        usX = s_ausPbpSubPositionWithRotation[ucRotate][0];
        usY = s_ausPbpSubPositionWithRotation[ucRotate][1];
    }
    else
    {
        usX = 50;
        usY = 50;
    }
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_INPUT_SOURCE_SUB, HV_TRUE, usX, 0, INVALID_PARAM_USHORT16);
    Hv_Mw_OsdSceneSetPositionRatio(SCENE_INPUT_SOURCE_SUB, HV_FALSE, usY, 0, INVALID_PARAM_USHORT16);

    Hv_Mw_OsdSetAllKeyEnable(HV_TRUE);
    if ((!Hv_Mw_OsdSceneIsShowing(SCENE_NOSIGNAL)) && (!Hv_Mw_OsdSceneIsShowing(SCENE_INPUT_SOURCE)))
    {
        Hv_App_OsdHideAll();
    }
    Hv_Mw_OsdSceneInit(SCENE_INPUT_SOURCE_SUB);
    Hv_Mw_OsdSceneShow((SCENE_INPUT_SOURCE_SUB));

    switch (enPort)
    {
        case LINK_PORT_INDEX_HDMI_RX0:
        {
            enSourceID = UD_VIDEOSOURCE_V_HDMI1;
            break;
        }
        case LINK_PORT_INDEX_HDMI_RX1:
        {
            enSourceID = UD_VIDEOSOURCE_V_HDMI2;
            break;
        }
        case LINK_PORT_INDEX_DP_RX0:
        {
            enSourceID = UD_VIDEOSOURCE_V_USB_C;
            break;
        }
        case LINK_PORT_INDEX_DP_RX1:
        {
            enSourceID = UD_VIDEOSOURCE_V_DP;
            break;
        }
        default:
        {
            return;
        }
    }
    Hv_Mw_OsdAdjustFunction(E_USERDATA_PXPVIDEOSOURCE, ACT_SET, enSourceID, SET_OSDDATA | SET_RELATED);
    Hv_Mw_OsdItemShow(ITEM_INPUT_TIMING_SCENE_INPUT_SOURCE_SUB);

    usStringLength = Hv_App_OsdSetTimingInfoString(aucString, s_stCurTiming.usSubHActive, s_stCurTiming.usSubVActive,
                                                   s_stCurTiming.usSubFrameRate, HV_FALSE, s_stCurTiming.bInterlacedMode);
    Hv_Mw_OsdDynamicAsciiStringUpdate(CUSTOMSTRING_TIMING_SCENE_INPUT_SOURCE_SUB_NORMAL, aucString, usStringLength);

    Hv_Mw_OsdStartAutoCloseTimer(SCENE_INPUT_SOURCE_SUB, HV_PROJECT_CONFIG_OSD_DIALOG_SHOW_TIME);
    OSD_LOG_INFO("Show sub signal plug in dialog, port = %d, string: %s", enPort, aucString);
}

VOID Hv_App_OsdSubSourcePlugInExit(VOID)
{
    Hv_Mw_OsdSceneHide(SCENE_INPUT_SOURCE_SUB);
    Hv_Mw_OsdSceneClear(SCENE_INPUT_SOURCE_SUB);
}

VOID Hv_App_OsdSourcePlugOut(USHORT16 usValue)
{
    OSD_LOG_INFO("Source plug out");
    USHORT16 usChannelType = usValue & 0x00FF;
    USHORT16 usPortIndex = (usValue >> 8) & 0x00FF;

    if (usChannelType == CHANNEL_TYPE_MAIN)
    {
        s_stCurTiming.bSignalSync = HV_FALSE;
    }
    else
    {
        s_stCurTiming.bSubSignalSync = HV_FALSE;
    }
    s_stCurTiming.bBypassDialog = HV_FALSE;

    if ((!s_stCurTiming.bSignalSync) && (!s_stCurTiming.bSubSignalSync))
    {
        OSD_LOG_INFO("No signal sync, hide all");
        Hv_App_OsdHideAll();
        Hv_Mw_OsdSetKeyState(OSDEVT_KEY_UP, KEY_STATE_REPEATABLE, HV_FALSE);
        Hv_Mw_OsdSetKeyState(OSDEVT_KEY_DOWN, KEY_STATE_REPEATABLE, HV_FALSE);
    }
}

VOID Hv_App_OsdSignalReady(USHORT16 usValue)
{
    USHORT16 usChannelType = usValue & 0x00FF;
    USHORT16 usPortIndex = (usValue >> 8) & 0x00FF;

    if (!s_stCurTiming.bBypassDialog)
    {
        Hv_App_OsdHideAll();
    }
    if (usChannelType == CHANNEL_TYPE_MAIN)
    {
        Hv_App_OsdSourceUpdateTimingInfo(usPortIndex);

        /* Recalculate OSD timing */
        const ChannelManager *pstManager = Hv_Mw_Channel_GetManager();
        const VideoConfigParams *pstConfigParams = &pstManager->astChannelData[0].stVideoConfigParams;
        Hv_Drv_OsdFrame_ReCalculateTiming(pstConfigParams->uiOutputFrameRate, s_stCurTiming.bIsVrr);
    }
    else
    {
        Hv_App_OsdSourceUpdateSubTimingInfo(usPortIndex);
    }
    Hv_App_UpdateSettingByCurrentTiming(usChannelType);
}

VOID Hv_App_OsdSignalStateEnter(USHORT16 usValue)
{
    LinkPortIndex enPortIndex = LINK_PORT_INDEX_INVALID;
    ChanelSignalState enSignalState = ChanelSignalState_Invalid;
    USHORT16 usChannelType = usValue & 0x00FF;
    //USHORT16 usPortIndex = (usValue >> 8) & 0x00FF;

    Hv_Mw_Channel_GetSignalState(usChannelType, &enPortIndex, &enSignalState);

    if (usChannelType == CHANNEL_TYPE_MAIN)
    {
        s_stCurTiming.bSignalSync = (ChanelSignalState_SignalSync == enSignalState);
    }
    else
    {
        s_stCurTiming.bSubSignalSync = (ChanelSignalState_SignalSync == enSignalState);
    }
    OSD_LOG_INFO("Osd receive signal change: port %u state %u", enPortIndex, enSignalState);

    switch (enSignalState)
    {
        case ChanelSignalState_SearchState:
        {
            break;
        }
        case ChanelSignalState_NoSignal:
        {
            if (usChannelType == CHANNEL_TYPE_MAIN)
            {
                /* show no signal */
                Hv_App_OsdNoSignalEnter(enPortIndex);
            }
            else
            {
                /* show sub no signal */
                Hv_App_OsdSubNoSignalEnter(enPortIndex);
            }
            s_stCurTiming.bBypassDialog = HV_FALSE;
            break;
        }
        case ChanelSignalState_SignalSync:
        {
            if (!s_stCurTiming.bBypassDialog)
            {
                /* show video source & timing & frame rate */
                if (usChannelType == CHANNEL_TYPE_MAIN)
                {
                    Hv_App_OsdSourcePlugInEnter(enPortIndex);
                }
                else
                {
                    if (UD_MAGNIFY_GLASS_V_OFF == Hv_App_GetOsdDataValue(E_USERDATA_MAGNIFY_GLASS))
                    {
                        Hv_App_OsdSubSourcePlugInEnter(enPortIndex);
                    }
                }
            }
            else
            {
                OSD_LOG_INFO("Bypass osd dialog");
                s_stCurTiming.bBypassDialog = HV_FALSE;
            }
            break;
        }
        default:
        {
            break;
        }
    }
}
