/* The pieces of Windows 8's User Account Control card that c8-askpass and
* c8-polkit share: the shield, the account picture, the card in the
* middle of a dimmed screen (a layer-shell overlay that keeps the
* keyboard) and the footer with Details and the answers. */
#ifndef C8_SHELL_UAC_H
#define C8_SHELL_UAC_H
#include <math.h>
#include <stdio.h>
#include <cairo.h>
#include <glib.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-wayland.h>
#include "askpass-wlr-layer-shell-unstable-v1-client-protocol.h"
#include "stealth.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* Who is asking: a file of /proc/PID, and the process's name */
static inline char *uac_proc_read(int pid, const char *what, gsize *len) {
char path[64], *data = NULL;
snprintf(path, sizeof(path), "/proc/%d/%s", pid, what);
if (!g_file_get_contents(path, &data, len, NULL)) {
return NULL;
}
return data;
}
static inline char *uac_proc_comm(int pid) {
char *comm = uac_proc_read(pid, "comm", NULL);
return comm ? g_strchomp(comm) : NULL;
}
#define UAC_CARD_W 540
#define UAC_FACE 64
/* The band across the top: blue when we know who is asking (sudo, ssh,
* polkit's actions), yellow when we don't, as UAC did for unknown
* publishers */
#define UAC_BAND_KNOWN 0x1e4e8c
#define UAC_BAND_UNKNOWN 0xf2c200
#define UAC_FOOTER 0xf0f0f0
/* A shield with a keyhole: white on the blue band, black on the yellow.
* data is a bool *, the band being blue. */
static inline void uac_draw_shield(struct c8ui_widget *w, cairo_t *cr,
void *data) {
bool known = *(bool *)data;
double x = w->rect.x, y = w->rect.y, s = w->rect.height;
cairo_save(cr);
cairo_translate(cr, x + (w->rect.width - s * 0.8) / 2, y);
cairo_scale(cr, s / 50.0, s / 50.0);
cairo_move_to(cr, 20, 2);
cairo_curve_to(cr, 27, 6, 33, 8, 40, 8);
cairo_curve_to(cr, 40, 30, 34, 42, 20, 48);
cairo_curve_to(cr, 6, 42, 0, 30, 0, 8);
cairo_curve_to(cr, 7, 8, 13, 6, 20, 2);
cairo_close_path(cr);
c8ui_set_source(cr, known ? 0xffffff : 0x000000, 1);
cairo_set_line_width(cr, 2.5);
cairo_stroke(cr);
cairo_arc(cr, 20, 21, 5, 0, 2 * M_PI);
cairo_fill(cr);
cairo_move_to(cr, 17.5, 23);
cairo_line_to(cr, 22.5, 23);
cairo_line_to(cr, 24, 34);
cairo_line_to(cr, 16, 34);
cairo_close_path(cr);
cairo_fill(cr);
cairo_restore(cr);
}
/* The account's picture, or a plain figure on the accent. data is a
* cairo_surface_t ** (the picture may change while shown). */
static inline void uac_draw_face(struct c8ui_widget *w, cairo_t *cr,
void *data) {
cairo_surface_t *face = *(cairo_surface_t **)data;
struct c8ui_rect *r = &w->rect;
if (face) {
c8ui_draw_surface_cover(cr, face, r->x, r->y, r->width, r->height);
return;
}
struct c8ui_colors c = c8ui_widget_colors(w);
c8ui_set_source(cr, c.accent, 1);
cairo_rectangle(cr, r->x, r->y, r->width, r->height);
cairo_fill(cr);
double cx = r->x + r->width / 2, cy = r->y + r->height / 2;
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_arc(cr, cx, cy - 8, 11, 0, 2 * M_PI);
cairo_fill(cr);
cairo_arc(cr, cx, cy + 26, 21, M_PI, 2 * M_PI);
cairo_fill(cr);
}
/* The white card with its band: shield, "User Account Control" and the
* question. Returns the card; *body_out is where the rest goes. */
static inline struct c8ui_widget *uac_card_new(bool *known,
const char *question, struct c8ui_widget **body_out) {
struct c8ui_widget *card = c8ui_box_new(C8UI_VERTICAL, 0);
card->fixed_width = UAC_CARD_W;
c8ui_widget_set_scheme(card, C8UI_SCHEME_LIGHT);
c8ui_widget_set_background(card, 0xffffff);
struct c8ui_widget *band = c8ui_box_new(C8UI_HORIZONTAL, 20);
c8ui_box_set_padding(band, 24, 18, 24, 20);
c8ui_widget_set_background(band, *known ? UAC_BAND_KNOWN :
UAC_BAND_UNKNOWN);
c8ui_widget_set_scheme(band, *known ? C8UI_SCHEME_DARK :
C8UI_SCHEME_LIGHT);
band->fill = true;
c8ui_widget_add(card, band);
c8ui_widget_add(band, c8ui_canvas_new(44, 54, uac_draw_shield, NULL,
known));
struct c8ui_widget *ask = c8ui_box_new(C8UI_VERTICAL, 6);
ask->expand = true;
c8ui_widget_add(band, ask);
c8ui_widget_add(ask, c8ui_label_new("User Account Control",
C8UI_TEXT_CAPTION));
c8ui_widget_add(ask, c8ui_label_new(question, C8UI_TEXT_TITLE));
struct c8ui_widget *body = c8ui_box_new(C8UI_VERTICAL, 8);
c8ui_box_set_padding(body, 24, 20, 24, 24);
body->fill = true;
c8ui_widget_add(card, body);
*body_out = body;
return card;
}
/* "Name: value" in the body, when there is a value */
static inline void uac_row(struct c8ui_widget *box, const char *name,
const char *value) {
if (value == NULL || value[0] == '\0') {
return;
}
char *text = g_strdup_printf("%s: %s", name, c8_stealth_show(value));
struct c8ui_widget *l = c8ui_label_new(text, C8UI_TEXT_BODY);
c8ui_label_set_max_lines(l, 3);
c8ui_widget_add(box, l);
g_free(text);
}
/* A grey line among the details */
static inline void uac_caption(struct c8ui_widget *box, const char *text) {
struct c8ui_widget *l = c8ui_label_new(text, C8UI_TEXT_CAPTION);
c8ui_label_set_color(l, C8UI_COLOR_SECONDARY);
c8ui_label_set_max_lines(l, 4);
c8ui_widget_add(box, l);
}
/* The grey footer: Details (showing or hiding details) on the left, the
* answers on the right. no_fn NULL leaves No out. */
static inline struct c8ui_widget *uac_footer(struct c8ui_widget *card,
const char *yes, c8ui_action_fn details_fn, c8ui_action_fn yes_fn,
c8ui_action_fn no_fn, void *data, struct c8ui_widget **yes_out) {
struct c8ui_widget *foot = c8ui_box_new(C8UI_HORIZONTAL, 12);
c8ui_box_set_padding(foot, 24, 14, 24, 14);
c8ui_widget_set_background(foot, UAC_FOOTER);
foot->fill = true;
c8ui_widget_add(card, foot);
struct c8ui_widget *more = c8ui_link_new("Details", details_fn, data);
more->align = C8UI_ALIGN_CENTER;
c8ui_widget_add(foot, more);
struct c8ui_widget *gap = c8ui_spacer_new(0, 0);
gap->expand = true;
c8ui_widget_add(foot, gap);
struct c8ui_widget *ok = c8ui_button_new(yes, yes_fn, data);
c8ui_button_set_primary(ok, true);
c8ui_widget_add(foot, ok);
if (no_fn) {
c8ui_widget_add(foot, c8ui_button_new("No", no_fn, data));
}
if (yes_out) {
*yes_out = ok;
}
return foot;
}
/* The card in the middle of the screen */
static inline struct c8ui_widget *uac_cover(struct c8ui_widget *card) {
struct c8ui_widget *cover = c8ui_box_new(C8UI_VERTICAL, 0);
struct c8ui_widget *above = c8ui_spacer_new(0, 0);
above->expand = true;
c8ui_widget_add(cover, above);
struct c8ui_widget *middle = c8ui_box_new(C8UI_HORIZONTAL, 0);
middle->fill = true;
c8ui_widget_add(cover, middle);
struct c8ui_widget *left = c8ui_spacer_new(0, 0);
left->expand = true;
c8ui_widget_add(middle, left);
c8ui_widget_add(middle, card);
struct c8ui_widget *right = c8ui_spacer_new(0, 0);
right->expand = true;
c8ui_widget_add(middle, right);
struct c8ui_widget *below = c8ui_spacer_new(0, 0);
below->expand = true;
c8ui_widget_add(cover, below);
return cover;
}
/* The dimmed overlay over the whole screen, keeping the keyboard. The
* listener's data is the window; configure it with
* uac_layer_configure. */
static inline void uac_layer_configure(void *data,
struct zwlr_layer_surface_v1 *layer, uint32_t serial, uint32_t width,
uint32_t height) {
zwlr_layer_surface_v1_ack_configure(layer, serial);
c8ui_window_configure(data, width, height);
}
static inline struct zwlr_layer_surface_v1 *uac_overlay_new(
struct zwlr_layer_shell_v1 *shell, struct c8ui_window *win,
const char *name_space, const struct zwlr_layer_surface_v1_listener *l) {
c8ui_window_set_background(win, 0x000000, 0.6);
struct wl_surface *surface = c8ui_window_surface(win);
struct zwlr_layer_surface_v1 *layer = zwlr_layer_shell_v1_get_layer_surface(
shell, surface, NULL, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, name_space);
zwlr_layer_surface_v1_add_listener(layer, l, win);
zwlr_layer_surface_v1_set_anchor(layer,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_exclusive_zone(layer, -1);
zwlr_layer_surface_v1_set_keyboard_interactivity(layer, 1);
wl_surface_commit(surface);
return layer;
}
#endif