/* * main.c -- dtchat's entry point and the --smoke-test * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * T2.1 is the shell: the main window, the menu bar, the roster/chat panes and * the status line. The model, accounts and protocols are wired in T2.2-T2.6. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include "ui_main.h" #include "ui_request.h" #include "demo.h" #include "ui_peer.h" #include "ui_file.h" #include "localui.h" #include "accounts_run.h" #include "ui_call.h" static XtAppContext app; static Widget toplevel; static DcModel *demo_model; static DcPeers *demo_peers; static DtLocal *local_mgr; static DtCallWindow *demo_call; static struct timespec perf_start; static int smoke_failed; static const char *demo_kind; static DtDemo *echo; struct Answer { int called; char *value; }; static void answer_cb(const char *value, void *arg) { struct Answer *answer = arg; answer->called++; free(answer->value); answer->value = value != NULL ? strdup(value) : NULL; } static char * b64_encode(const unsigned char *in, size_t len) { static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char *out = malloc((len + 2) / 3 * 4 + 1); size_t i, o = 0; if (out == NULL) return NULL; for (i = 0; i + 2 < len; i += 3) { out[o++] = table[in[i] >> 2]; out[o++] = table[((in[i] & 3) << 4) | (in[i + 1] >> 4)]; out[o++] = table[((in[i + 1] & 15) << 2) | (in[i + 2] >> 6)]; out[o++] = table[in[i + 2] & 63]; } if (i < len) { out[o++] = table[in[i] >> 2]; if (i + 1 < len) { out[o++] = table[((in[i] & 3) << 4) | (in[i + 1] >> 4)]; out[o++] = table[(in[i + 1] & 15) << 2]; } else { out[o++] = table[(in[i] & 3) << 4]; out[o++] = '='; } out[o++] = '='; } out[o] = '\0'; return out; } static DcJson * request_json(const char *kind, const char *title, const char *prompt, const char *hint) { DcJson *request = dc_json_new_object(); (void)dc_json_object_set(request, "kind", dc_json_new_string(kind)); (void)dc_json_object_set(request, "title", dc_json_new_string(title)); if (prompt != NULL) (void)dc_json_object_set(request, "prompt", dc_json_new_string(prompt)); if (hint != NULL) (void)dc_json_object_set(request, "hint", dc_json_new_string(hint)); return request; } static DcJson * qr_request(int *size_out) { enum { QW = 21, QH = 21 }; unsigned char modules[QW * QH]; char *b64; DcJson *request; DcJson *qr; int x, y, i; memset(modules, 0, sizeof(modules)); for (y = 0; y < QH; y++) { for (x = 0; x < QW; x++) { int dark = 0; /* three finder patterns */ for (i = 0; i < 3; i++) { int ox = i == 1 ? 14 : 0; int oy = i == 2 ? 14 : 0; if (x >= ox && x < ox + 7 && y >= oy && y < oy + 7) { int dx = x - ox, dy = y - oy; dark = dx == 0 || dx == 6 || dy == 0 || dy == 6 || (dx >= 2 && dx <= 4 && dy >= 2 && dy <= 4); } } /* timing lines */ if (x == 6 || y == 6) dark = ((x + y) % 2) == 0; modules[(size_t)y * QW + x] = (unsigned char)dark; } } b64 = b64_encode(modules, sizeof(modules)); if (b64 == NULL) return NULL; request = request_json("qr", "WhatsApp", NULL, "WhatsApp > Linked devices > Link a device"); qr = dc_json_new_object(); (void)dc_json_object_set(qr, "width", dc_json_new_int(QW)); (void)dc_json_object_set(qr, "height", dc_json_new_int(QH)); (void)dc_json_object_set(qr, "data", dc_json_new_string(b64)); (void)dc_json_object_set(request, "qr", qr); *size_out = QW; free(b64); return request; } static int request_checks(void) { struct Answer answer; DtRequestDialog *dialog; DcJson *request; int qr_size; memset(&answer, 0, sizeof(answer)); request = request_json("code", "Telegram", "Code from the app", NULL); dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, &answer); dc_json_free(request); if (dialog == NULL) return 0; dtchat_request_dialog_answer(dialog, "123456"); if (answer.called != 1 || answer.value == NULL || strcmp(answer.value, "123456") != 0) return 0; free(answer.value); memset(&answer, 0, sizeof(answer)); request = request_json("password", "Account", "Password", "for the demo"); dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, &answer); dc_json_free(request); if (dialog == NULL) return 0; dtchat_request_dialog_answer(dialog, NULL); if (answer.called != 1 || answer.value != NULL) return 0; memset(&answer, 0, sizeof(answer)); request = request_json("confirm", "Pairing", "Accept the peer?", NULL); dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, &answer); dc_json_free(request); if (dialog == NULL) return 0; dtchat_request_dialog_answer(dialog, "1"); if (answer.called != 1 || answer.value == NULL || strcmp(answer.value, "1") != 0) return 0; free(answer.value); memset(&answer, 0, sizeof(answer)); request = request_json("notice", "Session", "Linked.", NULL); dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, &answer); dc_json_free(request); if (dialog == NULL) return 0; dtchat_request_dialog_answer(dialog, "1"); if (answer.called != 1 || answer.value == NULL) return 0; free(answer.value); memset(&answer, 0, sizeof(answer)); request = qr_request(&qr_size); if (request == NULL) return 0; dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, &answer); dc_json_free(request); if (dialog == NULL || dtchat_request_dialog_shell(dialog) == NULL) return 0; dtchat_request_dialog_answer(dialog, NULL); if (answer.called != 1 || answer.value != NULL) return 0; memset(&answer, 0, sizeof(answer)); request = request_json("code", "Timeout", "Never answered", NULL); dialog = dtchat_request_dialog_create(toplevel, request, 120, answer_cb, &answer); dc_json_free(request); if (dialog == NULL) return 0; { long long start = (long long)time(NULL); while (answer.called == 0 && (long long)time(NULL) - start < 3) XtAppProcessEvent(app, XtIMAll); } if (answer.called != 1 || answer.value != NULL) return 0; return 1; } static DcPeers * make_demo_peers(void) { DcPeers *peers = dc_peers_new(); char pin[128]; if (peers == NULL) return NULL; { char txt[512]; snprintf(pin, sizeof(pin), "sha256:%064d", 7); snprintf(txt, sizeof(txt), "v=1;id=alice-id;name=alice;pin=%s", pin); (void)dc_peers_announce(peers, txt, "192.168.1.50", 4711); } snprintf(pin, sizeof(pin), "sha256:%064d", 9); (void)dc_peers_add_manual(peers, "bob-id", "bob", "192.168.1.51", 4712, pin); return peers; } static DcModel * make_demo_model(void) { DcModel *model = dc_model_new(); DcConv *conv; DcAccount *account; long long now = (long long)time(NULL); int i; char name[32]; if (model == NULL) return NULL; (void)dc_model_add_account(model, "libera", "irc", "Libera (IRC)"); conv = dc_model_add_conv(model, "libera", "#cde", DC_CONV_CHANNEL, "#cde"); if (conv != NULL) { dc_conv_set_topic(conv, "MCDE development"); dc_conv_set_presence(conv, DC_PRESENCE_ONLINE); (void)dc_conv_add_message(conv, "alice", "hello from the demo model", now - 180, 0); (void)dc_conv_add_message(conv, "kronos", "hi", now - 120, DTCHAT_MSG_SELF); (void)dc_conv_add_message(conv, "bob", "the roster binding works", now - 60, 0); (void)dc_conv_add_message(conv, "alice", "waves at the roster", now - 150, DTCHAT_MSG_ACTION); (void)dc_conv_add_message(conv, "system", "bob joined #cde", now - 90, DTCHAT_MSG_SYSTEM); } conv = dc_model_add_conv(model, "libera", "alice", DC_CONV_DIRECT, "alice"); if (conv != NULL) { dc_conv_set_presence(conv, DC_PRESENCE_AWAY); (void)dc_conv_add_message(conv, "alice", "are you there?", now - 300, 0); } conv = dc_model_add_conv(model, "libera", "#motif", DC_CONV_CHANNEL, "#motif"); if (conv != NULL) { dc_conv_set_presence(conv, DC_PRESENCE_DND); (void)dc_conv_add_message(conv, "carol", "dnd demo", now - 400, 0); } (void)dc_model_add_account(model, "jabber", "xmpp", "Jabber (XMPP)"); conv = dc_model_add_conv(model, "jabber", "bob@example.org", DC_CONV_DIRECT, "Bob"); if (conv != NULL) { dc_conv_set_presence(conv, DC_PRESENCE_ONLINE); (void)dc_conv_add_message(conv, "bob", "xmpp demo", now - 90, 0); } /* enough rows to make the roster scroll */ account = dc_model_add_account(model, "demo", "demo", "Demo rooms"); for (i = 1; i <= 45 && account != NULL; i++) { snprintf(name, sizeof(name), "room%02d", i); conv = dc_model_add_conv(model, "demo", name, DC_CONV_CHANNEL, name); if (conv != NULL) dc_conv_set_presence(conv, i % 3 == 0 ? DC_PRESENCE_OFFLINE : DC_PRESENCE_ONLINE); } return model; } static void demo_video_frame(unsigned char *i420, int n) { int x, y; int left = (n * 5) % (DC_VIDEO_W - 80); int top = (n * 3) % (DC_VIDEO_H - 60); unsigned char *u = i420 + (size_t)DC_VIDEO_W * DC_VIDEO_H; unsigned char *v = u + (size_t)(DC_VIDEO_W / 2) * (DC_VIDEO_H / 2); int cw = DC_VIDEO_W / 2; int ch = DC_VIDEO_H / 2; for (y = 0; y < DC_VIDEO_H; y++) for (x = 0; x < DC_VIDEO_W; x++) { int inside = x >= left && x < left + 80 && y >= top && y < top + 60; i420[(size_t)y * DC_VIDEO_W + x] = (unsigned char)(inside ? 235 : (16 + (x / 3 + y / 2 + n * 6) % 220)); } for (y = 0; y < ch; y++) for (x = 0; x < cw; x++) { u[(size_t)y * cw + x] = (unsigned char)(96 + (x * 3 / 4 + n) % 64); v[(size_t)y * cw + x] = (unsigned char)(160 - (y * 3 / 4 + n) % 64); } } static int call_window_checks(DtChatUi *ui) { unsigned char *i420 = malloc((size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2); if (i420 == NULL) return 0; demo_call = dtchat_call_window_create(dtchat_ui_shell(ui), NULL, "demo peer", NULL, NULL); if (demo_call == NULL) { free(i420); return 0; } demo_video_frame(i420, 0); dtchat_call_window_set_frame(demo_call, i420, DC_VIDEO_W, DC_VIDEO_H); XSync(XtDisplay(dtchat_ui_shell(ui)), False); free(i420); if (!dtchat_call_window_has_frame(demo_call)) return 0; dtchat_call_window_destroy(demo_call); demo_call = NULL; return 1; } static int smoke_ui_checks(DtChatUi *ui) { DcConv *selected; size_t before; int i; if (!dtchat_ui_is_viewable(ui) || !dtchat_ui_smoke(ui)) return 0; dtchat_ui_select(ui, "jabber", "bob@example.org"); selected = dc_model_find_conv(demo_model, "jabber", "bob@example.org"); if (!dtchat_ui_is_current(ui, "jabber", "bob@example.org") || selected == NULL || dc_conv_unread(selected) != 0) return 0; before = dtchat_ui_shown_messages(ui); if (dc_conv_add_message(selected, "bob", "runtime message", (long long)time(NULL), 0) != 0) return 0; dtchat_ui_message_added(ui, "jabber", "bob@example.org"); if (dtchat_ui_shown_messages(ui) <= before || !dtchat_ui_contains(ui, "runtime message")) return 0; if (dtchat_ui_send_text(ui, "smoke send") != 0 || !dtchat_ui_contains(ui, "smoke send")) return 0; /* push past 2000 messages; the view must reload at the ring size */ for (i = 0; i < 5000; i++) { if (dc_conv_add_message(selected, "load", "filler", (long long)time(NULL), 0) != 0) return 0; } dtchat_ui_refresh(ui); if (dtchat_ui_shown_messages(ui) > 2000 || dc_conv_unread(selected) != 0) return 0; /* action and system formatting live in #cde */ dtchat_ui_select(ui, "libera", "#cde"); if (!dtchat_ui_contains(ui, "* alice") || !dtchat_ui_contains(ui, "-- bob joined #cde")) return 0; /* T2.8: Tab completion, Up/Down history, Esc */ dtchat_ui_set_input(ui, "al"); if (!dtchat_ui_complete(ui) || !dtchat_ui_input_equals(ui, "alice ")) return 0; dtchat_ui_set_input(ui, ""); if (dtchat_ui_send_text(ui, "history one") != 0 || dtchat_ui_send_text(ui, "history two") != 0) return 0; if (!dtchat_ui_history(ui, -1) || !dtchat_ui_input_equals(ui, "history two")) return 0; if (!dtchat_ui_history(ui, -1) || !dtchat_ui_input_equals(ui, "history one")) return 0; if (!dtchat_ui_history(ui, 1) || !dtchat_ui_input_equals(ui, "history two")) return 0; if (!dtchat_ui_history(ui, 1) || !dtchat_ui_input_equals(ui, "")) return 0; dtchat_ui_set_input(ui, "draft"); if (!dtchat_ui_history(ui, -1) || !dtchat_ui_input_equals(ui, "history two")) return 0; if (!dtchat_ui_history(ui, 1) || !dtchat_ui_input_equals(ui, "draft")) return 0; dtchat_ui_clear_input(ui); if (!dtchat_ui_input_equals(ui, "")) return 0; /* T5.8: the Nearby section and the peer dialog */ dtchat_ui_select_peer(ui, "alice-id"); if (!dtchat_ui_is_peer(ui, "alice-id") || !dtchat_ui_contains(ui, "Fingerprint: sha256:")) return 0; { DcPeer *peer = dc_peers_find(demo_peers, "alice-id"); DtPeerDialog *dialog; if (peer == NULL) return 0; dialog = dtchat_peer_dialog_create(dtchat_ui_shell(ui), peer, NULL, NULL); if (dialog == NULL) return 0; dtchat_peer_dialog_destroy(dialog); } { DtFileOfferDialog *offer = dtchat_file_offer_dialog( dtchat_ui_shell(ui), "alice", "photo.jpg", 1234567, NULL, NULL); if (offer == NULL) return 0; dtchat_file_offer_dialog_destroy(offer); } if (!call_window_checks(ui)) return 0; return 1; } static void ui_send_test_cb(XtPointer client, XtIntervalId *id) { DtChatUi *ui = (DtChatUi *)client; const char *text = getenv("DTCHAT_TEST_UI_SEND"); const char *file = getenv("DTCHAT_TEST_UI_SEND_FILE"); const char *join = getenv("DTCHAT_TEST_JOIN"); (void)id; if (text != NULL) (void)dtchat_ui_send_text(ui, text); if (file != NULL) (void)dtchat_ui_send_file(ui, file); if (join != NULL) (void)dtchat_ui_join(ui, join); } static void ui_accounts_dialog_cb(XtPointer client, XtIntervalId *id) { (void)id; dtchat_ui_show_accounts((DtChatUi *)client); } static void ui_join_dialog_cb(XtPointer client, XtIntervalId *id) { (void)id; dtchat_ui_join_dialog((DtChatUi *)client); } static void smoke_check(XtPointer client, XtIntervalId *id) { DtChatUi *ui = client; (void)id; XSync(XtDisplay(toplevel), False); if (smoke_ui_checks(ui) && request_checks()) { printf("dtchat smoke: PASS\n"); } else { printf("dtchat smoke: FAIL window, selection, runtime, cap, format or dialogs\n"); smoke_failed = 1; } XtAppSetExitFlag(app); } static void demo_request_cb(XtPointer client, XtIntervalId *id) { DtChatUi *ui = client; const char *kind = demo_kind != NULL ? demo_kind : "qr"; int qr_size; (void)id; if (strcmp(kind, "error") == 0) { dtchat_error_dialog(dtchat_ui_shell(ui), "Telegram", "The adapter could not sign in. Check the code and try again."); return; } if (strcmp(kind, "qr") == 0) { DcJson *request = qr_request(&qr_size); if (request != NULL) { (void)dtchat_request_dialog_create(dtchat_ui_shell(ui), request, 0, answer_cb, NULL); dc_json_free(request); } return; } { DcJson *request = request_json(kind, "dtchat", strcmp(kind, "password") == 0 ? "Password" : "Code from the app", NULL); if (request != NULL) { (void)dtchat_request_dialog_create(dtchat_ui_shell(ui), request, 0, answer_cb, NULL); dc_json_free(request); } } } static char * default_adapter_path(const char *argv0) { const char *slash; char *path; size_t len; if (argv0 == NULL || *argv0 == '\0') return strdup("dtchat-demo"); slash = strrchr(argv0, '/'); if (slash == NULL) return strdup("dtchat-demo"); len = (size_t)(slash - argv0); path = malloc(len + sizeof("/dtchat-demo")); if (path == NULL) return NULL; snprintf(path, len + sizeof("/dtchat-demo"), "%.*s/dtchat-demo", (int)len, argv0); return path; } static void call_first_cb(XtPointer client, XtIntervalId *id) { static int attempts; (void)client; (void)id; if (local_mgr == NULL) { if (++attempts < 20) XtAppAddTimeOut(app, 500, call_first_cb, NULL); return; } if (dtchat_local_call_first(local_mgr) == 0) { fprintf(stderr, "dtchat: call to the first peer started\n"); return; } if (++attempts < 20) XtAppAddTimeOut(app, 500, call_first_cb, NULL); } static void demo_smoke_check(XtPointer client, XtIntervalId *id) { DtChatUi *ui = client; (void)id; XSync(XtDisplay(toplevel), False); if (dtchat_ui_contains(ui, "demo ping") && dtchat_demo_sent(echo) >= 1) { printf("dtchat smoke: PASS\n"); } else { printf("dtchat smoke: FAIL demo adapter round trip\n"); smoke_failed = 1; } XtAppSetExitFlag(app); } static void demo_smoke_send(XtPointer client, XtIntervalId *id) { DtChatUi *ui = client; (void)id; if (!dtchat_ui_is_viewable(ui) || !dtchat_ui_smoke(ui) || !dtchat_demo_online(echo) || dtchat_ui_send_text(ui, "demo ping") != 0) { printf("dtchat smoke: FAIL demo adapter online/select/send\n"); smoke_failed = 1; XtAppSetExitFlag(app); return; } XtAppAddTimeOut(app, 700, demo_smoke_check, ui); } static double ms_since(const struct timespec *start) { struct timespec now; (void)clock_gettime(CLOCK_MONOTONIC, &now); return (double)(now.tv_sec - start->tv_sec) * 1000.0 + (double)(now.tv_nsec - start->tv_nsec) / 1000000.0; } static long proc_kb(const char *key) { FILE *fp = fopen("/proc/self/smaps_rollup", "r"); char line[256]; long value = -1; size_t keylen = strlen(key); if (fp == NULL) return -1; while (fgets(line, sizeof(line), fp) != NULL) { if (strncmp(line, key, keylen) == 0) { value = strtol(line + keylen, NULL, 10); break; } } fclose(fp); return value; } static DcModel * make_perf_model(void) { DcModel *model = dc_model_new(); long long now = (long long)time(NULL); int c, m; if (model == NULL) return NULL; (void)dc_model_add_account(model, "perf", "perf", "Performance"); for (c = 0; c < 50; c++) { char name[32]; DcConv *conv; snprintf(name, sizeof(name), "room%02d", c); conv = dc_model_add_conv(model, "perf", name, DC_CONV_CHANNEL, name); if (conv == NULL) continue; dc_conv_set_presence(conv, c % 3 == 0 ? DC_PRESENCE_OFFLINE : DC_PRESENCE_ONLINE); for (m = 0; m < 200; m++) { char text[96]; int flags = 0; snprintf(text, sizeof(text), "message %03d in %s: the quick brown fox jumps " "over the lazy dog", m, name); if (m % 37 == 0) flags = DTCHAT_MSG_ACTION; else if (m % 53 == 0) flags = DTCHAT_MSG_SYSTEM; (void)dc_conv_add_message(conv, m % 2 ? "alice" : "bob", text, now - 10000 + m, flags); } } return model; } static void perf_report(DtChatUi *ui) { struct rusage ru; double startup = ms_since(&perf_start); double cpu_ms; XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); XSync(XtDisplay(toplevel), False); dtchat_ui_select(ui, "perf", "room49"); XSync(XtDisplay(toplevel), False); getrusage(RUSAGE_SELF, &ru); cpu_ms = (double)ru.ru_utime.tv_sec * 1000.0 + (double)ru.ru_utime.tv_usec / 1000.0 + (double)ru.ru_stime.tv_sec * 1000.0 + (double)ru.ru_stime.tv_usec / 1000.0; printf("perf: conversations=50 messages=10000 startup_ms=%.1f " "select_ms=%.1f cpu_ms=%.1f rss_kb=%ld pss_kb=%ld " "maxrss_kb=%ld\n", startup, ms_since(&perf_start) - startup, cpu_ms, proc_kb("Rss:"), proc_kb("Pss:"), ru.ru_maxrss); fflush(stdout); } int main(int argc, char **argv) { static String fallback[] = { "*fontList: fixed", "Dtchat*roster.fontList: fixed", "Dtchat*transcript.fontList: fixed", "Dtchat*input.fontList: fixed", "Dtchat*status.fontList: fixed", "Dtchat*menuBar.fontList: fixed", NULL }; DtChatUi *ui; const char *demo_request = NULL; const char *adapter_opt = NULL; const char *local_config = NULL; const char *local_listen = NULL; const char *local_downloads = NULL; const char *peer_text[8]; int npeers = 0; char *adapter_path = NULL; int local_mode = 0; int call_demo = 0; int call_first = 0; int demo_flag = 0; int perf_flag = 0; int smoke = 0; int account_mode; int i; DtAccounts *accounts_rt = NULL; /* * Xt does not know --smoke-test; remove it before XtVaAppInitialize * so it is not mistaken for a file argument. */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--smoke-test") == 0) { smoke = 1; memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; } else if (strcmp(argv[i], "--call-first") == 0) { call_first = 1; memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; } else if (strcmp(argv[i], "--call-demo") == 0) { call_demo = 1; memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; } else if (strcmp(argv[i], "--perf-test") == 0) { perf_flag = 1; memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; } else if (strcmp(argv[i], "--local") == 0) { local_mode = 1; memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; } else if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) { local_config = argv[i + 1]; memmove(&argv[i], &argv[i + 2], (size_t)(argc - i - 1) * sizeof(argv[0])); argc -= 2; i--; } else if (strcmp(argv[i], "--listen") == 0 && i + 1 < argc) { local_listen = argv[i + 1]; memmove(&argv[i], &argv[i + 2], (size_t)(argc - i - 1) * sizeof(argv[0])); argc -= 2; i--; } else if (strcmp(argv[i], "--peer") == 0 && i + 1 < argc && npeers < 8) { peer_text[npeers++] = argv[i + 1]; memmove(&argv[i], &argv[i + 2], (size_t)(argc - i - 1) * sizeof(argv[0])); argc -= 2; i--; } else if (strcmp(argv[i], "--downloads") == 0 && i + 1 < argc) { local_downloads = argv[i + 1]; memmove(&argv[i], &argv[i + 2], (size_t)(argc - i - 1) * sizeof(argv[0])); argc -= 2; i--; } else if (strcmp(argv[i], "--demo") == 0) { demo_flag = 1; memmove(&argv[i], &argv[i + 1], (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; } else if (strcmp(argv[i], "--adapter") == 0 && i + 1 < argc) { adapter_opt = argv[i + 1]; memmove(&argv[i], &argv[i + 2], (size_t)(argc - i - 1) * sizeof(argv[0])); argc -= 2; i--; } else if (strcmp(argv[i], "--demo-request") == 0 && i + 1 < argc) { demo_request = argv[i + 1]; demo_kind = demo_request; memmove(&argv[i], &argv[i + 2], (size_t)(argc - i - 1) * sizeof(argv[0])); argc -= 2; i--; } } setlocale(LC_ALL, ""); dc_i18n_init(); XtSetLanguageProc(NULL, NULL, NULL); toplevel = XtVaAppInitialize(&app, "Dtchat", NULL, 0, &argc, argv, fallback, XmNtitle, "dtchat", XmNwidth, 760, XmNheight, 540, NULL); if (local_mode && demo_flag) { fprintf(stderr, "dtchat: --local and --demo are exclusive\n"); return 2; } (void)clock_gettime(CLOCK_MONOTONIC, &perf_start); account_mode = !local_mode && !demo_flag && !perf_flag && !smoke; demo_model = perf_flag ? make_perf_model() : (local_mode || demo_flag || account_mode) ? dc_model_new() : make_demo_model(); demo_peers = (local_mode || perf_flag || account_mode) ? dc_peers_new() : make_demo_peers(); if (demo_model == NULL || demo_peers == NULL) { fprintf(stderr, "dtchat: out of memory\n"); return 1; } ui = dtchat_ui_create(toplevel, demo_model); if (ui == NULL) { fprintf(stderr, "dtchat: out of memory\n"); dc_model_free(demo_model); return 1; } dtchat_ui_set_peers(ui, demo_peers); XtRealizeWidget(toplevel); if (call_first) XtAppAddTimeOut(app, 1000, call_first_cb, NULL); if (call_demo) { unsigned char *i420 = malloc((size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2); XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); if (i420 != NULL) { demo_video_frame(i420, 0); demo_call = dtchat_call_window_create(toplevel, NULL, "alice", NULL, NULL); if (demo_call != NULL) dtchat_call_window_set_frame(demo_call, i420, DC_VIDEO_W, DC_VIDEO_H); free(i420); } } if (perf_flag) { perf_report(ui); dtchat_ui_destroy(ui); dc_model_free(demo_model); dc_peers_free(demo_peers); return 0; } if (local_mode) { char cfg[512]; const char *home; if (local_config != NULL) { snprintf(cfg, sizeof(cfg), "%s", local_config); } else { home = getenv("HOME"); if (home == NULL) { fprintf(stderr, "dtchat: no HOME\n"); return 1; } snprintf(cfg, sizeof(cfg), "%s/.dt/dtchat", home); } local_mgr = dtchat_local_start(ui, demo_model, demo_peers, cfg, local_listen != NULL ? local_listen : "0", peer_text, npeers, local_downloads); if (local_mgr == NULL) { fprintf(stderr, "dtchat: local mode failed\n"); dtchat_ui_destroy(ui); dc_model_free(demo_model); dc_peers_free(demo_peers); return 1; } } if (account_mode) { char bindir[512]; const char *slash = strrchr(argv[0], '/'); if (slash != NULL && slash != argv[0]) snprintf(bindir, sizeof(bindir), "%.*s", (int)(slash - argv[0]), argv[0]); else snprintf(bindir, sizeof(bindir), "."); accounts_rt = dtchat_accounts_start(toplevel, ui, demo_model, local_config, bindir); } if (demo_flag) { adapter_path = adapter_opt != NULL ? strdup(adapter_opt) : default_adapter_path(argv[0]); if (adapter_path == NULL || access(adapter_path, X_OK) != 0) { fprintf(stderr, "dtchat: no demo adapter at %s\n", adapter_path != NULL ? adapter_path : "(null)"); dtchat_ui_destroy(ui); dc_model_free(demo_model); free(adapter_path); return 1; } echo = dtchat_demo_start(toplevel, ui, demo_model, adapter_path); free(adapter_path); if (echo == NULL) { fprintf(stderr, "dtchat: could not start the demo adapter\n"); dtchat_ui_destroy(ui); dc_model_free(demo_model); return 1; } } if (smoke && echo != NULL) { XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); XtAppAddTimeOut(app, 2000, demo_smoke_send, ui); } else if (smoke) { XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); XtAppAddTimeOut(app, 5000, smoke_check, ui); } if (getenv("DTCHAT_TEST_UI_SEND") != NULL || getenv("DTCHAT_TEST_UI_SEND_FILE") != NULL || getenv("DTCHAT_TEST_JOIN") != NULL) XtAppAddTimeOut(app, 2500, ui_send_test_cb, ui); if (getenv("DTCHAT_TEST_JOIN_DIALOG") != NULL) XtAppAddTimeOut(app, 1500, ui_join_dialog_cb, ui); if (getenv("DTCHAT_TEST_ACCOUNTS_DIALOG") != NULL) XtAppAddTimeOut(app, 1500, ui_accounts_dialog_cb, ui); if (demo_request != NULL) { XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); XtAppAddTimeOut(app, 700, demo_request_cb, (XtPointer)ui); } XtAppMainLoop(app); dtchat_call_window_destroy(demo_call); dtchat_local_stop(local_mgr); dtchat_accounts_stop(accounts_rt); dtchat_demo_stop(echo); dtchat_ui_destroy(ui); dc_model_free(demo_model); dc_peers_free(demo_peers); dc_i18n_close(); return smoke_failed ? 1 : 0; }