glogin-provider.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2015 K. Lange
  5. */
  6. /*
  7. * glogin
  8. *
  9. * Graphical Login screen
  10. */
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <unistd.h>
  14. #include <math.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <sys/utsname.h>
  18. #include <sys/wait.h>
  19. #include <sys/time.h>
  20. #include <toaru/graphics.h>
  21. #include <toaru/kbd.h>
  22. #include <toaru/yutani.h>
  23. #include <toaru/auth.h>
  24. #include <toaru/confreader.h>
  25. #include <toaru/sdf.h>
  26. #include <toaru/trace.h>
  27. #define TRACE_APP_NAME "glogin-provider"
  28. static sprite_t logo;
  29. static gfx_context_t * ctx;
  30. static uint16_t win_width;
  31. static uint16_t win_height;
  32. static int uid = 0;
  33. #define USERNAME_BOX 1
  34. #define PASSWORD_BOX 2
  35. static int LOGO_FINAL_OFFSET = 100;
  36. static int BOX_WIDTH = 272;
  37. static int BOX_HEIGHT = 104;
  38. static int BOX_ROUNDNESS = 8;
  39. static int CENTER_BOX_X=1;
  40. static int CENTER_BOX_Y=1;
  41. static int BOX_LEFT=-1;
  42. static int BOX_RIGHT=-1;
  43. static int BOX_TOP=-1;
  44. static int BOX_BOTTOM=-1;
  45. static int BOX_COLOR_R=0;
  46. static int BOX_COLOR_G=0;
  47. static int BOX_COLOR_B=0;
  48. static int BOX_COLOR_A=127;
  49. static char * WALLPAPER = "/usr/share/wallpaper.bmp";
  50. static char * LOGO = "/usr/share/logo_login.bmp";
  51. #define TEXTBOX_INTERIOR_LEFT 4
  52. #define EXTRA_TEXT_OFFSET 15
  53. int center_x(int x) {
  54. return (win_width - x) / 2;
  55. }
  56. int center_y(int y) {
  57. return (win_height - y) / 2;
  58. }
  59. #define INPUT_SIZE 1024
  60. int buffer_put(char * input_buffer, char c) {
  61. int input_collected = strlen(input_buffer);
  62. if (c == 8) {
  63. /* Backspace */
  64. if (input_collected > 0) {
  65. input_collected--;
  66. input_buffer[input_collected] = '\0';
  67. }
  68. return 0;
  69. }
  70. if (c < 10 || (c > 10 && c < 32) || c > 126) {
  71. return 0;
  72. }
  73. input_buffer[input_collected] = c;
  74. input_collected++;
  75. input_buffer[input_collected] = '\0';
  76. if (input_collected == INPUT_SIZE - 1) {
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. struct text_box {
  82. int x;
  83. int y;
  84. unsigned int width;
  85. unsigned int height;
  86. uint32_t text_color;
  87. struct login_container * parent;
  88. int is_focused:1;
  89. int is_password:1;
  90. unsigned int cursor;
  91. char * buffer;
  92. char * placeholder;
  93. };
  94. struct login_container {
  95. int x;
  96. int y;
  97. unsigned int width;
  98. unsigned int height;
  99. struct text_box * username_box;
  100. struct text_box * password_box;
  101. int show_error:1;
  102. };
  103. void draw_text_box(gfx_context_t * ctx, struct text_box * tb) {
  104. int x = tb->parent->x + tb->x;
  105. int y = tb->parent->y + tb->y;
  106. int text_offset = 15;
  107. if (tb->is_focused) {
  108. draw_rounded_rectangle(ctx, 1 + x, 1 + y, tb->width - 2, tb->height - 2, 4, rgb(8,193,236));
  109. draw_rounded_rectangle(ctx, 2 + x, 2 + y, tb->width - 4, tb->height - 4, 4, rgb(244,244,244));
  110. } else {
  111. draw_rounded_rectangle(ctx, 1 + x, 1 + y, tb->width - 2, tb->height - 2, 4, rgb(158,169,177));
  112. }
  113. /* Line width 2? */
  114. char * text = tb->buffer;
  115. char password_circles[512];
  116. uint32_t color = tb->text_color;
  117. if (strlen(tb->buffer) == 0 && !tb->is_focused) {
  118. text = tb->placeholder;
  119. color = rgba(0,0,0,127);
  120. } else if (tb->is_password) {
  121. strcpy(password_circles, "");
  122. for (unsigned int i = 0; i < strlen(tb->buffer); ++i) {
  123. strcat(password_circles, "\007");
  124. }
  125. text = password_circles;
  126. }
  127. draw_sdf_string(ctx, x + TEXTBOX_INTERIOR_LEFT, y + text_offset - 12, text, 15, color, SDF_FONT_THIN);
  128. if (tb->is_focused) {
  129. int width = draw_sdf_string_width(text, 15, SDF_FONT_THIN) + 2;
  130. draw_line(ctx, x + TEXTBOX_INTERIOR_LEFT + width, x + TEXTBOX_INTERIOR_LEFT + width, y + 2, y + text_offset + 1, tb->text_color);
  131. }
  132. }
  133. void draw_login_container(gfx_context_t * ctx, struct login_container * lc) {
  134. /* Draw rounded rectangle */
  135. draw_rounded_rectangle(ctx, lc->x, lc->y, lc->width, lc->height, BOX_ROUNDNESS, rgba(BOX_COLOR_R,BOX_COLOR_G,BOX_COLOR_B,BOX_COLOR_A));
  136. /* Draw labels */
  137. if (lc->show_error) {
  138. char * error_message = "Incorrect username or password.";
  139. //set_font_size(11);
  140. //draw_string(ctx, lc->x + (lc->width - draw_string_width(error_message)) / 2, lc->y + 6 + EXTRA_TEXT_OFFSET, rgb(240, 20, 20), error_message);
  141. draw_sdf_string(ctx, lc->x + (lc->width - draw_sdf_string_width(error_message, 14, SDF_FONT_THIN)) / 2, lc->y + 6 + EXTRA_TEXT_OFFSET - 14, error_message, 14, rgb(240,20,20), SDF_FONT_THIN);
  142. }
  143. draw_text_box(ctx, lc->username_box);
  144. draw_text_box(ctx, lc->password_box);
  145. }
  146. /**
  147. * Get hostname information updated with the current time.
  148. *
  149. * @param hostname
  150. */
  151. static void get_updated_hostname_with_time_info(char hostname[]) {
  152. // get hostname
  153. char _hostname[256];
  154. gethostname(_hostname, 255);
  155. // get current time
  156. struct tm * timeinfo;
  157. struct timeval now;
  158. gettimeofday(&now, NULL); //time(NULL);
  159. timeinfo = localtime((time_t *)&now.tv_sec);
  160. // format the hostname info
  161. char _date[256];
  162. strftime(_date, 256, "%a %B %d %Y", timeinfo);
  163. sprintf(hostname, "%s // %s", _hostname, _date);
  164. }
  165. static void draw_sdf_string_shadow(gfx_context_t * ctx, char * string, int font_size, int left, int top, uint32_t text_color, uint32_t shadow_color, int blur, int font) {
  166. int w = draw_sdf_string_width(string, font_size, font);
  167. sprite_t * _tmp_s = create_sprite(w + blur * 2, font_size + blur * 2, ALPHA_EMBEDDED);
  168. gfx_context_t * _tmp = init_graphics_sprite(_tmp_s);
  169. draw_fill(_tmp, rgba(0,0,0,0));
  170. draw_sdf_string(_tmp, blur, blur, string, font_size, shadow_color, font);
  171. blur_context_box(_tmp, blur);
  172. free(_tmp);
  173. draw_sprite(ctx, _tmp_s, left - blur, top - blur);
  174. sprite_free(_tmp_s);
  175. draw_sdf_string(ctx, left, top, string, 14, text_color, font);
  176. }
  177. int main (int argc, char ** argv) {
  178. if (getuid() != 0) {
  179. return 1;
  180. }
  181. fprintf(stdout, "Hello\n");
  182. yutani_t * y = yutani_init();
  183. if (!y) {
  184. fprintf(stderr, "[glogin] Connection to server failed.\n");
  185. return 1;
  186. }
  187. /* Load config */
  188. {
  189. confreader_t * conf = confreader_load("/etc/glogin.conf");
  190. LOGO_FINAL_OFFSET = confreader_intd(conf, "style", "logo_padding", LOGO_FINAL_OFFSET);
  191. BOX_WIDTH = confreader_intd(conf, "style", "box_width", BOX_WIDTH);
  192. BOX_HEIGHT = confreader_intd(conf, "style", "box_height", BOX_HEIGHT);
  193. BOX_ROUNDNESS = confreader_intd(conf, "style", "box_roundness", BOX_ROUNDNESS);
  194. CENTER_BOX_X = confreader_intd(conf, "style", "center_box_x", CENTER_BOX_X);
  195. CENTER_BOX_Y = confreader_intd(conf, "style", "center_box_y", CENTER_BOX_Y);
  196. BOX_LEFT = confreader_intd(conf, "style", "box_left", BOX_LEFT);
  197. BOX_RIGHT = confreader_intd(conf, "style", "box_right", BOX_RIGHT);
  198. BOX_TOP = confreader_intd(conf, "style", "box_top", BOX_TOP);
  199. BOX_BOTTOM = confreader_intd(conf, "style", "box_bottom", BOX_BOTTOM);
  200. BOX_COLOR_R = confreader_intd(conf, "style", "box_color_r", BOX_COLOR_R);
  201. BOX_COLOR_G = confreader_intd(conf, "style", "box_color_g", BOX_COLOR_G);
  202. BOX_COLOR_B = confreader_intd(conf, "style", "box_color_b", BOX_COLOR_B);
  203. BOX_COLOR_A = confreader_intd(conf, "style", "box_color_a", BOX_COLOR_A);
  204. WALLPAPER = confreader_getd(conf, "image", "wallpaper", WALLPAPER);
  205. LOGO = confreader_getd(conf, "image", "logo", LOGO);
  206. confreader_free(conf);
  207. TRACE("Loading complete");
  208. }
  209. TRACE("Loading logo...");
  210. load_sprite(&logo, LOGO);
  211. logo.alpha = ALPHA_EMBEDDED;
  212. TRACE("... done.");
  213. /* Generate surface for background */
  214. sprite_t * bg_sprite;
  215. int width = y->display_width;
  216. int height = y->display_height;
  217. int skip_animation = 0;
  218. /* Do something with a window */
  219. TRACE("Connecting to window server...");
  220. yutani_window_t * wina = yutani_window_create(y, width, height);
  221. assert(wina);
  222. yutani_set_stack(y, wina, 0);
  223. ctx = init_graphics_yutani_double_buffer(wina);
  224. draw_fill(ctx, rgba(0,0,0,255));
  225. yutani_flip(y, wina);
  226. TRACE("... done.");
  227. redo_everything:
  228. win_width = width;
  229. win_height = height;
  230. #if 0
  231. cairo_surface_t * cs = cairo_image_surface_create_for_data((void*)ctx->backbuffer, CAIRO_FORMAT_ARGB32, ctx->width, ctx->height, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, ctx->width));
  232. cairo_t * cr = cairo_create(cs);
  233. #endif
  234. TRACE("Loading wallpaper...");
  235. {
  236. sprite_t * wallpaper = malloc(sizeof(sprite_t));
  237. load_sprite(wallpaper, WALLPAPER);
  238. float x = (float)width / (float)wallpaper->width;
  239. float y = (float)height / (float)wallpaper->height;
  240. int nh = (int)(x * (float)wallpaper->height);
  241. int nw = (int)(y * (float)wallpaper->width);;
  242. bg_sprite = create_sprite(width, height, ALPHA_OPAQUE);
  243. gfx_context_t * bg = init_graphics_sprite(bg_sprite);
  244. if (nw > width) {
  245. draw_sprite_scaled(bg, wallpaper, (width - nw) / 2, 0, nw, height);
  246. } else {
  247. draw_sprite_scaled(bg, wallpaper, 0, (height - nh) / 2, width, nh);
  248. }
  249. /* Three box blurs = good enough approximation of a guassian, but faster*/
  250. blur_context_box(bg, 20);
  251. blur_context_box(bg, 20);
  252. blur_context_box(bg, 20);
  253. free(bg);
  254. free(wallpaper);
  255. }
  256. TRACE("... done.");
  257. while (1) {
  258. yutani_set_stack(y, wina, 0);
  259. yutani_focus_window(y, wina->wid);
  260. draw_fill(ctx, rgb(0,0,0));
  261. draw_sprite(ctx, bg_sprite, center_x(width), center_y(height));
  262. flip(ctx);
  263. yutani_flip(y, wina);
  264. char * foo = malloc(sizeof(uint32_t) * width * height);
  265. memcpy(foo, ctx->backbuffer, sizeof(uint32_t) * width * height);
  266. TRACE("Begin animation.");
  267. if (!skip_animation) {
  268. struct timeval start;
  269. gettimeofday(&start, NULL);
  270. while (1) {
  271. uint32_t tick;
  272. struct timeval t;
  273. gettimeofday(&t, NULL);
  274. uint32_t sec_diff = t.tv_sec - start.tv_sec;
  275. uint32_t usec_diff = t.tv_usec - start.tv_usec;
  276. if (t.tv_usec < start.tv_usec) {
  277. sec_diff -= 1;
  278. usec_diff = (1000000 + t.tv_usec) - start.tv_usec;
  279. }
  280. tick = (uint32_t)(sec_diff * 1000 + usec_diff / 1000);
  281. int i = (float)LOGO_FINAL_OFFSET * (float)tick / 700.0f;
  282. if (i >= LOGO_FINAL_OFFSET) break;
  283. memcpy(ctx->backbuffer, foo, sizeof(uint32_t) * width * height);
  284. draw_sprite(ctx, &logo, center_x(logo.width), center_y(logo.height) - i);
  285. flip(ctx);
  286. yutani_flip_region(y, wina, center_x(logo.width), center_y(logo.height) - i, logo.width, logo.height + 5);
  287. usleep(10000);
  288. }
  289. }
  290. TRACE("End animation.");
  291. skip_animation = 0;
  292. char username[INPUT_SIZE] = {0};
  293. char password[INPUT_SIZE] = {0};
  294. char hostname[512];
  295. // we do it here to calculate the final string position
  296. get_updated_hostname_with_time_info(hostname);
  297. char kernel_v[512];
  298. {
  299. struct utsname u;
  300. uname(&u);
  301. char * os_name_ = "ToaruOS";
  302. snprintf(kernel_v, 512, "%s %s", os_name_, u.release);
  303. }
  304. uid = 0;
  305. int box_x, box_y;
  306. if (CENTER_BOX_X) {
  307. box_x = center_x(BOX_WIDTH);
  308. } else if (BOX_LEFT == -1) {
  309. box_x = win_width - BOX_RIGHT - BOX_WIDTH;
  310. } else {
  311. box_x = BOX_LEFT;
  312. }
  313. if (CENTER_BOX_Y) {
  314. box_y = center_y(0) + 8;
  315. } else if (BOX_TOP == -1) {
  316. box_y = win_width - BOX_BOTTOM - BOX_HEIGHT;
  317. } else {
  318. box_y = BOX_TOP;
  319. }
  320. int focus = 0;
  321. //set_font_size(11);
  322. int hostname_label_left = width - 10 - draw_sdf_string_width(hostname, 14, SDF_FONT_BOLD);
  323. int kernel_v_label_left = 10;
  324. struct text_box username_box = { (BOX_WIDTH - 170) / 2, 30, 170, 20, rgb(0,0,0), NULL, 0, 0, 0, username, "Username" };
  325. struct text_box password_box = { (BOX_WIDTH - 170) / 2, 58, 170, 20, rgb(0,0,0), NULL, 0, 1, 0, password, "Password" };
  326. struct login_container lc = { box_x, box_y, BOX_WIDTH, BOX_HEIGHT, &username_box, &password_box, 0 };
  327. username_box.parent = &lc;
  328. password_box.parent = &lc;
  329. while (1) {
  330. focus = 0;
  331. memset(username, 0x0, INPUT_SIZE);
  332. memset(password, 0x0, INPUT_SIZE);
  333. while (1) {
  334. // update time info
  335. get_updated_hostname_with_time_info(hostname);
  336. memcpy(ctx->backbuffer, foo, sizeof(uint32_t) * width * height);
  337. draw_sprite(ctx, &logo, center_x(logo.width), center_y(logo.height) - LOGO_FINAL_OFFSET);
  338. draw_sdf_string_shadow(ctx, hostname, 14, hostname_label_left, height - 22, rgb(255,255,255), rgb(0,0,0), 4, SDF_FONT_BOLD);
  339. draw_sdf_string_shadow(ctx, kernel_v, 14, kernel_v_label_left, height - 22, rgb(255,255,255), rgb(0,0,0), 4, SDF_FONT_BOLD);
  340. if (focus == USERNAME_BOX) {
  341. username_box.is_focused = 1;
  342. password_box.is_focused = 0;
  343. } else if (focus == PASSWORD_BOX) {
  344. username_box.is_focused = 0;
  345. password_box.is_focused = 1;
  346. } else {
  347. username_box.is_focused = 0;
  348. password_box.is_focused = 0;
  349. }
  350. draw_login_container(ctx, &lc);
  351. flip(ctx);
  352. yutani_flip(y, wina);
  353. struct yutani_msg_key_event kbd;
  354. struct yutani_msg_window_mouse_event mou;
  355. int msg_type = 0;
  356. collect_events:
  357. do {
  358. yutani_msg_t * msg = yutani_poll(y);
  359. switch (msg->type) {
  360. case YUTANI_MSG_KEY_EVENT:
  361. {
  362. struct yutani_msg_key_event * ke = (void*)msg->data;
  363. if (ke->event.action == KEY_ACTION_DOWN) {
  364. memcpy(&kbd, ke, sizeof(struct yutani_msg_key_event));
  365. msg_type = 1;
  366. }
  367. }
  368. break;
  369. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  370. {
  371. struct yutani_msg_window_mouse_event * me = (void*)msg->data;
  372. memcpy(&mou, me, sizeof(struct yutani_msg_mouse_event));
  373. msg_type = 2;
  374. }
  375. break;
  376. case YUTANI_MSG_WELCOME:
  377. {
  378. struct yutani_msg_welcome * mw = (void*)msg->data;
  379. yutani_window_resize(y, wina, mw->display_width, mw->display_height);
  380. }
  381. break;
  382. case YUTANI_MSG_RESIZE_OFFER:
  383. {
  384. struct yutani_msg_window_resize * wr = (void*)msg->data;
  385. width = wr->width;
  386. height = wr->height;
  387. yutani_window_resize_accept(y, wina, width, height);
  388. reinit_graphics_yutani(ctx, wina);
  389. yutani_window_resize_done(y, wina);
  390. sprite_free(bg_sprite);
  391. //cairo_destroy(cr);
  392. //cairo_surface_destroy(cs);
  393. skip_animation = 1;
  394. goto redo_everything;
  395. }
  396. break;
  397. }
  398. free(msg);
  399. } while (!msg_type);
  400. if (msg_type == 1) {
  401. if (kbd.event.keycode == '\n') {
  402. if (focus == USERNAME_BOX) {
  403. focus = PASSWORD_BOX;
  404. continue;
  405. } else if (focus == PASSWORD_BOX) {
  406. break;
  407. } else {
  408. focus = USERNAME_BOX;
  409. continue;
  410. }
  411. }
  412. if (kbd.event.keycode == '\t') {
  413. if (focus == USERNAME_BOX) {
  414. focus = PASSWORD_BOX;
  415. } else {
  416. focus = USERNAME_BOX;
  417. }
  418. continue;
  419. }
  420. if (kbd.event.key) {
  421. if (!focus) {
  422. focus = USERNAME_BOX;
  423. }
  424. if (focus == USERNAME_BOX) {
  425. buffer_put(username, kbd.event.key);
  426. } else if (focus == PASSWORD_BOX) {
  427. buffer_put(password, kbd.event.key);
  428. }
  429. }
  430. } else if (msg_type == 2) {
  431. if ((mou.command == YUTANI_MOUSE_EVENT_DOWN
  432. && mou.buttons & YUTANI_MOUSE_BUTTON_LEFT)
  433. || (mou.command == YUTANI_MOUSE_EVENT_CLICK)) {
  434. /* Determine if we were inside of a text box */
  435. if (mou.new_x >= (int)lc.x + (int)username_box.x &&
  436. mou.new_x <= (int)lc.x + (int)username_box.x + (int)username_box.width &&
  437. mou.new_y >= (int)lc.y + (int)username_box.y &&
  438. mou.new_y <= (int)lc.y + (int)username_box.y + (int)username_box.height) {
  439. /* Ensure this box is focused. */
  440. focus = USERNAME_BOX;
  441. continue;
  442. } else if (
  443. (int)mou.new_x >= (int)lc.x + (int)password_box.x &&
  444. (int)mou.new_x <= (int)lc.x + (int)password_box.x + (int)password_box.width &&
  445. (int)mou.new_y >= (int)lc.y + (int)password_box.y &&
  446. (int)mou.new_y <= (int)lc.y + (int)password_box.y + (int)password_box.height) {
  447. /* Ensure this box is focused. */
  448. focus = PASSWORD_BOX;
  449. continue;
  450. } else {
  451. focus = 0;
  452. continue;
  453. }
  454. } else {
  455. goto collect_events;
  456. }
  457. }
  458. }
  459. fprintf(stdout, "USER %s\n", username);
  460. fprintf(stdout, "PASS %s\n", password);
  461. fprintf(stdout, "AUTH\n");
  462. char tmp[1024];
  463. fgets(tmp, 1024, stdin);
  464. if (!strcmp(tmp,"FAIL\n")) {
  465. lc.show_error = 1;
  466. continue;
  467. } else if (!strcmp(tmp,"SUCC\n")) {
  468. fprintf(stderr,"Success!\n");
  469. goto _success;
  470. }
  471. }
  472. }
  473. _success:
  474. yutani_close(y, wina);
  475. return 0;
  476. }