blob: 48c1842603c5da35ac553119d1c416f5ef4a4107 [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
Peter Åstrand (astrand)f523ee12017-10-09 14:59:24 +020026using namespace rfb;
27
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000028static LogWriter vlog("Geometry");
29
30StringParameter Geometry::m_geometryParam("Geometry",
31 "Screen area shown to VNC clients. "
32 "Format is <width>x<height>+<offset_x>+<offset_y>, "
33 "more information in man X, section GEOMETRY SPECIFICATIONS. "
34 "If the argument is empty, full screen is shown to VNC clients.",
35 "");
36
37Geometry::Geometry(int fullWidth, int fullHeight)
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000038 : m_fullWidth(fullWidth),
39 m_fullHeight(fullHeight),
40 m_rect(0, 0, fullWidth, fullHeight)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000041{
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000042 // Parse geometry specification and save the result in m_rect.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000043 const char *param = m_geometryParam.getData();
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000044 bool geometrySpecified = (strlen(param) > 0);
45 if (geometrySpecified) {
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000046 m_rect = parseString(param);
47 }
48 delete[] param; // don't forget to deallocate memory
49 // allocated by StringParameter::getData()
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000050 if (m_rect.is_empty()) {
51 vlog.info("Desktop geometry is invalid");
52 return; // further processing does not make sense
53 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000054
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000055 // Everything went good so far.
56 vlog.info("Desktop geometry is set to %dx%d+%d+%d",
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000057 width(), height(), offsetLeft(), offsetTop());
58}
59
60Rect Geometry::parseString(const char *arg) const
61{
62 Rect result; // empty by default
63
64 if (arg != NULL && strlen(arg) > 0) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000065 int w, h;
66 int x = 0, y = 0;
67 char sign_x[2] = "+";
68 char sign_y[2] = "+";
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000069 int n = sscanf(arg, "%dx%d%1[+-]%d%1[+-]%d",
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000070 &w, &h, sign_x, &x, sign_y, &y);
71 if ((n == 2 || n == 6) && w > 0 && h > 0 && x >= 0 && y >= 0) {
72 if (sign_x[0] == '-')
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000073 x = m_fullWidth - w - x;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000074 if (sign_y[0] == '-')
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000075 y = m_fullHeight - h - y;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000076 Rect partRect(x, y, x + w, y + h);
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000077 result = partRect.intersect(m_rect);
78 if (result.area() <= 0) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000079 vlog.error("Requested area is out of the desktop boundaries");
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000080 result.clear();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000081 }
82 } else {
83 vlog.error("Wrong argument format");
84 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000085 } else {
86 vlog.error("Missing argument");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000087 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000088
89 return result;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000090}
91