DesktopsEvent

Demonstrates how to access desktop changed handle options, add/remove call back for changed events, and perform related testing when programming with ADLX. To receive the event, another application must be used to change these settings, such as the Eyefinity.

Sample Path

/Samples/C/Desktop/DesktopsEvent

Code

Copied!

//
// Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved.
//
//-------------------------------------------------------------------------------------------------


#include "SDK/ADLXHelper/Windows/C/ADLXHelper.h"
#include "SDK/Include/IDesktops.h"
#include "SDK/Include/IDisplays.h"
#include "conio.h"

// Event to verify call back
HANDLE hDesktopChangedEvent = NULL;
HANDLE hQuitSignal = NULL;

// Call back to handle changed events
adlx_bool ADLX_STD_CALL OnDesktopListChanged(IADLXDesktopListChangedListener* pThis, IADLXDesktopList* pDesktopList)
{
    printf("Desktop list is changed\\n");
    ADLX_RESULT  res = ADLX_FAIL ;
    adlx_bool slsActive = false;
    IADLXDesktop* desktop = NULL;
    for (adlx_uint crt = pDesktopList->pVtbl->Begin(pDesktopList); crt != pDesktopList->pVtbl->End(pDesktopList); crt++)
    {
        res = pDesktopList->pVtbl->At_DesktopList(pDesktopList, crt, &desktop);
        if (NULL != desktop)
        {
            ADLX_DESKTOP_TYPE  adlxType;
            desktop->pVtbl->Type(desktop, &adlxType);
            if (adlxType == DESKTOP_EYEFINITY )
            {
                printf("Eyefinity Creation Event is received\\n");
                slsActive = true;
                if (desktop != NULL)
                {
                    desktop->pVtbl->Release(desktop);
                    desktop = NULL;
                }
                break;
            }
            if (desktop != NULL)
            {
                desktop->pVtbl->Release(desktop);
                desktop = NULL;
            }
        }
    }

    if (false == slsActive)
    {
        printf("Eyefinity Destruction Event is received\\n");
    }
    SetEvent(hDesktopChangedEvent);

    return true;
}

// Thread to detect quit input
DWORD WINAPI QuitWait(LPVOID lpParam);

static IADLXDesktopListChangedListenerVtbl vtbl = {&OnDesktopListChanged};
static IADLXDesktopListChangedListener eventListener = {&vtbl};

int main()
{
    // Add the following code to set environment variable
    LPCSTR EnvironmentVariable = "ADL_4KWORKAROUND_CANCEL";
    SetEnvironmentVariableA(EnvironmentVariable, "TRUE");

    // Define return code
    ADLX_RESULT  res = ADLX_FAIL ;

    // Initialize ADLX
    res = ADLXHelper_Initialize();
    IADLXSystem* sys = ADLXHelper_GetSystemServices();
    IADLXDesktopServices* desktopServices = NULL;
    IADLXSimpleEyefinity* eyefinity = NULL;
    IADLXDesktopChangedHandling* eventHandling = NULL;
    IADLXEyefinityDesktop* eyefinityDesktop = NULL;
    if (ADLX_SUCCEEDED (res))
    {
        // Create desktop changed event and user input event
        hDesktopChangedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        hQuitSignal = CreateEvent(NULL, TRUE, FALSE, NULL);
        HANDLE hSignalArray[2] = { hDesktopChangedEvent, hQuitSignal };

        // Get desktop services
        res = sys->pVtbl->GetDesktopsServices(sys, &desktopServices);

        if (desktopServices != NULL && ADLX_SUCCEEDED (res))
        {
            adlx_uint numDesktop;
            res = desktopServices->pVtbl->GetNumberOfDesktops(desktopServices, &numDesktop);
            printf("The number of desktop is: %d\\n", numDesktop);

            desktopServices->pVtbl->GetSimpleEyefinity(desktopServices, &eyefinity);
            if (eyefinity != NULL && ADLX_SUCCEEDED (res))
            {
                // Verify if Eyefinity is supported
                adlx_bool supported = false;
                eyefinity->pVtbl->IsSupported(eyefinity, &supported);
                if (supported)
                {
                    desktopServices->pVtbl->GetDesktopChangedHandling(desktopServices, &eventHandling);
                    eventHandling->pVtbl->AddDesktopListEventListener(eventHandling, &eventListener);

                    printf("Waiting for desktop event... Press Q/q to quit\\n");
                    // create a thread to detect user's input for quit
                    adlx_bool loopFlag = true;
                    DWORD threadId = 0;
                    HANDLE hThread = CreateThread(NULL, 0, QuitWait, &loopFlag, 0, &threadId);

                    res = eyefinity->pVtbl->Create(eyefinity, &eyefinityDesktop);

                    while (true)
                    {
                        // Waiting for receiving desktop event or user exit event
                        WaitForMultipleObjects(2, hSignalArray, FALSE, INFINITE);
                        if (!loopFlag)
                        {
                            CloseHandle(hThread);
                            break;
                        }
                        ResetEvent(hDesktopChangedEvent);
                    }

                    res = eyefinity->pVtbl->DestroyAll(eyefinity);
                    eventHandling->pVtbl->RemoveDesktopListEventListener(eventHandling, &eventListener);
                }
                else
                {
                    printf("Do not support eyefinity\\n");
                }
            }
            else
            {
                printf("Get simple eyefinity failed\\n");
            }
        }
        else
        {
            printf("Failed to get the desktop Services\\n");
        }
    }
    else
    {
        printf("ADLX initialize failed\\n");
        return 0;
    }

    if (eyefinityDesktop != NULL)
    {
        eyefinityDesktop->pVtbl->Release(eyefinityDesktop);
        eyefinityDesktop = NULL;
    }
    if (eventHandling != NULL)
    {
        eventHandling->pVtbl->Release(eventHandling);
        eventHandling = NULL;
    }
    if (eyefinity != NULL)
    {
        eyefinity->pVtbl->Release(eyefinity);
        eyefinity = NULL;
    }
    if (desktopServices != NULL)
    {
        desktopServices->pVtbl->Release(desktopServices);
        desktopServices = NULL;
    }

    // Destroy ADLX
    res = ADLXHelper_Terminate();
    printf("Destroy ADLX res: %d\\n", res);

    // Close event
    if (hDesktopChangedEvent)
        CloseHandle(hDesktopChangedEvent);
    if (hQuitSignal)
        CloseHandle(hQuitSignal);

    // Pause for user see the print out
    system("pause");

    return 0;
}

DWORD WINAPI QuitWait(LPVOID lpParam)
{
    adlx_bool* loopFlag = (adlx_bool*)lpParam;
    while (true)
    {
        // Non-block I/O, check I/O cache whether have characters.
        if (_kbhit())
        {
            char c;
            if ((c = getchar()) == 'q' || c == 'Q')
            {
                *loopFlag = false;
                SetEvent(hQuitSignal);
            }
        }
        Sleep(100);
    }
}

Related pages

  • Visit the Adlx product page for download links and more information.

Looking for more documentation on GPUOpen?

AMD GPUOpen software blogs

Our handy software release blogs will help you make good use of our tools, SDKs, and effects, as well as sharing the latest features with new releases.

GPUOpen Manuals

Don’t miss our manual documentation! And if slide decks are what you’re after, you’ll find 100+ of our finest presentations here.

AMD GPUOpen Performance Guides

The home of great performance and optimization advice for AMD RDNA™ 2 GPUs, AMD Ryzen™ CPUs, and so much more.

Getting started: AMD GPUOpen software

New or fairly new to AMD’s tools, libraries, and effects? This is the best place to get started on GPUOpen!

AMD GPUOpen Getting Started Development and Performance

Looking for tips on getting started with developing and/or optimizing your game, whether on AMD hardware or generally? We’ve got you covered!

AMD GPUOpen Technical blogs

Browse our technical blogs, and find valuable advice on developing with AMD hardware, ray tracing, Vulkan®, DirectX®, Unreal Engine, and lots more.

Find out more about our software!

AMD GPUOpen Effects - AMD FidelityFX technologies

Create wonder. No black boxes. Meet the AMD FidelityFX SDK!

AMD GPUOpen Samples

Browse all our useful samples. Perfect for when you’re needing to get started, want to integrate one of our libraries, and much more.

AMD GPUOpen developer SDKs

Discover what our SDK technologies can offer you. Query hardware or software, manage memory, create rendering applications or machine learning, and much more!

AMD GPUOpen Developer Tools

Analyze, Optimize, Profile, Benchmark. We provide you with the developer tools you need to make sure your game is the best it can be!