Touchgui/plugins/org.apache.cordova.vibration/src/ubuntu/vibration.cpp

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.vibration/src/ubuntu/vibration.cpp	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,64 @@
     1.4 +/*
     1.5 + *
     1.6 + * Copyright 2013 Canonical Ltd.
     1.7 + *
     1.8 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.9 + * you may not use this file except in compliance with the License.
    1.10 + * You may obtain a copy of the License at
    1.11 + *
    1.12 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.13 + *
    1.14 + * Unless required by applicable law or agreed to in writing,
    1.15 + * software distributed under the License is distributed on an
    1.16 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.17 + * KIND, either express or implied.  See the License for the
    1.18 + * specific language governing permissions and limitations
    1.19 + * under the License.
    1.20 + *
    1.21 +*/
    1.22 +
    1.23 +#include "vibration.h"
    1.24 +
    1.25 +void Vibration::vibrate(int, int, int mills) {
    1.26 +    QSharedPointer<QFeedbackHapticsEffect> vibrate = QSharedPointer<QFeedbackHapticsEffect>::create();
    1.27 +    vibrate->setIntensity(1.0);
    1.28 +    vibrate->setDuration(mills);
    1.29 +
    1.30 +    vibrate->start();
    1.31 +
    1.32 +    _effects.append(vibrate);
    1.33 +}
    1.34 +
    1.35 +void Vibration::cancelVibration(int, int) {
    1.36 +    _timers.clear();
    1.37 +    _effects.clear();
    1.38 +}
    1.39 +
    1.40 +void Vibration::vibrateWithPattern(int, int, const QList<int> &pattern, int repeat) {
    1.41 +    QSharedPointer<QTimer> timer = QSharedPointer<QTimer>::create();
    1.42 +    QSharedPointer<int> k = QSharedPointer<int>::create();
    1.43 +
    1.44 +    QSharedPointer<QFeedbackHapticsEffect> vibrate = QSharedPointer<QFeedbackHapticsEffect>::create();
    1.45 +    vibrate->setIntensity(1.0);
    1.46 +
    1.47 +    _effects.append(vibrate);
    1.48 +    _timers.append(timer);
    1.49 +
    1.50 +    timer->connect(timer.data(), &QTimer::timeout, [=, timer = timer.data()] () {
    1.51 +        if (*k >= pattern.size()) {
    1.52 +            if (repeat < 0 || repeat >= pattern.size()) {
    1.53 +                timer->stop();
    1.54 +                return;
    1.55 +            }
    1.56 +            *k = repeat;
    1.57 +        }
    1.58 +        bool idle = (*k % 2 == 0);
    1.59 +        if (!idle) {
    1.60 +            vibrate->setDuration(pattern[*k]);
    1.61 +            vibrate->start();
    1.62 +        }
    1.63 +        timer->start(pattern[*k]);
    1.64 +        (*k)++;
    1.65 +    });
    1.66 +    timer->start(1);
    1.67 +}

mercurial