webperimental: killstack decides stack protects.
[freeciv.git] / client / chatline_common.c
blobef44382253c63085462550ff3ecb756840d42cca
1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <stdarg.h>
19 #include <string.h>
21 /* utility */
22 #include "astring.h"
23 #include "fcintl.h"
24 #include "fcthread.h"
25 #include "fc_utf8.h"
26 #include "log.h"
28 /* common */
29 #include "featured_text.h"
30 #include "packets.h"
32 /* include */
33 #include "chatline_g.h"
35 /* client */
36 #include "client_main.h"
37 #include "options.h"
39 #include "chatline_common.h"
41 static fc_mutex ow_mutex;
43 /****************************************************************************
44 Send the message as a chat to the server.
45 ****************************************************************************/
46 int send_chat(const char *message)
48 return dsend_packet_chat_msg_req(&client.conn, message);
51 /****************************************************************************
52 Send the message as a chat to the server. Message is constructed
53 in printf style.
54 ****************************************************************************/
55 int send_chat_printf(const char *format, ...)
57 struct packet_chat_msg_req packet;
58 va_list args;
60 va_start(args, format);
61 fc_utf8_vsnprintf_trunc(packet.message, sizeof(packet.message),
62 format, args);
63 va_end(args);
65 return send_packet_chat_msg_req(&client.conn, &packet);
68 /**************************************************************************
69 Allocate output window mutex
70 **************************************************************************/
71 void fc_allocate_ow_mutex(void)
73 fc_allocate_mutex(&ow_mutex);
76 /**************************************************************************
77 Release output window mutex
78 **************************************************************************/
79 void fc_release_ow_mutex(void)
81 fc_release_mutex(&ow_mutex);
84 /**************************************************************************
85 Initialize output window mutex
86 **************************************************************************/
87 void fc_init_ow_mutex(void)
89 fc_init_mutex(&ow_mutex);
92 /**************************************************************************
93 Destroy output window mutex
94 **************************************************************************/
95 void fc_destroy_ow_mutex(void)
97 fc_destroy_mutex(&ow_mutex);
100 /**************************************************************************
101 Add a line of text to the output ("chatline") window, like puts() would
102 do it in the console.
103 **************************************************************************/
104 void output_window_append(const struct ft_color color,
105 const char *featured_text)
107 char plain_text[MAX_LEN_MSG];
108 struct text_tag_list *tags;
110 /* Separate the text and the tags. */
111 featured_text_to_plain_text(featured_text, plain_text,
112 sizeof(plain_text), &tags, FALSE);
114 if (ft_color_requested(color)) {
115 /* A color is requested. */
116 struct text_tag *ptag = text_tag_new(TTT_COLOR, 0, FT_OFFSET_UNSET,
117 color);
119 if (ptag) {
120 /* Prepends to the list, to avoid to overwrite inside colors. */
121 text_tag_list_prepend(tags, ptag);
122 } else {
123 log_error("Failed to create a color text tag (fg = %s, bg = %s).",
124 (NULL != color.foreground ? color.foreground : "NULL"),
125 (NULL != color.background ? color.background : "NULL"));
129 fc_allocate_ow_mutex();
130 real_output_window_append(plain_text, tags, -1);
131 fc_release_ow_mutex();
132 text_tag_list_destroy(tags);
135 /**************************************************************************
136 Add a line of text to the output ("chatline") window. The text is
137 constructed in printf style.
138 **************************************************************************/
139 void output_window_vprintf(const struct ft_color color,
140 const char *format, va_list args)
142 char featured_text[MAX_LEN_MSG];
144 fc_vsnprintf(featured_text, sizeof(featured_text), format, args);
145 output_window_append(color, featured_text);
149 /**************************************************************************
150 Add a line of text to the output ("chatline") window. The text is
151 constructed in printf style.
152 **************************************************************************/
153 void output_window_printf(const struct ft_color color,
154 const char *format, ...)
156 va_list args;
158 va_start(args, format);
159 output_window_vprintf(color, format, args);
160 va_end(args);
163 /**************************************************************************
164 Add a line of text to the output ("chatline") window from server event.
165 **************************************************************************/
166 void output_window_event(const char *plain_text,
167 const struct text_tag_list *tags, int conn_id)
169 fc_allocate_ow_mutex();
170 real_output_window_append(plain_text, tags, conn_id);
171 fc_release_ow_mutex();
174 /****************************************************************************
175 Standard welcome message.
176 ****************************************************************************/
177 void chat_welcome_message(bool gui_has_copying_mitem)
179 output_window_append(ftc_any, _("Freeciv is free software and you are "
180 "welcome to distribute copies of it "
181 "under certain conditions;"));
182 if (gui_has_copying_mitem) {
183 output_window_append(ftc_any, _("See the \"Copying\" item on the "
184 "Help menu."));
185 } else {
186 output_window_append(ftc_any, _("See COPYING file distributed with "
187 "this program."));
189 output_window_append(ftc_any, _("Now ... Go give 'em hell!"));
192 /**************************************************************************
193 Writes the supplied string into the file defined by the variable
194 'default_chat_logfile'.
195 **************************************************************************/
196 void write_chatline_content(const char *txt)
198 FILE *fp = fc_fopen(gui_options.default_chat_logfile, "w");
199 char buf[512];
201 fc_snprintf(buf, sizeof(buf), _("Exporting output window to '%s' ..."),
202 gui_options.default_chat_logfile);
203 output_window_append(ftc_client, buf);
205 if (fp) {
206 fputs(txt, fp);
207 fclose(fp);
208 output_window_append(ftc_client, _("Export complete."));
209 } else {
210 output_window_append(ftc_client,
211 _("Export failed, couldn't write to file."));