added rsp for query domain

This commit is contained in:
FelixKratz 2021-09-26 18:23:06 +02:00
parent fc43dc9eac
commit b8ec7da349
8 changed files with 49 additions and 23 deletions

View file

@ -6,10 +6,10 @@
extern CFArrayRef SLSHWCaptureWindowList(uint32_t cid, uint32_t* wid, uint32_t count, uint32_t flags);
extern void SLSCaptureWindowsContentsToRectWithOptions(uint32_t cid, uint32_t* wid, bool meh, CGRect bounds, uint32_t flags, CGImageRef* image);
void print_all_menu_items() {
void print_all_menu_items(FILE* rsp) {
CFArrayRef window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
int window_count = CFArrayGetCount(window_list);
printf("Total Windows: %d \n", window_count);
fprintf("Total Windows: %d \n", window_count);
for (int i = 0; i < window_count; ++i) {
CFDictionaryRef dictionary = CFArrayGetValueAtIndex(window_list, i);
@ -34,7 +34,7 @@ void print_all_menu_items() {
char* name = cfstring_copy(name_ref);
if (strcmp(name, "") == 0) continue;
printf("Menu Item -> Owner: %s; with PID:%llu, Name: %s \n", owner, owner_pid, name);
fprintf(rsp, "Menu Item -> Owner: %s; with PID:%llu, Name: %s \n", owner, owner_pid, name);
free(owner);
free(name);

View file

@ -15,7 +15,7 @@ struct alias {
CGRect bounds;
};
void print_all_menu_items();
void print_all_menu_items(FILE* rsp);
void alias_init(struct alias* alias, char* owner, char* name);
bool alias_update_image(struct alias* alias);
void alias_find_window(struct alias* alias);

View file

@ -87,16 +87,16 @@ static EVENT_CALLBACK(EVENT_HANDLER_SHELL_REFRESH) {
static EVENT_CALLBACK(EVENT_HANDLER_DAEMON_MESSAGE) {
debug("%s\n", __FUNCTION__);
if (g_verbose) {
fprintf(stdout, "%s:", __FUNCTION__);
for (char *message = context; *message;) {
message += fprintf(stdout, " %s", message);
}
putc('\n', stdout);
fflush(stdout);
}
int sockfd = *((int*)context);
int length;
char* message = socket_read(sockfd, &length);
FILE* rsp = fdopen(sockfd, "w");
handle_message(NULL, context);
if (message && rsp) handle_message(rsp, message);
if (rsp) fclose(rsp), fflush(rsp);
if (message) free(message);
socket_close(sockfd);
free(context);
return EVENT_SUCCESS;
}

View file

@ -89,6 +89,7 @@ struct event {
void *context;
volatile uint32_t *info;
enum event_type type;
int param;
};
struct event *event_create(struct event_loop *event_loop, enum event_type type, void *context);

View file

@ -655,7 +655,7 @@ static void handle_domain_query(FILE* rsp, struct token domain, char* message) {
struct token token = get_token(&message);
if (token_equals(token, COMMAND_QUERY_DEFAULT_ITEMS)) {
print_all_menu_items();
print_all_menu_items(rsp);
}
}
@ -694,6 +694,8 @@ void handle_message(FILE *rsp, char *message) {
}
static SOCKET_DAEMON_HANDLER(message_handler) {
struct event *event = event_create(&g_event_loop, DAEMON_MESSAGE, message);
int* _sockfd = malloc(sizeof(int));
memcpy(_sockfd, &sockfd, sizeof(int));
struct event *event = event_create(&g_event_loop, DAEMON_MESSAGE, _sockfd);
event_loop_post(&g_event_loop, event);
}

View file

@ -62,13 +62,9 @@ static void *socket_connection_handler(void *context) {
struct daemon *daemon = context;
while (daemon->is_running) {
int sockfd = accept(daemon->sockfd, NULL, 0);
if (sockfd == -1) continue;
int length;
char *message = socket_read(sockfd, &length);
socket_close(sockfd);
if (message) daemon->handler(message);
if (sockfd != -1) daemon->handler(sockfd);
}
return NULL;
}

View file

@ -1,7 +1,7 @@
#ifndef SOCKET_H
#define SOCKET_H
#define SOCKET_DAEMON_HANDLER(name) void name(char *message)
#define SOCKET_DAEMON_HANDLER(name) void name(int sockfd)
typedef SOCKET_DAEMON_HANDLER(socket_daemon_handler);
#define FAILURE_MESSAGE "\x07"

View file

@ -82,8 +82,35 @@ static int client_send_message(int argc, char **argv) {
}
shutdown(sockfd, SHUT_WR);
int result = EXIT_SUCCESS;
int byte_count = 0;
char rsp[BUFSIZ];
struct pollfd fds[] = {
{ sockfd, POLLIN, 0 }
};
while (poll(fds, 1, -1) > 0) {
if (fds[0].revents & POLLIN) {
if ((byte_count = recv(sockfd, rsp, sizeof(rsp)-1, 0)) <= 0) {
break;
}
rsp[byte_count] = '\0';
if (rsp[0] == FAILURE_MESSAGE[0]) {
result = EXIT_FAILURE;
fprintf(stderr, "%s", rsp + 1);
fflush(stderr);
} else {
fprintf(stdout, "%s", rsp);
fflush(stdout);
}
}
}
socket_close(sockfd);
return EXIT_SUCCESS;
return result;
}
static void acquire_lockfile(void) {