Merge pull request #38 from taiyu-len/master

get/set_userdata stores swayc_t *, fixed memory leak, minor changes.
This commit is contained in:
Drew DeVault 2015-08-16 08:09:59 -04:00
commit 76ec9422a6
6 changed files with 49 additions and 32 deletions

View file

@ -31,6 +31,9 @@ static void free_swayc(swayc_t *c) {
} }
remove_child(c->parent, c); remove_child(c->parent, c);
} }
if (c->name) {
free(c->name);
}
free(c); free(c);
} }
@ -51,8 +54,10 @@ swayc_t *new_output(wlc_handle handle) {
output->height = size->h; output->height = size->h;
output->handle = handle; output->handle = handle;
add_child(&root_container, output); //link this to handler
wlc_handle_set_user_data(handle, output);
add_child(&root_container, output);
//TODO something with this //TODO something with this
int total_width = 0; int total_width = 0;
container_map(&root_container, add_output_widths, &total_width); container_map(&root_container, add_output_widths, &total_width);
@ -135,6 +140,8 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) {
view->name = strdup(title); view->name = strdup(title);
view->visible = true; view->visible = true;
//Link view to handle
wlc_handle_set_user_data(handle, view);
//Case of focused workspace, just create as child of it //Case of focused workspace, just create as child of it
if (sibling->type == C_WORKSPACE) { if (sibling->type == C_WORKSPACE) {
add_child(sibling, view); add_child(sibling, view);

View file

@ -69,7 +69,7 @@ static void handle_output_destroyed(wlc_handle output) {
static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) {
sway_log(L_DEBUG, "Output %d resolution changed to %d x %d", output, to->w, to->h); sway_log(L_DEBUG, "Output %d resolution changed to %d x %d", output, to->w, to->h);
swayc_t *c = get_swayc_for_handle(output, &root_container); swayc_t *c = wlc_handle_get_user_data(output);
if (!c) return; if (!c) return;
c->width = to->w; c->width = to->w;
c->height = to->h; c->height = to->h;
@ -77,7 +77,7 @@ static void handle_output_resolution_change(wlc_handle output, const struct wlc_
} }
static void handle_output_focused(wlc_handle output, bool focus) { static void handle_output_focused(wlc_handle output, bool focus) {
swayc_t *c = get_swayc_for_handle(output, &root_container); swayc_t *c = wlc_handle_get_user_data(output);
if (!c) return; if (!c) return;
if (focus) { if (focus) {
unfocus_all(&root_container); unfocus_all(&root_container);
@ -88,28 +88,28 @@ static void handle_output_focused(wlc_handle output, bool focus) {
static bool handle_view_created(wlc_handle handle) { static bool handle_view_created(wlc_handle handle) {
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = get_focused_container(&root_container);
swayc_t *view = new_view(focused, handle); swayc_t *view = new_view(focused, handle);
//Leave unamanaged windows alone
if (view) { if (view) {
//Set maximize flag for windows. arrange_windows(view->parent, -1, -1);
//TODO: floating windows have this unset
wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true); wlc_view_set_state(handle, WLC_BIT_MAXIMIZED, true);
if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) {
unfocus_all(&root_container); unfocus_all(&root_container);
focus_view(view); focus_view(view);
arrange_windows(view->parent, -1, -1); }
} else { //Unmanaged view else {
wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
wlc_view_focus(handle); wlc_view_focus(handle);
} }
if (wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) { } else {
unfocus_all(&root_container); wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
focus_view(focused); wlc_view_focus(handle);
arrange_windows(focused, -1, -1);
} }
return true; return true;
} }
static void handle_view_destroyed(wlc_handle handle) { static void handle_view_destroyed(wlc_handle handle) {
sway_log(L_DEBUG, "Destroying window %d", handle); sway_log(L_DEBUG, "Destroying window %d", handle);
swayc_t *view = get_swayc_for_handle(handle, &root_container); swayc_t *view = wlc_handle_get_user_data(handle);
swayc_t *parent; swayc_t *parent;
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = get_focused_container(&root_container);

View file

@ -215,10 +215,10 @@ swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent) {
} }
swayc_t *get_focused_container(swayc_t *parent) { swayc_t *get_focused_container(swayc_t *parent) {
if (parent->focused == NULL) { while (parent->focused) {
return parent; parent = parent->focused;
} }
return get_focused_container(parent->focused); return parent;
} }
void unfocus_all(swayc_t *container) { void unfocus_all(swayc_t *container) {

View file

@ -1,6 +1,10 @@
#ifndef _SWAY_LOG_H #ifndef _SWAY_LOG_H
#define _SWAY_LOG_H #define _SWAY_LOG_H
#ifndef __GNUC__
# define __attribute__(x)
#endif
typedef enum { typedef enum {
L_SILENT = 0, L_SILENT = 0,
L_ERROR = 1, L_ERROR = 1,
@ -8,9 +12,10 @@ typedef enum {
L_DEBUG = 3, L_DEBUG = 3,
} log_importance_t; } log_importance_t;
void init_log(int verbosity); void init_log(int verbosity);
void sway_log_colors(int mode); void sway_log_colors(int mode);
void sway_log(int verbosity, char* format, ...); void sway_log(int verbosity, char* format, ...)__attribute__((format (printf,2,3)));
void sway_abort(char* format, ...); void sway_abort(char* format, ...) __attribute__((format (printf,1,2)));
#endif #endif

View file

@ -53,8 +53,9 @@ char *strip_comments(char *str) {
list_t *split_string(const char *str, const char *delims) { list_t *split_string(const char *str, const char *delims) {
list_t *res = create_list(); list_t *res = create_list();
int i, j; int i, j;
for (i = 0, j = 0; i < strlen(str) + 1; ++i) { int len = strlen(str);
if (strchr(delims, str[i]) || i == strlen(str)) { for (i = 0, j = 0; i < len + 1; ++i) {
if (strchr(delims, str[i]) || i == len) {
if (i - j == 0) { if (i - j == 0) {
continue; continue;
} }
@ -63,7 +64,7 @@ list_t *split_string(const char *str, const char *delims) {
left[i - j] = 0; left[i - j] = 0;
list_add(res, left); list_add(res, left);
j = i + 1; j = i + 1;
while (j <= strlen(str) && str[j] && strchr(delims, str[j])) { while (j <= len && str[j] && strchr(delims, str[j])) {
j++; j++;
i++; i++;
} }
@ -110,40 +111,44 @@ int unescape_string(char *string) {
for (i = 0; string[i]; ++i) { for (i = 0; string[i]; ++i) {
if (string[i] == '\\') { if (string[i] == '\\') {
--len; --len;
int shift = 0;
switch (string[++i]) { switch (string[++i]) {
case '0': case '0':
string[i - 1] = '\0'; string[i - 1] = '\0';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'a': case 'a':
string[i - 1] = '\a'; string[i - 1] = '\a';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'b': case 'b':
string[i - 1] = '\b'; string[i - 1] = '\b';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 't': case 't':
string[i - 1] = '\t'; string[i - 1] = '\t';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'n': case 'n':
string[i - 1] = '\n'; string[i - 1] = '\n';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'v': case 'v':
string[i - 1] = '\v'; string[i - 1] = '\v';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'f': case 'f':
string[i - 1] = '\f'; string[i - 1] = '\f';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
case 'r': case 'r':
string[i - 1] = '\r'; string[i - 1] = '\r';
memmove(string + i, string + i + 1, len - i); shift = 1;
break; break;
} }
if (shift) {
memmove(string + i, string + i + shift, len - i);
}
} }
} }
return len; return len;

View file

@ -26,7 +26,7 @@ char *workspace_next_name(void) {
list_t *args = split_string(command, " "); list_t *args = split_string(command, " ");
if (strcmp("workspace", args->items[0]) == 0 && args->length > 1) { if (strcmp("workspace", args->items[0]) == 0 && args->length > 1) {
sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", args->items[1]); sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", (char *)args->items[1]);
char* target = malloc(strlen(args->items[1]) + 1); char* target = malloc(strlen(args->items[1]) + 1);
strcpy(target, args->items[1]); strcpy(target, args->items[1]);
while (*target == ' ' || *target == '\t') while (*target == ' ' || *target == '\t')