Skip to content

Commit

Permalink
wm: add window resizing; libc: add abs family of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ffwff committed Sep 8, 2019
1 parent 71b002a commit 1914f5a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pkgs/libcanvas/src
Submodule src updated 1 files
+27 −8 canvas.h
98 changes: 55 additions & 43 deletions pkgs/wm/canvwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "../.build/font8x8_basic.h"
#include "wmc.h"

#define WIDTH 256
#define HEIGHT 256
#define INIT_WIDTH 256
#define INIT_HEIGHT 256
#define FONT_WIDTH 8
#define FONT_HEIGHT 8

Expand Down Expand Up @@ -45,45 +45,52 @@ void canvas_ctx_draw_text(struct canvas_ctx *ctx, int xs, int ys, const char *s)
}
}

void window_redraw(struct canvas_ctx *ctx, int is_pressed) {
if(is_pressed) {
canvas_ctx_fill_rect(ctx, 0, 0, WIDTH, HEIGHT, canvas_color_rgb(0xff, 0xff, 0xff));
void window_redraw(struct canvas_ctx *ctx, struct fbdev_bitblit *sprite, int white) {
sprite->source = (unsigned long *)canvas_ctx_get_surface(ctx);
if(white) {
canvas_ctx_fill_rect(ctx, 0, 0, sprite->width, sprite->height, canvas_color_rgb(0xff, 0xff, 0xff));
} else {
canvas_ctx_fill_rect(ctx, 0, 0, WIDTH, HEIGHT, canvas_color_rgb(0x32, 0x36, 0x39));
canvas_ctx_fill_rect(ctx, 0, 0, sprite->width, sprite->height, canvas_color_rgb(0x32, 0x36, 0x39));
}
canvas_ctx_stroke_rect(ctx, 0, 0, sprite->width - 1, sprite->height - 1, canvas_color_rgb(0x20, 0x21, 0x24));

{
const char *title = "Hello World";
int x_title = (sprite->width - strlen(title) * FONT_WIDTH) / 2;
canvas_ctx_draw_text(ctx, x_title, 10, title);
}
canvas_ctx_stroke_rect(ctx, 0, 0, WIDTH - 1, HEIGHT - 1, canvas_color_rgb(0x20, 0x21, 0x24));
}

int is_coord_in_sprite(struct fbdev_bitblit *sprite, int x, int y) {
if(x < 0) return 0;
if(y < 0) return 0;
/* CHECKS */

int is_coord_in_sprite(struct fbdev_bitblit *sprite, unsigned int x, unsigned int y) {
return sprite->x <= x && x <= (sprite->x + sprite->width) &&
sprite->y <= y && y <= (sprite->y + sprite->height);
}

int main(int argc, char **argv) {
struct canvas_ctx *ctx = canvas_ctx_create(WIDTH, HEIGHT, LIBCANVAS_FORMAT_RGB24);
window_redraw(ctx, 0);
int is_coord_in_bottom_right_corner(struct fbdev_bitblit *sprite, unsigned int x, unsigned int y) {
const int RESIZE_DIST = 10;
int cx = sprite->x + sprite->width;
int cy = sprite->y + sprite->height;

{
const char *title = "Hello World";
int x_title = (WIDTH - strlen(title) * FONT_WIDTH) / 2;
canvas_ctx_draw_text(ctx, x_title, 10, title);
}
return abs(cx - (int)x) <= RESIZE_DIST && abs(cy - (int)y) <= RESIZE_DIST;
}

// draw it!
int fd = open("/fb0", 0);
int main(int argc, char **argv) {
struct fbdev_bitblit sprite = {
.target_buffer = GFX_BACK_BUFFER,
.source = 0,
.x = 0,
.y = 0,
.width = INIT_WIDTH,
.height = INIT_HEIGHT,
.type = GFX_BITBLIT_SURFACE
};
struct canvas_ctx *ctx = canvas_ctx_create(sprite.width, sprite.height, LIBCANVAS_FORMAT_RGB24);
window_redraw(ctx, &sprite, 0);

unsigned char *data = canvas_ctx_get_surface(ctx);
for (int i = 0; i < (WIDTH * HEIGHT * 4); i += 4) {
unsigned char r = data[i + 0];
unsigned char g = data[i + 1];
unsigned char b = data[i + 2];
data[i + 0] = b;
data[i + 1] = g;
data[i + 2] = r;
data[i + 3] = 0;
}
// connect
int fd = open("/fb0", 0);

printf("initializing connection\n");
int fb_fd = open("/fb0", 0);
Expand All @@ -93,17 +100,8 @@ int main(int argc, char **argv) {
wmc_connection_obtain(&conn, ATOM_MOUSE_EVENT_MASK);

// event loop
struct fbdev_bitblit sprite = {
.target_buffer = GFX_BACK_BUFFER,
.source = (unsigned long *)data,
.x = 0,
.y = 0,
.width = WIDTH,
.height = HEIGHT,
.type = GFX_BITBLIT_SURFACE
};

int mouse_drag = 0;
int mouse_resize = 0;

struct wm_atom atom;
int needs_redraw = 0;
Expand Down Expand Up @@ -135,12 +133,26 @@ int main(int argc, char **argv) {
case ATOM_MOUSE_EVENT_TYPE: {
if(atom.mouse_event.type == WM_MOUSE_PRESS &&
(is_coord_in_sprite(&sprite, atom.mouse_event.x, atom.mouse_event.y) ||
mouse_drag)) {
mouse_drag)) {
mouse_drag = 1;
sprite.x += atom.mouse_event.delta_x;
sprite.y += atom.mouse_event.delta_y;
if(is_coord_in_bottom_right_corner(&sprite,
atom.mouse_event.x,
atom.mouse_event.y) || mouse_resize) {
// resize
mouse_resize = 1;
sprite.width += atom.mouse_event.delta_x;
sprite.height += atom.mouse_event.delta_y;
canvas_ctx_resize_buffer(ctx, sprite.width, sprite.height);
window_redraw(ctx, &sprite, 0);
} else {
if(!(atom.mouse_event.delta_x < 0 && sprite.x < -atom.mouse_event.delta_x))
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;
}
} else if (atom.mouse_event.type == WM_MOUSE_RELEASE && mouse_drag) {
mouse_drag = 0;
mouse_resize = 0;
}
needs_redraw = 1;
wmc_send_atom(&conn, &respond_atom);
Expand Down
2 changes: 2 additions & 0 deletions userspace/libc/src/core.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ lib LibC
alias UInt = UInt32
alias Long = Int32
alias ULong = UInt32
alias LongLong = Int64
alias ULongLong = UInt64
end

# Ints
Expand Down
13 changes: 13 additions & 0 deletions userspace/libc/src/functions/stdlib.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ fun rand : LibC::Int
# TODO: chosen by an fair dice roll
# guaranteed to be random
4
end

# abs
fun abs(j : LibC::Int) : LibC::Int
j > 0 ? j : (j * -1)
end

fun labs(j : LibC::Long) : LibC::Long
j > 0 ? j : (j * -1)
end

fun llabs(j : LibC::LongLong) : LibC::LongLong
j > 0 ? j : (j * -1)
end
2 changes: 2 additions & 0 deletions userspace/libc/src/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ long int strtol(const char *nptr, char **endptr, int base);
double strtod(const char *nptr, char **endptr);

int abs(int j);
long int labs(long int j);
long long int llabs(long long int j);

void exit(int status);
void abort(void);
Expand Down

0 comments on commit 1914f5a

Please sign in to comment.