~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to libgames-support/games-settings.c

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright Ā© 2007, 2010 Christian Persch
 
3
 *
 
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)
 
7
 *  any later version.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include <config.h>
 
20
 
 
21
#include "games-settings.h"
 
22
 
 
23
#include <gtk/gtk.h>
 
24
 
 
25
#if GTK_CHECK_VERSION (2, 90, 7)
 
26
#define GDK_KEY(symbol) GDK_KEY_##symbol
 
27
#else
 
28
#include <gdk/gdkkeysyms.h>
 
29
#define GDK_KEY(symbol) GDK_##symbol
 
30
#endif
 
31
 
 
32
#include "games-gtk-compat.h"
 
33
#include "games-debug.h"
 
34
 
 
35
#define I_(string) g_intern_static_string (string)
 
36
 
 
37
#define SCHEMA_NAME           I_("org.gnome.Games.WindowState")
 
38
 
 
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")
 
43
 
 
44
typedef struct {
 
45
  GSettings *settings;
 
46
  GtkWindow *window;
 
47
  int width;
 
48
  int height;
 
49
  guint is_maximised : 1;
 
50
  guint is_fullscreen : 1;
 
51
} WindowState;
 
52
 
 
53
static void
 
54
free_window_state (WindowState *state)
 
55
{
 
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);
 
61
 
 
62
  g_settings_apply (state->settings);
 
63
 
 
64
  g_object_unref (state->settings);
 
65
 
 
66
  g_slice_free (WindowState, state);
 
67
}
 
68
 
 
69
static gboolean
 
70
window_configure_event_cb (GtkWidget *widget,
 
71
                           GdkEventConfigure *event,
 
72
                           WindowState *state)
 
73
{
 
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",
 
76
                      state->window,
 
77
                      state->width, state->height,
 
78
                      event->width, event->height,
 
79
                      state->is_maximised ? "t" : "f",
 
80
                      state->is_fullscreen ? "t" : "f");
 
81
 
 
82
  if (!state->is_maximised && !state->is_fullscreen) {
 
83
    state->width = event->width;
 
84
    state->height = event->height;
 
85
  }
 
86
 
 
87
  return FALSE;
 
88
}
 
89
 
 
90
static gboolean
 
91
window_state_event_cb (GtkWidget *widget,
 
92
                       GdkEventWindowState *event,
 
93
                       WindowState *state)
 
94
{
 
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",
 
97
                      state->window,
 
98
                      event->changed_mask, event->new_window_state,
 
99
                      state->is_maximised ? "t" : "f",
 
100
                      state->is_fullscreen ? "t" : "f");
 
101
 
 
102
  if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) {
 
103
    state->is_maximised = (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
 
104
  }
 
105
  if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
 
106
    state->is_fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) != 0;
 
107
  }
 
108
 
 
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");
 
113
 
 
114
 
 
115
  return FALSE;
 
116
}
 
117
 
 
118
#if 0 //#ifndef HAVE_HILDON
 
119
 
 
120
#define ACCELMAP_EXT "accels"
 
121
 
 
122
static char *
 
123
games_conf_get_accel_map_path (GamesConf *conf,
 
124
                               gboolean ensure_dir_exists)
 
125
{
 
126
  GamesConfPrivate *priv = conf->priv;
 
127
  char *game_name, *conf_dir;
 
128
  char *conf_file = NULL;
 
129
  const char *override;
 
130
 
 
131
  game_name = g_ascii_strdown (priv->game_name, -1);
 
132
 
 
133
#ifdef HAVE_GNOME
 
134
  override = g_getenv ("GNOME22_USER_DIR");
 
135
  if (override)
 
136
    conf_dir = g_build_filename (override, "accels", NULL);
 
137
  else
 
138
    conf_dir = g_build_filename (g_get_home_dir (), ".gnome2", "accels", NULL);
 
139
#else
 
140
  conf_dir = g_build_filename (g_get_user_config_dir (), "gnome-games", NULL);
 
141
#endif
 
142
  if (!conf_dir)
 
143
    goto loser;
 
144
 
 
145
  /* Mode 0700 per the XDG basedir spec */
 
146
  if (ensure_dir_exists &&
 
147
      g_mkdir_with_parents (conf_dir, 0700) < 0) {
 
148
    int err = errno;
 
149
 
 
150
    if (err != EEXIST) {
 
151
      g_warning ("Failed to create config directory \"%s\": %s\n", conf_dir, g_strerror (err));
 
152
      goto loser;
 
153
    }
 
154
  }
 
155
 
 
156
#ifdef HAVE_GNOME
 
157
  conf_file = g_build_filename (conf_dir, game_name, NULL);
 
158
#else
 
159
{
 
160
  char *accelmap_filename;
 
161
 
 
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);
 
165
}
 
166
#endif
 
167
 
 
168
loser:
 
169
  g_free (conf_dir);
 
170
  g_free (game_name);
 
171
 
 
172
  return conf_file;
 
173
}
 
174
 
 
175
static void
 
176
games_conf_load_accel_map (GamesConf *conf)
 
177
{
 
178
  char *conf_file;
 
179
 
 
180
  conf_file = games_conf_get_accel_map_path (conf, FALSE);
 
181
  if (!conf_file)
 
182
    return;
 
183
 
 
184
  gtk_accel_map_load (conf_file);
 
185
  g_free (conf_file);
 
186
}
 
187
 
 
188
static void
 
189
games_conf_save_accel_map (GamesConf *conf)
 
190
{
 
191
  char *conf_file;
 
192
 
 
193
  conf_file = games_conf_get_accel_map_path (conf, TRUE);
 
194
  if (!conf_file)
 
195
    return;
 
196
 
 
197
  gtk_accel_map_save (conf_file);
 
198
  g_free (conf_file);
 
199
}
 
200
 
 
201
#endif /* !HAVE_HILDON */
 
202
 
 
203
typedef struct {
 
204
  guint keyval;
 
205
  GdkModifierType modifiers;
 
206
} KeyEntry;
 
207
 
 
208
static gboolean
 
209
variant_to_keyval (GVariant *value,
 
210
                   gpointer *result,
 
211
                   KeyEntry *entry)
 
212
{
 
213
  if (value == NULL) {
 
214
    entry->keyval = GDK_KEY (VoidSymbol);
 
215
    entry->modifiers = 0;
 
216
    return TRUE;
 
217
  }
 
218
 
 
219
  gtk_accelerator_parse (g_variant_get_string (value, NULL),
 
220
                         &entry->keyval, &entry->modifiers);
 
221
  if (entry->keyval == 0 && entry->modifiers == 0)
 
222
    return FALSE;
 
223
 
 
224
  return TRUE;
 
225
}
 
226
 
 
227
/**
 
228
 * games_settings_get_keyval:
 
229
 * @settings: a #GSettings
 
230
 * @key: the key name
 
231
 * @keyval: (out):
 
232
 * @modifiers: (out):
 
233
 *
 
234
 * Returns the keyboard key associated with @key in @group, or 0 if
 
235
 * the value could not be parsed as a keyval.
 
236
 *
 
237
 * Returns: a keyboard key value
 
238
 */
 
239
void
 
240
games_settings_get_keyval (GSettings *settings,
 
241
                           const char *key,
 
242
                           guint *keyval,
 
243
                           GdkModifierType *modifiers)
 
244
{
 
245
  KeyEntry entry;
 
246
 
 
247
  g_return_if_fail (G_IS_SETTINGS (settings));
 
248
  g_return_if_fail (key != NULL && key[0] != '\0');
 
249
 
 
250
  g_settings_get_mapped (settings, key, (GSettingsGetMapping) variant_to_keyval, &entry);
 
251
  if (keyval)
 
252
    *keyval = entry.keyval;
 
253
  if (modifiers)
 
254
    *modifiers = entry.modifiers;
 
255
}
 
256
 
 
257
/**
 
258
 * games_settings_set_keyval:
 
259
 * @settings: a #GSettings
 
260
 * @key: the key name
 
261
 * @keyval: the value to store
 
262
 * @modifiers: key modifiers with @keyval
 
263
 *
 
264
 * Associates @keyval with the key @key in group @group.
 
265
 *
 
266
 * It is a programmer error to pass a key that isn't valid for settings.
 
267
 *
 
268
 * Returns: TRUE if setting the key succeeded, FALSE if the key was not writable
 
269
 */
 
270
gboolean
 
271
games_settings_set_keyval (GSettings *settings,
 
272
                           const char *key,
 
273
                           guint keyval,
 
274
                           GdkModifierType modifiers)
 
275
{
 
276
  char *value;
 
277
  gboolean rv;
 
278
 
 
279
  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
 
280
  g_return_val_if_fail (key != NULL && key[0] != '\0', FALSE);
 
281
 
 
282
  value = gtk_accelerator_name (keyval, modifiers);
 
283
  rv = g_settings_set_string (settings, key, value);
 
284
  g_free (value);
 
285
 
 
286
  return rv;
 
287
}
 
288
 
 
289
/**
 
290
 * games_settings_bind_window_state:
 
291
 * @path: a valid #GSettings path
 
292
 * @window: a #GtkWindow
 
293
 *
 
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.
 
297
 *
 
298
 * To make sure the state is saved at exit, g_settings_sync() must be called.
 
299
 */
 
300
void
 
301
games_settings_bind_window_state (const char *path,
 
302
                                  GtkWindow *window)
 
303
{
 
304
  WindowState *state;
 
305
  int width, height;
 
306
  gboolean maximised, fullscreen;
 
307
 
 
308
  g_return_if_fail (GTK_IS_WINDOW (window));
 
309
  g_return_if_fail (!gtk_widget_get_realized (GTK_WIDGET (window)));
 
310
 
 
311
  state = g_slice_new0 (WindowState);
 
312
 
 
313
  state->window = window;
 
314
  state->settings = g_settings_new_with_path (SCHEMA_NAME, path);
 
315
 
 
316
  /* We delay storing the state until exit */
 
317
  g_settings_delay (state->settings);
 
318
 
 
319
  g_object_set_data_full (G_OBJECT (window), "GamesSettings::WindowState",
 
320
                          state, (GDestroyNotify) free_window_state);
 
321
 
 
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);
 
326
 
 
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);
 
331
 
 
332
  if (width > 0 && height > 0) {
 
333
    _games_debug_print (GAMES_DEBUG_WINDOW_STATE,
 
334
                        "[window %p] restoring size %dx%d\n",
 
335
                        state->window,
 
336
                        width, height);
 
337
    gtk_window_set_default_size (GTK_WINDOW (window), width, height);
 
338
  }
 
339
  if (maximised) {
 
340
    _games_debug_print (GAMES_DEBUG_WINDOW_STATE,
 
341
                        "[window %p] restoring maximised state\n",
 
342
                        state->window);
 
343
    gtk_window_maximize (GTK_WINDOW (window));
 
344
  }
 
345
  if (fullscreen) {
 
346
    _games_debug_print (GAMES_DEBUG_WINDOW_STATE,
 
347
                        "[window %p] restoring fullscreen state\n",
 
348
                        state->window);
 
349
    gtk_window_fullscreen (GTK_WINDOW (window));
 
350
  }
 
351
}