Subversion Repositories tpanel

Rev

Rev 483 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tiosrotate.h"
20
#import <Foundation/Foundation.h>
21
#import <UIKit/UIKit.h>
22
#include <QGuiApplication>
23
#include <QScreen>
24
#include "terror.h"
25
 
26
#define O_UNDEFINED             -1
27
#define O_LANDSCAPE             0
28
#define O_PORTRAIT              1
29
#define O_REVERSE_LANDSCAPE     8
30
#define O_REVERSE_PORTRAIT      9
31
#define O_FACE_UP               15
32
#define O_FACE_DOWN             16
33
 
34
bool TIOSRotate::mPortrait{false};
35
 
36
TIOSRotate::TIOSRotate()
37
{
38
    DECL_TRACER("TIOSRotate::TIOSRotate()");
39
}
40
 
41
TIOSRotate::~TIOSRotate()
42
{
43
    DECL_TRACER("TIOSRotate::~TIOSRotate()");
44
}
45
 
46
void TIOSRotate::rotate(int dir)
47
{
48
    DECL_TRACER("TIOSRotate::rotate(int dir)");
49
 
50
    float value = 0.0;
51
 
52
    switch(dir)
53
    {
54
        case O_LANDSCAPE:           value = UIInterfaceOrientationMaskLandscapeRight; break;
55
        case O_REVERSE_LANDSCAPE:   value = UIInterfaceOrientationMaskLandscapeLeft; break;
56
        case O_PORTRAIT:            value = UIInterfaceOrientationMaskPortrait; break;
57
        case O_REVERSE_PORTRAIT:    value = UIInterfaceOrientationMaskPortraitUpsideDown; break;
58
    }
59
 
60
    NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
61
 
484 andreas 62
    if (!array || array.count <= 0)
446 andreas 63
    {
484 andreas 64
        MSG_ERROR("Error getting the array of scenes! Will not rotate.");
446 andreas 65
        return;
66
    }
67
 
68
    UIWindowScene *scene = (UIWindowScene *)array[0];
69
 
70
    if (!scene)
71
    {
483 andreas 72
        MSG_ERROR("Error getting the first scene! Will not rotate.");
446 andreas 73
        return;
74
    }
75
 
76
    UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:value];
77
 
78
    if (!geometryPreferences)
79
    {
80
        MSG_ERROR("Error getting the geometry preferences! Changing orientation failed.");
81
        return;
82
    }
83
 
84
    [scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) { NSLog(@"%@", error); }];
85
}
86
 
87
void TIOSRotate::automaticRotation(bool allow)
88
{
89
    DECL_TRACER("TIOSRotate::automaticRotation(bool allow)");
90
 
91
    UIDevice *device = [UIDevice currentDevice];
92
    BOOL generatesNotes = [device isGeneratingDeviceOrientationNotifications];
93
 
94
    if (allow && !generatesNotes)
95
    {
96
        [device beginGeneratingDeviceOrientationNotifications];
97
    }
98
    else if (generatesNotes)
99
    {
100
        [device endGeneratingDeviceOrientationNotifications];
101
    }
102
 
103
}
104
 
105
int TIOSRotate::getCurrentOrientation()
106
{
107
    DECL_TRACER("TIOSRotate::getCurrentOrientation()");
108
 
109
    UIDevice *device = [UIDevice currentDevice];
110
 
111
    if (!device)
112
        return O_UNDEFINED;
113
 
114
    int value = 0;
115
    value = [device orientation];
116
    MSG_DEBUG("getCurrentOrientation: " << value)
117
 
118
    switch(value)
119
    {
120
        case UIDeviceOrientationLandscapeLeft:       return O_LANDSCAPE;
121
        case UIDeviceOrientationLandscapeRight:      return O_REVERSE_LANDSCAPE;
122
        case UIDeviceOrientationPortrait:            return O_PORTRAIT;
123
        case UIDeviceOrientationPortraitUpsideDown:  return O_REVERSE_PORTRAIT;
124
    }
125
 
126
    return O_UNDEFINED;
127
}
128
 
129
bool TIOSRotate::isAutomaticRotation()
130
{
131
    DECL_TRACER("TIOSRotate::isAutomaticRotation()");
132
 
133
    UIDevice *device = [UIDevice currentDevice];
134
    return [device isGeneratingDeviceOrientationNotifications];
135
}