From: Michael D. Lowis Date: Fri, 2 Aug 2024 12:44:37 +0000 (-0400) Subject: drag and resize now working correctly. still need to better handle redraw events... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ddae6a8cb6047fe2421437ccdf6f357f75a70b95;p=proto%2Fanvil.git drag and resize now working correctly. still need to better handle redraw events and draw window titles --- diff --git a/anvil.c b/anvil.c index 189a5ff..66cd393 100644 --- a/anvil.c +++ b/anvil.c @@ -66,7 +66,7 @@ int PtrX = 0, PtrY = 0; //int Num_Monitors = 0; //Monitor Monitors[10] = {0}; //Workspace Workspaces[10] = {0}; -Client* Withdrawn = NULL; +Client* AllClients = NULL; Client* Focused = NULL; static void check_for_wm(void) diff --git a/anvil.h b/anvil.h index c46dd30..50b773f 100644 --- a/anvil.h +++ b/anvil.h @@ -130,7 +130,7 @@ extern int PtrY; //extern int Num_Monitors; //extern Monitor Monitors[10]; //extern Workspace Workspaces[10]; -extern Client* Withdrawn; +extern Client* AllClients; extern Client* Focused; @@ -149,12 +149,15 @@ void Keys_Init(void); void Keys_Process(XKeyEvent* ev); /* client.c */ +Client* Client_Find(Window win); void Client_InitAll(void); void Client_Create(Window win); void Client_Destroy(Window win); void Client_Show(Window win); void Client_Hide(Window win); void Client_MoveResize(Client* c); +void Client_Move(Client* c, int xdiff, int ydiff); +void Client_Resize(Client* c, int xdiff, int ydiff); //Client* client_add(Window win, XWindowAttributes* attr); //void client_draw(Client* c); @@ -171,8 +174,8 @@ void Client_MoveResize(Client* c); void Mouse_Down(XButtonEvent* ev); void Mouse_Up(XButtonEvent* ev); void Mouse_Drag(XMotionEvent* ev); +void Mouse_ToCorner(Client* c); -//void mouse_tocorner(Client* c); //void mouse_totitle(Client* c); //void mouse_get(int* ptrx, int* ptry); diff --git a/client.c b/client.c index 2efe912..43a2034 100644 --- a/client.c +++ b/client.c @@ -1,6 +1,5 @@ #include "anvil.h" -static Client* FindClient(Window win); static void ReadProps(Client* c); static void Redraw(Client* c); @@ -33,6 +32,19 @@ void SetWMState(Client *c, int state) // return state; //} +Client* Client_Find(Window win) +{ + Client* curr = AllClients; + for (; curr; curr = curr->next) + { + if (curr->win == win || curr->frame == win) + { + break; + } + } + return curr; +} + static Client* Create(Window win) { Client* c = NULL; @@ -46,8 +58,8 @@ static Client* Create(Window win) c->y = attr.y; c->w = attr.width + FRAME_WIDTH_SUM; c->h = attr.height + FRAME_HEIGHT_SUM; - c->next = Withdrawn; - Withdrawn = c; + c->next = AllClients; + AllClients = c; /* Reparent the window if applicable */ c->frame = XCreateSimpleWindow(X.disp, X.root, c->x, c->y, c->w, c->h, 1, X.clr_bdr, X.clr_bg); @@ -85,7 +97,7 @@ static Client* Create(Window win) void Client_Show(Window win) { /* find or create the client structure */ - Client* c = FindClient(win); + Client* c = Client_Find(win); if (!c) { c = Create(win); } /* if we have a calid client, show it */ @@ -149,6 +161,19 @@ void Client_MoveResize(Client* c) Redraw(c); } +void Client_Move(Client* c, int xdiff, int ydiff) +{ + c->x += xdiff; + c->y += ydiff; + Client_MoveResize(c); +} + +void Client_Resize(Client* c, int xdiff, int ydiff) +{ + c->w += xdiff; + c->h += ydiff; + Client_MoveResize(c); +} static void ReadProps(Client* c) { @@ -261,18 +286,6 @@ static void Redraw(Client* c) // XClearArea(X.disp, c->win, 0, 0, 0, 0, True); } -static Client* FindClient(Window win) -{ - Client* curr = Withdrawn; - for (; curr; curr = curr->next) - { - if (curr->win == win || curr->frame == win) - { - break; - } - } - return curr; -} diff --git a/mouse.c b/mouse.c index aa73104..baac4df 100644 --- a/mouse.c +++ b/mouse.c @@ -1,79 +1,82 @@ #include "anvil.h" -//static inline int PRESSED(int mods, int btn) -//{ -// return ((mods & (1 << (btn + 7))) == (1 << (btn + 7))); -//} -// -//static inline int FLAGS_SET(int state, int flags) -//{ -// return ((!flags && !state) || ((state & flags) == flags)); -//} -// -//typedef struct { -// int mods; -// int btn; -// int type; -// void(*func)(Location* loc); -//} MouseAct; -// -//static void resize_frame(Location* loc) -//{ -// if (X.curr_ev->xbutton.y > MIN_HEIGHT) -// { -// X.mode = M_RESIZE; -// mouse_tocorner(loc->client); -// } -//} -// -//static void toggle_float(Location* loc) -//{ -// (void)loc; -//// mons_togglefloat(loc); -//} -// -//static void close_client(Location* loc) -//{ -// client_close(loc->client); -//} -// -//static void shade_client(Location* loc) -//{ -// client_shade(loc->client); -//} -// -//static void lower_client(Location* loc) -//{ -// (void)loc; -//// mons_lower(loc->monitor, loc->client); -//} -// -//static void raise_client(Location* loc) -//{ -// (void)loc; -//// mons_raise(loc->monitor, loc->client); -//} -// -//static void stack_clients(Location* loc) +static inline int PRESSED(int mods, int btn) +{ + return ((mods & (1 << (btn + 7))) == (1 << (btn + 7))); +} + +static inline int FLAGS_SET(int state, int flags) +{ + return ((!flags && !state) || ((state & flags) == flags)); +} + +typedef struct { + int mods; + int btn; + int type; + void(*func)(Client* client); +} MouseAct; + +static void resize_frame(Client* client) +{ + (void)client; + if (X.curr_ev->xbutton.y > MIN_HEIGHT) + { + X.mode = M_RESIZE; + Mouse_ToCorner(client); + } +} + +static void toggle_float(Client* client) +{ + (void)client; +// mons_togglefloat(loc); +} + +static void close_client(Client* client) +{ + (void)client; +// client_close(client); +} + +static void shade_client(Client* client) +{ + (void)client; +// client_shade(client); +} + +static void lower_client(Client* client) +{ + (void)client; +// mons_lower(loc->monitor, loc->client); +} + +static void raise_client(Client* client) +{ + (void)client; +// mons_raise(loc->monitor, loc->client); +} + +//static void stack_clients(Client* client) //{ -// (void)loc; +// (void)client; //// stacked_set(loc); //} -// -//static void reposition_tile(Location* loc) + +//static void reposition_tile(Client* client) //{ -// (void)loc; +// (void)client; // X.mode = (X.edge == E_TOP ? M_TILE_RESIZE : M_COL_RESIZE); //} -// -//MouseAct Floating[] = { -// { 0, Button1, ButtonPress, resize_frame }, -// { MODKEY|ShiftMask, Button2, ButtonPress, toggle_float }, -// { MODKEY, Button2, ButtonPress, close_client }, -// { 0, Button3, ButtonPress, shade_client }, -// { 0, Button4, ButtonPress, lower_client }, -// { 0, Button5, ButtonPress, raise_client } -//}; + +MouseAct Floating[] = { + { 0, Button1, ButtonPress, resize_frame }, + { MODKEY|ShiftMask, Button2, ButtonPress, toggle_float }, + { MODKEY, Button2, ButtonPress, close_client }, + { 0, Button3, ButtonPress, shade_client }, + { 0, Button4, ButtonPress, lower_client }, + { 0, Button5, ButtonPress, raise_client } +}; // //MouseAct Stacked[] = { // { 0, Button1, ButtonPress, reposition_tile }, @@ -83,24 +86,24 @@ // { 0, Button3, ButtonPress, stack_clients }, //}; // -//static void process(XButtonEvent* ev, Location* loc, MouseAct* actions, int nactions) -//{ -// for (int i = 0; i < nactions; i++) -// { -// MouseAct* act = &actions[i]; -// int match = ( -// (ev->type == act->type) && -// ((int)ev->button == act->btn) && -// FLAGS_SET(ev->state, act->mods) -// ); -// if (match) -// { -// X.curr_ev = (XEvent*)ev; -// act->func(loc); -// break; -// } -// } -//} +static void ProcessMouseEvent(XButtonEvent* ev, Client* client, MouseAct* actions, int nactions) +{ + for (int i = 0; i < nactions; i++) + { + MouseAct* act = &actions[i]; + int match = ( + (ev->type == act->type) && + ((int)ev->button == act->btn) && + FLAGS_SET(ev->state, act->mods) + ); + if (match) + { + X.curr_ev = (XEvent*)ev; + act->func(client); + break; + } + } +} // //void mouse_down(XButtonEvent* ev, Location* loc) //{ @@ -116,36 +119,12 @@ // //static void float_drag(XMotionEvent* ev, Location* loc) //{ -// if (PRESSED(ev->state, Button1)) -// { -// if (X.mode != M_RESIZE) -// { -// client_move(loc->client, ev->x_root - X.last_x, ev->y_root - X.last_y); -// } -// else -// { -// client_resize(loc->client, ev->x_root - X.last_x, ev->y_root - X.last_y); -// } -// } //} // //void mouse_up(XButtonEvent* ev, Location* loc) //{ // (void)ev; // (void)loc; -// if (X.mode == M_TILE_RESIZE) -// { -//// mons_tilemove(loc, ev->y_root - X.start_y); -// } -// else if (X.mode == M_COL_RESIZE) -// { -//// mons_coladjust(loc->monitor, loc->column, ev->x_root - X.start_x); -// } -// else -// { -// /* nothing to do here */ -// } -// X.mode = M_IDLE; //} // //void mouse_drag(XMotionEvent* ev, Location* loc) @@ -156,14 +135,6 @@ // } //} // -//void mouse_tocorner(Client* c) -//{ -// int new_w = c->w - BORDER_WIDTH/2; -// int new_h = c->h - BORDER_WIDTH/2; -// XWarpPointer(X.disp, None, c->frame, 0, 0, 0, 0, new_w, new_h); -// X.last_x = c->x + new_w; -// X.last_y = c->y + new_h; -//} // //void mouse_totitle(Client* c) //{ @@ -183,53 +154,81 @@ void Mouse_Down(XButtonEvent* ev) { - (void)ev; -// Location loc = {0}; -// if (mons_find(ev->window, &loc) && (loc.client->frame == ev->window)) -// { -// Client* c = loc.client; -// if (ev->y < MIN_HEIGHT) -// { -// X.edge = E_TOP; -// } -// else if (ev->y_root > (c->y + c->h - BORDER_WIDTH)) -// { -// X.edge = E_BOTTOM; -// } -// else if (ev->x < BORDER_WIDTH) -// { -// X.edge = E_LEFT; -// } -// else if (ev->x_root > (c->x + c->w - BORDER_WIDTH)) -// { -// X.edge = E_RIGHT; -// } -// else -// { -// X.edge = E_NONE; -// } -// mouse_down(ev, &loc); -// } + Client* c = Client_Find(ev->window); + if (c && (c->frame = ev->window)) + { + if (ev->y < MIN_HEIGHT) + { + X.edge = E_TOP; + } + else if (ev->y_root > (c->y + c->h - BORDER_WIDTH)) + { + X.edge = E_BOTTOM; + } + else if (ev->x < BORDER_WIDTH) + { + X.edge = E_LEFT; + } + else if (ev->x_root > (c->x + c->w - BORDER_WIDTH)) + { + X.edge = E_RIGHT; + } + else + { + X.edge = E_NONE; + } + ProcessMouseEvent(ev, c, Floating, sizeof(Floating)/sizeof(Floating[0])); + } } void Mouse_Up(XButtonEvent* ev) { (void)ev; -// Location loc = {0}; -// if (mons_find(ev->window, &loc) && (loc.client->frame == ev->window)) -// { -// mouse_up(ev, &loc); -// } + Client* c = Client_Find(ev->window); + if (c && (c->frame = ev->window)) + { + if (X.mode == M_TILE_RESIZE) + { + // mons_tilemove(loc, ev->y_root - X.start_y); + } + else if (X.mode == M_COL_RESIZE) + { + // mons_coladjust(loc->monitor, loc->column, ev->x_root - X.start_x); + } + else + { + /* nothing to do here */ + } + X.mode = M_IDLE; + } } void Mouse_Drag(XMotionEvent* ev) { (void)ev; -// Location loc = {0}; -// if (mons_find(ev->window, &loc) && (loc.client->frame == ev->window)) -// { -// mouse_drag(ev, &loc); -// } + Client* c = Client_Find(ev->window); + if (c && (c->frame = ev->window)) + { + if (PRESSED(ev->state, Button1)) + { + if (X.mode != M_RESIZE) + { + Client_Move(c, ev->x_root - X.last_x, ev->y_root - X.last_y); + } + else + { + Client_Resize(c, ev->x_root - X.last_x, ev->y_root - X.last_y); + } + } + } } +void Mouse_ToCorner(Client* c) +{ + int new_w = c->w - BORDER_WIDTH/2; + int new_h = c->h - BORDER_WIDTH/2; + XWarpPointer(X.disp, None, c->frame, 0, 0, 0, 0, new_w, new_h); + X.last_x = c->x + new_w; + X.last_y = c->y + new_h; +}