Skip to content

Commit

Permalink
libc: make ipc structs packed, add sys/kbd.h
Browse files Browse the repository at this point in the history
- kbdfs: add raw packet fs node
  • Loading branch information
ffwff committed Sep 9, 2019
1 parent 5d09beb commit c81f306
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
17 changes: 11 additions & 6 deletions pkgs/wm/cterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct cterm_state {
int root_x, root_y;
char *buffer;
size_t buffer_len;
int stdio_fd;
int in_fd, out_fd;
};

void cterm_init(struct cterm_state *state) {
Expand All @@ -97,9 +97,14 @@ void cterm_init(struct cterm_state *state) {
state->cy = 0;
state->buffer = 0;
state->buffer_len = 0;

char path[128] = { 0 };
snprintf(path, sizeof(path), "/pipes/cterm:%d", getpid());
state->stdio_fd = create(path);

snprintf(path, sizeof(path), "/pipes/cterm:%d:in", getpid());
state->in_fd = create(path);

snprintf(path, sizeof(path), "/pipes/cterm:%d:out", getpid());
state->out_fd = create(path);
}

void cterm_draw_buffer(struct cterm_state *state);
Expand Down Expand Up @@ -190,7 +195,7 @@ void cterm_add_character(struct cterm_state *state, char ch) {

int cterm_read_buf(struct cterm_state *state) {
char buf[4096];
int retval = read(state->stdio_fd, buf, sizeof(buf));
int retval = read(state->out_fd, buf, sizeof(buf));
if(retval <= 0) return retval;
for(int i = 0; i < retval; i++) {
cterm_add_character(state, buf[i]);
Expand All @@ -206,8 +211,8 @@ int main(int argc, char **argv) {
// spawn main
struct startup_info s_info = {
.stdin = STDIN_FILENO,
.stdout = state.stdio_fd,
.stderr = state.stdio_fd,
.stdout = state.out_fd,
.stderr = state.out_fd,
};
char *spawn_argv[] = {"/hd0/main", NULL};
spawnxv(&s_info, "/hd0/main", (char **)spawn_argv);
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/io/keyboard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ KEYBOARD_MAP_SHIFT = StaticArray[

class Keyboard
@[Flags]
enum Modifiers
enum Modifiers : Int32
ShiftL = 1 << 0
ShiftR = 1 << 1
CtrlL = 1 << 3
Expand Down
59 changes: 56 additions & 3 deletions src/fs/impl/kbdfs.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
class KbdFsNode < VFSNode
getter fs
lib KbdFSData
@[Packed]
struct Packet
ch : Int32
modifiers : Int32
end
end

class KbdFSNode < VFSNode
getter fs, raw_node, first_child

def initialize(@fs : KbdFS)
@raw_node = @first_child = KbdFSRawNode.new(fs)
end

def each_child(&block)
node = first_child
while !node.nil?
yield node.not_nil!
node = node.next_node
end
end

def open(path)
each_child do |node|
if node.name == path
return node
end
end
end

def read(slice : Slice, offset : UInt32,
Expand Down Expand Up @@ -40,14 +65,39 @@ class KbdFsNode < VFSNode

end

class KbdFSRawNode < VFSNode
getter name, fs

def initialize(@fs : KbdFS)
@name = GcString.new "raw"
end

@ch = 0
@modifiers = 0
property ch, modifiers

def read(slice : Slice, offset : UInt32,
process : Multiprocessing::Process? = nil) : Int32
packet = uninitialized KbdFSData::Packet
packet.ch = ch
packet.modifiers = modifiers
@ch = 0
@modifiers = 0
size = min slice.size, sizeof(KbdFSData::Packet)
memcpy(slice.to_unsafe, pointerof(packet).as(UInt8*), size.to_usize)
size
end

end

class KbdFS < VFS
getter name

getter queue

def initialize(@kbd : Keyboard)
@name = GcString.new "kbd"
@root = KbdFsNode.new self
@root = KbdFSNode.new self
@kbd.kbdfs = self
@queue = VFSQueue.new
end
Expand Down Expand Up @@ -103,6 +153,9 @@ class KbdFS < VFS
end
end

@root.not_nil!.raw_node.ch = ch.ord.to_i32
@root.not_nil!.raw_node.modifiers = @kbd.modifiers.value

@queue.not_nil!.keep_if do |msg|
case msg.buffering
when VFSNode::Buffering::Unbuffered
Expand Down
2 changes: 1 addition & 1 deletion userspace/libc/src/include/sys/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct fbdev_bitblit {
unsigned long *source;
unsigned long x, y, width, height;
int type;
};
} __attribute__((packed));

// ioctl values
#define GFX_BITBLIT 3
Expand Down
2 changes: 1 addition & 1 deletion userspace/libc/src/include/sys/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct winsize {
unsigned short ws_col; /* columns, in characters */
unsigned short ws_xpixel; /* horizontal size, pixels */
unsigned short ws_ypixel; /* vertical size, pixels */
};
} __attribute__((packed));

int ioctl(int fd, int request, void *arg);

Expand Down
6 changes: 6 additions & 0 deletions userspace/libc/src/include/sys/kbd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

struct mouse_packet {
int ch;
int modifiers;
} __attribute__((packed));
2 changes: 1 addition & 1 deletion userspace/libc/src/include/sys/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

struct mouse_packet {
unsigned long x, y, attr_byte;
};
} __attribute__((packed));

#define MOUSE_ATTR_LEFT_BTN (1 << 0)
#define MOUSE_ATTR_RIGHT_BTN (1 << 1)
Expand Down

0 comments on commit c81f306

Please sign in to comment.