1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
22 #include <cppconsui/CoreManager.h>
23 #include <cppconsui/CppConsUI.h>
24 #include <libpurple/purple.h>
27 #define CONF_PREFIX "/centerim5"
28 #define CONF_PLUGINS_PREF CONF_PREFIX "/plugins"
29 #define CONF_PLUGINS_SAVE_PREF CONF_PLUGINS_PREF "/loaded"
31 #define CENTERIM (CenterIM::instance())
33 class CenterIM
: public CppConsUI::InputProcessor
{
45 // Color schemes. Keep synchronized with scheme_names.
48 SCHEME_ACCOUNTSTATUSMENU
= SCHEME_BEGIN
,
50 SCHEME_BUDDYLISTBUDDY
,
51 SCHEME_BUDDYLISTBUDDY_AWAY
,
52 SCHEME_BUDDYLISTBUDDY_NA
,
53 SCHEME_BUDDYLISTBUDDY_OFFLINE
,
54 SCHEME_BUDDYLISTBUDDY_ONLINE
,
56 SCHEME_BUDDYLISTCONTACT
,
57 SCHEME_BUDDYLISTCONTACT_AWAY
,
58 SCHEME_BUDDYLISTCONTACT_NA
,
59 SCHEME_BUDDYLISTCONTACT_OFFLINE
,
60 SCHEME_BUDDYLISTCONTACT_ONLINE
,
61 SCHEME_BUDDYLISTGROUP
,
63 SCHEME_CONVERSATION_ACTIVE
,
64 SCHEME_CONVERSATION_NEW
,
69 SCHEME_HEADER_REQUEST
,
75 static CenterIM
*instance();
78 virtual bool processInput(const TermKeyKey
&key
) override
;
82 // Returns a position and size of a selected area.
83 CppConsUI::Rect
getScreenArea(ScreenArea area
);
84 CppConsUI::Rect
getScreenAreaCentered(ScreenArea area
);
86 static const char *const version_
;
88 bool loadColorSchemeConfig();
91 bool isEnabledExpandedConversationMode() const { return convs_expanded_
; }
93 sigc::connection
timeoutConnect(const sigc::slot
<bool> &slot
,
94 unsigned interval
, int priority
= G_PRIORITY_DEFAULT
);
95 sigc::connection
timeoutOnceConnect(const sigc::slot
<void> &slot
,
96 unsigned interval
, int priority
= G_PRIORITY_DEFAULT
);
99 struct IOClosurePurple
{
100 PurpleInputFunction function
;
104 IOClosurePurple() : function(nullptr), result(0), data(nullptr) {}
107 GMainLoop
*mainloop_
;
108 bool mainloop_error_exit_
;
109 CppConsUI::CoreManager
*mngr_
;
110 // Flag indicating if the conversation full-screen mode is activated.
111 bool convs_expanded_
;
112 // Flag indicating if idle reporting is based on keyboard presses.
113 bool idle_reporting_on_keyboard_
;
115 guint stdin_timeout_id_
;
117 volatile bool resize_pending_
;
118 const char *sigwinch_write_error_
;
119 std::size_t sigwinch_write_error_size_
;
121 PurpleCoreUiOps centerim_core_ui_ops_
;
122 PurpleDebugUiOps logbuf_debug_ui_ops_
;
123 PurpleEventLoopUiOps centerim_glib_eventloops_
;
125 CppConsUI::Rect areas_
[AREAS_NUM
];
127 static const char *color_names_
[];
128 static const char *scheme_names_
[];
130 static CenterIM
*my_instance_
;
133 virtual ~CenterIM() {}
134 CONSUI_DISABLE_COPY(CenterIM
);
136 static int run(int argc
, char *argv
[]);
137 friend int main(int argc
, char *argv
[]);
139 int runAll(int argc
, char *argv
[]);
140 void printUsage(FILE *out
, const char *prg_name
);
141 void printVersion(FILE *out
);
142 int initializePurple(const char *config_path
);
143 void finalizePurple();
144 void initializePreferences();
146 int initializeScreenResizing();
147 void finalizeScreenResizing();
149 // Recalculates area sizes to fit into current screen size.
150 void onScreenResized();
152 void onTopWindowChanged();
154 // PurpleEventLoopUiOps callbacks.
155 // Adds IO watch to glib main loop context.
156 static guint
input_add_purple(int fd
, PurpleInputCondition condition
,
157 PurpleInputFunction function
, gpointer data
);
159 // Helper functions for input_add_purple().
160 // Processes IO input to purple callback.
161 static gboolean
io_input_purple(
162 GIOChannel
*source
, GIOCondition condition
, gpointer data
);
163 // Destroyes libpurple IO input callback internal data.
164 static void io_destroy_purple(gpointer data
);
166 static gboolean
stdin_bytes_available_(
167 GIOChannel
* /*source*/, GIOCondition
/*condition*/, gpointer data
)
169 return reinterpret_cast<CenterIM
*>(data
)->stdin_bytes_available();
171 gboolean
stdin_bytes_available();
172 static gboolean
stdin_timeout_(gpointer data
)
174 return reinterpret_cast<CenterIM
*>(data
)->stdin_timeout();
176 gboolean
stdin_timeout();
178 static gboolean
resize_bytes_available_(
179 GIOChannel
* /*source*/, GIOCondition
/*condition*/, gpointer data
)
181 return reinterpret_cast<CenterIM
*>(data
)->resize_bytes_available();
183 gboolean
resize_bytes_available();
186 static gboolean
draw_(gpointer data
)
188 return reinterpret_cast<CenterIM
*>(data
)->draw();
192 static void sigwinch_handler_(int signum
)
194 CENTERIM
->sigwinch_handler(signum
);
196 void sigwinch_handler(int signum
);
198 // CppConsUI callbacks.
199 // Registers a redraw request.
200 void redraw_cppconsui();
201 // Logs a debug message produced by CppConsUI.
202 void log_debug_cppconsui(const char *message
);
204 // PurpleCoreUiOps callbacks.
205 // Returns information about CenterIM such as name, website etc.
206 static GHashTable
*get_ui_info();
208 // PurpleDebugUiOps callbacks.
209 static void purple_print(
210 PurpleDebugLevel level
, const char *category
, const char *arg_s
);
211 static gboolean
purple_is_enabled(
212 PurpleDebugLevel level
, const char *category
);
214 // Called when the CONF_PREFIX/dimensions preferences change their values.
215 static void dimensions_change_(
216 const char *name
, PurplePrefType type
, gconstpointer val
, gpointer data
)
218 reinterpret_cast<CenterIM
*>(data
)->dimensions_change(name
, type
, val
);
220 void dimensions_change(
221 const char *name
, PurplePrefType type
, gconstpointer val
);
223 // Called when the /libpurple/away/idle_reporting preference changes its
225 static void idle_reporting_change_(
226 const char *name
, PurplePrefType type
, gconstpointer val
, gpointer data
)
228 reinterpret_cast<CenterIM
*>(data
)->idle_reporting_change(name
, type
, val
);
230 void idle_reporting_change(
231 const char *name
, PurplePrefType type
, gconstpointer val
);
234 void loadDefaultColorSchemeConfig();
235 bool saveColorSchemeConfig();
236 const char *schemeToString(int scheme
);
237 int stringToScheme(const char *str
);
238 char *colorToString(int color
);
239 bool stringToColor(const char *str
, int *color
);
240 char *colorAttributesToString(int attrs
);
241 bool stringToColorAttributes(const char *str
, int *attrs
);
242 void loadDefaultKeyConfig();
243 bool saveKeyConfig();
245 void actionFocusBuddyList();
246 void actionFocusActiveConversation();
247 void actionOpenAccountStatusMenu();
248 void actionOpenGeneralMenu();
249 void actionBuddyListToggleOffline();
250 void actionFocusPrevConversation();
251 void actionFocusNextConversation();
252 void actionFocusConversation(int i
);
253 void actionExpandConversation();
255 void declareBindables();
260 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: