Skip to content

Commit

Permalink
Cleanup and refactor x11 code
Browse files Browse the repository at this point in the history
- Deleted X11 code for an unfinished/broken feature
- Rename xtra -> x11focus
- Cleanup the remaining code a bit
  • Loading branch information
JFreegman committed Nov 25, 2020
1 parent 898d89e commit 56ba61e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 464 deletions.
2 changes: 1 addition & 1 deletion cfg/checks/x11.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Variables for X11 support
X11_LIBS = x11
X11_CFLAGS = -DX11
X11_OBJ = xtra.o
X11_OBJ = x11focus.o

# Check if we can build X11 support
CHECK_X11_LIBS = $(shell $(PKG_CONFIG) --exists $(X11_LIBS) || echo -n "error")
Expand Down
2 changes: 1 addition & 1 deletion src/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "misc_tools.h"
#include "notify.h"
#include "settings.h"
#include "xtra.h"
#include "x11focus.h"

#if defined(AUDIO) || defined(SOUND_NOTIFY)
#ifdef __APPLE__
Expand Down
24 changes: 3 additions & 21 deletions src/toxic.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#include "windows.h"

#ifdef X11
#include "xtra.h"
#include "x11focus.h"
#endif

#ifdef AUDIO
Expand Down Expand Up @@ -205,10 +205,7 @@ void exit_toxic_success(Tox *m)
curl_global_cleanup();

#ifdef X11
/* We have to terminate xtra last coz reasons
* Please don't call this anywhere else coz trust me
*/
terminate_xtra();
terminate_x11focus();
#endif /* X11 */

exit(EXIT_SUCCESS);
Expand Down Expand Up @@ -1350,21 +1347,6 @@ static void init_default_data_files(void)
free(user_config_dir);
}

// this doesn't do anything (yet)
#ifdef X11
void DnD_callback(const char *asdv, DropType dt)
{
UNUSED_VAR(asdv);
UNUSED_VAR(dt);
// if (dt != DT_plain)
// return;

// pthread_mutex_lock(&Winthread.lock);
// line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv);
// pthread_mutex_unlock(&Winthread.lock);
}
#endif /* X11 */

int main(int argc, char **argv)
{
/* Make sure all written files are read/writeable only by the current user. */
Expand Down Expand Up @@ -1422,7 +1404,7 @@ int main(int argc, char **argv)

#ifdef X11

if (init_xtra(DnD_callback) == -1) {
if (init_x11focus() == -1) {
queue_init_message("X failed to initialize");
}

Expand Down
87 changes: 87 additions & 0 deletions src/x11focus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* x11focus.c
*
*
* Copyright (C) 2020 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "x11focus.h"

#ifndef __APPLE__

#include <X11/Xlib.h>

static struct Focus {
Display *display;
Window terminal_window;
} Focus;

static long unsigned int focused_window_id(void)
{
if (!Focus.display) {
return 0;
}

Window focus;
int revert;

XLockDisplay(Focus.display);
XGetInputFocus(Focus.display, &focus, &revert);
XUnlockDisplay(Focus.display);

return focus;
}

bool is_focused(void)
{
if (!Focus.display) {
return false;
}

return Focus.terminal_window == focused_window_id();
}

int init_x11focus(void)
{
if (XInitThreads() == 0) {
return -1;
}

Focus.display = XOpenDisplay(NULL);

if (!Focus.display) {
return -1;
}

Focus.terminal_window = focused_window_id();

return 0;
}

void terminate_x11focus(void)
{
if (!Focus.display || !Focus.terminal_window) {
return;
}

XLockDisplay(Focus.display);
XCloseDisplay(Focus.display);
XUnlockDisplay(Focus.display);
}

#endif /* !__APPLE__ */
24 changes: 9 additions & 15 deletions src/xtra.h → src/x11focus.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* xtra.h
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
* Copyright (C) 2020 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
Expand All @@ -20,21 +20,15 @@
*
*/

#ifndef XTRA_H
#define XTRA_H
#ifndef X11FOCUS_H
#define X11FOCUS_H

/* NOTE: If no xlib present don't compile */

typedef enum {
DT_plain,
DT_file_list
}
DropType;
#include <stdbool.h>

typedef void (*drop_callback)(const char *, DropType);
/* NOTE: If no xlib present don't compile */

int init_xtra(drop_callback d);
void terminate_xtra(void);
int is_focused(void); /* returns bool */
int init_x11focus(void);
void terminate_x11focus(void);
bool is_focused(void);

#endif /* XTRA_H */
#endif /* X11FOCUS */
Loading

0 comments on commit 56ba61e

Please sign in to comment.