Merge branch 'master' into cmd-swap

This commit is contained in:
Ryan Dwyer 2018-05-28 00:14:22 +10:00 committed by GitHub
commit 8fda41dab5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 185 additions and 76 deletions

View file

@ -1,37 +1,56 @@
#define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 500
#include <assert.h> #include <assert.h>
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include <fcntl.h>
#include <pango/pangocairo.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <pango/pangocairo.h>
#include <unistd.h> #include <unistd.h>
#include <wayland-client.h> #include <wayland-client.h>
#include "config.h" #include "config.h"
#include "pool-buffer.h" #include "pool-buffer.h"
static bool set_cloexec(int fd) {
long flags = fcntl(fd, F_GETFD);
if (flags == -1) {
return false;
}
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
return false;
}
return true;
}
static int create_pool_file(size_t size, char **name) { static int create_pool_file(size_t size, char **name) {
static const char template[] = "sway-client-XXXXXX"; static const char template[] = "sway-client-XXXXXX";
const char *path = getenv("XDG_RUNTIME_DIR"); const char *path = getenv("XDG_RUNTIME_DIR");
if (!path) { if (path == NULL) {
fprintf(stderr, "XDG_RUNTIME_DIR is not set\n");
return -1; return -1;
} }
int ts = (path[strlen(path) - 1] == '/'); size_t name_size = strlen(template) + 1 + strlen(path) + 1;
*name = malloc(name_size);
*name = malloc( if (*name == NULL) {
strlen(template) + fprintf(stderr, "allocation failed\n");
strlen(path) + return -1;
(ts ? 0 : 1) + 1); }
sprintf(*name, "%s%s%s", path, ts ? "" : "/", template); snprintf(*name, name_size, "%s/%s", path, template);
int fd = mkstemp(*name); int fd = mkstemp(*name);
if (fd < 0) { if (fd < 0) {
return -1; return -1;
} }
if (!set_cloexec(fd)) {
close(fd);
return -1;
}
if (ftruncate(fd, size) < 0) { if (ftruncate(fd, size) < 0) {
close(fd); close(fd);
return -1; return -1;
@ -53,7 +72,7 @@ static struct pool_buffer *create_buffer(struct wl_shm *shm,
struct pool_buffer *buf, int32_t width, int32_t height, struct pool_buffer *buf, int32_t width, int32_t height,
uint32_t format) { uint32_t format) {
uint32_t stride = width * 4; uint32_t stride = width * 4;
uint32_t size = stride * height; size_t size = stride * height;
char *name; char *name;
int fd = create_pool_file(size, &name); int fd = create_pool_file(size, &name);
@ -68,8 +87,10 @@ static struct pool_buffer *create_buffer(struct wl_shm *shm,
free(name); free(name);
fd = -1; fd = -1;
buf->size = size;
buf->width = width; buf->width = width;
buf->height = height; buf->height = height;
buf->data = data;
buf->surface = cairo_image_surface_create_for_data(data, buf->surface = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_ARGB32, width, height, stride); CAIRO_FORMAT_ARGB32, width, height, stride);
buf->cairo = cairo_create(buf->surface); buf->cairo = cairo_create(buf->surface);
@ -92,6 +113,9 @@ void destroy_buffer(struct pool_buffer *buffer) {
if (buffer->pango) { if (buffer->pango) {
g_object_unref(buffer->pango); g_object_unref(buffer->pango);
} }
if (buffer->data) {
munmap(buffer->data, buffer->size);
}
memset(buffer, 0, sizeof(struct pool_buffer)); memset(buffer, 0, sizeof(struct pool_buffer));
} }

View file

@ -12,6 +12,8 @@ struct pool_buffer {
cairo_t *cairo; cairo_t *cairo;
PangoContext *pango; PangoContext *pango;
uint32_t width, height; uint32_t width, height;
void *data;
size_t size;
bool busy; bool busy;
}; };

View file

@ -18,6 +18,7 @@ struct criteria {
char *target; // workspace or output name for `assign` criteria char *target; // workspace or output name for `assign` criteria
pcre *title; pcre *title;
pcre *shell;
pcre *app_id; pcre *app_id;
pcre *class; pcre *class;
pcre *instance; pcre *instance;

View file

@ -201,7 +201,7 @@ const char *view_get_window_role(struct sway_view *view);
uint32_t view_get_window_type(struct sway_view *view); uint32_t view_get_window_type(struct sway_view *view);
const char *view_get_type(struct sway_view *view); const char *view_get_shell(struct sway_view *view);
void view_configure(struct sway_view *view, double ox, double oy, int width, void view_configure(struct sway_view *view, double ox, double oy, int width,
int height); int height);

View file

@ -56,6 +56,7 @@ struct swaylock_surface {
struct zwlr_layer_surface_v1 *layer_surface; struct zwlr_layer_surface_v1 *layer_surface;
struct pool_buffer buffers[2]; struct pool_buffer buffers[2];
struct pool_buffer *current_buffer; struct pool_buffer *current_buffer;
bool frame_pending, dirty;
uint32_t width, height; uint32_t width, height;
int32_t scale; int32_t scale;
char *output_name; char *output_name;
@ -74,5 +75,7 @@ void swaylock_handle_key(struct swaylock_state *state,
xkb_keysym_t keysym, uint32_t codepoint); xkb_keysym_t keysym, uint32_t codepoint);
void render_frame(struct swaylock_surface *surface); void render_frame(struct swaylock_surface *surface);
void render_frames(struct swaylock_state *state); void render_frames(struct swaylock_state *state);
void damage_surface(struct swaylock_surface *surface);
void damage_state(struct swaylock_state *state);
#endif #endif

View file

@ -13,6 +13,7 @@
bool criteria_is_empty(struct criteria *criteria) { bool criteria_is_empty(struct criteria *criteria) {
return !criteria->title return !criteria->title
&& !criteria->shell
&& !criteria->app_id && !criteria->app_id
&& !criteria->class && !criteria->class
&& !criteria->instance && !criteria->instance
@ -29,6 +30,7 @@ bool criteria_is_empty(struct criteria *criteria) {
void criteria_destroy(struct criteria *criteria) { void criteria_destroy(struct criteria *criteria) {
pcre_free(criteria->title); pcre_free(criteria->title);
pcre_free(criteria->shell);
pcre_free(criteria->app_id); pcre_free(criteria->app_id);
pcre_free(criteria->class); pcre_free(criteria->class);
pcre_free(criteria->instance); pcre_free(criteria->instance);
@ -53,6 +55,13 @@ static bool criteria_matches_view(struct criteria *criteria,
} }
} }
if (criteria->shell) {
const char *shell = view_get_shell(view);
if (!shell || regex_cmp(shell, criteria->shell) != 0) {
return false;
}
}
if (criteria->app_id) { if (criteria->app_id) {
const char *app_id = view_get_app_id(view); const char *app_id = view_get_app_id(view);
if (!app_id || regex_cmp(app_id, criteria->app_id) != 0) { if (!app_id || regex_cmp(app_id, criteria->app_id) != 0) {
@ -206,6 +215,7 @@ enum criteria_token {
T_FLOATING, T_FLOATING,
T_ID, T_ID,
T_INSTANCE, T_INSTANCE,
T_SHELL,
T_TILING, T_TILING,
T_TITLE, T_TITLE,
T_URGENT, T_URGENT,
@ -229,6 +239,8 @@ static enum criteria_token token_from_name(char *name) {
return T_ID; return T_ID;
} else if (strcmp(name, "instance") == 0) { } else if (strcmp(name, "instance") == 0) {
return T_INSTANCE; return T_INSTANCE;
} else if (strcmp(name, "shell") == 0) {
return T_SHELL;
} else if (strcmp(name, "title") == 0) { } else if (strcmp(name, "title") == 0) {
return T_TITLE; return T_TITLE;
} else if (strcmp(name, "urgent") == 0) { } else if (strcmp(name, "urgent") == 0) {
@ -271,6 +283,9 @@ static char *get_focused_prop(enum criteria_token token) {
case T_INSTANCE: case T_INSTANCE:
value = view_get_instance(view); value = view_get_instance(view);
break; break;
case T_SHELL:
value = view_get_shell(view);
break;
case T_TITLE: case T_TITLE:
value = view_get_class(view); value = view_get_class(view);
break; break;
@ -332,6 +347,9 @@ static bool parse_token(struct criteria *criteria, char *name, char *value) {
case T_TITLE: case T_TITLE:
generate_regex(&criteria->title, effective_value); generate_regex(&criteria->title, effective_value);
break; break;
case T_SHELL:
generate_regex(&criteria->shell, effective_value);
break;
case T_APP_ID: case T_APP_ID:
generate_regex(&criteria->app_id, effective_value); generate_regex(&criteria->app_id, effective_value);
break; break;

View file

@ -3,13 +3,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/types/wlr_xdg_shell.h> #include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/edges.h>
#include "log.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/server.h"
#include "sway/tree/container.h" #include "sway/tree/container.h"
#include "sway/tree/layout.h" #include "sway/tree/layout.h"
#include "sway/server.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
#include "sway/input/seat.h"
#include "sway/input/input-manager.h"
#include "log.h"
static const struct sway_view_child_impl popup_impl; static const struct sway_view_child_impl popup_impl;
@ -248,7 +249,8 @@ void handle_xdg_shell_surface(struct wl_listener *listener, void *data) {
wlr_log(L_DEBUG, "New xdg_shell toplevel title='%s' app_id='%s'", wlr_log(L_DEBUG, "New xdg_shell toplevel title='%s' app_id='%s'",
xdg_surface->toplevel->title, xdg_surface->toplevel->app_id); xdg_surface->toplevel->title, xdg_surface->toplevel->app_id);
wlr_xdg_surface_ping(xdg_surface); wlr_xdg_surface_ping(xdg_surface);
wlr_xdg_toplevel_set_maximized(xdg_surface, true); wlr_xdg_toplevel_set_tiled(xdg_surface, WLR_EDGE_LEFT | WLR_EDGE_RIGHT |
WLR_EDGE_TOP | WLR_EDGE_BOTTOM);
struct sway_xdg_shell_view *xdg_shell_view = struct sway_xdg_shell_view *xdg_shell_view =
calloc(1, sizeof(struct sway_xdg_shell_view)); calloc(1, sizeof(struct sway_xdg_shell_view));

View file

@ -560,6 +560,11 @@ The following attributes may be matched with:
value is \_\_focused\_\_, then the window instance must be the same as that value is \_\_focused\_\_, then the window instance must be the same as that
of the currently focused window. of the currently focused window.
*shell*
Compare value against the window shell, such as "xdg\_shell" or "xwayland".
Can be a regular expression. If value is \_\_focused\_\_, then the shell
must be the same as that of the currently focused window.
*tiling* *tiling*
Matches tiling windows. Matches tiling windows.

View file

@ -107,7 +107,7 @@ uint32_t view_get_window_type(struct sway_view *view) {
return 0; return 0;
} }
const char *view_get_type(struct sway_view *view) { const char *view_get_shell(struct sway_view *view) {
switch(view->type) { switch(view->type) {
case SWAY_VIEW_XDG_SHELL_V6: case SWAY_VIEW_XDG_SHELL_V6:
return "xdg_shell_v6"; return "xdg_shell_v6";
@ -654,10 +654,12 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) {
return title ? strlen(title) : 0; return title ? strlen(title) : 0;
} }
const char *title = view_get_title(view); const char *title = view_get_title(view);
const char *app_id = view_get_app_id(view);
const char *class = view_get_class(view); const char *class = view_get_class(view);
const char *instance = view_get_instance(view); const char *instance = view_get_instance(view);
const char *shell = view_get_type(view); const char *shell = view_get_shell(view);
size_t title_len = title ? strlen(title) : 0; size_t title_len = title ? strlen(title) : 0;
size_t app_id_len = app_id ? strlen(app_id) : 0;
size_t class_len = class ? strlen(class) : 0; size_t class_len = class ? strlen(class) : 0;
size_t instance_len = instance ? strlen(instance) : 0; size_t instance_len = instance ? strlen(instance) : 0;
size_t shell_len = shell ? strlen(shell) : 0; size_t shell_len = shell ? strlen(shell) : 0;
@ -675,6 +677,10 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) {
lenient_strcat(buffer, title); lenient_strcat(buffer, title);
len += title_len; len += title_len;
format += 6; format += 6;
} else if (strncmp(next, "%app_id", 7) == 0) {
lenient_strcat(buffer, app_id);
len += app_id_len;
format += 7;
} else if (strncmp(next, "%class", 6) == 0) { } else if (strncmp(next, "%class", 6) == 0) {
lenient_strcat(buffer, class); lenient_strcat(buffer, class);
len += class_len; len += class_len;

View file

@ -131,14 +131,58 @@ static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.closed = layer_surface_closed, .closed = layer_surface_closed,
}; };
static void handle_wl_output_geometry(void *data, struct wl_output *output, int32_t x, static const struct wl_callback_listener surface_frame_listener;
int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel,
const char *make, const char *model, int32_t transform) { static void surface_frame_handle_done(void *data, struct wl_callback *callback,
uint32_t time) {
struct swaylock_surface *surface = data;
wl_callback_destroy(callback);
surface->frame_pending = false;
if (surface->dirty) {
// Schedule a frame in case the surface is damaged again
struct wl_callback *callback = wl_surface_frame(surface->surface);
wl_callback_add_listener(callback, &surface_frame_listener, surface);
surface->frame_pending = true;
render_frame(surface);
surface->dirty = false;
}
}
static const struct wl_callback_listener surface_frame_listener = {
.done = surface_frame_handle_done,
};
void damage_surface(struct swaylock_surface *surface) {
surface->dirty = true;
if (surface->frame_pending) {
return;
}
struct wl_callback *callback = wl_surface_frame(surface->surface);
wl_callback_add_listener(callback, &surface_frame_listener, surface);
surface->frame_pending = true;
wl_surface_commit(surface->surface);
}
void damage_state(struct swaylock_state *state) {
struct swaylock_surface *surface;
wl_list_for_each(surface, &state->surfaces, link) {
damage_surface(surface);
}
}
static void handle_wl_output_geometry(void *data, struct wl_output *output,
int32_t x, int32_t y, int32_t width_mm, int32_t height_mm,
int32_t subpixel, const char *make, const char *model,
int32_t transform) {
// Who cares // Who cares
} }
static void handle_wl_output_mode(void *data, struct wl_output *output, uint32_t flags, static void handle_wl_output_mode(void *data, struct wl_output *output,
int32_t width, int32_t height, int32_t refresh) { uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
// Who cares // Who cares
} }
@ -151,7 +195,7 @@ static void handle_wl_output_scale(void *data, struct wl_output *output,
struct swaylock_surface *surface = data; struct swaylock_surface *surface = data;
surface->scale = factor; surface->scale = factor;
if (surface->state->run_display) { if (surface->state->run_display) {
render_frames(surface->state); damage_surface(surface);
} }
} }

View file

@ -96,14 +96,14 @@ void swaylock_handle_key(struct swaylock_state *state,
case XKB_KEY_KP_Enter: /* fallthrough */ case XKB_KEY_KP_Enter: /* fallthrough */
case XKB_KEY_Return: case XKB_KEY_Return:
state->auth_state = AUTH_STATE_VALIDATING; state->auth_state = AUTH_STATE_VALIDATING;
render_frames(state); damage_state(state);
wl_display_roundtrip(state->display); wl_display_roundtrip(state->display);
if (attempt_password(&state->password)) { if (attempt_password(&state->password)) {
state->run_display = false; state->run_display = false;
break; break;
} }
state->auth_state = AUTH_STATE_INVALID; state->auth_state = AUTH_STATE_INVALID;
render_frames(state); damage_state(state);
break; break;
case XKB_KEY_Delete: case XKB_KEY_Delete:
case XKB_KEY_BackSpace: case XKB_KEY_BackSpace:
@ -112,19 +112,19 @@ void swaylock_handle_key(struct swaylock_state *state,
} else { } else {
state->auth_state = AUTH_STATE_CLEAR; state->auth_state = AUTH_STATE_CLEAR;
} }
render_frames(state); damage_state(state);
break; break;
case XKB_KEY_Escape: case XKB_KEY_Escape:
clear_password_buffer(&state->password); clear_password_buffer(&state->password);
state->auth_state = AUTH_STATE_CLEAR; state->auth_state = AUTH_STATE_CLEAR;
render_frames(state); damage_state(state);
break; break;
case XKB_KEY_Caps_Lock: case XKB_KEY_Caps_Lock:
/* The state is getting active after this /* The state is getting active after this
* so we need to manually toggle it */ * so we need to manually toggle it */
state->xkb.caps_lock = !state->xkb.caps_lock; state->xkb.caps_lock = !state->xkb.caps_lock;
state->auth_state = AUTH_STATE_INPUT_NOP; state->auth_state = AUTH_STATE_INPUT_NOP;
render_frames(state); damage_state(state);
break; break;
case XKB_KEY_Shift_L: case XKB_KEY_Shift_L:
case XKB_KEY_Shift_R: case XKB_KEY_Shift_R:
@ -137,13 +137,13 @@ void swaylock_handle_key(struct swaylock_state *state,
case XKB_KEY_Super_L: case XKB_KEY_Super_L:
case XKB_KEY_Super_R: case XKB_KEY_Super_R:
state->auth_state = AUTH_STATE_INPUT_NOP; state->auth_state = AUTH_STATE_INPUT_NOP;
render_frames(state); damage_state(state);
break; break;
default: default:
if (codepoint) { if (codepoint) {
append_ch(&state->password, codepoint); append_ch(&state->password, codepoint);
state->auth_state = AUTH_STATE_INPUT; state->auth_state = AUTH_STATE_INPUT;
render_frames(state); damage_state(state);
} }
break; break;
} }

View file

@ -23,6 +23,10 @@ void render_frame(struct swaylock_surface *surface) {
surface->current_buffer = get_next_buffer(state->shm, surface->current_buffer = get_next_buffer(state->shm,
surface->buffers, buffer_width, buffer_height); surface->buffers, buffer_width, buffer_height);
if (surface->current_buffer == NULL) {
return;
}
cairo_t *cairo = surface->current_buffer->cairo; cairo_t *cairo = surface->current_buffer->cairo;
cairo_identity_matrix(cairo); cairo_identity_matrix(cairo);