Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/chatline_common.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
chatline_common.c
Go to the documentation of this file.
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.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdarg.h>
19#include <string.h>
20
21/* utility */
22#include "astring.h"
23#include "fcintl.h"
24#include "fcthread.h"
25#include "fc_utf8.h"
26#include "log.h"
27
28/* common */
29#include "featured_text.h"
30#include "packets.h"
31
32/* include */
33#include "chatline_g.h"
34
35/* client */
36#include "client_main.h"
37#include "options.h"
38
39#include "chatline_common.h"
40
41 static fc_mutex ow_mutex;
42
43/**********************************************************************/
46 int send_chat(const char *message)
47{
48 return dsend_packet_chat_msg_req(&client.conn, message);
49}
50
51/**********************************************************************/
55 int send_chat_printf(const char *format, ...)
56{
57 struct packet_chat_msg_req packet;
58 va_list args;
59
60 va_start(args, format);
61 fc_utf8_vsnprintf_trunc(packet.message, sizeof(packet.message),
62 format, args);
63 va_end(args);
64
65 return send_packet_chat_msg_req(&client.conn, &packet);
66}
67
68/**********************************************************************/
71 void fc_allocate_ow_mutex(void)
72{
73 fc_allocate_mutex(&ow_mutex);
74}
75
76/**********************************************************************/
79 void fc_release_ow_mutex(void)
80{
81 fc_release_mutex(&ow_mutex);
82}
83
84/**********************************************************************/
87 void fc_init_ow_mutex(void)
88{
89 fc_init_mutex(&ow_mutex);
90}
91
92/**********************************************************************/
95 void fc_destroy_ow_mutex(void)
96{
97 fc_destroy_mutex(&ow_mutex);
98}
99
100/**********************************************************************/
104 void output_window_append(const struct ft_color color,
105 const char *featured_text)
106{
107 char plain_text[MAX_LEN_MSG];
108 struct text_tag_list *tags;
109
110 /* Separate the text and the tags. */
111 featured_text_to_plain_text(featured_text, plain_text,
112 sizeof(plain_text), &tags, FALSE);
113
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);
118
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"));
126 }
127 }
128
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);
133}
134
135/**********************************************************************/
139 void output_window_vprintf(const struct ft_color color,
140 const char *format, va_list args)
141{
142 char featured_text[MAX_LEN_MSG];
143
144 fc_vsnprintf(featured_text, sizeof(featured_text), format, args);
145 output_window_append(color, featured_text);
146}
147
148
149/**********************************************************************/
153 void output_window_printf(const struct ft_color color,
154 const char *format, ...)
155{
156 va_list args;
157
158 va_start(args, format);
159 output_window_vprintf(color, format, args);
160 va_end(args);
161}
162
163/**********************************************************************/
166 void output_window_event(const char *plain_text,
167 const struct text_tag_list *tags, int conn_id)
168{
169 fc_allocate_ow_mutex();
170 real_output_window_append(plain_text, tags, conn_id);
171 fc_release_ow_mutex();
172}
173
174/**********************************************************************/
177 void chat_welcome_message(bool gui_has_copying_mitem)
178{
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."));
188 }
189 output_window_append(ftc_any, _("Now ... Go give 'em hell!"));
190}
191
192/**********************************************************************/
196 void write_chatline_content(const char *txt)
197{
198 FILE *fp = fc_fopen(gui_options.default_chat_logfile, "w");
199 char buf[512];
200
201 fc_snprintf(buf, sizeof(buf), _("Exporting output window to '%s' ..."),
202 gui_options.default_chat_logfile);
203 output_window_append(ftc_client, buf);
204
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."));
212 }
213}
astring.h
send_chat_printf
int send_chat_printf(const char *format,...)
Definition chatline_common.c:55
send_chat
int send_chat(const char *message)
Definition chatline_common.c:46
ow_mutex
static fc_mutex ow_mutex
Definition chatline_common.c:41
output_window_append
void output_window_append(const struct ft_color color, const char *featured_text)
Definition chatline_common.c:104
output_window_event
void output_window_event(const char *plain_text, const struct text_tag_list *tags, int conn_id)
Definition chatline_common.c:166
write_chatline_content
void write_chatline_content(const char *txt)
Definition chatline_common.c:196
fc_allocate_ow_mutex
void fc_allocate_ow_mutex(void)
Definition chatline_common.c:71
fc_release_ow_mutex
void fc_release_ow_mutex(void)
Definition chatline_common.c:79
output_window_vprintf
void output_window_vprintf(const struct ft_color color, const char *format, va_list args)
Definition chatline_common.c:139
output_window_printf
void output_window_printf(const struct ft_color color, const char *format,...)
Definition chatline_common.c:153
chat_welcome_message
void chat_welcome_message(bool gui_has_copying_mitem)
Definition chatline_common.c:177
fc_destroy_ow_mutex
void fc_destroy_ow_mutex(void)
Definition chatline_common.c:95
fc_init_ow_mutex
void fc_init_ow_mutex(void)
Definition chatline_common.c:87
chatline_common.h
chatline_g.h
client
struct civclient client
Definition client_main.c:149
client_main.h
fc_utf8_vsnprintf_trunc
int fc_utf8_vsnprintf_trunc(char *str, size_t n, const char *format, va_list args)
Definition fc_utf8.c:552
fc_utf8.h
fcintl.h
_
#define _(String)
Definition fcintl.h:67
fcthread.h
fc_allocate_mutex
void fc_allocate_mutex(fc_mutex *mutex)
fc_release_mutex
void fc_release_mutex(fc_mutex *mutex)
fc_destroy_mutex
void fc_destroy_mutex(fc_mutex *mutex)
fc_init_mutex
void fc_init_mutex(fc_mutex *mutex)
featured_text_to_plain_text
size_t featured_text_to_plain_text(const char *featured_text, char *plain_text, size_t plain_text_len, struct text_tag_list **tags, bool replace_link_text)
Definition featured_text.c:864
ftc_client
const struct ft_color ftc_client
Definition featured_text.c:78
ftc_any
const struct ft_color ftc_any
Definition featured_text.c:73
text_tag_new
struct text_tag * text_tag_new(enum text_tag_type tag_type, ft_offset_t start_offset, ft_offset_t stop_offset,...)
Definition featured_text.c:638
featured_text.h
FT_OFFSET_UNSET
#define FT_OFFSET_UNSET
Definition featured_text.h:104
ft_color_requested
static bool ft_color_requested(const struct ft_color color)
Definition featured_text.h:160
TTT_COLOR
@ TTT_COLOR
Definition featured_text.h:125
real_output_window_append
void real_output_window_append(const char *astring, const struct text_tag_list *tags, int conn_id)
Definition chatline.c:874
log.h
log_error
#define log_error(message,...)
Definition log.h:103
gui_options
struct client_options gui_options
Definition options.c:71
options.h
packets.h
MAX_LEN_MSG
#define MAX_LEN_MSG
Definition packets.h:43
dsend_packet_chat_msg_req
int dsend_packet_chat_msg_req(struct connection *pc, const char *message)
Definition packets_gen.c:11274
send_packet_chat_msg_req
int send_packet_chat_msg_req(struct connection *pc, const struct packet_chat_msg_req *packet)
Definition packets_gen.c:11262
civclient::conn
struct connection conn
Definition client_main.h:96
client_options::default_chat_logfile
char default_chat_logfile[512]
Definition options.h:119
color
Definition colors.h:20
ft_color
Definition featured_text.h:141
message
Definition messagewin_common.h:29
packet_chat_msg_req
Definition packets_gen.h:305
packet_chat_msg_req::message
char message[MAX_LEN_MSG]
Definition packets_gen.h:306
text_tag
Definition featured_text.c:49
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:896
fc_fopen
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:506
FALSE
#define FALSE
Definition support.h:47