dwlbtray.c (4027B) - View raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <signal.h> #include <string.h> #include <glib.h> #include <glib-object.h> #include <glib-unix.h> #include <gdk/gdk.h> #include <gtk/gtk.h> #include <gtk4-layer-shell.h> #include "snhost.h" typedef struct args_parsed { char cssdata[64]; int barheight; int position; } args_parsed; static const int margin = 4; static const int spacing = 4; enum { DWLB_POSITION_TOP, DWLB_POSITION_BOTTOM }; static void activate(GtkApplication* app, void *data) { args_parsed *args = (args_parsed*)data; GdkDisplay *display = gdk_display_get_default(); int iconsize, win_default_width, win_default_height; iconsize = args->barheight - 2 * margin; win_default_width = args->barheight; win_default_height = args->barheight; GtkCssProvider *css = gtk_css_provider_new(); gtk_css_provider_load_from_string(css, args->cssdata); gtk_style_context_add_provider_for_display(display, GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_USER); g_object_unref(css); gboolean anchors[4]; switch (args->position) { case DWLB_POSITION_TOP: anchors[GTK_LAYER_SHELL_EDGE_LEFT] = FALSE; anchors[GTK_LAYER_SHELL_EDGE_RIGHT] = TRUE; anchors[GTK_LAYER_SHELL_EDGE_TOP] = TRUE; anchors[GTK_LAYER_SHELL_EDGE_BOTTOM] = FALSE; break; case DWLB_POSITION_BOTTOM: anchors[GTK_LAYER_SHELL_EDGE_LEFT] = FALSE; anchors[GTK_LAYER_SHELL_EDGE_RIGHT] = TRUE; anchors[GTK_LAYER_SHELL_EDGE_TOP] = FALSE; anchors[GTK_LAYER_SHELL_EDGE_BOTTOM] = TRUE; break; default: g_assert_not_reached(); break; } GListModel *mons = gdk_display_get_monitors(display); // Create tray for each monitor unsigned int i; for (i = 0; i < g_list_model_get_n_items(mons); i++) { GdkMonitor *mon = g_list_model_get_item(mons, i); const char *conn = gdk_monitor_get_connector(mon); SnHost *host = sn_host_new(win_default_width, win_default_height, iconsize, margin, spacing, conn); GtkWindow *window = GTK_WINDOW(host); gtk_window_set_application(window, app); gtk_layer_init_for_window(window); gtk_layer_set_layer(window, GTK_LAYER_SHELL_LAYER_BOTTOM); gtk_layer_set_exclusive_zone(window, -1); gtk_layer_set_monitor(window, mon); for (int j = 0; j < GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER; j++) { gtk_layer_set_anchor(window, j, anchors[j]); } gtk_window_present(window); } } static void terminate_app_helper(void *data, void *udata) { GtkWindow *window = GTK_WINDOW(data); gtk_window_close(window); } static gboolean terminate_app(GtkApplication *app) { GList *windows = gtk_application_get_windows(app); g_list_foreach(windows, terminate_app_helper, NULL); return G_SOURCE_REMOVE; } int main(int argc, char *argv[]) { const char cssskele[] = "window{background-color:%s;}"; args_parsed args; args.barheight = 22; args.position = DWLB_POSITION_TOP; snprintf(args.cssdata, sizeof(args.cssdata), cssskele, "#222222"); int option; while ((option = getopt(argc, argv, "bc:s:")) != -1) { switch (option) { case 'b': // "bottom" args.position = DWLB_POSITION_BOTTOM; break; case 'c': // "color" snprintf(args.cssdata, sizeof(args.cssdata), cssskele, optarg); break; case 's': // "size" args.barheight = strtol(optarg, NULL, 10); break; } } GtkApplication *app = gtk_application_new("org.dwlb.dwlbtray", G_APPLICATION_DEFAULT_FLAGS); g_signal_connect(app, "activate", G_CALLBACK(activate), &args); g_unix_signal_add(SIGINT, (GSourceFunc)terminate_app, app); g_unix_signal_add(SIGTERM, (GSourceFunc)terminate_app, app); char *argv_inner[] = { argv[0], NULL }; int status = g_application_run(G_APPLICATION(app), 1, argv_inner); g_object_unref(app); return status; }