Compare commits
60 Commits
6382afeb03
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
a5d31cccdd
|
|||
|
86a89452a7
|
|||
| 123fe77bbb | |||
| 1ec92ec3e2 | |||
|
cdcafa1216
|
|||
|
d8cd91ea48
|
|||
|
35f0ea9680
|
|||
|
4ccdc9c23d
|
|||
|
ac4cfc1ed3
|
|||
|
9fbd7f45e9
|
|||
|
ea47717783
|
|||
|
850221793f
|
|||
|
5b41eb97cd
|
|||
|
cecd7dc2d9
|
|||
|
92a810f345
|
|||
|
9f8b2453fa
|
|||
|
4428eb056d
|
|||
|
fb0999df90
|
|||
|
41a2289c96
|
|||
|
9b7c3a67ef
|
|||
|
ab3d441928
|
|||
|
94d3be12ec
|
|||
|
323a0f3711
|
|||
|
8c0b295115
|
|||
|
89275d0643
|
|||
|
d516d8f9e0
|
|||
|
6d65c53d43
|
|||
|
9baa3d4d1c
|
|||
|
3edac120d2
|
|||
|
b5c0cfefd7
|
|||
|
a69f3bbc64
|
|||
|
194c2b000d
|
|||
|
3b7bd55519
|
|||
|
8337ab9519
|
|||
|
0e07ba135b
|
|||
|
4b6df52b41
|
|||
|
a08fbcbb9c
|
|||
|
5dd042384d
|
|||
|
a748159dcd
|
|||
|
6f4690705a
|
|||
|
a5ce85be78
|
|||
|
a772d0f56f
|
|||
|
5ecaa97f10
|
|||
|
99d2e33a46
|
|||
|
d1fa848632
|
|||
|
23fb95a5e4
|
|||
|
47d578ca71
|
|||
|
ef37f28fa5
|
|||
|
3d5f8ab7e0
|
|||
|
21d7adf882
|
|||
|
3e2bf57218
|
|||
|
049b18a496
|
|||
|
ced78b004f
|
|||
|
5e22aed718
|
|||
|
6b49045ee9
|
|||
|
2902c994ba
|
|||
|
1dbcf3e654
|
|||
|
8fba9ee71f
|
|||
|
943ede70a0
|
|||
|
fac2a84d7f
|
@@ -6,6 +6,11 @@ IndentWidth: 2
|
||||
ConstructorInitializerIndentWidth: 2
|
||||
ContinuationIndentWidth: 2
|
||||
---
|
||||
Language: C
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Right
|
||||
---
|
||||
Language: Cpp
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Right
|
||||
---
|
||||
|
||||
@@ -41,6 +41,14 @@ add_executable(${APP_NAME}
|
||||
${SOURCE_DIR}/atoms.c
|
||||
${SOURCE_DIR}/layout.c
|
||||
${SOURCE_DIR}/xres.c
|
||||
${SOURCE_DIR}/status.c
|
||||
${SOURCE_DIR}/audio.c
|
||||
${SOURCE_DIR}/rect.c
|
||||
${SOURCE_DIR}/wallpaper.c
|
||||
${SOURCE_DIR}/config.c
|
||||
${SOURCE_DIR}/renderer.c
|
||||
${SOURCE_DIR}/image.c
|
||||
${SOURCE_DIR}/tray.c
|
||||
)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
@@ -65,6 +73,9 @@ pkg_check_modules(deps REQUIRED
|
||||
cairo-xcb
|
||||
pango
|
||||
pangocairo
|
||||
imlib2
|
||||
|
||||
libpulse-mainloop-glib
|
||||
)
|
||||
|
||||
target_include_directories(${APP_NAME} SYSTEM
|
||||
@@ -76,6 +87,7 @@ target_include_directories(${APP_NAME}
|
||||
)
|
||||
|
||||
target_link_libraries(${APP_NAME}
|
||||
PRIVATE m
|
||||
PRIVATE ${deps_LIBRARIES}
|
||||
)
|
||||
|
||||
|
||||
112
src/action.c
112
src/action.c
@@ -3,10 +3,12 @@
|
||||
#include <glib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "audio.h"
|
||||
#include "client.h"
|
||||
#include "monitor.h"
|
||||
#include "types.h"
|
||||
#include "wm.h"
|
||||
#include "xcursor.h"
|
||||
|
||||
void focus_client_in_same_tag(const user_action_arg_t *arg) {
|
||||
bool next = arg->b;
|
||||
@@ -29,6 +31,29 @@ void select_tag_of_current_monitor(const user_action_arg_t *arg) {
|
||||
monitor_select_tag(wm.current_monitor, arg->ui);
|
||||
}
|
||||
|
||||
void send_client_to_tag(const user_action_arg_t *arg) {
|
||||
if (wm.client_focused) client_send_to_tag(wm.client_focused, arg->ui);
|
||||
}
|
||||
|
||||
void send_client_to_next_monitor(const user_action_arg_t *arg) {
|
||||
if (!wm.client_focused) return;
|
||||
|
||||
monitor_t *next_monitor = wm_get_next_monitor(wm.client_focused->monitor);
|
||||
if (wm.client_focused->monitor == next_monitor) return;
|
||||
|
||||
client_send_to_monitor(wm.client_focused, next_monitor);
|
||||
}
|
||||
|
||||
void focus_next_monitor(const user_action_arg_t *arg) {
|
||||
monitor_t *next_monitor = wm_get_next_monitor(wm.current_monitor);
|
||||
if (next_monitor == wm.current_monitor) return;
|
||||
|
||||
wm_set_current_monitor(next_monitor, true);
|
||||
if (wm.current_monitor->selected_tag->task_list) {
|
||||
monitor_deal_focus(wm.current_monitor);
|
||||
}
|
||||
}
|
||||
|
||||
void spawn(const user_action_arg_t *arg) {
|
||||
const char *cmd = (const char *)arg->ptr;
|
||||
if (cmd == nullptr || strlen(cmd) == 0) return;
|
||||
@@ -44,3 +69,90 @@ void quit(const user_action_arg_t *arg) {
|
||||
wm_quit();
|
||||
}
|
||||
}
|
||||
|
||||
void raise_or_run(const user_action_arg_t *arg) {
|
||||
const char *class = ((const char **)arg->ptr)[0];
|
||||
client_t *client = client_get_next_by_class(wm.client_focused, class);
|
||||
|
||||
if (client) {
|
||||
bool monitor_changed = client->monitor != wm.current_monitor;
|
||||
bool tag_changed = client->tags != client->monitor->selected_tag->mask;
|
||||
if (monitor_changed || tag_changed) {
|
||||
point_t point = monitor_changed ? monitor_get_restore_cursor_point(
|
||||
client->monitor)
|
||||
: xcursor_query_pointer_position();
|
||||
wm_ignore_enter_notify_at_point(point);
|
||||
}
|
||||
wm_set_current_monitor(client->monitor, true);
|
||||
monitor_select_tag(client->monitor, client->tags);
|
||||
client_stack_raise(client);
|
||||
client_focus(client);
|
||||
return;
|
||||
}
|
||||
|
||||
const char *command = ((const char **)arg->ptr)[1];
|
||||
user_action_arg_t arguments = {.ptr = command};
|
||||
spawn(&arguments);
|
||||
}
|
||||
|
||||
void toggle_mute(const user_action_arg_t *arg) {
|
||||
if (wm.status->pulse_context) toggle_pulse_mute(wm.status->pulse_context);
|
||||
}
|
||||
|
||||
void change_volume(const user_action_arg_t *arg) {
|
||||
if (wm.status->pulse_context) {
|
||||
change_pulse_volume(wm.status->pulse_context, arg->i);
|
||||
}
|
||||
}
|
||||
|
||||
void toggle_client_floating(const user_action_arg_t *arg) {
|
||||
if (!wm.client_focused) return;
|
||||
|
||||
client_set_floating(wm.client_focused, !wm.client_focused->floating);
|
||||
}
|
||||
|
||||
void toggle_client_fullscreen(const user_action_arg_t *arg) {
|
||||
if (!wm.client_focused) return;
|
||||
|
||||
client_set_fullscreen(wm.client_focused, !wm.client_focused->fullscreen);
|
||||
}
|
||||
|
||||
void toggle_client_maximize(const user_action_arg_t *arg) {
|
||||
if (!wm.client_focused) return;
|
||||
|
||||
client_set_maximize(wm.client_focused, !wm.client_focused->maximize);
|
||||
}
|
||||
|
||||
void toggle_client_minimize(const user_action_arg_t *arg) {
|
||||
client_t *client = wm.client_focused;
|
||||
if (arg->ptr) client = (client_t *)arg->ptr;
|
||||
if (!client) return;
|
||||
|
||||
client_set_minimize(client, !client->minimize);
|
||||
}
|
||||
|
||||
void kill_client(const user_action_arg_t *arg) {
|
||||
if (wm.client_focused) {
|
||||
client_kill(wm.client_focused);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 根据 client 的当前状态切换 client 下一状态
|
||||
* @description 如果当前 client 被遮挡了或者最小化了,则显示出来,否则最小化
|
||||
*/
|
||||
void change_client_state_dwim(const user_action_arg_t *arg) {
|
||||
client_t *client = (client_t *)arg->ptr;
|
||||
if (!client) return;
|
||||
|
||||
if (client != wm.client_stack_list &&
|
||||
client_get_obscured_state(client, wm.client_stack_list) !=
|
||||
obscured_none) {
|
||||
client_stack_raise(client);
|
||||
client_focus(client);
|
||||
return;
|
||||
}
|
||||
|
||||
client_set_minimize(client, !client->minimize);
|
||||
if (!client->minimize) client_stack_raise(client);
|
||||
}
|
||||
|
||||
13
src/action.h
13
src/action.h
@@ -13,6 +13,7 @@ typedef union user_action_arg_t {
|
||||
typedef enum click_area_t {
|
||||
click_none,
|
||||
click_tag,
|
||||
click_client_name,
|
||||
} click_area_t;
|
||||
|
||||
typedef enum modifier_t {
|
||||
@@ -50,5 +51,17 @@ typedef struct keyboard_t {
|
||||
|
||||
void focus_client_in_same_tag(const user_action_arg_t *arg);
|
||||
void select_tag_of_current_monitor(const user_action_arg_t *arg);
|
||||
void send_client_to_tag(const user_action_arg_t *arg);
|
||||
void send_client_to_next_monitor(const user_action_arg_t *arg);
|
||||
void focus_next_monitor(const user_action_arg_t *arg);
|
||||
void spawn(const user_action_arg_t *arg);
|
||||
void quit(const user_action_arg_t *arg);
|
||||
void raise_or_run(const user_action_arg_t *arg);
|
||||
void toggle_mute(const user_action_arg_t *arg);
|
||||
void change_volume(const user_action_arg_t *arg);
|
||||
void toggle_client_floating(const user_action_arg_t *arg);
|
||||
void toggle_client_fullscreen(const user_action_arg_t *arg);
|
||||
void toggle_client_maximize(const user_action_arg_t *arg);
|
||||
void toggle_client_minimize(const user_action_arg_t *arg);
|
||||
void kill_client(const user_action_arg_t *arg);
|
||||
void change_client_state_dwim(const user_action_arg_t *arg);
|
||||
|
||||
@@ -1,15 +1,34 @@
|
||||
_XKB_RULES_NAMES
|
||||
|
||||
_XROOTPMAP_ID
|
||||
ESETROOT_PMAP_ID
|
||||
|
||||
WM_CHANGE_STATE
|
||||
WM_DELETE_WINDOW
|
||||
WM_PROTOCOLS
|
||||
WM_TAKE_FOCUS
|
||||
WM_NAME
|
||||
WM_ICON_NAME
|
||||
WM_WINDOW_ROLE
|
||||
|
||||
UTF8_STRING
|
||||
COMPOUND_TEXT
|
||||
MANAGER
|
||||
_XEMBED
|
||||
_XEMBED_INFO
|
||||
|
||||
_NET_ACTIVE_WINDOW
|
||||
_NET_CLIENT_LIST
|
||||
_NET_SYSTEM_TRAY_COLORS
|
||||
_NET_SYSTEM_TRAY_OPCODE
|
||||
_NET_SYSTEM_TRAY_ORIENTATION
|
||||
_NET_WM_DESKTOP
|
||||
_NET_WM_NAME
|
||||
_NET_WM_ICON_NAME
|
||||
_NET_WM_STATE
|
||||
_NET_WM_STATE_FULLSCREEN
|
||||
_NET_WM_STATE_MAXIMIZED_HORZ
|
||||
_NET_WM_STATE_MAXIMIZED_VERT
|
||||
_NET_WM_STATE_SKIP_TASKBAR
|
||||
_NET_WM_WINDOW_TYPE
|
||||
_NET_WM_WINDOW_TYPE_DIALOG
|
||||
_NET_SUPPORTED
|
||||
_NET_SUPPORTING_WM_CHECK
|
||||
|
||||
18
src/atoms.c
18
src/atoms.c
@@ -1,9 +1,12 @@
|
||||
#include "atoms.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "atoms-intern.h"
|
||||
#include "utils.h"
|
||||
#include "wm.h"
|
||||
|
||||
void atoms_init(xcb_connection_t *conn) {
|
||||
xcb_intern_atom_cookie_t cookies[countof(ATOM_LIST)];
|
||||
@@ -12,12 +15,25 @@ void atoms_init(xcb_connection_t *conn) {
|
||||
cookies[i] = xcb_intern_atom_unchecked(conn, false, atom.len, atom.name);
|
||||
}
|
||||
|
||||
atom_item_t *atom = nullptr;
|
||||
uint32_t supported_length = 0;
|
||||
xcb_atom_t supported_list[countof(ATOM_LIST)] = {XCB_ATOM_NONE};
|
||||
|
||||
xcb_intern_atom_reply_t *reply = nullptr;
|
||||
for (int i = 0; i < countof(ATOM_LIST); i++) {
|
||||
reply = xcb_intern_atom_reply(conn, cookies[i], nullptr);
|
||||
if (reply) {
|
||||
*ATOM_LIST[i].atom = reply->atom;
|
||||
atom = &ATOM_LIST[i];
|
||||
*atom->atom = reply->atom;
|
||||
p_delete(&reply);
|
||||
|
||||
if (strstr(atom->name, "_NET_") == atom->name) {
|
||||
supported_list[supported_length++] = *atom->atom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, wm.screen->root,
|
||||
_NET_SUPPORTED, XCB_ATOM_ATOM, 32, supported_length,
|
||||
supported_list);
|
||||
}
|
||||
|
||||
182
src/audio.c
Normal file
182
src/audio.c
Normal file
@@ -0,0 +1,182 @@
|
||||
#include "audio.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <math.h>
|
||||
#include <pulse/context.h>
|
||||
#include <pulse/def.h>
|
||||
#include <pulse/glib-mainloop.h>
|
||||
#include <pulse/introspect.h>
|
||||
#include <pulse/mainloop-api.h>
|
||||
#include <pulse/proplist.h>
|
||||
#include <pulse/subscribe.h>
|
||||
#include <pulse/volume.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "app.h"
|
||||
#include "utils.h"
|
||||
|
||||
struct pulse_context_t {
|
||||
GMainContext *g_context;
|
||||
pulse_notify_t callback;
|
||||
pa_glib_mainloop *loop;
|
||||
pa_context *context;
|
||||
pa_cvolume volume;
|
||||
pulse_t pulse;
|
||||
uint32_t index;
|
||||
bool inited;
|
||||
};
|
||||
|
||||
static void clean_pulse_context(pulse_context_t *context, bool full_cleanup);
|
||||
static void init_pulse_context(pulse_context_t *context, bool create_loop);
|
||||
static void state_callback(pa_context *context, void *userdata);
|
||||
static void subscribe_callback(pa_context *context,
|
||||
pa_subscription_event_type_t t, uint32_t index,
|
||||
void *userdata);
|
||||
static void server_info_callback(pa_context *context,
|
||||
const pa_server_info *info, void *userdata);
|
||||
static void sink_info_callback(pa_context *context, const pa_sink_info *info,
|
||||
int eol, void *userdata);
|
||||
static void parse_sink(const pa_sink_info *info, pulse_t *pulse);
|
||||
|
||||
pulse_context_t *init_pulse(GMainContext *context, pulse_notify_t callback) {
|
||||
pulse_context_t *ctx = p_new(pulse_context_t, 1);
|
||||
ctx->g_context = context;
|
||||
ctx->callback = callback;
|
||||
init_pulse_context(ctx, true);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void change_pulse_volume(pulse_context_t *context, int step) {
|
||||
if (!context->inited || !step || step > 100 || step < -100) return;
|
||||
|
||||
pa_cvolume vol = context->volume;
|
||||
if (step > 0) {
|
||||
pa_cvolume_inc(&vol, PA_VOLUME_NORM * step / 100);
|
||||
if (pa_cvolume_max(&vol) > PA_VOLUME_NORM) {
|
||||
pa_cvolume_set(&vol, vol.channels, PA_VOLUME_NORM);
|
||||
}
|
||||
} else {
|
||||
pa_cvolume_dec(&vol, PA_VOLUME_NORM * (-step) / 100);
|
||||
if (pa_cvolume_min(&vol) < PA_VOLUME_MUTED) {
|
||||
pa_cvolume_set(&vol, vol.channels, PA_VOLUME_MUTED);
|
||||
}
|
||||
}
|
||||
pa_context *ctx = context->context;
|
||||
uint32_t index = context->index;
|
||||
pa_context_set_sink_volume_by_index(ctx, index, &vol, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void toggle_pulse_mute(pulse_context_t *context) {
|
||||
if (!context->inited) return;
|
||||
|
||||
pa_context *ctx = context->context;
|
||||
uint32_t index = context->index;
|
||||
bool mute = !context->pulse.mute;
|
||||
pa_context_set_sink_mute_by_index(ctx, index, mute, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void clean_pulse(pulse_context_t *context) {
|
||||
clean_pulse_context(context, true);
|
||||
p_delete(&context);
|
||||
}
|
||||
|
||||
void clean_pulse_context(pulse_context_t *context, bool full_cleanup) {
|
||||
memset(&context->pulse, 0, sizeof(context->pulse));
|
||||
if (context->callback) context->callback(&context->pulse);
|
||||
if (context->context) {
|
||||
pa_context_set_state_callback(context->context, nullptr, nullptr);
|
||||
pa_context_set_subscribe_callback(context->context, nullptr, nullptr);
|
||||
pa_context_disconnect(context->context);
|
||||
pa_context_unref(context->context);
|
||||
context->context = nullptr;
|
||||
}
|
||||
context->inited = false;
|
||||
if (full_cleanup && context->loop) {
|
||||
pa_glib_mainloop_free(context->loop);
|
||||
context->loop = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void init_pulse_context(pulse_context_t *context, bool create_loop) {
|
||||
if (create_loop) {
|
||||
if (context->loop) pa_glib_mainloop_free(context->loop);
|
||||
context->loop = pa_glib_mainloop_new(context->g_context);
|
||||
}
|
||||
pa_mainloop_api *api = pa_glib_mainloop_get_api(context->loop);
|
||||
context->context = pa_context_new(api, APP_NAME "-audio-status");
|
||||
pa_context_connect(context->context, nullptr, PA_CONTEXT_NOFAIL, nullptr);
|
||||
pa_context_set_state_callback(context->context, state_callback, context);
|
||||
}
|
||||
|
||||
void state_callback(pa_context *context, void *userdata) {
|
||||
pulse_context_t *ctx = userdata;
|
||||
|
||||
pa_context_state_t state = pa_context_get_state(context);
|
||||
if (PA_CONTEXT_IS_GOOD(state)) {
|
||||
pa_context_set_subscribe_callback(context, subscribe_callback, ctx);
|
||||
pa_context_subscribe(context, PA_SUBSCRIPTION_MASK_SINK, nullptr, nullptr);
|
||||
pa_context_get_server_info(context, server_info_callback, ctx);
|
||||
} else if (state == PA_CONTEXT_FAILED) {
|
||||
clean_pulse_context(ctx, false);
|
||||
init_pulse_context(ctx, false);
|
||||
}
|
||||
}
|
||||
|
||||
void subscribe_callback(pa_context *context, pa_subscription_event_type_t t,
|
||||
uint32_t index, void *userdata) {
|
||||
auto facility = t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
|
||||
if (facility != PA_SUBSCRIPTION_EVENT_SINK) return;
|
||||
|
||||
pa_subscription_event_type_t event = t & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
|
||||
if (event == PA_SUBSCRIPTION_EVENT_CHANGE) {
|
||||
pa_context_get_server_info(context, server_info_callback, userdata);
|
||||
}
|
||||
}
|
||||
|
||||
void server_info_callback(pa_context *context, const pa_server_info *info,
|
||||
void *userdata) {
|
||||
pa_context_get_sink_info_by_name(context, info->default_sink_name,
|
||||
sink_info_callback, userdata);
|
||||
}
|
||||
|
||||
void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol,
|
||||
void *userdata) {
|
||||
if (eol != 0 || !info) return;
|
||||
|
||||
pulse_context_t *ctx = userdata;
|
||||
ctx->inited = true;
|
||||
ctx->volume = info->volume;
|
||||
ctx->index = info->index;
|
||||
parse_sink(info, &ctx->pulse);
|
||||
if (ctx->callback) ctx->callback(&ctx->pulse);
|
||||
}
|
||||
|
||||
static inline int volume_to_percent(pa_volume_t volume) {
|
||||
return (int)round(((double)volume) * 100 / PA_VOLUME_NORM);
|
||||
}
|
||||
|
||||
void parse_sink(const pa_sink_info *info, pulse_t *pulse) {
|
||||
size_t size = sizeof(pulse->device_name);
|
||||
pulse->mute = info->mute;
|
||||
pulse->volume_percent = volume_to_percent(pa_cvolume_avg(&info->volume));
|
||||
strncpy(pulse->device_name, info->description, size);
|
||||
|
||||
const char *key = nullptr;
|
||||
void *state = nullptr;
|
||||
while ((key = pa_proplist_iterate(info->proplist, &state)) != nullptr) {
|
||||
if (strcmp(key, "api.alsa.path") != 0 &&
|
||||
strcmp(key, "device.description") != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *value = pa_proplist_gets(info->proplist, key);
|
||||
size_t len = strnlen(value, size);
|
||||
if (len && (len < strnlen(pulse->device_name, size))) {
|
||||
strncpy(pulse->device_name, value, size);
|
||||
pulse->device_name[size - 1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/audio.h
Normal file
19
src/audio.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct pulse_context_t pulse_context_t;
|
||||
|
||||
typedef struct pulse_t {
|
||||
int volume_percent;
|
||||
bool mute;
|
||||
char device_name[255];
|
||||
} pulse_t;
|
||||
|
||||
typedef void (*pulse_notify_t)(pulse_t *pulse);
|
||||
|
||||
pulse_context_t *init_pulse(GMainContext *context, pulse_notify_t callback);
|
||||
void change_pulse_volume(pulse_context_t *context, int step);
|
||||
void toggle_pulse_mute(pulse_context_t *context);
|
||||
void clean_pulse(pulse_context_t *context);
|
||||
@@ -11,3 +11,7 @@ typedef struct extent_in_bar_t {
|
||||
int16_t start;
|
||||
int16_t end;
|
||||
} extent_in_bar_t;
|
||||
|
||||
typedef struct point_t {
|
||||
int16_t x, y;
|
||||
} point_t;
|
||||
|
||||
444
src/client.c
444
src/client.c
@@ -10,6 +10,7 @@
|
||||
#include "atoms-extern.h"
|
||||
#include "base.h"
|
||||
#include "monitor.h"
|
||||
#include "rect.h"
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "wm.h"
|
||||
@@ -23,6 +24,19 @@ client_t *client_get_by_window(xcb_window_t window) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
client_t *client_get_next_by_class(client_t *current, const char *class) {
|
||||
client_t *start = current ? current->next : wm.client_list;
|
||||
for (client_t *c = start; c; c = c->next) {
|
||||
if (strcmp(class, c->class) == 0) return c;
|
||||
}
|
||||
|
||||
for (client_t *c = wm.client_list; c && c != start; c = c->next) {
|
||||
if (strcmp(class, c->class) == 0) return c;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void client_attach_list(client_t *client) {
|
||||
client->next = wm.client_list;
|
||||
wm.client_list = client;
|
||||
@@ -77,6 +91,8 @@ task_in_tag_t *client_get_previous_task_in_tag(client_t *client, tag_t *tag) {
|
||||
static void client_add_to_tag(client_t *client, tag_t *tag) {
|
||||
if (client_get_task_in_tag(client, tag)) return;
|
||||
|
||||
xwindow_set_wm_desktop(client->window, tag->index);
|
||||
|
||||
task_in_tag_t *task = p_new(task_in_tag_t, 1);
|
||||
task->client = client;
|
||||
task->next = tag->task_list;
|
||||
@@ -102,9 +118,7 @@ static inline void client_wipe(client_t *client) {
|
||||
p_delete(&client->class);
|
||||
p_delete(&client->instance);
|
||||
p_delete(&client->name);
|
||||
p_delete(&client->icon_name);
|
||||
p_delete(&client->net_name);
|
||||
p_delete(&client->net_icon_name);
|
||||
p_delete(&client->role);
|
||||
p_delete(&client);
|
||||
}
|
||||
@@ -123,12 +137,12 @@ static inline void client_tags_apply(client_t *client) {
|
||||
|
||||
static inline void client_update_names(client_t *c) {
|
||||
xwindow_get_text_property(c->window, WM_NAME, &c->name);
|
||||
xwindow_get_text_property(c->window, WM_ICON_NAME, &c->icon_name);
|
||||
xwindow_get_text_property(c->window, _NET_WM_NAME, &c->net_name);
|
||||
xwindow_get_text_property(c->window, _NET_WM_ICON_NAME, &c->net_icon_name);
|
||||
}
|
||||
|
||||
static inline void client_init_geometry(client_t *c) {
|
||||
if (c->maximize || c->fullscreen || c->minimize) return;
|
||||
|
||||
area_t workarea = c->monitor->workarea;
|
||||
if (c->geometry.x + client_width(c) > workarea.x + workarea.width) {
|
||||
c->geometry.x = workarea.x + workarea.width - client_width(c);
|
||||
@@ -145,27 +159,52 @@ static inline void client_init_geometry(client_t *c) {
|
||||
client_change_border_width(c, wm.border_width);
|
||||
}
|
||||
|
||||
static void client_init_tag_by_wm_desktop(client_t *client, uint32_t tag_mask) {
|
||||
uint32_t tag_index = 0;
|
||||
if (!xwindow_get_wm_desktop(client->window, &tag_index)) {
|
||||
goto tag_fallback;
|
||||
}
|
||||
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
for (tag_t *tag = m->tag_list; tag; tag = tag->next) {
|
||||
if (tag->index == tag_index) {
|
||||
client->monitor = m;
|
||||
client->tags = tag->mask;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tag_fallback:
|
||||
client->tags = tag_mask;
|
||||
}
|
||||
|
||||
static void set_window_event_mask(xcb_window_t window, bool clean) {
|
||||
xcb_cw_t change_mask = XCB_CW_EVENT_MASK;
|
||||
uint32_t init_event_mask =
|
||||
XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
|
||||
XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY |
|
||||
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
|
||||
uint32_t event_mask = clean ? XCB_EVENT_MASK_NO_EVENT : init_event_mask;
|
||||
xcb_params_cw_t params = {.event_mask = event_mask};
|
||||
xcb_aux_change_window_attributes(wm.xcb_conn, window, change_mask, ¶ms);
|
||||
}
|
||||
|
||||
void client_manage(xcb_window_t window,
|
||||
xcb_get_geometry_reply_t *geometry_reply) {
|
||||
client_t *c = p_new(client_t, 1);
|
||||
c->window = window;
|
||||
c->geometry.x = geometry_reply->x;
|
||||
c->geometry.y = geometry_reply->y;
|
||||
c->geometry.width = geometry_reply->width;
|
||||
c->geometry.height = geometry_reply->height;
|
||||
c->border_width = geometry_reply->border_width;
|
||||
c->old_geometry.x = geometry_reply->x;
|
||||
c->old_geometry.y = geometry_reply->y;
|
||||
c->old_geometry.width = geometry_reply->width;
|
||||
c->old_geometry.height = geometry_reply->height;
|
||||
c->geometry = c->old_geometry;
|
||||
c->old_border_width = geometry_reply->border_width;
|
||||
|
||||
c->minimize = xwindow_get_state(window) == XCB_ICCCM_WM_STATE_ICONIC;
|
||||
|
||||
set_window_event_mask(window, false);
|
||||
|
||||
{
|
||||
const xcb_params_cw_t params = {
|
||||
.event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
|
||||
XCB_EVENT_MASK_PROPERTY_CHANGE |
|
||||
XCB_EVENT_MASK_STRUCTURE_NOTIFY |
|
||||
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
|
||||
};
|
||||
xcb_aux_change_window_attributes(wm.xcb_conn, window, XCB_CW_EVENT_MASK,
|
||||
¶ms);
|
||||
xcb_map_window(wm.xcb_conn, window);
|
||||
}
|
||||
client_update_names(c);
|
||||
xwindow_get_text_property(c->window, WM_WINDOW_ROLE, &c->role);
|
||||
client_update_wm_hints(c);
|
||||
@@ -191,10 +230,11 @@ void client_manage(xcb_window_t window,
|
||||
if (c->transient_for_window && (transient_for_client = client_get_by_window(
|
||||
c->transient_for_window))) {
|
||||
c->monitor = transient_for_client->monitor;
|
||||
c->tags = transient_for_client->tags;
|
||||
client_init_tag_by_wm_desktop(c, transient_for_client->tags);
|
||||
} else {
|
||||
c->monitor = wm.current_monitor;
|
||||
c->tags = c->monitor->selected_tag->mask;
|
||||
client_init_tag_by_wm_desktop(c, c->monitor->selected_tag->mask);
|
||||
client_apply_rules(c, wm.rules, wm.rules_count);
|
||||
}
|
||||
|
||||
client_init_geometry(c);
|
||||
@@ -203,6 +243,18 @@ void client_manage(xcb_window_t window,
|
||||
|
||||
client_attach_list(c);
|
||||
client_attach_stack(c);
|
||||
client_update_window_type(c);
|
||||
client_update_size_hints(c);
|
||||
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_APPEND, wm.screen->root,
|
||||
_NET_CLIENT_LIST, XCB_ATOM_WINDOW, 32, 1, &window);
|
||||
|
||||
if (c->minimize) {
|
||||
xwindow_set_state(window, XCB_ICCCM_WM_STATE_ICONIC);
|
||||
} else {
|
||||
xwindow_set_state(window, XCB_ICCCM_WM_STATE_NORMAL);
|
||||
xcb_map_window(wm.xcb_conn, window);
|
||||
}
|
||||
|
||||
monitor_arrange(c->monitor);
|
||||
monitor_draw_bar(c->monitor);
|
||||
@@ -214,21 +266,53 @@ void client_manage(xcb_window_t window,
|
||||
}
|
||||
|
||||
char **client_get_task_title(client_t *client) {
|
||||
if (client->net_icon_name) return &client->net_icon_name;
|
||||
if (client->net_name) return &client->net_name;
|
||||
if (client->icon_name) return &client->icon_name;
|
||||
if (client->name) return &client->name;
|
||||
if (client->instance) return &client->instance;
|
||||
if (client->class) return &client->class;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool client_need_layout(client_t *client) {
|
||||
if (client->floating || client->fullscreen || client->maximize ||
|
||||
client->minimize) {
|
||||
client->minimize || client->size_freeze) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void client_send_to_tag(client_t *client, uint32_t tag_mask) {
|
||||
if (client->tags == tag_mask) return;
|
||||
client->tags = tag_mask;
|
||||
client_tags_apply(client);
|
||||
monitor_arrange(client->monitor);
|
||||
monitor_draw_bar(client->monitor);
|
||||
monitor_deal_focus(client->monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
void client_send_to_monitor(client_t *client, monitor_t *monitor) {
|
||||
if (client->monitor == monitor) return;
|
||||
|
||||
for (tag_t *tag = client->monitor->tag_list; tag; tag = tag->next) {
|
||||
client_remove_from_tag(client, tag);
|
||||
}
|
||||
|
||||
monitor_t *m = client->monitor;
|
||||
|
||||
client->monitor = monitor;
|
||||
client->tags = monitor->selected_tag->mask;
|
||||
client_add_to_tag(client, monitor->selected_tag);
|
||||
|
||||
wm_set_current_monitor(monitor, true);
|
||||
monitor_arrange(m);
|
||||
monitor_arrange(monitor);
|
||||
monitor_draw_bar(m);
|
||||
monitor_draw_bar(monitor);
|
||||
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
void client_move_to(client_t *client, int16_t x, int16_t y) {
|
||||
client->geometry.x = x;
|
||||
client->geometry.y = y;
|
||||
@@ -269,6 +353,36 @@ void client_change_border_width(client_t *client, uint16_t border_width) {
|
||||
logger("== client 0x%x border width: %u\n", client->window, border_width);
|
||||
}
|
||||
|
||||
void client_update_window_type(client_t *client) {
|
||||
xcb_window_t window = client->window;
|
||||
xcb_get_property_cookie_t state_cookie = xcb_get_property_unchecked(
|
||||
wm.xcb_conn, false, window, _NET_WM_STATE, XCB_ATOM_ATOM, 0, 1);
|
||||
xcb_get_property_cookie_t window_type_cookie = xcb_get_property_unchecked(
|
||||
wm.xcb_conn, false, window, _NET_WM_WINDOW_TYPE, XCB_ATOM_ATOM, 0, 1);
|
||||
xcb_get_property_reply_t *state_reply =
|
||||
xcb_get_property_reply(wm.xcb_conn, state_cookie, nullptr);
|
||||
xcb_get_property_reply_t *window_type_reply =
|
||||
xcb_get_property_reply(wm.xcb_conn, window_type_cookie, nullptr);
|
||||
|
||||
if (state_reply) {
|
||||
xcb_atom_t state = *(xcb_atom_t *)xcb_get_property_value(state_reply);
|
||||
client->skip_taskbar = state == _NET_WM_STATE_SKIP_TASKBAR;
|
||||
if (state == _NET_WM_STATE_FULLSCREEN) {
|
||||
client_set_fullscreen(client, true);
|
||||
}
|
||||
p_delete(&state_reply);
|
||||
}
|
||||
|
||||
if (window_type_reply) {
|
||||
xcb_atom_t window_type =
|
||||
*(xcb_atom_t *)xcb_get_property_value(window_type_reply);
|
||||
if (window_type == _NET_WM_WINDOW_TYPE_DIALOG) {
|
||||
client_set_floating(client, true);
|
||||
}
|
||||
p_delete(&window_type_reply);
|
||||
}
|
||||
}
|
||||
|
||||
void client_update_wm_hints(client_t *client) {
|
||||
xcb_get_property_cookie_t cookie =
|
||||
xcb_icccm_get_wm_hints(wm.xcb_conn, client->window);
|
||||
@@ -284,7 +398,136 @@ void client_update_wm_hints(client_t *client) {
|
||||
}
|
||||
}
|
||||
|
||||
void client_unmanage(client_t *client) {
|
||||
void client_update_size_hints(client_t *client) {
|
||||
xcb_connection_t *conn = wm.xcb_conn;
|
||||
xcb_size_hints_t hints;
|
||||
xcb_get_property_cookie_t cookie =
|
||||
xcb_icccm_get_wm_normal_hints_unchecked(conn, client->window);
|
||||
if (!xcb_icccm_get_wm_normal_hints_reply(conn, cookie, &hints, nullptr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t min_width = 0, min_height = 0, max_width = 0, max_height = 0;
|
||||
if (hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
|
||||
min_width = hints.min_width;
|
||||
min_height = hints.min_height;
|
||||
}
|
||||
if (hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE) {
|
||||
max_width = hints.max_width;
|
||||
max_height = hints.max_height;
|
||||
}
|
||||
if (min_width == max_width && min_height == max_height) {
|
||||
client->size_freeze = true;
|
||||
client_set_floating(client, true);
|
||||
}
|
||||
}
|
||||
|
||||
void client_set_floating(client_t *client, bool floating) {
|
||||
if (client->floating == floating || client->fullscreen) return;
|
||||
|
||||
logger("== client set floating: %s\n", floating ? "true" : "false");
|
||||
logger("== old geometry: x -> %d, y -> %d, width -> %u, height -> %u\n",
|
||||
client->old_geometry.x, client->old_geometry.y,
|
||||
client->old_geometry.width, client->old_geometry.height);
|
||||
client->floating = floating;
|
||||
if (floating) {
|
||||
client_apply_workarea_geometry(client, client->old_geometry);
|
||||
client->old_geometry = client->geometry;
|
||||
}
|
||||
|
||||
monitor_arrange(client->monitor);
|
||||
}
|
||||
|
||||
void client_set_fullscreen(client_t *client, bool fullscreen) {
|
||||
if (client->fullscreen == fullscreen) return;
|
||||
|
||||
client->fullscreen = fullscreen;
|
||||
if (fullscreen) {
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, client->window,
|
||||
_NET_WM_STATE, XCB_ATOM_ATOM, 32, 1,
|
||||
&_NET_WM_STATE_FULLSCREEN);
|
||||
client->old_border_width = client->border_width;
|
||||
if (client->floating) client->old_geometry = client->geometry;
|
||||
client_change_border_width(client, 0);
|
||||
client_apply_geometry(client, client->monitor->geometry);
|
||||
client_stack_raise(client);
|
||||
} else {
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, client->window,
|
||||
_NET_WM_STATE, XCB_ATOM_ATOM, 32, 0, 0);
|
||||
client_change_border_width(client, client->old_border_width);
|
||||
client_apply_geometry(client, client->old_geometry);
|
||||
}
|
||||
monitor_arrange(client->monitor);
|
||||
client_focus(client);
|
||||
}
|
||||
|
||||
void client_set_maximize(client_t *client, bool maximize) {
|
||||
if (client->maximize == maximize) return;
|
||||
|
||||
client->maximize = maximize;
|
||||
if (maximize) {
|
||||
xcb_atom_t max_atoms[] = {
|
||||
_NET_WM_STATE_MAXIMIZED_HORZ,
|
||||
_NET_WM_STATE_MAXIMIZED_VERT,
|
||||
};
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, client->window,
|
||||
_NET_WM_STATE, XCB_ATOM_ATOM, 32, countof(max_atoms),
|
||||
max_atoms);
|
||||
client->old_border_width = client->border_width;
|
||||
if (client->floating) client->old_geometry = client->geometry;
|
||||
client_change_border_width(client, 0);
|
||||
client_apply_geometry(client, client->monitor->workarea);
|
||||
client_stack_raise(client);
|
||||
} else {
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, client->window,
|
||||
_NET_WM_STATE, XCB_ATOM_ATOM, 32, 0, 0);
|
||||
client_change_border_width(client, client->old_border_width);
|
||||
client_apply_geometry(client, client->old_geometry);
|
||||
}
|
||||
monitor_arrange(client->monitor);
|
||||
client_focus(client);
|
||||
}
|
||||
|
||||
void client_set_minimize(client_t *client, bool minimize) {
|
||||
if (client->minimize == minimize) return;
|
||||
|
||||
client->minimize = minimize;
|
||||
if (minimize) {
|
||||
xwindow_set_state(client->window, XCB_ICCCM_WM_STATE_ICONIC);
|
||||
|
||||
xcb_grab_server(wm.xcb_conn);
|
||||
set_window_event_mask(client->window, true);
|
||||
xcb_unmap_window(wm.xcb_conn, client->window);
|
||||
set_window_event_mask(client->window, false);
|
||||
xcb_ungrab_server(wm.xcb_conn);
|
||||
} else {
|
||||
xwindow_set_state(client->window, XCB_ICCCM_WM_STATE_NORMAL);
|
||||
xcb_map_window(wm.xcb_conn, client->window);
|
||||
}
|
||||
monitor_arrange(client->monitor);
|
||||
client_focus(client);
|
||||
}
|
||||
|
||||
static void update_client_list(void) {
|
||||
xcb_connection_t *conn = wm.xcb_conn;
|
||||
xcb_window_t root = wm.screen->root;
|
||||
xcb_atom_t atom = _NET_CLIENT_LIST;
|
||||
uint8_t mode = XCB_PROP_MODE_PREPEND;
|
||||
xcb_atom_t type = XCB_ATOM_WINDOW;
|
||||
|
||||
xcb_delete_property(conn, root, atom);
|
||||
for (client_t *c = wm.client_list; c; c = c->next) {
|
||||
xcb_change_property(conn, mode, root, atom, type, 32, 1, &c->window);
|
||||
}
|
||||
}
|
||||
|
||||
void client_kill(client_t *client) {
|
||||
if (!xwindow_send_event(client->window, WM_DELETE_WINDOW)) {
|
||||
xwindow_kill_window(client->window);
|
||||
}
|
||||
}
|
||||
|
||||
void client_unmanage(client_t *client, bool destroyed) {
|
||||
monitor_t *m = client->monitor;
|
||||
|
||||
client_detach_list(client);
|
||||
@@ -294,8 +537,17 @@ void client_unmanage(client_t *client) {
|
||||
if (tag->mask & client->tags) client_remove_from_tag(client, tag);
|
||||
}
|
||||
|
||||
if (!destroyed) {
|
||||
set_window_event_mask(client->window, true);
|
||||
client_change_border_width(client, client->old_border_width);
|
||||
xwindow_set_state(client->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
client_wipe(client);
|
||||
|
||||
update_client_list();
|
||||
|
||||
wm_restack_clients();
|
||||
monitor_deal_focus(m);
|
||||
monitor_arrange(m);
|
||||
@@ -303,16 +555,89 @@ void client_unmanage(client_t *client) {
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
void client_apply_task_geometry(client_t *client, task_in_tag_t *task) {
|
||||
if (!client || !task || client != task->client) return;
|
||||
|
||||
if (task->geometry.x != client->geometry.x ||
|
||||
task->geometry.y != client->geometry.y) {
|
||||
client_move_to(client, task->geometry.x, task->geometry.y);
|
||||
void client_apply_geometry(client_t *client, area_t geometry) {
|
||||
uint16_t mask = 0;
|
||||
const xcb_params_configure_window_t params = {
|
||||
.x = geometry.x,
|
||||
.y = geometry.y,
|
||||
.width = geometry.width,
|
||||
.height = geometry.height,
|
||||
};
|
||||
if (client->geometry.x != geometry.x) mask |= XCB_CONFIG_WINDOW_X;
|
||||
if (client->geometry.y != geometry.y) mask |= XCB_CONFIG_WINDOW_Y;
|
||||
if (client->geometry.width != geometry.width) mask |= XCB_CONFIG_WINDOW_WIDTH;
|
||||
if (client->geometry.height != geometry.height) {
|
||||
mask |= XCB_CONFIG_WINDOW_HEIGHT;
|
||||
}
|
||||
if (task->geometry.width != client->geometry.width ||
|
||||
task->geometry.height != client->geometry.height) {
|
||||
client_resize(client, task->geometry.width, task->geometry.height);
|
||||
|
||||
client->geometry = geometry;
|
||||
xcb_aux_configure_window(wm.xcb_conn, client->window, mask, ¶ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 调整 client 的几何信息但需让 client 任在其 monitor 的 workarea 中
|
||||
* @param client 要调整几何信息的 client
|
||||
* @param geometry 目标几何信息
|
||||
*/
|
||||
void client_apply_workarea_geometry(client_t *client, area_t geometry) {
|
||||
area_t workarea = client->monitor->workarea;
|
||||
uint16_t width = MIN(geometry.width, workarea.width);
|
||||
uint16_t height = MIN(geometry.height, workarea.height);
|
||||
int16_t x = MAX(geometry.x, workarea.x);
|
||||
int16_t y = MAX(geometry.y, workarea.y);
|
||||
if (x + width + client->border_width * 2 > workarea.x + workarea.width) {
|
||||
x = workarea.x + workarea.width - width - client->border_width * 2;
|
||||
}
|
||||
if (y + height + client->border_width * 2 > workarea.y + workarea.height) {
|
||||
y = workarea.y + workarea.height - height - client->border_width * 2;
|
||||
}
|
||||
|
||||
area_t rect = {.x = x, .y = y, .width = width, .height = height};
|
||||
client_apply_geometry(client, rect);
|
||||
}
|
||||
|
||||
void client_apply_rules(client_t *client, const rule_t rules[],
|
||||
size_t rules_count) {
|
||||
for (size_t i = 0; i < rules_count; i++) {
|
||||
const rule_t *r = &rules[i];
|
||||
if (!((!r->role || (client->role && strstr(client->role, r->role))) &&
|
||||
(!r->class || (client->class && strstr(client->class, r->class))))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
client->fullscreen = r->fullscreen;
|
||||
client->maximize = r->maximize;
|
||||
client->floating = r->floating;
|
||||
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
for (tag_t *t = m->tag_list; t; t = t->next) {
|
||||
if (t->index != r->tag_index) continue;
|
||||
|
||||
client->monitor = m;
|
||||
client->tags = t->mask;
|
||||
|
||||
if (r->switch_to_tag) {
|
||||
wm_set_current_monitor(m, true);
|
||||
m->selected_tag = t;
|
||||
|
||||
logger("++ switch to tag: %u\n", t->index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (client->fullscreen) {
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, client->window,
|
||||
_NET_WM_STATE, XCB_ATOM_ATOM, 32, 1,
|
||||
&_NET_WM_STATE_FULLSCREEN);
|
||||
} else if (client->maximize) {
|
||||
xcb_atom_t max_atoms[] = {
|
||||
_NET_WM_STATE_MAXIMIZED_HORZ,
|
||||
_NET_WM_STATE_MAXIMIZED_VERT,
|
||||
};
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, client->window,
|
||||
_NET_WM_STATE, XCB_ATOM_ATOM, 32, countof(max_atoms),
|
||||
max_atoms);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,6 +647,8 @@ void client_focus(client_t *client) {
|
||||
}
|
||||
|
||||
void client_stack_raise(client_t *client) {
|
||||
if (wm.client_stack_list == client) return;
|
||||
|
||||
client_detach_stack(client);
|
||||
client_attach_stack(client);
|
||||
wm_restack_clients();
|
||||
@@ -330,3 +657,50 @@ void client_stack_raise(client_t *client) {
|
||||
bool client_is_visible(client_t *client) {
|
||||
return client->tags & client->monitor->selected_tag->mask;
|
||||
}
|
||||
|
||||
static inline rect_t client_to_rect(client_t *client) {
|
||||
return (rect_t){
|
||||
.x1 = client->geometry.x,
|
||||
.y1 = client->geometry.y,
|
||||
.x2 = client->geometry.x + (int32_t)client_width(client),
|
||||
.y2 = client->geometry.y + (int32_t)client_height(client),
|
||||
};
|
||||
}
|
||||
|
||||
obscured_t client_get_obscured_state(client_t *client, client_t *client_stack) {
|
||||
if (!client || !client_stack) return obscured_none;
|
||||
|
||||
rect_t source = client_to_rect(client);
|
||||
size_t clip_count = 0;
|
||||
size_t clip_capacity = 0;
|
||||
rect_t *clips = nullptr;
|
||||
|
||||
bool has_overlap = false;
|
||||
for (client_t *c = client_stack; c; c = c->stack_next) {
|
||||
if (c == client) break;
|
||||
if (c->minimize || !client_is_visible(c)) continue;
|
||||
|
||||
rect_t clip = client_to_rect(c);
|
||||
if (!rect_intersection(source, clip, nullptr)) continue;
|
||||
|
||||
has_overlap = true;
|
||||
if (clip_count == clip_capacity) {
|
||||
clip_capacity = clip_capacity ? clip_capacity * 2 : 4;
|
||||
p_realloc(&clips, clip_capacity);
|
||||
}
|
||||
clips[clip_count++] = clip;
|
||||
}
|
||||
|
||||
if (!has_overlap) {
|
||||
p_delete(&clips);
|
||||
return obscured_none;
|
||||
}
|
||||
|
||||
rect_t *remaining = nullptr;
|
||||
size_t remaining_count =
|
||||
rect_subtract_many(source, clips, clip_count, &remaining);
|
||||
|
||||
p_delete(&clips);
|
||||
p_delete(&remaining);
|
||||
return remaining_count == 0 ? obscured_fully : obscured_partially;
|
||||
}
|
||||
|
||||
18
src/client.h
18
src/client.h
@@ -1,11 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "color.h"
|
||||
#include "types.h"
|
||||
|
||||
client_t *client_get_by_window(xcb_window_t window);
|
||||
client_t *client_get_next_by_class(client_t *current, const char *class);
|
||||
task_in_tag_t *client_get_task_in_tag(client_t *client, tag_t *tag);
|
||||
task_in_tag_t *client_get_next_task_in_tag(client_t *client, tag_t *tag);
|
||||
task_in_tag_t *client_get_previous_task_in_tag(client_t *client, tag_t *tag);
|
||||
@@ -22,7 +24,9 @@ void client_resize(client_t *client, uint16_t width, uint16_t height);
|
||||
void client_change_border_color(client_t *client, color_t *color);
|
||||
void client_change_border_width(client_t *client, uint16_t border_width);
|
||||
|
||||
void client_update_window_type(client_t *client);
|
||||
void client_update_wm_hints(client_t *client);
|
||||
void client_update_size_hints(client_t *client);
|
||||
|
||||
void client_set_floating(client_t *client, bool floating);
|
||||
void client_set_fullscreen(client_t *client, bool fullscreen);
|
||||
@@ -46,14 +50,24 @@ void client_set_transient_for_window(client_t *client,
|
||||
void client_set_leader_window(client_t *client, xcb_window_t leader_window);
|
||||
|
||||
void client_kill(client_t *client);
|
||||
void client_unmanage(client_t *client);
|
||||
void client_apply_task_geometry(client_t *client, task_in_tag_t *task);
|
||||
void client_unmanage(client_t *client, bool destroyed);
|
||||
void client_apply_geometry(client_t *client, area_t geometry);
|
||||
void client_apply_workarea_geometry(client_t *client, area_t geometry);
|
||||
void client_apply_rules(client_t *client, const rule_t rules[],
|
||||
size_t rules_count);
|
||||
|
||||
void client_focus(client_t *client);
|
||||
void client_stack_raise(client_t *client);
|
||||
|
||||
bool client_is_visible(client_t *client);
|
||||
|
||||
typedef enum obscured_t {
|
||||
obscured_none,
|
||||
obscured_partially,
|
||||
obscured_fully,
|
||||
} obscured_t;
|
||||
obscured_t client_get_obscured_state(client_t *client, client_t *client_stack);
|
||||
|
||||
static inline uint16_t client_width(client_t *c) {
|
||||
return c->geometry.width + c->border_width * 2;
|
||||
}
|
||||
|
||||
81
src/config.c
Normal file
81
src/config.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "default_config.h"
|
||||
#include "utils.h"
|
||||
|
||||
void config_status_set_gap(config_status_t *status_config, uint32_t gap,
|
||||
uint32_t item_gap) {
|
||||
if (!status_config) fatal("no valid status config");
|
||||
status_config->status_gap = gap;
|
||||
status_config->status_item_gap = item_gap;
|
||||
}
|
||||
|
||||
void config_status_add_item(config_status_t **status_config,
|
||||
config_status_item_t status_item) {
|
||||
const auto item_count = (*status_config)->status_count + 1;
|
||||
const auto new_size =
|
||||
sizeof(config_status_t) + item_count * sizeof(config_status_item_t);
|
||||
xrealloc((void **)status_config, (ssize_t)new_size);
|
||||
(*status_config)->list[item_count - 1] = status_item;
|
||||
(*status_config)->status_count = item_count;
|
||||
}
|
||||
|
||||
void config_status_filter_item(config_status_t **status_config,
|
||||
status_item_filter filter) {
|
||||
if (!status_config || !*status_config) fatal("no valid status config");
|
||||
if (!filter) return;
|
||||
|
||||
config_status_t *config = *status_config;
|
||||
size_t write_idx = 0;
|
||||
size_t item_count = config->status_count;
|
||||
|
||||
for (size_t read_idx = 0; read_idx < item_count; read_idx++) {
|
||||
config_status_item_t *item = &config->list[read_idx];
|
||||
if (!filter(item)) continue;
|
||||
if (write_idx != read_idx) {
|
||||
config->list[write_idx] = *item;
|
||||
}
|
||||
write_idx++;
|
||||
}
|
||||
|
||||
if (write_idx == item_count) return;
|
||||
|
||||
config->status_count = write_idx;
|
||||
size_t new_size =
|
||||
sizeof(config_status_t) + write_idx * sizeof(config_status_item_t);
|
||||
xrealloc((void **)status_config, (ssize_t)new_size);
|
||||
}
|
||||
|
||||
static config_status_t *config_create_default_status(void) {
|
||||
size_t item_count = countof(status_list);
|
||||
size_t size = sizeof(config_status_t) + item_count * sizeof(status_list[0]);
|
||||
config_status_t *status = xmalloc((ssize_t)size);
|
||||
|
||||
status->status_gap = status_config.status_gap;
|
||||
status->status_item_gap = status_config.status_item_gap;
|
||||
status->status_count = item_count;
|
||||
memcpy(status->list, status_list, item_count * sizeof(status_list[0]));
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static config_t runtime_config = {};
|
||||
|
||||
const config_t *init_config(void) {
|
||||
if (!runtime_config.status) {
|
||||
runtime_config.status = config_create_default_status();
|
||||
}
|
||||
if (!runtime_config.status_renderer) {
|
||||
runtime_config.status_renderer = renderer_render_status;
|
||||
}
|
||||
return &runtime_config;
|
||||
}
|
||||
|
||||
void config_set_custom_status_renderer(config_t *config,
|
||||
status_renderer_t status_renderer) {
|
||||
if (!config) fatal("no valid config");
|
||||
config->status_renderer =
|
||||
status_renderer ? status_renderer : renderer_render_status;
|
||||
}
|
||||
57
src/config.h
Normal file
57
src/config.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "renderer.h"
|
||||
#include "status.h"
|
||||
|
||||
typedef struct config_status_item_t {
|
||||
status_type_t type;
|
||||
icon_type_t icon_type;
|
||||
const char *icon;
|
||||
const char *color;
|
||||
} config_status_item_t;
|
||||
|
||||
typedef struct config_status_t {
|
||||
uint32_t status_gap;
|
||||
uint32_t status_item_gap;
|
||||
size_t status_count;
|
||||
config_status_item_t list[];
|
||||
} config_status_t;
|
||||
|
||||
void config_status_set_gap(config_status_t *status_config, uint32_t gap,
|
||||
uint32_t item_gap);
|
||||
|
||||
void config_status_add_item(config_status_t **status_config,
|
||||
config_status_item_t status_item);
|
||||
|
||||
typedef bool (*status_item_filter)(config_status_item_t *item);
|
||||
/**
|
||||
* @brief 按过滤条件原地筛选 status 项并保持原有顺序
|
||||
*
|
||||
* 当 filter(item) 返回 true 时保留该项,返回 false 时删除该项。
|
||||
* 筛选后会压缩 list、更新 status_count,并收缩配置对象内存。
|
||||
*
|
||||
* @param status_config 配置指针地址(函数内部会重分配内存)
|
||||
* @param filter 过滤函数
|
||||
*/
|
||||
void config_status_filter_item(config_status_t **status_config,
|
||||
status_item_filter filter);
|
||||
|
||||
typedef void (*status_renderer_t)(config_status_t *config, size_t config_count,
|
||||
status_t *status, renderer_status_t *output);
|
||||
|
||||
typedef struct config_t {
|
||||
config_status_t *status;
|
||||
status_renderer_t status_renderer;
|
||||
} config_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化配置对象
|
||||
* @return 返回静态变量指针,不需要释放
|
||||
*/
|
||||
const config_t *init_config(void);
|
||||
|
||||
void config_set_custom_status_renderer(config_t *config,
|
||||
status_renderer_t status_renderer);
|
||||
@@ -1,10 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <X11/XF86keysym.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "action.h"
|
||||
#include "config.h"
|
||||
#include "layout.h"
|
||||
#include "renderer.h"
|
||||
#include "status.h"
|
||||
#include "types.h"
|
||||
|
||||
static const char *const font_family = "monospace";
|
||||
@@ -26,21 +30,176 @@ static const char *const active_tag_color = "#eeeeee";
|
||||
static const char *const border_color = "#444444";
|
||||
static const char *const active_border_color = "#005577";
|
||||
|
||||
static const config_status_item_t status_list[] = {
|
||||
{
|
||||
.type = status_net_down,
|
||||
.icon_type = icon_type_image,
|
||||
.icon = "/home/zedhugh/develop/zswm/src/resources/icons/"
|
||||
"corner-left-down-line.png",
|
||||
.color = "#87af5f",
|
||||
},
|
||||
{
|
||||
.type = status_net_up,
|
||||
.icon_type = icon_type_image,
|
||||
.icon =
|
||||
"/home/zedhugh/develop/zswm/src/resources/icons/corner-right-up-line.png",
|
||||
.color = "#e54c62",
|
||||
},
|
||||
{
|
||||
.type = status_audio,
|
||||
.icon_type = icon_type_image,
|
||||
.icon = "/home/zedhugh/develop/zswm/src/resources/icons/volume-up-fill.png",
|
||||
.color = "#7493d2",
|
||||
},
|
||||
{
|
||||
.type = status_memory,
|
||||
.icon_type = icon_type_image,
|
||||
.icon = "/home/zedhugh/develop/zswm/src/resources/icons/ram-line.png",
|
||||
.color = "#e0da37",
|
||||
},
|
||||
{
|
||||
.type = status_cpu,
|
||||
.icon_type = icon_type_image,
|
||||
.icon = "/home/zedhugh/develop/zswm/src/resources/icons/cpu-line.png",
|
||||
.color = "#e33a6e",
|
||||
},
|
||||
{
|
||||
.type = status_time,
|
||||
.icon_type = icon_type_image,
|
||||
.icon = "/home/zedhugh/develop/zswm/src/resources/icons/time-line.png",
|
||||
.color = "#7788af",
|
||||
},
|
||||
};
|
||||
static const config_status_t status_config = {
|
||||
.status_gap = 10,
|
||||
.status_item_gap = 5,
|
||||
};
|
||||
|
||||
static const config_t config = {
|
||||
.status = (config_status_t *)&status_config,
|
||||
};
|
||||
|
||||
static const button_t button_list[] = {
|
||||
{click_tag, modifier_none, button_left, select_tag_of_current_monitor, {0}},
|
||||
{
|
||||
click_client_name,
|
||||
modifier_none,
|
||||
button_left,
|
||||
change_client_state_dwim,
|
||||
{0},
|
||||
},
|
||||
};
|
||||
|
||||
#define TAGKEYS(KEY, TAG) \
|
||||
{modifier_super, KEY, select_tag_of_current_monitor, {.ui = TAG}}, { \
|
||||
modifier_super | modifier_shift, KEY, send_client_to_tag, {.ui = TAG}, \
|
||||
}
|
||||
|
||||
static const char launcher[] =
|
||||
"rofi -show combi -modes combi -combi-modes window,drun,run,ssh,windowcd";
|
||||
static const char terminal[] = "xterm";
|
||||
static const char terminal_class[] = "XTerm";
|
||||
static const char editor[] = "emacsclient -a '' -r -n";
|
||||
static const char editor_class[] = "Emacs";
|
||||
static const char browser[] = "firefox-bin";
|
||||
static const char browser_class[] = "firefox";
|
||||
static const char chrome[] = "google-chrome-stable";
|
||||
static const char chrome_class[] = "Google-chrome";
|
||||
static const char mpv[] = "mpv";
|
||||
static const char mpv_class[] = "mpv";
|
||||
static const keyboard_t key_list[] = {
|
||||
{modifier_alt, XK_p, spawn, {.ptr = launcher}},
|
||||
{modifier_alt, XK_q, quit, {.b = false}},
|
||||
{modifier_alt, XK_r, quit, {.b = true}},
|
||||
{modifier_alt | modifier_shift, XK_j, focus_client_in_same_tag, {.b = true}},
|
||||
{modifier_alt | modifier_shift, XK_k, focus_client_in_same_tag, {.b = false}},
|
||||
{modifier_super, XK_r, spawn, {.ptr = launcher}},
|
||||
{modifier_super, XK_Return, spawn, {.ptr = terminal}},
|
||||
{
|
||||
modifier_control | modifier_alt,
|
||||
XK_r,
|
||||
raise_or_run,
|
||||
{.ptr = (const char *[]){terminal_class, terminal}},
|
||||
},
|
||||
{
|
||||
modifier_super,
|
||||
XK_e,
|
||||
raise_or_run,
|
||||
{.ptr = (const char *[]){editor_class, editor}},
|
||||
},
|
||||
{
|
||||
modifier_super,
|
||||
XK_q,
|
||||
raise_or_run,
|
||||
{.ptr = (const char *[]){browser_class, browser}},
|
||||
},
|
||||
{
|
||||
modifier_super,
|
||||
XK_a,
|
||||
raise_or_run,
|
||||
{.ptr = (const char *[]){chrome_class, chrome}},
|
||||
},
|
||||
{
|
||||
modifier_super,
|
||||
XK_v,
|
||||
raise_or_run,
|
||||
{.ptr = (const char *[]){mpv_class, mpv}},
|
||||
},
|
||||
{modifier_super | modifier_shift, XK_q, quit, {.b = false}},
|
||||
{modifier_super | modifier_control, XK_r, quit, {.b = true}},
|
||||
{modifier_super | modifier_shift,
|
||||
XK_j,
|
||||
focus_client_in_same_tag,
|
||||
{.b = true}},
|
||||
{modifier_super | modifier_shift,
|
||||
XK_k,
|
||||
focus_client_in_same_tag,
|
||||
{.b = false}},
|
||||
|
||||
TAGKEYS(XK_1, 1 << 0),
|
||||
TAGKEYS(XK_2, 1 << 1),
|
||||
TAGKEYS(XK_3, 1 << 2),
|
||||
TAGKEYS(XK_4, 1 << 3),
|
||||
TAGKEYS(XK_5, 1 << 4),
|
||||
TAGKEYS(XK_6, 1 << 5),
|
||||
TAGKEYS(XK_7, 1 << 6),
|
||||
TAGKEYS(XK_8, 1 << 7),
|
||||
TAGKEYS(XK_9, 1 << 8),
|
||||
|
||||
{modifier_super, XK_o, send_client_to_next_monitor, {0}},
|
||||
|
||||
{modifier_super | modifier_control, XK_j, focus_next_monitor, {0}},
|
||||
{modifier_super | modifier_control, XK_space, toggle_client_floating, {0}},
|
||||
{modifier_super, XK_f, toggle_client_fullscreen, {0}},
|
||||
{modifier_super, XK_m, toggle_client_maximize, {0}},
|
||||
{modifier_super | modifier_shift, XK_c, kill_client, {0}},
|
||||
|
||||
{modifier_super, XK_Up, change_volume, {.i = 1}},
|
||||
{modifier_super, XK_Down, change_volume, {.i = -1}},
|
||||
{modifier_super | modifier_shift, XK_m, toggle_mute, {0}},
|
||||
{modifier_none, XF86XK_AudioRaiseVolume, change_volume, {.i = 1}},
|
||||
{modifier_none, XF86XK_AudioLowerVolume, change_volume, {.i = -1}},
|
||||
{modifier_none, XF86XK_AudioMute, toggle_mute, {0}},
|
||||
|
||||
};
|
||||
|
||||
static const layout_t layout_list[] = {
|
||||
{.symbol = "[]=", .arrange = tile},
|
||||
{.symbol = "[M]", .arrange = monocle},
|
||||
{.symbol = "><=", .arrange = nullptr},
|
||||
};
|
||||
|
||||
static const char *const autostart_list[] = {
|
||||
"gentoo-pipewire-launcher restart",
|
||||
"picom -b --backend xrender",
|
||||
"fcitx5 -d",
|
||||
nullptr,
|
||||
};
|
||||
|
||||
static const rule_t rules[] = {
|
||||
{.role = "dialog", .tag_index = -1, .floating = true},
|
||||
{.class = "firefox", .role = "About", .tag_index = 1, .floating = true},
|
||||
{.class = "firefox", .role = "browser", .tag_index = 1, .maximize = true},
|
||||
{.class = "Google-chrome", .tag_index = 1, .maximize = true},
|
||||
{.class = "mpv", .tag_index = 11, .switch_to_tag = true, .fullscreen = true},
|
||||
{.class = "Emacs", .tag_index = 10, .switch_to_tag = true, .maximize = true},
|
||||
};
|
||||
|
||||
static const char *const wallpapers[] = {"~/bg/*", "~/Downloads/bg/*"};
|
||||
/* static const char *const wallpapers[] = {}; */
|
||||
static const uint32_t wallpaper_interval = 0;
|
||||
|
||||
225
src/event.c
225
src/event.c
@@ -2,18 +2,20 @@
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xcb.h>
|
||||
#include <xcb/xcb_event.h>
|
||||
#include <xcb/xcb_icccm.h>
|
||||
#include <xcb/xcb_keysyms.h>
|
||||
#include <xcb/xproto.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
|
||||
#include "action.h"
|
||||
#include "atoms-extern.h"
|
||||
#include "base.h"
|
||||
#include "client.h"
|
||||
#include "default_config.h"
|
||||
#include "monitor.h"
|
||||
#include "tray.h"
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "wm.h"
|
||||
@@ -24,26 +26,44 @@ static void button_press(xcb_button_press_event_t *ev) {
|
||||
click_area_t click_area = click_none;
|
||||
user_action_arg_t arg = {0};
|
||||
|
||||
monitor_t *monitor = nullptr;
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
if (ev->event == m->bar_window) {
|
||||
if (wm.current_monitor != m) wm.current_monitor = m;
|
||||
|
||||
if (ev->event_x >= m->tag_extent.start &&
|
||||
ev->event_x <= m->tag_extent.end) {
|
||||
click_area = click_tag;
|
||||
|
||||
for (tag_t *tag = m->tag_list; tag; tag = tag->next) {
|
||||
if (ev->event_x >= tag->bar_extent.start &&
|
||||
ev->event_x <= tag->bar_extent.end) {
|
||||
arg.ui = tag->mask;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
monitor = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!monitor) return;
|
||||
|
||||
wm_set_current_monitor(monitor, false);
|
||||
if (ev->event_x >= monitor->tag_extent.start &&
|
||||
ev->event_x <= monitor->tag_extent.end) {
|
||||
click_area = click_tag;
|
||||
|
||||
for (tag_t *tag = monitor->tag_list; tag; tag = tag->next) {
|
||||
if (ev->event_x >= tag->bar_extent.start &&
|
||||
ev->event_x <= tag->bar_extent.end) {
|
||||
arg.ui = tag->mask;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (click_area == click_none) {
|
||||
for (task_in_tag_t *task = monitor->selected_tag->task_list; task;
|
||||
task = task->next) {
|
||||
if (ev->event_x >= task->bar_extent.start &&
|
||||
ev->event_x <= task->bar_extent.end) {
|
||||
click_area = click_client_name;
|
||||
arg.ptr = task->client;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (click_area == click_none) return;
|
||||
|
||||
for (int i = 0; i < countof(button_list); i++) {
|
||||
if (click_area == button_list[i].click_area &&
|
||||
button_list[i].button == ev->detail &&
|
||||
@@ -54,6 +74,8 @@ static void button_press(xcb_button_press_event_t *ev) {
|
||||
}
|
||||
|
||||
static void configure_request(xcb_configure_request_event_t *ev) {
|
||||
if (tray_handle_configure_request(ev)) return;
|
||||
|
||||
client_t *client = client_get_by_window(ev->window);
|
||||
if (client) {
|
||||
if (ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
|
||||
@@ -69,7 +91,7 @@ static void configure_request(xcb_configure_request_event_t *ev) {
|
||||
task->geometry.width = ev->width;
|
||||
if (ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
|
||||
task->geometry.height = ev->height;
|
||||
client_apply_task_geometry(client, task);
|
||||
client_apply_geometry(client, task->geometry);
|
||||
}
|
||||
} else {
|
||||
uint16_t value_mask = ev->value_mask;
|
||||
@@ -83,6 +105,7 @@ static void configure_request(xcb_configure_request_event_t *ev) {
|
||||
.stack_mode = ev->stack_mode,
|
||||
};
|
||||
xcb_configure_window_aux(wm.xcb_conn, ev->window, value_mask, &value_list);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +122,11 @@ static void key_press(xcb_key_press_event_t *ev) {
|
||||
for (int i = 0; i < countof(key_list); i++) {
|
||||
keyboard_t key = key_list[i];
|
||||
if (keysym == key.keysym && ev->state == key.modifiers && key.func) {
|
||||
/* 查询光标所在屏幕并将其设置为当前屏幕 */
|
||||
point_t point = {.x = ev->root_x, .y = ev->root_y};
|
||||
monitor_t *monitor = wm_get_monitor_by_point(point);
|
||||
wm_set_current_monitor(monitor, false);
|
||||
|
||||
key.func(&key.arg);
|
||||
return;
|
||||
}
|
||||
@@ -106,10 +134,10 @@ static void key_press(xcb_key_press_event_t *ev) {
|
||||
}
|
||||
|
||||
static void map_request(xcb_map_request_event_t *ev) {
|
||||
xcb_get_window_attributes_cookie_t wa_cookie =
|
||||
xcb_get_window_attributes_unchecked(wm.xcb_conn, ev->window);
|
||||
if (tray_handle_map_request(ev)) return;
|
||||
|
||||
xcb_get_window_attributes_reply_t *wa_reply =
|
||||
xcb_get_window_attributes_reply(wm.xcb_conn, wa_cookie, nullptr);
|
||||
xwindow_get_attributes_reply(ev->window);
|
||||
|
||||
if (!wa_reply) return;
|
||||
if (wa_reply->override_redirect) {
|
||||
@@ -119,10 +147,8 @@ static void map_request(xcb_map_request_event_t *ev) {
|
||||
|
||||
client_t *c = client_get_by_window(ev->window);
|
||||
if (!c) {
|
||||
xcb_get_geometry_cookie_t geo_cookie =
|
||||
xcb_get_geometry(wm.xcb_conn, ev->window);
|
||||
xcb_get_geometry_reply_t *geo_reply =
|
||||
xcb_get_geometry_reply(wm.xcb_conn, geo_cookie, nullptr);
|
||||
xwindow_get_geometry_reply(ev->window);
|
||||
if (!geo_reply) {
|
||||
p_delete(&wa_reply);
|
||||
return;
|
||||
@@ -135,37 +161,169 @@ static void map_request(xcb_map_request_event_t *ev) {
|
||||
p_delete(&wa_reply);
|
||||
}
|
||||
|
||||
typedef enum _NET_WM_STATE_ACTION : uint32_t {
|
||||
_NET_WM_STATE_ADD = 1,
|
||||
_NET_WM_STATE_REMOVE = 0,
|
||||
_NET_WM_STATE_TOGGLE = 2,
|
||||
} _NET_WM_STATE_ACTION;
|
||||
|
||||
static inline bool get_bool_property(bool init_property,
|
||||
_NET_WM_STATE_ACTION action) {
|
||||
switch (action) {
|
||||
case _NET_WM_STATE_ADD:
|
||||
return true;
|
||||
case _NET_WM_STATE_REMOVE:
|
||||
return false;
|
||||
case _NET_WM_STATE_TOGGLE:
|
||||
return !init_property;
|
||||
default:
|
||||
fatal(
|
||||
"Invalid _NET_WM_STATE_ACTION: %u\n"
|
||||
"Only supported:"
|
||||
"_NET_WM_STATE_ADD: %u"
|
||||
"_NET_WM_STATE_REMOVE: %u"
|
||||
"_NET_WM_STATE_TOGGLE: %u",
|
||||
action, _NET_WM_STATE_ADD, _NET_WM_STATE_REMOVE, _NET_WM_STATE_TOGGLE);
|
||||
}
|
||||
}
|
||||
|
||||
static void client_message(xcb_client_message_event_t *ev) {
|
||||
if (tray_handle_client_message(ev)) return;
|
||||
|
||||
client_t *c = client_get_by_window(ev->window);
|
||||
if (c == nullptr) return;
|
||||
|
||||
if (ev->type == _NET_ACTIVE_WINDOW) {
|
||||
for (tag_t *t = c->monitor->tag_list; t; t = t->next) {
|
||||
if ((t->mask & c->tags) == 0) continue;
|
||||
|
||||
logger("== _NET_ACTIVE_WINDOW: %s[%u]\n", c->class, ev->data.data32[0]);
|
||||
|
||||
switch (ev->data.data32[0]) {
|
||||
case 2: /* 来自 pager */
|
||||
wm_set_current_monitor(c->monitor, true);
|
||||
monitor_select_tag(c->monitor, t->mask);
|
||||
client_focus(c);
|
||||
break;
|
||||
case 1: /* 来自应用程序 */
|
||||
c->urgent = true;
|
||||
monitor_draw_bar(c->monitor);
|
||||
break;
|
||||
case 0: /* 来自老版本标志 */
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (ev->type == _NET_WM_STATE) {
|
||||
/**
|
||||
* _NET_WM_STATE 协议数据格式为
|
||||
* data.data32 = {
|
||||
* action, // 操作类型(添加/移除/切换)
|
||||
* property1, // 第一个状态原子(如_NET_WM_STATE_FULLSCREEN)
|
||||
* property2, // 第二个状态原子(可选,通常为0)
|
||||
* 0, // 预留字段(固定为0)
|
||||
* 0 // 预留字段(固定为0)
|
||||
* };
|
||||
* action 值可能为
|
||||
* _NET_WM_STATE_ADD(1):添加全屏状态,窗口进入全屏模式
|
||||
* _NET_WM_STATE_REMOVE(0):移除全屏状态,窗口退出全屏模式
|
||||
* _NET_WM_STATE_TOGGLE(2):切换全屏状态(若当前全屏则退出,反之进入)
|
||||
*/
|
||||
_NET_WM_STATE_ACTION action = ev->data.data32[0];
|
||||
|
||||
if (ev->data.data32[1] == _NET_WM_STATE_FULLSCREEN ||
|
||||
ev->data.data32[2] == _NET_WM_STATE_FULLSCREEN) {
|
||||
client_set_fullscreen(c, get_bool_property(c->fullscreen, action));
|
||||
} else if (ev->data.data32[1] == _NET_WM_STATE_MAXIMIZED_HORZ ||
|
||||
ev->data.data32[1] == _NET_WM_STATE_MAXIMIZED_VERT ||
|
||||
ev->data.data32[2] == _NET_WM_STATE_MAXIMIZED_HORZ ||
|
||||
ev->data.data32[2] == _NET_WM_STATE_MAXIMIZED_VERT) {
|
||||
client_set_maximize(c, get_bool_property(c->maximize, action));
|
||||
}
|
||||
} else if (ev->type == WM_CHANGE_STATE) {
|
||||
switch (ev->data.data32[0]) {
|
||||
case XCB_ICCCM_WM_STATE_ICONIC:
|
||||
client_set_minimize(c, true);
|
||||
break;
|
||||
case XCB_ICCCM_WM_STATE_NORMAL:
|
||||
client_set_minimize(c, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void property_notify(xcb_property_notify_event_t *ev) {
|
||||
if (tray_handle_property_notify(ev)) return;
|
||||
|
||||
client_t *client = client_get_by_window(ev->window);
|
||||
if (client == nullptr) return;
|
||||
|
||||
if (ev->atom == WM_NAME) {
|
||||
xwindow_get_text_property(ev->window, ev->atom, &client->name);
|
||||
monitor_draw_bar(client->monitor);
|
||||
} else if (ev->atom == WM_ICON_NAME) {
|
||||
if (client->icon_name) p_delete(&client->icon_name);
|
||||
xwindow_get_text_property(ev->window, ev->atom, &client->icon_name);
|
||||
monitor_draw_bar(client->monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
} else if (ev->atom == _NET_WM_NAME) {
|
||||
xwindow_get_text_property(ev->window, ev->atom, &client->net_name);
|
||||
monitor_draw_bar(client->monitor);
|
||||
} else if (ev->atom == _NET_WM_ICON_NAME) {
|
||||
xwindow_get_text_property(ev->window, ev->atom, &client->net_icon_name);
|
||||
monitor_draw_bar(client->monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
} else if (ev->atom == XCB_ATOM_WM_HINTS) {
|
||||
client_update_wm_hints(client);
|
||||
monitor_draw_bar(client->monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
}
|
||||
|
||||
static void destroy_notify(xcb_destroy_notify_event_t *ev) {
|
||||
if (tray_handle_destroy_notify(ev)) return;
|
||||
|
||||
client_t *client = client_get_by_window(ev->window);
|
||||
if (client) client_unmanage(client);
|
||||
if (client) client_unmanage(client, true);
|
||||
}
|
||||
|
||||
static void unmap_notify(xcb_unmap_notify_event_t *ev) {
|
||||
if (tray_handle_unmap_notify(ev)) return;
|
||||
|
||||
client_t *client = client_get_by_window(ev->window);
|
||||
if (client) client_unmanage(client);
|
||||
if (!client || client->minimize) return;
|
||||
|
||||
if (XCB_EVENT_SENT(ev)) {
|
||||
xwindow_set_state(ev->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
|
||||
} else {
|
||||
client_unmanage(client, false);
|
||||
}
|
||||
}
|
||||
|
||||
static void map_notify(xcb_map_notify_event_t *ev) {
|
||||
/* 锁屏重新进入桌面后绘制 bar 内容以免 bar 不显示内容 */
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
if (ev->window == m->bar_window) {
|
||||
monitor_draw_bar(m);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void expose(xcb_expose_event_t *ev) {
|
||||
if (ev->count > 0) return;
|
||||
|
||||
monitor_t *monitor = wm_get_monitor_by_window(ev->window);
|
||||
monitor_draw_bar(monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
static void enter_notify(xcb_enter_notify_event_t *ev) {
|
||||
if (wm_should_ignore_enter_notify(ev)) return;
|
||||
|
||||
client_t *client = client_get_by_window(ev->event);
|
||||
if (!client || wm.client_focused == client) return;
|
||||
|
||||
client_focus(client);
|
||||
wm_set_current_monitor(client->monitor, false);
|
||||
}
|
||||
|
||||
static void selection_clear(xcb_selection_clear_event_t *ev) {
|
||||
tray_handle_selection_clear(ev);
|
||||
}
|
||||
|
||||
static void handle_xcb_event(xcb_generic_event_t *event) {
|
||||
@@ -185,9 +343,14 @@ static void handle_xcb_event(xcb_generic_event_t *event) {
|
||||
EVENT(XCB_KEY_RELEASE, key_press);
|
||||
EVENT(XCB_CONFIGURE_REQUEST, configure_request);
|
||||
EVENT(XCB_MAP_REQUEST, map_request);
|
||||
EVENT(XCB_MAP_NOTIFY, map_notify);
|
||||
EVENT(XCB_EXPOSE, expose);
|
||||
EVENT(XCB_CLIENT_MESSAGE, client_message);
|
||||
EVENT(XCB_PROPERTY_NOTIFY, property_notify);
|
||||
EVENT(XCB_UNMAP_NOTIFY, unmap_notify);
|
||||
EVENT(XCB_DESTROY_NOTIFY, destroy_notify);
|
||||
EVENT(XCB_ENTER_NOTIFY, enter_notify);
|
||||
EVENT(XCB_SELECTION_CLEAR, selection_clear);
|
||||
|
||||
#undef EVENT
|
||||
}
|
||||
|
||||
170
src/image.c
Normal file
170
src/image.c
Normal file
@@ -0,0 +1,170 @@
|
||||
#include "image.h"
|
||||
|
||||
#include <Imlib2.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
typedef struct image_cache_entry_t {
|
||||
char *path;
|
||||
cairo_surface_t *surface;
|
||||
int width;
|
||||
int height;
|
||||
} image_cache_entry_t;
|
||||
|
||||
static image_cache_entry_t *image_cache = nullptr;
|
||||
static size_t image_cache_count = 0;
|
||||
static cairo_user_data_key_t image_surface_data_key;
|
||||
|
||||
static inline uint32_t premultiply_argb(uint32_t pixel) {
|
||||
uint8_t a = (pixel >> 24) & 0xff;
|
||||
uint8_t r = (pixel >> 16) & 0xff;
|
||||
uint8_t g = (pixel >> 8) & 0xff;
|
||||
uint8_t b = pixel & 0xff;
|
||||
r = (uint8_t)((r * a + 127) / 255);
|
||||
g = (uint8_t)((g * a + 127) / 255);
|
||||
b = (uint8_t)((b * a + 127) / 255);
|
||||
return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) |
|
||||
(uint32_t)b;
|
||||
}
|
||||
|
||||
static bool image_load_surface(const char *path, cairo_surface_t **surface,
|
||||
int *width, int *height) {
|
||||
if (!path || !*path || !surface || !width || !height) return false;
|
||||
|
||||
Imlib_Image image = imlib_load_image(path);
|
||||
if (!image) return false;
|
||||
|
||||
imlib_context_set_image(image);
|
||||
int img_width = imlib_image_get_width();
|
||||
int img_height = imlib_image_get_height();
|
||||
if (img_width <= 0 || img_height <= 0) {
|
||||
imlib_free_image();
|
||||
return false;
|
||||
}
|
||||
|
||||
DATA32 *src = imlib_image_get_data_for_reading_only();
|
||||
if (!src) {
|
||||
imlib_free_image();
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t pixel_count = (size_t)img_width * (size_t)img_height;
|
||||
uint32_t *dst = xmalloc((ssize_t)(pixel_count * sizeof(uint32_t)));
|
||||
for (size_t i = 0; i < pixel_count; i++) {
|
||||
dst[i] = premultiply_argb(src[i]);
|
||||
}
|
||||
|
||||
cairo_surface_t *img_surface = cairo_image_surface_create_for_data(
|
||||
(unsigned char *)dst, CAIRO_FORMAT_ARGB32, img_width, img_height,
|
||||
img_width * (int)sizeof(uint32_t));
|
||||
cairo_status_t status = cairo_surface_status(img_surface);
|
||||
if (status != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_surface_destroy(img_surface);
|
||||
p_delete(&dst);
|
||||
imlib_free_image();
|
||||
return false;
|
||||
}
|
||||
|
||||
cairo_surface_set_user_data(img_surface, &image_surface_data_key, dst, free);
|
||||
*surface = img_surface;
|
||||
*width = img_width;
|
||||
*height = img_height;
|
||||
|
||||
imlib_free_image();
|
||||
return true;
|
||||
}
|
||||
|
||||
static image_cache_entry_t *image_cache_find(const char *path) {
|
||||
if (!path || !*path) return nullptr;
|
||||
for (size_t i = 0; i < image_cache_count; i++) {
|
||||
image_cache_entry_t *entry = &image_cache[i];
|
||||
if (!entry->path) continue;
|
||||
if (strcmp(entry->path, path) == 0) return entry;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static image_cache_entry_t *image_cache_add(const char *path) {
|
||||
if (!path || !*path) return nullptr;
|
||||
|
||||
size_t new_count = image_cache_count + 1;
|
||||
xrealloc((void **)&image_cache,
|
||||
(ssize_t)(new_count * sizeof(image_cache_entry_t)));
|
||||
image_cache_entry_t *entry = &image_cache[image_cache_count];
|
||||
entry->path = strdup(path);
|
||||
entry->surface = nullptr;
|
||||
entry->width = 0;
|
||||
entry->height = 0;
|
||||
image_cache_count = new_count;
|
||||
return entry;
|
||||
}
|
||||
|
||||
static image_cache_entry_t *image_cache_get(const char *path) {
|
||||
image_cache_entry_t *entry = image_cache_find(path);
|
||||
if (entry && entry->surface) return entry;
|
||||
if (!entry) entry = image_cache_add(path);
|
||||
if (!entry) return nullptr;
|
||||
|
||||
if (entry->surface) {
|
||||
cairo_surface_destroy(entry->surface);
|
||||
entry->surface = nullptr;
|
||||
}
|
||||
|
||||
if (!image_load_surface(path, &entry->surface, &entry->width, &entry->height)) {
|
||||
return nullptr;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
bool image_get_scaled_size(const char *path, int32_t target_height, int *width,
|
||||
int *height) {
|
||||
if (!width || !height || target_height <= 0) return false;
|
||||
image_cache_entry_t *entry = image_cache_get(path);
|
||||
if (!entry || !entry->surface || entry->width <= 0 || entry->height <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*height = target_height;
|
||||
*width = (entry->width * target_height + entry->height / 2) / entry->height;
|
||||
if (*width <= 0) *width = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool image_draw(cairo_t *cr, const char *path, int32_t x, int32_t y,
|
||||
int32_t width, int32_t height) {
|
||||
if (!cr || width <= 0 || height <= 0) return false;
|
||||
image_cache_entry_t *entry = image_cache_get(path);
|
||||
if (!entry || !entry->surface || entry->width <= 0 || entry->height <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cairo_save(cr);
|
||||
cairo_translate(cr, (double)x, (double)y);
|
||||
cairo_scale(cr, (double)width / (double)entry->width,
|
||||
(double)height / (double)entry->height);
|
||||
cairo_set_source_surface(cr, entry->surface, 0, 0);
|
||||
cairo_paint(cr);
|
||||
cairo_restore(cr);
|
||||
return true;
|
||||
}
|
||||
|
||||
void image_cache_clean(void) {
|
||||
if (!image_cache) return;
|
||||
|
||||
for (size_t i = 0; i < image_cache_count; i++) {
|
||||
image_cache_entry_t *entry = &image_cache[i];
|
||||
if (entry->surface) {
|
||||
cairo_surface_destroy(entry->surface);
|
||||
entry->surface = nullptr;
|
||||
}
|
||||
p_delete(&entry->path);
|
||||
entry->width = 0;
|
||||
entry->height = 0;
|
||||
}
|
||||
|
||||
p_delete(&image_cache);
|
||||
image_cache_count = 0;
|
||||
}
|
||||
36
src/image.h
Normal file
36
src/image.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <cairo.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief 获取图片按目标高度缩放后的尺寸
|
||||
*
|
||||
* @param path 图片路径
|
||||
* @param target_height 目标高度(像素)
|
||||
* @param width 返回缩放后的宽度
|
||||
* @param height 返回缩放后的高度
|
||||
* @return 加载并计算成功返回 true
|
||||
*/
|
||||
bool image_get_scaled_size(const char *path, int32_t target_height, int *width,
|
||||
int *height);
|
||||
|
||||
/**
|
||||
* @brief 在 cairo 上下文中绘制图片
|
||||
*
|
||||
* @param cr cairo 上下文
|
||||
* @param path 图片路径
|
||||
* @param x 绘制起始 x
|
||||
* @param y 绘制起始 y
|
||||
* @param width 绘制宽度
|
||||
* @param height 绘制高度
|
||||
* @return 绘制成功返回 true
|
||||
*/
|
||||
bool image_draw(cairo_t *cr, const char *path, int32_t x, int32_t y,
|
||||
int32_t width, int32_t height);
|
||||
|
||||
/**
|
||||
* @brief 释放图片缓存
|
||||
*/
|
||||
void image_cache_clean(void);
|
||||
66
src/layout.c
66
src/layout.c
@@ -1,5 +1,6 @@
|
||||
#include "layout.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "base.h"
|
||||
@@ -25,3 +26,68 @@ void monocle(tag_t *tag) {
|
||||
}
|
||||
logger("== monocle tag: %s end ==\n", tag->name);
|
||||
}
|
||||
|
||||
void tile(tag_t *tag) {
|
||||
logger("== tile tag: %s start ==\n", tag->name);
|
||||
uint16_t amount = 0;
|
||||
for (task_in_tag_t *task = tag->task_list; task; task = task->next) {
|
||||
if (client_need_layout(task->client)) ++amount;
|
||||
}
|
||||
if (amount == 0) goto end;
|
||||
|
||||
uint16_t columns = (uint32_t)floor(sqrt(amount));
|
||||
if (columns * (columns + 1) <= amount) ++columns;
|
||||
|
||||
uint32_t rows_in_other_cols = amount / columns;
|
||||
uint32_t rows_in_main_col;
|
||||
while ((rows_in_main_col = amount - (columns - 1) * rows_in_other_cols) >
|
||||
rows_in_other_cols) {
|
||||
++rows_in_other_cols;
|
||||
}
|
||||
|
||||
area_t *workarea = &tag->task_list->client->monitor->workarea;
|
||||
|
||||
uint16_t width_avg = workarea->width / columns;
|
||||
uint16_t width_for_main_col = workarea->width - (columns - 1) * width_avg;
|
||||
uint16_t width_for_other_cols = width_avg;
|
||||
|
||||
uint16_t i = 0, row = 0, col = 0, row_count, width, height;
|
||||
int16_t x = workarea->x, y = workarea->y;
|
||||
for (task_in_tag_t *task = tag->task_list; task; task = task->next) {
|
||||
if (!client_need_layout(task->client)) continue;
|
||||
|
||||
if (i < rows_in_main_col) {
|
||||
row = i;
|
||||
width = width_for_main_col;
|
||||
row_count = rows_in_main_col;
|
||||
} else {
|
||||
width = width_for_other_cols;
|
||||
row_count = rows_in_other_cols;
|
||||
if (((i - rows_in_main_col) % row_count) == 0) {
|
||||
col++;
|
||||
row = 0;
|
||||
x += col == 1 ? width_for_main_col : width_for_other_cols;
|
||||
y = workarea->y;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t h_avg = workarea->height / row_count;
|
||||
uint16_t h_main = workarea->height - h_avg * (row_count - 1);
|
||||
height = row == 0 ? h_main : h_avg;
|
||||
task->geometry.x = x;
|
||||
task->geometry.y = y;
|
||||
task->geometry.width = width - task->client->border_width * 2;
|
||||
task->geometry.height = height - task->client->border_width * 2;
|
||||
y += height;
|
||||
|
||||
++i;
|
||||
|
||||
logger(
|
||||
"== window 0x%x: x: %d, y: %d, width: %u, height: %u, border width: %u\n",
|
||||
task->client->window, task->geometry.x, task->geometry.y,
|
||||
task->geometry.width, task->geometry.height, task->client->border_width);
|
||||
}
|
||||
|
||||
end:
|
||||
logger("== tile tag: %s end\n", tag->name);
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
#include "types.h"
|
||||
|
||||
void monocle(tag_t *tag);
|
||||
void tile(tag_t *tag);
|
||||
|
||||
194
src/monitor.c
194
src/monitor.c
@@ -3,6 +3,7 @@
|
||||
#include <cairo-xcb.h>
|
||||
#include <cairo.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xcb_aux.h>
|
||||
#include <xcb/xcb_icccm.h>
|
||||
@@ -11,7 +12,12 @@
|
||||
#include "base.h"
|
||||
#include "client.h"
|
||||
#include "color.h"
|
||||
#include "config.h"
|
||||
#include "image.h"
|
||||
#include "renderer.h"
|
||||
#include "status.h"
|
||||
#include "text.h"
|
||||
#include "tray.h"
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "wm.h"
|
||||
@@ -49,7 +55,10 @@ uint32_t monitor_initialize_tag(monitor_t *monitor, const char **tags,
|
||||
}
|
||||
|
||||
void monitor_deal_focus(monitor_t *monitor) {
|
||||
if (!monitor->selected_tag->task_list) return;
|
||||
if (!monitor->selected_tag->task_list) {
|
||||
client_focus(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
client_t *client = nullptr;
|
||||
for (client_t *c = wm.client_stack_list; c; c = c->stack_next) {
|
||||
@@ -64,17 +73,30 @@ void monitor_deal_focus(monitor_t *monitor) {
|
||||
void monitor_select_tag(monitor_t *monitor, uint32_t tag_mask) {
|
||||
if (monitor->selected_tag->mask == tag_mask) return;
|
||||
|
||||
for (task_in_tag_t *task = monitor->selected_tag->task_list; task;
|
||||
task = task->next) {
|
||||
if (task->client->floating || task->client->size_freeze ||
|
||||
!monitor->selected_tag->layout->arrange) {
|
||||
task->geometry = task->client->geometry;
|
||||
task->client->old_geometry = task->client->geometry;
|
||||
}
|
||||
if (task->client->fullscreen || task->client->maximize ||
|
||||
task->client->minimize) {
|
||||
task->geometry = task->client->geometry;
|
||||
}
|
||||
}
|
||||
|
||||
for (tag_t *tag = monitor->tag_list; tag; tag = tag->next) {
|
||||
if (tag->mask == tag_mask) {
|
||||
monitor->selected_tag = tag;
|
||||
monitor_arrange(monitor);
|
||||
monitor_draw_bar(monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
monitor_deal_focus(monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
static void tag_clean(tag_t *tag) {
|
||||
@@ -218,17 +240,132 @@ static void monitor_draw_layout_symbol(monitor_t *monitor) {
|
||||
}
|
||||
}
|
||||
|
||||
static void monitor_draw_tasks(monitor_t *monitor) {
|
||||
static void monitor_draw_status(monitor_t *monitor, status_t *status,
|
||||
int16_t right_edge, int16_t left_edge) {
|
||||
static renderer_status_t *rendered_status = nullptr;
|
||||
static size_t rendered_status_capacity = 0;
|
||||
|
||||
monitor->status_extent.end = right_edge;
|
||||
monitor->status_extent.start = right_edge;
|
||||
int32_t end = left_edge;
|
||||
|
||||
if (status == nullptr || !wm.config || !wm.config->status) return;
|
||||
|
||||
config_status_t *status_config = wm.config->status;
|
||||
size_t item_count = status_config->status_count;
|
||||
if (item_count == 0) return;
|
||||
|
||||
if (rendered_status_capacity < item_count) {
|
||||
size_t size =
|
||||
sizeof(renderer_status_t) + item_count * sizeof(renderer_status_item_t);
|
||||
xrealloc((void **)&rendered_status, (ssize_t)size);
|
||||
rendered_status_capacity = item_count;
|
||||
}
|
||||
|
||||
status_renderer_t renderer = wm.config->status_renderer;
|
||||
if (!renderer) renderer = renderer_render_status;
|
||||
|
||||
renderer(status_config, item_count, status, rendered_status);
|
||||
item_count = MIN(item_count, rendered_status->item_count);
|
||||
|
||||
int32_t start = monitor->status_extent.start;
|
||||
int32_t icon_target_height =
|
||||
(int32_t)wm.bar_height - (int32_t)wm.padding.bar_y * 2;
|
||||
if (icon_target_height <= 0) icon_target_height = wm.bar_height;
|
||||
|
||||
for (size_t ri = item_count; ri > 0; ri--) {
|
||||
size_t i = ri - 1;
|
||||
renderer_status_item_t *item = &rendered_status->item_list[i];
|
||||
bool has_text = item->text[0] != '\0';
|
||||
int text_width = 0;
|
||||
if (has_text) text_get_size(item->text, &text_width, nullptr);
|
||||
|
||||
bool has_text_icon = false;
|
||||
int icon_text_width = 0;
|
||||
if (item->icon_type == icon_type_text && item->icon_text &&
|
||||
item->icon_text[0]) {
|
||||
has_text_icon = true;
|
||||
text_get_size(item->icon_text, &icon_text_width, nullptr);
|
||||
}
|
||||
|
||||
bool has_image_icon = false;
|
||||
int icon_width = 0;
|
||||
int icon_height = 0;
|
||||
if (item->icon_type == icon_type_image && item->icon_path &&
|
||||
item->icon_path[0]) {
|
||||
has_image_icon = image_get_scaled_size(
|
||||
item->icon_path, icon_target_height, &icon_width, &icon_height);
|
||||
}
|
||||
bool has_icon = has_text_icon || has_image_icon;
|
||||
|
||||
int width = 0;
|
||||
if (has_text_icon) width += icon_text_width;
|
||||
if (has_image_icon) width += icon_width;
|
||||
if (has_text) width += text_width;
|
||||
if (has_icon && has_text) width += (int)rendered_status->item_gap;
|
||||
if (!width) continue;
|
||||
|
||||
start -= width;
|
||||
if (start <= end) return;
|
||||
|
||||
color_t *color = item->color ? item->color : &wm.color_set.tag_color;
|
||||
int32_t draw_x = start;
|
||||
if (has_text_icon && icon_text_width > 0) {
|
||||
area_t icon_rect = {
|
||||
.x = (int16_t)draw_x,
|
||||
.y = 0,
|
||||
.width = (uint16_t)icon_text_width,
|
||||
.height = wm.bar_height,
|
||||
};
|
||||
draw_text(monitor->bar_cr, item->icon_text, color, icon_rect, true);
|
||||
draw_x += icon_text_width;
|
||||
} else if (has_image_icon) {
|
||||
int32_t icon_y = ((int32_t)wm.bar_height - icon_height) / 2;
|
||||
image_draw(monitor->bar_cr, item->icon_path, draw_x, icon_y, icon_width,
|
||||
icon_height);
|
||||
draw_x += icon_width;
|
||||
}
|
||||
if (has_icon && has_text) draw_x += (int32_t)rendered_status->item_gap;
|
||||
|
||||
if (has_text && text_width > 0) {
|
||||
area_t rect = {
|
||||
.x = (int16_t)draw_x,
|
||||
.y = 0,
|
||||
.width = (uint16_t)text_width,
|
||||
.height = wm.bar_height,
|
||||
};
|
||||
draw_text(monitor->bar_cr, item->text, color, rect, true);
|
||||
}
|
||||
monitor->status_extent.start = (int16_t)start;
|
||||
|
||||
if (ri > 1) {
|
||||
start -= (int32_t)rendered_status->gap;
|
||||
if (start < end) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void monitor_draw_tasks(monitor_t *monitor, int16_t right_edge) {
|
||||
task_in_tag_t *task_list = monitor->selected_tag->task_list;
|
||||
if (!task_list) return;
|
||||
|
||||
uint16_t task_count = 0;
|
||||
for (task_in_tag_t *task = task_list; task; task = task->next) ++task_count;
|
||||
for (task_in_tag_t *task = task_list; task; task = task->next) {
|
||||
if (task->client->skip_taskbar) continue;
|
||||
++task_count;
|
||||
};
|
||||
if (task_count == 0) return;
|
||||
|
||||
int16_t x = monitor->layout_symbol_extent.end;
|
||||
uint16_t task_width = (monitor->geometry.width - x) / task_count;
|
||||
int32_t available_width = (int32_t)right_edge - x;
|
||||
if (available_width <= 0) return;
|
||||
|
||||
uint16_t task_width = (uint16_t)(available_width / task_count);
|
||||
if (task_width < wm.font_size) return;
|
||||
|
||||
for (task_in_tag_t *task = task_list; task; task = task->next) {
|
||||
if (task->client->skip_taskbar) continue;
|
||||
|
||||
task->bar_extent.start = x;
|
||||
task->bar_extent.end = x + task_width;
|
||||
x += task_width;
|
||||
@@ -249,6 +386,9 @@ static void monitor_draw_tasks(monitor_t *monitor) {
|
||||
void monitor_draw_bar(monitor_t *monitor) {
|
||||
cairo_t *cr = monitor->bar_cr;
|
||||
uint16_t height = wm.bar_height;
|
||||
uint16_t tray_width = tray_get_width(monitor);
|
||||
int16_t status_right = (int16_t)monitor->geometry.width - tray_width;
|
||||
if (status_right < 0) status_right = 0;
|
||||
area_t bar_area = {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
@@ -259,24 +399,56 @@ void monitor_draw_bar(monitor_t *monitor) {
|
||||
|
||||
monitor_draw_tags(monitor);
|
||||
monitor_draw_layout_symbol(monitor);
|
||||
monitor_draw_tasks(monitor);
|
||||
monitor_draw_status(monitor, wm.status, status_right,
|
||||
monitor->layout_symbol_extent.end);
|
||||
monitor_draw_tasks(monitor, monitor->status_extent.start);
|
||||
tray_place(monitor, monitor->geometry.width);
|
||||
}
|
||||
|
||||
void monitor_arrange(monitor_t *monitor) {
|
||||
tag_t *tag = monitor->selected_tag;
|
||||
if (tag->layout && tag->layout->arrange) tag->layout->arrange(tag);
|
||||
|
||||
for (client_t *c = wm.client_list; c; c = c->next) {
|
||||
for (client_t *c = wm.client_stack_list; c; c = c->stack_next) {
|
||||
if (c->monitor != monitor || c->minimize) continue;
|
||||
|
||||
task_in_tag_t *task = client_get_task_in_tag(c, tag);
|
||||
if (task) {
|
||||
client_apply_task_geometry(c, task);
|
||||
if (client_need_layout(c)) {
|
||||
client_apply_geometry(c, task->geometry);
|
||||
} else if (c->fullscreen) {
|
||||
client_apply_geometry(c, c->monitor->geometry);
|
||||
} else if (c->maximize) {
|
||||
client_apply_geometry(c, c->monitor->workarea);
|
||||
} else {
|
||||
client_move_to(c, task->geometry.x, task->geometry.y);
|
||||
}
|
||||
} else {
|
||||
int16_t x = -client_width(c);
|
||||
int16_t y = -client_height(c);
|
||||
if (x != c->geometry.x || y != c->geometry.y) {
|
||||
client_move_to(c, x, y);
|
||||
}
|
||||
client_move_to(c, x, y);
|
||||
}
|
||||
}
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
void monitor_save_cursor_point(monitor_t *monitor) {
|
||||
monitor->cursor_position = xcursor_query_pointer_position();
|
||||
monitor->position_inited = true;
|
||||
}
|
||||
|
||||
point_t monitor_get_restore_cursor_point(monitor_t *monitor) {
|
||||
if (!monitor->position_inited) {
|
||||
point_t point = xcursor_query_pointer_position();
|
||||
monitor_t *m = wm_get_monitor_by_point(point);
|
||||
monitor->cursor_position.x = point.x - m->geometry.x + monitor->geometry.x;
|
||||
monitor->cursor_position.y = point.y - m->geometry.y + monitor->geometry.y;
|
||||
monitor->position_inited = true;
|
||||
}
|
||||
|
||||
return monitor->cursor_position;
|
||||
}
|
||||
|
||||
void monitor_restore_cursor_point(monitor_t *monitor) {
|
||||
xcursor_set_pointer_position(monitor_get_restore_cursor_point(monitor));
|
||||
}
|
||||
|
||||
@@ -12,3 +12,6 @@ void monitor_clean(monitor_t *monitor);
|
||||
void monitor_init_bar(monitor_t *monitor);
|
||||
void monitor_draw_bar(monitor_t *monitor);
|
||||
void monitor_arrange(monitor_t *monitor);
|
||||
void monitor_save_cursor_point(monitor_t *monitor);
|
||||
point_t monitor_get_restore_cursor_point(monitor_t *monitor);
|
||||
void monitor_restore_cursor_point(monitor_t *monitor);
|
||||
|
||||
155
src/rect.c
Normal file
155
src/rect.c
Normal file
@@ -0,0 +1,155 @@
|
||||
#include "rect.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
static inline bool rect_valid(rect_t rect) {
|
||||
return rect.x1 < rect.x2 && rect.y1 < rect.y2;
|
||||
}
|
||||
|
||||
/* 将一个合法矩形追加到动态矩形数组中。 */
|
||||
static inline void rect_list_append(rect_t **list, size_t *length,
|
||||
size_t *capacity, rect_t rect) {
|
||||
if (!rect_valid(rect)) return;
|
||||
|
||||
if (*length == *capacity) {
|
||||
*capacity = *capacity ? *capacity * 2 : 4;
|
||||
p_realloc(list, *capacity);
|
||||
}
|
||||
(*list)[(*length)++] = rect;
|
||||
}
|
||||
|
||||
bool rect_intersection(rect_t a, rect_t b, rect_t *intersection) {
|
||||
if (!rect_valid(a) || !rect_valid(b)) return false;
|
||||
|
||||
rect_t overlap = {
|
||||
.x1 = MAX(a.x1, b.x1),
|
||||
.y1 = MAX(a.y1, b.y1),
|
||||
.x2 = MIN(a.x2, b.x2),
|
||||
.y2 = MIN(a.y2, b.y2),
|
||||
};
|
||||
if (!rect_valid(overlap)) return false;
|
||||
|
||||
if (intersection) *intersection = overlap;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t rect_subtract(rect_t source, rect_t clip, rect_t remaining[4]) {
|
||||
if (!rect_valid(source)) return 0;
|
||||
|
||||
rect_t overlap;
|
||||
if (!rect_intersection(source, clip, &overlap)) {
|
||||
if (remaining) remaining[0] = source;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t count = 0;
|
||||
/*
|
||||
* source: [sx1,sx2) x [sy1,sy2)
|
||||
* overlap: [ox1,ox2) x [oy1,oy2) = source 与 clip 的交集
|
||||
*
|
||||
* y=sy1 ┌───────────────────────────────────────────┐
|
||||
* │ TOP │
|
||||
* │ UL UR │
|
||||
* y=oy1 ├───────────────┬───────────────┬───────────┤
|
||||
* │ LEFT │ OVERLAP │ RIGHT │
|
||||
* y=oy2 ├───────────────┴───────────────┴───────────┤
|
||||
* │ LL LR │
|
||||
* │ BOTTOM │
|
||||
* y=sy2 └───────────────────────────────────────────┘
|
||||
* x=sx1 x=ox1 x=ox2 x=sx2
|
||||
*
|
||||
* 角块归属:
|
||||
* UL、UR 属于 TOP
|
||||
* LL、LR 属于 BOTTOM
|
||||
* LEFT/RIGHT 只覆盖 y ∈ [oy1, oy2),因此不会包含四个角块。
|
||||
*/
|
||||
rect_t parts[] = {
|
||||
/* top */
|
||||
{
|
||||
.x1 = source.x1,
|
||||
.y1 = source.y1,
|
||||
.x2 = source.x2,
|
||||
.y2 = overlap.y1,
|
||||
},
|
||||
/* bottom */
|
||||
{
|
||||
.x1 = source.x1,
|
||||
.y1 = overlap.y2,
|
||||
.x2 = source.x2,
|
||||
.y2 = source.y2,
|
||||
},
|
||||
/* left */
|
||||
{
|
||||
.x1 = source.x1,
|
||||
.y1 = overlap.y1,
|
||||
.x2 = overlap.x1,
|
||||
.y2 = overlap.y2,
|
||||
},
|
||||
/* right */
|
||||
{
|
||||
.x1 = overlap.x2,
|
||||
.y1 = overlap.y1,
|
||||
.x2 = source.x2,
|
||||
.y2 = overlap.y2,
|
||||
},
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < countof(parts); i++) {
|
||||
if (!rect_valid(parts[i])) continue;
|
||||
if (remaining) remaining[count] = parts[i];
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t rect_subtract_many(rect_t source, const rect_t clips[],
|
||||
size_t clip_count, rect_t **remaining) {
|
||||
/*
|
||||
* 实现说明:
|
||||
* 初始剩余集合为 {source},然后依次处理每个 clip:
|
||||
* 1) 用 clip 去减当前集合中的每个碎片
|
||||
* 2) 将产生的新碎片合并到 next 集合
|
||||
* 3) 用 next 替换 current,进入下一轮
|
||||
* 最终 current 即 source - clips[0] - clips[1] - ... 的结果。
|
||||
*/
|
||||
if (remaining) *remaining = nullptr;
|
||||
if (!rect_valid(source)) return 0;
|
||||
|
||||
size_t current_length = 1;
|
||||
size_t current_capacity = 1;
|
||||
rect_t *current = p_new(rect_t, current_capacity);
|
||||
current[0] = source;
|
||||
|
||||
if (clips) {
|
||||
for (size_t i = 0; i < clip_count && current_length > 0; i++) {
|
||||
rect_t clip = clips[i];
|
||||
if (!rect_valid(clip)) continue;
|
||||
|
||||
size_t next_length = 0;
|
||||
size_t next_capacity = current_length ? current_length : 1;
|
||||
rect_t *next = p_new(rect_t, next_capacity);
|
||||
|
||||
for (size_t j = 0; j < current_length; j++) {
|
||||
rect_t fragments[4];
|
||||
size_t fragment_count = rect_subtract(current[j], clip, fragments);
|
||||
for (size_t k = 0; k < fragment_count; k++) {
|
||||
rect_list_append(&next, &next_length, &next_capacity, fragments[k]);
|
||||
}
|
||||
}
|
||||
|
||||
p_delete(¤t);
|
||||
current = next;
|
||||
current_length = next_length;
|
||||
current_capacity = next_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
if (remaining) {
|
||||
*remaining = current;
|
||||
} else {
|
||||
p_delete(¤t);
|
||||
}
|
||||
|
||||
return current_length;
|
||||
}
|
||||
41
src/rect.h
Normal file
41
src/rect.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct rect_t {
|
||||
int32_t x1;
|
||||
int32_t y1;
|
||||
int32_t x2;
|
||||
int32_t y2;
|
||||
} rect_t;
|
||||
|
||||
bool rect_intersection(rect_t a, rect_t b, rect_t *intersection);
|
||||
size_t rect_subtract(rect_t source, rect_t clip, rect_t remaining[4]);
|
||||
/**
|
||||
* @brief 从 source 中按顺序减去 clips 中的多个矩形
|
||||
* @param source 源矩形
|
||||
* @param clips 要减去的矩形数组,可为 nullptr
|
||||
* @param clip_count clips 的长度
|
||||
* @param remaining 输出剩余矩形数组;若不为 nullptr,则由函数分配内存并由调用方释放
|
||||
* @return 剩余矩形数量
|
||||
*
|
||||
* @code{.c}
|
||||
* rect_t source = {.x1 = 0, .y1 = 0, .x2 = 100, .y2 = 80};
|
||||
* rect_t clips[] = {
|
||||
* {.x1 = 10, .y1 = 10, .x2 = 40, .y2 = 40},
|
||||
* {.x1 = 30, .y1 = 20, .x2 = 70, .y2 = 60},
|
||||
* };
|
||||
*
|
||||
* rect_t *remaining = nullptr;
|
||||
* size_t count = rect_subtract_many(source, clips, 2, &remaining);
|
||||
*
|
||||
* for (size_t i = 0; i < count; i++) {
|
||||
* // use remaining[i]
|
||||
* }
|
||||
*
|
||||
* free(remaining);
|
||||
* @endcode
|
||||
*/
|
||||
size_t rect_subtract_many(rect_t source, const rect_t clips[],
|
||||
size_t clip_count, rect_t **remaining);
|
||||
90
src/renderer.c
Normal file
90
src/renderer.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "renderer.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "color.h"
|
||||
#include "config.h"
|
||||
#include "status.h"
|
||||
#include "utils.h"
|
||||
|
||||
static void status_text_from_data(status_type_t type, const status_t *status,
|
||||
char *text, size_t text_size) {
|
||||
if (!text || text_size == 0) return;
|
||||
text[0] = '\0';
|
||||
if (!status) return;
|
||||
|
||||
switch (type) {
|
||||
case status_net_down:
|
||||
snprintf(text, text_size, "%s", status->net_speed.down);
|
||||
break;
|
||||
case status_net_up:
|
||||
snprintf(text, text_size, "%s", status->net_speed.up);
|
||||
break;
|
||||
case status_audio:
|
||||
if (status->pulse) {
|
||||
snprintf(text, text_size, "%d%%%s%s%s", status->pulse->volume_percent,
|
||||
status->pulse->mute ? "M" : "",
|
||||
strlen(status->pulse->device_name) ? "|" : "",
|
||||
status->pulse->device_name);
|
||||
} else {
|
||||
snprintf(text, text_size, "0%%");
|
||||
}
|
||||
break;
|
||||
case status_memory:
|
||||
snprintf(text, text_size, "%s(%.1f%%)", status->mem_usage.mem_used_text,
|
||||
status->mem_usage.mem_percent);
|
||||
break;
|
||||
case status_cpu:
|
||||
snprintf(text, text_size, "%.1lf", status->cpu_usage_percent);
|
||||
break;
|
||||
case status_time:
|
||||
snprintf(text, text_size, "%s", status->time);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void renderer_render_status(config_status_t *config, size_t config_count,
|
||||
status_t *status, renderer_status_t *output) {
|
||||
static color_t *color_cache = nullptr;
|
||||
static size_t color_cache_count = 0;
|
||||
|
||||
if (!config || !output) return;
|
||||
|
||||
size_t item_count = config->status_count;
|
||||
if (config_count && config_count < item_count) item_count = config_count;
|
||||
|
||||
output->gap = config->status_gap;
|
||||
output->item_gap = config->status_item_gap;
|
||||
output->item_count = item_count;
|
||||
|
||||
if (item_count == 0) return;
|
||||
|
||||
if (color_cache_count < item_count) {
|
||||
xrealloc((void **)&color_cache, (ssize_t)(sizeof(color_t) * item_count));
|
||||
color_cache_count = item_count;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < item_count; i++) {
|
||||
const config_status_item_t *config_item = &config->list[i];
|
||||
renderer_status_item_t *output_item = &output->item_list[i];
|
||||
memset(output_item, 0, sizeof(*output_item));
|
||||
|
||||
output_item->icon_type = config_item->icon_type;
|
||||
if (config_item->icon_type == icon_type_text) {
|
||||
output_item->icon_text = config_item->icon;
|
||||
} else if (config_item->icon_type == icon_type_image) {
|
||||
output_item->icon_path = config_item->icon;
|
||||
}
|
||||
|
||||
status_text_from_data(config_item->type, status, output_item->text,
|
||||
sizeof(output_item->text));
|
||||
|
||||
if (config_item->color && *config_item->color) {
|
||||
color_parse(config_item->color, &color_cache[i]);
|
||||
output_item->color = &color_cache[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/renderer.h
Normal file
41
src/renderer.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "color.h"
|
||||
|
||||
typedef struct config_status_t config_status_t;
|
||||
typedef struct status_t status_t;
|
||||
|
||||
typedef enum icon_type_t {
|
||||
icon_type_none,
|
||||
icon_type_text,
|
||||
icon_type_image,
|
||||
} icon_type_t;
|
||||
|
||||
typedef struct renderer_status_item_t {
|
||||
icon_type_t icon_type;
|
||||
char text[124];
|
||||
const char *icon_text;
|
||||
const char *icon_path;
|
||||
color_t *color;
|
||||
} renderer_status_item_t;
|
||||
|
||||
typedef struct renderer_status_t {
|
||||
uint32_t gap;
|
||||
uint32_t item_gap;
|
||||
size_t item_count;
|
||||
renderer_status_item_t item_list[];
|
||||
} renderer_status_t;
|
||||
|
||||
/**
|
||||
* @brief 根据 status 配置和实时状态生成可绘制的 status 数据
|
||||
*
|
||||
* @param config status 配置
|
||||
* @param config_count 最大输出项数,传 0 表示使用 config->status_count
|
||||
* @param status 实时状态数据
|
||||
* @param output 渲染输出缓冲
|
||||
*/
|
||||
void renderer_render_status(config_status_t *config, size_t config_count,
|
||||
status_t *status, renderer_status_t *output);
|
||||
253
src/status.c
Normal file
253
src/status.c
Normal file
@@ -0,0 +1,253 @@
|
||||
#include "status.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <glibconfig.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "audio.h"
|
||||
|
||||
static constexpr char CPU_FILE[] = "/proc/stat";
|
||||
static constexpr char MEM_FILE[] = "/proc/meminfo";
|
||||
static constexpr char NET_FILE[] = "/proc/net/dev";
|
||||
|
||||
static constexpr uint32_t INTERVAL = 1;
|
||||
static constexpr char TIME_FORMAT[] = "%A %m-%d %H:%M:%S";
|
||||
|
||||
static status_t status;
|
||||
static guint timer = 0;
|
||||
static status_changed_notify listener = nullptr;
|
||||
|
||||
typedef struct cpu_stat_t {
|
||||
uint64_t user;
|
||||
uint64_t nice;
|
||||
uint64_t system;
|
||||
uint64_t idle;
|
||||
uint64_t iowait;
|
||||
uint64_t irq;
|
||||
uint64_t softirq;
|
||||
uint64_t steal;
|
||||
uint64_t guest;
|
||||
uint64_t guest_nice;
|
||||
} cpu_stat_t;
|
||||
|
||||
static double calc_cpu_usage(cpu_stat_t *curr, cpu_stat_t *prev) {
|
||||
uint64_t curr_idle = curr->idle + curr->iowait;
|
||||
uint64_t curr_total = curr->user + curr->nice + curr->system + curr->idle +
|
||||
curr->iowait + curr->irq + curr->softirq + curr->steal +
|
||||
curr->guest + curr->guest_nice;
|
||||
|
||||
uint64_t prev_idle = prev->idle + prev->iowait;
|
||||
uint64_t prev_total = prev->user + prev->nice + prev->system + prev->idle +
|
||||
prev->iowait + prev->irq + prev->softirq + prev->steal +
|
||||
prev->guest + prev->guest_nice;
|
||||
|
||||
uint64_t total = curr_total - prev_total;
|
||||
uint64_t idle = curr_idle - prev_idle;
|
||||
uint64_t active = total - idle;
|
||||
if (total == 0) return 0.0;
|
||||
|
||||
double usage = ((double)active) / total * 100;
|
||||
return usage;
|
||||
}
|
||||
|
||||
static bool get_cpu_usage(double *usage, GError *error) {
|
||||
static cpu_stat_t prev_cpu_stat = {0};
|
||||
static bool inited = false;
|
||||
|
||||
gchar *contents = nullptr;
|
||||
gsize length = 0;
|
||||
|
||||
if (!g_file_get_contents(CPU_FILE, &contents, &length, &error)) return false;
|
||||
|
||||
gchar **lines = g_strsplit(contents, "\n", 0);
|
||||
g_free(contents);
|
||||
|
||||
char cpu_line[256];
|
||||
for (gchar **line = lines; *line != nullptr; ++line) {
|
||||
if (strncmp(*line, "cpu ", 4) == 0) {
|
||||
strncpy(cpu_line, *line, sizeof(cpu_line));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cpu_stat_t stat = {0};
|
||||
sscanf(cpu_line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", &stat.user,
|
||||
&stat.nice, &stat.system, &stat.idle, &stat.iowait, &stat.irq,
|
||||
&stat.softirq, &stat.steal, &stat.guest, &stat.guest_nice);
|
||||
|
||||
if (!inited) {
|
||||
prev_cpu_stat = stat;
|
||||
inited = true;
|
||||
}
|
||||
|
||||
double percent = calc_cpu_usage(&stat, &prev_cpu_stat);
|
||||
prev_cpu_stat = stat;
|
||||
*usage = percent;
|
||||
|
||||
g_strfreev(lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void bytes_to_readable_size(uint64_t bytes, char *str) {
|
||||
static char *uints[] = {"B", "K", "M", "G", "T"};
|
||||
|
||||
double size = (double)bytes;
|
||||
int i = 0;
|
||||
while (size > 1024.0) {
|
||||
size /= 1024.0;
|
||||
++i;
|
||||
}
|
||||
if (i == 0) {
|
||||
sprintf(str, "%ld%s", bytes, uints[i]);
|
||||
} else {
|
||||
sprintf(str, "%.1lf%s", size, uints[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void calc_mem_usage(memory_usage_t *usage) {
|
||||
usage->mem_used = usage->mem_total - usage->mem_free - usage->buffers -
|
||||
usage->cached - usage->s_reclaimable;
|
||||
usage->swap_used = usage->swap_total - usage->swap_free;
|
||||
|
||||
usage->mem_percent =
|
||||
((float)usage->mem_used) / ((float)usage->mem_total) * 100;
|
||||
if (usage->swap_total == 0) {
|
||||
usage->swap_percent = 0;
|
||||
} else {
|
||||
usage->swap_percent =
|
||||
((float)usage->swap_used) / ((float)usage->swap_total) * 100;
|
||||
}
|
||||
|
||||
bytes_to_readable_size(usage->mem_used * 1024, usage->mem_used_text);
|
||||
bytes_to_readable_size(usage->swap_used * 1024, usage->swap_used_text);
|
||||
}
|
||||
|
||||
static bool get_mem_usage(memory_usage_t *usage, GError *error) {
|
||||
gchar *contents = nullptr;
|
||||
gsize length = 0;
|
||||
|
||||
if (!g_file_get_contents(MEM_FILE, &contents, &length, &error)) return false;
|
||||
|
||||
gchar **lines = g_strsplit(contents, "\n", 0);
|
||||
g_free(contents);
|
||||
|
||||
for (gchar **line = lines; *line != nullptr; ++line) {
|
||||
char name[32];
|
||||
uint64_t kb = 0;
|
||||
sscanf(*line, "%[^:]: %lu", name, &kb);
|
||||
if (g_str_equal(name, "MemTotal")) {
|
||||
usage->mem_total = kb;
|
||||
} else if (g_str_equal(name, "MemFree")) {
|
||||
usage->mem_free = kb;
|
||||
} else if (g_str_equal(name, "Buffers")) {
|
||||
usage->buffers = kb;
|
||||
} else if (g_str_equal(name, "Cached")) {
|
||||
usage->cached = kb;
|
||||
} else if (g_str_equal(name, "SwapTotal")) {
|
||||
usage->swap_total = kb;
|
||||
} else if (g_str_equal(name, "SwapFree")) {
|
||||
usage->swap_free = kb;
|
||||
} else if (g_str_equal(name, "SReclaimable")) {
|
||||
usage->s_reclaimable = kb;
|
||||
}
|
||||
}
|
||||
calc_mem_usage(usage);
|
||||
g_strfreev(lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
typedef struct net_stat_t {
|
||||
uint64_t rx_bytes;
|
||||
uint64_t tx_bytes;
|
||||
} net_stat_t;
|
||||
|
||||
static void calc_speed(const uint32_t interval, const net_stat_t *current,
|
||||
const net_stat_t *previous, net_speed_t *speed) {
|
||||
speed->rx_bytes = current->rx_bytes - previous->rx_bytes;
|
||||
speed->tx_bytes = current->tx_bytes - previous->tx_bytes;
|
||||
bytes_to_readable_size(speed->rx_bytes / interval, speed->down);
|
||||
bytes_to_readable_size(speed->tx_bytes / interval, speed->up);
|
||||
}
|
||||
|
||||
static bool get_net_speed(const uint32_t interval, net_speed_t *speed,
|
||||
GError *error) {
|
||||
static net_stat_t prev = {0};
|
||||
static bool inited = false;
|
||||
|
||||
gchar *contents = nullptr;
|
||||
gsize length = 0;
|
||||
|
||||
if (!g_file_get_contents(NET_FILE, &contents, &length, &error)) return false;
|
||||
|
||||
gchar **lines = g_strsplit(contents, "\n", 0);
|
||||
g_free(contents);
|
||||
|
||||
net_stat_t stat = {0};
|
||||
for (gchar **line = lines; *line != nullptr; ++line) {
|
||||
if (strchr(*line, ':') == nullptr) continue;
|
||||
|
||||
char name[16];
|
||||
uint64_t rx_bytes, tx_bytes;
|
||||
sscanf(*line, " %[^:]: %lu %*u %*u %*u %*u %*u %*u %*u %lu", name,
|
||||
&rx_bytes, &tx_bytes);
|
||||
if (g_str_equal(name, "lo")) continue;
|
||||
|
||||
stat.rx_bytes += rx_bytes;
|
||||
stat.tx_bytes += tx_bytes;
|
||||
}
|
||||
|
||||
if (!inited) {
|
||||
inited = true;
|
||||
prev = stat;
|
||||
}
|
||||
|
||||
calc_speed(interval, &stat, &prev, speed);
|
||||
prev = stat;
|
||||
|
||||
g_strfreev(lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void get_date_time(char *time_text, size_t length) {
|
||||
time_t current_time = time(nullptr);
|
||||
struct tm *time_info = localtime(¤t_time);
|
||||
strftime(time_text, length, TIME_FORMAT, time_info);
|
||||
}
|
||||
|
||||
static inline void notify_status_change(void) {
|
||||
if (listener) listener(&status);
|
||||
}
|
||||
|
||||
static gboolean update_status(gpointer data) {
|
||||
GError *error = nullptr;
|
||||
get_net_speed(INTERVAL, &status.net_speed, error);
|
||||
get_mem_usage(&status.mem_usage, error);
|
||||
get_cpu_usage(&status.cpu_usage_percent, error);
|
||||
get_date_time(status.time, sizeof(status.time));
|
||||
notify_status_change();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void notify_pulse_change(pulse_t *pulse) {
|
||||
status.pulse = pulse;
|
||||
notify_status_change();
|
||||
}
|
||||
|
||||
void init_status(GMainContext *context, status_changed_notify callback) {
|
||||
listener = callback;
|
||||
status.pulse_context = init_pulse(context, notify_pulse_change);
|
||||
update_status(nullptr);
|
||||
timer = g_timeout_add_seconds(INTERVAL, update_status, nullptr);
|
||||
}
|
||||
|
||||
void clean_status(void) {
|
||||
clean_pulse(status.pulse_context);
|
||||
g_source_remove(timer);
|
||||
status.pulse_context = nullptr;
|
||||
listener = nullptr;
|
||||
}
|
||||
53
src/status.h
Normal file
53
src/status.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "audio.h"
|
||||
|
||||
typedef struct memory_usage_t {
|
||||
char mem_used_text[32];
|
||||
char swap_used_text[32];
|
||||
|
||||
float mem_percent;
|
||||
float swap_percent;
|
||||
uint64_t mem_used;
|
||||
uint64_t swap_used;
|
||||
|
||||
uint64_t mem_total;
|
||||
uint64_t mem_free;
|
||||
uint64_t buffers;
|
||||
uint64_t cached;
|
||||
uint64_t swap_total;
|
||||
uint64_t swap_free;
|
||||
uint64_t s_reclaimable;
|
||||
} memory_usage_t;
|
||||
|
||||
typedef struct net_speed_t {
|
||||
uint64_t rx_bytes;
|
||||
uint64_t tx_bytes;
|
||||
char up[64];
|
||||
char down[64];
|
||||
} net_speed_t;
|
||||
|
||||
typedef enum status_type_t {
|
||||
status_net_down,
|
||||
status_net_up,
|
||||
status_audio,
|
||||
status_memory,
|
||||
status_cpu,
|
||||
status_time,
|
||||
} status_type_t;
|
||||
|
||||
typedef struct status_t {
|
||||
net_speed_t net_speed;
|
||||
memory_usage_t mem_usage;
|
||||
double cpu_usage_percent;
|
||||
char time[64];
|
||||
pulse_t *pulse;
|
||||
pulse_context_t *pulse_context;
|
||||
} status_t;
|
||||
|
||||
typedef void (*status_changed_notify)(status_t *status);
|
||||
void init_status(GMainContext *context, status_changed_notify callback);
|
||||
void clean_status(void);
|
||||
602
src/tray.c
Normal file
602
src/tray.c
Normal file
@@ -0,0 +1,602 @@
|
||||
#include "tray.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xcb.h>
|
||||
#include <xcb/xcb_aux.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "atoms-extern.h"
|
||||
#include "base.h"
|
||||
#include "monitor.h"
|
||||
#include "utils.h"
|
||||
#include "wm.h"
|
||||
#include "xwindow.h"
|
||||
|
||||
typedef enum tray_opcode_t {
|
||||
TRAY_REQUEST_DOCK = 0,
|
||||
TRAY_BEGIN_MESSAGE = 1,
|
||||
TRAY_CANCEL_MESSAGE = 2,
|
||||
} tray_opcode_t;
|
||||
|
||||
typedef enum tray_orientation_t {
|
||||
TRAY_ORIENTATION_HORIZONTAL = 0,
|
||||
} tray_orientation_t;
|
||||
|
||||
typedef enum xembed_message_t {
|
||||
XEMBED_EMBEDDED_NOTIFY = 0,
|
||||
} xembed_message_t;
|
||||
|
||||
enum {
|
||||
XEMBED_VERSION = 0,
|
||||
};
|
||||
|
||||
typedef struct tray_icon_t {
|
||||
xcb_window_t window;
|
||||
uint16_t natural_width;
|
||||
uint16_t natural_height;
|
||||
uint8_t ignore_unmaps;
|
||||
bool mapped;
|
||||
} tray_icon_t;
|
||||
|
||||
typedef struct tray_t {
|
||||
bool initialized;
|
||||
xcb_atom_t selection_atom;
|
||||
xcb_window_t window;
|
||||
monitor_t *host_monitor;
|
||||
|
||||
tray_icon_t *icons;
|
||||
size_t icon_count;
|
||||
size_t icon_capacity;
|
||||
|
||||
uint16_t spacing;
|
||||
uint16_t side_padding;
|
||||
} tray_t;
|
||||
|
||||
static tray_t tray = {
|
||||
.selection_atom = XCB_ATOM_NONE,
|
||||
.window = XCB_WINDOW_NONE,
|
||||
};
|
||||
|
||||
static xcb_atom_t tray_intern_atom(const char *name) {
|
||||
if (!name || !name[0]) return XCB_ATOM_NONE;
|
||||
|
||||
xcb_intern_atom_cookie_t cookie =
|
||||
xcb_intern_atom(wm.xcb_conn, false, (uint16_t)strlen(name), name);
|
||||
xcb_intern_atom_reply_t *reply =
|
||||
xcb_intern_atom_reply(wm.xcb_conn, cookie, nullptr);
|
||||
if (!reply) return XCB_ATOM_NONE;
|
||||
|
||||
xcb_atom_t atom = reply->atom;
|
||||
p_delete(&reply);
|
||||
return atom;
|
||||
}
|
||||
|
||||
static tray_icon_t *tray_get_icon(xcb_window_t window) {
|
||||
for (size_t i = 0; i < tray.icon_count; i++) {
|
||||
tray_icon_t *icon = &tray.icons[i];
|
||||
if (icon->window == window) return icon;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static size_t tray_find_icon_index(xcb_window_t window) {
|
||||
for (size_t i = 0; i < tray.icon_count; i++) {
|
||||
if (tray.icons[i].window == window) return i;
|
||||
}
|
||||
|
||||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
static uint16_t tray_target_icon_height(void) {
|
||||
return MAX((uint16_t)1, wm.bar_height);
|
||||
}
|
||||
|
||||
static uint32_t tray_color_component_u16(double channel) {
|
||||
if (channel < 0.0) channel = 0.0;
|
||||
if (channel > 1.0) channel = 1.0;
|
||||
|
||||
uint32_t value = (uint32_t)(channel * 65535.0 + 0.5);
|
||||
return MIN(value, 65535u);
|
||||
}
|
||||
|
||||
static void tray_get_icon_layout(const tray_icon_t *icon, uint16_t *width,
|
||||
uint16_t *height) {
|
||||
uint32_t target_height = tray_target_icon_height();
|
||||
uint32_t natural_width = icon->natural_width;
|
||||
uint32_t natural_height = icon->natural_height;
|
||||
|
||||
if (natural_width == 0) natural_width = target_height;
|
||||
if (natural_height == 0) natural_height = target_height;
|
||||
if (natural_height == 0) natural_height = 1;
|
||||
|
||||
uint32_t scaled_width =
|
||||
(natural_width * target_height + natural_height / 2) / natural_height;
|
||||
if (scaled_width == 0) scaled_width = 1;
|
||||
|
||||
if (width) *width = (uint16_t)MIN((uint32_t)UINT16_MAX, scaled_width);
|
||||
if (height) *height = (uint16_t)MIN((uint32_t)UINT16_MAX, target_height);
|
||||
}
|
||||
|
||||
static uint16_t tray_compute_width(void) {
|
||||
if (!tray.initialized || tray.host_monitor == nullptr || tray.icon_count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t width = (uint32_t)tray.side_padding * 2;
|
||||
for (size_t i = 0; i < tray.icon_count; i++) {
|
||||
uint16_t icon_width = 0;
|
||||
tray_get_icon_layout(&tray.icons[i], &icon_width, nullptr);
|
||||
width += icon_width;
|
||||
}
|
||||
if (tray.icon_count > 1) {
|
||||
width += (uint32_t)(tray.icon_count - 1) * tray.spacing;
|
||||
}
|
||||
|
||||
return (uint16_t)MIN((uint32_t)UINT16_MAX, width);
|
||||
}
|
||||
|
||||
static void tray_send_xembed_message(xcb_window_t window, uint32_t message,
|
||||
uint32_t detail, uint32_t data1,
|
||||
uint32_t data2) {
|
||||
xcb_client_message_event_t ev;
|
||||
p_clear(&ev, 1);
|
||||
ev.response_type = XCB_CLIENT_MESSAGE;
|
||||
ev.format = 32;
|
||||
ev.window = window;
|
||||
ev.type = _XEMBED;
|
||||
ev.data.data32[0] = XCB_CURRENT_TIME;
|
||||
ev.data.data32[1] = message;
|
||||
ev.data.data32[2] = detail;
|
||||
ev.data.data32[3] = data1;
|
||||
ev.data.data32[4] = data2;
|
||||
|
||||
xcb_send_event(wm.xcb_conn, false, window, XCB_EVENT_MASK_NO_EVENT,
|
||||
(char *)&ev);
|
||||
}
|
||||
|
||||
static void tray_select_icon_events(xcb_window_t window) {
|
||||
xcb_params_cw_t params = {
|
||||
.back_pixel = wm.color_set.bar_bg.argb,
|
||||
.border_pixel = 0,
|
||||
.event_mask = XCB_EVENT_MASK_PROPERTY_CHANGE |
|
||||
XCB_EVENT_MASK_STRUCTURE_NOTIFY,
|
||||
};
|
||||
uint32_t mask =
|
||||
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_EVENT_MASK;
|
||||
xcb_aux_change_window_attributes(wm.xcb_conn, window, mask, ¶ms);
|
||||
xcb_clear_area(wm.xcb_conn, false, window, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
static void tray_release_icon_window(tray_icon_t *icon) {
|
||||
if (!icon || icon->window == XCB_WINDOW_NONE) return;
|
||||
|
||||
xcb_change_save_set(wm.xcb_conn, XCB_SET_MODE_DELETE, icon->window);
|
||||
xcb_params_cw_t params = {
|
||||
.event_mask = XCB_EVENT_MASK_NO_EVENT,
|
||||
.border_pixel = 0,
|
||||
};
|
||||
xcb_aux_change_window_attributes(
|
||||
wm.xcb_conn, icon->window, XCB_CW_EVENT_MASK | XCB_CW_BORDER_PIXEL,
|
||||
¶ms);
|
||||
xcb_reparent_window(wm.xcb_conn, icon->window, wm.screen->root, 0, 0);
|
||||
}
|
||||
|
||||
static void tray_layout_icons(int16_t right_edge) {
|
||||
if (!tray.initialized || tray.window == XCB_WINDOW_NONE ||
|
||||
tray.host_monitor == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t tray_width = tray_compute_width();
|
||||
if (tray_width == 0) {
|
||||
xcb_unmap_window(wm.xcb_conn, tray.window);
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t tray_x = (int32_t)right_edge - tray_width;
|
||||
if (tray_x < 0) tray_x = 0;
|
||||
|
||||
xcb_configure_window_value_list_t tray_values = {
|
||||
.x = (int16_t)tray_x,
|
||||
.y = 0,
|
||||
.width = tray_width,
|
||||
.height = wm.bar_height,
|
||||
};
|
||||
uint16_t tray_mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
|
||||
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
|
||||
xcb_configure_window_aux(wm.xcb_conn, tray.window, tray_mask, &tray_values);
|
||||
xcb_map_window(wm.xcb_conn, tray.window);
|
||||
|
||||
int32_t x = tray.side_padding;
|
||||
for (size_t i = 0; i < tray.icon_count; i++) {
|
||||
tray_icon_t *icon = &tray.icons[i];
|
||||
uint16_t icon_width = 0;
|
||||
uint16_t icon_height = 0;
|
||||
tray_get_icon_layout(icon, &icon_width, &icon_height);
|
||||
|
||||
int32_t y = ((int32_t)wm.bar_height - icon_height) / 2;
|
||||
if (y < 0) y = 0;
|
||||
|
||||
xcb_configure_window_value_list_t icon_values = {
|
||||
.x = (int16_t)x,
|
||||
.y = (int16_t)y,
|
||||
.width = icon_width,
|
||||
.height = icon_height,
|
||||
.border_width = 0,
|
||||
};
|
||||
uint16_t icon_mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
|
||||
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
|
||||
XCB_CONFIG_WINDOW_BORDER_WIDTH;
|
||||
xcb_configure_window_aux(wm.xcb_conn, icon->window, icon_mask,
|
||||
&icon_values);
|
||||
xcb_map_window(wm.xcb_conn, icon->window);
|
||||
icon->mapped = true;
|
||||
|
||||
x += icon_width + tray.spacing;
|
||||
}
|
||||
}
|
||||
|
||||
static void tray_request_redraw(void) {
|
||||
if (!tray.initialized || tray.host_monitor == nullptr) return;
|
||||
|
||||
monitor_draw_bar(tray.host_monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
static void tray_remove_icon_by_index(size_t index, bool release_window) {
|
||||
if (index >= tray.icon_count) return;
|
||||
|
||||
tray_icon_t icon = tray.icons[index];
|
||||
if (release_window) tray_release_icon_window(&icon);
|
||||
|
||||
if (index + 1 < tray.icon_count) {
|
||||
memmove(&tray.icons[index], &tray.icons[index + 1],
|
||||
sizeof(*tray.icons) * (tray.icon_count - index - 1));
|
||||
}
|
||||
tray.icon_count--;
|
||||
|
||||
tray_request_redraw();
|
||||
}
|
||||
|
||||
static void tray_add_icon(xcb_window_t window) {
|
||||
if (!tray.initialized || window == XCB_WINDOW_NONE ||
|
||||
tray_get_icon(window) != nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tray.icon_count == tray.icon_capacity) {
|
||||
tray.icon_capacity = (size_t)p_alloc_nr(tray.icon_capacity);
|
||||
p_realloc(&tray.icons, tray.icon_capacity);
|
||||
}
|
||||
|
||||
xcb_get_geometry_reply_t *geometry = xwindow_get_geometry_reply(window);
|
||||
if (!geometry) return;
|
||||
|
||||
tray_icon_t *icon = &tray.icons[tray.icon_count++];
|
||||
*icon = (tray_icon_t){
|
||||
.window = window,
|
||||
.natural_width = geometry->width,
|
||||
.natural_height = geometry->height,
|
||||
.ignore_unmaps = 1,
|
||||
.mapped = true,
|
||||
};
|
||||
p_delete(&geometry);
|
||||
|
||||
xcb_change_save_set(wm.xcb_conn, XCB_SET_MODE_INSERT, window);
|
||||
xcb_reparent_window(wm.xcb_conn, window, tray.window, 0, 0);
|
||||
tray_select_icon_events(window);
|
||||
tray_send_xembed_message(window, XEMBED_EMBEDDED_NOTIFY, 0, tray.window,
|
||||
XEMBED_VERSION);
|
||||
xcb_map_window(wm.xcb_conn, window);
|
||||
|
||||
tray_request_redraw();
|
||||
}
|
||||
|
||||
static bool tray_create_window(void) {
|
||||
static xcb_colormap_t colormap = XCB_NONE;
|
||||
if (tray.host_monitor == nullptr || tray.host_monitor->bar_window == XCB_NONE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
visual_t *visual = xwindow_get_xcb_visual(false);
|
||||
if (visual == nullptr) return false;
|
||||
|
||||
xcb_visualid_t visual_id = visual->visual->visual_id;
|
||||
if (colormap == XCB_NONE) {
|
||||
colormap = xcb_generate_id(wm.xcb_conn);
|
||||
xcb_create_colormap(wm.xcb_conn, XCB_COLORMAP_ALLOC_NONE, colormap,
|
||||
wm.screen->root, visual_id);
|
||||
}
|
||||
|
||||
xcb_window_t window = xcb_generate_id(wm.xcb_conn);
|
||||
uint32_t value_mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL |
|
||||
XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
|
||||
const xcb_create_window_value_list_t value_list = {
|
||||
.background_pixel = wm.color_set.bar_bg.argb,
|
||||
.border_pixel = 0,
|
||||
.event_mask = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
|
||||
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
|
||||
.colormap = colormap,
|
||||
};
|
||||
|
||||
xcb_void_cookie_t cookie = xcb_create_window_aux_checked(
|
||||
wm.xcb_conn, visual->depth, window, tray.host_monitor->bar_window, 0, 0, 1,
|
||||
wm.bar_height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, visual_id, value_mask,
|
||||
&value_list);
|
||||
p_delete(&visual);
|
||||
if (xcb_request_check(wm.xcb_conn, cookie)) {
|
||||
logger("cannot create system tray window\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
tray.window = window;
|
||||
xwindow_set_class_instance(window);
|
||||
#define NAME APP_NAME "_tray"
|
||||
xwindow_set_name_static(window, NAME);
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, window, _NET_WM_NAME,
|
||||
UTF8_STRING, 8, sizeof(NAME) - 1, NAME);
|
||||
#undef NAME
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool tray_take_selection_owner(void) {
|
||||
xcb_get_selection_owner_cookie_t owner_cookie =
|
||||
xcb_get_selection_owner(wm.xcb_conn, tray.selection_atom);
|
||||
xcb_get_selection_owner_reply_t *owner_reply =
|
||||
xcb_get_selection_owner_reply(wm.xcb_conn, owner_cookie, nullptr);
|
||||
if (owner_reply && owner_reply->owner != XCB_WINDOW_NONE) {
|
||||
logger("system tray selection is already owned by window %u\n",
|
||||
owner_reply->owner);
|
||||
p_delete(&owner_reply);
|
||||
return false;
|
||||
}
|
||||
p_delete(&owner_reply);
|
||||
|
||||
xcb_set_selection_owner(wm.xcb_conn, tray.window, tray.selection_atom,
|
||||
XCB_CURRENT_TIME);
|
||||
|
||||
owner_cookie = xcb_get_selection_owner(wm.xcb_conn, tray.selection_atom);
|
||||
owner_reply = xcb_get_selection_owner_reply(wm.xcb_conn, owner_cookie, nullptr);
|
||||
bool success = owner_reply && owner_reply->owner == tray.window;
|
||||
p_delete(&owner_reply);
|
||||
|
||||
if (!success) logger("cannot acquire system tray selection\n");
|
||||
return success;
|
||||
}
|
||||
|
||||
static void tray_set_properties(void) {
|
||||
uint32_t orientation = TRAY_ORIENTATION_HORIZONTAL;
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, tray.window,
|
||||
_NET_SYSTEM_TRAY_ORIENTATION, XCB_ATOM_CARDINAL, 32, 1,
|
||||
&orientation);
|
||||
|
||||
const color_t *fg = &wm.color_set.tag_color;
|
||||
uint32_t colors[12] = {
|
||||
tray_color_component_u16(fg->red),
|
||||
tray_color_component_u16(fg->green),
|
||||
tray_color_component_u16(fg->blue),
|
||||
tray_color_component_u16(fg->red),
|
||||
tray_color_component_u16(fg->green),
|
||||
tray_color_component_u16(fg->blue),
|
||||
tray_color_component_u16(fg->red),
|
||||
tray_color_component_u16(fg->green),
|
||||
tray_color_component_u16(fg->blue),
|
||||
tray_color_component_u16(fg->red),
|
||||
tray_color_component_u16(fg->green),
|
||||
tray_color_component_u16(fg->blue),
|
||||
};
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, tray.window,
|
||||
_NET_SYSTEM_TRAY_COLORS, XCB_ATOM_CARDINAL, 32,
|
||||
countof(colors), colors);
|
||||
}
|
||||
|
||||
static void tray_announce_manager(void) {
|
||||
xcb_client_message_event_t ev;
|
||||
p_clear(&ev, 1);
|
||||
ev.response_type = XCB_CLIENT_MESSAGE;
|
||||
ev.window = wm.screen->root;
|
||||
ev.format = 32;
|
||||
ev.type = MANAGER;
|
||||
ev.data.data32[0] = XCB_CURRENT_TIME;
|
||||
ev.data.data32[1] = tray.selection_atom;
|
||||
ev.data.data32[2] = tray.window;
|
||||
|
||||
xcb_send_event(wm.xcb_conn, false, wm.screen->root,
|
||||
XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)&ev);
|
||||
}
|
||||
|
||||
void tray_init(void) {
|
||||
if (tray.initialized) return;
|
||||
|
||||
tray.host_monitor = wm.monitor_list;
|
||||
if (tray.host_monitor == nullptr) return;
|
||||
|
||||
tray.side_padding = 0;
|
||||
tray.spacing = 0;
|
||||
|
||||
char selection_name[64];
|
||||
snprintf(selection_name, sizeof(selection_name), "_NET_SYSTEM_TRAY_S%d",
|
||||
wm.default_screen);
|
||||
tray.selection_atom = tray_intern_atom(selection_name);
|
||||
if (tray.selection_atom == XCB_ATOM_NONE) {
|
||||
logger("cannot intern tray selection atom %s\n", selection_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tray_create_window()) return;
|
||||
if (!tray_take_selection_owner()) {
|
||||
tray_cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
tray.initialized = true;
|
||||
tray_set_properties();
|
||||
tray_announce_manager();
|
||||
tray_layout_icons((int16_t)tray.host_monitor->geometry.width);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
void tray_cleanup(void) {
|
||||
for (size_t i = 0; i < tray.icon_count; i++) {
|
||||
tray_release_icon_window(&tray.icons[i]);
|
||||
}
|
||||
p_delete(&tray.icons);
|
||||
tray.icon_count = 0;
|
||||
tray.icon_capacity = 0;
|
||||
|
||||
if (tray.selection_atom != XCB_ATOM_NONE) {
|
||||
xcb_set_selection_owner(wm.xcb_conn, XCB_WINDOW_NONE, tray.selection_atom,
|
||||
XCB_CURRENT_TIME);
|
||||
}
|
||||
|
||||
if (tray.window != XCB_WINDOW_NONE) {
|
||||
xcb_destroy_window(wm.xcb_conn, tray.window);
|
||||
}
|
||||
|
||||
tray = (tray_t){
|
||||
.selection_atom = XCB_ATOM_NONE,
|
||||
.window = XCB_WINDOW_NONE,
|
||||
};
|
||||
}
|
||||
|
||||
bool tray_handle_client_message(xcb_client_message_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
|
||||
if (ev->type != _NET_SYSTEM_TRAY_OPCODE) return false;
|
||||
if (ev->window != tray.window && ev->window != wm.screen->root) return false;
|
||||
|
||||
switch (ev->data.data32[1]) {
|
||||
case TRAY_REQUEST_DOCK:
|
||||
tray_add_icon((xcb_window_t)ev->data.data32[2]);
|
||||
break;
|
||||
case TRAY_BEGIN_MESSAGE:
|
||||
case TRAY_CANCEL_MESSAGE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
xcb_flush(wm.xcb_conn);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tray_handle_configure_request(xcb_configure_request_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
|
||||
if (ev->window == tray.window) {
|
||||
tray_layout_icons((int16_t)tray.host_monitor->geometry.width);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
return true;
|
||||
}
|
||||
|
||||
tray_icon_t *icon = tray_get_icon(ev->window);
|
||||
if (!icon) return false;
|
||||
|
||||
if ((ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) && ev->width > 0) {
|
||||
icon->natural_width = ev->width;
|
||||
}
|
||||
if ((ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT) && ev->height > 0) {
|
||||
icon->natural_height = ev->height;
|
||||
}
|
||||
|
||||
tray_request_redraw();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tray_handle_map_request(xcb_map_request_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
|
||||
if (ev->window == tray.window) {
|
||||
tray_layout_icons((int16_t)tray.host_monitor->geometry.width);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
return true;
|
||||
}
|
||||
|
||||
tray_icon_t *icon = tray_get_icon(ev->window);
|
||||
if (!icon && ev->parent != tray.window) return false;
|
||||
|
||||
if (icon) icon->mapped = true;
|
||||
xcb_map_window(wm.xcb_conn, ev->window);
|
||||
tray_layout_icons((int16_t)tray.host_monitor->geometry.width);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tray_handle_destroy_notify(xcb_destroy_notify_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
|
||||
if (ev->window == tray.window) {
|
||||
tray.window = XCB_WINDOW_NONE;
|
||||
tray.initialized = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t index = tray_find_icon_index(ev->window);
|
||||
if (index == SIZE_MAX) return false;
|
||||
|
||||
tray_remove_icon_by_index(index, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tray_handle_unmap_notify(xcb_unmap_notify_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
|
||||
tray_icon_t *icon = tray_get_icon(ev->window);
|
||||
if (!icon) return ev->window == tray.window;
|
||||
|
||||
if (icon->ignore_unmaps > 0) {
|
||||
icon->ignore_unmaps--;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t index = tray_find_icon_index(ev->window);
|
||||
if (index != SIZE_MAX) tray_remove_icon_by_index(index, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tray_handle_property_notify(xcb_property_notify_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
return tray_is_window(ev->window);
|
||||
}
|
||||
|
||||
bool tray_handle_selection_clear(xcb_selection_clear_event_t *ev) {
|
||||
if (!tray.initialized) return false;
|
||||
if (ev->owner != tray.window) return false;
|
||||
if (ev->selection != tray.selection_atom) return false;
|
||||
|
||||
monitor_t *host_monitor = tray.host_monitor;
|
||||
logger("system tray selection lost\n");
|
||||
tray_cleanup();
|
||||
|
||||
if (host_monitor != nullptr) monitor_draw_bar(host_monitor);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t tray_get_width(monitor_t *monitor) {
|
||||
if (!tray.initialized || monitor == nullptr || monitor != tray.host_monitor) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return tray_compute_width();
|
||||
}
|
||||
|
||||
void tray_place(monitor_t *monitor, int16_t right_edge) {
|
||||
if (!tray.initialized || monitor == nullptr || monitor != tray.host_monitor) {
|
||||
return;
|
||||
}
|
||||
|
||||
tray_layout_icons(right_edge);
|
||||
}
|
||||
|
||||
bool tray_is_window(xcb_window_t window) {
|
||||
if (window == XCB_WINDOW_NONE) return false;
|
||||
if (tray.window == window) return true;
|
||||
return tray_get_icon(window) != nullptr;
|
||||
}
|
||||
21
src/tray.h
Normal file
21
src/tray.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
void tray_init(void);
|
||||
void tray_cleanup(void);
|
||||
|
||||
bool tray_handle_client_message(xcb_client_message_event_t *ev);
|
||||
bool tray_handle_configure_request(xcb_configure_request_event_t *ev);
|
||||
bool tray_handle_map_request(xcb_map_request_event_t *ev);
|
||||
bool tray_handle_destroy_notify(xcb_destroy_notify_event_t *ev);
|
||||
bool tray_handle_unmap_notify(xcb_unmap_notify_event_t *ev);
|
||||
bool tray_handle_property_notify(xcb_property_notify_event_t *ev);
|
||||
bool tray_handle_selection_clear(xcb_selection_clear_event_t *ev);
|
||||
|
||||
uint16_t tray_get_width(monitor_t *monitor);
|
||||
void tray_place(monitor_t *monitor, int16_t right_edge);
|
||||
bool tray_is_window(xcb_window_t window);
|
||||
22
src/types.h
22
src/types.h
@@ -26,9 +26,11 @@ struct client_t {
|
||||
monitor_t *monitor;
|
||||
uint32_t tags;
|
||||
|
||||
area_t old_geometry;
|
||||
area_t geometry;
|
||||
color_t *border_color;
|
||||
uint16_t border_width;
|
||||
uint16_t old_border_width;
|
||||
color_t *border_color;
|
||||
|
||||
bool floating;
|
||||
bool fullscreen;
|
||||
@@ -36,9 +38,11 @@ struct client_t {
|
||||
bool minimize;
|
||||
bool sticky;
|
||||
bool urgent;
|
||||
bool skip_taskbar;
|
||||
bool size_freeze; /* 尺寸冻结窗口一定是浮动窗口 */
|
||||
|
||||
char *class, *instance;
|
||||
char *name, *icon_name, *net_name, *net_icon_name;
|
||||
char *name, *net_name;
|
||||
char *role;
|
||||
|
||||
xcb_window_t transient_for_window;
|
||||
@@ -59,8 +63,12 @@ struct monitor_t {
|
||||
|
||||
extent_in_bar_t tag_extent;
|
||||
extent_in_bar_t layout_symbol_extent;
|
||||
extent_in_bar_t status_extent;
|
||||
|
||||
xcb_window_t bar_window;
|
||||
|
||||
point_t cursor_position;
|
||||
bool position_inited;
|
||||
};
|
||||
|
||||
struct tag_t {
|
||||
@@ -94,3 +102,13 @@ typedef struct padding_t {
|
||||
uint16_t bar_y;
|
||||
uint16_t tag_x;
|
||||
} padding_t;
|
||||
|
||||
typedef struct rule_t {
|
||||
const char *role;
|
||||
const char *class;
|
||||
uint32_t tag_index;
|
||||
bool switch_to_tag;
|
||||
bool fullscreen;
|
||||
bool maximize;
|
||||
bool floating;
|
||||
} rule_t;
|
||||
|
||||
@@ -88,7 +88,16 @@ static inline void logger(const char *format, ...) {
|
||||
#define logger(format, ...) printf(format, ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifdef MIN
|
||||
#undef MIN
|
||||
#endif
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifdef MAX
|
||||
#undef MAX
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
313
src/wallpaper.c
Normal file
313
src/wallpaper.c
Normal file
@@ -0,0 +1,313 @@
|
||||
#include "wallpaper.h"
|
||||
|
||||
#include <Imlib2.h>
|
||||
#include <cairo-xcb.h>
|
||||
#include <cairo.h>
|
||||
#include <glib.h>
|
||||
#include <glibconfig.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <wordexp.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "atoms-extern.h"
|
||||
#include "base.h"
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "xwindow.h"
|
||||
|
||||
struct wallpaper_context_t {
|
||||
uint32_t interval;
|
||||
uint32_t image_count;
|
||||
char **image_path_list;
|
||||
|
||||
xcb_screen_t *screen;
|
||||
xcb_connection_t *conn;
|
||||
|
||||
area_t *monitor_geometries;
|
||||
uint32_t monitor_count;
|
||||
|
||||
xcb_pixmap_t pixmap;
|
||||
cairo_t *cr;
|
||||
|
||||
uint32_t index;
|
||||
|
||||
guint timer;
|
||||
};
|
||||
|
||||
static inline bool file_exist(const char *path) {
|
||||
if (!path || !*path) return false;
|
||||
return access(path, R_OK) == 0;
|
||||
}
|
||||
|
||||
static char **parse_wallpaper_paths(char **images, uint32_t image_count) {
|
||||
GStrvBuilder *builder = g_strv_builder_new();
|
||||
for (uint32_t i = 0; i < image_count; i++) {
|
||||
wordexp_t p;
|
||||
if (wordexp(images[i], &p, 0)) continue;
|
||||
|
||||
for (size_t i = 0; i < p.we_wordc; i++) {
|
||||
size_t len = strlen(p.we_wordv[i]) + 1;
|
||||
char *realpath = p_new(char, len);
|
||||
strncpy(realpath, p.we_wordv[i], len);
|
||||
g_strstrip(realpath);
|
||||
if (file_exist(realpath)) g_strv_builder_add(builder, realpath);
|
||||
|
||||
p_delete(&realpath);
|
||||
}
|
||||
wordfree(&p);
|
||||
}
|
||||
|
||||
return g_strv_builder_unref_to_strv(builder);
|
||||
}
|
||||
|
||||
static uint32_t *generate_wallpaper(wallpaper_context_t *context) {
|
||||
int width = context->screen->width_in_pixels;
|
||||
int height = context->screen->height_in_pixels;
|
||||
|
||||
Imlib_Image final_image = imlib_create_image(width, height);
|
||||
if (!final_image) return nullptr;
|
||||
|
||||
imlib_context_set_image(final_image);
|
||||
imlib_image_set_has_alpha(true);
|
||||
|
||||
/* 重置像素数据 */
|
||||
imlib_image_clear();
|
||||
for (uint32_t i = 0; i < context->monitor_count; i++) {
|
||||
uint32_t img_index = (context->index + i) % context->image_count;
|
||||
const char *path = context->image_path_list[img_index];
|
||||
logger("== wallpaper[%u]: %s\n", img_index, path);
|
||||
Imlib_Image wallpaper_img = imlib_load_image(path);
|
||||
if (!wallpaper_img) continue;
|
||||
|
||||
uint32_t dst_width = context->monitor_geometries[i].width;
|
||||
uint32_t dst_height = context->monitor_geometries[i].height;
|
||||
|
||||
/* 获取图片原始尺寸 */
|
||||
imlib_context_set_image(wallpaper_img);
|
||||
uint32_t img_width = imlib_image_get_width();
|
||||
uint32_t img_height = imlib_image_get_height();
|
||||
|
||||
/* 计算缩放比例,保持原始宽高比 */
|
||||
float width_ratio = (float)dst_width / img_width;
|
||||
float height_ratio = (float)dst_height / img_height;
|
||||
float scale = MAX(width_ratio, height_ratio);
|
||||
|
||||
/* 图片居中显示,计算原图绘制区域 */
|
||||
int src_width = dst_width / scale;
|
||||
int src_height = dst_height / scale;
|
||||
int src_x = (img_width - src_width) / 2;
|
||||
int src_y = (img_height - src_height) / 2;
|
||||
|
||||
/* 绘制图片到最终图像上 */
|
||||
imlib_context_set_image(final_image);
|
||||
imlib_blend_image_onto_image(wallpaper_img, true, src_x, src_y, src_width,
|
||||
src_height, context->monitor_geometries[i].x,
|
||||
context->monitor_geometries[i].y, dst_width,
|
||||
dst_height);
|
||||
|
||||
/* 释放图片资源 */
|
||||
imlib_context_set_image(wallpaper_img);
|
||||
imlib_free_image();
|
||||
}
|
||||
|
||||
/* 获取最终图像数据 */
|
||||
imlib_context_set_image(final_image);
|
||||
uint32_t *data = imlib_image_get_data_for_reading_only();
|
||||
|
||||
/* 复制数据到自己分配的内存中(原数据会在图像释放后无效) */
|
||||
int count = width * height;
|
||||
uint32_t *result = p_new(uint32_t, count);
|
||||
if (result) memcpy(result, data, count * sizeof(uint32_t));
|
||||
|
||||
/* 释放图像 */
|
||||
imlib_context_set_image(final_image);
|
||||
imlib_free_image();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
double get_time() {
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, nullptr);
|
||||
return tv.tv_sec + tv.tv_usec / 1000000.0;
|
||||
}
|
||||
|
||||
static void cairo_set_wallpaper(wallpaper_context_t *context,
|
||||
uint8_t *wallpaper_data) {
|
||||
xcb_connection_t *conn = context->conn;
|
||||
xcb_window_t root = context->screen->root;
|
||||
xcb_pixmap_t pixmap = context->pixmap;
|
||||
uint32_t width = context->screen->width_in_pixels;
|
||||
uint32_t height = context->screen->height_in_pixels;
|
||||
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
|
||||
cairo_surface_t *s = cairo_image_surface_create_for_data(
|
||||
wallpaper_data, CAIRO_FORMAT_ARGB32, width, height, stride);
|
||||
cairo_set_source_surface(context->cr, s, 0, 0);
|
||||
cairo_paint(context->cr);
|
||||
|
||||
xcb_change_window_attributes_value_list_t value_list = {
|
||||
.background_pixmap = pixmap,
|
||||
};
|
||||
xcb_change_window_attributes_aux(conn, root, XCB_CW_BACK_PIXMAP, &value_list);
|
||||
|
||||
/* 即使壁纸对应的 pixmap 未变更,也强制通知合成器壁纸已更改 */
|
||||
uint8_t mode = XCB_PROP_MODE_REPLACE;
|
||||
xcb_pixmap_t none = XCB_PIXMAP_NONE;
|
||||
xcb_atom_t atom = _XROOTPMAP_ID;
|
||||
xcb_change_property(conn, mode, root, atom, XCB_ATOM_PIXMAP, 32, 1, &none);
|
||||
xcb_change_property(conn, mode, root, atom, XCB_ATOM_PIXMAP, 32, 1, &pixmap);
|
||||
atom = ESETROOT_PMAP_ID;
|
||||
xcb_change_property(conn, mode, root, atom, XCB_ATOM_PIXMAP, 32, 1, &none);
|
||||
xcb_change_property(conn, mode, root, atom, XCB_ATOM_PIXMAP, 32, 1, &pixmap);
|
||||
|
||||
xcb_clear_area(conn, 0, root, 0, 0, 0, 0);
|
||||
xcb_flush(conn);
|
||||
cairo_surface_destroy(s);
|
||||
}
|
||||
|
||||
static void set_wallpaper(wallpaper_context_t *context) {
|
||||
double start = get_time();
|
||||
uint32_t *wallpaper = generate_wallpaper(context);
|
||||
logger("== generate wallpaper cost: %lf, %p\n", get_time() - start,
|
||||
wallpaper);
|
||||
if (!wallpaper) return;
|
||||
|
||||
uint8_t *data = (uint8_t *)wallpaper;
|
||||
|
||||
cairo_set_wallpaper(context, data);
|
||||
|
||||
p_delete(&wallpaper);
|
||||
|
||||
context->index += context->monitor_count;
|
||||
}
|
||||
|
||||
static gboolean update_wallpaper(gpointer user_data) {
|
||||
if (!user_data) return FALSE;
|
||||
|
||||
wallpaper_context_t *wc = user_data;
|
||||
set_wallpaper(wc);
|
||||
|
||||
/* 屏幕比壁纸多则不动态切换 */
|
||||
if (wc->monitor_count >= wc->image_count) return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static bool prepare_wallpaper_surface(wallpaper_context_t *context) {
|
||||
uint16_t width = context->screen->width_in_pixels;
|
||||
uint16_t height = context->screen->height_in_pixels;
|
||||
xcb_window_t root = context->screen->root;
|
||||
xcb_connection_t *conn = context->conn;
|
||||
xcb_pixmap_t pixmap = context->pixmap;
|
||||
|
||||
visual_t *visual = xwindow_get_xcb_visual(false);
|
||||
|
||||
if (pixmap != XCB_PIXMAP_NONE) {
|
||||
xcb_kill_client(conn, pixmap);
|
||||
xcb_free_pixmap(conn, pixmap);
|
||||
}
|
||||
|
||||
pixmap = xcb_generate_id(conn);
|
||||
xcb_create_pixmap(conn, visual->depth, pixmap, root, width, height);
|
||||
xcb_gcontext_t gc = xcb_generate_id(conn);
|
||||
xcb_create_gc(conn, gc, pixmap, 0, nullptr);
|
||||
xcb_change_window_attributes_value_list_t value_list = {
|
||||
.background_pixmap = pixmap,
|
||||
};
|
||||
xcb_change_window_attributes_aux(conn, root, XCB_CW_BACK_PIXMAP, &value_list);
|
||||
|
||||
/* 兼容合成器,不设置若开启合成器壁纸会不显示 */
|
||||
uint8_t mode = XCB_PROP_MODE_REPLACE;
|
||||
xcb_atom_t atom = _XROOTPMAP_ID;
|
||||
xcb_change_property(conn, mode, root, atom, XCB_ATOM_PIXMAP, 32, 1, &pixmap);
|
||||
atom = ESETROOT_PMAP_ID;
|
||||
xcb_change_property(conn, mode, root, atom, XCB_ATOM_PIXMAP, 32, 1, &pixmap);
|
||||
|
||||
xcb_clear_area(conn, 0, root, 0, 0, width, height);
|
||||
xcb_free_gc(conn, gc);
|
||||
xcb_flush(conn);
|
||||
|
||||
context->pixmap = pixmap;
|
||||
|
||||
if (context->cr) cairo_destroy(context->cr);
|
||||
cairo_surface_t *surface =
|
||||
cairo_xcb_surface_create(conn, pixmap, visual->visual, width, height);
|
||||
p_delete(&visual);
|
||||
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_surface_destroy(surface);
|
||||
return false;
|
||||
}
|
||||
|
||||
cairo_t *cr = cairo_create(surface);
|
||||
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_destroy(cr);
|
||||
cairo_surface_destroy(surface);
|
||||
return false;
|
||||
}
|
||||
|
||||
context->cr = cr;
|
||||
|
||||
cairo_surface_destroy(surface);
|
||||
cairo_set_source_rgba(cr, 0, 0, 0, 1);
|
||||
cairo_paint(cr);
|
||||
xcb_clear_area(conn, 0, root, 0, 0, 0, 0);
|
||||
xcb_flush(context->conn);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
wallpaper_context_t *wallpaper_init(wallpaper_config_t config) {
|
||||
if (!config.path_count || !config.paths) return nullptr;
|
||||
if (!config.monitors) return nullptr;
|
||||
|
||||
char **images = parse_wallpaper_paths(config.paths, config.path_count);
|
||||
if (!images || !g_strv_length(images)) return nullptr;
|
||||
|
||||
wallpaper_context_t *ctx = p_new(wallpaper_context_t, 1);
|
||||
ctx->interval = config.interval;
|
||||
ctx->image_path_list = images;
|
||||
ctx->image_count = g_strv_length(images);
|
||||
|
||||
ctx->conn = config.conn;
|
||||
ctx->screen = config.screen;
|
||||
|
||||
{
|
||||
uint32_t i = 0;
|
||||
monitor_t *m = nullptr;
|
||||
for (m = config.monitors; m; m = m->next) ctx->monitor_count++;
|
||||
|
||||
ctx->monitor_geometries = p_new(area_t, ctx->monitor_count);
|
||||
for (m = config.monitors; m; m = m->next) {
|
||||
ctx->monitor_geometries[i++] = m->geometry;
|
||||
}
|
||||
}
|
||||
|
||||
if (!prepare_wallpaper_surface(ctx)) {
|
||||
wallpaper_clean(ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!update_wallpaper(ctx) || !ctx->interval) {
|
||||
wallpaper_clean(ctx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ctx->timer = g_timeout_add_seconds(ctx->interval, update_wallpaper, ctx);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void wallpaper_clean(wallpaper_context_t *context) {
|
||||
if (!context) return;
|
||||
|
||||
if (context->timer) g_source_remove(context->timer);
|
||||
if (context->cr) cairo_destroy(context->cr);
|
||||
p_delete(&context->monitor_geometries);
|
||||
if (context->image_path_list) g_strfreev(context->image_path_list);
|
||||
|
||||
p_delete(&context);
|
||||
}
|
||||
19
src/wallpaper.h
Normal file
19
src/wallpaper.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <xcb/xproto.h>
|
||||
#include "types.h"
|
||||
|
||||
typedef struct wallpaper_context_t wallpaper_context_t;
|
||||
|
||||
typedef struct wallpaper_config_t {
|
||||
uint32_t interval;
|
||||
uint32_t path_count;
|
||||
char **paths;
|
||||
xcb_screen_t *screen;
|
||||
xcb_connection_t *conn;
|
||||
monitor_t *monitors;
|
||||
} wallpaper_config_t;
|
||||
|
||||
wallpaper_context_t *wallpaper_init(wallpaper_config_t config);
|
||||
void wallpaper_clean(wallpaper_context_t *context);
|
||||
349
src/wm.c
349
src/wm.c
@@ -5,6 +5,7 @@
|
||||
#include <glibconfig.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@@ -21,14 +22,21 @@
|
||||
#include "atoms-extern.h"
|
||||
#include "atoms.h"
|
||||
#include "backtrace.h"
|
||||
#include "base.h"
|
||||
#include "buffer.h"
|
||||
#include "client.h"
|
||||
#include "color.h"
|
||||
#include "config.h"
|
||||
#include "default_config.h"
|
||||
#include "event.h"
|
||||
#include "image.h"
|
||||
#include "monitor.h"
|
||||
#include "status.h"
|
||||
#include "text.h"
|
||||
#include "tray.h"
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
#include "wallpaper.h"
|
||||
#include "xcursor.h"
|
||||
#include "xkb.h"
|
||||
#include "xres.h"
|
||||
@@ -41,18 +49,21 @@ static void wm_detect_monitor(void);
|
||||
static void wm_scan_clients(void);
|
||||
static void wm_clean(void);
|
||||
static void wm_setup(void);
|
||||
static void wm_init_wallpaper(void);
|
||||
static void wm_get_xres_config(void);
|
||||
static void wm_init_color_set(void);
|
||||
static void wm_setup_keybindings(void);
|
||||
static void wm_update_status(status_t *status);
|
||||
static void wm_run_autostart(const char *const commands[]);
|
||||
static void run_once(const char *command);
|
||||
static bool command_already_running(const char *command);
|
||||
static gboolean clear_ignore_enter_notify(gpointer data);
|
||||
|
||||
wm_t wm;
|
||||
|
||||
/** argv used to run wm */
|
||||
static char **cmd_argv;
|
||||
|
||||
/** A pipe that is used to asynchronously handle SIGCHLD */
|
||||
static int sigchld_pipe[2];
|
||||
|
||||
static gboolean restart_on_signal(gpointer data) {
|
||||
wm_restart();
|
||||
return TRUE;
|
||||
@@ -69,15 +80,40 @@ static void signal_fatal(int signal_number) {
|
||||
fatal("signal %d, dumping backtrace\n%s", signal_number, buffer.s);
|
||||
}
|
||||
|
||||
/* Signal handler for SIGCHLD. Causes reap_children() to be called. */
|
||||
static void signal_child(int signum) {
|
||||
assert(signum == SIGCHLD);
|
||||
int res = write(sigchld_pipe[1], " ", 1);
|
||||
(void)res;
|
||||
assert(res == 1);
|
||||
static guint sources[3] = {0};
|
||||
|
||||
static gboolean clear_ignore_enter_notify(gpointer data) {
|
||||
wm.ignore_enter_notify = false;
|
||||
wm.ignored_enter_notify_point = (point_t){0};
|
||||
wm.clear_ignore_enter_notify_source = 0;
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
static guint sources[3] = {0};
|
||||
void wm_ignore_enter_notify_at_point(point_t point) {
|
||||
wm.ignore_enter_notify = true;
|
||||
wm.ignored_enter_notify_point = point;
|
||||
if (wm.clear_ignore_enter_notify_source) {
|
||||
g_source_remove(wm.clear_ignore_enter_notify_source);
|
||||
}
|
||||
wm.clear_ignore_enter_notify_source =
|
||||
g_idle_add(clear_ignore_enter_notify, nullptr);
|
||||
}
|
||||
|
||||
bool wm_should_ignore_enter_notify(const xcb_enter_notify_event_t *ev) {
|
||||
if (!wm.ignore_enter_notify) return false;
|
||||
if (ev->root_x != wm.ignored_enter_notify_point.x ||
|
||||
ev->root_y != wm.ignored_enter_notify_point.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wm.clear_ignore_enter_notify_source) {
|
||||
g_source_remove(wm.clear_ignore_enter_notify_source);
|
||||
wm.clear_ignore_enter_notify_source = 0;
|
||||
}
|
||||
wm.ignore_enter_notify = false;
|
||||
wm.ignored_enter_notify_point = (point_t){0};
|
||||
return true;
|
||||
}
|
||||
|
||||
void wm_setup_signal(void) {
|
||||
sources[0] = g_unix_signal_add(SIGINT, exit_on_signal, nullptr);
|
||||
@@ -92,10 +128,6 @@ void wm_setup_signal(void) {
|
||||
sigaction(SIGILL, &sa, 0);
|
||||
sigaction(SIGSEGV, &sa, 0);
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
sa.sa_handler = signal_child;
|
||||
sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
|
||||
sigaction(SIGCHLD, &sa, 0);
|
||||
}
|
||||
|
||||
void wm_check_other_wm(void) {
|
||||
@@ -142,6 +174,67 @@ void wm_check_xcb_extensions(void) {
|
||||
wm.have_randr = query && query->present;
|
||||
}
|
||||
|
||||
static inline int32_t right(area_t a) {
|
||||
return (int32_t)a.x + (int32_t)a.width;
|
||||
}
|
||||
static inline int32_t bottom(area_t a) {
|
||||
return (int32_t)a.y + (int32_t)a.height;
|
||||
}
|
||||
static inline bool fully_contains(area_t outer, area_t inner) {
|
||||
return inner.x >= outer.x && inner.y >= outer.y &&
|
||||
right(inner) <= right(outer) && bottom(inner) <= bottom(outer);
|
||||
}
|
||||
static inline void monitor_remove_duplication(monitor_t **monitor_list) {
|
||||
if (!monitor_list || !*monitor_list) return;
|
||||
|
||||
int32_t count = 0;
|
||||
for (monitor_t *m = *monitor_list; m; m = m->next) count++;
|
||||
if (count <= 1) return;
|
||||
|
||||
monitor_t **nodes = p_new(monitor_t *, count);
|
||||
bool *remove = p_new(bool, count);
|
||||
|
||||
size_t index = 0;
|
||||
for (monitor_t *m = *monitor_list; m; m = m->next) nodes[index++] = m;
|
||||
|
||||
for (int32_t i = count - 1; i >= 0; i--) {
|
||||
if (remove[i]) continue;
|
||||
for (int32_t j = i - 1; j >= 0; j--) {
|
||||
if (remove[j]) continue;
|
||||
if (fully_contains(nodes[j]->geometry, nodes[i]->geometry)) {
|
||||
remove[i] = true;
|
||||
} else if (fully_contains(nodes[i]->geometry, nodes[j]->geometry)) {
|
||||
remove[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
monitor_t *head = nullptr;
|
||||
monitor_t *tail = nullptr;
|
||||
for (int32_t i = 0; i < count; i++) {
|
||||
monitor_t *node = nodes[i];
|
||||
if (remove[i]) {
|
||||
p_delete(&node->name);
|
||||
p_delete(&node);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tail) {
|
||||
tail->next = node;
|
||||
} else {
|
||||
head = node;
|
||||
}
|
||||
node->next = nullptr;
|
||||
tail = node;
|
||||
}
|
||||
|
||||
if (tail) tail->next = nullptr;
|
||||
*monitor_list = head;
|
||||
|
||||
p_delete(&nodes);
|
||||
p_delete(&remove);
|
||||
}
|
||||
|
||||
static bool wm_detect_monitor_by_randr(void) {
|
||||
xcb_randr_get_monitors_cookie_t monitors_cookie =
|
||||
xcb_randr_get_monitors(wm.xcb_conn, wm.screen->root, 1);
|
||||
@@ -189,6 +282,8 @@ static bool wm_detect_monitor_by_randr(void) {
|
||||
|
||||
p_delete(&monitors_reply);
|
||||
|
||||
monitor_remove_duplication(&wm.monitor_list);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -237,6 +332,8 @@ static bool wm_detect_monitor_by_xinerama(void) {
|
||||
}
|
||||
p_delete(&screens_reply);
|
||||
|
||||
monitor_remove_duplication(&wm.monitor_list);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -256,16 +353,102 @@ void wm_detect_monitor(void) {
|
||||
wm.current_monitor = monitor;
|
||||
}
|
||||
|
||||
void wm_scan_clients(void) {}
|
||||
typedef struct window_list_t {
|
||||
xcb_window_t *normal_list;
|
||||
xcb_window_t *transient_list;
|
||||
uint32_t normal_count;
|
||||
uint32_t transient_count;
|
||||
} window_list_t;
|
||||
void wm_scan_clients(void) {
|
||||
xcb_query_tree_cookie_t cookie = xcb_query_tree(wm.xcb_conn, wm.screen->root);
|
||||
xcb_query_tree_reply_t *tree_reply =
|
||||
xcb_query_tree_reply(wm.xcb_conn, cookie, nullptr);
|
||||
if (!tree_reply) return;
|
||||
|
||||
xcb_window_t *list = xcb_query_tree_children(tree_reply);
|
||||
int len = xcb_query_tree_children_length(tree_reply);
|
||||
if (!len) {
|
||||
p_delete(&tree_reply);
|
||||
return;
|
||||
}
|
||||
|
||||
xcb_get_window_attributes_reply_t *wa_reply = nullptr;
|
||||
xcb_get_geometry_reply_t *geo_reply = nullptr;
|
||||
for (int i = 0; i < len; i++) {
|
||||
xcb_window_t window = list[i];
|
||||
wa_reply = xwindow_get_attributes_reply(window);
|
||||
if (!wa_reply) continue;
|
||||
|
||||
uint8_t override_redirect = wa_reply->override_redirect;
|
||||
uint8_t map_state = wa_reply->map_state;
|
||||
p_delete(&wa_reply);
|
||||
|
||||
if (override_redirect || xwindow_get_transient_for(window)) continue;
|
||||
|
||||
geo_reply = xwindow_get_geometry_reply(window);
|
||||
if (!geo_reply) continue;
|
||||
|
||||
if (map_state == XCB_MAP_STATE_VIEWABLE ||
|
||||
xwindow_get_state(window) == XCB_ICCCM_WM_STATE_ICONIC) {
|
||||
client_manage(window, geo_reply);
|
||||
}
|
||||
p_delete(&geo_reply);
|
||||
}
|
||||
for (int i = 0; i < len; i++) {
|
||||
xcb_window_t window = list[i];
|
||||
wa_reply = xwindow_get_attributes_reply(window);
|
||||
if (!wa_reply) continue;
|
||||
|
||||
uint8_t map_state = wa_reply->map_state;
|
||||
p_delete(&wa_reply);
|
||||
|
||||
geo_reply = xwindow_get_geometry_reply(window);
|
||||
if (!geo_reply) continue;
|
||||
|
||||
if (xwindow_get_transient_for(window) &&
|
||||
(map_state == XCB_MAP_STATE_VIEWABLE ||
|
||||
xwindow_get_state(window) == XCB_ICCCM_WM_STATE_ICONIC)) {
|
||||
client_manage(window, geo_reply);
|
||||
}
|
||||
p_delete(&geo_reply);
|
||||
}
|
||||
p_delete(&tree_reply);
|
||||
}
|
||||
|
||||
void wm_clean(void) {
|
||||
wallpaper_clean(wm.wallpaper);
|
||||
wm.wallpaper = nullptr;
|
||||
|
||||
clean_status();
|
||||
text_clean_pango_layout();
|
||||
image_cache_clean();
|
||||
tray_cleanup();
|
||||
|
||||
{
|
||||
client_t *c = wm.client_stack_list;
|
||||
while (c) {
|
||||
client_t *next_client = c->stack_next;
|
||||
client_unmanage(c, false);
|
||||
c = next_client;
|
||||
}
|
||||
}
|
||||
|
||||
monitor_clean(wm.monitor_list);
|
||||
|
||||
for (int i = 0; i < countof(sources); i++) {
|
||||
guint source_id = sources[i];
|
||||
if (source_id) g_source_remove(source_id);
|
||||
}
|
||||
if (wm.clear_ignore_enter_notify_source) {
|
||||
g_source_remove(wm.clear_ignore_enter_notify_source);
|
||||
wm.clear_ignore_enter_notify_source = 0;
|
||||
}
|
||||
wm.ignore_enter_notify = false;
|
||||
wm.ignored_enter_notify_point = (point_t){0};
|
||||
|
||||
xcb_delete_property(wm.xcb_conn, wm.screen->root, _NET_ACTIVE_WINDOW);
|
||||
xcb_delete_property(wm.xcb_conn, wm.screen->root, _NET_SUPPORTING_WM_CHECK);
|
||||
xcb_aux_sync(wm.xcb_conn);
|
||||
|
||||
xcb_destroy_window(wm.xcb_conn, wm.wm_check_window);
|
||||
wm.wm_check_window = XCB_WINDOW_NONE;
|
||||
@@ -279,9 +462,15 @@ void wm_clean(void) {
|
||||
}
|
||||
|
||||
static void wm_setup(void) {
|
||||
wm.config = init_config();
|
||||
if (!wm.config) fatal("cannot init config");
|
||||
|
||||
wm.layout_list = layout_list;
|
||||
wm.layout_count = (uint16_t)countof(layout_list);
|
||||
|
||||
wm.rules = rules;
|
||||
wm.rules_count = countof(rules);
|
||||
|
||||
wm_get_xres_config();
|
||||
wm_init_color_set();
|
||||
|
||||
@@ -299,8 +488,9 @@ static void wm_setup(void) {
|
||||
monitor_draw_bar(m);
|
||||
}
|
||||
|
||||
uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
|
||||
uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR | XCB_CW_BACK_PIXEL;
|
||||
xcb_params_cw_t params = {
|
||||
.back_pixel = wm.screen->black_pixel,
|
||||
.cursor = xcursor_get_xcb_cursor(cursor_normal),
|
||||
.event_mask =
|
||||
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_KEY_PRESS,
|
||||
@@ -309,6 +499,7 @@ static void wm_setup(void) {
|
||||
|
||||
wm_setup_keybindings();
|
||||
atoms_init(wm.xcb_conn);
|
||||
tray_init();
|
||||
|
||||
{
|
||||
wm.wm_check_window = xcb_generate_id(wm.xcb_conn);
|
||||
@@ -334,6 +525,18 @@ static void wm_setup(void) {
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
static void wm_init_wallpaper(void) {
|
||||
wallpaper_config_t config = {
|
||||
.interval = wallpaper_interval,
|
||||
.path_count = countof(wallpapers),
|
||||
.paths = (char **)wallpapers,
|
||||
.screen = wm.screen,
|
||||
.conn = wm.xcb_conn,
|
||||
.monitors = wm.monitor_list,
|
||||
};
|
||||
wm.wallpaper = wallpaper_init(config);
|
||||
}
|
||||
|
||||
static void wm_get_xres_config(void) {
|
||||
xres_init_xrm_db();
|
||||
|
||||
@@ -376,6 +579,42 @@ void wm_setup_keybindings(void) {
|
||||
xwindow_grab_keys(wm.screen->root, key_list, countof(key_list));
|
||||
}
|
||||
|
||||
void wm_update_status(status_t *status) {
|
||||
wm.status = status;
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) monitor_draw_bar(m);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 允许自启动程序
|
||||
* @param commands 命令字符串数组(以 nullptr 结尾)
|
||||
*/
|
||||
void wm_run_autostart(const char *const commands[]) {
|
||||
for (int i = 0; commands[i]; i++) run_once(commands[i]);
|
||||
}
|
||||
|
||||
void run_once(const char *command) {
|
||||
if (command_already_running(command)) return;
|
||||
g_spawn_command_line_async(command, nullptr);
|
||||
}
|
||||
|
||||
bool command_already_running(const char *command) {
|
||||
char shell_cmd[1024];
|
||||
const char *user = getenv("USER");
|
||||
snprintf(shell_cmd, sizeof(shell_cmd), "pgrep -u %s -fx '%s'", user, command);
|
||||
|
||||
char *output = nullptr;
|
||||
char *error = nullptr;
|
||||
g_spawn_command_line_sync(shell_cmd, &output, &error, nullptr, nullptr);
|
||||
|
||||
bool result =
|
||||
(error == nullptr || !strlen(error)) && (output && strlen(output));
|
||||
p_delete(&output);
|
||||
p_delete(&error);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wm_restart(void) {
|
||||
wm.need_restart = true;
|
||||
if (g_main_loop_is_running(wm.loop)) {
|
||||
@@ -409,6 +648,79 @@ void wm_restack_clients(void) {
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
void wm_set_current_monitor(monitor_t *monitor, bool restore_cursor) {
|
||||
if (!monitor || wm.current_monitor == monitor) return;
|
||||
|
||||
point_t point = xcursor_query_pointer_position();
|
||||
monitor_t *point_monitor = wm_get_monitor_by_point(point);
|
||||
if (point_monitor == wm.current_monitor) {
|
||||
point_monitor->cursor_position = point;
|
||||
point_monitor->position_inited = true;
|
||||
}
|
||||
|
||||
wm.current_monitor = monitor;
|
||||
if (restore_cursor) monitor_restore_cursor_point(monitor);
|
||||
}
|
||||
|
||||
static inline int intersect(area_t area, monitor_t *m) {
|
||||
return MAX(0, MIN(area.x + area.width, m->geometry.x + m->geometry.width) -
|
||||
MAX(area.x, m->geometry.x)) *
|
||||
MAX(0, MIN(area.y + area.height, m->geometry.y + m->geometry.height) -
|
||||
MAX(area.y, m->geometry.y));
|
||||
}
|
||||
|
||||
monitor_t *__attribute__((returns_nonnull)) wm_get_monitor_by_area(
|
||||
area_t area) {
|
||||
if (wm.current_monitor == nullptr) {
|
||||
fatal("wm_get_monitor_by_area: current monitor is nullptr");
|
||||
}
|
||||
|
||||
monitor_t *monitor = wm.current_monitor;
|
||||
int temp_area, max_area = 0;
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
if ((temp_area = intersect(area, m)) > max_area) {
|
||||
max_area = temp_area;
|
||||
monitor = m;
|
||||
}
|
||||
}
|
||||
|
||||
return monitor;
|
||||
}
|
||||
|
||||
monitor_t *__attribute__((returns_nonnull)) wm_get_monitor_by_point(
|
||||
point_t point) {
|
||||
area_t area = {.x = point.x, .y = point.y, .width = 1, .height = 1};
|
||||
return wm_get_monitor_by_area(area);
|
||||
}
|
||||
|
||||
monitor_t *wm_get_monitor_by_window(xcb_window_t window) {
|
||||
if (window == wm.screen->root) {
|
||||
xcb_query_pointer_cookie_t cookie = xcb_query_pointer(wm.xcb_conn, window);
|
||||
xcb_query_pointer_reply_t *reply =
|
||||
xcb_query_pointer_reply(wm.xcb_conn, cookie, nullptr);
|
||||
if (reply) {
|
||||
area_t area = {reply->root_x, reply->root_y, 1, 1};
|
||||
p_delete(&reply);
|
||||
return wm_get_monitor_by_area(area);
|
||||
}
|
||||
}
|
||||
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
if (window == m->bar_window) return m;
|
||||
}
|
||||
|
||||
client_t *c = client_get_by_window(window);
|
||||
if (c) return c->monitor;
|
||||
|
||||
return wm.current_monitor;
|
||||
}
|
||||
|
||||
monitor_t *wm_get_next_monitor(monitor_t *monitor) {
|
||||
monitor_t *next_monitor = monitor->next;
|
||||
if (next_monitor == nullptr) next_monitor = wm.monitor_list;
|
||||
return next_monitor;
|
||||
}
|
||||
|
||||
static void debug_show_monitor_list(void) {
|
||||
logger("\n========================== monitors ==========================\n");
|
||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||
@@ -436,12 +748,17 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
wm_detect_monitor();
|
||||
wm_setup();
|
||||
wm_init_wallpaper();
|
||||
setup_event_loop();
|
||||
wm_scan_clients();
|
||||
|
||||
debug_show_monitor_list();
|
||||
|
||||
wm_run_autostart(autostart_list);
|
||||
|
||||
if (wm.loop == nullptr) {
|
||||
wm.loop = g_main_loop_new(nullptr, FALSE);
|
||||
init_status(g_main_loop_get_context(wm.loop), wm_update_status);
|
||||
g_main_loop_run(wm.loop);
|
||||
}
|
||||
g_main_loop_unref(wm.loop);
|
||||
|
||||
22
src/wm.h
22
src/wm.h
@@ -5,7 +5,12 @@
|
||||
#include <xcb/xcb_xrm.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
|
||||
#include "base.h"
|
||||
#include "status.h"
|
||||
#include "types.h"
|
||||
#include "wallpaper.h"
|
||||
|
||||
typedef struct config_t config_t;
|
||||
|
||||
typedef struct wm_t {
|
||||
padding_t padding;
|
||||
@@ -15,17 +20,24 @@ typedef struct wm_t {
|
||||
|
||||
bool need_restart;
|
||||
GMainLoop *loop;
|
||||
status_t *status;
|
||||
const config_t *config;
|
||||
|
||||
client_t *client_list;
|
||||
client_t *client_stack_list;
|
||||
client_t *client_focused;
|
||||
monitor_t *monitor_list;
|
||||
monitor_t *current_monitor;
|
||||
bool ignore_enter_notify;
|
||||
point_t ignored_enter_notify_point;
|
||||
guint clear_ignore_enter_notify_source;
|
||||
const layout_t *layout_list;
|
||||
|
||||
char *font_family;
|
||||
uint32_t font_size;
|
||||
uint32_t dpi;
|
||||
const rule_t *rules;
|
||||
uint32_t rules_count;
|
||||
|
||||
xcb_connection_t *xcb_conn;
|
||||
xcb_screen_t *screen;
|
||||
@@ -44,6 +56,8 @@ typedef struct wm_t {
|
||||
struct xkb_context *xkb_ctx;
|
||||
struct xkb_state *xkb_state;
|
||||
|
||||
wallpaper_context_t *wallpaper;
|
||||
|
||||
color_set_t color_set;
|
||||
} wm_t;
|
||||
|
||||
@@ -52,3 +66,11 @@ extern wm_t wm;
|
||||
void wm_restart(void);
|
||||
void wm_quit(void);
|
||||
void wm_restack_clients(void);
|
||||
void wm_ignore_enter_notify_at_point(point_t point);
|
||||
bool wm_should_ignore_enter_notify(const xcb_enter_notify_event_t *ev);
|
||||
void wm_set_current_monitor(monitor_t *monitor, bool restore_cursor);
|
||||
monitor_t *__attribute__((returns_nonnull)) wm_get_monitor_by_area(area_t area);
|
||||
monitor_t *__attribute__((returns_nonnull)) wm_get_monitor_by_point(
|
||||
point_t point);
|
||||
monitor_t *wm_get_monitor_by_window(xcb_window_t window);
|
||||
monitor_t *wm_get_next_monitor(monitor_t *monitor);
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "xcursor.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <xcb/xcb.h>
|
||||
#include <xcb/xcb_cursor.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "base.h"
|
||||
#include "utils.h"
|
||||
#include "wm.h"
|
||||
|
||||
@@ -126,3 +129,20 @@ void xcursor_clean(void) {
|
||||
}
|
||||
if (cursor_ctx) xcb_cursor_context_free(cursor_ctx);
|
||||
}
|
||||
|
||||
point_t xcursor_query_pointer_position(void) {
|
||||
xcb_query_pointer_cookie_t cookie =
|
||||
xcb_query_pointer(wm.xcb_conn, wm.screen->root);
|
||||
xcb_query_pointer_reply_t *reply =
|
||||
xcb_query_pointer_reply(wm.xcb_conn, cookie, nullptr);
|
||||
point_t pos = {.x = reply->root_x, .y = reply->root_y};
|
||||
p_delete(&reply);
|
||||
return pos;
|
||||
}
|
||||
|
||||
void xcursor_set_pointer_position(point_t point) {
|
||||
xcb_window_t window = XCB_WINDOW_NONE;
|
||||
xcb_window_t root = wm.screen->root;
|
||||
xcb_warp_pointer(wm.xcb_conn, window, root, 0, 0, 1, 1, point.x, point.y);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <X11/cursorfont.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "base.h"
|
||||
|
||||
typedef enum cursor_t {
|
||||
cursor_normal = XC_left_ptr,
|
||||
cursor_resize = XC_bottom_right_corner,
|
||||
@@ -11,3 +13,5 @@ typedef enum cursor_t {
|
||||
|
||||
xcb_cursor_t xcursor_get_xcb_cursor(cursor_t cursor);
|
||||
void xcursor_clean(void);
|
||||
point_t xcursor_query_pointer_position(void);
|
||||
void xcursor_set_pointer_position(point_t point);
|
||||
|
||||
@@ -136,7 +136,7 @@ bool xwindow_send_event(xcb_window_t window, xcb_atom_t atom) {
|
||||
bool exist = false;
|
||||
|
||||
xcb_get_property_cookie_t cookie =
|
||||
xcb_icccm_get_wm_protocols(wm.xcb_conn, window, atom);
|
||||
xcb_icccm_get_wm_protocols(wm.xcb_conn, window, WM_PROTOCOLS);
|
||||
xcb_icccm_get_wm_protocols_reply_t reply;
|
||||
if (xcb_icccm_get_wm_protocols_reply(wm.xcb_conn, cookie, &reply, nullptr)) {
|
||||
for (uint32_t i = 0; !exist && i < reply.atoms_len; i++) {
|
||||
@@ -154,6 +154,9 @@ bool xwindow_send_event(xcb_window_t window, xcb_atom_t atom) {
|
||||
ev.type = WM_PROTOCOLS;
|
||||
ev.data.data32[0] = atom;
|
||||
ev.data.data32[1] = XCB_CURRENT_TIME;
|
||||
|
||||
xcb_connection_t *conn = wm.xcb_conn;
|
||||
xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)&ev);
|
||||
}
|
||||
|
||||
return exist;
|
||||
@@ -205,3 +208,81 @@ void xwindow_get_text_property(xcb_window_t window, xcb_atom_t property,
|
||||
|
||||
p_delete(&reply);
|
||||
}
|
||||
|
||||
void xwindow_set_wm_desktop(xcb_window_t window, uint32_t desktop) {
|
||||
xcb_change_property(wm.xcb_conn, XCB_PROP_MODE_REPLACE, window,
|
||||
_NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 32, 1, &desktop);
|
||||
}
|
||||
|
||||
bool xwindow_get_wm_desktop(xcb_window_t window, uint32_t *desktop) {
|
||||
xcb_get_property_cookie_t cookie = xcb_get_property_unchecked(
|
||||
wm.xcb_conn, false, window, _NET_WM_DESKTOP, XCB_ATOM_CARDINAL, 0, 1);
|
||||
xcb_get_property_reply_t *reply =
|
||||
xcb_get_property_reply(wm.xcb_conn, cookie, nullptr);
|
||||
if (!reply) return false;
|
||||
|
||||
bool success = reply->type == XCB_ATOM_CARDINAL && reply->format == 32;
|
||||
if (success) *desktop = *(uint32_t *)xcb_get_property_value(reply);
|
||||
|
||||
p_delete(&reply);
|
||||
return success;
|
||||
}
|
||||
|
||||
void xwindow_kill_window(xcb_window_t window) {
|
||||
xcb_grab_server(wm.xcb_conn);
|
||||
xcb_set_close_down_mode(wm.xcb_conn, XCB_CLOSE_DOWN_DESTROY_ALL);
|
||||
xcb_kill_client(wm.xcb_conn, window);
|
||||
xcb_flush(wm.xcb_conn);
|
||||
xcb_ungrab_server(wm.xcb_conn);
|
||||
}
|
||||
|
||||
xcb_window_t xwindow_get_transient_for(xcb_window_t window) {
|
||||
xcb_connection_t *conn = wm.xcb_conn;
|
||||
xcb_window_t t_window = XCB_WINDOW_NONE;
|
||||
xcb_get_property_cookie_t cookie =
|
||||
xcb_icccm_get_wm_transient_for(conn, window);
|
||||
xcb_icccm_get_wm_transient_for_reply(conn, cookie, &t_window, nullptr);
|
||||
return t_window;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取窗口的状态
|
||||
* @param window 目标窗口
|
||||
* @returns 成功返回 xcb_icccm_wm_state_t 枚举类型,失败返回 -1
|
||||
*/
|
||||
int32_t xwindow_get_state(xcb_window_t window) {
|
||||
xcb_connection_t *conn = wm.xcb_conn;
|
||||
xcb_icccm_wm_hints_t hints;
|
||||
xcb_get_property_cookie_t cookie = xcb_icccm_get_wm_hints(conn, window);
|
||||
if (!xcb_icccm_get_wm_hints_reply(conn, cookie, &hints, nullptr)) return -1;
|
||||
return hints.initial_state;
|
||||
}
|
||||
|
||||
void xwindow_set_state(xcb_window_t window, xcb_icccm_wm_state_t state) {
|
||||
xcb_icccm_wm_hints_t hints;
|
||||
xcb_icccm_wm_hints_set_none(&hints);
|
||||
switch (state) {
|
||||
case XCB_ICCCM_WM_STATE_WITHDRAWN:
|
||||
xcb_icccm_wm_hints_set_withdrawn(&hints);
|
||||
break;
|
||||
case XCB_ICCCM_WM_STATE_NORMAL:
|
||||
xcb_icccm_wm_hints_set_normal(&hints);
|
||||
break;
|
||||
case XCB_ICCCM_WM_STATE_ICONIC:
|
||||
xcb_icccm_wm_hints_set_iconic(&hints);
|
||||
break;
|
||||
}
|
||||
xcb_icccm_set_wm_hints(wm.xcb_conn, window, &hints);
|
||||
}
|
||||
|
||||
xcb_get_geometry_reply_t *xwindow_get_geometry_reply(xcb_window_t window) {
|
||||
xcb_get_geometry_cookie_t cookie = xcb_get_geometry(wm.xcb_conn, window);
|
||||
return xcb_get_geometry_reply(wm.xcb_conn, cookie, nullptr);
|
||||
}
|
||||
|
||||
xcb_get_window_attributes_reply_t *xwindow_get_attributes_reply(
|
||||
xcb_window_t window) {
|
||||
xcb_get_window_attributes_cookie_t wa_cookie =
|
||||
xcb_get_window_attributes(wm.xcb_conn, window);
|
||||
return xcb_get_window_attributes_reply(wm.xcb_conn, wa_cookie, nullptr);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <xcb/xcb_icccm.h>
|
||||
#include <xcb/xproto.h>
|
||||
|
||||
#include "action.h"
|
||||
@@ -20,6 +21,16 @@ bool xwindow_send_event(xcb_window_t window, xcb_atom_t atom);
|
||||
void xwindow_focus(xcb_window_t window);
|
||||
void xwindow_get_text_property(xcb_window_t window, xcb_atom_t property,
|
||||
char **out);
|
||||
void xwindow_set_wm_desktop(xcb_window_t window, uint32_t desktop);
|
||||
bool xwindow_get_wm_desktop(xcb_window_t window, uint32_t *desktop);
|
||||
void xwindow_kill_window(xcb_window_t window);
|
||||
|
||||
xcb_window_t xwindow_get_transient_for(xcb_window_t window);
|
||||
int32_t xwindow_get_state(xcb_window_t window);
|
||||
void xwindow_set_state(xcb_window_t window, xcb_icccm_wm_state_t state);
|
||||
xcb_get_geometry_reply_t *xwindow_get_geometry_reply(xcb_window_t window);
|
||||
xcb_get_window_attributes_reply_t *xwindow_get_attributes_reply(
|
||||
xcb_window_t window);
|
||||
|
||||
#define xwindow_set_name_static(window, name) \
|
||||
xcb_icccm_set_wm_name(wm.xcb_conn, window, XCB_ATOM_STRING, 8, \
|
||||
|
||||
Reference in New Issue
Block a user