handle configurable zoom buttons
[presencevnc] / src / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "preferences.h"
3 #include "vncview.h"
4
5 #include <QtMaemo5>
6 #include <QX11Info>
7
8 #include <X11/Xlib.h>
9 #include <X11/Xatom.h>
10
11 #include <iostream>
12
13 MainWindow::MainWindow(QString url, int quality):
14         QMainWindow(0),
15         vnc_view(0),
16         scroll_area(new QScrollArea(0))
17 {
18         setWindowTitle("Presence VNC");
19 //      swipe_start = QPoint(0,0);
20         setAttribute(Qt::WA_Maemo5StackedWindow);
21         QSettings settings;
22
23         //set up toolbar
24         toolbar = new QToolBar(0);
25         toolbar->setAttribute(Qt::WA_InputMethodEnabled, true);
26         toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
27         toolbar->addAction("Tab", this, SLOT(sendTab()));
28         toolbar->addAction("Esc", this, SLOT(sendEsc()));
29         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
30         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
31         toolbar->addAction("IM", toolbar, SLOT(setFocus())); //doesn't work
32         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
33         addToolBar(toolbar);
34         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
35
36         //set up menu
37         QMenuBar *menu = new QMenuBar(this);
38         QAction *connect_action = new QAction("Connect", this);
39         disconnect_action = new QAction("Disconnect", this);
40         menu->addAction(connect_action);
41         menu->addAction(disconnect_action);
42         scaling = new QAction("Rescale Remote Screen", this);
43         scaling->setCheckable(true);
44         scaling->setChecked(settings.value("rescale", true).toBool());
45         menu->addAction(scaling);
46         QAction *show_toolbar = new QAction("Show Toolbar", this);
47         show_toolbar->setCheckable(true);
48         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
49         menu->addAction(show_toolbar);
50         QAction *pref_action = new QAction("Preferences", this);
51         menu->addAction(pref_action);
52         QAction *about_action = new QAction("About", this);
53         menu->addAction(about_action);
54
55         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
56         //menu->hide();
57
58         connect(about_action, SIGNAL(triggered()),
59                 this, SLOT(about()));
60         connect(pref_action, SIGNAL(triggered()),
61                 this, SLOT(showPreferences()));
62         connect(connect_action, SIGNAL(triggered()),
63                 this, SLOT(connectDialog()));
64         connect(disconnect_action, SIGNAL(triggered()),
65                 this, SLOT(disconnectFromHost()));
66         connect(show_toolbar, SIGNAL(toggled(bool)),
67                 toolbar, SLOT(setVisible(bool)));
68         connect(show_toolbar, SIGNAL(toggled(bool)),
69                 this, SLOT(forceResizeDelayed()));
70
71         setCentralWidget(scroll_area);
72
73         grabZoomKeys(true);
74         reloadSettings();
75
76         connect(QApplication::desktop(), SIGNAL(resized(int)),
77                 this, SLOT(forceResize()));
78
79         if(url.isNull()) {
80                 disconnect_action->setEnabled(false);
81                 toolbar->setEnabled(false);
82                 connectDialog();
83         } else {
84                 vnc_view = new VncView(0, url, RemoteView::Quality(quality));
85                 connect(scaling, SIGNAL(toggled(bool)),
86                         vnc_view, SLOT(enableScaling(bool)));
87                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
88                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
89                 scroll_area->setWidget(vnc_view);
90                 vnc_view->start();
91                 vnc_view->enableScaling(scaling->isChecked());
92         }
93 }
94
95 void MainWindow::grabZoomKeys(bool grab)
96 {
97         unsigned long val = (grab)?1:0;
98         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
99         if(!atom) {
100                 qWarning("Couldn't get zoom key atom");
101                 return;
102         }
103         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
104                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
105 }
106
107 void MainWindow::closeEvent(QCloseEvent*) {
108         grabZoomKeys(false);
109
110         QSettings settings;
111         settings.setValue("show_toolbar", toolbar->isVisible());
112         settings.setValue("rescale", scaling->isChecked());
113         settings.sync();
114
115         hide();
116
117         disconnectFromHost();
118 }
119
120 void MainWindow::about() {
121         QMessageBox::about(this, tr("About Presence VNC"),
122                 tr("<center><h1>Presence VNC 0.1 beta</h1>\
123 A touch screen friendly VNC client\
124 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
125 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
126 <p>This program is free software; License: <a href=\"http://www.gnu.org/licenses/gpl-2.0.html\">GNU GPL 2</a> or later.</p>"));
127 }
128
129 /* swipe not used, and doesn't work without scaling anyway :/
130 virtual bool event(QEvent *event) {
131         if(event->type() == QEvent::MouseMove) {
132                 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
133                 if(!swipe_start.isNull()) {
134                         QPoint diff = swipe_start - ev->pos();
135                         const int swipe_dist = 60;
136                         if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
137                                 menu->show();
138                                 swipe_start = QPoint(0,0);
139                         }
140                 } else if((width() - ev->x()) < 10) {
141                         swipe_start = ev->pos();
142                 }
143                 std::cout << "mousex: " << width() - ev->x() << "\n";
144                 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
145                 return true;
146         } else if(event->type() == QEvent::MouseButtonRelease) {
147                 swipe_start = QPoint(0,0);
148                 return true;
149         } else {
150 //                      std::cout << "event " << event->type() << "\n";
151                 return QScrollArea::event(event);
152         }
153 }
154 */
155
156 void MainWindow::connectDialog()
157 {
158         QSettings settings;
159         QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
160         if(url.isEmpty()) { //dialog dismissed or nothing entered
161                 return;
162         }
163         settings.setValue("last_hostname", url);
164         url = "vnc://" + url;
165
166         disconnectFromHost();
167
168         vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
169         scroll_area->setWidget(vnc_view);
170
171         connect(scaling, SIGNAL(toggled(bool)),
172                 vnc_view, SLOT(enableScaling(bool)));
173         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
174                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
175         vnc_view->start();
176         vnc_view->enableScaling(scaling->isChecked());
177         disconnect_action->setEnabled(true);
178         toolbar->setEnabled(true);
179 }
180
181 void MainWindow::disconnectFromHost()
182 {
183         if(!vnc_view)
184                 return;
185
186         vnc_view->startQuitting();
187         scroll_area->setWidget(0);
188
189         vnc_view->disconnect(); //remove all connections
190         delete vnc_view;
191         vnc_view = 0;
192         disconnect_action->setEnabled(false);
193         toolbar->setEnabled(false);
194 }
195
196 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
197 {
198         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
199
200         switch(status) {
201         case RemoteView::Connecting:
202                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
203                 break;
204         case RemoteView::Connected:
205                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
206                 break;
207         case RemoteView::Disconnecting:
208                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
209                         QMaemo5InformationBox::information(this, "Connection lost");
210                 }
211                 break;
212         case RemoteView::Disconnected:
213                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
214                 if(old_status == RemoteView::Disconnecting) {
215                         scroll_area->setWidget(0); //remove widget
216                 }
217                 break;
218         }
219
220         old_status = status;
221 }
222
223 //when rescaling is enabled, this resizes the widget to use available screen space
224 //necessary when rotating, showing fullscreen, etc.
225 void MainWindow::forceResize()
226 {
227         if(vnc_view and scaling->isChecked()) {
228                 vnc_view->resize(scroll_area->size());
229         }
230
231
232 void MainWindow::forceResizeDelayed()
233 {
234         QTimer::singleShot(500, this, SLOT(forceResize()));
235 }
236
237 void MainWindow::toggleFullscreen()
238 {
239         setWindowState(windowState() ^ Qt::WindowFullScreen); 
240         forceResizeDelayed();
241 }
242
243 void MainWindow::showModifierMenu()
244 {
245         static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
246         static QAction *win = mod_menu->addAction(tr("Win"));
247         static QAction *alt = mod_menu->addAction(tr("Alt"));
248         win->setCheckable(true);
249         alt->setCheckable(true);
250
251         //show menu at top-left corner of toolbar
252         QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
253         if(!chosen) {
254                 return;
255         } else if(chosen == alt) {
256                 vnc_view->sendKey(Qt::Key_Alt);
257         } else if(chosen == win) {
258                 vnc_view->sendKey(Qt::Key_Meta);
259         } else {
260                 std::cout << "unhandled action?\n";
261         }
262 }
263
264 void MainWindow::showPreferences()
265 {
266         Preferences *p = new Preferences(this);
267         p->exec();
268         delete p;
269
270         reloadSettings();
271 }
272
273 void MainWindow::reloadSettings()
274 {
275         QSettings settings;
276         int rotation = settings.value("screen_rotation", 0).toInt();
277         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
278         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
279         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
280
281         if(vnc_view)
282                 vnc_view->reloadSettings();
283 }