Qball's Weblog

MultiMonitor Background settings.

Tags General 

I got sick and tired of no program being able to set desktop background on multimonitor setups.
It either stretch it over the 2 desktops, or whatever, because most tools like xsetbg and feh assume it one screen.

This tools sets one image scaled to the size of each monitor. Clipping the image (monitor is then centered on the image) is optional.
It will never try to scale up the image.

?View Code CPP
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
/* multidesktopbackground.vala
 *
 * Copyright (C) 2011 Qball Cow <qball @gmpclient.org>
 *
 * 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 3 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, see <http: //www.gnu.org/licenses/>.
 *
 * Author:
 * 	Qball Cow </qball><qball @gmpclient.org>
 */
using Gtk;
 
/**
 * Set the actual background on the X server
 */
static void set_background(Gdk.Window root_window, Gdk.Pixbuf background_pb)
{
		Gdk.Pixmap pixmap;
		Gdk.Bitmap bitmap;
		// Grab server, without this, it won't work.
		Gdk.x11_grab_server();
 
		// Set background pixmap.
		Gdk.pixbuf_render_pixmap_and_mask(background_pb,out pixmap, out bitmap, );
		root_window.set_back_pixmap(pixmap, false);
 
		// Otherwise the background will only update after being exposed.
		/// @todo can I trigger an expose event on GdkWindow?
		root_window.draw_pixbuf(null,
				background_pb, ,,,, 
				background_pb.get_width(), background_pb.get_height(),
				Gdk.RgbDither.NONE, ,);
 
		// Release
		Gdk.x11_ungrab_server();
}
 
 
/**
 * Option parser
 */
string  clipping = null;
const GLib.OptionEntry[] entries = {
        {"clipping", 'c', , GLib.OptionArg.STRING, ref clipping, "Enable clipping", null},
        {null}
};
 
/**
 * Main program
 */
static int main (string[] argv)
{
	bool clip = false;
 
	/**
	 * Parse command line list
	 */
    GLib.OptionContext og = new GLib.OptionContext("Evo");
    og.add_main_entries(entries,null);
	try{
		og.parse(ref argv);
	}catch (Error e) {
		GLib.error("Failed to parse command line options: %s\n", e.message);
	}
	/**
	 * Parse clipping
	 */	
	if(clipping != null) {
		if(clipping.down() == "true") {
			clip = true;
		}
	}
 
	/**
	 * Get filename
	 */
	if(argv.length  != 2) {
		GLib.error("Failed parsing commandlines");
	}
	string file = argv[1];
 
	// Initialize GTK.
	Gtk.init(ref argv);
 
	// Get display
	Gdk.Display display = Gdk.Display.get_default();
 
	// Get number of screens.
	int num_screens = display.get_n_screens();
 
	// Walk the screens.
	for(int screen = ; screen < num_screens ; screen++)
	{
		Gdk.Screen gscreen = display.get_screen(screen);
		Gdk.Window root_window = gscreen.get_root_window();
 
		int rw_width = gscreen.get_width();
		int rw_height = gscreen.get_height();
 
		// Create background pixmap.
		var background_pb = new Gdk.Pixbuf(Gdk.Colorspace.RGB,false,8, rw_width, rw_height);
 
 
		// Iterate over all the monitors and draw the image there.
 
		// Get number of monitors.
		int num_monitors = gscreen.get_n_monitors();
		for(int monitor = ; monitor < num_monitors; monitor++)
		{
			Gdk.Rectangle rectangle;
			// get monitor size
			gscreen.get_monitor_geometry(monitor, out rectangle);
 
			// Gdk.Pixbuf loading and drawing inside.
			try
			{
				Gdk.Pixbuf? pb = new Gdk.Pixbuf.from_file(file); 
				// Get size
				int pb_width = pb.get_width();
				int pb_height = pb.get_height();
				// Scaling
				double x_scale = pb_width/(double)rectangle.width;
				double y_scale = pb_height/(double)rectangle.height;
 
				if(x_scale >= 1 && y_scale >= 1 && clip) 
				{
					if(x_scale > y_scale)
					{
						int new_width = (int)(pb_width/y_scale);
						int new_height = (int)(pb_height/y_scale);
						pb = pb.scale_simple(new_width, new_height, Gdk.InterpType.HYPER); 
 
						int x_offset = (new_width - rectangle.width)/2;
						int y_offset = (new_height- rectangle.height)/2;
						pb.copy_area(
								x_offset,y_offset,
								new_width-x_offset*2,new_height-y_offset*2,
								background_pb,
								rectangle.x, rectangle.y
								);
					} else {
						int new_width = (int)(pb_width/x_scale);
						int new_height = (int)(pb_height/x_scale);
						pb = pb.scale_simple(new_width, new_height, Gdk.InterpType.HYPER); 
 
						int x_offset = (new_width - rectangle.width)/2;
						int y_offset = (new_height- rectangle.height)/2;
						pb.copy_area(
								x_offset,y_offset,
								new_width-x_offset*2,new_height-y_offset*2,
								background_pb,
								rectangle.x,rectangle.y
								);
					}
				}
				else if(x_scale > y_scale)
				{
					int new_width = (int)(pb_width/x_scale);
					int new_height = (int)(pb_height/x_scale);
					pb = pb.scale_simple(new_width, new_height, Gdk.InterpType.HYPER); 
 
					pb.copy_area(,,
							new_width, new_height,
							background_pb,
							rectangle.x + (rectangle.width-new_width)/2,
							rectangle.y + (rectangle.height-new_height)/2
							);
				}
				else 
				{
					int new_width = (int)(pb_width/y_scale);
					int new_height = (int)(pb_height/y_scale);
					pb = pb.scale_simple(new_width, new_height, Gdk.InterpType.HYPER); 
 
					pb.copy_area(,,
							new_width, new_height,
							background_pb,
							rectangle.x + (rectangle.width-new_width)/2,
							rectangle.y + (rectangle.height-new_height)/2
							);
 
				}
 
			} catch (Error e) {
				GLib.error("Failed loading pixbuf: %s\n", e.message);
			}
 
		}
		set_background(root_window, background_pb);
	}
 
	return 1;
}
</qball>

compile with:

?View Code BASH
1
valac --pkg=gtk+-2.0  MultiDesktopBackground.vala  --pkg=gdk-x11-2.0 --pkg=x11

edit: updated it a bit and put in git: here.