summaryrefslogtreecommitdiffstats
path: root/vcl/unx/generic/app/randrwrapper.cxx
blob: 1ef474c347f9be6154c898b10fdcec605cf36fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifdef USE_RANDR

#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

#include <sal/log.hxx>

namespace
{

class RandRWrapper
{
    bool m_bValid;

    explicit RandRWrapper(Display*);
public:
    static RandRWrapper& get(Display*);
    static void releaseWrapper();

    Bool XRRQueryExtension(Display* i_pDisp, int* o_event_base, int* o_error_base )
    {
        Bool bRet = False;
        if( m_bValid )
            bRet = ::XRRQueryExtension( i_pDisp, o_event_base, o_error_base );
        return bRet;
    }
    XRRScreenConfiguration* XRRGetScreenInfo( Display* i_pDisp, Drawable i_aDrawable )
    {
        return m_bValid ? ::XRRGetScreenInfo( i_pDisp, i_aDrawable ) : nullptr;
    }
    void XRRFreeScreenConfigInfo( XRRScreenConfiguration* i_pConfig )
    {
        if( m_bValid )
            ::XRRFreeScreenConfigInfo( i_pConfig );
    }
    void XRRSelectInput( Display* i_pDisp, ::Window i_window, int i_nMask )
    {
        if( m_bValid )
            ::XRRSelectInput( i_pDisp, i_window, i_nMask );
    }
    int XRRUpdateConfiguration( XEvent* i_pEvent )
    {
        return m_bValid ? ::XRRUpdateConfiguration( i_pEvent ) : 0;
    }
    XRRScreenSize* XRRConfigSizes( XRRScreenConfiguration* i_pConfig, int* o_nSizes )
    {
        return m_bValid ? ::XRRConfigSizes( i_pConfig, o_nSizes ) : nullptr;
    }
    SizeID XRRConfigCurrentConfiguration( XRRScreenConfiguration* i_pConfig, Rotation* o_pRot )
    {
        return m_bValid ? ::XRRConfigCurrentConfiguration( i_pConfig, o_pRot ) : 0;
    }
    int XRRRootToScreen( Display *dpy, ::Window root )
    {
        return m_bValid ? ::XRRRootToScreen( dpy, root ) : -1;
    }
};

RandRWrapper::RandRWrapper( Display* pDisplay ) :
    m_bValid( true )
{
    int nEventBase = 0, nErrorBase = 0;
    if( !XRRQueryExtension( pDisplay, &nEventBase, &nErrorBase ) )
        m_bValid = false;
}

RandRWrapper* pWrapper = nullptr;

RandRWrapper& RandRWrapper::get( Display* i_pDisplay )
{
    if( ! pWrapper )
        pWrapper = new RandRWrapper( i_pDisplay );
    return *pWrapper;
}

void RandRWrapper::releaseWrapper()
{
    delete pWrapper;
    pWrapper = nullptr;
}

} // namespace

#endif

#include <unx/saldisp.hxx>
#if OSL_DEBUG_LEVEL > 1
#include <cstdio>
#endif

void SalDisplay::InitRandR( ::Window aRoot ) const
{
    #ifdef USE_RANDR
    RandRWrapper::get( GetDisplay() ).XRRSelectInput( GetDisplay(), aRoot, RRScreenChangeNotifyMask );
    #else
    (void)this;
    (void)aRoot;
    #endif
}

void SalDisplay::DeInitRandR()
{
    #ifdef USE_RANDR
    RandRWrapper::releaseWrapper();
#if OSL_DEBUG_LEVEL > 1
    SAL_INFO("vcl.app", "SalDisplay::DeInitRandR().");
#endif
    #endif
}

void SalDisplay::processRandREvent( XEvent* pEvent )
{
#ifdef USE_RANDR
    XConfigureEvent* pCnfEvent=reinterpret_cast<XConfigureEvent*>(pEvent);
    if( !pWrapper || pWrapper->XRRRootToScreen(GetDisplay(),pCnfEvent->window) == -1 )
        return;

    int nRet = pWrapper->XRRUpdateConfiguration( pEvent );
    if( nRet != 1 || pEvent->type == ConfigureNotify) // this should then be a XRRScreenChangeNotifyEvent
        return;

    // update screens
    bool bNotify = false;
    for(ScreenData & rScreen : m_aScreens)
    {
        if( rScreen.m_bInit )
        {
            XRRScreenConfiguration *pConfig = nullptr;
            XRRScreenSize *pSizes = nullptr;
            int nSizes = 0;
            Rotation nRot = 0;
            SizeID nId = 0;

            pConfig = pWrapper->XRRGetScreenInfo( GetDisplay(), rScreen.m_aRoot );
            nId = pWrapper->XRRConfigCurrentConfiguration( pConfig, &nRot );
            pSizes = pWrapper->XRRConfigSizes( pConfig, &nSizes );
            XRRScreenSize *pTargetSize = pSizes + nId;

            bNotify = bNotify ||
                      rScreen.m_aSize.Width() != pTargetSize->width ||
                      rScreen.m_aSize.Height() != pTargetSize->height;

            rScreen.m_aSize = AbsoluteScreenPixelSize( pTargetSize->width, pTargetSize->height );

            pWrapper->XRRFreeScreenConfigInfo( pConfig );

#if OSL_DEBUG_LEVEL > 1
            SAL_INFO("vcl.app", "screen " << nId
                    << " changed to size " << (int)pTargetSize->width
                    << "x" << (int)pTargetSize->height);
#endif
        }
    }
    if( bNotify )
        emitDisplayChanged();
#else
    (void)this;
    (void)pEvent;
#endif
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */