typedef struct Monitor {
struct Monitor* next;
- int x, y, w, h;
+ int x, y, w, h, midx, midy;
Workspace* wspaces;
Workspace* cspace;
} Monitor;
void mons_addclient(Client* c);
void mons_delclient(Client* c);
int mons_find(Window win, Location* loc);
+void mons_place(Client* c);
/* client.c */
extern Client* Focused;
#include "anvil.h"
+#include <math.h>
Monitor* Monitors = NULL;
m->y = mons[i].y_org;
m->w = mons[i].width;
m->h = mons[i].height;
+ m->midx = m->x + m->w/2;
+ m->midy = m->y + m->h/2;
m->next = Monitors;
Monitors = m;
}
/* for now just add it to the first monitor in the list */
c->next = Monitors->cspace->floating;
Monitors->cspace->floating = c;
- /* TODO: use size and position to pick the "right" monitor */
+ /* now find the best one based on distance to midpoint */
+ mons_place(c);
}
Client* delclient(Client* list, Client* dead)
int mons_find(Window win, Location* loc)
{
- Monitor* mons = Monitors;
for (Monitor* mon = Monitors; mon; mon = mon->next)
{
- for (Workspace* wspace = mons->wspaces; wspace; wspace = wspace->next)
+ for (Workspace* wspace = mon->wspaces; wspace; wspace = wspace->next)
{
for (Client* client = wspace->floating; client; client = client->next)
{
}
return 0;
}
+
+void mons_place(Client* c)
+{
+ Location loc;
+ if (mons_find(c->win, &loc))
+ {
+ int mindist = 0;
+ Monitor* closest = NULL;
+ /* find the closest monitor by calculating distance between midpoints */
+ int midx = (c->x - BORDER_WIDTH) + (c->w + 2*BORDER_WIDTH)/2;
+ int midy = (c->y - BORDER_WIDTH - TITLE_HEIGHT) + (c->h + 2*BORDER_WIDTH + TITLE_HEIGHT)/2;
+ for (Monitor* mon = Monitors; mon; mon = mon->next)
+ {
+ /* dist = sqrt((x2-x1)^2 + (y2-y1)^2) */
+ int dist = sqrt((midx - mon->midx)*(midx - mon->midx) + (midy - mon->midy)*(midy - mon->midy));
+ if (!closest || dist < mindist)
+ {
+ closest = mon;
+ mindist = dist;
+ }
+ }
+ /* if we changed monitiors, make sure we update accordingly */
+ if (loc.monitor != closest)
+ {
+ loc.workspace->floating = delclient(loc.workspace->floating, c);
+ c->next = closest->cspace->floating;
+ closest->cspace->floating = c;
+ }
+ }
+}
\ No newline at end of file