summaryrefslogtreecommitdiff
path: root/vcl/android/androidinst.cxx
blob: aba4b0b3ca65f3eacb5ce2e6b41e49fec816c042 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 *   Copyright (C) 2012 Novell, Inc.
 *   Michael Meeks <michael.meeks@suse.com> (initial developer)
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */
#include <android/androidinst.hxx>
#include <headless/svpdummies.hxx>
#include <generic/gendata.hxx>
#include <android/log.h>
#include <android/input.h>
#include <android/looper.h>
#include <android/native_window.h>
#include <osl/detail/android.h>
#include <rtl/strbuf.hxx>

static rtl::OString MotionEdgeFlagsToString(int32_t nFlags)
{
    rtl::OStringBuffer aStr;
    if (nFlags == AMOTION_EVENT_EDGE_FLAG_NONE)
        aStr.append ("no-edge");
    if (nFlags & AMOTION_EVENT_EDGE_FLAG_TOP)
        aStr.append (":top");
    if (nFlags & AMOTION_EVENT_EDGE_FLAG_BOTTOM)
        aStr.append (":bottom");
    if (nFlags & AMOTION_EVENT_EDGE_FLAG_LEFT)
        aStr.append (":left");
    if (nFlags & AMOTION_EVENT_EDGE_FLAG_RIGHT)
        aStr.append (":right");
    return aStr.makeStringAndClear();
}

static rtl::OString KeyMetaStateToString(int32_t nFlags)
{
    rtl::OStringBuffer aStr;
    if (nFlags == AMETA_NONE)
        aStr.append ("no-meta");
    if (nFlags & AMETA_ALT_ON)
        aStr.append (":alt");
    if (nFlags & AMETA_SHIFT_ON)
        aStr.append (":shift");
    if (nFlags & AMETA_SYM_ON)
        aStr.append (":sym");
    return aStr.makeStringAndClear();
}

int32_t AMotionEvent_getEdgeFlags(const AInputEvent* motion_event);

extern "C" {
    void onAppCmd_cb (struct android_app* app, int32_t cmd)
    {
        fprintf (stderr, "app cmd for app %p, cmd %d\n", app, cmd);
        ANativeWindow *pWindow = global_android_app->window;
        switch (cmd) {
        case APP_CMD_INIT_WINDOW:
        {
            ARect aRect = { 0, 0, 0, 0 };
            aRect.right = ANativeWindow_getWidth(pWindow);
            aRect.bottom = ANativeWindow_getHeight(pWindow);
            fprintf (stderr, "we have an app window ! %p %dx%x (%d)\n",
                     pWindow, aRect.right, aRect.bottom,
                     ANativeWindow_getFormat(pWindow));
            ANativeWindow_Buffer aBuffer;
            memset ((void *)&aBuffer, 0, sizeof (aBuffer));
            int32_t nRet = ANativeWindow_lock(pWindow, &aBuffer, &aRect);
            fprintf (stderr, "locked window %d returned rect: %d,%d->%d,%d "
                     "buffer: %dx%d stride %d, format %d, bits %p\n",
                     nRet, aRect.left, aRect.top, aRect.right, aRect.bottom,
                     aBuffer.width, aBuffer.height, aBuffer.stride,
                     aBuffer.format, aBuffer.bits);
            if (aBuffer.bits)
            {
                // hard-code / guess at a format ...
                int32_t *p = (int32_t *)aBuffer.bits;
                for (int32_t y = 0; y < aBuffer.height; y++)
                {
                    for (int32_t x = 0; x < aBuffer.stride / 4; x++)
                        *p++ = (y << 24) + x;
                }
            }
            ANativeWindow_unlockAndPost(pWindow);
            fprintf (stderr, "done render!\n");
            break;
        }
        case APP_CMD_WINDOW_RESIZED:
        {
            ARect aRect = { 0, 0, 0, 0 };
            aRect.right = ANativeWindow_getWidth(pWindow);
            aRect.bottom = ANativeWindow_getHeight(pWindow);
            fprintf (stderr, "app window resized to ! %p %dx%x (%d)\n",
                     pWindow, aRect.right, aRect.bottom,
                     ANativeWindow_getFormat(pWindow));
            break;
        }

        case APP_CMD_CONTENT_RECT_CHANGED:
        {
            ARect aRect = global_android_app->contentRect;
            fprintf (stderr, "content rect changed [ k/b popped up etc. ] %d,%d->%d,%d\n",
                     aRect.left, aRect.top, aRect.right, aRect.bottom);
            break;
        }
        default:
            fprintf (stderr, "unhandled app cmd %d\n", cmd);
            break;
        }
    }

    int32_t onInputEvent_cb (struct android_app* app, AInputEvent* event)
    {
        fprintf (stderr, "input event for app %p, event %p type %d source %d device id %d\n",
                 app, event,
                 AInputEvent_getType(event),
                 AInputEvent_getSource(event),
                 AInputEvent_getDeviceId(event));

        switch (AInputEvent_getType(event))
        {
        case AINPUT_EVENT_TYPE_KEY:
        {
            int32_t nAction = AKeyEvent_getAction(event);
            fprintf (stderr, "key event keycode %d '%s' %s\n",
                     AKeyEvent_getKeyCode(event),
                     nAction == AKEY_EVENT_ACTION_DOWN ? "down" :
                     nAction == AKEY_EVENT_ACTION_UP ? "up" : "multiple",
                     KeyMetaStateToString(AKeyEvent_getMetaState(event)).getStr());
            break;
        }
        case AINPUT_EVENT_TYPE_MOTION:
        {
            fprintf (stderr, "motion event %d %g %g %s\n",
                     AMotionEvent_getAction(event),
                     AMotionEvent_getXOffset(event),
                     AMotionEvent_getYOffset(event),
                     MotionEdgeFlagsToString(AMotionEvent_getEdgeFlags(event)).getStr());
            break;
        }
        default:
            fprintf (stderr, "unknown event type %p %d\n",
                     event, AInputEvent_getType(event));
        }
        return 1; // handled 0 for not ...
    }
}

AndroidSalInstance::AndroidSalInstance( SalYieldMutex *pMutex )
    : SvpSalInstance( pMutex )
{
    fprintf (stderr, "created Android Sal Instance for app %p window %p\n",
             global_android_app,
             global_android_app ? global_android_app->window : NULL);
    if (global_android_app)
    {
        pthread_mutex_lock (&global_android_app->mutex);
        global_android_app->onAppCmd = onAppCmd_cb;
        global_android_app->onInputEvent = onInputEvent_cb;
        if (global_android_app->window != NULL)
            onAppCmd_cb (global_android_app, APP_CMD_INIT_WINDOW);
        pthread_mutex_unlock (&global_android_app->mutex);
    }
}

AndroidSalInstance::~AndroidSalInstance()
{
    fprintf (stderr, "destroyed Android Sal Instance\n");
}

void AndroidSalInstance::Wakeup()
{
    fprintf (stderr, "Wakeup alooper\n");
    if (global_android_app && global_android_app->looper)
        ALooper_wake (global_android_app->looper);
    else
        fprintf (stderr, "busted - no global looper\n");
}

void AndroidSalInstance::DoReleaseYield (int nTimeoutMS)
{
    // release yield mutex
    sal_uLong nAcquireCount = ReleaseYieldMutex();
    struct android_app *pApp = global_android_app;

    fprintf (stderr, "DoReleaseYield #2 %d ms\n", nTimeoutMS);
    void *outData = NULL;
    int outFd = 0, outEvents = 0;
    int nRet = ALooper_pollAll(nTimeoutMS, &outFd, &outEvents, &outData);
    fprintf (stderr, "ret %d %d %d %p\n", nRet, outFd, outEvents, outData);

    // acquire yield mutex again
    AcquireYieldMutex(nAcquireCount);

    // FIXME: this is more or less deranged: why can we not
    // set a callback in the native app glue's ALooper_addFd ?
    if (nRet == LOOPER_ID_MAIN)
        pApp->cmdPollSource.process(pApp, &pApp->cmdPollSource);
    if (nRet == LOOPER_ID_INPUT)
        pApp->inputPollSource.process(pApp, &pApp->inputPollSource);
}

bool AndroidSalInstance::AnyInput( sal_uInt16 nType )
{
    (void) nType;
    // FIXME: ideally we should check the input queue to avoid being busy ...
    fprintf (stderr, "FIXME: AnyInput returns true\n");
    // global_android_app->inputQueue ? ...
    return true;
}

class AndroidSalSystem : public SvpSalSystem {
public:
    AndroidSalSystem() : SvpSalSystem() {}
    virtual ~AndroidSalSystem() {}
    virtual int ShowNativeDialog( const rtl::OUString& rTitle,
                                  const rtl::OUString& rMessage,
                                  const std::list< rtl::OUString >& rButtons,
                                  int nDefButton )
    {
        (void)rButtons; (void)nDefButton;
        __android_log_print(ANDROID_LOG_INFO, "LibreOffice - dialog '%s': '%s'",
                            rtl::OUStringToOString(rTitle, RTL_TEXTENCODING_ASCII_US).getStr(),
                            rtl::OUStringToOString(rMessage, RTL_TEXTENCODING_ASCII_US).getStr());
        return 0;
    }
};

SalSystem *AndroidSalInstance::CreateSalSystem()
{
    return new AndroidSalSystem();
}

class AndroidSalData : public SalGenericData
{
public:
    AndroidSalData( SalInstance *pInstance ) : SalGenericData( SAL_DATA_ANDROID, pInstance ) {}
    virtual void ErrorTrapPush() {}
    virtual bool ErrorTrapPop( bool ) { return false; }
};

// All the interesting stuff is slaved from the AndroidSalInstance
void InitSalData()   {}
void DeInitSalData() {}
void InitSalMain()   {}
void DeInitSalMain() {}

void SalAbort( const rtl::OUString& rErrorText, bool bDumpCore )
{
    rtl::OUString aError( rErrorText );
    if( aError.isEmpty() )
        aError = rtl::OUString::createFromAscii("Unknown application error");
    ::fprintf( stderr, "%s\n", rtl::OUStringToOString(rErrorText, osl_getThreadTextEncoding()).getStr() );

    __android_log_print(ANDROID_LOG_INFO, "SalAbort: '%s'",
                        rtl::OUStringToOString(aError, RTL_TEXTENCODING_ASCII_US).getStr());
    if( bDumpCore )
        abort();
    else
        _exit(1);
}

const OUString& SalGetDesktopEnvironment()
{
    static rtl::OUString aEnv( RTL_CONSTASCII_USTRINGPARAM( "android" ) );
    return aEnv;
}

SalData::SalData() :
    m_pInstance( 0 ),
    m_pPlugin( 0 ),
    m_pPIManager(0 )
{
}

SalData::~SalData()
{
}

// This is our main entry point:
SalInstance *CreateSalInstance()
{
    AndroidSalInstance* pInstance = new AndroidSalInstance( new SalYieldMutex() );
    new AndroidSalData( pInstance );
    return pInstance;
}

void DestroySalInstance( SalInstance *pInst )
{
    pInst->ReleaseYieldMutex();
    delete pInst;
}

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