Skip to content

Commit

Permalink
wm: add window focusing, update canvwin demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ffwff committed Sep 10, 2019
1 parent 83a7601 commit fb61b29
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkgs/wm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build() {
_download https://raw.githubusercontent.com/dhepper/font8x8/master/font8x8_basic.h "$build_dir/font8x8_basic.h"

${opt_arch}-gcc -g -o $build_dir/wm $script_dir/wm.c -I$opt_toolsdir/include -L$opt_toolsdir/lib -lm -msse2
# ${opt_arch}-gcc -g -o $build_dir/canvwin $script_dir/canvwin.c -I$opt_toolsdir/include -L$opt_toolsdir/lib -lm -msse2
${opt_arch}-gcc -g -o $build_dir/canvwin $script_dir/canvwin.c -I$opt_toolsdir/include -L$opt_toolsdir/lib -lm -msse2
${opt_arch}-gcc -g -o $build_dir/cterm $script_dir/cterm.c -I$opt_toolsdir/include -L$opt_toolsdir/lib -lm -msse2
# ${opt_arch}-gcc -g -o $build_dir/samplwin $script_dir/samplwin.c -I$opt_toolsdir/include -L$opt_toolsdir/lib -lm -msse2
# ${opt_arch}-gcc -g -o $build_dir/cairowin $script_dir/cairowin.c -I$opt_toolsdir/include -L$opt_toolsdir/lib -msse2 -lcairo -lpixman-1 -lm
Expand Down
31 changes: 21 additions & 10 deletions pkgs/wm/canvwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,22 @@ int main(int argc, char **argv) {
while ((retval = wmc_recv_atom(&conn, &atom)) >= 0) {
if(retval == 0)
goto wait;
struct wm_atom respond_atom = {
.type = ATOM_RESPOND_TYPE,
.respond.retval = 0,
};
switch (atom.type) {
case ATOM_REDRAW_TYPE: {
struct wm_atom respond_atom = {
.type = ATOM_WIN_REFRESH_TYPE,
.win_refresh = (struct wm_atom_win_refresh){
.did_redraw = 0,
.x = sprite.x,
.y = sprite.y,
.width = sprite.width,
.height = sprite.height,
}
};
if (needs_redraw || atom.redraw.force_redraw) {
needs_redraw = 0;
ioctl(fb_fd, GFX_BITBLIT, &sprite);
respond_atom.respond.retval = 1;
respond_atom.win_refresh.did_redraw = 1;
}
wmc_send_atom(&conn, &respond_atom);
break;
Expand All @@ -125,6 +131,11 @@ int main(int argc, char **argv) {
sprite.x = atom.move.x;
sprite.y = atom.move.y;
needs_redraw = 1;

struct wm_atom respond_atom = {
.type = ATOM_RESPOND_TYPE,
.respond.retval = 0,
};
wmc_send_atom(&conn, &respond_atom);
break;
}
Expand All @@ -147,17 +158,17 @@ int main(int argc, char **argv) {
sprite.x += atom.mouse_event.delta_x;
if(!(atom.mouse_event.delta_y < 0 && sprite.y < -atom.mouse_event.delta_y))
sprite.y += atom.mouse_event.delta_y;
if((atom.mouse_event.x - sprite.x) < 10) {
// window_redraw(ctx, &sprite, 1);
char *spawn_argv[] = {"canvwin", NULL};
spawnv("canvwin", (char **)spawn_argv);
}
}
} else if (atom.mouse_event.type == WM_MOUSE_RELEASE && mouse_drag) {
mouse_drag = 0;
mouse_resize = 0;
}
needs_redraw = 1;

struct wm_atom respond_atom = {
.type = ATOM_RESPOND_TYPE,
.respond.retval = 0,
};
wmc_send_atom(&conn, &respond_atom);
break;
}
Expand Down
41 changes: 37 additions & 4 deletions pkgs/wm/wm.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct wm_state {
int nwindows;
struct wm_atom queue[WM_EVENT_QUEUE_LEN];
int queue_len;
int focused_wid;
};

#define WM_WINDOW_PROG 0
Expand Down Expand Up @@ -129,6 +130,7 @@ struct wm_window *wm_add_win_prog(struct wm_state *state, int mfd, int sfd) {
win->type = WM_WINDOW_PROG;
win->as.prog.mfd = mfd;
win->as.prog.sfd = sfd;
state->focused_wid = win->wid;
return win;
}

Expand Down Expand Up @@ -192,6 +194,13 @@ int wm_handle_connection_request(struct wm_state *state, struct wm_connection_re
return 1;
}

/* MISC */

int point_in_win_prog(struct wm_window_prog *prog, unsigned int x, unsigned int y) {
return prog->x <= x && x <= (prog->x + prog->width) &&
prog->y <= y && y <= (prog->y + prog->height);
}

int main(int argc, char **argv) {
int fb_fd = open("/fb0", 0);

Expand All @@ -200,6 +209,7 @@ int main(int argc, char **argv) {
ioctl(fb_fd, TIOCGWINSZ, &ws);

struct wm_state wm = {0};
wm.focused_wid = -1;

// wallpaper
{
Expand Down Expand Up @@ -321,6 +331,24 @@ int main(int argc, char **argv) {
}

if(!wm_atom_eq(&mouse_atom, &wm.last_mouse_atom)) {
// focus on window
if(mouse_atom.mouse_event.type == WM_MOUSE_PRESS) {
int old_wid = wm.focused_wid;
struct wm_window *focused_win = NULL;
for(int i = wm.nwindows; i >= 0; i--) {
if (wm.windows[i].type == WM_WINDOW_PROG &&
point_in_win_prog(&wm.windows[i].as.prog, sprite->x, sprite->y)) {
wm.focused_wid = wm.windows[i].wid;
focused_win = &wm.windows[i];
break;
}
}
if(old_wid != wm.focused_wid && focused_win) {
// TODO: not hard code this
focused_win->z_index = 2;
wm_sort_windows_by_z(&wm);
}
}
wm_add_queue(&wm, &mouse_atom);
}

Expand All @@ -346,9 +374,11 @@ int main(int argc, char **argv) {
struct wm_atom respond_atom;

// transmit events in queue
for(int i = 0; i < wm.queue_len; i++) {
if((win->as.prog.event_mask & (1 << wm.queue[i].type)) != 0) {
win_write_and_wait(&win->as.prog, &wm.queue[i], &respond_atom);
if(wm.focused_wid == win->wid) {
for(int i = 0; i < wm.queue_len; i++) {
if((win->as.prog.event_mask & (1 << wm.queue[i].type)) != 0) {
win_write_and_wait(&win->as.prog, &wm.queue[i], &respond_atom);
}
}
}

Expand All @@ -361,7 +391,10 @@ int main(int argc, char **argv) {

if (retval == sizeof(struct wm_atom)) {
if (respond_atom.type == ATOM_WIN_REFRESH_TYPE) {
//
win->as.prog.x = respond_atom.win_refresh.x;
win->as.prog.y = respond_atom.win_refresh.y;
win->as.prog.width = respond_atom.win_refresh.width;
win->as.prog.height = respond_atom.win_refresh.height;
if(respond_atom.win_refresh.did_redraw)
wm.needs_redraw = 1;
} else {
Expand Down

0 comments on commit fb61b29

Please sign in to comment.