2013-03-18 01:06:52 +04:00
#
# Copyright 2006-2009 Red Hat, Inc.
# Jeremy Katz <katzj@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
import os
2013-08-09 04:47:17 +04:00
from virtinst import VirtualDevice
2013-07-14 02:56:09 +04:00
from virtinst . xmlbuilder import XMLProperty
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
def _get_mode_prop ( channel_type ) :
xpath = " ./channel[@name= ' %s ' ]/@mode " % channel_type
2013-09-19 21:27:30 +04:00
return XMLProperty ( xpath )
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-07-16 02:53:53 +04:00
def _validate_port ( name , val ) :
if val is None :
return val
val = int ( val )
if val < 5900 and val != - 1 :
raise ValueError ( _ ( " %s must be above 5900, or "
" -1 for auto allocation " ) % name )
return val
class VirtualGraphics ( VirtualDevice ) :
2013-07-16 17:14:37 +04:00
virtual_device_type = VirtualDevice . VIRTUAL_DEV_GRAPHICS
2013-03-18 01:06:52 +04:00
TYPE_SDL = " sdl "
TYPE_VNC = " vnc "
TYPE_RDP = " rdp "
TYPE_SPICE = " spice "
2013-07-16 02:53:53 +04:00
TYPES = [ TYPE_VNC , TYPE_SDL , TYPE_RDP , TYPE_SPICE ]
2013-03-18 01:06:52 +04:00
CHANNEL_TYPE_MAIN = " main "
CHANNEL_TYPE_DISPLAY = " display "
CHANNEL_TYPE_INPUTS = " inputs "
CHANNEL_TYPE_CURSOR = " cursor "
CHANNEL_TYPE_PLAYBACK = " playback "
CHANNEL_TYPE_RECORD = " record "
2013-07-16 02:53:53 +04:00
CHANNEL_TYPES = [ CHANNEL_TYPE_MAIN , CHANNEL_TYPE_DISPLAY ,
2013-03-18 01:06:52 +04:00
CHANNEL_TYPE_INPUTS , CHANNEL_TYPE_CURSOR ,
CHANNEL_TYPE_PLAYBACK , CHANNEL_TYPE_RECORD ]
CHANNEL_MODE_SECURE = " secure "
CHANNEL_MODE_INSECURE = " insecure "
CHANNEL_MODE_ANY = " any "
2013-07-16 02:53:53 +04:00
CHANNEL_MODES = [ CHANNEL_MODE_SECURE , CHANNEL_MODE_INSECURE ,
2013-03-18 01:06:52 +04:00
CHANNEL_MODE_ANY ]
KEYMAP_LOCAL = " local "
KEYMAP_DEFAULT = " default "
_special_keymaps = [ KEYMAP_LOCAL , KEYMAP_DEFAULT ]
@staticmethod
def valid_keymaps ( ) :
"""
Return a list of valid keymap values .
"""
2013-04-14 00:31:13 +04:00
from virtinst import hostkeymap
2013-03-18 01:06:52 +04:00
2013-04-14 00:31:13 +04:00
orig_list = hostkeymap . keytable . values ( )
2013-03-18 01:06:52 +04:00
sort_list = [ ]
orig_list . sort ( )
for k in orig_list :
if k not in sort_list :
sort_list . append ( k )
return sort_list
@staticmethod
def pretty_type_simple ( gtype ) :
if ( gtype in [ VirtualGraphics . TYPE_VNC ,
VirtualGraphics . TYPE_SDL ,
VirtualGraphics . TYPE_RDP ] ) :
return str ( gtype ) . upper ( )
return str ( gtype ) . capitalize ( )
2013-09-11 19:47:09 +04:00
def __init__ ( self , * args , * * kwargs ) :
VirtualDevice . __init__ ( self , * args , * * kwargs )
2013-03-18 01:06:52 +04:00
2013-04-14 00:14:14 +04:00
self . _local_keymap = - 1
2013-03-18 01:06:52 +04:00
2013-07-16 02:53:53 +04:00
_XML_PROP_ORDER = [ " type " , " port " , " tlsPort " , " autoport " ,
" keymap " , " listen " ,
" passwd " , " display " , " xauth " ]
2013-04-14 00:14:14 +04:00
def _default_keymap ( self , force_local = False ) :
2013-07-16 02:53:53 +04:00
if self . type != " vnc " and self . type != " spice " :
return None
2013-07-06 04:14:57 +04:00
if ( not force_local and
2013-07-06 19:20:28 +04:00
self . conn . check_conn_support (
self . conn . SUPPORT_CONN_KEYMAP_AUTODETECT ) ) :
2013-03-18 01:06:52 +04:00
return None
2013-04-14 00:14:14 +04:00
if self . _local_keymap == - 1 :
2013-04-14 00:31:13 +04:00
from virtinst import hostkeymap
self . _local_keymap = hostkeymap . default_keymap ( )
2013-04-14 00:14:14 +04:00
return self . _local_keymap
2013-03-18 01:06:52 +04:00
2013-07-16 02:53:53 +04:00
def _set_keymap_converter ( self , val ) :
if val == self . KEYMAP_DEFAULT :
2013-03-18 01:06:52 +04:00
return self . _default_keymap ( )
2013-07-16 02:53:53 +04:00
if val == self . KEYMAP_LOCAL :
2013-04-14 00:14:14 +04:00
return self . _default_keymap ( force_local = True )
2013-07-16 02:53:53 +04:00
return val
2013-09-19 21:27:30 +04:00
keymap = XMLProperty ( " ./@keymap " ,
2013-07-16 02:53:53 +04:00
default_cb = _default_keymap ,
set_converter = _set_keymap_converter )
def _get_default_port ( self ) :
if self . type == " vnc " or self . type == " spice " :
return - 1
return None
def _get_default_tlsport ( self ) :
if self . type == " spice " :
return - 1
return None
def _get_default_autoport ( self ) :
# By default, don't do this for VNC to maintain back compat with
# old libvirt that didn't support 'autoport'
2013-09-02 19:54:02 +04:00
if self . type != " spice " :
return None
if ( self . port == - 1 or self . tlsPort == - 1 ) :
return True
2013-07-16 02:53:53 +04:00
return None
2013-09-19 21:27:30 +04:00
port = XMLProperty ( " ./@port " , is_int = True ,
2013-07-16 02:53:53 +04:00
set_converter = lambda s , v : _validate_port ( " Port " , v ) ,
default_cb = _get_default_port )
2013-09-19 21:27:30 +04:00
tlsPort = XMLProperty ( " ./@tlsPort " , is_int = True ,
2013-07-16 02:53:53 +04:00
set_converter = lambda s , v : _validate_port ( " TLS port " , v ) ,
default_cb = _get_default_tlsport )
2013-09-19 21:27:30 +04:00
autoport = XMLProperty ( " ./@autoport " , is_yesno = True ,
2013-07-16 21:04:24 +04:00
default_cb = _get_default_autoport )
2013-03-18 01:06:52 +04:00
channel_main_mode = _get_mode_prop ( CHANNEL_TYPE_MAIN )
channel_display_mode = _get_mode_prop ( CHANNEL_TYPE_DISPLAY )
channel_inputs_mode = _get_mode_prop ( CHANNEL_TYPE_INPUTS )
channel_cursor_mode = _get_mode_prop ( CHANNEL_TYPE_CURSOR )
channel_playback_mode = _get_mode_prop ( CHANNEL_TYPE_PLAYBACK )
channel_record_mode = _get_mode_prop ( CHANNEL_TYPE_RECORD )
2013-07-16 02:53:53 +04:00
def _get_default_display ( self ) :
if self . type != " sdl " :
return None
if " DISPLAY " not in os . environ :
raise RuntimeError ( " No DISPLAY environment variable set. " )
return os . environ [ " DISPLAY " ]
def _get_default_xauth ( self ) :
if self . type != " sdl " :
return None
return os . path . expanduser ( " ~/.Xauthority " )
2013-09-19 21:27:30 +04:00
xauth = XMLProperty ( " ./@xauth " ,
2013-07-16 02:53:53 +04:00
default_cb = _get_default_xauth )
2013-09-19 21:27:30 +04:00
display = XMLProperty ( " ./@display " ,
2013-07-16 02:53:53 +04:00
default_cb = _get_default_display )
2013-09-19 21:27:30 +04:00
type = XMLProperty ( " ./@type " , default_cb = lambda s : " vnc " )
listen = XMLProperty ( " ./@listen " )
passwd = XMLProperty ( " ./@passwd " )
passwdValidTo = XMLProperty ( " ./@passwdValidTo " )
socket = XMLProperty ( " ./@socket " )
2013-07-24 16:46:55 +04:00
VirtualGraphics . register_type ( )