Compare commits

...

2 Commits

3 changed files with 71 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ struct pulse_context_t {
pa_cvolume volume; pa_cvolume volume;
pulse_t pulse; pulse_t pulse;
uint32_t index; uint32_t index;
bool inited;
}; };
static void clean_pulse_context(pulse_context_t *context, bool full_cleanup); static void clean_pulse_context(pulse_context_t *context, bool full_cleanup);
@@ -49,7 +50,7 @@ pulse_context_t *init_pulse(GMainContext *context, pulse_notify_t callback) {
} }
void change_pulse_volume(pulse_context_t *context, int step) { void change_pulse_volume(pulse_context_t *context, int step) {
if (!context->pulse.inited || !step || step > 100 || step < -100) return; if (!context->inited || !step || step > 100 || step < -100) return;
pa_cvolume vol = context->volume; pa_cvolume vol = context->volume;
if (step > 0) { if (step > 0) {
@@ -69,7 +70,7 @@ void change_pulse_volume(pulse_context_t *context, int step) {
} }
void toggle_pulse_mute(pulse_context_t *context) { void toggle_pulse_mute(pulse_context_t *context) {
if (!context->pulse.inited) return; if (!context->inited) return;
pa_context *ctx = context->context; pa_context *ctx = context->context;
uint32_t index = context->index; uint32_t index = context->index;
@@ -92,7 +93,7 @@ void clean_pulse_context(pulse_context_t *context, bool full_cleanup) {
pa_context_unref(context->context); pa_context_unref(context->context);
context->context = nullptr; context->context = nullptr;
} }
context->pulse.inited = false; context->inited = false;
if (full_cleanup && context->loop) { if (full_cleanup && context->loop) {
pa_glib_mainloop_free(context->loop); pa_glib_mainloop_free(context->loop);
context->loop = nullptr; context->loop = nullptr;
@@ -146,7 +147,7 @@ void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol,
if (eol != 0 || !info) return; if (eol != 0 || !info) return;
pulse_context_t *ctx = userdata; pulse_context_t *ctx = userdata;
ctx->pulse.inited = true; ctx->inited = true;
ctx->volume = info->volume; ctx->volume = info->volume;
ctx->index = info->index; ctx->index = info->index;
parse_sink(info, &ctx->pulse); parse_sink(info, &ctx->pulse);

View File

@@ -8,8 +8,7 @@ typedef struct pulse_context_t pulse_context_t;
typedef struct pulse_t { typedef struct pulse_t {
int volume_percent; int volume_percent;
bool mute; bool mute;
bool inited; char device_name[255];
char device_name[254];
} pulse_t; } pulse_t;
typedef void (*pulse_notify_t)(pulse_t *pulse); typedef void (*pulse_notify_t)(pulse_t *pulse);

View File

@@ -135,6 +135,67 @@ void wm_check_xcb_extensions(void) {
wm.have_randr = query && query->present; 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) { static bool wm_detect_monitor_by_randr(void) {
xcb_randr_get_monitors_cookie_t monitors_cookie = xcb_randr_get_monitors_cookie_t monitors_cookie =
xcb_randr_get_monitors(wm.xcb_conn, wm.screen->root, 1); xcb_randr_get_monitors(wm.xcb_conn, wm.screen->root, 1);
@@ -182,6 +243,8 @@ static bool wm_detect_monitor_by_randr(void) {
p_delete(&monitors_reply); p_delete(&monitors_reply);
monitor_remove_duplication(&wm.monitor_list);
return true; return true;
} }
@@ -230,6 +293,8 @@ static bool wm_detect_monitor_by_xinerama(void) {
} }
p_delete(&screens_reply); p_delete(&screens_reply);
monitor_remove_duplication(&wm.monitor_list);
return true; return true;
} }