~ubuntu-branches/ubuntu/natty/gnome-desktop/natty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
diff -Nur -x '*.orig' -x '*~' gnome-desktop-2.29.90/libgnome-desktop/gnome-rr-config.c gnome-desktop-2.29.90.new/libgnome-desktop/gnome-rr-config.c
--- gnome-desktop-2.29.90/libgnome-desktop/gnome-rr-config.c	2009-10-30 11:23:56.000000000 +0000
+++ gnome-desktop-2.29.90.new/libgnome-desktop/gnome-rr-config.c	2010-02-15 00:01:14.463252030 +0000
@@ -41,6 +41,7 @@
 
 #define CONFIG_INTENDED_BASENAME "monitors.xml"
 #define CONFIG_BACKUP_BASENAME "monitors.xml.backup"
+#define CONFIG_DESIRED_BASENAME "monitors.xml.desired"
 
 /* In version 0 of the config file format, we had several <configuration>
  * toplevel elements and no explicit version number.  So, the filed looked
@@ -85,6 +86,10 @@
 static void             output_free           (GnomeOutputInfo  *output);
 static GnomeOutputInfo *output_copy           (GnomeOutputInfo  *output);
 
+static gboolean        gnome_rr_config_save_to_file (GnomeRRConfig *configuration,
+                       GError **err, gchar *filename);
+static void            remove_desired_settings      (void);
+
 typedef struct Parser Parser;
 
 /* Parser for monitor configurations */
@@ -410,6 +415,16 @@
 	NULL, /* error */
     };
 
+	/* return NULL if the file doesn't exist */
+	if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+    {
+	result = NULL;
+	g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT,
+		     "No such file: %s", filename);	
+	return result;
+    }
+
+
     parser->config_file_version = 0;
     parser->configurations = g_ptr_array_new ();
     parser->outputs = g_ptr_array_new ();
@@ -916,6 +931,65 @@
     return g_build_filename (g_get_user_config_dir (), CONFIG_INTENDED_BASENAME, NULL);
 }
 
+static gchar *
+get_desired_config_filename (void)
+{
+    return g_build_filename (g_get_user_config_dir (), CONFIG_DESIRED_BASENAME, NULL);
+}
+
+static GnomeRRConfig **
+desired_configurations_read (GError **error)
+{
+    char *filename;
+    GnomeRRConfig **configs;
+    GError *err;
+
+    /* Try the desired configuration file... */
+
+    filename = get_desired_config_filename ();
+    
+#if 0
+    g_debug("desired is = %s\n", filename);
+#endif
+    err = NULL;
+
+#if 0
+    g_debug("reading configuration...\n");
+#endif
+    configs = configurations_read_from_file (filename, &err);
+
+    g_free (filename);
+    
+#if 0
+    g_debug("done\n");
+#endif
+    if (err)
+    {
+        if (g_error_matches (err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+        {
+            
+#if 0
+            g_debug("error MATCHES\n");
+#endif
+            g_error_free (err);
+            
+            /* Okay, so try the old configuration file */
+            filename = gnome_rr_config_get_intended_filename ();
+            configs = configurations_read_from_file (filename, error);
+            g_free (filename);
+        }
+        else
+        {
+#if 0
+            g_debug("error does NOT MATCH\n");
+#endif
+            g_propagate_error (error, err);
+        }
+    }
+    
+    return configs;
+}
+
 static const char *
 get_rotation_name (GnomeRRRotation r)
 {
@@ -1057,13 +1131,13 @@
 }
 
 
-gboolean
-gnome_rr_config_save (GnomeRRConfig *configuration, GError **error)
+static gboolean
+gnome_rr_config_save_to_file (GnomeRRConfig *configuration, GError **error, gchar *intended_filename)
 {
     GnomeRRConfig **configurations;
     GString *output;
     int i;
-    gchar *intended_filename;
+
     gchar *backup_filename;
     gboolean result;
 
@@ -1073,7 +1147,7 @@
     output = g_string_new ("");
 
     backup_filename = gnome_rr_config_get_backup_filename ();
-    intended_filename = gnome_rr_config_get_intended_filename ();
+
 
     configurations = configurations_read_from_file (intended_filename, NULL); /* NULL-GError */
     
@@ -1108,6 +1182,37 @@
     return result;
 }
 
+gboolean
+gnome_rr_config_save (GnomeRRConfig *configuration, GError **err)
+{
+    gchar *intended_filename;
+    intended_filename = gnome_rr_config_get_intended_filename ();
+    
+    return(gnome_rr_config_save_to_file(configuration, err, intended_filename));
+}
+
+gboolean
+ubuntu_gnome_rr_config_save_desired (GnomeRRConfig *configuration, GError **err)
+{
+    gchar *filename;
+    filename = get_desired_config_filename ();
+    
+    return(gnome_rr_config_save_to_file(configuration, err, filename));
+}
+
+static void
+remove_desired_settings(void)
+{
+    gchar *filename;
+    filename = get_desired_config_filename ();
+    if (g_file_test (filename, G_FILE_TEST_EXISTS))
+        g_unlink (filename);
+
+    g_free (filename);
+
+}
+
+
 static GnomeRRConfig *
 gnome_rr_config_copy (GnomeRRConfig *config)
 {
@@ -1127,7 +1232,7 @@
 }
 
 static GnomeRRConfig *
-config_new_stored (GnomeRRScreen *screen, const char *filename, GError **error)
+gnome_rr_desired_config_new_stored (GnomeRRScreen *screen, GError **error)
 {
     GnomeRRConfig *current;
     GnomeRRConfig **configs;
@@ -1138,7 +1243,7 @@
     
     current = gnome_rr_config_new_current (screen);
     
-    configs = configurations_read_from_file (filename, error);
+    configs = desired_configurations_read (error);
 
     result = NULL;
     if (configs)
@@ -1154,10 +1259,53 @@
 	    }
 	}
 
+	/*
 	if (result == NULL)
 	    g_set_error (error, GNOME_RR_ERROR, GNOME_RR_ERROR_NO_MATCHING_CONFIG,
 			 _("none of the saved display configurations matched the active configuration"));
+	*/
+
+	configurations_free (configs);
+    }
+
+    gnome_rr_config_free (current);
+    
+    return result;
+}
+
+
+static GnomeRRConfig *
+config_new_stored (GnomeRRScreen *screen, const char *filename, GError **error)
+{
+    GnomeRRConfig *current;
+    GnomeRRConfig **configs;
+    GnomeRRConfig *result;
 
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+    
+    current = gnome_rr_config_new_current (screen);
+    
+    configs = configurations_read_from_file (filename, error);
+
+    result = NULL;
+    if (configs)
+    {
+	int i;
+	
+	for (i = 0; configs[i] != NULL; ++i)
+	{
+	    if (gnome_rr_config_match (configs[i], current))
+	    {
+		result = gnome_rr_config_copy (configs[i]);
+		break;
+	    }
+	}
+    /*
+	if (result == NULL)
+	    g_set_error (error, GNOME_RR_ERROR, GNOME_RR_ERROR_NO_MATCHING_CONFIG,
+			 _("none of the saved display configurations matched the active configuration"));
+    */
 	configurations_free (configs);
     }
 
@@ -1191,6 +1339,65 @@
 }
 #endif
 
+
+void
+ubuntu_compute_virtual_size_for_configuration (GnomeRRConfig *config, int *ret_width, int *ret_height)
+{
+    int i;
+    int width, height;
+
+    width = height = 0;
+
+    for (i = 0; config->outputs[i] != NULL; i++)
+    {
+	GnomeOutputInfo *output;
+
+	output = config->outputs[i];
+
+	if (output->on)
+	{
+	    width = MAX (width, output->x + output->width);
+	    height = MAX (height, output->y + output->height);
+	}
+    }
+
+    *ret_width = width;
+    *ret_height = height;
+}
+
+static gboolean
+check_framebuffer_size (GnomeRRConfig *config, GnomeRRScreen *screen)
+{
+    int req_width, req_height;
+    int min_width, max_width;
+    int min_height, max_height;
+    
+    /* Put the desired configuration 
+     * instead of app->current_configuration
+     */
+    ubuntu_compute_virtual_size_for_configuration (config, &req_width, &req_height);
+    
+    /* Put screen 
+     * instead of app->screen
+     */
+    gnome_rr_screen_get_ranges (screen, &min_width, &max_width, &min_height, &max_height);
+
+#if 0
+    g_debug ("X Server supports:\n");
+    g_debug ("min_width = %d, max_width = %d\n", min_width, max_width);
+    g_debug ("min_height = %d, max_height = %d\n", min_height, max_height);
+
+    g_debug ("Requesting size of %dx%d\n", req_width, req_height);
+#endif
+
+    if (!(min_width <= req_width && req_width <= max_width
+	  && min_height <= req_height && req_height <= max_height))
+        return FALSE;
+    else
+        return TRUE;
+}
+
+
 gboolean
 gnome_rr_config_apply_with_time (GnomeRRConfig *config,
 				 GnomeRRScreen *screen,
@@ -1324,6 +1531,7 @@
 gnome_rr_config_apply_from_filename_with_time (GnomeRRScreen *screen, const char *filename, guint32 timestamp, GError **error)
 {
     GnomeRRConfig *stored;
+    gboolean is_desired = FALSE;
     GError *my_error;
 
     g_return_val_if_fail (screen != NULL, FALSE);
@@ -1340,22 +1548,71 @@
 	    /* This means the screen didn't change, so just proceed */
     }
 
-    stored = config_new_stored (screen, filename, error);
-
-    if (stored)
-    {
-	gboolean result;
-
-	result = gnome_rr_config_apply_with_time (stored, screen, timestamp, error);
+    /* Look for the desired settings and try to load that
+     * instead.
+     * Then remove such settings.
+     * else read the usual configuration
+     */
+    gchar *desired_filename;
+    desired_filename = get_desired_config_filename ();
+    
+    /* If the desired file exists */
+    if ((g_file_test(desired_filename, G_FILE_TEST_EXISTS))) {
+        stored = gnome_rr_desired_config_new_stored (screen, NULL);
+        
+        /* If the desired file can't be applied */
+        if (!stored) {
+            stored = gnome_rr_config_new_stored (screen, error);
+        }
+        else {
+            /* the desired file is valid and 
+               can be applied */
+            is_desired = TRUE;
+        }
+    }
+    /* If the desired file does not exist */
+    else {
+        stored = config_new_stored (screen, filename, error);
+    }
 
-	gnome_rr_config_free (stored);
-	
-	return result;
+    /* remove desired */
+	remove_desired_settings();
+	
+    if (stored) {
+    	gboolean result = FALSE;
+        
+        /* Check framebuffer size. Do not apply
+	     * the settings if the fb is not big
+	     * enough
+	     */
+        if (check_framebuffer_size (stored, screen)) {
+        
+        	result = gnome_rr_config_apply_with_time (stored, screen, timestamp, error);
+
+        	/* if we are using the desired settings and
+        	   the settings were applied successfully */
+        	if (is_desired && result) {
+                /* Try to save the desired settings to the default
+                 * settings file
+                 */
+                g_clear_error (error);
+                gnome_rr_config_sanitize (stored);
+                result = gnome_rr_config_save (stored, error);
+        	}
+    	
+    	} else {
+		g_set_error (error, GNOME_RR_ERROR, GNOME_RR_ERROR_UNKNOWN,
+				"X server does not support size requested");
+	}
+    	
+    	gnome_rr_config_free (stored);
+    	
+    	return result;
     }
-    else
-    {
-	return FALSE;
+    else {
+	    return FALSE;
     }
+
 }
 
 /*
diff -Nur -x '*.orig' -x '*~' gnome-desktop-2.29.90/libgnome-desktop/libgnomeui/gnome-rr-config.h gnome-desktop-2.29.90.new/libgnome-desktop/libgnomeui/gnome-rr-config.h
--- gnome-desktop-2.29.90/libgnome-desktop/libgnomeui/gnome-rr-config.h	2009-08-19 19:13:53.000000000 +0100
+++ gnome-desktop-2.29.90.new/libgnome-desktop/libgnomeui/gnome-rr-config.h	2010-02-14 23:58:02.013254194 +0000
@@ -123,6 +123,11 @@
 char *gnome_rr_config_get_backup_filename (void);
 char *gnome_rr_config_get_intended_filename (void);
 
+gboolean        ubuntu_gnome_rr_config_save_desired (GnomeRRConfig *configuration,
+                        GError **err);
+void            ubuntu_compute_virtual_size_for_configuration (GnomeRRConfig *config,
+                          int *ret_width, int *ret_height);
+          
 /* A utility function that isn't really in the spirit of this file, but I don't
  * don't know a better place for it.
  */