summaryrefslogtreecommitdiffstats
path: root/src/widget.cpp
blob: d2a676585008e4a1b16e7017fb8b08e0264cbd47 (plain) (blame)
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
#include "widget.h"
#include "ui_widget.h"
#include "config.h"

#include <QString>
#include <QSet>
#include <algorithm>
#include <iostream>

//___________________________________________________________________________
int compareModeString (const QString & a, const QString & b) {
  if (a.size() == b.size())
    return a.compare(b);
  else
    if (a.size() > b.size())
      return -1;
    else
      return 1;
}

//___________________________________________________________________________
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    _ui(new Ui::Widget)
{
  _ui->setupUi(this);

  // Get initial data (to be freed)
  _display = XOpenDisplay(NULL);
  _screenResources = XRRGetScreenResourcesCurrent(_display, DefaultRootWindow(_display));

  // Get the information about the X elements
  updateScreenResources();

  switch ( _connectedOutputList.size() ){
    /*************************************************************************/
    case 1:// In case of one connected output - xrandr --auto
      qDebug() << "Normal output";
      exit(0);
      break;
    /*************************************************************************/
    case 2: // In case of two connected outputs

      // Check if one of the connected outputs is a beamer. This program only
      // adresses cases in which eiter of the outouts is a beamer. Meaning
      // either output one has dimension zero or output tow has no dimension.
      // The case of two beamers is not covered.
      if (/*true*/((_outputMap[_connectedOutputList[0]]->mm_width == 0
           && _outputMap[_connectedOutputList[0]]->mm_height == 0 )
          && ! (_outputMap[_connectedOutputList[1]]->mm_width == 0
           && _outputMap[_connectedOutputList[1]]->mm_height == 0 ))
          || ( ! (_outputMap[_connectedOutputList[0]]->mm_width == 0
           && _outputMap[_connectedOutputList[0]]->mm_height == 0 )
          && (_outputMap[_connectedOutputList[1]]->mm_width == 0
           && _outputMap[_connectedOutputList[1]]->mm_height == 0 ))) {
        std::cout << "BEAMER CONNECTED!" << std::endl;

        // Get a human readable reference
        if (_outputMap[_connectedOutputList[0]]->mm_width == 0
             && _outputMap[_connectedOutputList[0]]->mm_height == 0 ) {
          _beamer = _connectedOutputList[0];
          _monitor = _connectedOutputList[1];
        } else {
          _beamer = _connectedOutputList[1];
          _monitor = _connectedOutputList[0];

        }

        // Check if the beamer transmitted reliable data.
        bool gotEDID = false;
        int nprop;
        Atom *props = XRRListOutputProperties(_display, _beamer, &nprop);
        for (int i = 0; i < nprop; ++i) {
          char *atom_name = XGetAtomName (_display, props[i]);
          if ( strcmp (atom_name, "EDID") == 0) {
            gotEDID = true;
            break;
          }
        }
        free(props);

        //          // TO BE DONE VIA BASH
//        // Iterate over the modes in the config file
//        for (QList<QString>::const_iterator it =
//             Config::inst()->getModeLines().begin();
//             it != Config::inst()->getModeLines().end(); ++it) {
//          // Add mode to xrandr
//          QProcess p;
//          QStringList arguments;
//          arguments << "--current" << "--newmode"
//                    << it->split(" ", QString::SkipEmptyParts);
//          p.start("xrandr", arguments);
//          p.waitForFinished();
//        }

        // Get the names of the modes and intersect
        QSet<XRRModeInfo*> beamerModes, monitorModes;
        for (int i = 0; i < _outputMap[_beamer]->nmode; ++i)
          beamerModes.insert(_modeMap[_outputMap[_beamer]->modes[i]]);
        for (int i = 0; i < _outputMap[_beamer]->nmode; ++i)
          monitorModes.insert(_modeMap[_outputMap[_monitor]->modes[i]]);

        // Intersect them by the resolution sorted
        // dont care about O(n³)
        QList<XRRModeInfo*> commonModes;
        for (QSet<XRRModeInfo*>::iterator i = beamerModes.begin();
             i != beamerModes.end(); ++i) {
          for (QSet<XRRModeInfo*>::iterator j = monitorModes.begin();
               j != monitorModes.end(); ++j) {
            QList<XRRModeInfo*>::iterator k = commonModes.begin();
            for (;;) {
              if (k == commonModes.end()){
                commonModes.insert(k, *i);
                break;
              }
              if ( (*i)->width == (*k)->width ) {
                if ( (*i)->height == (*k)->height )
                  break;
                if ( (*i)->height > (*k)->height ) {
                  commonModes.insert(k, *i);
                  break;
                }
              }
              if ( (*i)->width > (*k)->width ) {
                commonModes.insert(k, *i);
                break;
              }
              ++k;
            }
          }
        }

        // If the beamer transmits no reliable EDID data add modes
        if (gotEDID) {
          std::cout << "GOT EDID!" << std::endl;
          // Extract the preferred mode of the beamer
          RRMode preferredBeamerModeId;
          for (int i = 0; i < _outputMap[_beamer]->nmode; ++i) {
            if (i < _outputMap[_beamer]->npreferred) {
              preferredBeamerModeId = _outputMap[_beamer]->modes[i];
              break;
            }
          }

          // Compute the aspect ratio of the beamer
          float aspectRatio = (float)_modeMap[preferredBeamerModeId]->width
                     / _modeMap[preferredBeamerModeId]->height;          

          // Fill widget with data
          for ( QList<XRRModeInfo*>::iterator i = commonModes.begin();
                i != commonModes.end(); ++i ) {
            float modeAspectRatio = ((float)(*i)->width / (*i)->height);
            if ( modeAspectRatio == aspectRatio ) // TODO APPROX
              _ui->comboBox->addItem((*i)->name);
          }

//          // TO BE DONE VIA BASH
//          // Iterate over all modes and add them to the outputs,
//          // if the AR matches.
//          for(ModeMap::iterator it = _modeMap.begin();
//              it != _modeMap.end(); ++it) {
//            float MODEAR = ((float)(*it)->width / (*it)->height);
//            if ( MODEAR == AR ) {
//              XRRAddOutputMode(_display, _beamer, (*it)->id);
//              XRRAddOutputMode(_display, _monitor, (*it)->id);
//            }
//          }
        } else {
          std::cout << "NO EDID!" << std::endl;
          // Fill widget with data without AR match
          // Fill widget with data
          for ( QList<XRRModeInfo*>::iterator i = commonModes.begin();
                i != commonModes.end(); ++i ) {
              _ui->comboBox->addItem((*i)->name);
          }
          // TO BE DONE VIA BASH
//          // Iterate over all modes and add them to the outputs
//          for(ModeMap::iterator it = _modeMap.begin();
//              it != _modeMap.end(); ++it) {
//            XRRAddOutputMode(_display, _beamer, (*it)->id);
//            XRRAddOutputMode(_display, _monitor, (*it)->id);
//          }
        }

        // Remove borders and stuff COMMENT FOR DEBUIGGIN
        setWindowFlags(windowFlags() | Qt::FramelessWindowHint);

        // Resize widget to its content
        resize(sizeHint());

        // Center dialog on screenbottom
        const QRect desktopRect = QApplication::desktop()->screenGeometry();
        this->move( desktopRect.width()/2-this->width()/2,
                    desktopRect.height()-this->height());


      } // End of the beamer section
      /*********************************************************************/
      // If NEITHER of the outputs is a beamer (likely dualscreen setup)
      else {
        // TODO(manuel): Furture feature. Setup dualscreen
        qDebug() << "Dual output";
        exit(0);
      }
      break;
    /*************************************************************************/
    default:
      // If there are more than 3 outputs
      // its up to the user. Quit.
      qDebug() << ">2 outputs. Quit.";
      exit(0);
      break;
    }
    /*************************************************************************/
}

//___________________________________________________________________________
void Widget::updateScreenResources()
{
  // Clear the modemap (nothing to be freed, stored in screenResources)
  _modeMap.clear();

  // Create the modemap
  for (int i = 0; i < _screenResources->nmode; ++i) {
    _modeMap.insert(
          _screenResources->modes[i].id,
          &_screenResources->modes[i]);
  }

  // Clear the crtmap
  for (CrtcMap::iterator it = _crtcMap.begin();
       it != _crtcMap.end();) {
    XRRFreeCrtcInfo(*it);
    it = _crtcMap.erase(it);
  }

  // Create crtcMap
  for (int i = 0; i < _screenResources->ncrtc; ++i) {
    XRRCrtcInfo * info = XRRGetCrtcInfo(
                           _display,
                           _screenResources,
                           _screenResources->crtcs[i]);
    _crtcMap.insert(
          _screenResources->crtcs[i],
          info);
  }

  // Clear the outputmap
  for (OutputMap::iterator it = _outputMap.begin();
       it != _outputMap.end();) {
    XRRFreeOutputInfo(*it);
    it = _outputMap.erase(it);
  }

  // Create outputmap and connectedOutputMap
  for (int i = 0; i < _screenResources->noutput; ++i) {
    XRROutputInfo* info = XRRGetOutputInfo(
                           _display,
                           _screenResources,
                           _screenResources->outputs[i]);
    _outputMap.insert(
          _screenResources->outputs[i],
          info);

    // Store the connected ones separate
    if ( info->connection == RR_Connected )
      _connectedOutputList.push_back(_screenResources->outputs[i]);
  }
}

//___________________________________________________________________________
Widget::~Widget()
{
  delete _ui;
  XCloseDisplay(_display);
  XRRFreeScreenResources(_screenResources);
}