mirror of
https://github.com/labwc/labwc.git
synced 2025-11-08 13:30:00 -05:00
SnapToRegion: Add regions_from_cursor()
This commit is contained in:
parent
96a591297d
commit
0c31886061
2 changed files with 33 additions and 0 deletions
|
|
@ -30,6 +30,7 @@ void regions_init(struct server *server, struct seat *seat);
|
||||||
void regions_update(struct output *output);
|
void regions_update(struct output *output);
|
||||||
void regions_destroy(struct wl_list *regions);
|
void regions_destroy(struct wl_list *regions);
|
||||||
|
|
||||||
|
struct region *regions_from_cursor(struct server *server);
|
||||||
struct region *regions_from_name(const char *region_name, struct output *output);
|
struct region *regions_from_name(const char *region_name, struct output *output);
|
||||||
|
|
||||||
#endif /* __LABWC_REGIONS_H */
|
#endif /* __LABWC_REGIONS_H */
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <wlr/types/wlr_scene.h>
|
#include <wlr/types/wlr_scene.h>
|
||||||
#include <wlr/util/box.h>
|
#include <wlr/util/box.h>
|
||||||
|
|
@ -39,6 +40,37 @@ regions_from_name(const char *region_name, struct output *output)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct region *
|
||||||
|
regions_from_cursor(struct server *server)
|
||||||
|
{
|
||||||
|
assert(server);
|
||||||
|
double lx = server->seat.cursor->x;
|
||||||
|
double ly = server->seat.cursor->y;
|
||||||
|
|
||||||
|
struct wlr_output *wlr_output = wlr_output_layout_output_at(
|
||||||
|
server->output_layout, lx, ly);
|
||||||
|
struct output *output = output_from_wlr_output(server, wlr_output);
|
||||||
|
if (!output) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
double dist;
|
||||||
|
double dist_min = DBL_MAX;
|
||||||
|
struct region *closest_region = NULL;
|
||||||
|
struct region *region;
|
||||||
|
wl_list_for_each(region, &output->regions, link) {
|
||||||
|
if (wlr_box_contains_point(®ion->geo, lx, ly)) {
|
||||||
|
/* No need for sqrt((x1 - x2)^2 + (y1 - y2)^2) as we just compare */
|
||||||
|
dist = pow(region->center.x - lx, 2) + pow(region->center.y - ly, 2);
|
||||||
|
if (dist < dist_min) {
|
||||||
|
closest_region = region;
|
||||||
|
dist_min = dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return closest_region;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
regions_update(struct output *output)
|
regions_update(struct output *output)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue