tools/qml/deviceorientation_maemo.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "deviceorientation.h"
       
    43 #include <stdio.h>
       
    44 #include <stdlib.h>
       
    45 
       
    46 class MaemoOrientation : public DeviceOrientation
       
    47 {
       
    48     Q_OBJECT
       
    49 public:
       
    50     MaemoOrientation()
       
    51         : DeviceOrientation(),m_current(Portrait), m_lastSeen(Portrait), m_lastSeenCount(0)
       
    52     {
       
    53         m_current = get();
       
    54         if (m_current == UnknownOrientation) 
       
    55             m_current = Portrait;
       
    56 
       
    57         startTimer(100);
       
    58     }
       
    59 
       
    60     Orientation orientation() const {
       
    61         return m_current;
       
    62     }
       
    63 
       
    64     void setOrientation(Orientation) { }
       
    65 
       
    66 protected:
       
    67     virtual void timerEvent(QTimerEvent *)
       
    68     {
       
    69         Orientation c = get();
       
    70 
       
    71         if (c == m_lastSeen) {
       
    72             m_lastSeenCount++;
       
    73         } else {
       
    74             m_lastSeenCount = 0;
       
    75             m_lastSeen = c;
       
    76         }
       
    77 
       
    78         if (m_lastSeen != UnknownOrientation && m_lastSeen != m_current && m_lastSeenCount > 4) {
       
    79             m_current = m_lastSeen;
       
    80             emit orientationChanged();
       
    81             printf("%d\n", m_current);
       
    82         }
       
    83     }
       
    84 
       
    85 signals:
       
    86     void changed();
       
    87 
       
    88 private:
       
    89     Orientation m_current;
       
    90     Orientation m_lastSeen;
       
    91     int m_lastSeenCount;
       
    92 
       
    93     Orientation get()
       
    94     {
       
    95         Orientation o = UnknownOrientation;
       
    96 
       
    97         int ax, ay, az;
       
    98 
       
    99         read(&ax, &ay, &az);
       
   100 
       
   101         if (abs(az) > 850) {
       
   102             o = UnknownOrientation;
       
   103         } else if (ax < -750) {
       
   104             o = Portrait;
       
   105         } else if (ay < -750) {
       
   106             o = Landscape;
       
   107         }
       
   108 
       
   109         return o;
       
   110     }
       
   111 
       
   112     int read(int *ax,int *ay,int *az)
       
   113     {
       
   114         static const char *accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
       
   115 
       
   116         FILE *fd;
       
   117         int rs;
       
   118         fd = fopen(accel_filename, "r");
       
   119         if(fd==NULL){ printf("liqaccel, cannot open for reading\n"); return -1;}
       
   120         rs=fscanf((FILE*) fd,"%i %i %i",ax,ay,az);
       
   121         fclose(fd);
       
   122         if(rs != 3){ printf("liqaccel, cannot read information\n"); return -2;}
       
   123         return 0;
       
   124     }
       
   125 };
       
   126 
       
   127 
       
   128 DeviceOrientation* DeviceOrientation::instance()
       
   129 {
       
   130     static MaemoOrientation *o = 0;
       
   131     if (!o)
       
   132         o = new MaemoOrientation;
       
   133     return o;
       
   134 }
       
   135 
       
   136 #include "deviceorientation_maemo.moc"