Skip to content

Commit

Permalink
wm: add wmc header for window manager clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ffwff committed Sep 8, 2019
1 parent 277f050 commit e670c6b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 51 deletions.
51 changes: 10 additions & 41 deletions pkgs/wm/canvwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <canvas.h>

#include "../.build/font8x8_basic.h"
#include "wm.h"
#include "wmc.h"

#define WIDTH 256
#define HEIGHT 256
Expand Down Expand Up @@ -75,40 +75,9 @@ int main(int argc, char **argv) {
printf("initializing connection\n");
int fb_fd = open("/fb0", 0);

// window manager pipes
int wm_control_fd = open("/pipes/wm", 0);
int win_fd_m = -1, win_fd_s = -1;

// connect to wm
struct wm_connection_request conn_req = {
.pid = getpid()
};
write(wm_control_fd, (char *)&conn_req, sizeof(struct wm_connection_request));
while(1) {
// try to poll for pipes
char mpath[128] = { 0 };
snprintf(mpath, sizeof(mpath), "/pipes/wm:%d:m", conn_req.pid);
if(win_fd_m == -1) {
if((win_fd_m = open(mpath, 0)) < 0) {
goto await_conn;
}
}

char spath[128] = { 0 };
snprintf(spath, sizeof(spath), "/pipes/wm:%d:s", conn_req.pid);
if(win_fd_s == -1) {
if((win_fd_s = open(spath, 0)) < 0) {
goto await_conn;
}
}

if(win_fd_m != -1 && win_fd_s != -1) {
break;
}

await_conn:
usleep(1);
}
struct wmc_connection conn;
wmc_connection_init(&conn);
wmc_connection_obtain(&conn);

// event loop
struct fbdev_bitblit sprite = {
Expand All @@ -125,12 +94,12 @@ int main(int argc, char **argv) {
.type = ATOM_CONFIGURE_MASK,
.configure.event_mask = 0,
};
write(win_fd_s, (char *)&configure_atom, sizeof(configure_atom));
wmc_send_atom(&conn, &configure_atom);

struct wm_atom atom;
int needs_redraw = 0;
int retval = 0;
while ((retval = read(win_fd_m, (char *)&atom, sizeof(struct wm_atom))) >= 0) {
while ((retval = wmc_recv_atom(&conn, &atom)) >= 0) {
if(retval == 0)
goto wait;
struct wm_atom respond_atom = {
Expand All @@ -144,14 +113,14 @@ int main(int argc, char **argv) {
ioctl(fb_fd, GFX_BITBLIT, &sprite);
respond_atom.respond.retval = 1;
}
write(win_fd_s, (char *)&respond_atom, sizeof(respond_atom));
wmc_send_atom(&conn, &respond_atom);
break;
}
case ATOM_MOVE_TYPE: {
sprite.x = atom.move.x;
sprite.y = atom.move.y;
needs_redraw = 1;
write(win_fd_s, (char *)&respond_atom, sizeof(respond_atom));
wmc_send_atom(&conn, &respond_atom);
break;
}
case ATOM_MOUSE_EVENT_TYPE: {
Expand All @@ -161,12 +130,12 @@ int main(int argc, char **argv) {
window_redraw(ctx, 0);
}
needs_redraw = 1;
write(win_fd_s, (char *)&respond_atom, sizeof(respond_atom));
wmc_send_atom(&conn, &respond_atom);
break;
}
}
wait:
waitfd(win_fd_m, (useconds_t)-1);
wmc_wait_atom(&conn);
}

return 0;
Expand Down
23 changes: 13 additions & 10 deletions pkgs/wm/wm.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,20 @@ void wm_add_queue(struct wm_state *state, struct wm_atom *atom) {

/* CONNECTIONS */

void wm_handle_connection_request(struct wm_state *state, struct wm_connection_request *conn_req) {
char mpath[128] = { 0 };
snprintf(mpath, sizeof(mpath), "/pipes/wm:%d:m", conn_req->pid);
int mfd = create(mpath);
if(mfd < 0) { return; }
int wm_handle_connection_request(struct wm_state *state, struct wm_connection_request *conn_req) {
char path[128] = { 0 };

char spath[128] = { 0 };
snprintf(spath, sizeof(spath), "/pipes/wm:%d:s", conn_req->pid);
int sfd = create(spath);
if(sfd < 0) { close(mfd); return; }
snprintf(path, sizeof(path), "/pipes/wm:%d:m", conn_req->pid);
int mfd = create(path);
if(mfd < 0) { return 0; }

snprintf(path, sizeof(path), "/pipes/wm:%d:s", conn_req->pid);
int sfd = create(path);
if(sfd < 0) { close(mfd); return 0; }

wm_add_win_prog(state, mfd, sfd);
wm_sort_windows_by_z(state);
return 1;
}

int main(int argc, char **argv) {
Expand Down Expand Up @@ -269,7 +270,9 @@ int main(int argc, char **argv) {
read(control_fd, (char *)&conn_req, sizeof(struct wm_connection_request))
== sizeof(struct wm_connection_request)
) {
wm_handle_connection_request(&wm, &conn_req);
if (wm_handle_connection_request(&wm, &conn_req)) {
wm.needs_redraw = 1;
}
}
}
{
Expand Down
69 changes: 69 additions & 0 deletions pkgs/wm/wmc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <syscalls.h>
#include "wm.h"

struct wmc_connection {
int wm_control_fd;
int win_fd_m, win_fd_s;
};

int wmc_connection_init(struct wmc_connection *conn) {
conn->wm_control_fd = open("/pipes/wm", 0);
if(conn->wm_control_fd < 0) {
return 0;
}
conn->win_fd_m = -1;
conn->win_fd_s = -1;
return 1;
}

void wmc_connection_deinit(struct wmc_connection *conn) {
close(conn->wm_control_fd);
close(conn->win_fd_m);
close(conn->win_fd_s);
}

void wmc_connection_obtain(struct wmc_connection *conn) {
struct wm_connection_request conn_req = {
.pid = getpid()
};
write(conn->wm_control_fd, (char *)&conn_req, sizeof(struct wm_connection_request));
while(1) {
// try to poll for pipes
char path[128] = { 0 };

if(conn->win_fd_m == -1) {
snprintf(path, sizeof(path), "/pipes/wm:%d:m", conn_req.pid);
if((conn->win_fd_m = open(path, 0)) < 0) {
goto await_conn;
}
}

if(conn->win_fd_s == -1) {
snprintf(path, sizeof(path), "/pipes/wm:%d:s", conn_req.pid);
if((conn->win_fd_s = open(path, 0)) < 0) {
goto await_conn;
}
}

if(conn->win_fd_m != -1 && conn->win_fd_s != -1) {
break;
}

await_conn:
usleep(1);
}
}

int wmc_send_atom(struct wmc_connection *conn, struct wm_atom *atom) {
return write(conn->win_fd_s, (char *)atom, sizeof(struct wm_atom));
}

int wmc_recv_atom(struct wmc_connection *conn, struct wm_atom *atom) {
return read(conn->win_fd_m, (char *)atom, sizeof(struct wm_atom));
}

int wmc_wait_atom(struct wmc_connection *conn) {
return waitfd(conn->win_fd_m, (useconds_t)-1);
}

0 comments on commit e670c6b

Please sign in to comment.