Commit 1e45abe9 authored by o9000's avatar o9000

Refactoring

parent 6852e253
......@@ -116,7 +116,9 @@ include_directories( ${PROJECT_BINARY_DIR}
set( SOURCES src/config.c
src/panel.c
src/server.c
src/tint.c
src/main.c
src/init.c
src/signals.c
src/mouse_actions.c
src/drag_and_drop.c
src/clock/clock.c
......@@ -137,6 +139,7 @@ set( SOURCES src/config.c
src/tint2rc.c
src/util/area.c
src/util/common.c
src/util/fps_distribution.c
src/util/strnatcmp.c
src/util/timer.c
src/util/cache.c
......
......@@ -33,6 +33,8 @@ static int dnd_sent_request;
static char *dnd_launcher_exec;
static gboolean dnd_debug = FALSE;
gboolean hidden_panel_shown_for_dnd;
void dnd_init()
{
dnd_source_window = 0;
......@@ -42,6 +44,7 @@ void dnd_init()
dnd_atom = None;
dnd_sent_request = 0;
dnd_launcher_exec = 0;
hidden_panel_shown_for_dnd = FALSE;
}
void handle_dnd_enter(XClientMessageEvent *e)
......
......@@ -7,6 +7,9 @@
#define DRAG_AND_DROP_H
#include <X11/Xlib.h>
#include <glib.h>
extern gboolean hidden_panel_shown_for_dnd;
void dnd_init();
......
#include "init.h"
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "fps_distribution.h"
#include "panel.h"
#include "server.h"
#include "signals.h"
#include "tooltip.h"
#include "uevent.h"
#include "version.h"
void print_usage()
{
printf("Usage: tint2 [OPTION...]\n"
"\n"
"Options:\n"
" -c path_to_config_file Loads the configuration file from a\n"
" custom location.\n"
" -v, --version Prints version information and exits.\n"
" -h, --help Display this help and exits.\n"
"\n"
"For more information, run `man tint2` or visit the project page\n"
"<https://gitlab.com/o9000/tint2>.\n");
}
void handle_cli_arguments(int argc, char **argv)
{
// Read command line arguments
for (int i = 1; i < argc; ++i) {
gboolean error = FALSE;
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
print_usage();
exit(0);
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
printf("tint2 version %s\n", VERSION_STRING);
exit(0);
} else if (strcmp(argv[i], "-c") == 0) {
if (i + 1 < argc) {
i++;
config_path = strdup(argv[i]);
} else {
error = TRUE;
}
} else if (strcmp(argv[i], "-s") == 0) {
if (i + 1 < argc) {
i++;
snapshot_path = strdup(argv[i]);
} else {
error = TRUE;
}
} else if (i + 1 == argc) {
config_path = strdup(argv[i]);
}
#ifdef ENABLE_BATTERY
else if (strcmp(argv[i], "--battery-sys-prefix") == 0) {
if (i + 1 < argc) {
i++;
battery_sys_prefix = strdup(argv[i]);
} else {
error = TRUE;
}
}
#endif
else {
error = TRUE;
}
if (error) {
print_usage();
exit(1);
}
}
}
void handle_env_vars()
{
debug_geometry = getenv("DEBUG_GEOMETRY") != NULL;
debug_gradients = getenv("DEBUG_GRADIENTS") != NULL;
debug_fps = getenv("DEBUG_FPS") != NULL;
debug_frames = getenv("DEBUG_FRAMES") != NULL;
if (debug_fps)
init_fps_distribution();
}
static timeout *detect_compositor_timer = NULL;
static int detect_compositor_timer_counter = 0;
void detect_compositor(void *arg)
{
if (server.composite_manager) {
stop_timeout(detect_compositor_timer);
return;
}
detect_compositor_timer_counter--;
if (detect_compositor_timer_counter < 0) {
stop_timeout(detect_compositor_timer);
return;
}
// No compositor, check for one
if (XGetSelectionOwner(server.display, server.atom._NET_WM_CM_S0) != None) {
stop_timeout(detect_compositor_timer);
// Restart tint2
fprintf(stderr, "Detected compositor, restarting tint2...\n");
kill(getpid(), SIGUSR1);
}
}
void start_detect_compositor()
{
// Already have a compositor, nothing to do
if (server.composite_manager)
return;
stop_timeout(detect_compositor_timer);
// Check every 0.5 seconds for up to 30 seconds
detect_compositor_timer_counter = 60;
detect_compositor_timer = add_timeout(500, 500, detect_compositor, 0, &detect_compositor_timer);
}
void create_default_elements()
{
default_config();
default_timeout();
default_systray();
memset(&server, 0, sizeof(server));
#ifdef ENABLE_BATTERY
default_battery();
#endif
default_clock();
default_launcher();
default_taskbar();
default_tooltip();
default_execp();
default_button();
default_panel();
}
void init_post_config()
{
server_init_visual();
server_init_xdamage();
imlib_context_set_display(server.display);
imlib_context_set_visual(server.visual);
imlib_context_set_colormap(server.colormap);
init_signals_postconfig();
// load default icon
const gchar *const *data_dirs = g_get_system_data_dirs();
for (int i = 0; data_dirs[i] != NULL; i++) {
gchar *path = g_build_filename(data_dirs[i], "tint2", "default_icon.png", NULL);
if (g_file_test(path, G_FILE_TEST_EXISTS))
default_icon = load_image(path, TRUE);
g_free(path);
}
if (!default_icon) {
fprintf(stderr,
RED "Could not load default_icon.png. Please check that tint2 has been installed correctly!" RESET
"\n");
}
}
void init_X11_pre_config()
{
server.display = XOpenDisplay(NULL);
if (!server.display) {
fprintf(stderr, "tint2: could not open display.\n");
exit(1);
}
server.x11_fd = ConnectionNumber(server.display);
XSetErrorHandler((XErrorHandler)server_catch_error);
XSetIOErrorHandler((XIOErrorHandler)x11_io_error);
server_init_atoms();
server.screen = DefaultScreen(server.display);
server.root_win = RootWindow(server.display, server.screen);
server.desktop = get_current_desktop();
// Needed since the config file uses '.' as decimal separator
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "POSIX");
/* Catch events */
XSelectInput(server.display, server.root_win, PropertyChangeMask | StructureNotifyMask);
// get monitor and desktop config
get_monitors();
get_desktops();
server.disable_transparency = 0;
xsettings_client = xsettings_client_new(server.display, server.screen, xsettings_notify_cb, NULL, NULL);
}
void init(int argc, char **argv)
{
setlinebuf(stdout);
setlinebuf(stderr);
handle_cli_arguments(argc, argv);
create_default_elements();
init_signals();
handle_env_vars();
init_X11_pre_config();
if (!config_read()) {
fprintf(stderr, "Could not read config file.\n");
print_usage();
cleanup();
return;
}
init_post_config();
start_detect_compositor();
init_panel();
}
void cleanup()
{
#ifdef HAVE_SN
if (startup_notifications) {
sn_display_unref(server.sn_display);
server.sn_display = NULL;
}
#endif // HAVE_SN
cleanup_button();
cleanup_execp();
cleanup_systray();
cleanup_tooltip();
cleanup_clock();
cleanup_launcher();
#ifdef ENABLE_BATTERY
cleanup_battery();
#endif
cleanup_panel();
cleanup_config();
if (default_icon) {
imlib_context_set_image(default_icon);
imlib_free_image();
default_icon = NULL;
}
imlib_context_disconnect_display();
xsettings_client_destroy(xsettings_client);
xsettings_client = NULL;
cleanup_server();
cleanup_timeout();
if (server.display)
XCloseDisplay(server.display);
server.display = NULL;
if (sigchild_pipe_valid) {
sigchild_pipe_valid = FALSE;
close(sigchild_pipe[1]);
close(sigchild_pipe[0]);
}
uevent_cleanup();
cleanup_fps_distribution();
}
#ifndef INIT_H
#define INIT_H
void init(int argc, char **argv);
void cleanup();
#endif
This diff is collapsed.
......@@ -38,8 +38,6 @@
void panel_clear_background(void *obj);
int signal_pending;
MouseAction mouse_left;
MouseAction mouse_middle;
MouseAction mouse_right;
......
......@@ -29,7 +29,6 @@
#include "battery.h"
#endif
extern int signal_pending;
// --------------------------------------------------
// mouse events
extern MouseAction mouse_left;
......
......@@ -27,8 +27,10 @@
#include <string.h>
#include <unistd.h>
#include "server.h"
#include "common.h"
#include "config.h"
#include "server.h"
#include "signals.h"
#include "window.h"
Server server;
......@@ -733,3 +735,43 @@ void forward_click(XEvent *e)
// XSetInputFocus(server.display, e->xbutton.window, RevertToParent, e->xbutton.time);
XSendEvent(server.display, e->xbutton.window, False, ButtonPressMask, e);
}
void handle_crash(const char *reason)
{
#ifndef DISABLE_BACKTRACE
char path[4096];
sprintf(path, "%s/.tint2-crash.log", get_home_dir());
int log_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
log_string(log_fd, RED "tint2 crashed, reason: ");
log_string(log_fd, reason);
log_string(log_fd, RESET "\n");
dump_backtrace(log_fd);
log_string(log_fd, RED "Please create a bug report with this log output." RESET "\n");
close(log_fd);
#endif
}
void x11_io_error(Display *display)
{
handle_crash("X11 I/O error");
}
#ifdef HAVE_SN
static int error_trap_depth = 0;
void error_trap_push(SnDisplay *display, Display *xdisplay)
{
++error_trap_depth;
}
void error_trap_pop(SnDisplay *display, Display *xdisplay)
{
if (error_trap_depth == 0) {
fprintf(stderr, "Error trap underflow!\n");
return;
}
XSync(xdisplay, False); /* get all errors out of the queue */
--error_trap_depth;
}
#endif // HAVE_SN
......@@ -135,6 +135,7 @@ typedef struct Viewport {
typedef struct Server {
Display *display;
int x11_fd;
Window root_win;
Window composite_manager;
gboolean real_transparency;
......@@ -181,6 +182,9 @@ void server_init_atoms();
void server_init_visual();
void server_init_xdamage();
void x11_io_error(Display *display);
void handle_crash(const char *reason);
// detect root background
void get_root_pixmap();
......@@ -197,4 +201,9 @@ void change_desktop(int desktop);
// Forward mouse click to the desktop window
void forward_click(XEvent *e);
#ifdef HAVE_SN
void error_trap_push(SnDisplay *display, Display *xdisplay);
void error_trap_pop(SnDisplay *display, Display *xdisplay);
#endif
#endif
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#ifdef HAVE_SN
#include <libsn/sn.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "common.h"
#include "panel.h"
#include "launcher.h"
#include "server.h"
#include "signals.h"
static sig_atomic_t signal_pending;
void signal_handler(int sig)
{
// signal handler is light as it should be
signal_pending = sig;
}
void init_signals()
{
// Set signal handlers
signal_pending = 0;
struct sigaction sa_chld = {.sa_handler = SIG_DFL, .sa_flags = SA_NOCLDWAIT | SA_RESTART};
sigaction(SIGCHLD, &sa_chld, 0);
struct sigaction sa = {.sa_handler = signal_handler, .sa_flags = SA_RESTART};
sigaction(SIGUSR1, &sa, 0);
sigaction(SIGUSR2, &sa, 0);
sigaction(SIGINT, &sa, 0);
sigaction(SIGTERM, &sa, 0);
sigaction(SIGHUP, &sa, 0);
#ifdef BACKTRACE_ON_SIGNAL
struct sigaction sa_crash = {.sa_handler = crash_handler};
sigaction(SIGSEGV, &sa_crash, 0);
sigaction(SIGFPE, &sa_crash, 0);
sigaction(SIGPIPE, &sa_crash, 0);
sigaction(SIGBUS, &sa_crash, 0);
sigaction(SIGABRT, &sa_crash, 0);
sigaction(SIGSYS, &sa_crash, 0);
#endif
}
#ifdef BACKTRACE_ON_SIGNAL
void crash_handler(int sig)
{
handle_crash(signal_name(sig));
struct sigaction sa = {.sa_handler = SIG_DFL};
sigaction(sig, &sa, 0);
raise(sig);
}
#endif
int sigchild_pipe_valid = FALSE;
int sigchild_pipe[2];
static void sigchld_handler(int sig)
{
if (!sigchild_pipe_valid)
return;
int savedErrno = errno;
ssize_t unused = write(sigchild_pipe[1], "x", 1);
(void)unused;
fsync(sigchild_pipe[1]);
errno = savedErrno;
}
void sigchld_handler_async()
{
// Wait for all dead processes
pid_t pid;
int status;
while ((pid = waitpid(-1, &status, WNOHANG)) != -1 && pid != 0) {
#ifdef HAVE_SN
if (startup_notifications) {
SnLauncherContext *ctx = (SnLauncherContext *)g_tree_lookup(server.pids, GINT_TO_POINTER(pid));
if (ctx) {
g_tree_remove(server.pids, GINT_TO_POINTER(pid));
sn_launcher_context_complete(ctx);
sn_launcher_context_unref(ctx);
}
}
#endif
for (GList *l = panel_config.execp_list; l; l = l->next) {
Execp *execp = (Execp *)l->data;
if (g_tree_lookup(execp->backend->cmd_pids, GINT_TO_POINTER(pid)))
execp_cmd_completed(execp, pid);
}
}
}
void init_signals_postconfig()
{
gboolean need_sigchld = FALSE;
#ifdef HAVE_SN
// Initialize startup-notification
if (startup_notifications) {
server.sn_display = sn_display_new(server.display, error_trap_push, error_trap_pop);
server.pids = g_tree_new(cmp_ptr);
need_sigchld = TRUE;
}
#endif // HAVE_SN
if (panel_config.execp_list)
need_sigchld = TRUE;
if (need_sigchld) {
// Setup a handler for child termination
if (pipe(sigchild_pipe) != 0) {
fprintf(stderr, "Creating pipe failed.\n");
} else {
fcntl(sigchild_pipe[0], F_SETFL, O_NONBLOCK | fcntl(sigchild_pipe[0], F_GETFL));
fcntl(sigchild_pipe[1], F_SETFL, O_NONBLOCK | fcntl(sigchild_pipe[1], F_GETFL));
sigchild_pipe_valid = 1;
struct sigaction act = {.sa_handler = sigchld_handler, .sa_flags = SA_RESTART};
if (sigaction(SIGCHLD, &act, 0)) {
perror("sigaction");
}
}
}
}
void emit_self_restart(const char *reason)
{
fprintf(stderr,
YELLOW "%s %d: triggering tint2 restart, reason: %s" RESET "\n",
__FILE__,
__LINE__,
reason);
signal_pending = SIGUSR1;
}
int get_signal_pending()
{
return signal_pending;
}
#ifndef SIGNALS_H
#define SIGNALS_H
void init_signals();
void init_signals_postconfig();
void emit_self_restart(const char *reason);
int get_signal_pending();
void sigchld_handler_async();
extern int sigchild_pipe_valid;
extern int sigchild_pipe[2];
#endif
......@@ -859,55 +859,6 @@ gboolean embed_icon(TrayWindow *traywin)
error = FALSE;
XErrorHandler old = XSetErrorHandler(window_error_handler);
if (0) {
Atom acttype;
int actfmt;
unsigned long nbitem, bytes;
unsigned long *data = 0;
int ret;
if (systray_profile)
fprintf(stderr,
"XGetWindowProperty(server.display, traywin->win, server.atom._XEMBED_INFO, 0, 2, False, "
"server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data)\n");
ret = XGetWindowProperty(server.display,
traywin->win,
server.atom._XEMBED_INFO,
0,
2,
False,
server.atom._XEMBED_INFO,
&acttype,
&actfmt,
&nbitem,
&bytes,
(unsigned char **)&data);
if (ret == Success) {
if (data) {
if (nbitem >= 2) {
int hide = ((data[1] & XEMBED_MAPPED) == 0);
if (hide) {
// In theory we have to check the embedding with this and remove icons that refuse embedding.
// In practice we have no idea when the other application processes the event and accepts the
// embed so we cannot check now without a race.
// Race can be triggered with PyGtk(2) apps.
// We could defer this for later (if we set PropertyChangeMask in XSelectInput we get notified)
// but for some reason it breaks transparency for Qt icons. So we don't.
// fprintf(stderr, RED "tint2: window refused embedding" RESET "\n");
// remove_icon(traywin);
// XFree(data);
// return FALSE;
}
}
XFree(data);
}
} else {
fprintf(stderr, RED "tint2 : xembed error" RESET "\n");
remove_icon(traywin);
return FALSE;
}
}
// Redirect rendering when using compositing
if (systray_composited) {
if (systray_profile)
......
......@@ -330,8 +330,6 @@ void task_update_icon(Task *task)
if (img == NULL) {
imlib_context_set_image(default_icon);
img = imlib_clone_image();
if (0)
fprintf(stderr, "%s: Using default icon for %s\n", __FUNCTION__, task->title ? task->title : "task");
}
// transform icons
......@@ -782,3 +780,13 @@ void task_handle_mouse_event(Task *task, MouseAction action)
}
}
}
void task_update_desktop(Task *task)
{
// fprintf(stderr, "%s %d:\n", __FUNCTION__, __LINE__);
Window win = task->win;
remove_task(task);
task = add_task(win);
reset_active_task();
schedule_panel_redraw();
}
......@@ -86,6 +86,7 @@ void draw_task(void *obj, cairo_t *c);
void on_change_task(void *obj);
void task_update_icon(Task *task);
void task_update_desktop(Task *task);
gboolean task_update_title(Task *task);
void reset_active_task();
void set_task_state(Task *task, TaskState state);
......
......@@ -299,16 +299,6 @@ void init_taskbar_panel(void *p)
top_bottom_border_width(&panel->g_task.area));
panel->g_task.text_posx += panel->g_task.icon_size1 + panel->g_task.area.paddingx;
panel->g_task.icon_posy = (panel->g_task.area.height - panel->g_task.icon_size1) / 2;
if (0)
printf("task: icon_size = %d, textx = %f, texth = %f, icony = %d, w = %d, h = %d, maxw = %d, maxh = %d\n",
panel->g_task.icon_size1,
panel->g_task.text_posx,
panel->g_task.text_height,
panel->g_task.icon_posy,
panel->g_task.area.width,
panel->g_task.area.height,
panel->g_task.maximum_width,
panel->g_task.maximum_height);
}
Taskbar *taskbar;
......
......@@ -214,3 +214,35 @@ void draw_taskbarname(void *obj, cairo_t *c)
g_object_unref(layout);
}
void update_desktop_names()
{
if (!taskbarname_enabled)
return;
GSList *list = get_desktop_names();
for (int i = 0; i < num_panels; i++) {
int j;
GSList *l;
for (j = 0, l = list; j < panels[i].num_desktops; j++) {
gchar *name;
if (l) {
name = g_strdup(l->data);
l = l->next;
} else {
name = g_strdup_printf("%d", j + 1);
}
Taskbar *taskbar = &panels[i].taskbar[j];
if (strcmp(name, taskbar->bar_name.name) != 0) {
g_free(taskbar->bar_name.name);
taskbar->bar_name.name = name;
taskbar->bar_name.area.resize_needed = 1;
} else {
g_free(name);
}
}
}
for (GSList *l = list; l; l = l->next)
g_free(l->data);
g_slist_free(list);
schedule_panel_redraw();
}
......@@ -23,4 +23,6 @@ gboolean resize_taskbarname(void *obj);
void taskbarname_default_font_changed();
void update_desktop_names();
#endif
......@@ -22,6 +22,7 @@ include_directories( ../util
set(SOURCES ../util/common.c
../util/strnatcmp.c
../util/cache.c
../util/timer.c