[go: up one dir, main page]

Menu

[3dc168]: / ui.c  Maximize  Restore  History

Download this file

155 lines (139 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This file is part of the loginx project
//
// Copyright (c) 2013 by Mike Sharov <msharov@users.sourceforge.net>
// This file is free software, distributed under the ISC license.
#include "defs.h"
#include <ncurses.h>
#include <ctype.h>
#ifndef COLOR_DEFAULT
#define COLOR_DEFAULT -1
#endif
#define min(a,b) ((a)<(b)?(a):(b))
enum {
MAX_PROMPT_WIDTH = sizeof(PASSWORD_PROMPT)-1,
MAX_INPUT_WIDTH = sizeof(PASSWORD_MASKSTR),
LOGIN_WINDOW_WIDTH = 1+1+MAX_PROMPT_WIDTH+1+MAX_INPUT_WIDTH+1+1,
LOGIN_WINDOW_HEIGHT = 1+2+1,
MSG_WINDOW_HEIGHT = 8
};
enum {
colorpair_None,
colorpair_StaticText,
colorpair_Input,
colorpair_Error
};
//----------------------------------------------------------------------
static WINDOW* _loginbox = NULL;
static WINDOW* _msgboxframe = NULL;
static WINDOW* _msgbox = NULL;
//----------------------------------------------------------------------
static void CursesInit (void)
{
static bool s_initialized = false;
if (s_initialized)
return;
if (!initscr())
ExitWithMessage ("failed to initialize curses");
atexit (CursesCleanup);
start_color();
use_default_colors();
init_pair (colorpair_StaticText, COLOR_GREEN, COLOR_DEFAULT);
init_pair (colorpair_Input, COLOR_CYAN, COLOR_DEFAULT);
init_pair (colorpair_Error, COLOR_RED, COLOR_DEFAULT);
noecho();
s_initialized = true;
}
void CursesCleanup (void)
{
if (isendwin())
return;
#define CLDWIN(w)\
if (w) {\
wclear (w);\
wnoutrefresh (w);\
delwin (w);\
w = NULL;\
}
CLDWIN(_loginbox)
CLDWIN(_msgbox)
CLDWIN(_msgboxframe)
#undef CLDWIN
wclear (stdscr);
wnoutrefresh (stdscr);
doupdate();
endwin();
}
void LoginBox (char* password, size_t passwordsz)
{
CursesInit();
if (!_loginbox) {
_loginbox = newwin (LOGIN_WINDOW_HEIGHT, LOGIN_WINDOW_WIDTH, (LINES-LOGIN_WINDOW_HEIGHT)/2, (COLS-LOGIN_WINDOW_WIDTH)/2);
if (!_loginbox)
ExitWithMessage ("failed to create login window");
keypad (_loginbox, true);
nodelay (_loginbox, false);
}
int key;
unsigned pwlen = 0;
acclist_t al = ReadAccounts();
const unsigned aln = NAccounts();
unsigned ali = SelectedAccount();
if (!aln || ali >= aln)
ExitWithMessage ("no usable accounts found");
memset (password, 0, passwordsz);
do {
wattr_set (_loginbox, A_NORMAL, colorpair_StaticText, NULL);
werase (_loginbox);
box (_loginbox, 0, 0);
mvwaddstr (_loginbox, 1,2, USERNAME_PROMPT);
mvwaddstr (_loginbox, 2,2, PASSWORD_PROMPT);
wattr_set (_loginbox, A_NORMAL, colorpair_Input, NULL);
mvwaddnstr (_loginbox, 1,2+sizeof(USERNAME_PROMPT), al[ali]->name, MAX_INPUT_WIDTH);
mvwhline (_loginbox, 2,2+sizeof(PASSWORD_PROMPT), ' ', strlen(PASSWORD_MASKSTR)+1);
mvwaddnstr (_loginbox, 2,2+sizeof(PASSWORD_PROMPT), PASSWORD_MASKSTR, min(strlen(PASSWORD_MASKSTR),pwlen));
wrefresh (_loginbox);
key = wgetch (_loginbox);
if (isprint(key) && pwlen < passwordsz-1)
password[pwlen++] = key;
else if (key == KEY_BACKSPACE && pwlen > 0)
password[--pwlen] = 0;
else if (key == KEY_UP)
ali = (ali-1) % aln;
else if (key == KEY_DOWN || key == '\t')
ali = (ali+1) % aln;
} while (key != '\n');
SetSelectedAccount (ali);
}
void AddMessage (const char* msg, bool is_error)
{
CursesInit();
if (!_msgboxframe) {
_msgboxframe = newwin (MSG_WINDOW_HEIGHT+2, COLS, LINES-MSG_WINDOW_HEIGHT-2, 0);
if (!_msgboxframe)
return;
wattr_set (_msgboxframe, A_NORMAL, colorpair_StaticText, NULL);
box (_msgboxframe, 0, 0);
wnoutrefresh (_msgboxframe);
}
if (!_msgbox) {
_msgbox = newwin (getmaxy(_msgboxframe)-2, getmaxx(_msgboxframe)-2, getbegy(_msgboxframe)+1, getbegx(_msgboxframe)+1);
if (!_msgbox)
return;
scrollok (_msgbox, true);
}
if (is_error)
wattr_set (_msgbox, A_BOLD, colorpair_Error, NULL);
else
wattr_set (_msgbox, A_NORMAL, colorpair_StaticText, NULL);
waddstr (_msgbox, msg);
waddstr (_msgbox, "\n");
wrefresh (_msgbox);
}
void ClearScreen (void)
{
if (!isatty (STDOUT_FILENO))
return;
// Clear the screen; c resets terminal, [r resets scroll region, [H homes cursor, [J erases
#define RESET_SCREEN_CMD "\ec\e[r\e[H\e[J"
write (STDOUT_FILENO, RESET_SCREEN_CMD, sizeof(RESET_SCREEN_CMD));
}