2
* Copyright Ā© 2007, 2010 Christian Persch
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; either version 3, or (at your option)
9
* This program is distributed in the hope conf it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include "games-settings.h"
25
#if GTK_CHECK_VERSION (2, 90, 7)
26
#define GDK_KEY(symbol) GDK_KEY_##symbol
28
#include <gdk/gdkkeysyms.h>
29
#define GDK_KEY(symbol) GDK_##symbol
32
#include "games-gtk-compat.h"
33
#include "games-debug.h"
35
#define I_(string) g_intern_static_string (string)
37
#define SCHEMA_NAME I_("org.gnome.Games.WindowState")
39
#define STATE_KEY_MAXIMIZED I_("maximized")
40
#define STATE_KEY_FULLSCREEN I_("fullscreen")
41
#define STATE_KEY_WIDTH I_("width")
42
#define STATE_KEY_HEIGHT I_("height")
49
guint is_maximised : 1;
50
guint is_fullscreen : 1;
54
free_window_state (WindowState *state)
56
/* Now store the settings */
57
g_settings_set_int (state->settings, STATE_KEY_WIDTH, state->width);
58
g_settings_set_int (state->settings, STATE_KEY_HEIGHT, state->height);
59
g_settings_set_boolean (state->settings, STATE_KEY_MAXIMIZED, state->is_maximised);
60
g_settings_set_boolean (state->settings, STATE_KEY_FULLSCREEN, state->is_fullscreen);
62
g_settings_apply (state->settings);
64
g_object_unref (state->settings);
66
g_slice_free (WindowState, state);
70
window_configure_event_cb (GtkWidget *widget,
71
GdkEventConfigure *event,
74
_games_debug_print (GAMES_DEBUG_WINDOW_STATE,
75
"[window %p] configure event current %dx%d new %dx%d [state: is-maximised:%s is-fullscreen:%s]\n",
77
state->width, state->height,
78
event->width, event->height,
79
state->is_maximised ? "t" : "f",
80
state->is_fullscreen ? "t" : "f");
82
if (!state->is_maximised && !state->is_fullscreen) {
83
state->width = event->width;
84
state->height = event->height;
91
window_state_event_cb (GtkWidget *widget,
92
GdkEventWindowState *event,
95
_games_debug_print (GAMES_DEBUG_WINDOW_STATE,
96
"[window %p] state event, mask:%x new-state:%x current state: is-maximised:%s is-fullscreen:%s\n",
98
event->changed_mask, event->new_window_state,
99
state->is_maximised ? "t" : "f",
100
state->is_fullscreen ? "t" : "f");
102
if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) {
103
state->is_maximised = (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
105
if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
106
state->is_fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) != 0;
109
_games_debug_print (GAMES_DEBUG_WINDOW_STATE,
110
" > new state: is-maximised:%s is-fullscreen:%s\n",
111
state->is_maximised ? "t" : "f",
112
state->is_fullscreen ? "t" : "f");
118
#if 0 //#ifndef HAVE_HILDON
120
#define ACCELMAP_EXT "accels"
123
games_conf_get_accel_map_path (GamesConf *conf,
124
gboolean ensure_dir_exists)
126
GamesConfPrivate *priv = conf->priv;
127
char *game_name, *conf_dir;
128
char *conf_file = NULL;
129
const char *override;
131
game_name = g_ascii_strdown (priv->game_name, -1);
134
override = g_getenv ("GNOME22_USER_DIR");
136
conf_dir = g_build_filename (override, "accels", NULL);
138
conf_dir = g_build_filename (g_get_home_dir (), ".gnome2", "accels", NULL);
140
conf_dir = g_build_filename (g_get_user_config_dir (), "gnome-games", NULL);
145
/* Mode 0700 per the XDG basedir spec */
146
if (ensure_dir_exists &&
147
g_mkdir_with_parents (conf_dir, 0700) < 0) {
151
g_warning ("Failed to create config directory \"%s\": %s\n", conf_dir, g_strerror (err));
157
conf_file = g_build_filename (conf_dir, game_name, NULL);
160
char *accelmap_filename;
162
accelmap_filename = g_strdup_printf ("%s.%s", game_name, ACCELMAP_EXT);
163
conf_file = g_build_filename (conf_dir, accelmap_filename, NULL);
164
g_free (accelmap_filename);
176
games_conf_load_accel_map (GamesConf *conf)
180
conf_file = games_conf_get_accel_map_path (conf, FALSE);
184
gtk_accel_map_load (conf_file);
189
games_conf_save_accel_map (GamesConf *conf)
193
conf_file = games_conf_get_accel_map_path (conf, TRUE);
197
gtk_accel_map_save (conf_file);
201
#endif /* !HAVE_HILDON */
205
GdkModifierType modifiers;
209
variant_to_keyval (GVariant *value,
214
entry->keyval = GDK_KEY (VoidSymbol);
215
entry->modifiers = 0;
219
gtk_accelerator_parse (g_variant_get_string (value, NULL),
220
&entry->keyval, &entry->modifiers);
221
if (entry->keyval == 0 && entry->modifiers == 0)
228
* games_settings_get_keyval:
229
* @settings: a #GSettings
234
* Returns the keyboard key associated with @key in @group, or 0 if
235
* the value could not be parsed as a keyval.
237
* Returns: a keyboard key value
240
games_settings_get_keyval (GSettings *settings,
243
GdkModifierType *modifiers)
247
g_return_if_fail (G_IS_SETTINGS (settings));
248
g_return_if_fail (key != NULL && key[0] != '\0');
250
g_settings_get_mapped (settings, key, (GSettingsGetMapping) variant_to_keyval, &entry);
252
*keyval = entry.keyval;
254
*modifiers = entry.modifiers;
258
* games_settings_set_keyval:
259
* @settings: a #GSettings
261
* @keyval: the value to store
262
* @modifiers: key modifiers with @keyval
264
* Associates @keyval with the key @key in group @group.
266
* It is a programmer error to pass a key that isn't valid for settings.
268
* Returns: TRUE if setting the key succeeded, FALSE if the key was not writable
271
games_settings_set_keyval (GSettings *settings,
274
GdkModifierType modifiers)
279
g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
280
g_return_val_if_fail (key != NULL && key[0] != '\0', FALSE);
282
value = gtk_accelerator_name (keyval, modifiers);
283
rv = g_settings_set_string (settings, key, value);
290
* games_settings_bind_window_state:
291
* @path: a valid #GSettings path
292
* @window: a #GtkWindow
294
* Restore the window configuration, and persist changes to the window configuration:
295
* window width and height, and maximised and fullscreen state.
296
* @window must not be realised yet.
298
* To make sure the state is saved at exit, g_settings_sync() must be called.
301
games_settings_bind_window_state (const char *path,
306
gboolean maximised, fullscreen;
308
g_return_if_fail (GTK_IS_WINDOW (window));
309
g_return_if_fail (!gtk_widget_get_realized (GTK_WIDGET (window)));
311
state = g_slice_new0 (WindowState);
313
state->window = window;
314
state->settings = g_settings_new_with_path (SCHEMA_NAME, path);
316
/* We delay storing the state until exit */
317
g_settings_delay (state->settings);
319
g_object_set_data_full (G_OBJECT (window), "GamesSettings::WindowState",
320
state, (GDestroyNotify) free_window_state);
322
g_signal_connect (window, "configure-event",
323
G_CALLBACK (window_configure_event_cb), state);
324
g_signal_connect (window, "window-state-event",
325
G_CALLBACK (window_state_event_cb), state);
327
maximised = g_settings_get_boolean (state->settings, STATE_KEY_MAXIMIZED);
328
fullscreen = g_settings_get_boolean (state->settings, STATE_KEY_FULLSCREEN);
329
width = g_settings_get_int (state->settings, STATE_KEY_WIDTH);
330
height = g_settings_get_int (state->settings, STATE_KEY_HEIGHT);
332
if (width > 0 && height > 0) {
333
_games_debug_print (GAMES_DEBUG_WINDOW_STATE,
334
"[window %p] restoring size %dx%d\n",
337
gtk_window_set_default_size (GTK_WINDOW (window), width, height);
340
_games_debug_print (GAMES_DEBUG_WINDOW_STATE,
341
"[window %p] restoring maximised state\n",
343
gtk_window_maximize (GTK_WINDOW (window));
346
_games_debug_print (GAMES_DEBUG_WINDOW_STATE,
347
"[window %p] restoring fullscreen state\n",
349
gtk_window_fullscreen (GTK_WINDOW (window));