green_boy

Example Curses Login Form

Nov 12th, 2025 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.65 KB | Source Code | 0 0
  1. /*
  2.  * CRM LOGIN
  3.  *
  4.  * Main login page for a generic CRM.
  5.  */
  6.  
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <ncurses.h>
  12. #include <form.h>
  13.  
  14. #include "common_util.h"
  15. #include "date_time_util.h"
  16.  
  17. enum {
  18.     FORM_LOGIN_USER,
  19.     FORM_LOGIN_PASS,
  20.     FORM_LOGIN_PROG,
  21.     FORM_LOGIN_DATE,
  22.     FORM_LOGIN_TIME,
  23.     FORM_LOGIN_FMSG,
  24.     FORM_LOGIN_LINE,
  25.     FORM_LOGIN_FIELD_COUNT
  26. };
  27.  
  28. static void driver(FORM *form, int ch)
  29. {
  30.     FIELD **fields, **field;
  31.     char *buffer;
  32.     char *text;
  33.  
  34.     fields = form->field;
  35.     field = NULL;
  36.  
  37.     switch (ch) {
  38.         case KEY_F(2):
  39.         case KEY_ENTER:
  40.         case '\n':
  41.             /* Force a form buffer refresh with some key movement: */
  42.             form_driver(form, REQ_NEXT_FIELD);
  43.             form_driver(form, REQ_PREV_FIELD);
  44.  
  45.             if (
  46.                 strcmp(trim_whitespace(field_buffer(fields[FORM_LOGIN_USER], 0)), "u/green_boy") == 0 &&
  47.                 strcmp(trim_whitespace(field_buffer(fields[FORM_LOGIN_PASS], 0)), "pass") == 0
  48.             ) {
  49.                 set_field_buffer(fields[FORM_LOGIN_FMSG], 0, "Login succeeded");
  50.  
  51.             }
  52.             else {
  53.                 set_field_buffer(fields[FORM_LOGIN_FMSG], 0, "Login failed");
  54.             }
  55.  
  56.             switch(field_just(fields[FORM_LOGIN_FMSG])) {
  57.                 case JUSTIFY_RIGHT:
  58.                     text = "DEBUG: Right justified requested for form message";
  59.                     break;
  60.                 case JUSTIFY_LEFT:
  61.                     text = "DEBUG: Left justified requested for form message";
  62.                     break;
  63.                 case JUSTIFY_CENTER:
  64.                     text = "DEBUG: Centered requested for form message";
  65.                     break;
  66.                 case NO_JUSTIFICATION:
  67.                     text = "DEBUG: Floating requested for form message";
  68.                     break;
  69.                 default:
  70.                     text = "DEBUG: Not set requested for form message";
  71.                     break;
  72.             }
  73.             set_field_buffer(fields[FORM_LOGIN_LINE], 0, text);
  74.  
  75.             /* Reload field values, clearing blanking fields */
  76.             for (field = fields; *field != NULL; field++) {
  77.                 buffer = trim_whitespace(field_buffer(*field, 0));
  78.                 if (field_opts(*field) & O_BLANK) {
  79.                     set_field_buffer(*field, 0, "");
  80.                 }
  81.                 else if (field_opts(*field) & O_EDIT) {
  82.                     set_field_buffer(*field, 0, buffer);
  83.                 }
  84.             }
  85.  
  86.             refresh();
  87.             pos_form_cursor(form);
  88.             break;
  89.  
  90.         case KEY_DOWN:
  91.         case '\t':
  92.             form_driver(form, REQ_NEXT_FIELD);
  93.             form_driver(form, REQ_END_LINE);
  94.             break;
  95.  
  96.         case KEY_UP:
  97.         case KEY_BTAB:
  98.             form_driver(form, REQ_PREV_FIELD);
  99.             form_driver(form, REQ_END_LINE);
  100.             break;
  101.  
  102.         case KEY_LEFT:
  103.             form_driver(form, REQ_PREV_CHAR);
  104.             break;
  105.  
  106.         case KEY_RIGHT:
  107.             form_driver(form, REQ_NEXT_CHAR);
  108.             break;
  109.  
  110.         /* Perform a backspace-delete: */
  111.         case KEY_BACKSPACE:
  112.         case 127:
  113.             form_driver(form, REQ_DEL_PREV);
  114.             break;
  115.  
  116.         /* Perform a forward-delete: */
  117.         case KEY_DC:
  118.             form_driver(form, REQ_DEL_CHAR);
  119.             break;
  120.  
  121.         default:
  122.             form_driver(form, ch);
  123.             break;
  124.     }
  125.  
  126.     wrefresh(form->sub);
  127. }
  128.  
  129. void login_form(void)
  130. {
  131.     static FIELD *fields[FORM_LOGIN_FIELD_COUNT];
  132.     char date[CUR_DATE_MAX_SZ];
  133.     char time[CUR_TIME_MAX_SZ];
  134.  
  135.     WINDOW *win_body, *win_form;
  136.     FORM *form;
  137.     int ch, i;
  138.  
  139.     ch = '\0';
  140.  
  141.     initscr();
  142.     noecho();
  143.     cbreak();
  144.     keypad(stdscr, TRUE);
  145.  
  146.     /* Establish the needed fields with a null terminator: */
  147.     assert_not_null(fields[FORM_LOGIN_USER] = new_field(1, 18, 10, 39, 0, 0));
  148.     assert_not_null(fields[FORM_LOGIN_PASS] = new_field(1, 18, 12, 39, 0, 0));
  149.     assert_not_null(fields[FORM_LOGIN_PROG] = new_field(1, 18, 14, 39, 0, 0));
  150.  
  151.     assert_not_null(fields[FORM_LOGIN_DATE] = new_field(1, 12, 23,  3, 0, 0));
  152.     assert_not_null(fields[FORM_LOGIN_TIME] = new_field(1,  5, 23, 74, 0, 0));
  153.  
  154.     assert_not_null(fields[FORM_LOGIN_FMSG] = new_field(1, 32, 16, 25, 0, 0));
  155.     assert_not_null(fields[FORM_LOGIN_LINE] = new_field(1, 76, 22,  3, 0, 0));
  156.  
  157.     fields[FORM_LOGIN_FIELD_COUNT] = NULL;
  158.  
  159.     /* Set up form field options and formatting: */
  160.     set_field_opts(fields[FORM_LOGIN_USER], O_VISIBLE | O_EDIT | O_ACTIVE | O_PUBLIC);
  161.     set_field_opts(fields[FORM_LOGIN_PASS], O_VISIBLE | O_EDIT | O_ACTIVE | O_BLANK);
  162.     set_field_opts(fields[FORM_LOGIN_PROG], O_VISIBLE | O_EDIT | O_ACTIVE | O_PUBLIC);
  163.     set_field_opts(fields[FORM_LOGIN_DATE], O_VISIBLE | O_PUBLIC);
  164.     set_field_opts(fields[FORM_LOGIN_TIME], O_VISIBLE | O_PUBLIC);
  165.     set_field_opts(fields[FORM_LOGIN_FMSG], O_VISIBLE | O_PUBLIC);
  166.     set_field_opts(fields[FORM_LOGIN_LINE], O_VISIBLE | O_PUBLIC);
  167.  
  168.     set_field_back(fields[FORM_LOGIN_USER], A_UNDERLINE);
  169.     set_field_back(fields[FORM_LOGIN_PASS], A_UNDERLINE);
  170.     set_field_back(fields[FORM_LOGIN_PROG], A_UNDERLINE);
  171.     set_field_back(fields[FORM_LOGIN_LINE], A_UNDERLINE);
  172.     set_field_back(fields[FORM_LOGIN_FMSG], A_UNDERLINE);
  173.  
  174.     set_field_just(fields[FORM_LOGIN_FMSG], JUSTIFY_CENTER);
  175.     set_field_just(fields[FORM_LOGIN_DATE], JUSTIFY_LEFT);
  176.     set_field_just(fields[FORM_LOGIN_TIME], JUSTIFY_RIGHT);
  177.     set_field_just(fields[FORM_LOGIN_LINE], JUSTIFY_CENTER);
  178.  
  179.     /* Set a default application: */
  180.     /* TODO: this is hokey, parameterise field initialisation! */
  181.     set_field_buffer(fields[FORM_LOGIN_PROG], 0, "CRMMAIN");
  182.  
  183.     /*
  184.      * Wire up a form with two windows:
  185.      * - Main window is for labels.
  186.      * - Form window is for the form itself and its fields.
  187.      */
  188.     assert_not_null(form = new_form(fields));
  189.     assert_not_null(win_body = newwin(24, 80, 0, 0));
  190.     assert_not_null(win_form = derwin(win_body, 24, 80, 0, 0));
  191.     set_form_win(form, win_body);
  192.     set_form_sub(form, win_form);
  193.     pos_form_cursor(form);
  194.     post_form(form);
  195.  
  196.     /* Draw out the labels: */
  197.     mvwprintw(win_body,  3, 33, "COMPANY NAME HERE");
  198.     mvwprintw(win_body,  5, 34, "SYSTEM SIGN ON");
  199.     mvwprintw(win_body, 10, 28, "Username :");
  200.     mvwprintw(win_body, 12, 28, "Password :");
  201.     mvwprintw(win_body, 14, 25, "Application :");
  202.  
  203.     get_cur_date(date, sizeof(date));
  204.     get_cur_time(time, sizeof(time));
  205.     set_field_buffer(fields[FORM_LOGIN_DATE], 0, date);
  206.     set_field_buffer(fields[FORM_LOGIN_TIME], 0, time);
  207.     wrefresh(form->sub);
  208.  
  209.     /* Refresh and display: */
  210.     refresh();
  211.     wrefresh(win_body);
  212.     wrefresh(win_form);
  213.  
  214.     while ((ch = getch()) != KEY_F(1))
  215.         driver(form, ch);
  216.  
  217.     unpost_form(form);
  218.     for (i = 0; i < FORM_LOGIN_FIELD_COUNT; i++) {
  219.         free_field(fields[i]);
  220.     }
  221.     free_form(form);
  222.     delwin(win_form);
  223.     delwin(win_body);
  224.     endwin();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment