blob: 728c1136df47fdc455bc56ebd9a956a27b89d032 [file] [log] [blame]
Constantin Kaplinsky23c60222008-06-04 03:58:07 +00001/* Copyright (C) 2006-2008 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00002 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19//
20// Geometry.cxx
21//
22
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000023#include <rfb/LogWriter.h>
24#include <x0vncserver/Geometry.h>
25
26static LogWriter vlog("Geometry");
27
28StringParameter Geometry::m_geometryParam("Geometry",
29 "Screen area shown to VNC clients. "
30 "Format is <width>x<height>+<offset_x>+<offset_y>, "
31 "more information in man X, section GEOMETRY SPECIFICATIONS. "
32 "If the argument is empty, full screen is shown to VNC clients.",
33 "");
34
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000035StringParameter Geometry::m_videoAreaParam("VideoArea",
36 "Screen area to be handled as video. "
37 "Format is <width>x<height>+<offset_x>+<offset_y>.",
38 "");
39
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000040Geometry::Geometry(int fullWidth, int fullHeight)
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000041 : m_fullWidth(fullWidth),
42 m_fullHeight(fullHeight),
43 m_rect(0, 0, fullWidth, fullHeight)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000044{
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000045 // Parse geometry specification and save the result in m_rect.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000046 const char *param = m_geometryParam.getData();
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000047 bool geometrySpecified = (strlen(param) > 0);
48 if (geometrySpecified) {
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000049 m_rect = parseString(param);
50 }
51 delete[] param; // don't forget to deallocate memory
52 // allocated by StringParameter::getData()
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000053 if (m_rect.is_empty()) {
54 vlog.info("Desktop geometry is invalid");
55 return; // further processing does not make sense
56 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000057
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000058 // Everything went good so far.
59 vlog.info("Desktop geometry is set to %dx%d+%d+%d",
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000060 width(), height(), offsetLeft(), offsetTop());
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000061
62 // Handle the VideoArea parameter, save the result in m_videoRect.
63 param = m_videoAreaParam.getData();
64 bool videoAreaSpecified = (strlen(param) > 0);
65 if (videoAreaSpecified) {
66 m_videoRect = parseString(param);
67 if (isVideoAreaSet()) {
68 vlog.info("Video area set to %dx%d+%d+%d",
69 m_videoRect.width(), m_videoRect.height(),
70 m_videoRect.tl.x, m_videoRect.tl.y);
71 } else {
72 vlog.info("Video area was not set");
73 }
74 }
75 delete[] param;
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000076}
77
78Rect Geometry::parseString(const char *arg) const
79{
80 Rect result; // empty by default
81
82 if (arg != NULL && strlen(arg) > 0) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000083 int w, h;
84 int x = 0, y = 0;
85 char sign_x[2] = "+";
86 char sign_y[2] = "+";
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000087 int n = sscanf(arg, "%dx%d%1[+-]%d%1[+-]%d",
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000088 &w, &h, sign_x, &x, sign_y, &y);
89 if ((n == 2 || n == 6) && w > 0 && h > 0 && x >= 0 && y >= 0) {
90 if (sign_x[0] == '-')
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000091 x = m_fullWidth - w - x;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000092 if (sign_y[0] == '-')
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000093 y = m_fullHeight - h - y;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000094 Rect partRect(x, y, x + w, y + h);
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000095 result = partRect.intersect(m_rect);
96 if (result.area() <= 0) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000097 vlog.error("Requested area is out of the desktop boundaries");
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000098 result.clear();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000099 }
100 } else {
101 vlog.error("Wrong argument format");
102 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +0000103 } else {
104 vlog.error("Missing argument");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000105 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +0000106
107 return result;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000108}
109