aboutsummaryrefslogtreecommitdiffstats
path: root/unix/x0vncserver/Geometry.cxx
blob: 37b4e74e49645f360d9129551e15fd211e8cc701 (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
/* Copyright (C) 2006-2008 Constantin Kaplinsky.  All Rights Reserved.
 *    
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

//
// Geometry.cxx
//

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <rfb/LogWriter.h>
#include <x0vncserver/Geometry.h>

using namespace rfb;

static LogWriter vlog("Geometry");

StringParameter Geometry::m_geometryParam("Geometry",
  "Screen area shown to VNC clients. "
  "Format is <width>x<height>+<offset_x>+<offset_y>, "
  "more information in man X, section GEOMETRY SPECIFICATIONS. "
  "If the argument is empty, full screen is shown to VNC clients.",
  "");

Geometry::Geometry(int fullWidth, int fullHeight)
{
  recalc(fullWidth, fullHeight);
}

void Geometry::recalc(int fullWidth, int fullHeight)
{
  m_fullWidth = fullWidth;
  m_fullHeight = fullHeight;
  m_rect.setXYWH(0, 0, fullWidth, fullHeight);

  // Parse geometry specification and save the result in m_rect.
  const char *param = m_geometryParam;
  bool geometrySpecified = (strlen(param) > 0);
  if (geometrySpecified) {
    m_rect = parseString(param);
  }
  if (m_rect.is_empty()) {
    vlog.info("Desktop geometry is invalid");
    return;                     // further processing does not make sense
  }

  // Everything went good so far.
  vlog.info("Desktop geometry is set to %dx%d+%d+%d",
            width(), height(), offsetLeft(), offsetTop());
}

Rect Geometry::parseString(const char *arg) const
{
  Rect result;                  // empty by default

  if (arg != NULL && strlen(arg) > 0) {
    int w, h;
    int x = 0, y = 0;
    char sign_x[2] = "+";
    char sign_y[2] = "+";
    int n = sscanf(arg, "%dx%d%1[+-]%d%1[+-]%d",
                   &w, &h, sign_x, &x, sign_y, &y);
    if ((n == 2 || n == 6) && w > 0 && h > 0 && x >= 0 && y >= 0) {
      if (sign_x[0] == '-')
        x = m_fullWidth - w - x;
      if (sign_y[0] == '-')
        y = m_fullHeight - h - y;
      Rect partRect(x, y, x + w, y + h);
      result = partRect.intersect(m_rect);
      if (result.area() <= 0) {
        vlog.error("Requested area is out of the desktop boundaries");
        result.clear();
      }
    } else {
      vlog.error("Wrong argument format");
    }
  } else {
    vlog.error("Missing argument");
  }

  return result;
}