2011-11-24 15:34:52 +00:00
/*
* Copyright © 2011 Red Hat , Inc .
*
* 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 . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*
* Authors : Peter Hutterer < peter . hutterer @ redhat . com >
* Bastien Nocera < hadess @ hadess . net >
*
*/
# include <config.h>
# include "cc-wacom-page.h"
2011-11-28 18:23:28 +00:00
# include "cc-wacom-nav-button.h"
2012-01-16 17:18:32 +00:00
# include "cc-wacom-stylus-page.h"
2012-01-09 16:01:14 -08:00
# include "gui_gtk.h"
2011-11-24 15:34:52 +00:00
# include <gtk/gtk.h>
# include <string.h>
# define WID(x) (GtkWidget *) gtk_builder_get_object (priv->builder, x)
G_DEFINE_TYPE ( CcWacomPage , cc_wacom_page , GTK_TYPE_BOX )
# define WACOM_PAGE_PRIVATE(o) \
( G_TYPE_INSTANCE_GET_PRIVATE ( ( o ) , CC_TYPE_WACOM_PAGE , CcWacomPagePrivate ) )
2012-01-13 11:51:27 +00:00
# define THRESHOLD_MISCLICK 15
# define THRESHOLD_DOUBLECLICK 7
2011-11-24 15:34:52 +00:00
struct _CcWacomPagePrivate
{
2012-01-26 17:49:56 +00:00
CcWacomPanel * panel ;
2011-11-25 16:43:55 +00:00
GsdWacomDevice * stylus , * eraser ;
2011-11-24 18:58:29 +00:00
GtkBuilder * builder ;
2011-11-28 18:23:28 +00:00
GtkWidget * nav ;
2012-01-16 17:18:32 +00:00
GtkWidget * notebook ;
2012-01-12 18:57:55 +00:00
CalibArea * area ;
2011-11-24 18:58:29 +00:00
GSettings * wacom_settings ;
2011-11-24 15:34:52 +00:00
/* The UI doesn't support cursor/pad at the moment */
} ;
/* Button combo box storage columns */
enum {
BUTTONNUMBER_COLUMN ,
BUTTONNAME_COLUMN ,
N_BUTTONCOLUMNS
} ;
/* Tablet mode combo box storage columns */
enum {
MODENUMBER_COLUMN ,
MODELABEL_COLUMN ,
N_MODECOLUMNS
} ;
/* Tablet mode options - keep in sync with .ui */
enum {
MODE_ABSOLUTE , /* stylus + eraser absolute */
MODE_RELATIVE , /* stylus + eraser relative */
} ;
2012-01-09 16:01:14 -08:00
static void
set_calibration ( gint * cal ,
gsize ncal ,
GSettings * settings )
{
GVariant * current ; /* current calibration */
GVariant * array ; /* new calibration */
GVariant * * tmp ;
gsize nvalues ;
int i ;
current = g_settings_get_value ( settings , " area " ) ;
2012-01-10 19:44:43 +00:00
g_variant_get_fixed_array ( current , & nvalues , sizeof ( gint32 ) ) ;
2012-01-12 18:57:55 +00:00
if ( ( ncal ! = 4 ) | | ( nvalues ! = 4 ) ) {
2012-01-10 19:45:02 +00:00
g_warning ( " Unable set set device calibration property. Got % " G_GSIZE_FORMAT " items to put in % " G_GSIZE_FORMAT " slots; expected %d items. \n " , ncal , nvalues , 4 ) ;
2012-01-09 16:01:14 -08:00
return ;
}
tmp = g_malloc ( nvalues * sizeof ( GVariant * ) ) ;
for ( i = 0 ; i < ncal ; i + + )
tmp [ i ] = g_variant_new_int32 ( cal [ i ] ) ;
array = g_variant_new_array ( G_VARIANT_TYPE_INT32 , tmp , nvalues ) ;
g_settings_set_value ( settings , " area " , array ) ;
g_free ( tmp ) ;
g_variant_unref ( array ) ;
}
2012-01-12 18:57:55 +00:00
static void
finish_calibration ( CalibArea * area ,
gpointer user_data )
2012-01-09 16:01:14 -08:00
{
2012-01-12 18:57:55 +00:00
CcWacomPage * page = ( CcWacomPage * ) user_data ;
CcWacomPagePrivate * priv = page - > priv ;
2012-01-10 19:19:24 +00:00
XYinfo axis ;
2012-01-10 18:33:26 +00:00
gboolean swap_xy ;
2012-01-12 18:57:55 +00:00
int cal [ 4 ] ;
2012-01-09 16:01:14 -08:00
2012-01-12 18:57:55 +00:00
if ( calib_area_finish ( area , & axis , & swap_xy ) ) {
2012-01-10 19:19:24 +00:00
cal [ 0 ] = axis . x_min ;
cal [ 1 ] = axis . y_min ;
cal [ 2 ] = axis . x_max ;
cal [ 3 ] = axis . y_max ;
2012-01-12 18:57:55 +00:00
set_calibration ( cal , 4 , page - > priv - > wacom_settings ) ;
2012-01-09 16:01:14 -08:00
}
2012-01-12 18:57:55 +00:00
calib_area_free ( area ) ;
page - > priv - > area = NULL ;
gtk_widget_set_sensitive ( WID ( " button-calibrate " ) , TRUE ) ;
}
static gboolean
run_calibration ( CcWacomPage * page ,
2012-01-13 13:30:03 -08:00
gint * cal ,
gint monitor )
2012-01-12 18:57:55 +00:00
{
XYinfo old_axis ;
g_assert ( page - > priv - > area = = NULL ) ;
old_axis . x_min = cal [ 0 ] ;
old_axis . y_min = cal [ 1 ] ;
old_axis . x_max = cal [ 2 ] ;
old_axis . y_max = cal [ 3 ] ;
page - > priv - > area = calib_area_new ( NULL ,
2012-01-13 13:30:03 -08:00
monitor ,
2012-01-12 18:57:55 +00:00
finish_calibration ,
page ,
& old_axis ,
2012-01-13 11:51:27 +00:00
THRESHOLD_MISCLICK ,
THRESHOLD_DOUBLECLICK ) ;
2012-01-12 18:57:55 +00:00
2012-01-10 20:16:33 +00:00
return FALSE ;
2012-01-09 16:01:14 -08:00
}
static void
2012-01-12 15:22:04 +00:00
calibrate_button_clicked_cb ( GtkButton * button ,
CcWacomPage * page )
2012-01-09 16:01:14 -08:00
{
2012-01-12 15:23:53 +00:00
int i , calibration [ 4 ] ;
2012-01-12 15:26:16 +00:00
GVariant * variant ;
2012-01-12 15:23:53 +00:00
int * current ;
2012-01-12 15:26:16 +00:00
gsize ncal ;
2012-01-12 13:02:22 -08:00
gint monitor ;
monitor = gsd_wacom_device_get_display_monitor ( page - > priv - > stylus ) ;
if ( monitor < 0 ) {
/* The display the tablet should be mapped to could not be located.
* This shouldn ' t happen if the EDID data is good . . .
*/
g_critical ( " Output associated with the tablet is not connected. Unable to calibrate. " ) ;
return ;
}
2012-01-12 15:26:16 +00:00
variant = g_settings_get_value ( page - > priv - > wacom_settings , " area " ) ;
current = ( int * ) g_variant_get_fixed_array ( variant , & ncal , sizeof ( gint32 ) ) ;
2012-01-12 15:23:53 +00:00
2012-01-12 15:26:16 +00:00
if ( ncal ! = 4 ) {
g_warning ( " Device calibration property has wrong length. Got % " G_GSIZE_FORMAT " items; expected %d. \n " , ncal , 4 ) ;
2012-01-12 15:23:53 +00:00
g_free ( current ) ;
return ;
}
2012-01-09 16:01:14 -08:00
2012-01-12 15:23:53 +00:00
for ( i = 0 ; i < 4 ; i + + )
calibration [ i ] = current [ i ] ;
if ( calibration [ 0 ] = = - 1 & &
calibration [ 1 ] = = - 1 & &
calibration [ 2 ] = = - 1 & &
calibration [ 3 ] = = - 1 ) {
gint * device_cal ;
2012-01-12 15:26:16 +00:00
device_cal = gsd_wacom_device_get_area ( page - > priv - > stylus ) ;
2012-01-12 15:23:53 +00:00
for ( i = 0 ; i < 4 ; i + + )
calibration [ i ] = device_cal [ i ] ;
g_free ( device_cal ) ;
}
2012-01-13 13:30:03 -08:00
run_calibration ( page , calibration , monitor ) ;
2012-01-12 18:57:55 +00:00
gtk_widget_set_sensitive ( GTK_WIDGET ( button ) , FALSE ) ;
2012-01-12 15:23:53 +00:00
}
2012-01-09 16:01:14 -08:00
2011-11-24 15:34:52 +00:00
static void
tabletmode_changed_cb ( GtkComboBox * combo , gpointer user_data )
{
CcWacomPagePrivate * priv = CC_WACOM_PAGE ( user_data ) - > priv ;
GtkListStore * liststore ;
GtkTreeIter iter ;
gint mode ;
gboolean is_absolute ;
if ( ! gtk_combo_box_get_active_iter ( combo , & iter ) )
return ;
liststore = GTK_LIST_STORE ( WID ( " liststore-tabletmode " ) ) ;
gtk_tree_model_get ( GTK_TREE_MODEL ( liststore ) , & iter ,
MODENUMBER_COLUMN , & mode ,
- 1 ) ;
is_absolute = ( mode = = MODE_ABSOLUTE ) ;
g_settings_set_boolean ( priv - > wacom_settings , " is-absolute " , is_absolute ) ;
}
static void
left_handed_toggled_cb ( GtkSwitch * sw , GParamSpec * pspec , gpointer * user_data )
{
CcWacomPagePrivate * priv = CC_WACOM_PAGE ( user_data ) - > priv ;
const gchar * rotation ;
rotation = gtk_switch_get_active ( sw ) ? " half " : " none " ;
g_settings_set_string ( priv - > wacom_settings , " rotation " , rotation ) ;
}
static void
set_left_handed_from_gsettings ( CcWacomPage * page )
{
CcWacomPagePrivate * priv = CC_WACOM_PAGE ( page ) - > priv ;
const gchar * rotation ;
rotation = g_settings_get_string ( priv - > wacom_settings , " rotation " ) ;
if ( strcmp ( rotation , " half " ) = = 0 )
gtk_switch_set_active ( GTK_SWITCH ( WID ( " switch-left-handed " ) ) , TRUE ) ;
}
static void
set_mode_from_gsettings ( GtkComboBox * combo , CcWacomPage * page )
{
CcWacomPagePrivate * priv = page - > priv ;
gboolean is_absolute ;
is_absolute = g_settings_get_boolean ( priv - > wacom_settings , " is-absolute " ) ;
/* this must be kept in sync with the .ui file */
gtk_combo_box_set_active ( combo , is_absolute ? MODE_ABSOLUTE : MODE_RELATIVE ) ;
}
static void
combobox_text_cellrenderer ( GtkComboBox * combo , int name_column )
{
GtkCellRenderer * renderer ;
renderer = gtk_cell_renderer_text_new ( ) ;
gtk_cell_layout_pack_start ( GTK_CELL_LAYOUT ( combo ) , renderer , TRUE ) ;
gtk_cell_layout_set_attributes ( GTK_CELL_LAYOUT ( combo ) , renderer ,
" text " , BUTTONNAME_COLUMN , NULL ) ;
}
2012-01-26 17:49:56 +00:00
static gboolean
display_clicked_cb ( GtkButton * button ,
CcWacomPage * page )
{
cc_wacom_panel_switch_to_panel ( page - > priv - > panel , " display " ) ;
return TRUE ;
}
2011-11-24 15:34:52 +00:00
/* Boilerplate code goes below */
static void
cc_wacom_page_get_property ( GObject * object ,
guint property_id ,
GValue * value ,
GParamSpec * pspec )
{
switch ( property_id )
{
default :
G_OBJECT_WARN_INVALID_PROPERTY_ID ( object , property_id , pspec ) ;
}
}
static void
cc_wacom_page_set_property ( GObject * object ,
guint property_id ,
const GValue * value ,
GParamSpec * pspec )
{
switch ( property_id )
{
default :
G_OBJECT_WARN_INVALID_PROPERTY_ID ( object , property_id , pspec ) ;
}
}
static void
cc_wacom_page_dispose ( GObject * object )
{
CcWacomPagePrivate * priv = CC_WACOM_PAGE ( object ) - > priv ;
2012-01-12 18:57:55 +00:00
if ( priv - > area ) {
calib_area_free ( priv - > area ) ;
priv - > area = NULL ;
}
if ( priv - > builder ) {
2011-11-24 15:34:52 +00:00
g_object_unref ( priv - > builder ) ;
priv - > builder = NULL ;
}
2012-01-12 18:57:55 +00:00
2011-11-24 15:34:52 +00:00
G_OBJECT_CLASS ( cc_wacom_page_parent_class ) - > dispose ( object ) ;
}
static void
cc_wacom_page_class_init ( CcWacomPageClass * klass )
{
GObjectClass * object_class = G_OBJECT_CLASS ( klass ) ;
g_type_class_add_private ( klass , sizeof ( CcWacomPagePrivate ) ) ;
object_class - > get_property = cc_wacom_page_get_property ;
object_class - > set_property = cc_wacom_page_set_property ;
object_class - > dispose = cc_wacom_page_dispose ;
}
static void
cc_wacom_page_init ( CcWacomPage * self )
{
CcWacomPagePrivate * priv ;
GError * error = NULL ;
GtkComboBox * combo ;
GtkWidget * box ;
GtkSwitch * sw ;
char * objects [ ] = {
" main-grid " ,
" liststore-tabletmode " ,
" liststore-buttons " ,
" adjustment-tip-feel " ,
" adjustment-eraser-feel " ,
NULL
} ;
priv = self - > priv = WACOM_PAGE_PRIVATE ( self ) ;
priv - > builder = gtk_builder_new ( ) ;
gtk_builder_add_objects_from_file ( priv - > builder ,
GNOMECC_UI_DIR " /gnome-wacom-properties.ui " ,
objects ,
& error ) ;
if ( error ! = NULL ) {
g_warning ( " Error loading UI file: %s " , error - > message ) ;
g_object_unref ( priv - > builder ) ;
g_error_free ( error ) ;
return ;
}
box = WID ( " main-grid " ) ;
gtk_container_add ( GTK_CONTAINER ( self ) , box ) ;
gtk_widget_set_vexpand ( GTK_WIDGET ( box ) , TRUE ) ;
2012-01-16 17:18:32 +00:00
self - > priv - > notebook = WID ( " stylus-notebook " ) ;
2012-01-09 16:01:14 -08:00
g_signal_connect ( WID ( " button-calibrate " ) , " clicked " ,
G_CALLBACK ( calibrate_button_clicked_cb ) , self ) ;
2011-11-24 15:34:52 +00:00
combo = GTK_COMBO_BOX ( WID ( " combo-tabletmode " ) ) ;
combobox_text_cellrenderer ( combo , MODELABEL_COLUMN ) ;
g_signal_connect ( G_OBJECT ( combo ) , " changed " ,
G_CALLBACK ( tabletmode_changed_cb ) , self ) ;
sw = GTK_SWITCH ( WID ( " switch-left-handed " ) ) ;
g_signal_connect ( G_OBJECT ( sw ) , " notify::active " ,
G_CALLBACK ( left_handed_toggled_cb ) , self ) ;
2012-01-26 17:49:56 +00:00
g_signal_connect ( G_OBJECT ( WID ( " display-link " ) ) , " activate-link " ,
G_CALLBACK ( display_clicked_cb ) , self ) ;
2011-11-28 18:23:28 +00:00
priv - > nav = cc_wacom_nav_button_new ( ) ;
gtk_grid_attach ( GTK_GRID ( box ) , priv - > nav , 0 , 0 , 1 , 1 ) ;
2011-11-24 15:34:52 +00:00
}
2011-11-25 15:05:34 +00:00
static void
set_icon_name ( CcWacomPage * page ,
const char * widget_name ,
const char * icon_name )
{
CcWacomPagePrivate * priv ;
char * filename , * path ;
priv = page - > priv ;
filename = g_strdup_printf ( " %s.svg " , icon_name ) ;
path = g_build_filename ( PIXMAP_DIR , filename , NULL ) ;
g_free ( filename ) ;
gtk_image_set_from_file ( GTK_IMAGE ( WID ( widget_name ) ) , path ) ;
g_free ( path ) ;
}
2012-01-16 17:18:32 +00:00
typedef struct {
GsdWacomStylus * stylus ;
GsdWacomStylus * eraser ;
} StylusPair ;
2011-11-25 15:05:34 +00:00
static void
2012-01-16 17:18:32 +00:00
add_styli ( CcWacomPage * page )
2011-11-25 15:05:34 +00:00
{
2012-01-16 17:18:32 +00:00
GList * styli , * l ;
CcWacomPagePrivate * priv ;
priv = page - > priv ;
styli = gsd_wacom_device_list_styli ( priv - > stylus ) ;
2011-11-25 15:05:34 +00:00
2012-01-16 17:18:32 +00:00
for ( l = styli ; l ; l = l - > next ) {
GsdWacomStylus * stylus , * eraser ;
GtkWidget * page ;
stylus = l - > data ;
if ( gsd_wacom_stylus_get_has_eraser ( stylus ) ) {
GsdWacomDeviceType type ;
type = gsd_wacom_stylus_get_stylus_type ( stylus ) ;
eraser = gsd_wacom_device_get_stylus_for_type ( priv - > eraser , type ) ;
} else {
eraser = NULL ;
}
page = cc_wacom_stylus_page_new ( stylus , eraser ) ;
cc_wacom_stylus_page_set_navigation ( CC_WACOM_STYLUS_PAGE ( page ) , GTK_NOTEBOOK ( priv - > notebook ) ) ;
gtk_widget_show ( page ) ;
gtk_notebook_append_page ( GTK_NOTEBOOK ( priv - > notebook ) , page , NULL ) ;
}
2011-11-25 15:05:34 +00:00
g_list_free ( styli ) ;
2012-01-16 17:18:32 +00:00
/*FIXME: Set the page with the last used item */
2011-11-25 15:05:34 +00:00
}
2011-11-24 15:34:52 +00:00
GtkWidget *
2012-01-26 17:49:56 +00:00
cc_wacom_page_new ( CcWacomPanel * panel ,
GsdWacomDevice * stylus ,
2011-11-24 18:58:29 +00:00
GsdWacomDevice * eraser )
2011-11-24 15:34:52 +00:00
{
CcWacomPage * page ;
CcWacomPagePrivate * priv ;
2011-11-24 18:58:29 +00:00
g_return_val_if_fail ( GSD_IS_WACOM_DEVICE ( stylus ) , NULL ) ;
g_return_val_if_fail ( gsd_wacom_device_get_device_type ( stylus ) = = WACOM_TYPE_STYLUS , NULL ) ;
g_return_val_if_fail ( GSD_IS_WACOM_DEVICE ( eraser ) , NULL ) ;
g_return_val_if_fail ( gsd_wacom_device_get_device_type ( eraser ) = = WACOM_TYPE_ERASER , NULL ) ;
2011-11-24 15:34:52 +00:00
page = g_object_new ( CC_TYPE_WACOM_PAGE , NULL ) ;
priv = page - > priv ;
2012-01-26 17:49:56 +00:00
priv - > panel = panel ;
2011-11-24 18:58:29 +00:00
priv - > stylus = stylus ;
priv - > eraser = eraser ;
2011-11-24 15:34:52 +00:00
/* FIXME move this to construct */
2011-11-25 16:43:55 +00:00
priv - > wacom_settings = gsd_wacom_device_get_settings ( stylus ) ;
2011-11-24 15:34:52 +00:00
set_mode_from_gsettings ( GTK_COMBO_BOX ( WID ( " combo-tabletmode " ) ) , page ) ;
2011-11-24 19:12:14 +00:00
2011-11-25 15:05:34 +00:00
/* Tablet name */
2011-11-25 16:43:55 +00:00
gtk_label_set_text ( GTK_LABEL ( WID ( " label-tabletmodel " ) ) , gsd_wacom_device_get_name ( stylus ) ) ;
2011-11-25 15:05:34 +00:00
/* Left-handedness */
2011-11-25 16:43:55 +00:00
if ( gsd_wacom_device_reversible ( stylus ) = = FALSE ) {
2011-11-24 19:12:14 +00:00
gtk_widget_hide ( WID ( " label-left-handed " ) ) ;
gtk_widget_hide ( WID ( " switch-left-handed " ) ) ;
} else {
set_left_handed_from_gsettings ( page ) ;
}
2011-11-24 15:34:52 +00:00
2011-12-08 13:25:32 -08:00
/* Calibration for screen tablets */
2012-01-26 16:09:35 +00:00
if ( gsd_wacom_device_is_screen_tablet ( stylus ) ! = FALSE ) {
2011-12-08 13:25:32 -08:00
gtk_widget_show ( WID ( " button-calibrate " ) ) ;
2012-01-26 16:09:35 +00:00
gtk_widget_hide ( WID ( " combo-tabletmode " ) ) ;
gtk_widget_hide ( WID ( " label-trackingmode " ) ) ;
2012-01-26 17:49:56 +00:00
gtk_widget_show ( WID ( " display-link " ) ) ;
2011-12-08 13:25:32 -08:00
}
2011-11-25 15:05:34 +00:00
/* Tablet icon */
2011-11-25 16:43:55 +00:00
set_icon_name ( page , " image-tablet " , gsd_wacom_device_get_icon_name ( stylus ) ) ;
2011-11-25 15:05:34 +00:00
2012-01-16 17:18:32 +00:00
/* Add styli */
add_styli ( page ) ;
2011-11-24 19:07:41 +00:00
2011-11-24 15:34:52 +00:00
return GTK_WIDGET ( page ) ;
}
2011-11-28 18:23:28 +00:00
void
cc_wacom_page_set_navigation ( CcWacomPage * page ,
GtkNotebook * notebook ,
gboolean ignore_first_page )
{
CcWacomPagePrivate * priv ;
g_return_if_fail ( CC_IS_WACOM_PAGE ( page ) ) ;
priv = page - > priv ;
g_object_set ( G_OBJECT ( priv - > nav ) ,
" notebook " , notebook ,
" ignore-first " , ignore_first_page ,
NULL ) ;
}