[Xfce4-commits] [apps/xfce4-screensaver] 394/425: Fix and simplify user avatar loading
noreply at xfce.org
noreply at xfce.org
Mon Oct 15 01:54:01 CEST 2018
This is an automated email from the git hooks/post-receive script.
b l u e s a b r e p u s h e d a c o m m i t t o b r a n c h m a s t e r
in repository apps/xfce4-screensaver.
commit 6dff3c77610d689994968b9fb14a58605046526c
Author: Simon Steinbeiss <simon.steinbeiss at elfenbeinturm.at>
Date: Thu Oct 11 22:35:10 2018 +0200
Fix and simplify user avatar loading
The old code did several things we don't want or need anymore.
1) Lots of cairo drawing to achieve a rounded rectangle as border for
the user avatar.
2) Lots of file checks, which we don't really need as
gdk_pixbuf_new_from_file gives us a proper error message if the image
cannot be loaded. The old code contained no workarounds for when an
image file didn't comply, so we may as well rely on gdk to decide.
---
src/gs-lock-plug.c | 312 ++---------------------------------------------------
1 file changed, 6 insertions(+), 306 deletions(-)
diff --git a/src/gs-lock-plug.c b/src/gs-lock-plug.c
index 2dcac57..910aa64 100644
--- a/src/gs-lock-plug.c
+++ b/src/gs-lock-plug.c
@@ -625,329 +625,29 @@ gs_lock_plug_run (GSLockPlug *plug)
return ri.response_id;
}
-
-static cairo_surface_t *
-surface_from_pixbuf (GdkPixbuf *pixbuf)
-{
- cairo_surface_t *surface;
- cairo_t *cr;
-
- surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
- CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
- gdk_pixbuf_get_width (pixbuf),
- gdk_pixbuf_get_height (pixbuf));
- cr = cairo_create (surface);
- gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
- cairo_paint (cr);
- cairo_destroy (cr);
-
- return surface;
-}
-
-static void
-rounded_rectangle (cairo_t *cr,
- gdouble aspect,
- gdouble x,
- gdouble y,
- gdouble corner_radius,
- gdouble width,
- gdouble height)
-{
- gdouble radius;
- gdouble degrees;
-
- radius = corner_radius / aspect;
- degrees = G_PI / 180.0;
-
- cairo_new_sub_path (cr);
- cairo_arc (cr,
- x + width - radius,
- y + radius,
- radius,
- -90 * degrees,
- 0 * degrees);
- cairo_arc (cr,
- x + width - radius,
- y + height - radius,
- radius,
- 0 * degrees,
- 90 * degrees);
- cairo_arc (cr,
- x + radius,
- y + height - radius,
- radius,
- 90 * degrees,
- 180 * degrees);
- cairo_arc (cr,
- x + radius,
- y + radius,
- radius,
- 180 * degrees,
- 270 * degrees);
- cairo_close_path (cr);
-}
-
-/* copied from gnome-screensaver 3.x */
-
-/**
- * go_cairo_convert_data_to_pixbuf:
- * @src: a pointer to pixel data in cairo format
- * @dst: a pointer to pixel data in pixbuf format
- * @width: image width
- * @height: image height
- * @rowstride: data rowstride
- *
- * Converts the pixel data stored in @src in CAIRO_FORMAT_ARGB32 cairo format
- * to GDK_COLORSPACE_RGB pixbuf format and move them
- * to @dst. If @src == @dst, pixel are converted in place.
- **/
-
-static void
-go_cairo_convert_data_to_pixbuf (unsigned char *dst,
- unsigned char const *src,
- int width,
- int height,
- int rowstride)
-{
- int i,j;
- unsigned int t;
- unsigned char a, b, c;
-
- g_return_if_fail (dst != NULL);
-
-#define MULT(d,c,a,t) G_STMT_START { t = (a)? c * 255 / a: 0; d = t;} G_STMT_END
-
- if (src == dst || src == NULL) {
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- MULT(a, dst[2], dst[3], t);
- MULT(b, dst[1], dst[3], t);
- MULT(c, dst[0], dst[3], t);
- dst[0] = a;
- dst[1] = b;
- dst[2] = c;
-#else
- MULT(a, dst[1], dst[0], t);
- MULT(b, dst[2], dst[0], t);
- MULT(c, dst[3], dst[0], t);
- dst[3] = dst[0];
- dst[0] = a;
- dst[1] = b;
- dst[2] = c;
-#endif
- dst += 4;
- }
- dst += rowstride - width * 4;
- }
- } else {
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- MULT(dst[0], src[2], src[3], t);
- MULT(dst[1], src[1], src[3], t);
- MULT(dst[2], src[0], src[3], t);
- dst[3] = src[3];
-#else
- MULT(dst[0], src[1], src[0], t);
- MULT(dst[1], src[2], src[0], t);
- MULT(dst[2], src[3], src[0], t);
- dst[3] = src[0];
-#endif
- src += 4;
- dst += 4;
- }
- src += rowstride - width * 4;
- dst += rowstride - width * 4;
- }
- }
-#undef MULT
-}
-
-static void
-cairo_to_pixbuf (guint8 *src_data,
- GdkPixbuf *dst_pixbuf)
-{
- unsigned char *src;
- unsigned char *dst;
- guint w;
- guint h;
- guint rowstride;
-
- w = gdk_pixbuf_get_width (dst_pixbuf);
- h = gdk_pixbuf_get_height (dst_pixbuf);
- rowstride = gdk_pixbuf_get_rowstride (dst_pixbuf);
-
- dst = gdk_pixbuf_get_pixels (dst_pixbuf);
- src = src_data;
-
- go_cairo_convert_data_to_pixbuf (dst, src, w, h, rowstride);
-}
-
-static GdkPixbuf *
-frame_pixbuf (GdkPixbuf *source)
-{
- GdkPixbuf *dest;
- cairo_t *cr;
- cairo_surface_t *surface;
- guint w;
- guint h;
- guint rowstride;
- int frame_width;
- double radius;
- guint8 *data;
-
- frame_width = 5;
-
- w = gdk_pixbuf_get_width (source) + frame_width * 2;
- h = gdk_pixbuf_get_height (source) + frame_width * 2;
- radius = w / 10;
-
- dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
- TRUE,
- 8,
- w,
- h);
- rowstride = gdk_pixbuf_get_rowstride (dest);
-
-
- data = g_new0 (guint8, h * rowstride);
-
- surface = cairo_image_surface_create_for_data (data,
- CAIRO_FORMAT_ARGB32,
- w,
- h,
- rowstride);
- cr = cairo_create (surface);
- cairo_surface_destroy (surface);
-
- /* set up image */
- cairo_rectangle (cr, 0, 0, w, h);
- cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
- cairo_fill (cr);
-
- rounded_rectangle (cr,
- 1.0,
- frame_width + 0.5,
- frame_width + 0.5,
- radius,
- w - frame_width * 2 - 1,
- h - frame_width * 2 - 1);
- cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.3);
- cairo_fill_preserve (cr);
-
- surface = surface_from_pixbuf (source);
- cairo_set_source_surface (cr, surface, frame_width, frame_width);
- cairo_fill (cr);
- cairo_surface_destroy (surface);
-
- cairo_to_pixbuf (data, dest);
-
- cairo_destroy (cr);
- g_free (data);
-
- return dest;
-}
-
-/* end copied from gdm-user.c */
-
-static void
-image_set_from_pixbuf (GtkImage *image,
- GdkPixbuf *source)
-{
- GdkPixbuf *pixbuf;
-
- pixbuf = frame_pixbuf (source);
- gtk_image_set_from_pixbuf (image, pixbuf);
- g_object_unref (pixbuf);
-}
-
-static gboolean
-check_user_file (const gchar *filename,
- uid_t user,
- gssize max_file_size,
- gboolean relax_group,
- gboolean relax_other)
-{
- struct stat fileinfo;
-
- if (max_file_size < 0)
- {
- max_file_size = G_MAXSIZE;
- }
-
- /* Exists/Readable? */
- if (g_stat (filename, &fileinfo) < 0)
- {
- return FALSE;
- }
-
- /* Is a regular file */
- if (G_UNLIKELY (!S_ISREG (fileinfo.st_mode)))
- {
- return FALSE;
- }
-
- /* Owned by user? */
- if (G_UNLIKELY (fileinfo.st_uid != user))
- {
- return FALSE;
- }
-
- /* Group not writable or relax_group? */
- if (G_UNLIKELY ((fileinfo.st_mode & S_IWGRP) == S_IWGRP && !relax_group))
- {
- return FALSE;
- }
-
- /* Other not writable or relax_other? */
- if (G_UNLIKELY ((fileinfo.st_mode & S_IWOTH) == S_IWOTH && !relax_other))
- {
- return FALSE;
- }
-
- /* Size is kosher? */
- if (G_UNLIKELY (fileinfo.st_size > max_file_size))
- {
- return FALSE;
- }
-
- return TRUE;
-}
-
static gboolean
set_face_image (GSLockPlug *plug)
{
- GdkPixbuf *pixbuf;
+ GdkPixbuf *pixbuf = NULL;
const char *homedir;
char *path;
- int icon_size = 96;
- gsize user_max_file = 65536;
- uid_t uid;
+ GError *error = NULL;
homedir = g_get_home_dir ();
- uid = getuid ();
-
path = g_build_filename (homedir, ".face", NULL);
- pixbuf = NULL;
- if (check_user_file (path, uid, user_max_file, 0, 0))
- {
- pixbuf = gdk_pixbuf_new_from_file_at_size (path,
- icon_size,
- icon_size,
- NULL);
- }
+ pixbuf = gdk_pixbuf_new_from_file_at_scale (path, 80, 80, FALSE, &error);
g_free (path);
if (pixbuf == NULL)
{
+ g_warning ("Could not load the user avatar: %s", error->message);
+ g_error_free (error);
return FALSE;
}
- image_set_from_pixbuf (GTK_IMAGE (plug->priv->auth_face_image), pixbuf);
-
+ gtk_image_set_from_pixbuf (GTK_IMAGE (plug->priv->auth_face_image), pixbuf);
g_object_unref (pixbuf);
return TRUE;
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the Xfce4-commits
mailing list