个人工具

UbuntuHelp:RotateWallpapers

来自Ubuntu中文

跳转至: 导航, 搜索

<<Include(Tag/Deletion)>> This program was custom made by a developer in the nautilus channel. Thanks gicmo! change-bg rotates your wallpaper (jpg/png) in a gnome enviornment over a specified amount of time.

Dependencies

Install both of the following. libgnomevfs2-dev build-essential

Source Code

1.Copy this source code into a blank text document. 2.Save as change-bg.c

/* 

   Compile with:
   gcc -Wall -o change-bg `pkg-config --cflags --libs gnome-vfs-2.0 gconf-2.0` change-bg.c
   
   Copyright (C) 2005, Christian Kellner

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.


*/

#include <libgnomevfs/gnome-vfs.h>
#include <gconf/gconf-client.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>

GConfClient  *gcc = NULL;
char         *text_uri = NULL;
GnomeVFSURI  *uri;
GList        *files = NULL;
GList        *iter = NULL;
static gboolean
change_picture (gpointer data)
{
	GnomeVFSURI *nuri;
	gboolean result;
	char *path;

	if (gcc == NULL) {
		gcc = gconf_client_get_default ();
	}
	path = (char *) iter->data;
	nuri = gnome_vfs_uri_append_string (uri, path);
	
	path = gnome_vfs_uri_to_string (nuri, GNOME_VFS_URI_HIDE_USER_NAME |
					GNOME_VFS_URI_HIDE_PASSWORD |
					GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD |
					GNOME_VFS_URI_HIDE_FRAGMENT_IDENTIFIER |
					GNOME_VFS_URI_HIDE_HOST_PORT |
					GNOME_VFS_URI_HIDE_HOST_NAME);

	fprintf (stdout, "Setting bg to %s\n", path);
	
	result = gconf_client_set_string (gcc,
					  "/desktop/gnome/background/picture_filename",
					  path,
					  NULL);

	g_free (path);

	if (result == FALSE) {
		fprintf (stderr, "Error setting %s as bg\n", path);
	}

	if (iter->next != NULL) {
		iter = iter->next;
	} else {
		iter = files;
	}
	
	return TRUE;
}

static gboolean
visit_files (const gchar *rel_path,
	     GnomeVFSFileInfo *info,
	     gboolean recursing_will_loop,
	     gpointer data,
	     gboolean *recurse)
{
	GList **files = (GList **) data;
	*recurse = TRUE;

	if (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE &&
	    (g_str_equal (info->mime_type, "image/png") ||
	     g_str_equal (info->mime_type, "image/jpeg"))) {
		
		*files = g_list_prepend (*files, g_strdup (rel_path));
		fprintf (stdout, "Adding %s\n", rel_path);
	}
	
	return !recursing_will_loop;
}

int
main (int argc, char **argv)
{
	GnomeVFSResult  result;
	GMainLoop      *ml;
	int             mins;
		
	if (argc != 3) {
		fprintf (stderr, "Usage: %s <seconds> <uri>\n", argv[0]);
		return 1;
	}

	if (! gnome_vfs_init ()) {
		fprintf (stderr, "Cannot initialize gnome-vfs.\n");
		return 1;
	}

	text_uri = gnome_vfs_make_uri_from_shell_arg (argv[2]);

	mins = atoi (argv[1]);
	
	if (text_uri == NULL || mins == 0) {
		fprintf (stderr, "Usage: %s <seconds> 0> <uri>\n", argv[0]);
		return 1;
	}
	
	fprintf (stdout, "Generating file list ...\n");
	files = NULL;
	result = gnome_vfs_directory_visit (text_uri,
					    GNOME_VFS_FILE_INFO_GET_MIME_TYPE | 
					    GNOME_VFS_FILE_INFO_FORCE_FAST_MIME_TYPE,
					    GNOME_VFS_DIRECTORY_VISIT_LOOPCHECK |
					    GNOME_VFS_DIRECTORY_VISIT_SAMEFS,
					    visit_files,
					    &files);

	if (result != GNOME_VFS_OK) {
		fprintf (stderr, "Error: %s\n",
			 gnome_vfs_result_to_string (result));
		return 1;
	}
	
	uri = gnome_vfs_uri_new (text_uri);
	
	iter = files;
	
	g_timeout_add (1000 * mins, change_picture, NULL);
	
	ml = g_main_loop_new (NULL, FALSE);

	fprintf (stdout, "Going into main loop (timeout: %d seconds)\n", mins);
	g_main_loop_run (ml);
	
	return 0;
}

Compiling

Change directories to where change-bg.c is located and run the following command.

gcc -Wall -o change-bg `pkg-config --cflags --libs gnome-vfs-2.0 gconf-2.0` change-bg.c

Using

./change-bg <seconds> <location of wallpapers>

Doing it with a Bash script

As an alternative you could save the following script and call it from a startup program [Sessions]. Modify the WALLPAPER_LOCATION to the place where you store your wallpaper images.

#!/bin/bash
# change_wp [seconds]	  Changes the wallpaper at a given interval of seconds.
# example: change_wp 3600	 change the wallpaper every hour.
# If you do not supply any time it just changes the wallpaper and exits.
# Copyleft (C) 2007, 	Albert Bicchi 	[email protected]
WALLPAPER_LOCATION="/home/albert/wallpapers"
find "${WALLPAPER_LOCATION}" -iname '*.jp*g' -o -iname '*.png' > /tmp/wallpapers
TOTAL=`cat /tmp/wallpapers | wc -l`
while [ 1 ]
do
	((LINE=RANDOM%TOTAL+1))
	WALLPAPER=`sed -n "$LINE"p /tmp/wallpapers`
	gconftool -t str -s /desktop/gnome/background/picture_filename "${WALLPAPER}"
	if [ "$1" = "" ]; then
		exit 0
	fi
	sleep "$1s"
done