mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-17 01:00:41 +01:00
Remote assistance (experimental) and unified host window
Like the official client: share a device ID + verification code; the other person connects from 开始协助. Guest identity before sign-in, account identity after (same share ID). One GTK window: 本设备 (allow switch, ID/code with copy, verification modes incl. custom code, rotation schedule, copy & share) and 账号 (QR before sign-in, masked account + sign out after). Logged-in hosts keep the window hidden; `uuremote-server show` / the desktop launcher opens it. App icon and .desktop entry in contrib/desktop. Protocol from the official Windows lite host; guest flow, wrong-code rejection, rotation, switching off, sign-in/sign-out verified with the official Mac viewer. Signed-in assistance from another account is not yet verified. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
03f611811c
commit
94039c092a
15 files changed
+1606
-399
No files matched your search
@@ -0,0 +1,576 @@
|
||||
// GTK3 window for guest remote assistance (`uuremote-server assist`). Layout follows the official
|
||||
// "本设备" card (allow switch, device ID, temporary code with show/hide, copy & share) without any
|
||||
// NetEase assets. GTK runs on its own thread; Rust pushes state through g_idle_add and polls the
|
||||
// user's commands. The code is only drawn in this local window and copied on explicit request.
|
||||
#include <gtk/gtk.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *CSS =
|
||||
"window { background-color: #f5f6f8; }\n"
|
||||
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
||||
".card { background-color: #ffffff; border-radius: 10px; border: 1px solid #e5e7eb; }\n"
|
||||
".heading { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n"
|
||||
".key { font-size: 14px; color: #4b5563; }\n"
|
||||
".big { font-size: 32px; font-weight: 800; color: #111111; }\n"
|
||||
".note { font-size: 13px; color: #9ca3af; }\n"
|
||||
".hint { font-size: 13px; color: #6b7280; }\n"
|
||||
".footer { font-size: 12px; color: #9ca3af; }\n"
|
||||
"button.flat { padding: 4px 8px; }\n"
|
||||
"button.share { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db;"
|
||||
" border-radius: 6px; padding: 6px 14px; font-size: 14px; }\n"
|
||||
"separator { background-color: #e5e7eb; min-height: 1px; }\n"
|
||||
".user { font-size: 22px; font-weight: 700; color: #111111; }\n"
|
||||
".status { font-size: 13px; color: #2f6bff; }\n"
|
||||
".status.ok { color: #16a34a; }\n"
|
||||
".status.error { color: #dc2626; }\n";
|
||||
|
||||
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8 };
|
||||
enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 };
|
||||
|
||||
typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State;
|
||||
typedef struct { int cmd; char arg[40]; } Cmd;
|
||||
|
||||
// Small FIFO so quick successive UI actions (e.g. set code, then mode) are all delivered.
|
||||
static Cmd queue[16];
|
||||
static int q_head, q_len;
|
||||
static pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static atomic_int closed;
|
||||
static pthread_t ui_thread;
|
||||
static int ui_started, init_state; // 0 pending, 1 ok, -1 failed
|
||||
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
||||
static GtkWidget *window, *allow_switch, *id_label, *code_label, *eye_button, *share_button, *hint_label, *verify_combo, *note_label;
|
||||
static GtkWidget *refresh_items[5], *footer_label;
|
||||
// Account card: QR login before sign-in, user info (+ logout) after.
|
||||
static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label;
|
||||
static int show_assist = 1, show_account;
|
||||
static int cur_verify, cur_refresh, cur_has_custom;
|
||||
static char current_id[64], current_code[64];
|
||||
static int code_visible = 1, suppress_switch, start_hidden, close_hides;
|
||||
|
||||
static GtkWidget *label(const char *text, const char *cls) {
|
||||
GtkWidget *l = gtk_label_new(text);
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
|
||||
gtk_widget_set_halign(l, GTK_ALIGN_START);
|
||||
return l;
|
||||
}
|
||||
|
||||
static void post_arg(int cmd, const char *arg) {
|
||||
pthread_mutex_lock(&q_lock);
|
||||
if (q_len < 16) {
|
||||
Cmd *c = &queue[(q_head + q_len) % 16];
|
||||
c->cmd = cmd; g_strlcpy(c->arg, arg ? arg : "", sizeof c->arg); q_len++;
|
||||
}
|
||||
pthread_mutex_unlock(&q_lock);
|
||||
}
|
||||
static void post(int cmd) { post_arg(cmd, NULL); }
|
||||
static void post_int(int cmd, int v) { char b[8]; g_snprintf(b, sizeof b, "%d", v); post_arg(cmd, b); }
|
||||
|
||||
// 8-16 ASCII letters/digits including both, as the official custom code dialog requires.
|
||||
static int valid_custom(const char *c) {
|
||||
size_t n = strlen(c); int alpha = 0, digit = 0;
|
||||
if (n < 8 || n > 16) return 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (g_ascii_isalpha(c[i])) alpha = 1; else if (g_ascii_isdigit(c[i])) digit = 1; else return 0;
|
||||
}
|
||||
return alpha && digit;
|
||||
}
|
||||
|
||||
static void on_custom_changed(GtkEditable *e, gpointer ok) {
|
||||
gtk_widget_set_sensitive(GTK_WIDGET(ok), valid_custom(gtk_entry_get_text(GTK_ENTRY(e))));
|
||||
}
|
||||
|
||||
// Returns 1 when a new custom code was entered and posted.
|
||||
static int ask_custom_code(void) {
|
||||
GtkWidget *d = gtk_dialog_new_with_buttons("设置自定义验证码", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
"取消", GTK_RESPONSE_CANCEL, NULL);
|
||||
GtkWidget *ok = gtk_dialog_add_button(GTK_DIALOG(d), "确定", GTK_RESPONSE_OK);
|
||||
gtk_widget_set_sensitive(ok, FALSE);
|
||||
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(d));
|
||||
gtk_container_set_border_width(GTK_CONTAINER(area), 16);
|
||||
gtk_box_set_spacing(GTK_BOX(area), 8);
|
||||
GtkWidget *entry = gtk_entry_new();
|
||||
gtk_entry_set_max_length(GTK_ENTRY(entry), 16);
|
||||
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
|
||||
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "请输入自定义验证码");
|
||||
g_signal_connect(entry, "changed", G_CALLBACK(on_custom_changed), ok);
|
||||
gtk_box_pack_start(GTK_BOX(area), entry, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(area), label("8-16 位,需同时包含字母和数字;不会自动刷新,请妥善保管", "note"), FALSE, FALSE, 0);
|
||||
gtk_widget_show_all(d);
|
||||
int done = 0;
|
||||
if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK) {
|
||||
const char *code = gtk_entry_get_text(GTK_ENTRY(entry));
|
||||
if (valid_custom(code)) { post_arg(CMD_CUSTOM, code); done = 1; }
|
||||
}
|
||||
gtk_widget_destroy(d);
|
||||
return done;
|
||||
}
|
||||
|
||||
static int suppress_combo;
|
||||
static void on_verify_changed(GtkComboBox *c, gpointer d) {
|
||||
(void)d;
|
||||
if (suppress_combo) return;
|
||||
int v = gtk_combo_box_get_active(c);
|
||||
if (v != VERIFY_TEMPORARY && !cur_has_custom && !ask_custom_code()) {
|
||||
suppress_combo = 1; gtk_combo_box_set_active(c, cur_verify); suppress_combo = 0;
|
||||
return;
|
||||
}
|
||||
post_int(CMD_VERIFY, v);
|
||||
}
|
||||
static void on_set_custom(GtkButton *b, gpointer d) { (void)b; (void)d; ask_custom_code(); }
|
||||
|
||||
static int suppress_refresh;
|
||||
static void on_refresh_item(GtkCheckMenuItem *item, gpointer data) {
|
||||
if (suppress_refresh || !gtk_check_menu_item_get_active(item)) return;
|
||||
post_int(CMD_REFRESH, GPOINTER_TO_INT(data));
|
||||
}
|
||||
static void on_refresh_now(GtkMenuItem *i, gpointer d) { (void)i; (void)d; post(CMD_RESET); }
|
||||
|
||||
static const char *REFRESH_NOTES[5] = { "每次远控结束刷新", "每小时自动刷新", "每 12 小时自动刷新", "每 24 小时自动刷新", "不自动刷新" };
|
||||
|
||||
static void render_code(void) {
|
||||
if (!code_label) return;
|
||||
gtk_label_set_text(GTK_LABEL(code_label), current_code[0] ? (code_visible ? current_code : "********") : "--------");
|
||||
gtk_button_set_label(GTK_BUTTON(eye_button), code_visible ? "隐藏" : "显示");
|
||||
}
|
||||
|
||||
static void copy_text(const char *text, const char *hint) {
|
||||
if (!text || !text[0]) return;
|
||||
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
|
||||
gtk_label_set_text(GTK_LABEL(hint_label), hint);
|
||||
}
|
||||
// The ID is copied without the display spaces so it pastes straight into the viewer's ID field.
|
||||
static void on_copy_id(GtkButton *b, gpointer d) {
|
||||
(void)b; (void)d;
|
||||
char digits[64]; size_t n = 0;
|
||||
for (const char *p = current_id; *p && n + 1 < sizeof digits; p++) if (*p != ' ') digits[n++] = *p;
|
||||
digits[n] = 0;
|
||||
copy_text(digits, "已复制设备 ID");
|
||||
}
|
||||
static void on_copy_code(GtkButton *b, gpointer d) { (void)b; (void)d; copy_text(current_code, "已复制验证码,请只发给信任的人"); }
|
||||
|
||||
static void on_eye(GtkButton *b, gpointer d) { (void)b; (void)d; code_visible = !code_visible; render_code(); }
|
||||
|
||||
static void on_share(GtkButton *b, gpointer d) {
|
||||
(void)b; (void)d;
|
||||
if (!current_id[0] || !current_code[0]) return;
|
||||
char *text = g_strdup_printf("UU 远程协助\n设备 ID:%s\n验证码:%s", current_id, current_code);
|
||||
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
|
||||
g_free(text);
|
||||
gtk_label_set_text(GTK_LABEL(hint_label), "已复制设备 ID 和验证码,请只发给信任的人");
|
||||
}
|
||||
|
||||
static gboolean on_switch(GtkSwitch *s, gboolean state, gpointer d) {
|
||||
(void)s; (void)d;
|
||||
if (!suppress_switch) post(state ? CMD_ALLOW : CMD_DENY);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Host service: the close button only hides the window (assistance keeps its own on/off switch).
|
||||
static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) {
|
||||
(void)e; (void)d;
|
||||
if (!close_hides) return FALSE;
|
||||
gtk_widget_hide(w);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void on_destroy(GtkWidget *w, gpointer d) {
|
||||
(void)w; (void)d;
|
||||
window = code_label = NULL;
|
||||
atomic_store(&closed, 1);
|
||||
post(CMD_STOP);
|
||||
gtk_main_quit();
|
||||
}
|
||||
|
||||
static gboolean draw_avatar(GtkWidget *w, cairo_t *cr, gpointer d) {
|
||||
(void)w; (void)d;
|
||||
double s = 64;
|
||||
cairo_arc(cr, s / 2, s / 2, s / 2 - 1, 0, 2 * G_PI);
|
||||
cairo_set_source_rgb(cr, 0.84, 0.90, 1.0); cairo_fill_preserve(cr);
|
||||
cairo_set_source_rgb(cr, 0.36, 0.55, 1.0); cairo_set_line_width(cr, 2); cairo_stroke(cr);
|
||||
cairo_save(cr);
|
||||
cairo_arc(cr, s / 2, s / 2, s / 2 - 2, 0, 2 * G_PI); cairo_clip(cr);
|
||||
cairo_set_source_rgb(cr, 0.27, 0.51, 1.0);
|
||||
cairo_arc(cr, s / 2, s * 0.38, s * 0.16, 0, 2 * G_PI); cairo_fill(cr);
|
||||
cairo_arc(cr, s / 2, s * 0.92, s * 0.32, G_PI, 2 * G_PI); cairo_fill(cr);
|
||||
cairo_restore(cr);
|
||||
return TRUE;
|
||||
}
|
||||
static void on_logout(GtkButton *b, gpointer d) {
|
||||
(void)b; (void)d;
|
||||
GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
|
||||
"退出登录后,本机将从你的设备列表中下线,远程协助会改用访客身份。确定退出吗?");
|
||||
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
|
||||
post(CMD_LOGOUT);
|
||||
gtk_label_set_text(GTK_LABEL(user_label), "正在退出登录…");
|
||||
}
|
||||
gtk_widget_destroy(dlg);
|
||||
}
|
||||
|
||||
static GtkWidget *build_account_card(void) {
|
||||
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
|
||||
gtk_box_pack_start(GTK_BOX(header), label("账号", "heading"), FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||
account_stack = gtk_stack_new();
|
||||
gtk_stack_set_transition_type(GTK_STACK(account_stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
|
||||
gtk_stack_set_vhomogeneous(GTK_STACK(account_stack), FALSE); // user page is shorter than the QR page
|
||||
|
||||
// Not signed in: QR code + guidance.
|
||||
GtkWidget *qr_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 24);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(qr_page), 20);
|
||||
qr_stack = gtk_stack_new();
|
||||
gtk_widget_set_size_request(qr_stack, 180, 180);
|
||||
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
|
||||
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
|
||||
GtkWidget *spinner = gtk_spinner_new();
|
||||
gtk_spinner_start(GTK_SPINNER(spinner));
|
||||
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "note"), FALSE, FALSE, 0);
|
||||
gtk_stack_add_named(GTK_STACK(qr_stack), loading, "loading");
|
||||
qr_image = gtk_image_new();
|
||||
gtk_stack_add_named(GTK_STACK(qr_stack), qr_image, "qr");
|
||||
gtk_box_pack_start(GTK_BOX(qr_page), qr_stack, FALSE, FALSE, 0);
|
||||
GtkWidget *qr_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
|
||||
gtk_widget_set_valign(qr_text, GTK_ALIGN_CENTER);
|
||||
gtk_box_pack_start(GTK_BOX(qr_text), label("扫码登录", "heading"), FALSE, FALSE, 0);
|
||||
GtkWidget *cap = label("请使用网易 UU 远程 App 扫一扫登录。\n登录后本机会出现在你的设备列表中,可直接远程控制;\n远程协助在登录前后都可以使用。", "hint");
|
||||
gtk_label_set_xalign(GTK_LABEL(cap), 0);
|
||||
gtk_box_pack_start(GTK_BOX(qr_text), cap, FALSE, FALSE, 0);
|
||||
login_status = label("", "status");
|
||||
gtk_label_set_line_wrap(GTK_LABEL(login_status), TRUE);
|
||||
gtk_box_pack_start(GTK_BOX(qr_text), login_status, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(qr_page), qr_text, TRUE, TRUE, 0);
|
||||
gtk_stack_add_named(GTK_STACK(account_stack), qr_page, "qr");
|
||||
|
||||
// Signed in: avatar + account name + logout.
|
||||
GtkWidget *user_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 16);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(user_page), 20);
|
||||
GtkWidget *avatar = gtk_drawing_area_new();
|
||||
gtk_widget_set_size_request(avatar, 64, 64);
|
||||
gtk_widget_set_valign(avatar, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(avatar, "draw", G_CALLBACK(draw_avatar), NULL);
|
||||
gtk_box_pack_start(GTK_BOX(user_page), avatar, FALSE, FALSE, 0);
|
||||
GtkWidget *user_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
|
||||
gtk_widget_set_valign(user_text, GTK_ALIGN_CENTER);
|
||||
user_label = label("", "user");
|
||||
gtk_box_pack_start(GTK_BOX(user_text), user_label, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(user_text), label("已登录 · 本机已出现在你的设备列表中", "hint"), FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(user_page), user_text, TRUE, TRUE, 0);
|
||||
GtkWidget *logout = gtk_button_new_with_label("退出登录");
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(logout), "share");
|
||||
gtk_widget_set_valign(logout, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(logout, "clicked", G_CALLBACK(on_logout), NULL);
|
||||
gtk_box_pack_end(GTK_BOX(user_page), logout, FALSE, FALSE, 0);
|
||||
gtk_stack_add_named(GTK_STACK(account_stack), user_page, "user");
|
||||
|
||||
gtk_box_pack_start(GTK_BOX(card), account_stack, FALSE, FALSE, 0);
|
||||
return card;
|
||||
}
|
||||
|
||||
static void build_window(void) {
|
||||
GtkCssProvider *css = gtk_css_provider_new();
|
||||
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
g_object_unref(css);
|
||||
|
||||
gtk_window_set_default_icon_name("uu-remote-linux"); // contrib/desktop icon, when installed
|
||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机(非官方)");
|
||||
gtk_window_set_default_size(GTK_WINDOW(window), 760, -1);
|
||||
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||
g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
|
||||
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
|
||||
|
||||
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
|
||||
gtk_container_add(GTK_CONTAINER(window), root);
|
||||
|
||||
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||
gtk_box_pack_start(GTK_BOX(root), card, FALSE, FALSE, 0);
|
||||
assist_card = card;
|
||||
|
||||
// Header: 本设备 ... 允许他人远程协助 [switch]
|
||||
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
|
||||
gtk_box_pack_start(GTK_BOX(header), label("本设备", "heading"), FALSE, FALSE, 0);
|
||||
allow_switch = gtk_switch_new();
|
||||
gtk_widget_set_valign(allow_switch, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(allow_switch, "state-set", G_CALLBACK(on_switch), NULL);
|
||||
gtk_box_pack_end(GTK_BOX(header), allow_switch, FALSE, FALSE, 0);
|
||||
gtk_box_pack_end(GTK_BOX(header), label("允许他人远程协助", "key"), FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||
|
||||
// Body: ID | code + controls
|
||||
GtkWidget *body = gtk_grid_new();
|
||||
gtk_container_set_border_width(GTK_CONTAINER(body), 20);
|
||||
gtk_grid_set_column_spacing(GTK_GRID(body), 48);
|
||||
gtk_grid_set_row_spacing(GTK_GRID(body), 6);
|
||||
gtk_grid_attach(GTK_GRID(body), label("本设备 ID", "key"), 0, 0, 1, 1);
|
||||
GtkWidget *id_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
|
||||
id_label = label("获取中…", "big");
|
||||
gtk_label_set_selectable(GTK_LABEL(id_label), TRUE);
|
||||
gtk_box_pack_start(GTK_BOX(id_row), id_label, FALSE, FALSE, 0);
|
||||
GtkWidget *copy_id = gtk_button_new_with_label("复制");
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(copy_id), "flat");
|
||||
gtk_widget_set_valign(copy_id, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(copy_id, "clicked", G_CALLBACK(on_copy_id), NULL);
|
||||
gtk_box_pack_start(GTK_BOX(id_row), copy_id, FALSE, FALSE, 0);
|
||||
gtk_grid_attach(GTK_GRID(body), id_row, 0, 1, 1, 1);
|
||||
|
||||
GtkWidget *verify_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
|
||||
gtk_box_pack_start(GTK_BOX(verify_row), label("验证方式:", "key"), FALSE, FALSE, 0);
|
||||
verify_combo = gtk_combo_box_text_new();
|
||||
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用临时验证码");
|
||||
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用自定义验证码");
|
||||
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "同时使用两种验证码");
|
||||
gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), 0);
|
||||
g_signal_connect(verify_combo, "changed", G_CALLBACK(on_verify_changed), NULL);
|
||||
gtk_box_pack_start(GTK_BOX(verify_row), verify_combo, FALSE, FALSE, 0);
|
||||
gtk_grid_attach(GTK_GRID(body), verify_row, 1, 0, 1, 1);
|
||||
GtkWidget *code_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
|
||||
code_label = label("--------", "big");
|
||||
gtk_box_pack_start(GTK_BOX(code_row), code_label, FALSE, FALSE, 0);
|
||||
eye_button = gtk_button_new_with_label("隐藏");
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(eye_button), "flat");
|
||||
g_signal_connect(eye_button, "clicked", G_CALLBACK(on_eye), NULL);
|
||||
// Gear menu: 立即刷新 / 自动刷新 ▸ (每次远控结束, 每小时, 每 12 小时, 每 24 小时, 关闭) / 设置自定义验证码
|
||||
GtkWidget *menu = gtk_menu_new();
|
||||
GtkWidget *now = gtk_menu_item_new_with_label("立即刷新");
|
||||
g_signal_connect(now, "activate", G_CALLBACK(on_refresh_now), NULL);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), now);
|
||||
GtkWidget *auto_item = gtk_menu_item_new_with_label("自动刷新");
|
||||
GtkWidget *sub = gtk_menu_new();
|
||||
const char *names[5] = { "每次远控结束", "每小时", "每 12 小时", "每 24 小时", "关闭" };
|
||||
GSList *group = NULL;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
refresh_items[i] = gtk_radio_menu_item_new_with_label(group, names[i]);
|
||||
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(refresh_items[i]));
|
||||
g_signal_connect(refresh_items[i], "toggled", G_CALLBACK(on_refresh_item), GINT_TO_POINTER(i));
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(sub), refresh_items[i]);
|
||||
}
|
||||
gtk_menu_item_set_submenu(GTK_MENU_ITEM(auto_item), sub);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), auto_item);
|
||||
GtkWidget *custom_item = gtk_menu_item_new_with_label("设置自定义验证码…");
|
||||
g_signal_connect(custom_item, "activate", G_CALLBACK(on_set_custom), NULL);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), custom_item);
|
||||
gtk_widget_show_all(menu);
|
||||
GtkWidget *gear = gtk_menu_button_new();
|
||||
gtk_button_set_image(GTK_BUTTON(gear), gtk_image_new_from_icon_name("emblem-system-symbolic", GTK_ICON_SIZE_BUTTON));
|
||||
gtk_widget_set_tooltip_text(gear, "刷新设置");
|
||||
gtk_menu_button_set_popup(GTK_MENU_BUTTON(gear), menu);
|
||||
GtkWidget *copy_code = gtk_button_new_with_label("复制");
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(copy_code), "flat");
|
||||
gtk_widget_set_valign(copy_code, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(copy_code, "clicked", G_CALLBACK(on_copy_code), NULL);
|
||||
gtk_widget_set_valign(eye_button, GTK_ALIGN_CENTER);
|
||||
gtk_widget_set_valign(gear, GTK_ALIGN_CENTER);
|
||||
gtk_box_pack_start(GTK_BOX(code_row), copy_code, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(code_row), eye_button, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(code_row), gear, FALSE, FALSE, 0);
|
||||
gtk_grid_attach(GTK_GRID(body), code_row, 1, 1, 1, 1);
|
||||
note_label = label(REFRESH_NOTES[0], "note");
|
||||
gtk_grid_attach(GTK_GRID(body), note_label, 1, 2, 1, 1);
|
||||
|
||||
share_button = gtk_button_new_with_label("复制并分享");
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(share_button), "share");
|
||||
gtk_widget_set_valign(share_button, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(share_button, "clicked", G_CALLBACK(on_share), NULL);
|
||||
gtk_grid_attach(GTK_GRID(body), share_button, 2, 1, 1, 1);
|
||||
gtk_box_pack_start(GTK_BOX(card), body, FALSE, FALSE, 0);
|
||||
|
||||
hint_label = label("", "hint");
|
||||
gtk_box_pack_start(GTK_BOX(root), hint_label, FALSE, FALSE, 0);
|
||||
account_card = build_account_card();
|
||||
gtk_widget_set_margin_top(account_card, 6);
|
||||
gtk_box_pack_start(GTK_BOX(root), account_card, FALSE, FALSE, 0);
|
||||
footer_label = label("开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口即停止远程协助", "footer");
|
||||
gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0);
|
||||
if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行");
|
||||
render_code();
|
||||
gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window)));
|
||||
gtk_widget_set_visible(assist_card, show_assist);
|
||||
gtk_widget_set_visible(hint_label, show_assist);
|
||||
gtk_widget_set_visible(account_card, show_account);
|
||||
if (!start_hidden) gtk_widget_show(window);
|
||||
}
|
||||
|
||||
static void *ui_main(void *arg) {
|
||||
(void)arg;
|
||||
int ok = gtk_init_check(NULL, NULL);
|
||||
if (ok) build_window();
|
||||
pthread_mutex_lock(&init_lock);
|
||||
init_state = ok ? 1 : -1;
|
||||
pthread_cond_signal(&init_cond);
|
||||
pthread_mutex_unlock(&init_lock);
|
||||
if (ok) gtk_main();
|
||||
else atomic_store(&closed, 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Returns 0 when the window was created, -1 when no display is usable. hidden: create without
|
||||
// showing (host service, opened later with uu_assist_ui_present); hide_on_close: closing hides.
|
||||
static gboolean apply_sections(gpointer d) {
|
||||
(void)d;
|
||||
if (window) {
|
||||
gtk_widget_set_visible(assist_card, show_assist); gtk_widget_set_visible(hint_label, show_assist);
|
||||
gtk_widget_set_visible(account_card, show_account);
|
||||
}
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
static int ui_start(int hidden, int hide_on_close);
|
||||
int uu_assist_ui_start(int hidden, int hide_on_close) {
|
||||
if (ui_started) { show_assist = 1; g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
|
||||
show_assist = 1;
|
||||
return ui_start(hidden, hide_on_close);
|
||||
}
|
||||
// Reveals the account card on the shared window; standalone `login` opens it with the account card only.
|
||||
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
|
||||
(void)name; (void)os; (void)cpu; (void)gpu;
|
||||
show_account = 1;
|
||||
if (ui_started) { g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
|
||||
show_assist = 0;
|
||||
return ui_start(0, 0);
|
||||
}
|
||||
void uu_ui_show_account(void) { show_account = 1; if (ui_started && init_state == 1) g_idle_add(apply_sections, NULL); }
|
||||
static int ui_start(int hidden, int hide_on_close) {
|
||||
if (ui_started) return init_state == 1 ? 0 : -1;
|
||||
start_hidden = hidden; close_hides = hide_on_close;
|
||||
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
|
||||
ui_started = 1;
|
||||
pthread_mutex_lock(&init_lock);
|
||||
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
|
||||
pthread_mutex_unlock(&init_lock);
|
||||
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static gboolean apply_state(gpointer data) {
|
||||
State *s = data;
|
||||
if (window) {
|
||||
g_strlcpy(current_id, s->id, sizeof current_id);
|
||||
g_strlcpy(current_code, s->code, sizeof current_code);
|
||||
gtk_label_set_text(GTK_LABEL(id_label), current_id[0] ? current_id : "获取中…");
|
||||
render_code();
|
||||
suppress_switch = 1;
|
||||
gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed);
|
||||
suppress_switch = 0;
|
||||
cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom;
|
||||
suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0;
|
||||
if (s->refresh >= 0 && s->refresh < 5) {
|
||||
suppress_refresh = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(refresh_items[s->refresh]), TRUE); suppress_refresh = 0;
|
||||
}
|
||||
gtk_label_set_text(GTK_LABEL(note_label), s->verify == VERIFY_CUSTOM ? "自定义验证码不会自动刷新"
|
||||
: s->verify == VERIFY_BOTH ? "也可使用自定义验证码连接" : REFRESH_NOTES[s->refresh >= 0 && s->refresh < 5 ? s->refresh : 0]);
|
||||
gtk_widget_set_sensitive(share_button, current_id[0] && current_code[0]);
|
||||
gtk_label_set_text(GTK_LABEL(hint_label), s->allowed
|
||||
? "对方在 UU 远程「开始协助」中输入设备 ID 和验证码即可连接"
|
||||
: "已关闭:他人暂时无法远程协助本机,打开右上角开关后才能连接");
|
||||
}
|
||||
g_free(s->id); g_free(s->code); g_free(s);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
// id is already formatted for display (e.g. "123 456 789").
|
||||
void uu_assist_ui_set(const char *id, const char *code, int allowed, int verify, int refresh, int has_custom) {
|
||||
if (!ui_started || init_state != 1) return;
|
||||
State *s = g_new0(State, 1);
|
||||
s->id = g_strdup(id ? id : ""); s->code = g_strdup(code ? code : ""); s->allowed = allowed;
|
||||
s->verify = verify; s->refresh = refresh; s->has_custom = has_custom;
|
||||
g_idle_add(apply_state, s);
|
||||
}
|
||||
|
||||
// Pops the next user command (see CMD_*), copying its argument into arg.
|
||||
int uu_assist_ui_take_command(char *arg, size_t len) {
|
||||
int cmd = CMD_NONE;
|
||||
if (arg && len) arg[0] = 0;
|
||||
pthread_mutex_lock(&q_lock);
|
||||
if (q_len) {
|
||||
Cmd *c = &queue[q_head];
|
||||
cmd = c->cmd; if (arg && len) g_strlcpy(arg, c->arg, len);
|
||||
memset(c->arg, 0, sizeof c->arg); // drop the custom code copy promptly
|
||||
q_head = (q_head + 1) % 16; q_len--;
|
||||
}
|
||||
pthread_mutex_unlock(&q_lock);
|
||||
return cmd;
|
||||
}
|
||||
int uu_assist_ui_closed(void) { return atomic_load(&closed); }
|
||||
|
||||
static gboolean apply_present(gpointer data) {
|
||||
(void)data;
|
||||
if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); }
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
void uu_assist_ui_present(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_present, NULL); }
|
||||
|
||||
static gboolean apply_close(gpointer data) { (void)data; if (window) gtk_widget_destroy(window); return G_SOURCE_REMOVE; }
|
||||
void uu_assist_ui_close(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_close, NULL); }
|
||||
|
||||
// ---- Account card API (QR login / signed-in user); same names as the former login window ----
|
||||
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
|
||||
typedef struct { char *text; int kind; } StatusUpdate;
|
||||
|
||||
static gboolean apply_qr(gpointer data) {
|
||||
QrUpdate *u = data;
|
||||
if (window) {
|
||||
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4, NULL, NULL);
|
||||
GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pb, 180, 180, GDK_INTERP_NEAREST);
|
||||
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), scaled);
|
||||
g_object_unref(scaled); g_object_unref(pb);
|
||||
gtk_stack_set_visible_child_name(GTK_STACK(qr_stack), "qr");
|
||||
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "qr");
|
||||
}
|
||||
g_free(u->rgba); g_free(u);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
|
||||
if (!ui_started || init_state != 1 || atomic_load(&closed) || edge <= 0) return;
|
||||
QrUpdate *u = g_new(QrUpdate, 1);
|
||||
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4); u->edge = edge;
|
||||
g_idle_add(apply_qr, u);
|
||||
}
|
||||
static gboolean apply_login_status(gpointer data) {
|
||||
StatusUpdate *u = data;
|
||||
if (window) {
|
||||
GtkStyleContext *ctx = gtk_widget_get_style_context(login_status);
|
||||
gtk_style_context_remove_class(ctx, "ok"); gtk_style_context_remove_class(ctx, "error");
|
||||
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
|
||||
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
|
||||
gtk_label_set_text(GTK_LABEL(login_status), u->text);
|
||||
}
|
||||
g_free(u->text); g_free(u);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
void uu_login_ui_set_status(const char *text, int kind) {
|
||||
if (!ui_started || init_state != 1 || atomic_load(&closed)) return;
|
||||
StatusUpdate *u = g_new(StatusUpdate, 1);
|
||||
u->text = g_strdup(text ? text : ""); u->kind = kind;
|
||||
g_idle_add(apply_login_status, u);
|
||||
}
|
||||
static gboolean apply_user(gpointer data) {
|
||||
if (window) {
|
||||
gtk_label_set_text(GTK_LABEL(user_label), data);
|
||||
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "user");
|
||||
show_account = 1; apply_sections(NULL);
|
||||
}
|
||||
g_free(data);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
// Signed in: show the user page with this (already masked) account name.
|
||||
void uu_ui_set_user(const char *name) {
|
||||
if (!ui_started || init_state != 1) return;
|
||||
g_idle_add(apply_user, g_strdup(name ? name : ""));
|
||||
}
|
||||
void uu_login_ui_set_done(const char *text) { uu_ui_set_user(text); }
|
||||
int uu_login_ui_closed(void) { return atomic_load(&closed); }
|
||||
void uu_login_ui_close(void) { uu_assist_ui_close(); }
|
||||
@@ -1,321 +0,0 @@
|
||||
// GTK3 QR login window for `uuremote-server login`. The window runs its own GTK main loop on a
|
||||
// dedicated thread; the Rust login flow pushes QR images and status text through g_idle_add.
|
||||
// Layout follows the official login window (QR on the left), but uses no NetEase assets: the
|
||||
// mark is drawn with cairo and the right column shows this host's hardware summary instead of SMS.
|
||||
#include <gtk/gtk.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *CSS =
|
||||
"window { background-image: linear-gradient(165deg, #d6e8ff 0%, #eaf3ff 40%, #ffffff 100%); }\n"
|
||||
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
||||
".title { font-size: 26px; font-weight: 800; color: #111111; }\n"
|
||||
".subtitle { font-size: 13px; color: #6b7280; }\n"
|
||||
".heading { font-size: 19px; font-weight: 700; color: #1d1d1f; }\n"
|
||||
".card { background-color: #ffffff; border-radius: 10px; padding: 14px; }\n"
|
||||
".caption { font-size: 14px; color: #555555; }\n"
|
||||
".status { font-size: 13px; color: #2f6bff; }\n"
|
||||
".status.ok { color: #16a34a; }\n"
|
||||
".status.error { color: #dc2626; }\n"
|
||||
".key { font-size: 13px; color: #8a8f98; }\n"
|
||||
".value { font-size: 13px; color: #222222; }\n"
|
||||
".note { font-size: 12px; color: #6b7280; }\n"
|
||||
".done-mark { font-size: 72px; color: #16a34a; }\n"
|
||||
".done-text { font-size: 17px; font-weight: 700; color: #1d1d1f; }\n"
|
||||
".footer { font-size: 12px; color: #9ca3af; }\n"
|
||||
"button.primary { background-image: none; background-color: #2f6bff; color: #ffffff; border: none;"
|
||||
" border-radius: 6px; padding: 6px 28px; font-size: 15px; }\n"
|
||||
"button.primary:hover { background-color: #1f5ae6; }\n"
|
||||
"separator { background-color: #dfe5ee; min-width: 1px; }\n";
|
||||
|
||||
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
|
||||
typedef struct { char *text; int kind; } StatusUpdate;
|
||||
|
||||
static atomic_int closed;
|
||||
static pthread_t ui_thread;
|
||||
static int ui_started;
|
||||
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
||||
static int init_state; // 0 pending, 1 ok, -1 failed
|
||||
static char *info[4];
|
||||
static GtkWidget *window, *stack, *qr_image, *status_label, *done_label, *done_button;
|
||||
|
||||
static gboolean draw_mark(GtkWidget *w, cairo_t *cr, gpointer data) {
|
||||
(void)w; (void)data;
|
||||
cairo_pattern_t *g = cairo_pattern_create_linear(0, 0, 44, 44);
|
||||
cairo_pattern_add_color_stop_rgb(g, 0, 0.13, 0.83, 0.93);
|
||||
cairo_pattern_add_color_stop_rgb(g, 1, 0.18, 0.42, 1.0);
|
||||
double r = 11, s = 44;
|
||||
cairo_new_sub_path(cr);
|
||||
cairo_arc(cr, s - r, r, r, -G_PI / 2, 0);
|
||||
cairo_arc(cr, s - r, s - r, r, 0, G_PI / 2);
|
||||
cairo_arc(cr, r, s - r, r, G_PI / 2, G_PI);
|
||||
cairo_arc(cr, r, r, r, G_PI, 3 * G_PI / 2);
|
||||
cairo_close_path(cr);
|
||||
cairo_set_source(cr, g);
|
||||
cairo_fill(cr);
|
||||
cairo_pattern_destroy(g);
|
||||
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||
cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
|
||||
cairo_set_font_size(cr, 19);
|
||||
cairo_text_extents_t e;
|
||||
cairo_text_extents(cr, "UU", &e);
|
||||
cairo_move_to(cr, (s - e.width) / 2 - e.x_bearing, (s - e.height) / 2 - e.y_bearing);
|
||||
cairo_show_text(cr, "UU");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static GtkWidget *label(const char *text, const char *cls) {
|
||||
GtkWidget *l = gtk_label_new(text);
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
|
||||
return l;
|
||||
}
|
||||
|
||||
static void info_row(GtkGrid *grid, int row, const char *key, const char *value) {
|
||||
GtkWidget *k = label(key, "key"), *v = label(value && *value ? value : "-", "value");
|
||||
gtk_widget_set_halign(k, GTK_ALIGN_START);
|
||||
gtk_widget_set_valign(k, GTK_ALIGN_START);
|
||||
gtk_widget_set_halign(v, GTK_ALIGN_START);
|
||||
gtk_label_set_xalign(GTK_LABEL(v), 0);
|
||||
gtk_label_set_line_wrap(GTK_LABEL(v), TRUE);
|
||||
gtk_label_set_max_width_chars(GTK_LABEL(v), 26);
|
||||
gtk_grid_attach(grid, k, 0, row, 1, 1);
|
||||
gtk_grid_attach(grid, v, 1, row, 1, 1);
|
||||
}
|
||||
|
||||
static void on_destroy(GtkWidget *w, gpointer data) {
|
||||
(void)w; (void)data;
|
||||
atomic_store(&closed, 1);
|
||||
window = NULL;
|
||||
gtk_main_quit();
|
||||
}
|
||||
|
||||
static void on_done_clicked(GtkButton *b, gpointer data) {
|
||||
(void)b; (void)data;
|
||||
if (window) gtk_widget_destroy(window);
|
||||
}
|
||||
|
||||
static void build_window(void) {
|
||||
GtkCssProvider *css = gtk_css_provider_new();
|
||||
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
g_object_unref(css);
|
||||
|
||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机 - 登录");
|
||||
gtk_window_set_default_size(GTK_WINDOW(window), 720, 520);
|
||||
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
|
||||
|
||||
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
|
||||
gtk_container_add(GTK_CONTAINER(window), root);
|
||||
|
||||
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
|
||||
gtk_widget_set_halign(header, GTK_ALIGN_CENTER);
|
||||
GtkWidget *mark = gtk_drawing_area_new();
|
||||
gtk_widget_set_size_request(mark, 44, 44);
|
||||
gtk_widget_set_valign(mark, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(mark, "draw", G_CALLBACK(draw_mark), NULL);
|
||||
GtkWidget *titles = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||
GtkWidget *title = label("UU 远程 · Linux 主机", "title");
|
||||
gtk_widget_set_halign(title, GTK_ALIGN_START);
|
||||
GtkWidget *subtitle = label("登录后,本机会出现在你的 UU 远程设备列表中", "subtitle");
|
||||
gtk_widget_set_halign(subtitle, GTK_ALIGN_START);
|
||||
gtk_box_pack_start(GTK_BOX(titles), title, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(titles), subtitle, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(header), mark, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(header), titles, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(root), header, FALSE, FALSE, 4);
|
||||
|
||||
GtkWidget *columns = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
||||
gtk_box_set_homogeneous(GTK_BOX(columns), FALSE);
|
||||
gtk_widget_set_margin_top(columns, 22);
|
||||
gtk_box_pack_start(GTK_BOX(root), columns, TRUE, TRUE, 0);
|
||||
|
||||
// Left: QR login.
|
||||
GtkWidget *left = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
|
||||
gtk_widget_set_size_request(left, 330, -1);
|
||||
gtk_box_pack_start(GTK_BOX(left), label("扫码登录", "heading"), FALSE, FALSE, 0);
|
||||
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||
gtk_widget_set_halign(card, GTK_ALIGN_CENTER);
|
||||
gtk_widget_set_size_request(card, 252, 252);
|
||||
stack = gtk_stack_new();
|
||||
gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
|
||||
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
||||
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
|
||||
GtkWidget *spinner = gtk_spinner_new();
|
||||
gtk_spinner_start(GTK_SPINNER(spinner));
|
||||
gtk_widget_set_size_request(spinner, 32, 32);
|
||||
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "caption"), FALSE, FALSE, 0);
|
||||
gtk_stack_add_named(GTK_STACK(stack), loading, "loading");
|
||||
qr_image = gtk_image_new();
|
||||
gtk_stack_add_named(GTK_STACK(stack), qr_image, "qr");
|
||||
GtkWidget *done = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
|
||||
gtk_widget_set_valign(done, GTK_ALIGN_CENTER);
|
||||
gtk_box_pack_start(GTK_BOX(done), label("✓", "done-mark"), FALSE, FALSE, 0);
|
||||
done_label = label("登录成功", "done-text");
|
||||
gtk_label_set_justify(GTK_LABEL(done_label), GTK_JUSTIFY_CENTER);
|
||||
gtk_label_set_line_wrap(GTK_LABEL(done_label), TRUE);
|
||||
gtk_box_pack_start(GTK_BOX(done), done_label, FALSE, FALSE, 0);
|
||||
gtk_stack_add_named(GTK_STACK(stack), done, "done");
|
||||
gtk_box_pack_start(GTK_BOX(card), stack, TRUE, TRUE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(left), card, FALSE, FALSE, 0);
|
||||
GtkWidget *caption = label("请使用网易 UU 远程 App 扫一扫\n可快速登录", "caption");
|
||||
gtk_label_set_justify(GTK_LABEL(caption), GTK_JUSTIFY_CENTER);
|
||||
gtk_box_pack_start(GTK_BOX(left), caption, FALSE, FALSE, 0);
|
||||
status_label = label("", "status");
|
||||
gtk_label_set_line_wrap(GTK_LABEL(status_label), TRUE);
|
||||
gtk_label_set_justify(GTK_LABEL(status_label), GTK_JUSTIFY_CENTER);
|
||||
gtk_label_set_max_width_chars(GTK_LABEL(status_label), 34);
|
||||
gtk_box_pack_start(GTK_BOX(left), status_label, FALSE, FALSE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(columns), left, TRUE, TRUE, 0);
|
||||
|
||||
GtkWidget *sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
|
||||
gtk_widget_set_margin_top(sep, 6);
|
||||
gtk_widget_set_margin_bottom(sep, 24);
|
||||
gtk_box_pack_start(GTK_BOX(columns), sep, FALSE, FALSE, 0);
|
||||
|
||||
// Right: what will be registered.
|
||||
GtkWidget *right = gtk_box_new(GTK_ORIENTATION_VERTICAL, 14);
|
||||
gtk_widget_set_margin_start(right, 30);
|
||||
gtk_widget_set_size_request(right, 300, -1);
|
||||
GtkWidget *rh = label("本机信息", "heading");
|
||||
gtk_box_pack_start(GTK_BOX(right), rh, FALSE, FALSE, 0);
|
||||
GtkWidget *grid = gtk_grid_new();
|
||||
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
|
||||
gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
|
||||
gtk_widget_set_margin_top(grid, 10);
|
||||
const char *keys[4] = {"设备名称", "操作系统", "处理器", "显卡"};
|
||||
for (int i = 0; i < 4; i++) info_row(GTK_GRID(grid), i, keys[i], info[i]);
|
||||
gtk_box_pack_start(GTK_BOX(right), grid, FALSE, FALSE, 0);
|
||||
GtkWidget *note = label("登录后可在 Mac / Windows / 手机上的官方 UU 远程客户端中连接本机的桌面、文件传输和终端。"
|
||||
"登录信息只保存在本机配置文件中。", "note");
|
||||
gtk_label_set_line_wrap(GTK_LABEL(note), TRUE);
|
||||
gtk_label_set_max_width_chars(GTK_LABEL(note), 30);
|
||||
gtk_label_set_xalign(GTK_LABEL(note), 0);
|
||||
gtk_widget_set_margin_top(note, 10);
|
||||
gtk_box_pack_start(GTK_BOX(right), note, FALSE, FALSE, 0);
|
||||
done_button = gtk_button_new_with_label("完成");
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(done_button), "primary");
|
||||
gtk_widget_set_halign(done_button, GTK_ALIGN_START);
|
||||
gtk_widget_set_no_show_all(done_button, TRUE);
|
||||
g_signal_connect(done_button, "clicked", G_CALLBACK(on_done_clicked), NULL);
|
||||
gtk_box_pack_end(GTK_BOX(right), done_button, FALSE, FALSE, 20);
|
||||
gtk_box_pack_start(GTK_BOX(columns), right, TRUE, TRUE, 0);
|
||||
|
||||
GtkWidget *footer = label("开源 Linux 主机端 · 非网易官方客户端", "footer");
|
||||
gtk_box_pack_end(GTK_BOX(root), footer, FALSE, FALSE, 0);
|
||||
gtk_widget_show_all(window);
|
||||
}
|
||||
|
||||
static void *ui_main(void *arg) {
|
||||
(void)arg;
|
||||
int ok = gtk_init_check(NULL, NULL);
|
||||
if (ok) build_window();
|
||||
pthread_mutex_lock(&init_lock);
|
||||
init_state = ok ? 1 : -1;
|
||||
pthread_cond_signal(&init_cond);
|
||||
pthread_mutex_unlock(&init_lock);
|
||||
if (ok) gtk_main();
|
||||
else atomic_store(&closed, 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Returns 0 when the window is shown, -1 when no display is usable.
|
||||
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
|
||||
if (ui_started) return init_state == 1 ? 0 : -1;
|
||||
const char *src[4] = {name, os, cpu, gpu};
|
||||
for (int i = 0; i < 4; i++) info[i] = g_strdup(src[i] ? src[i] : "");
|
||||
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
|
||||
ui_started = 1;
|
||||
pthread_mutex_lock(&init_lock);
|
||||
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
|
||||
pthread_mutex_unlock(&init_lock);
|
||||
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static gboolean apply_qr(gpointer data) {
|
||||
QrUpdate *u = data;
|
||||
if (window) {
|
||||
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4,
|
||||
(GdkPixbufDestroyNotify)g_free, NULL);
|
||||
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), pb);
|
||||
g_object_unref(pb);
|
||||
gtk_stack_set_visible_child_name(GTK_STACK(stack), "qr");
|
||||
} else g_free(u->rgba);
|
||||
g_free(u);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
|
||||
if (atomic_load(&closed) || edge <= 0) return;
|
||||
QrUpdate *u = g_new(QrUpdate, 1);
|
||||
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4);
|
||||
u->edge = edge;
|
||||
g_idle_add(apply_qr, u);
|
||||
}
|
||||
|
||||
static gboolean apply_status(gpointer data) {
|
||||
StatusUpdate *u = data;
|
||||
if (window) {
|
||||
GtkStyleContext *ctx = gtk_widget_get_style_context(status_label);
|
||||
gtk_style_context_remove_class(ctx, "ok");
|
||||
gtk_style_context_remove_class(ctx, "error");
|
||||
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
|
||||
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
|
||||
gtk_label_set_text(GTK_LABEL(status_label), u->text);
|
||||
if (u->kind == 2 && !gtk_image_get_pixbuf(GTK_IMAGE(qr_image)))
|
||||
gtk_widget_hide(gtk_stack_get_child_by_name(GTK_STACK(stack), "loading"));
|
||||
}
|
||||
g_free(u->text);
|
||||
g_free(u);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
// kind: 0 info, 1 success, 2 error.
|
||||
void uu_login_ui_set_status(const char *text, int kind) {
|
||||
if (atomic_load(&closed)) return;
|
||||
StatusUpdate *u = g_new(StatusUpdate, 1);
|
||||
u->text = g_strdup(text ? text : "");
|
||||
u->kind = kind;
|
||||
g_idle_add(apply_status, u);
|
||||
}
|
||||
|
||||
static gboolean apply_done(gpointer data) {
|
||||
if (window) {
|
||||
gtk_label_set_text(GTK_LABEL(done_label), data);
|
||||
gtk_stack_set_visible_child_name(GTK_STACK(stack), "done");
|
||||
gtk_widget_show(done_button);
|
||||
gtk_widget_grab_focus(done_button);
|
||||
}
|
||||
g_free(data);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
void uu_login_ui_set_done(const char *text) {
|
||||
if (atomic_load(&closed)) return;
|
||||
g_idle_add(apply_done, g_strdup(text ? text : ""));
|
||||
}
|
||||
|
||||
int uu_login_ui_closed(void) { return atomic_load(&closed); }
|
||||
|
||||
static gboolean apply_close(gpointer data) {
|
||||
(void)data;
|
||||
if (window) gtk_widget_destroy(window);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
void uu_login_ui_close(void) {
|
||||
if (!ui_started) return;
|
||||
if (!atomic_load(&closed)) g_idle_add(apply_close, NULL);
|
||||
pthread_join(ui_thread, NULL);
|
||||
ui_started = 0;
|
||||
}
|
||||
Reference in new issue
Block a user