2013-07-30 01:32:39 +02:00
|
|
|
/*
|
2013-09-02 19:19:25 +02:00
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
2013-07-30 01:32:39 +02:00
|
|
|
|
2013-08-23 09:50:04 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 01:32:39 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-21 02:35:03 +02:00
|
|
|
#include "toxic_windows.h"
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "prompt.h"
|
2013-11-10 03:43:56 +01:00
|
|
|
#include "execute.h"
|
2013-09-21 02:35:03 +02:00
|
|
|
#include "misc_tools.h"
|
2013-08-17 11:59:28 +02:00
|
|
|
|
2013-10-16 23:59:56 +02:00
|
|
|
uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0};
|
2013-09-15 22:38:38 +02:00
|
|
|
uint8_t num_frnd_requests = 0;
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
/* Updates own nick in prompt statusbar */
|
2013-09-18 23:30:35 +02:00
|
|
|
void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len)
|
2013-09-07 01:59:45 +02:00
|
|
|
{
|
2013-11-29 23:48:08 +01:00
|
|
|
StatusBar *statusbar = prompt->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
2013-09-18 23:30:35 +02:00
|
|
|
statusbar->nick_len = len;
|
2013-09-07 01:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates own statusmessage in prompt statusbar */
|
2013-09-18 23:30:35 +02:00
|
|
|
void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg, uint16_t len)
|
2013-09-07 01:59:45 +02:00
|
|
|
{
|
2013-11-29 23:48:08 +01:00
|
|
|
StatusBar *statusbar = prompt->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
2013-09-18 23:30:35 +02:00
|
|
|
statusbar->statusmsg_len = len;
|
2013-09-07 01:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Updates own status in prompt statusbar */
|
|
|
|
void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status)
|
|
|
|
{
|
2013-11-29 23:48:08 +01:00
|
|
|
StatusBar *statusbar = prompt->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
statusbar->status = status;
|
|
|
|
}
|
|
|
|
|
2013-09-09 06:56:47 +02:00
|
|
|
/* Updates own connection status in prompt statusbar */
|
2013-09-07 03:37:16 +02:00
|
|
|
void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected)
|
|
|
|
{
|
2013-11-29 23:48:08 +01:00
|
|
|
StatusBar *statusbar = prompt->stb;
|
2013-09-07 03:37:16 +02:00
|
|
|
statusbar->is_online = is_connected;
|
|
|
|
}
|
|
|
|
|
2013-09-16 06:28:28 +02:00
|
|
|
/* Adds friend request to pending friend requests.
|
2013-10-16 23:59:56 +02:00
|
|
|
Returns request number on success, -1 if queue is full or other error. */
|
|
|
|
static int add_friend_request(uint8_t *public_key)
|
2013-09-06 08:51:10 +02:00
|
|
|
{
|
2013-12-01 22:57:05 +01:00
|
|
|
if (num_frnd_requests >= MAX_FRIENDS_NUM)
|
2013-12-01 23:08:57 +01:00
|
|
|
return -1;
|
2013-12-01 22:57:05 +01:00
|
|
|
|
|
|
|
int i;
|
2013-10-16 23:59:56 +02:00
|
|
|
|
2013-12-01 22:57:05 +01:00
|
|
|
for (i = 0; i <= num_frnd_requests; ++i) {
|
|
|
|
if (!strlen(pending_frnd_requests[i])) {
|
|
|
|
memcpy(pending_frnd_requests[i], public_key, TOX_CLIENT_ID_SIZE);
|
2013-10-16 23:59:56 +02:00
|
|
|
|
2013-12-01 22:57:05 +01:00
|
|
|
if (i == num_frnd_requests)
|
|
|
|
++num_frnd_requests;
|
2013-10-16 23:59:56 +02:00
|
|
|
|
2013-12-01 22:57:05 +01:00
|
|
|
return i;
|
2013-10-16 23:59:56 +02:00
|
|
|
}
|
2013-09-16 06:28:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-09-06 08:51:10 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-12-05 04:33:04 +01:00
|
|
|
PromptBuf *prt = self->promptbuf;
|
2013-12-04 09:57:03 +01:00
|
|
|
|
2013-10-23 09:24:08 +02:00
|
|
|
int x, y, y2, x2;
|
|
|
|
getyx(self->window, y, x);
|
|
|
|
getmaxyx(self->window, y2, x2);
|
|
|
|
|
|
|
|
/* BACKSPACE key: Remove one character from line */
|
|
|
|
if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->pos > 0)
|
|
|
|
del_char_buf_bck(prt->line, &prt->pos, &prt->len);
|
2013-12-04 22:14:33 +01:00
|
|
|
wmove(self->window, y, x-1); /* not necessary but fixes a display glitch */
|
2013-12-05 04:33:04 +01:00
|
|
|
prt->scroll = false;
|
2013-12-04 22:14:33 +01:00
|
|
|
}
|
2013-10-23 09:24:08 +02:00
|
|
|
|
2013-12-04 22:14:33 +01:00
|
|
|
else if (key == KEY_DC) { /* DEL key: Remove character at pos */
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->pos != prt->len) {
|
|
|
|
del_char_buf_frnt(prt->line, &prt->pos, &prt->len);
|
|
|
|
prt->scroll = false;
|
|
|
|
}
|
2013-12-04 22:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->pos != 0)
|
|
|
|
prt->pos = 0;
|
2013-12-04 22:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_END) { /* END key: move cursor to end of line */
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->pos != prt->len)
|
|
|
|
prt->pos = prt->len;
|
2013-12-04 22:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_LEFT) {
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->pos > 0)
|
|
|
|
--prt->pos;
|
2013-12-04 22:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_RIGHT) {
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->pos < prt->len)
|
|
|
|
++prt->pos;
|
2013-12-04 09:57:03 +01:00
|
|
|
} else
|
|
|
|
#if HAVE_WIDECHAR
|
|
|
|
if (iswprint(key))
|
|
|
|
#else
|
|
|
|
if (isprint(key))
|
|
|
|
#endif
|
|
|
|
{
|
2013-12-05 04:33:04 +01:00
|
|
|
if (prt->len < (MAX_STR_SIZE-1)) {
|
|
|
|
add_char_to_buf(prt->line, &prt->pos, &prt->len, key);
|
|
|
|
prt->scroll = true;
|
|
|
|
}
|
2013-08-06 12:16:17 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
/* RETURN key: execute command */
|
|
|
|
else if (key == '\n') {
|
2013-12-04 22:14:33 +01:00
|
|
|
wprintw(self->window, "\n");
|
2013-12-05 04:33:04 +01:00
|
|
|
uint8_t *line = wcs_to_char(prt->line);
|
2013-12-04 09:57:03 +01:00
|
|
|
execute(self->window, self, m, line, GLOBAL_COMMAND_MODE);
|
2013-12-05 04:33:04 +01:00
|
|
|
reset_buf(prt->line, &prt->pos, &prt->len);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void prompt_onDraw(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-12-05 04:33:04 +01:00
|
|
|
PromptBuf *prt = self->promptbuf;
|
2013-09-28 01:55:11 +02:00
|
|
|
|
2013-12-04 09:57:03 +01:00
|
|
|
curs_set(1);
|
2013-09-28 01:55:11 +02:00
|
|
|
int x, y, x2, y2;
|
2013-09-07 01:59:45 +02:00
|
|
|
getyx(self->window, y, x);
|
2013-09-28 01:55:11 +02:00
|
|
|
getmaxyx(self->window, y2, x2);
|
2013-12-05 04:33:04 +01:00
|
|
|
wclrtobot(self->window);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-12-05 04:33:04 +01:00
|
|
|
/* if len is >= screen width offset max x by X_OFST to account for prompt char */
|
|
|
|
int px2 = prt->len >= x2 ? x2 : x2 - X_OFST;
|
2013-12-05 00:09:51 +01:00
|
|
|
|
2013-12-05 04:33:04 +01:00
|
|
|
if (px2 <= 0)
|
|
|
|
return;
|
2013-12-04 22:14:33 +01:00
|
|
|
|
2013-12-05 04:33:04 +01:00
|
|
|
/* len offset to account for prompt char (0 if len is < width of screen) */
|
|
|
|
int p_ofst = px2 != x2 ? 0 : X_OFST;
|
|
|
|
|
|
|
|
if (prt->len > 0) {
|
|
|
|
mvwprintw(self->window, prt->orig_y, X_OFST, wcs_to_char(prt->line));
|
|
|
|
|
|
|
|
/* y distance between pos and len */
|
2013-12-05 07:22:02 +01:00
|
|
|
int d = prt->pos < (prt->len - px2) ? (y2 - y - 1) : 0;
|
|
|
|
|
|
|
|
/* 1 if end of line is touching bottom of window, 0 otherwise */
|
|
|
|
int bot = prt->orig_y + ((prt->len + p_ofst) / px2) == y2;
|
2013-12-05 00:09:51 +01:00
|
|
|
|
2013-12-04 22:14:33 +01:00
|
|
|
/* move point of line origin up when input scrolls screen down */
|
2013-12-05 07:22:02 +01:00
|
|
|
if (prt->scroll && (y + d == y2 - bot) && ((prt->len + p_ofst) % px2 == 0) ) {
|
2013-12-05 04:33:04 +01:00
|
|
|
--prt->orig_y;
|
|
|
|
prt->scroll = false;
|
|
|
|
}
|
2013-12-05 00:09:51 +01:00
|
|
|
|
|
|
|
} else {
|
2013-12-05 04:33:04 +01:00
|
|
|
prt->orig_y = y; /* Mark point of origin for new line */
|
|
|
|
|
2013-12-05 00:09:51 +01:00
|
|
|
wattron(self->window, COLOR_PAIR(GREEN));
|
|
|
|
mvwprintw(self->window, y, 0, "# ");
|
|
|
|
wattroff(self->window, COLOR_PAIR(GREEN));
|
2013-12-04 22:14:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 23:48:08 +01:00
|
|
|
StatusBar *statusbar = self->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
werase(statusbar->topline);
|
2013-11-24 05:46:46 +01:00
|
|
|
mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2);
|
2013-09-28 01:55:11 +02:00
|
|
|
wmove(statusbar->topline, 0, 0);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-09-07 03:37:16 +02:00
|
|
|
if (statusbar->is_online) {
|
2013-09-07 01:59:45 +02:00
|
|
|
int colour = WHITE;
|
|
|
|
char *status_text = "Unknown";
|
|
|
|
|
2013-11-12 07:50:04 +01:00
|
|
|
switch (statusbar->status) {
|
2013-09-07 01:59:45 +02:00
|
|
|
case TOX_USERSTATUS_NONE:
|
|
|
|
status_text = "Online";
|
|
|
|
colour = GREEN;
|
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_AWAY:
|
|
|
|
status_text = "Away";
|
|
|
|
colour = YELLOW;
|
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_BUSY:
|
|
|
|
status_text = "Busy";
|
|
|
|
colour = RED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-09-09 06:56:47 +02:00
|
|
|
wprintw(statusbar->topline, " %s ", statusbar->nick);
|
2013-09-07 01:59:45 +02:00
|
|
|
wattron(statusbar->topline, A_BOLD);
|
|
|
|
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
|
|
|
|
wprintw(statusbar->topline, "[%s]", status_text);
|
|
|
|
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
|
|
|
|
} else {
|
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-11-19 00:52:46 +01:00
|
|
|
wprintw(statusbar->topline, " %s ", statusbar->nick);
|
2013-09-07 01:59:45 +02:00
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
|
|
|
wprintw(statusbar->topline, "[Offline]");
|
|
|
|
}
|
|
|
|
|
2013-09-08 09:18:34 +02:00
|
|
|
wattron(statusbar->topline, A_BOLD);
|
2013-11-24 05:46:46 +01:00
|
|
|
wprintw(statusbar->topline, " - %s", statusbar->statusmsg);
|
2013-09-08 09:18:34 +02:00
|
|
|
wattroff(statusbar->topline, A_BOLD);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-12-04 22:14:33 +01:00
|
|
|
/* put cursor back in correct spot */
|
2013-12-05 04:33:04 +01:00
|
|
|
int y_m = prt->pos <= 0 ? prt->orig_y : prt->orig_y + ((prt->pos + p_ofst) / px2);
|
|
|
|
int x_m = prt->pos > 0 ? (prt->pos + X_OFST) % x2 : X_OFST;
|
2013-12-04 22:14:33 +01:00
|
|
|
wmove(self->window, y_m, x_m);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void prompt_onInit(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-28 01:55:11 +02:00
|
|
|
scrollok(self->window, true);
|
2013-11-19 21:32:35 +01:00
|
|
|
execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE);
|
2013-08-16 19:11:09 +02:00
|
|
|
wclrtoeol(self->window);
|
2013-07-30 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 07:22:21 +02:00
|
|
|
static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , uint8_t status)
|
|
|
|
{
|
|
|
|
if (friendnum < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
|
|
|
|
2013-11-29 16:14:59 +01:00
|
|
|
if (tox_get_name(m, friendnum, nick) == -1)
|
2013-09-23 07:22:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!nick[0])
|
|
|
|
snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME);
|
|
|
|
|
|
|
|
if (status == 1) {
|
|
|
|
wattron(self->window, COLOR_PAIR(GREEN));
|
|
|
|
wattron(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "\n%s ", nick);
|
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "has come online\n");
|
|
|
|
wattroff(self->window, COLOR_PAIR(GREEN));
|
|
|
|
} else {
|
|
|
|
wattron(self->window, COLOR_PAIR(RED));
|
|
|
|
wattron(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "\n%s ", nick);
|
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "has gone offline\n");
|
|
|
|
wattroff(self->window, COLOR_PAIR(RED));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-21 02:35:03 +02:00
|
|
|
static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length)
|
2013-09-15 22:38:38 +02:00
|
|
|
{
|
2013-11-28 13:20:52 +01:00
|
|
|
// make sure message data is null-terminated
|
|
|
|
data[length - 1] = 0;
|
|
|
|
|
2013-11-18 05:14:27 +01:00
|
|
|
wprintw(self->window, "\nFriend request with the message: %s\n", data);
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-10-16 23:59:56 +02:00
|
|
|
int n = add_friend_request(key);
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-09-22 10:32:51 +02:00
|
|
|
if (n == -1) {
|
|
|
|
wprintw(self->window, "Friend request queue is full. Discarding request.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintw(self->window, "Type \"/accept %d\" to accept it.\n", n);
|
2013-11-29 01:45:28 +01:00
|
|
|
alert_window(self, WINDOW_ALERT_2, true);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 01:59:45 +02:00
|
|
|
void prompt_init_statusbar(ToxWindow *self, Tox *m)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getmaxyx(self->window, y, x);
|
|
|
|
|
|
|
|
/* Init statusbar info */
|
2013-11-29 23:48:08 +01:00
|
|
|
StatusBar *statusbar = self->stb;
|
2013-09-07 01:59:45 +02:00
|
|
|
statusbar->status = TOX_USERSTATUS_NONE;
|
2013-09-07 03:39:22 +02:00
|
|
|
statusbar->is_online = false;
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_get_self_name(m, nick, TOX_MAX_NAME_LENGTH);
|
2013-09-07 01:59:45 +02:00
|
|
|
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
|
|
|
|
|
2013-09-08 09:18:34 +02:00
|
|
|
/* temporary until statusmessage saving works */
|
2013-12-04 22:21:32 +01:00
|
|
|
uint8_t *statusmsg = "Toxing on Toxic v.0.2.4";
|
2013-09-09 06:56:47 +02:00
|
|
|
m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1);
|
2013-09-08 09:18:34 +02:00
|
|
|
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
2013-09-07 01:59:45 +02:00
|
|
|
|
|
|
|
/* Init statusbar subwindow */
|
|
|
|
statusbar->topline = subwin(self->window, 2, x, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-09-19 23:03:22 +02:00
|
|
|
ToxWindow new_prompt(void)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
ToxWindow ret;
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
ret.active = true;
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onKey = &prompt_onKey;
|
|
|
|
ret.onDraw = &prompt_onDraw;
|
|
|
|
ret.onInit = &prompt_onInit;
|
2013-09-23 07:22:21 +02:00
|
|
|
ret.onConnectionChange = &prompt_onConnectionChange;
|
2013-09-06 08:51:10 +02:00
|
|
|
ret.onFriendRequest = &prompt_onFriendRequest;
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
strcpy(ret.name, "prompt");
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-12-05 04:33:04 +01:00
|
|
|
PromptBuf *promptbuf = calloc(1, sizeof(PromptBuf));
|
2013-09-13 08:02:49 +02:00
|
|
|
StatusBar *stb = calloc(1, sizeof(StatusBar));
|
2013-09-12 00:07:26 +02:00
|
|
|
|
2013-12-05 04:33:04 +01:00
|
|
|
if (stb != NULL && promptbuf != NULL) {
|
|
|
|
ret.promptbuf = promptbuf;
|
2013-09-13 08:02:49 +02:00
|
|
|
ret.stb = stb;
|
2013-12-04 09:57:03 +01:00
|
|
|
} else {
|
2013-09-12 00:07:26 +02:00
|
|
|
endwin();
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "calloc() failed. Aborting...\n");
|
2013-09-12 00:07:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-09-07 01:59:45 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
return ret;
|
2013-12-01 22:59:46 +01:00
|
|
|
}
|