Constantin Kaplinsky | 23c6022 | 2008-06-04 03:58:07 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2006-2008 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 2 | * |
| 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 Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 23 | #include <rfb/LogWriter.h> |
| 24 | #include <x0vncserver/Geometry.h> |
| 25 | |
Peter Åstrand (astrand) | f523ee1 | 2017-10-09 14:59:24 +0200 | [diff] [blame] | 26 | using namespace rfb; |
| 27 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 28 | static LogWriter vlog("Geometry"); |
| 29 | |
| 30 | StringParameter 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 | |
| 37 | Geometry::Geometry(int fullWidth, int fullHeight) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 38 | { |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 39 | recalc(fullWidth, fullHeight); |
| 40 | } |
| 41 | |
| 42 | void 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 Kaplinsky | 6d085f1 | 2008-08-20 09:54:32 +0000 | [diff] [blame] | 48 | // Parse geometry specification and save the result in m_rect. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 49 | const char *param = m_geometryParam.getData(); |
Constantin Kaplinsky | 6d085f1 | 2008-08-20 09:54:32 +0000 | [diff] [blame] | 50 | bool geometrySpecified = (strlen(param) > 0); |
| 51 | if (geometrySpecified) { |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 52 | m_rect = parseString(param); |
| 53 | } |
| 54 | delete[] param; // don't forget to deallocate memory |
| 55 | // allocated by StringParameter::getData() |
Constantin Kaplinsky | 6d085f1 | 2008-08-20 09:54:32 +0000 | [diff] [blame] | 56 | if (m_rect.is_empty()) { |
| 57 | vlog.info("Desktop geometry is invalid"); |
| 58 | return; // further processing does not make sense |
| 59 | } |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 60 | |
Constantin Kaplinsky | 6d085f1 | 2008-08-20 09:54:32 +0000 | [diff] [blame] | 61 | // Everything went good so far. |
| 62 | vlog.info("Desktop geometry is set to %dx%d+%d+%d", |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 63 | width(), height(), offsetLeft(), offsetTop()); |
| 64 | } |
| 65 | |
| 66 | Rect Geometry::parseString(const char *arg) const |
| 67 | { |
| 68 | Rect result; // empty by default |
| 69 | |
| 70 | if (arg != NULL && strlen(arg) > 0) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 71 | int w, h; |
| 72 | int x = 0, y = 0; |
| 73 | char sign_x[2] = "+"; |
| 74 | char sign_y[2] = "+"; |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 75 | int n = sscanf(arg, "%dx%d%1[+-]%d%1[+-]%d", |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 76 | &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 Kaplinsky | 6d085f1 | 2008-08-20 09:54:32 +0000 | [diff] [blame] | 79 | x = m_fullWidth - w - x; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 80 | if (sign_y[0] == '-') |
Constantin Kaplinsky | 6d085f1 | 2008-08-20 09:54:32 +0000 | [diff] [blame] | 81 | y = m_fullHeight - h - y; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 82 | Rect partRect(x, y, x + w, y + h); |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 83 | result = partRect.intersect(m_rect); |
| 84 | if (result.area() <= 0) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 85 | vlog.error("Requested area is out of the desktop boundaries"); |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 86 | result.clear(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 87 | } |
| 88 | } else { |
| 89 | vlog.error("Wrong argument format"); |
| 90 | } |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 91 | } else { |
| 92 | vlog.error("Missing argument"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 93 | } |
Constantin Kaplinsky | 5120e5e | 2008-08-20 06:04:57 +0000 | [diff] [blame] | 94 | |
| 95 | return result; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |