summaryrefslogtreecommitdiff
path: root/vcl/unx/generic/app/salinst.cxx
blob: f253f70d7d93b48d46edbdd35ac0a3eca008aeb8 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* -*- 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 .
 */

#include <stdlib.h>

#include <unx/saldata.hxx>
#include <unx/saldisp.hxx>
#include <unx/salinst.h>
#include <unx/geninst.h>
#include <unx/genpspgraphics.h>
#include <unx/salframe.h>
#include <unx/sm.hxx>
#include <unx/i18n_im.hxx>

#include <vcl/inputtypes.hxx>

#include <salwtype.hxx>

#include <config_features.h>
#include <vcl/skia/SkiaHelper.hxx>

// plugin factory function
extern "C"
{
    VCLPLUG_GEN_PUBLIC SalInstance* create_SalInstance()
    {
        /* #i92121# workaround deadlocks in the X11 implementation
        */
        static const char* pNoXInitThreads = getenv( "SAL_NO_XINITTHREADS" );
        /* #i90094#
           from now on we know that an X connection will be
           established, so protect X against itself
        */
        if( ! ( pNoXInitThreads && *pNoXInitThreads ) )
            XInitThreads();

        X11SalInstance* pInstance = new X11SalInstance( std::make_unique<SalYieldMutex>() );

        // initialize SalData
        X11SalData *pSalData = new X11SalData( SAL_DATA_UNX, pInstance );

        pSalData->Init();
        pInstance->SetLib( pSalData->GetLib() );

        return pInstance;
    }
}

X11SalInstance::X11SalInstance(std::unique_ptr<SalYieldMutex> pMutex)
    : SalGenericInstance(std::move(pMutex))
    , mpXLib(nullptr)
{
    ImplSVData* pSVData = ImplGetSVData();
    pSVData->maAppData.mxToolkitName = OUString("x11");
}

X11SalInstance::~X11SalInstance()
{
    // close session management
    SessionManagerClient::close();

    // dispose SalDisplay list from SalData
    // would be done in a static destructor else which is
    // a little late
    GetGenericUnixSalData()->Dispose();

#if HAVE_FEATURE_SKIA
    SkiaHelper::cleanup();
#endif
}

SalX11Display* X11SalInstance::CreateDisplay() const
{
    return new SalX11Display( mpXLib->GetDisplay() );
}

// AnyInput from sv/mow/source/app/svapp.cxx

namespace {

struct PredicateReturn
{
    VclInputFlags nType;
    bool          bRet;
};

}

extern "C" {
static Bool ImplPredicateEvent( Display *, XEvent *pEvent, char *pData )
{
    PredicateReturn *pPre = reinterpret_cast<PredicateReturn *>(pData);

    if ( pPre->bRet )
        return False;

    VclInputFlags nType;

    switch( pEvent->type )
    {
        case ButtonPress:
        case ButtonRelease:
        case MotionNotify:
        case EnterNotify:
        case LeaveNotify:
            nType = VclInputFlags::MOUSE;
            break;

        case KeyPress:
        //case KeyRelease:
            nType = VclInputFlags::KEYBOARD;
            break;
        case Expose:
        case GraphicsExpose:
        case NoExpose:
            nType = VclInputFlags::PAINT;
            break;
        default:
            nType = VclInputFlags::NONE;
    }

    if ( (nType & pPre->nType) || ( nType == VclInputFlags::NONE && (pPre->nType & VclInputFlags::OTHER) ) )
        pPre->bRet = true;

    return False;
}
}

bool X11SalInstance::AnyInput(VclInputFlags nType)
{
    GenericUnixSalData *pData = GetGenericUnixSalData();
    Display *pDisplay  = vcl_sal::getSalDisplay(pData)->GetDisplay();
    bool bRet = false;

    if( (nType & VclInputFlags::TIMER) && (mpXLib && mpXLib->CheckTimeout(false)) )
        bRet = true;

    if( !bRet && XPending(pDisplay) )
    {
        PredicateReturn aInput;
        XEvent          aEvent;

        aInput.bRet     = false;
        aInput.nType    = nType;

        XCheckIfEvent(pDisplay, &aEvent, ImplPredicateEvent,
                      reinterpret_cast<char *>(&aInput) );

        bRet = aInput.bRet;
    }
#if OSL_DEBUG_LEVEL > 1
    fprintf( stderr, "AnyInput 0x%x = %s\n", static_cast<unsigned int>(nType), bRet ? "true" : "false" );
#endif
    return bRet;
}

bool X11SalInstance::DoYield(bool bWait, bool bHandleAllCurrentEvents)
{
    return mpXLib->Yield( bWait, bHandleAllCurrentEvents );
}

OUString X11SalInstance::GetConnectionIdentifier()
{
    static const char* pDisplay = getenv( "DISPLAY" );
    return pDisplay ? OUString::createFromAscii(pDisplay) : OUString();
}

SalFrame *X11SalInstance::CreateFrame( SalFrame *pParent, SalFrameStyleFlags nSalFrameStyle )
{
    SalFrame *pFrame = new X11SalFrame( pParent, nSalFrameStyle );

    return pFrame;
}

SalFrame* X11SalInstance::CreateChildFrame( SystemParentData* pParentData, SalFrameStyleFlags nStyle )
{
    SalFrame* pFrame = new X11SalFrame( nullptr, nStyle, pParentData );

    return pFrame;
}

void X11SalInstance::DestroyFrame( SalFrame* pFrame )
{
    delete pFrame;
}

void X11SalInstance::AfterAppInit()
{
    assert( mpXLib->GetDisplay() );
    assert( mpXLib->GetInputMethod() );

    SalX11Display *pSalDisplay = CreateDisplay();
    mpXLib->GetInputMethod()->CreateMethod( mpXLib->GetDisplay() );
    pSalDisplay->SetupInput();
}

void X11SalInstance::AddToRecentDocumentList(const OUString&, const OUString&, const OUString&) {}

void X11SalInstance::PostPrintersChanged()
{
    SalDisplay* pDisp = vcl_sal::getSalDisplay(GetGenericUnixSalData());
    for (auto pSalFrame : pDisp->getFrames() )
        pDisp->PostEvent( pSalFrame, nullptr, SalEvent::PrinterChanged );
}

std::unique_ptr<GenPspGraphics> X11SalInstance::CreatePrintGraphics()
{
    return std::make_unique<GenPspGraphics>();
}

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