/* setbg * Copyright (C) 2008 JoaquĆ­n Aguirrezabalaga * * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #define BG_DCONF_KEY "/org/gnome/desktop/background/picture-uri" static void set_bg(const gchar *file) { gint winx,winy,winw,winh,wind; GdkPixbuf *pb, *pbs; GdkDisplay *disp; GdkWindow *root; GdkPixmap *pixmap; Display *xdisp; Pixmap xpm; XineramaScreenInfo *sinfo; int nscreens; int i; Atom prop_root, prop_esetroot, type; int format; unsigned long length, after; unsigned char *data_root, *data_esetroot; Window xwin; if (file == NULL) { g_error("ERROR: no file to set as background."); return; } disp = gdk_display_get_default(); xdisp = GDK_DISPLAY_XDISPLAY(disp); xwin = DefaultRootWindow(xdisp); root = gdk_screen_get_root_window(gdk_display_get_default_screen(disp)); gdk_window_get_geometry(root, &winx, &winy, &winw, &winh, &wind); pixmap = gdk_pixmap_new(root, winw, winh, -1); GFile *gfile = g_file_new_for_uri(file); GFileInputStream *stream = g_file_read(gfile, NULL, NULL); pb = gdk_pixbuf_new_from_stream(G_INPUT_STREAM(stream), NULL, NULL); sinfo = XineramaQueryScreens(xdisp, &nscreens); if (nscreens > 0) { for (i = 0; i < nscreens; i++) { pbs = gdk_pixbuf_scale_simple(pb, sinfo[i].width, sinfo[i].height, GDK_INTERP_BILINEAR); gdk_draw_pixbuf(pixmap, None, pbs, 0, 0, sinfo[i].x_org, sinfo[i].y_org, -1, -1, GDK_RGB_DITHER_NONE, 0, 0); g_object_unref(pbs); } } else { pbs = gdk_pixbuf_scale_simple(pb, winw, winh, GDK_INTERP_BILINEAR); gdk_draw_pixbuf(pixmap, None, pbs, 0, 0, 0, 0, -1, -1, GDK_RGB_DITHER_NONE, 0, 0); g_object_unref(pbs); } XFree(sinfo); g_object_unref(pb); /* Info on this from http://www.eterm.org/docs/view.php?doc=ref#trans */ /* render it to the pixmap */ xpm = GDK_PIXMAP_XID(pixmap); /* First check to see if the properties already exist, and if they are equal */ prop_root = XInternAtom(xdisp, "_XROOTPMAP_ID", True); prop_esetroot = XInternAtom(xdisp, "ESETROOT_PMAP_ID", True); if (prop_root != None && prop_esetroot != None) { XGetWindowProperty(xdisp, xwin, prop_root, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after, &data_root); if (type == XA_PIXMAP) { XGetWindowProperty(xdisp, xwin, prop_esetroot, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after, &data_esetroot); if (data_root && data_esetroot && type == XA_PIXMAP && *((Pixmap *) data_root) == *((Pixmap *) data_esetroot)) { /* It's safe. Kill the pixmap. */ XKillClient(xdisp, *((Pixmap *) data_root)); } } } /* This will locate the property, creating it if it doesn't exist */ prop_root = XInternAtom(xdisp, "_XROOTPMAP_ID", False); prop_esetroot = XInternAtom(xdisp, "ESETROOT_PMAP_ID", False); if (prop_root == None || prop_esetroot == None) { g_error("ERROR: BG set could not make atoms."); return; } XChangeProperty(xdisp, xwin, prop_root, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &xpm, 1); XChangeProperty(xdisp, xwin, prop_esetroot, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &xpm, 1); XSetCloseDownMode(xdisp, RetainPermanent); gdk_window_set_back_pixmap(root, pixmap, FALSE); g_object_unref(pixmap); gdk_window_clear(root); gdk_display_close(disp); } static gchar* get_stored_bg() { DConfClient *client; gchar *file; GVariant *value; client = dconf_client_new(); value = dconf_client_read(client, BG_DCONF_KEY); file = g_strdup(g_variant_get_string(value, NULL)); g_variant_unref(value); g_object_unref(client); return file; } static gboolean set_stored_bg(const gchar *file) { DConfClient *client; gboolean res; GVariant *value; client = dconf_client_new(); value = g_variant_new_string(file); res = dconf_client_write_fast(client, BG_DCONF_KEY , value, NULL); g_object_unref(client); return res; } static gchar* get_abs_file(const char* arg) { gchar *file; gchar *uri; GFile *gfile; file = g_strdup(arg); gfile = g_file_new_for_commandline_arg(arg); if (g_path_is_absolute(arg)) { g_free(file); file = g_file_get_path(gfile); } uri = g_file_get_uri(gfile); g_object_unref(gfile); if (g_file_test(file, G_FILE_TEST_EXISTS)) { g_free(file); return uri; } else { g_free(file); return NULL; } } int main(int argc, gchar** argv) { gchar *file; gdk_init(&argc, &argv); if (argc > 1) { file = get_abs_file(argv[1]); if (file == NULL) { g_error("File %s doesn't exist.\n", argv[1]); exit(1); } set_stored_bg(file); } else { file = get_stored_bg(); if (file == NULL) { g_error("Could not get stored bg.\n"); exit(2); } } g_printf("Setting %s as background.\n", file); set_bg(file); g_free(file); exit(0); }