Debian Codegraph @

src/spice-connection.vala

raw
1 /* spice-connection.vala
2 *
3 * Copyright (C) Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Felipe Borges <[email protected]>
19 *
20 */
21
22 using Spice;
23
24 namespace Connections {
25 private class SpiceConnection : Connection {
26 private Spice.Display display;
27 private Spice.Session session;
28 private Spice.MainChannel? _main_channel;
29 public Spice.MainChannel? main_channel {
30 get {
31 return _main_channel;
32 }
33
34 set {
35 _main_channel = value;
36 if (_main_channel == null) return;
37
38 main_event_id = main_channel.channel_event.connect (main_event);
39 }
40 }
41 ulong main_event_id;
42 ulong channel_new_id;
43
44 public override Gtk.Widget widget {
45 set {
46 display = value as Spice.Display;
47 }
48
49 get {
50 return display as Gtk.Widget;
51 }
52 }
53
54 protected override Gdk.Pixbuf? thumbnail {
55 owned get {
56 return display.get_pixbuf();
57 }
58
59 set {
60 return;
61 }
62 }
63
64 public override bool scaling {
65 set {
66 display.scaling = value;
67 }
68
69 get {
70 if (!connected)
71 return true;
72
73 return display.scaling;
74 }
75 }
76
77 public string scale_mode { get; set; default = "fit-window"; }
78 private bool _enable_audio = true;
79 public bool enable_audio {
80 set {
81 _enable_audio = value;
82 session.enable_audio = _enable_audio;
83 }
84
85 get {
86 return _enable_audio;
87 }
88 }
89
90 public override int port { get; protected set; default = 3128; }
91
92 construct {
93 session = new Session();
94 Spice.set_session_option (session);
95 display = new Spice.Display (session, 0);
96 notify["scale-mode"].connect (scale);
97
98 authentication_complete.connect (update_display_authenticated);
99 }
100
101 public SpiceConnection (string uuid) {
102 this.uuid = uuid;
103 }
104
105 public SpiceConnection.from_uri (string uri) {
106 this.uuid = Uuid.string_random ();
107 this.uri = uri;
108 }
109
110 public override void send_keys (uint[] keys) {
111 display.send_keys (keys, Spice.DisplayKeyEvent.CLICK);
112 }
113
114 private void update_display_authenticated () {
115 connect_it ();
116 }
117
118 public override void connect_it () {
119 if (connected) {
120 return;
121 }
122 connected = true;
123
124 main_cleanup ();
125
126 session.host = host;
127 session.port = port.to_string ();
128 session.password = password;
129
130 if (channel_new_id == 0)
131 channel_new_id = session.channel_new.connect (on_channel_new);
132
133 session.connect ();
134 }
135
136 public override void disconnect_it () {
137 if (connected) {
138 var session_object = session as GLib.Object;
139
140 if (channel_new_id > 0) {
141 session_object.disconnect (channel_new_id);
142 channel_new_id = 0;
143 }
144
145 session.disconnect ();
146 connected = false;
147 main_cleanup ();
148 }
149 }
150
151 public override void dispose_display () {}
152
153 ~SpiceConnection () {
154 debug ("Closing connection with %s", widget.name);
155
156 disconnect_it ();
157 }
158
159 private void on_channel_new (Spice.Session session, Spice.Channel channel) {
160 if (channel is Spice.MainChannel) {
161 main_channel = (channel as Spice.MainChannel);
162 }
163
164 }
165
166 private void main_event (ChannelEvent event) {
167 switch (event) {
168 case ChannelEvent.CLOSED:
169 disconnect_it ();
170 break;
171 case ChannelEvent.ERROR_AUTH:
172 need_password = true;
173 handle_auth ();
174 disconnect_it ();
175 break;
176 case ChannelEvent.ERROR_CONNECT:
177 case ChannelEvent.ERROR_IO:
178 case ChannelEvent.ERROR_LINK:
179 case ChannelEvent.ERROR_TLS:
180 debug ("main SPICE channel error: %d", event);
181 disconnect_it ();
182 break;
183 case ChannelEvent.OPENED:
184 show ();
185 break;
186 default:
187 debug ("unhandled main SPICE channel event: %d", event);
188 break;
189 }
190 }
191
192 private void main_cleanup () {
193 if (main_channel == null) return;
194
195 var o = main_channel as Object;
196 o.disconnect(main_event_id);
197 main_event_id = 0;
198 main_channel = null;
199 }
200
201 public void scale () {
202 display.scaling = display.expand = false;
203
204 switch (scale_mode) {
205 case "resize-desktop":
206 resize_desktop_to_window ();
207 break;
208 case "fit-window":
209 scale_to_fit_window ();
210 break;
211 case "original":
212 scale_to_original_size ();
213 break;
214 }
215 }
216
217 private void resize_desktop_to_window () {
218 display.resize_guest = true;
219 }
220
221 private void scale_to_fit_window () {
222 display.scaling = display.hexpand = true;
223 display.width_request = display.height_request = 0;
224 display.resize_guest = false;
225 }
226
227 private void scale_to_original_size () {
228 display.width_request = display.get_allocated_width ();
229 display.height_request = display.get_allocated_height ();
230 display.resize_guest = false;
231 }
232 }
233 }