Skip to content

Commit

Permalink
#148 fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya committed Sep 22, 2022
1 parent 1924a6d commit b727eee
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/osax/common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SA_COMMON_H
#define SA_COMMON_H

#define OSAX_VERSION "2.0.10"
#define OSAX_VERSION "2.0.11"

#define OSAX_ATTRIB_DOCK_SPACES 0x01
#define OSAX_ATTRIB_DPPM 0x02
Expand Down
38 changes: 29 additions & 9 deletions src/osax/payload.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Foundation/Foundation.h>

#include <mach-o/getsect.h>
#include <mach-o/dyld.h>
#include <mach/mach.h>
Expand Down Expand Up @@ -70,6 +71,7 @@
extern CFTypeRef SLSTransactionCreate(int cid);
extern CGError SLSTransactionCommit(CFTypeRef transaction, int unknown);
extern CGError SLSTransactionOrderWindow(CFTypeRef transaction, uint32_t wid, int order, uint32_t rel_wid);
extern CGError SLSTransactionSetWindowAlpha(CFTypeRef transaction, uint32_t wid, float alpha);

struct window_fade_context
{
Expand All @@ -78,6 +80,7 @@
volatile float alpha;
volatile float duration;
volatile bool skip;
volatile bool abort;
};

pthread_mutex_t window_fade_lock;
Expand Down Expand Up @@ -633,7 +636,8 @@ static void do_window_opacity(char *message)
{
entry:;
struct window_fade_context *context = (struct window_fade_context *) data;
context->skip = false;
context->abort = false;
context->skip = false;

float start_alpha;
float end_alpha = context->alpha;
Expand All @@ -644,7 +648,8 @@ static void do_window_opacity(char *message)
int frame_count = (int)(((float) total_duration / (float) frame_duration) + 1.0f);

for (int frame_index = 1; frame_index <= frame_count; ++frame_index) {
if (context->skip) goto entry;
if (context->abort) goto abort;
if (context->skip) goto entry;

float t = (float) frame_index / (float) frame_count;
if (t < 0.0f) t = 0.0f;
Expand All @@ -656,15 +661,14 @@ static void do_window_opacity(char *message)
usleep(frame_duration*1000);
}

abort:
pthread_mutex_lock(&window_fade_lock);

if (!context->skip) {
if (context->abort || !context->skip) {
table_remove(&window_fade_table, &context->wid);
pthread_mutex_unlock(&window_fade_lock);
free(context);
return NULL;
}

pthread_mutex_unlock(&window_fade_lock);

goto entry;
Expand Down Expand Up @@ -695,6 +699,7 @@ static void do_window_opacity_fade(char *message)
context->wid = wid;
context->alpha = alpha;
context->duration = duration;
context->abort = false;
context->skip = false;
__asm__ __volatile__ ("" ::: "memory");

Expand Down Expand Up @@ -768,7 +773,7 @@ static void do_window_shadow(char *message)
}
}

static void do_window_order_swap(char *message)
static void do_window_swap_proxy(char *message)
{
uint32_t a_wid;
unpack(message, a_wid);
Expand All @@ -778,9 +783,24 @@ static void do_window_order_swap(char *message)
unpack(message, b_wid);
if (!b_wid) return;

float alpha;
unpack(message, alpha);

int order;
unpack(message, order);

pthread_mutex_lock(&window_fade_lock);
struct window_fade_context *context = table_find(&window_fade_table, &a_wid);
if (context) {
context->abort = true;
__asm__ __volatile__ ("" ::: "memory");
usleep(8 * 1000);
}
pthread_mutex_unlock(&window_fade_lock);

CFTypeRef transaction = SLSTransactionCreate(_connection);
SLSTransactionOrderWindow(transaction, a_wid, -1, b_wid);
SLSTransactionOrderWindow(transaction, b_wid, 0, 0);
SLSTransactionOrderWindow(transaction, b_wid, order, a_wid);
SLSTransactionSetWindowAlpha(transaction, a_wid, alpha);
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);
}
Expand Down Expand Up @@ -853,7 +873,7 @@ static void handle_message(int sockfd, char *message)
do_window_opacity(message);
} break;
case 0x0E: {
do_window_order_swap(message);
do_window_swap_proxy(message);
} break;

}
Expand Down
1 change: 1 addition & 0 deletions src/sa.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ bool scripting_addition_set_shadow(uint32_t wid, bool shadow);
bool scripting_addition_focus_window(uint32_t wid);
bool scripting_addition_scale_window(uint32_t wid, float x, float y, float w, float h);
bool scripting_addition_swap_window_order(uint32_t a_wid, uint32_t b_wid);
bool scripting_addition_swap_window_proxy(uint32_t a_wid, uint32_t b_wid, float opacity, int order);

#endif
4 changes: 3 additions & 1 deletion src/sa.m
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,15 @@ bool scripting_addition_scale_window(uint32_t wid, float x, float y, float w, fl
return scripting_addition_send_bytes(bytes, length);
}

bool scripting_addition_swap_window_order(uint32_t a_wid, uint32_t b_wid)
bool scripting_addition_swap_window_proxy(uint32_t a_wid, uint32_t b_wid, float opacity, int order)
{
char bytes[0x100];

char length = 2;
pack(bytes, a_wid, length);
pack(bytes, b_wid, length);
pack(bytes, opacity, length);
pack(bytes, order, length);
bytes[1] = 0x0E;
bytes[0] = length-1;

Expand Down
2 changes: 2 additions & 0 deletions src/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ struct window_proxy
uint32_t id;
CGContextRef context;
float tx, ty, tw, th;
float alpha;
CGRect frame;
CFArrayRef image;
};

struct window_animation
Expand Down
34 changes: 26 additions & 8 deletions src/window_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,7 @@ void window_manager_resize_window(struct window *window, float width, float heig

static void window_manager_create_window_proxy(int animation_connection, uint32_t wid, struct window_proxy *proxy)
{
CFArrayRef image = SLSHWCaptureWindowList(animation_connection, &wid, 1, (1 << 11) | (1 << 8));
if (!image) return;
if (!proxy->image) return;

CFTypeRef frame_region;
CGSNewRegionWithRect(&proxy->frame, &frame_region);
Expand All @@ -535,15 +534,19 @@ static void window_manager_create_window_proxy(int animation_connection, uint32_

CGRect frame = { {0, 0}, proxy->frame.size };
CGContextClearRect(proxy->context, frame);
CGContextDrawImage(proxy->context, frame, (CGImageRef) CFArrayGetValueAtIndex(image, 0));
CGContextDrawImage(proxy->context, frame, (CGImageRef) CFArrayGetValueAtIndex(proxy->image, 0));
CGContextFlush(proxy->context);

CFRelease(frame_region);
CFRelease(image);
}

static void window_manager_destroy_window_proxy(int animation_connection, struct window_proxy *proxy)
{
if (proxy->image) {
CFRelease(proxy->image);
proxy->image = NULL;
}

if (proxy->context) {
CGContextRelease(proxy->context);
proxy->context = NULL;
Expand Down Expand Up @@ -581,7 +584,7 @@ void *window_manager_animate_window_list_thread_proc(void *data)
if (context->animation_list[i].skip) continue;

table_remove(&g_window_manager.window_animations_table, &context->animation_list[i].wid);
scripting_addition_swap_window_order(context->animation_list[i].wid, context->animation_list[i].proxy.id);
scripting_addition_swap_window_proxy(context->animation_list[i].wid, context->animation_list[i].proxy.id, context->animation_list[i].proxy.alpha, 0);
window_manager_destroy_window_proxy(context->animation_connection, &context->animation_list[i].proxy);
}
SLSReenableUpdate(context->animation_connection);
Expand Down Expand Up @@ -640,21 +643,25 @@ void window_manager_animate_window_list_async(struct window_capture *window_list
context->animation_list[i].proxy.frame.origin.y = (int)(existing_animation->proxy.ty);
context->animation_list[i].proxy.frame.size.width = (int)(existing_animation->proxy.tw);
context->animation_list[i].proxy.frame.size.height = (int)(existing_animation->proxy.th);
context->animation_list[i].proxy.alpha = existing_animation->proxy.alpha;
context->animation_list[i].proxy.image = CFRetain(existing_animation->proxy.image);
__asm__ __volatile__ ("" ::: "memory");

window_manager_create_window_proxy(context->animation_connection, context->animation_list[i].wid, &context->animation_list[i].proxy);

CFTypeRef transaction = SLSTransactionCreate(context->animation_connection);
SLSTransactionOrderWindow(transaction, context->animation_list[i].proxy.id, -1, existing_animation->proxy.id);
SLSTransactionOrderWindow(transaction, context->animation_list[i].proxy.id, 1, context->animation_list[i].wid);
SLSTransactionOrderWindow(transaction, existing_animation->proxy.id, 0, 0);
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);

window_manager_destroy_window_proxy(context->animation_connection, &existing_animation->proxy);
} else {
SLSGetWindowBounds(context->animation_connection, context->animation_list[i].wid, &context->animation_list[i].proxy.frame);
SLSGetWindowAlpha(context->animation_connection, context->animation_list[i].wid, &context->animation_list[i].proxy.alpha);
context->animation_list[i].proxy.image = SLSHWCaptureWindowList(context->animation_connection, &context->animation_list[i].wid, 1, (1 << 11) | (1 << 8));
window_manager_create_window_proxy(context->animation_connection, context->animation_list[i].wid, &context->animation_list[i].proxy);
scripting_addition_swap_window_order(context->animation_list[i].proxy.id, context->animation_list[i].wid);
scripting_addition_swap_window_proxy(context->animation_list[i].wid, context->animation_list[i].proxy.id, 0.0f, 1);
}

table_add(&g_window_manager.window_animations_table, &context->animation_list[i].wid, &context->animation_list[i]);
Expand Down Expand Up @@ -740,7 +747,18 @@ bool window_manager_set_opacity(struct window_manager *wm, struct window *window
}
}

return scripting_addition_set_opacity(window->id, opacity, wm->window_opacity_duration);
bool result = true;
pthread_mutex_lock(&g_window_manager.window_animations_lock);
struct window_animation *existing_animation = table_find(&g_window_manager.window_animations_table, &window->id);
if (existing_animation) {
existing_animation->proxy.alpha = opacity;
__asm__ __volatile__ ("" ::: "memory");
} else {
result = scripting_addition_set_opacity(window->id, opacity, wm->window_opacity_duration);
}
pthread_mutex_unlock(&g_window_manager.window_animations_lock);

return result;
}

void window_manager_set_window_opacity(struct window_manager *wm, struct window *window, float opacity)
Expand Down

0 comments on commit b727eee

Please sign in to comment.