blob: d911471333a46f2c99d9f1ece1e1c10b9bafefda [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 Kaplinskyb30ae7f2006-05-25 05:04:46 +000038{
Peter Åstrand (astrand)242c5b22018-03-07 13:00:47 +010039 recalc(fullWidth, fullHeight);
40}
41
42void Geometry::recalc(int fullWidth, int fullHeight)
43{
44 m_fullWidth = fullWidth;
45 m_fullHeight = fullHeight;
46 m_rect.setXYWH(0, 0, fullWidth, fullHeight);
47
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000048 // Parse geometry specification and save the result in m_rect.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000049 const char *param = m_geometryParam.getData();
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000050 bool geometrySpecified = (strlen(param) > 0);
51 if (geometrySpecified) {
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000052 m_rect = parseString(param);
53 }
54 delete[] param; // don't forget to deallocate memory
55 // allocated by StringParameter::getData()
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000056 if (m_rect.is_empty()) {
57 vlog.info("Desktop geometry is invalid");
58 return; // further processing does not make sense
59 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000060
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000061 // Everything went good so far.
62 vlog.info("Desktop geometry is set to %dx%d+%d+%d",
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000063 width(), height(), offsetLeft(), offsetTop());
64}
65
66Rect Geometry::parseString(const char *arg) const
67{
68 Rect result; // empty by default
69
70 if (arg != NULL && strlen(arg) > 0) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000071 int w, h;
72 int x = 0, y = 0;
73 char sign_x[2] = "+";
74 char sign_y[2] = "+";
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000075 int n = sscanf(arg, "%dx%d%1[+-]%d%1[+-]%d",
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000076 &w, &h, sign_x, &x, sign_y, &y);
77 if ((n == 2 || n == 6) && w > 0 && h > 0 && x >= 0 && y >= 0) {
78 if (sign_x[0] == '-')
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000079 x = m_fullWidth - w - x;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000080 if (sign_y[0] == '-')
Constantin Kaplinsky6d085f12008-08-20 09:54:32 +000081 y = m_fullHeight - h - y;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000082 Rect partRect(x, y, x + w, y + h);
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000083 result = partRect.intersect(m_rect);
84 if (result.area() <= 0) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000085 vlog.error("Requested area is out of the desktop boundaries");
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000086 result.clear();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000087 }
88 } else {
89 vlog.error("Wrong argument format");
90 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000091 } else {
92 vlog.error("Missing argument");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000093 }
Constantin Kaplinsky5120e5e2008-08-20 06:04:57 +000094
95 return result;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000096}
97