diff options
author | Astonex <softukitu@gmail.com> | 2013-07-31 23:02:09 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-07-31 23:02:09 +0100 |
commit | f05aa308701f33f3bf9df022a4b376deeedef235 (patch) | |
tree | c10b8a3cca6822400853bfd3bc2c5fd1cca73f83 /testing/toxic/chat.c | |
parent | 8dfba27242ca23fca5de852541f2101568dbf7cb (diff) | |
parent | c558cb63f6db35bd51f2f2331e21df03105ee82a (diff) |
Merge remote-tracking branch 'ProjectTox/master'
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r-- | testing/toxic/chat.c | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c new file mode 100644 index 00000000..dceb1d7b --- /dev/null +++ b/testing/toxic/chat.c | |||
@@ -0,0 +1,155 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <curses.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | #include <stdint.h> | ||
9 | #include <ctype.h> | ||
10 | |||
11 | #include "../../core/Messenger.h" | ||
12 | #include "../../core/network.h" | ||
13 | |||
14 | #include "windows.h" | ||
15 | |||
16 | typedef struct { | ||
17 | int friendnum; | ||
18 | |||
19 | char line[256]; | ||
20 | size_t pos; | ||
21 | |||
22 | WINDOW* history; | ||
23 | WINDOW* linewin; | ||
24 | |||
25 | } ChatContext; | ||
26 | |||
27 | extern void fix_name(uint8_t* name); | ||
28 | |||
29 | |||
30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | ||
31 | ChatContext* ctx = (ChatContext*) self->x; | ||
32 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | ||
33 | |||
34 | if(ctx->friendnum != num) | ||
35 | return; | ||
36 | |||
37 | getname(num, (uint8_t*) &nick); | ||
38 | |||
39 | msg[len-1] = '\0'; | ||
40 | nick[MAX_NAME_LENGTH-1] = '\0'; | ||
41 | |||
42 | fix_name(msg); | ||
43 | fix_name(nick); | ||
44 | |||
45 | wattron(ctx->history, COLOR_PAIR(4)); | ||
46 | wprintw(ctx->history, "%s: ", nick); | ||
47 | wattroff(ctx->history, COLOR_PAIR(4)); | ||
48 | |||
49 | wprintw(ctx->history, "%s\n", msg); | ||
50 | |||
51 | self->blink = true; | ||
52 | } | ||
53 | |||
54 | static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { | ||
55 | ChatContext* ctx = (ChatContext*) self->x; | ||
56 | |||
57 | if(ctx->friendnum != num) | ||
58 | return; | ||
59 | |||
60 | nick[len-1] = '\0'; | ||
61 | fix_name(nick); | ||
62 | |||
63 | wattron(ctx->history, COLOR_PAIR(3)); | ||
64 | wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick); | ||
65 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
66 | } | ||
67 | |||
68 | static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) { | ||
69 | |||
70 | } | ||
71 | |||
72 | static void chat_onKey(ToxWindow* self, int key) { | ||
73 | ChatContext* ctx = (ChatContext*) self->x; | ||
74 | |||
75 | if(isprint(key)) { | ||
76 | |||
77 | if(ctx->pos != sizeof(ctx->line)-1) { | ||
78 | ctx->line[ctx->pos++] = key; | ||
79 | ctx->line[ctx->pos] = '\0'; | ||
80 | } | ||
81 | } | ||
82 | else if(key == '\n') { | ||
83 | wattron(ctx->history, COLOR_PAIR(1)); | ||
84 | wprintw(ctx->history, "you: ", ctx->line); | ||
85 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
86 | |||
87 | wprintw(ctx->history, "%s\n", ctx->line); | ||
88 | |||
89 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | ||
90 | wattron(ctx->history, COLOR_PAIR(3)); | ||
91 | wprintw(ctx->history, " * Failed to send message.\n"); | ||
92 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
93 | } | ||
94 | |||
95 | ctx->line[0] = '\0'; | ||
96 | ctx->pos = 0; | ||
97 | } | ||
98 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | ||
99 | if(ctx->pos != 0) { | ||
100 | ctx->line[--ctx->pos] = '\0'; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | } | ||
105 | |||
106 | static void chat_onDraw(ToxWindow* self) { | ||
107 | int x, y; | ||
108 | ChatContext* ctx = (ChatContext*) self->x; | ||
109 | |||
110 | getmaxyx(self->window, y, x); | ||
111 | |||
112 | (void) x; | ||
113 | if(y < 3) | ||
114 | return; | ||
115 | |||
116 | wclear(ctx->linewin); | ||
117 | mvwhline(ctx->linewin, 0, 0, '_', COLS); | ||
118 | mvwprintw(ctx->linewin, 1, 0, "%s\n", ctx->line); | ||
119 | |||
120 | wrefresh(self->window); | ||
121 | } | ||
122 | |||
123 | static void chat_onInit(ToxWindow* self) { | ||
124 | int x, y; | ||
125 | ChatContext* ctx = (ChatContext*) self->x; | ||
126 | |||
127 | getmaxyx(self->window, y, x); | ||
128 | |||
129 | ctx->history = subwin(self->window, y - 4, x, 0, 0); | ||
130 | scrollok(ctx->history, 1); | ||
131 | |||
132 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | ||
133 | } | ||
134 | |||
135 | ToxWindow new_chat(int friendnum) { | ||
136 | ToxWindow ret; | ||
137 | |||
138 | memset(&ret, 0, sizeof(ret)); | ||
139 | |||
140 | ret.onKey = &chat_onKey; | ||
141 | ret.onDraw = &chat_onDraw; | ||
142 | ret.onInit = &chat_onInit; | ||
143 | ret.onMessage = &chat_onMessage; | ||
144 | ret.onNickChange = &chat_onNickChange; | ||
145 | ret.onStatusChange = &chat_onStatusChange; | ||
146 | |||
147 | snprintf(ret.title, sizeof(ret.title), "[chat %d]", friendnum); | ||
148 | |||
149 | ChatContext* x = calloc(1, sizeof(ChatContext)); | ||
150 | x->friendnum = friendnum; | ||
151 | |||
152 | ret.x = (void*) x; | ||
153 | |||
154 | return ret; | ||
155 | } | ||