2004-06-16 19:11:32 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpcontrollerinfo.c
|
|
|
|
* Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2004-06-17 02:14:44 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2004-06-16 19:11:32 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "libgimpwidgets/gimpwidgets.h"
|
2004-06-17 02:14:44 +08:00
|
|
|
#include "libgimpwidgets/gimpcontroller.h"
|
2004-06-16 19:11:32 +08:00
|
|
|
|
|
|
|
#include "widgets-types.h"
|
|
|
|
|
2004-06-17 02:14:44 +08:00
|
|
|
#include "config/gimpconfig.h"
|
|
|
|
#include "config/gimpconfig-params.h"
|
|
|
|
#include "config/gimpconfigwriter.h"
|
|
|
|
#include "config/gimpscanner.h"
|
|
|
|
|
2004-06-18 00:32:30 +08:00
|
|
|
#include "core/gimp-utils.h"
|
2004-06-17 02:14:44 +08:00
|
|
|
#include "core/gimpmarshal.h"
|
|
|
|
|
2004-06-16 19:11:32 +08:00
|
|
|
#include "gimpcontrollerinfo.h"
|
|
|
|
|
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
|
|
|
|
2004-06-17 02:14:44 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2004-06-17 22:07:05 +08:00
|
|
|
PROP_ENABLED,
|
2004-06-17 02:14:44 +08:00
|
|
|
PROP_CONTROLLER,
|
|
|
|
PROP_MAPPING
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
EVENT_MAPPED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void gimp_controller_info_class_init (GimpControllerInfoClass *klass);
|
|
|
|
static void gimp_controller_info_init (GimpControllerInfo *info);
|
|
|
|
|
|
|
|
static void gimp_controller_info_config_iface_init (GimpConfigInterface *config_iface);
|
2004-06-16 19:11:32 +08:00
|
|
|
|
2004-06-17 02:14:44 +08:00
|
|
|
static void gimp_controller_info_finalize (GObject *object);
|
|
|
|
static void gimp_controller_info_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gimp_controller_info_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
|
|
|
static gboolean gimp_controller_info_serialize_property (GimpConfig *config,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GimpConfigWriter *writer);
|
|
|
|
static gboolean gimp_controller_info_deserialize_property (GimpConfig *config,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GScanner *scanner,
|
|
|
|
GTokenType *expected);
|
|
|
|
|
|
|
|
static gboolean gimp_controller_info_event (GimpController *controller,
|
|
|
|
const GimpControllerEvent *event,
|
|
|
|
GimpControllerInfo *info);
|
2004-06-16 19:11:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
static GimpObjectClass *parent_class = NULL;
|
|
|
|
|
2004-06-17 02:14:44 +08:00
|
|
|
static guint info_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
2004-06-16 19:11:32 +08:00
|
|
|
|
|
|
|
GType
|
|
|
|
gimp_controller_info_get_type (void)
|
|
|
|
{
|
|
|
|
static GType controller_type = 0;
|
|
|
|
|
|
|
|
if (! controller_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo controller_info =
|
|
|
|
{
|
|
|
|
sizeof (GimpControllerInfoClass),
|
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gimp_controller_info_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GimpControllerInfo),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) gimp_controller_info_init,
|
|
|
|
};
|
2004-06-17 02:14:44 +08:00
|
|
|
static const GInterfaceInfo config_iface_info =
|
|
|
|
{
|
|
|
|
(GInterfaceInitFunc) gimp_controller_info_config_iface_init,
|
|
|
|
NULL, /* iface_finalize */
|
|
|
|
NULL /* iface_data */
|
|
|
|
};
|
2004-06-16 19:11:32 +08:00
|
|
|
|
|
|
|
controller_type = g_type_register_static (GIMP_TYPE_OBJECT,
|
|
|
|
"GimpControllerInfo",
|
|
|
|
&controller_info, 0);
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
g_type_add_interface_static (controller_type, GIMP_TYPE_CONFIG,
|
|
|
|
&config_iface_info);
|
2004-06-16 19:11:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return controller_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_class_init (GimpControllerInfoClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gimp_controller_info_finalize;
|
|
|
|
object_class->set_property = gimp_controller_info_set_property;
|
|
|
|
object_class->get_property = gimp_controller_info_get_property;
|
2004-06-17 02:14:44 +08:00
|
|
|
|
2004-06-17 22:07:05 +08:00
|
|
|
GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_ENABLED,
|
|
|
|
"enabled", NULL,
|
|
|
|
TRUE,
|
|
|
|
0);
|
2004-06-17 02:14:44 +08:00
|
|
|
GIMP_CONFIG_INSTALL_PROP_OBJECT (object_class, PROP_CONTROLLER,
|
|
|
|
"controller", NULL,
|
|
|
|
GIMP_TYPE_CONTROLLER,
|
|
|
|
0);
|
|
|
|
GIMP_CONFIG_INSTALL_PROP_POINTER (object_class, PROP_MAPPING,
|
|
|
|
"mapping", NULL,
|
|
|
|
0);
|
|
|
|
|
|
|
|
info_signals[EVENT_MAPPED] =
|
|
|
|
g_signal_new ("event-mapped",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GimpControllerInfoClass, event_mapped),
|
2004-06-18 00:32:30 +08:00
|
|
|
gimp_boolean_handled_accum, NULL,
|
2004-06-17 02:14:44 +08:00
|
|
|
gimp_marshal_BOOLEAN__OBJECT_POINTER_STRING,
|
|
|
|
G_TYPE_BOOLEAN, 3,
|
|
|
|
G_TYPE_OBJECT,
|
|
|
|
G_TYPE_POINTER,
|
|
|
|
G_TYPE_STRING);
|
2004-06-16 19:11:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_init (GimpControllerInfo *info)
|
|
|
|
{
|
|
|
|
info->controller = NULL;
|
2004-06-17 02:14:44 +08:00
|
|
|
info->mapping = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_config_iface_init (GimpConfigInterface *config_iface)
|
|
|
|
{
|
|
|
|
config_iface->serialize_property = gimp_controller_info_serialize_property;
|
|
|
|
config_iface->deserialize_property = gimp_controller_info_deserialize_property;
|
2004-06-16 19:11:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GimpControllerInfo *info = GIMP_CONTROLLER_INFO (object);
|
|
|
|
|
|
|
|
if (info->controller)
|
|
|
|
g_object_unref (info->controller);
|
|
|
|
|
|
|
|
if (info->mapping)
|
|
|
|
g_hash_table_destroy (info->mapping);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpControllerInfo *info = GIMP_CONTROLLER_INFO (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2004-06-17 22:07:05 +08:00
|
|
|
case PROP_ENABLED:
|
|
|
|
info->enabled = g_value_get_boolean (value);
|
|
|
|
break;
|
2004-06-17 02:14:44 +08:00
|
|
|
case PROP_CONTROLLER:
|
|
|
|
if (info->controller)
|
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (info->controller,
|
|
|
|
gimp_controller_info_event,
|
|
|
|
info);
|
|
|
|
g_object_unref (info->controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->controller = (GimpController *) g_value_dup_object (value);
|
|
|
|
|
|
|
|
if (info->controller)
|
|
|
|
{
|
|
|
|
g_signal_connect_object (info->controller, "event",
|
|
|
|
G_CALLBACK (gimp_controller_info_event),
|
|
|
|
G_OBJECT (info),
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PROP_MAPPING:
|
|
|
|
if (info->mapping)
|
|
|
|
g_hash_table_destroy (info->mapping);
|
|
|
|
info->mapping = g_value_get_pointer (value);
|
|
|
|
break;
|
|
|
|
|
2004-06-16 19:11:32 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpControllerInfo *info = GIMP_CONTROLLER_INFO (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2004-06-17 22:07:05 +08:00
|
|
|
case PROP_ENABLED:
|
|
|
|
g_value_set_boolean (value, info->enabled);
|
|
|
|
break;
|
2004-06-17 02:14:44 +08:00
|
|
|
case PROP_CONTROLLER:
|
|
|
|
g_value_set_object (value, info->controller);
|
|
|
|
break;
|
|
|
|
case PROP_MAPPING:
|
|
|
|
g_value_set_pointer (value, info->mapping);
|
|
|
|
break;
|
|
|
|
|
2004-06-16 19:11:32 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_controller_info_serialize_mapping (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
const gchar *event_name = key;
|
|
|
|
const gchar *action_name = value;
|
|
|
|
GimpConfigWriter *writer = data;
|
|
|
|
|
|
|
|
gimp_config_writer_open (writer, "map");
|
|
|
|
gimp_config_writer_string (writer, event_name);
|
|
|
|
gimp_config_writer_string (writer, action_name);
|
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_controller_info_serialize_property (GimpConfig *config,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
GHashTable *mapping;
|
|
|
|
|
|
|
|
if (property_id != PROP_MAPPING)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
mapping = g_value_get_pointer (value);
|
|
|
|
|
2004-06-17 22:07:05 +08:00
|
|
|
if (mapping)
|
|
|
|
{
|
|
|
|
gimp_config_writer_open (writer, pspec->name);
|
2004-06-17 02:14:44 +08:00
|
|
|
|
2004-06-17 22:07:05 +08:00
|
|
|
g_hash_table_foreach (mapping,
|
|
|
|
(GHFunc) gimp_controller_info_serialize_mapping,
|
|
|
|
writer);
|
2004-06-17 02:14:44 +08:00
|
|
|
|
2004-06-17 22:07:05 +08:00
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
}
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_controller_info_deserialize_property (GimpConfig *config,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GScanner *scanner,
|
|
|
|
GTokenType *expected)
|
|
|
|
{
|
2004-06-17 22:07:05 +08:00
|
|
|
GHashTable *mapping;
|
2004-06-17 02:14:44 +08:00
|
|
|
GTokenType token;
|
|
|
|
|
|
|
|
if (property_id != PROP_MAPPING)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
mapping = g_hash_table_new_full (g_str_hash,
|
|
|
|
g_str_equal,
|
|
|
|
(GDestroyNotify) g_free,
|
|
|
|
(GDestroyNotify) g_free);
|
|
|
|
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
|
|
|
|
while (g_scanner_peek_next_token (scanner) == token)
|
|
|
|
{
|
|
|
|
token = g_scanner_get_next_token (scanner);
|
|
|
|
|
|
|
|
switch (token)
|
|
|
|
{
|
|
|
|
case G_TOKEN_LEFT_PAREN:
|
|
|
|
token = G_TOKEN_IDENTIFIER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_IDENTIFIER:
|
|
|
|
if (! strcmp (scanner->value.v_identifier, "map"))
|
|
|
|
{
|
|
|
|
gchar *event_name;
|
|
|
|
gchar *action_name;
|
|
|
|
|
|
|
|
token = G_TOKEN_STRING;
|
|
|
|
if (! gimp_scanner_parse_string (scanner, &event_name))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
token = G_TOKEN_STRING;
|
|
|
|
if (! gimp_scanner_parse_string (scanner, &action_name))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
g_hash_table_insert (mapping, event_name, action_name);
|
|
|
|
}
|
|
|
|
token = G_TOKEN_RIGHT_PAREN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_RIGHT_PAREN:
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token == G_TOKEN_LEFT_PAREN)
|
|
|
|
{
|
|
|
|
token = G_TOKEN_RIGHT_PAREN;
|
|
|
|
|
|
|
|
if (g_scanner_peek_next_token (scanner) == token)
|
|
|
|
{
|
|
|
|
g_value_set_pointer (value, mapping);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error:
|
2004-06-17 22:07:05 +08:00
|
|
|
g_hash_table_destroy (mapping);
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
*expected = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-06-17 22:07:05 +08:00
|
|
|
void
|
|
|
|
gimp_controller_info_set_enabled (GimpControllerInfo *info,
|
|
|
|
gboolean enabled)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_CONTROLLER_INFO (info));
|
|
|
|
|
|
|
|
if (enabled != info->enabled)
|
|
|
|
g_object_set (info, "enabled", enabled, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gimp_controller_info_get_enabled (GimpControllerInfo *info)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_CONTROLLER_INFO (info), FALSE);
|
|
|
|
|
|
|
|
return info->enabled;
|
|
|
|
}
|
|
|
|
|
2004-06-17 02:14:44 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_controller_info_event (GimpController *controller,
|
|
|
|
const GimpControllerEvent *event,
|
|
|
|
GimpControllerInfo *info)
|
|
|
|
{
|
|
|
|
const gchar *event_name;
|
|
|
|
const gchar *event_blurb;
|
2004-06-17 22:07:05 +08:00
|
|
|
const gchar *action_name = NULL;
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
event_name = gimp_controller_get_event_name (controller, event->any.event_id);
|
|
|
|
event_blurb = gimp_controller_get_event_blurb (controller, event->any.event_id);
|
|
|
|
|
|
|
|
g_print ("Received '%s' (class '%s')\n"
|
|
|
|
" controller event '%s (%s)'\n",
|
2004-06-17 22:07:05 +08:00
|
|
|
controller->name, GIMP_CONTROLLER_GET_CLASS (controller)->name,
|
2004-06-17 02:14:44 +08:00
|
|
|
event_name, event_blurb);
|
|
|
|
|
2004-06-17 22:07:05 +08:00
|
|
|
if (! info->enabled)
|
|
|
|
{
|
|
|
|
g_print (" ignoring because controller is disabled\n");
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->mapping)
|
|
|
|
action_name = g_hash_table_lookup (info->mapping, event_name);
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
if (action_name)
|
|
|
|
{
|
|
|
|
gboolean retval = FALSE;
|
|
|
|
|
|
|
|
g_print (" maps to action '%s'\n", action_name);
|
|
|
|
|
|
|
|
g_signal_emit (info, info_signals[EVENT_MAPPED], 0,
|
|
|
|
controller, event, action_name, &retval);
|
|
|
|
|
|
|
|
if (retval)
|
2004-06-17 03:51:33 +08:00
|
|
|
g_print (" action was found\n\n");
|
2004-06-17 02:14:44 +08:00
|
|
|
else
|
2004-06-17 03:51:33 +08:00
|
|
|
g_print (" action NOT found\n\n");
|
2004-06-17 02:14:44 +08:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_print (" doesn't map to action\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|