/* Pictures from gdk-pixbuf as cairo images, and the account's own one.
* In a header because c8-shell, c8-lock, c8-askpass and c8-polkit all want them. */
#ifndef C8_SHELL_PIXELS_H
#define C8_SHELL_PIXELS_H
#include <pwd.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib.h>
#include <c8ui/c8ui.h>
#include "stealth.h"
/* Takes the pixbuf (NULL is fine); NULL when it isn't 8 bits a sample */
static inline cairo_surface_t *image_from_pixbuf(GdkPixbuf *pb) {
cairo_surface_t *img = NULL;
if (pb && gdk_pixbuf_get_bits_per_sample(pb) == 8) {
img = c8ui_surface_from_pixels(gdk_pixbuf_read_pixels(pb),
gdk_pixbuf_get_width(pb), gdk_pixbuf_get_height(pb),
gdk_pixbuf_get_rowstride(pb), gdk_pixbuf_get_n_channels(pb));
}
if (pb) {
g_object_unref(pb);
}
return img;
}
/* An account: login name, real name (the login name without one), and
* its picture as SDDM finds it, at most size px, or NULL. In stealth
* mode our own shows the persona's name and no picture; the login name
* stays real, as it's what PAM is asked about. */
static inline void account_read_for(const struct passwd *pw, char **user,
char **real_name, cairo_surface_t **face, int size) {
*user = g_strdup(pw ? pw->pw_name : g_get_user_name());
char *gecos = pw && pw->pw_gecos ? g_strdup(pw->pw_gecos) : NULL;
if (gecos) {
char *comma = strchr(gecos, ',');
if (comma) {
*comma = '\0';
}
}
*real_name = gecos && gecos[0] ? gecos : (g_free(gecos), g_strdup(*user));
if (c8_stealth() && (pw == NULL || pw->pw_uid == getuid())) {
g_free(*real_name);
*real_name = g_strdup(c8_stealth_name());
*face = NULL;
return;
}
const char *home = pw && pw->pw_dir ? pw->pw_dir : g_get_home_dir();
char *paths[] = {
g_build_filename(home, ".face.icon", NULL),
g_build_filename(home, ".face", NULL),
g_build_filename("/var/lib/AccountsService/icons", *user, NULL),
};
*face = NULL;
for (int i = 0; i < 3; i++) {
if (*face == NULL) {
*face = image_from_pixbuf(gdk_pixbuf_new_from_file_at_scale(
paths[i], size, size, TRUE, NULL));
}
g_free(paths[i]);
}
}
/* Ours */
static inline void account_read(char **user, char **real_name,
cairo_surface_t **face, int size) {
account_read_for(getpwuid(getuid()), user, real_name, face, size);
}
#endif