From 5ad00f7fd8b8d7ed9add2823f5c633fbeb323838 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Sun, 4 Feb 2024 00:37:50 -0500 Subject: [PATCH] refactor: x11 focus object is no longer global --- src/chat.c | 58 +++++++++++++++++++++----------------------- src/conference.c | 14 +++++------ src/file_transfers.c | 8 +++--- src/friendlist.c | 14 +++++------ src/game_base.c | 7 +++--- src/groupchats.c | 32 ++++++++++++------------ src/input.c | 46 ++++++++++++++++++----------------- src/input.h | 4 +-- src/notify.c | 45 +++++++++++++++++++--------------- src/notify.h | 12 ++++----- src/prompt.c | 18 +++++++------- src/toxic.c | 4 +-- src/toxic.h | 9 +++++++ src/toxic_strings.c | 15 +++--------- src/x11focus.c | 43 +++++++++++++++----------------- src/x11focus.h | 19 +++++++++++---- 16 files changed, 179 insertions(+), 169 deletions(-) diff --git a/src/chat.c b/src/chat.c index 28e64ad08..0d3fb7727 100644 --- a/src/chat.c +++ b/src/chat.c @@ -176,36 +176,37 @@ void kill_chat_window(ToxWindow *self, Toxic *toxic) del_window(self, toxic->c_config); } -static void recv_message_helper(ToxWindow *self, const Client_Config *c_config, const char *msg, +static void recv_message_helper(ToxWindow *self, const Toxic *toxic, const char *msg, const char *nick) { + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; line_info_add(self, c_config, true, nick, NULL, IN_MSG, 0, 0, "%s", msg); write_to_log(ctx->log, c_config, msg, nick, false); if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, + box_notify2(self, toxic, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "%s", msg); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, + box_notify(self, toxic, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, nick, "%s", msg); } } -static void recv_action_helper(ToxWindow *self, const Client_Config *c_config, const char *action, - const char *nick) +static void recv_action_helper(ToxWindow *self, const Toxic *toxic, const char *action, const char *nick) { + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; line_info_add(self, c_config, true, nick, NULL, IN_ACTION, 0, 0, "%s", action); write_to_log(ctx->log, c_config, action, nick, true); if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, + box_notify2(self, toxic, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "* %s %s", nick, action); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, + box_notify(self, toxic, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "* %s %s", nick, action); } } @@ -219,8 +220,6 @@ static void chat_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Mess return; } - const Client_Config *c_config = toxic->c_config; - if (self->num != num) { return; } @@ -229,12 +228,12 @@ static void chat_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Mess get_nick_truncate(toxic->tox, nick, num); if (type == TOX_MESSAGE_TYPE_NORMAL) { - recv_message_helper(self, c_config, msg, nick); + recv_message_helper(self, toxic, msg, nick); return; } if (type == TOX_MESSAGE_TYPE_ACTION) { - recv_action_helper(self, c_config, msg, nick); + recv_action_helper(self, toxic, msg, nick); return; } } @@ -572,8 +571,7 @@ static void chat_onFileControl(ToxWindow *self, Toxic *toxic, uint32_t friendnum ft->state = FILE_TRANSFER_STARTED; line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%zu] for '%s' accepted.", ft->index, ft->file_name); - sound_notify(self, c_config, silent, - NT_NOFOCUS | c_config->bell_on_filetrans_accept | NT_WNDALERT_2, NULL); + sound_notify(self, toxic, silent, NT_NOFOCUS | c_config->bell_on_filetrans_accept | NT_WNDALERT_2, NULL); } else if (ft->state == FILE_TRANSFER_PAUSED) { /* transfer is resumed */ ft->state = FILE_TRANSFER_STARTED; } @@ -785,10 +783,10 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u free(file_path); if (self->active_box != -1) { - box_notify2(self, c_config, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_filetrans, + box_notify2(self, toxic, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_filetrans, self->active_box, "Incoming file: %s", filename); } else { - box_notify(self, c_config, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_filetrans, + box_notify(self, toxic, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_filetrans, &self->active_box, self->name, "Incoming file: %s", filename); } @@ -843,11 +841,10 @@ static void chat_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t frien const char *description = type == TOX_CONFERENCE_TYPE_AV ? "an audio conference" : "a conference"; if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, self->active_box, + box_notify2(self, toxic, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, self->active_box, "invites you to join %s", description); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, &self->active_box, - name, + box_notify(self, toxic, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, &self->active_box, name, "invites you to join %s", description); } @@ -884,16 +881,16 @@ static void chat_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t friendnum memcpy(Friends.list[friendnumber].group_invite.data, invite_data, length); Friends.list[friendnumber].group_invite.length = length; - sound_notify(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, NULL); + sound_notify(self, toxic, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, NULL); char name[TOX_MAX_NAME_LENGTH]; get_nick_truncate(toxic->tox, name, friendnumber); if (self->active_box != -1) { - box_silent_notify2(self, c_config, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box, + box_silent_notify2(self, toxic, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box, "invites you to join group chat"); } else { - box_silent_notify(self, c_config, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, name, + box_silent_notify(self, toxic, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, name, "invites you to join group chat"); } @@ -967,10 +964,10 @@ static void chat_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_num get_nick_truncate(toxic->tox, name, friend_number); if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, self->active_box, + box_notify2(self, toxic, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, self->active_box, "invites you to play %s", game_string); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, &self->active_box, + box_notify(self, toxic, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, &self->active_box, name, "invites you to play %s", game_string); } @@ -1007,14 +1004,13 @@ static void chat_onInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, "Incoming audio call! Type: \"/answer\" or \"/reject\""); if (self->ringing_sound == -1) { - sound_notify(self, c_config, call_incoming, NT_LOOP | c_config->bell_on_invite, &self->ringing_sound); + sound_notify(self, toxic, call_incoming, NT_LOOP | c_config->bell_on_invite, &self->ringing_sound); } if (self->active_box != -1) { - box_silent_notify2(self, c_config, NT_NOFOCUS | NT_WNDALERT_0, self->active_box, "Incoming audio call!"); + box_silent_notify2(self, toxic, NT_NOFOCUS | NT_WNDALERT_0, self->active_box, "Incoming audio call!"); } else { - box_silent_notify(self, c_config, NT_NOFOCUS | NT_WNDALERT_0, &self->active_box, self->name, - "Incoming audio call!"); + box_silent_notify(self, toxic, NT_NOFOCUS | NT_WNDALERT_0, &self->active_box, self->name, "Incoming audio call!"); } } @@ -1038,7 +1034,7 @@ static void chat_onRinging(ToxWindow *self, Toxic *toxic, uint32_t friend_number #ifdef SOUND_NOTIFY if (self->ringing_sound == -1) { - sound_notify(self, c_config, call_outgoing, NT_LOOP, &self->ringing_sound); + sound_notify(self, toxic, call_outgoing, NT_LOOP, &self->ringing_sound); } #endif /* SOUND_NOTIFY */ @@ -1326,7 +1322,7 @@ static bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ltr || key == L'\n') { /* char is printable */ - input_new_char(self, c_config, key, x, x2); + input_new_char(self, toxic, key, x, x2); if (ctx->line[0] != '/' && !ctx->self_is_typing && statusbar->connection != TOX_CONNECTION_NONE) { set_self_typingstatus(self, toxic, true); @@ -1339,7 +1335,7 @@ static bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) return true; } - bool input_ret = input_handle(self, c_config, key, x, x2); + bool input_ret = input_handle(self, toxic, key, x, x2); if (key == L'\t' && ctx->len > 1 && ctx->line[0] == '/') { /* TAB key: auto-complete */ input_ret = true; @@ -1369,7 +1365,7 @@ static bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else if (key == L'\r') { diff --git a/src/conference.c b/src/conference.c index 8c36026b9..e0b93a3e4 100644 --- a/src/conference.c +++ b/src/conference.c @@ -401,16 +401,16 @@ static void conference_onConferenceMessage(ToxWindow *self, Toxic *toxic, uint32 /* Only play sound if mentioned by someone else */ if (strcasestr(msg, selfnick) && strcmp(selfnick, nick)) { if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, + box_notify2(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "%s %s", nick, msg); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, + box_notify(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "%s %s", nick, msg); } nick_clr = RED; } else { - sound_notify(self, c_config, silent, NT_WNDALERT_1, NULL); + sound_notify(self, toxic, silent, NT_WNDALERT_1, NULL); } line_info_add(self, c_config, true, nick, NULL, type == TOX_MESSAGE_TYPE_NORMAL ? IN_MSG : IN_ACTION, 0, @@ -915,7 +915,7 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr } if (ltr || key == L'\n') { /* char is printable */ - input_new_char(self, c_config, key, x, x2); + input_new_char(self, toxic, key, x, x2); return true; } @@ -923,7 +923,7 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr return true; } - if (input_handle(self, c_config, key, x, x2)) { + if (input_handle(self, toxic, key, x, x2)) { return true; } @@ -998,10 +998,10 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else if (key == T_KEY_C_DOWN) { /* Scroll peerlist up and down one position */ input_ret = true; diff --git a/src/file_transfers.c b/src/file_transfers.c index e887c3f4b..d1a3b49d6 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -335,8 +335,6 @@ int file_send_queue_remove(uint32_t friendnumber, size_t index) void close_file_transfer(ToxWindow *self, const Toxic *toxic, FileTransfer *ft, int CTRL, const char *message, Notification sound_type) { - const Client_Config *c_config = toxic->c_config; - if (ft == NULL) { return; } @@ -359,12 +357,12 @@ void close_file_transfer(ToxWindow *self, const Toxic *toxic, FileTransfer *ft, if (message && self) { if (self->active_box != -1 && sound_type != silent) { - box_notify2(self, c_config, sound_type, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", message); + box_notify2(self, toxic, sound_type, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", message); } else { - box_notify(self, c_config, sound_type, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", message); + box_notify(self, toxic, sound_type, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", message); } - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", message); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", message); } clear_file_transfer(ft); diff --git a/src/friendlist.c b/src/friendlist.c index 0c08da40b..f1ba13cf2 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -432,7 +432,7 @@ static void friendlist_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, To line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Warning: Too many windows are open."); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } static void friendlist_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Connection connection_status) @@ -661,7 +661,7 @@ static void friendlist_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t frie line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Game invite from %s failed: Too many windows are open.", nick); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } #endif // GAMES @@ -699,7 +699,7 @@ static void friendlist_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t num, u line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* File transfer from %s failed: too many windows are open.", nick); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } static void friendlist_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t num, uint8_t type, @@ -738,7 +738,7 @@ static void friendlist_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Conference chat invite from %s failed: too many windows are open.", nick); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } static void friendlist_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t num, const char *data, size_t length, @@ -775,7 +775,7 @@ static void friendlist_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t num line_info_add(home_window, c_config, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Group chat invite from %s failed: too many windows are open.", nick); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } /* move friendlist/blocklist cursor up and down */ @@ -1069,7 +1069,7 @@ static bool friendlist_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr } else { const char *msg = "* Warning: Too many windows are open."; line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, msg); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } break; @@ -1468,7 +1468,7 @@ static void friendlist_onAV(ToxWindow *self, Toxic *toxic, uint32_t friend_numbe const char *errmsg = "* Warning: Too many windows are open."; line_info_add(home_window, c_config, false, NULL, NULL, SYS_MSG, 0, RED, errmsg); - sound_notify(home_window, c_config, notif_error, NT_WNDALERT_1, NULL); + sound_notify(home_window, toxic, notif_error, NT_WNDALERT_1, NULL); } } } diff --git a/src/game_base.c b/src/game_base.c index 5a06bb934..96a99c824 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -154,15 +154,16 @@ void game_window_notify(const GameData *game, const char *message) return; } - const Client_Config *c_config = game->toxic->c_config; + const Toxic *toxic = game->toxic; + const Client_Config *c_config = toxic->c_config; const int bell_on_message = c_config->bell_on_message; if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | bell_on_message, + box_notify2(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | bell_on_message, self->active_box, "%s", message); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | bell_on_message, + box_notify(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | bell_on_message, &self->active_box, self->name, "%s", message); } } diff --git a/src/groupchats.c b/src/groupchats.c index 438e0055e..5ba3eb8d4 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -938,14 +938,14 @@ static void group_onAction(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, if (strcasestr(action, self_nick)) { if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | + box_notify2(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "* %s %s", nick, action); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | + box_notify(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "* %s %s", nick, action); } } else { - sound_notify(self, c_config, silent, NT_WNDALERT_1, NULL); + sound_notify(self, toxic, silent, NT_WNDALERT_1, NULL); } line_info_add(self, c_config, true, nick, NULL, IN_ACTION, 0, 0, "%s", action); @@ -985,17 +985,17 @@ static void groupchat_onGroupMessage(ToxWindow *self, Toxic *toxic, uint32_t gro /* Only play sound if mentioned by someone else */ if (strcasestr(msg, self_nick) && strcmp(self_nick, nick)) { - sound_notify(self, c_config, generic_message, NT_WNDALERT_0 | c_config->bell_on_message, NULL); + sound_notify(self, toxic, generic_message, NT_WNDALERT_0 | c_config->bell_on_message, NULL); if (self->active_box != -1) { - box_silent_notify2(self, c_config, NT_NOFOCUS, self->active_box, "%s %s", nick, msg); + box_silent_notify2(self, toxic, NT_NOFOCUS, self->active_box, "%s %s", nick, msg); } else { - box_silent_notify(self, c_config, NT_NOFOCUS, &self->active_box, self->name, "%s %s", nick, msg); + box_silent_notify(self, toxic, NT_NOFOCUS, &self->active_box, self->name, "%s %s", nick, msg); } nick_clr = RED; } else { - sound_notify(self, c_config, silent, NT_WNDALERT_1, NULL); + sound_notify(self, toxic, silent, NT_WNDALERT_1, NULL); } line_info_add(self, c_config, true, nick, NULL, IN_MSG, 0, nick_clr, "%s", msg); @@ -1027,10 +1027,10 @@ static void groupchat_onGroupPrivateMessage(ToxWindow *self, Toxic *toxic, uint3 write_to_log(ctx->log, c_config, msg, nick, false); if (self->active_box != -1) { - box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, + box_notify2(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "%s %s", nick, msg); } else { - box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, + box_notify(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "%s %s", nick, msg); } } @@ -1295,7 +1295,7 @@ static void groupchat_onGroupPeerJoin(ToxWindow *self, Toxic *toxic, uint32_t gr line_info_add(self, c_config, true, peer->name, NULL, CONNECTION, 0, GREEN, "has joined the room"); write_to_log(ctx->log, c_config, "has joined the room", peer->name, true); - sound_notify(self, c_config, silent, NT_WNDALERT_2, NULL); + sound_notify(self, toxic, silent, NT_WNDALERT_2, NULL); } group_update_name_list(groupnumber); @@ -1336,13 +1336,13 @@ void groupchat_onGroupPeerExit(ToxWindow *self, Toxic *toxic, uint32_t groupnumb line_info_add(self, c_config, true, name, NULL, DISCONNECTION, 0, RED, "[Quit]: %s", part_message); snprintf(log_str, sizeof(log_str), "has left the room (%s)", part_message); write_to_log(ctx->log, c_config, log_str, name, true); - sound_notify(self, c_config, silent, NT_WNDALERT_2, NULL); + sound_notify(self, toxic, silent, NT_WNDALERT_2, NULL); } else { const char *exit_string = get_group_exit_string(exit_type); line_info_add(self, c_config, true, name, NULL, DISCONNECTION, 0, RED, "[%s]", exit_string); snprintf(log_str, sizeof(log_str), "[%s]", exit_string); write_to_log(ctx->log, c_config, log_str, name, true); - sound_notify(self, c_config, silent, NT_WNDALERT_2, NULL); + sound_notify(self, toxic, silent, NT_WNDALERT_2, NULL); } } @@ -1891,7 +1891,7 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ltr || key == L'\n') { /* char is printable */ - input_new_char(self, c_config, key, x, x2); + input_new_char(self, toxic, key, x, x2); return true; } @@ -1899,7 +1899,7 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) return true; } - if (input_handle(self, c_config, key, x, x2)) { + if (input_handle(self, toxic, key, x, x2)) { return true; } @@ -1926,10 +1926,10 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else if (key == T_KEY_C_DOWN) { /* Scroll peerlist up and down one position */ input_ret = true; diff --git a/src/input.c b/src/input.c index ba94745c4..abe1a7dbf 100644 --- a/src/input.c +++ b/src/input.c @@ -39,7 +39,7 @@ #include "windows.h" /* add a char to input field and buffer */ -void input_new_char(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x) +void input_new_char(ToxWindow *self, const Toxic *toxic, wint_t key, int x, int mx_x) { ChatContext *ctx = self->chatwin; @@ -51,12 +51,12 @@ void input_new_char(ToxWindow *self, const Client_Config *c_config, wint_t key, int cur_len = wcwidth(key); if (cur_len == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); return; } if (add_char_to_buf(ctx, key) == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); return; } @@ -67,12 +67,12 @@ void input_new_char(ToxWindow *self, const Client_Config *c_config, wint_t key, } /* delete a char via backspace key from input field and buffer */ -static void input_backspace(ToxWindow *self, const Client_Config *c_config, int x, int mx_x) +static void input_backspace(ToxWindow *self, const Toxic *toxic, int x, int mx_x) { ChatContext *ctx = self->chatwin; if (del_char_buf_bck(ctx) == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); return; } @@ -87,45 +87,45 @@ static void input_backspace(ToxWindow *self, const Client_Config *c_config, int } /* delete a char via delete key from input field and buffer */ -static void input_delete(ToxWindow *self, const Client_Config *c_config) +static void input_delete(ToxWindow *self, const Toxic *toxic) { if (del_char_buf_frnt(self->chatwin) == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } /* delete last typed word */ -static void input_del_word(ToxWindow *self, const Client_Config *c_config) +static void input_del_word(ToxWindow *self, const Toxic *toxic) { ChatContext *ctx = self->chatwin; if (del_word_buf(ctx) == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } /* deletes entire line before cursor from input field and buffer */ -static void input_discard(ToxWindow *self, const Client_Config *c_config) +static void input_discard(ToxWindow *self, const Toxic *toxic) { if (discard_buf(self->chatwin) == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } /* deletes entire line after cursor from input field and buffer */ -static void input_kill(ChatContext *ctx, const Client_Config *c_config) +static void input_kill(ChatContext *ctx, const Toxic *toxic) { if (kill_buf(ctx) == -1) { - sound_notify(NULL, c_config, notif_error, NT_ALWAYS, NULL); + sound_notify(NULL, toxic, notif_error, NT_ALWAYS, NULL); } } -static void input_yank(ToxWindow *self, const Client_Config *c_config, int x, int mx_x) +static void input_yank(ToxWindow *self, const Toxic *toxic, int x, int mx_x) { ChatContext *ctx = self->chatwin; if (yank_buf(ctx) == -1) { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); return; } @@ -261,34 +261,36 @@ static void input_history(ToxWindow *self, const Client_Config *c_config, wint_t /* Handles non-printable input keys that behave the same for all types of chat windows. return true if key matches a function, false otherwise */ -bool input_handle(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x) +bool input_handle(ToxWindow *self, const Toxic *toxic, wint_t key, int x, int mx_x) { + const Client_Config *c_config = toxic->c_config; + bool match = true; switch (key) { case 0x7f: case KEY_BACKSPACE: - input_backspace(self, c_config, x, mx_x); + input_backspace(self, toxic, x, mx_x); break; case KEY_DC: - input_delete(self, c_config); + input_delete(self, toxic); break; case T_KEY_DISCARD: - input_discard(self, c_config); + input_discard(self, toxic); break; case T_KEY_KILL: - input_kill(self->chatwin, c_config); + input_kill(self->chatwin, toxic); break; case T_KEY_C_Y: - input_yank(self, c_config, x, mx_x); + input_yank(self, toxic, x, mx_x); break; case T_KEY_C_W: - input_del_word(self, c_config); + input_del_word(self, toxic); break; case KEY_HOME: diff --git a/src/input.h b/src/input.h index 2e2974ab0..998793421 100644 --- a/src/input.h +++ b/src/input.h @@ -27,10 +27,10 @@ #include "windows.h" /* add a char to input field and buffer for given chatcontext */ -void input_new_char(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x); +void input_new_char(ToxWindow *self, const Toxic *toxic, wint_t key, int x, int mx_x); /* Handles non-printable input keys that behave the same for all types of chat windows. return true if key matches a function, false otherwise */ -bool input_handle(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x); +bool input_handle(ToxWindow *self, const Toxic *toxic, wint_t key, int x, int mx_x); #endif /* INPUT_H */ diff --git a/src/notify.c b/src/notify.c index 6ef918413..ffb18d4f3 100644 --- a/src/notify.c +++ b/src/notify.c @@ -34,7 +34,10 @@ #include "misc_tools.h" #include "notify.h" #include "settings.h" + +#ifdef X11 #include "x11focus.h" +#endif /* X11 */ #if defined(AUDIO) || defined(SOUND_NOTIFY) #ifdef __APPLE__ @@ -127,15 +130,17 @@ static void tab_notify(ToxWindow *self, uint64_t flags) ++self->pending_messages; } -static bool notifications_are_disabled(const Client_Config *c_config, uint64_t flags) +static bool notifications_are_disabled(const Toxic *toxic, uint64_t flags) { + const Client_Config *c_config = toxic->c_config; + if (c_config->alerts != ALERTS_ENABLED) { return true; } bool res = (flags & NT_RESTOL) && (Control.cooldown > get_unix_time()); #ifdef X11 - return res || ((flags & NT_NOFOCUS) && is_focused()); + return res || ((flags & NT_NOFOCUS) && is_focused(&toxic->x11_focus)); #else return res; #endif @@ -598,12 +603,14 @@ static int m_play_sound(const Client_Config *c_config, Notification notif, uint6 #endif /* SOUND_NOTIFY */ } -int sound_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, +int sound_notify(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int *id_indicator) { + const Client_Config *c_config = toxic->c_config; + tab_notify(self, flags); - if (notifications_are_disabled(c_config, flags)) { + if (notifications_are_disabled(toxic, flags)) { return -1; } @@ -639,11 +646,11 @@ int sound_notify(ToxWindow *self, const Client_Config *c_config, Notification no return id; } -int sound_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, int id) +int sound_notify2(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int id) { tab_notify(self, flags); - if (notifications_are_disabled(c_config, flags)) { + if (notifications_are_disabled(toxic, flags)) { return -1; } @@ -659,7 +666,7 @@ int sound_notify2(ToxWindow *self, const Client_Config *c_config, Notification n return -1; } - m_open_device(c_config); + m_open_device(toxic->c_config); alSourceStop(actives[id].source); alDeleteSources(1, &actives[id].source); @@ -686,17 +693,17 @@ int sound_notify2(ToxWindow *self, const Client_Config *c_config, Notification n #endif /* SOUND_NOTIFY */ } -int box_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, +int box_notify(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int *id_indicator, const char *title, const char *format, ...) { - if (notifications_are_disabled(c_config, flags)) { + if (notifications_are_disabled(toxic, flags)) { tab_notify(self, flags); return -1; } #ifdef BOX_NOTIFY - int id = sound_notify(self, c_config, notif, flags, id_indicator); + int id = sound_notify(self, toxic, notif, flags, id_indicator); control_lock(); @@ -755,21 +762,21 @@ int box_notify(ToxWindow *self, const Client_Config *c_config, Notification noti control_unlock(); return id; #else - return sound_notify(self, c_config, notif, flags, id_indicator); + return sound_notify(self, toxic, notif, flags, id_indicator); #endif /* BOX_NOTIFY */ } -int box_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, +int box_notify2(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int id, const char *format, ...) { - if (notifications_are_disabled(c_config, flags)) { + if (notifications_are_disabled(toxic, flags)) { tab_notify(self, flags); return -1; } #ifdef BOX_NOTIFY - if (sound_notify2(self, c_config, notif, flags, id) == -1) { + if (sound_notify2(self, toxic, notif, flags, id) == -1) { return -1; } @@ -808,16 +815,16 @@ int box_notify2(ToxWindow *self, const Client_Config *c_config, Notification not return id; #else - return sound_notify2(self, c_config, notif, flags, id); + return sound_notify2(self, toxic, notif, flags, id); #endif /* BOX_NOTIFY */ } -int box_silent_notify(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int *id_indicator, +int box_silent_notify(ToxWindow *self, const Toxic *toxic, uint64_t flags, int *id_indicator, const char *title, const char *format, ...) { tab_notify(self, flags); - if (notifications_are_disabled(c_config, flags)) { + if (notifications_are_disabled(toxic, flags)) { return -1; } @@ -871,12 +878,12 @@ int box_silent_notify(ToxWindow *self, const Client_Config *c_config, uint64_t f #endif /* BOX_NOTIFY */ } -int box_silent_notify2(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int id, +int box_silent_notify2(ToxWindow *self, const Toxic *toxic, uint64_t flags, int id, const char *format, ...) { tab_notify(self, flags); - if (notifications_are_disabled(c_config, flags)) { + if (notifications_are_disabled(toxic, flags)) { return -1; } diff --git a/src/notify.h b/src/notify.h index 27a2a41bf..65b7291cb 100644 --- a/src/notify.h +++ b/src/notify.h @@ -65,20 +65,20 @@ void terminate_notify(void); /* Kills all notifications for `id`. This must be called before freeing a ToxWindow. */ void kill_notifs(int id); -int sound_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, +int sound_notify(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int *id_indicator); -int sound_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, int id); +int sound_notify2(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int id); void stop_sound(int id); -int box_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, +int box_notify(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int *id_indicator, const char *title, const char *format, ...); -int box_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, int id, +int box_notify2(ToxWindow *self, const Toxic *toxic, Notification notif, uint64_t flags, int id, const char *format, ...); -int box_silent_notify(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int *id_indicator, +int box_silent_notify(ToxWindow *self, const Toxic *toxic, uint64_t flags, int *id_indicator, const char *title, const char *format, ...); -int box_silent_notify2(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int id, +int box_silent_notify2(ToxWindow *self, const Toxic *toxic, uint64_t flags, int id, const char *format, ...); #ifdef SOUND_NOTIFY diff --git a/src/prompt.c b/src/prompt.c index bb0efa066..ca63a917b 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -239,7 +239,7 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ltr || key == '\n') { /* char is printable */ - input_new_char(self, c_config, key, x, x2); + input_new_char(self, toxic, key, x, x2); return true; } @@ -247,7 +247,7 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) return true; } - if (input_handle(self, c_config, key, x, x2)) { + if (input_handle(self, toxic, key, x, x2)) { return true; } @@ -280,10 +280,10 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else { - sound_notify(self, c_config, notif_error, 0, NULL); + sound_notify(self, toxic, notif_error, 0, NULL); } } else if (key == '\r') { input_ret = true; @@ -524,10 +524,10 @@ static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t fr write_to_log(ctx->log, c_config, msg, nick, true); if (self->active_box != -1) { - box_notify2(self, c_config, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, + box_notify2(self, toxic, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, "%s has come online", nick); } else { - box_notify(self, c_config, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, + box_notify(self, toxic, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, "Toxic", "%s has come online", nick); } } else if (connection_status == TOX_CONNECTION_NONE) { @@ -536,10 +536,10 @@ static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t fr write_to_log(ctx->log, c_config, msg, nick, true); if (self->active_box != -1) { - box_notify2(self, c_config, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, + box_notify2(self, toxic, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, "%s has gone offline", nick); } else { - box_notify(self, c_config, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, + box_notify(self, toxic, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, "Toxic", "%s has gone offline", nick); } } @@ -595,7 +595,7 @@ static void prompt_onFriendRequest(ToxWindow *self, Toxic *toxic, const char *ke } line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/accept %d\" or \"/decline %d\"", n, n); - sound_notify(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOTIFWND, NULL); + sound_notify(self, toxic, generic_message, NT_WNDALERT_1 | NT_NOTIFWND, NULL); } void prompt_init_statusbar(Toxic *toxic, bool first_time_run) diff --git a/src/toxic.c b/src/toxic.c index 1aa8d39dd..7599d3f12 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -193,7 +193,7 @@ void exit_toxic_success(Toxic *toxic) curl_global_cleanup(); #ifdef X11 - terminate_x11focus(); + terminate_x11focus(&toxic->x11_focus); #endif /* X11 */ kill_toxic(toxic); @@ -1734,7 +1734,7 @@ int main(int argc, char **argv) #ifdef X11 - if (init_x11focus() == -1) { + if (init_x11focus(&toxic->x11_focus) == -1) { queue_init_message("X failed to initialize"); } diff --git a/src/toxic.h b/src/toxic.h index 102b49ecd..d3663c4e9 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -51,6 +51,10 @@ #include "settings.h" #include "toxic_constants.h" +#ifdef X11 +#include "x11focus.h" +#endif + typedef struct Client_Data { bool is_encrypted; char pass[MAX_PASSWORD_LEN + 1]; @@ -68,6 +72,11 @@ typedef struct Toxic { #ifdef AUDIO ToxAV *av; #endif + +#ifdef X11 + X11_Focus x11_focus; +#endif + Client_Data client_data; Client_Config *c_config; Run_Options *run_opts; diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 3c91268b6..cdec45020 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -25,7 +25,6 @@ #include #include "misc_tools.h" -#include "notify.h" #include "toxic.h" #include "toxic_strings.h" #include "windows.h" @@ -198,15 +197,14 @@ void rm_trailing_spaces_buf(ChatContext *ctx) ctx->line[ctx->len] = L'\0'; } -#define HIST_PURGE MAX_LINE_HIST / 4 +#define HIST_PURGE ((MAX_LINE_HIST) / 4) /* shifts hist items back and makes room for HIST_PURGE new entries */ static void shift_hist_back(ChatContext *ctx) { - int i; int n = MAX_LINE_HIST - HIST_PURGE; - for (i = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { wmemcpy(ctx->ln_history[i], ctx->ln_history[i + HIST_PURGE], MAX_STR_SIZE); } @@ -245,7 +243,6 @@ void fetch_hist_item(const Client_Config *c_config, ChatContext *ctx, int key_di if (key_dir == KEY_UP) { if (--ctx->hst_pos < 0) { ctx->hst_pos = 0; - sound_notify(NULL, c_config, notif_error, NT_ALWAYS, NULL); } } else { if (++ctx->hst_pos >= ctx->hst_tot) { @@ -265,9 +262,7 @@ void fetch_hist_item(const Client_Config *c_config, ChatContext *ctx, int key_di void strsubst(char *str, char old, char new) { - int i; - - for (i = 0; str[i] != '\0'; ++i) { + for (int i = 0; str[i] != '\0'; ++i) { if (str[i] == old) { str[i] = new; } @@ -276,9 +271,7 @@ void strsubst(char *str, char old, char new) void wstrsubst(wchar_t *str, wchar_t old, wchar_t new) { - int i; - - for (i = 0; str[i] != L'\0'; ++i) { + for (int i = 0; str[i] != L'\0'; ++i) { if (str[i] == old) { str[i] = new; } diff --git a/src/x11focus.c b/src/x11focus.c index 1ab537245..4d59abf74 100644 --- a/src/x11focus.c +++ b/src/x11focus.c @@ -26,62 +26,57 @@ #include -static struct Focus { - Display *display; - Window terminal_window; -} Focus; - -static long unsigned int focused_window_id(void) +static long unsigned int focused_window_id(const X11_Focus *focus) { - if (!Focus.display) { + if (!focus->display) { return 0; } - Window focus; + Window window_focus; int revert; - XLockDisplay(Focus.display); - XGetInputFocus(Focus.display, &focus, &revert); - XUnlockDisplay(Focus.display); + XLockDisplay(focus->display); + XGetInputFocus(focus->display, &window_focus, &revert); + XUnlockDisplay(focus->display); - return focus; + return window_focus; } -bool is_focused(void) +bool is_focused(const X11_Focus *focus) { - if (!Focus.display) { + if (!focus->display) { return false; } - return Focus.terminal_window == focused_window_id(); + return focus->terminal_window == focused_window_id(focus); } -int init_x11focus(void) +int init_x11focus(X11_Focus *focus) { if (XInitThreads() == 0) { return -1; } - Focus.display = XOpenDisplay(NULL); + focus->display = XOpenDisplay(NULL); - if (!Focus.display) { + if (!focus->display) { return -1; } - Focus.terminal_window = focused_window_id(); + focus->terminal_window = focused_window_id(focus); return 0; } -void terminate_x11focus(void) +void terminate_x11focus(X11_Focus *focus) { - if (!Focus.display || !Focus.terminal_window) { + if (!focus->display || !focus->terminal_window) { return; } - XLockDisplay(Focus.display); - XCloseDisplay(Focus.display); - XUnlockDisplay(Focus.display); + XLockDisplay(focus->display); + XCloseDisplay(focus->display); + XUnlockDisplay(focus->display); } #endif /* !__APPLE__ */ diff --git a/src/x11focus.h b/src/x11focus.h index 754870cb3..0bc21a914 100644 --- a/src/x11focus.h +++ b/src/x11focus.h @@ -1,4 +1,4 @@ -/* xtra.h +/* x11focus.h * * * Copyright (C) 2024 Toxic All Rights Reserved. @@ -25,10 +25,19 @@ #include -/* NOTE: If no xlib present don't compile */ +#ifndef __APPLE__ +#include -int init_x11focus(void); -void terminate_x11focus(void); -bool is_focused(void); +struct X11_Focus { + Display *display; + Window terminal_window; +}; +typedef struct X11_Focus X11_Focus; + +int init_x11focus(X11_Focus *focus); +void terminate_x11focus(X11_Focus *focus); +bool is_focused(const X11_Focus *focus); + +#endif /* __APPLE__ */ #endif /* X11FOCUS */