Skip to content

Commit

Permalink
refactor: update tty driver & sbin/init
Browse files Browse the repository at this point in the history
  • Loading branch information
hzcx998 committed Feb 3, 2021
1 parent 42e1433 commit 55e3497
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 91 deletions.
6 changes: 2 additions & 4 deletions src/arch/x86/mach-i386/drivers/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ typedef struct _device_extension {
int column; /* 数据位于哪一列 */

fifo_io_t fifoio;
unsigned int keycode; /* 解析出来的键值 */
input_even_buf_t evbuf; /* 事件缓冲区 */
uint32_t flags;
uint32_t opened;
Expand Down Expand Up @@ -979,6 +978,7 @@ iostatus_t keyboard_read(device_object_t *device, io_request_t *ioreq)
if (even && ioreq->parame.read.length == sizeof(input_event_t)) {

if (ext->flags & DEV_NOWAIT) {
// 解析
if (input_even_get(&ext->evbuf, even) < 0) {
status = IO_FAILED;
} else {
Expand All @@ -989,6 +989,7 @@ iostatus_t keyboard_read(device_object_t *device, io_request_t *ioreq)
}
} else {
while (1) {
// 解析
if (!input_even_get(&ext->evbuf, even))
break;
task_yeild();
Expand Down Expand Up @@ -1062,8 +1063,6 @@ void kbd_thread(void *arg) {
keprint(PRINT_DEBUG "key even set: type=%d code=%x value=%d\n", e.type, e.code, e.value);
keprint(PRINT_DEBUG "key even buf: head=%d tail=%d\n", ext->evbuf.head, ext->evbuf.tail);
#endif
/* 解析成输入数据并放到缓冲区中 */
ext->keycode = key;
#ifdef DEBUG_DRV
keprint(PRINT_DEBUG "kbd_thread: key:%c\n", key);
#endif
Expand Down Expand Up @@ -1091,7 +1090,6 @@ static iostatus_t keyboard_enter(driver_object_t *driver)
devext->device_object = devobj;

devext->irq = IRQ1;
devext->keycode = 0;
devext->flags = 0;
devext->opened = 0;

Expand Down
32 changes: 18 additions & 14 deletions src/drivers/tty/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,24 @@ iostatus_t tty_read(device_object_t *device, io_request_t *ioreq)
device_extension_t *extension = device->device_extension;

iostatus_t status = IO_FAILED;
if (extension->public->current_device == extension->device_id) { /* 可拜访者设备id */
// if (extension->public->current_device == extension->device_id) { /* 可拜访者设备id */
// get key code from keyboard fifo buf
uint8_t *buf = ioreq->user_buffer;
size_t len = ioreq->parame.read.length;
size_t read_count = 0;
if (extension->public->current_device == TTY_DEVICE_RAW) {
// 判断是否有数据
if (extension->flags & TTYFLG_NOWAIT) {
int iolen = fifo_io_len(&extension->fifoio);
if (iolen <= 0) {
status = IO_FAILED;
goto end_read;
}
// 判断是否有数据
if (extension->flags & TTYFLG_NOWAIT) {
int iolen = fifo_io_len(&extension->fifoio);
if (iolen <= 0) {
status = IO_FAILED;
goto end_read;
}
}
// 等待是当前设备
while (extension->public->current_device != extension->device_id) {
task_yeild();
}
if (extension->public->current_device == TTY_DEVICE_RAW) {
while (len > 0) {
uint8_t code = fifo_io_get(&extension->fifoio);
if (code > 0) {
Expand All @@ -188,6 +192,7 @@ iostatus_t tty_read(device_object_t *device, io_request_t *ioreq)
} else {
/* 前台任务 */
if (extension->hold_pid == task_current->pid) {

while (len > 0) {
uint8_t code = fifo_io_get(&extension->fifoio);
if (code > 0) {
Expand All @@ -208,7 +213,7 @@ iostatus_t tty_read(device_object_t *device, io_request_t *ioreq)
}
}

}
// }
end_read:
ioreq->io_status.status = status;
/* 调用完成请求 */
Expand All @@ -221,10 +226,8 @@ iostatus_t tty_write(device_object_t *device, io_request_t *ioreq)
{
device_extension_t *extension = device->device_extension;
iostatus_t status = IO_SUCCESS;

ioreq->io_status.infomation = device_write(extension->con,
ioreq->user_buffer, ioreq->parame.write.length, ioreq->parame.write.offset);

ioreq->io_status.status = status;
/* 调用完成请求 */
io_complete_request(ioreq);
Expand All @@ -240,6 +243,9 @@ iostatus_t tty_devctl(device_object_t *device, io_request_t *ioreq)
ssize_t retval = 0;
switch (ioreq->parame.devctl.code)
{
case TTYIO_SELECT:
tty_set_current(extension, *(unsigned long *)arg);
break;
case TTYIO_HOLDER:
extension->hold_pid = *(unsigned long *) ioreq->parame.devctl.arg;
break;
Expand Down Expand Up @@ -281,7 +287,6 @@ static int tty_process(device_extension_t *extension, int code)
case KEY_F8:
{
int device_id = code - KEY_F1;
keprint("tty: set new visitor %d\n", device_id);
tty_set_current(extension, device_id);
}
return 0;
Expand Down Expand Up @@ -329,7 +334,6 @@ void tty_thread(void *arg)
/* put into fifo buf */
fifo_io_put(&extension->fifoio, event.code);
fifo_io_put(&extension->fifoio, 0); // key down

}
}
} else {
Expand Down
1 change: 0 additions & 1 deletion src/drivers/view/core/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ int view_env_try_resize(view_t *view, view_rect_t *rect)
return -1;
if (view_resize(view, rect->x, rect->y, rect->w.uw, rect->h.uh) < 0)
return -1;
keprint("view id %d\n", view->id);
view_msg_t m;
view_msg_header(&m, VIEW_MSG_RESIZE, view->id);
view_msg_data(&m, rect->x, rect->y, rect->w.uw, rect->h.uh);
Expand Down
62 changes: 3 additions & 59 deletions src/drivers/view/hal/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,6 @@
#include <unistd.h>
#include "drivers/view/hal.h"

#if 0

#ifndef DEV_NAME
#define DEV_NAME "kbd"
#endif

int view_keyboard_open(view_keyboard_t *keyboard)
{
keyboard->handle = device_open(DEV_NAME, 0);
if (keyboard->handle < 0)
return -1;
keyboard->ledstate = 0;
int ledstate;
device_devctl(keyboard->handle, EVENIO_GETLED, (unsigned long) &ledstate);
if ( ledstate&0x01 )
keyboard->ledstate |= VIEW_KMOD_NUM;

if ( ledstate & 0x02 )
keyboard->ledstate |= VIEW_KMOD_CAPS;
int flags = DEV_NOWAIT; // no block
device_devctl(keyboard->handle, EVENIO_SETFLG, (unsigned long) &flags);
return 0;
}

int view_keyboard_close(view_keyboard_t *keyboard)
{
#if 0 /* 需要取消原来的设置,但是目前打开了 */
int flags;
device_devctl(keyboard->handle, EVENIO_GETFLG, (unsigned long) &flags);
flags &= ~DEV_NOWAIT;
device_devctl(keyboard->handle, EVENIO_SETFLG, (unsigned long) &flags);
#endif
return device_close(keyboard->handle);
}

int view_keyboard_read(view_keyboard_t *keyboard)
{
struct input_event event;
int ret = 0;
memset(&event, 0, sizeof(event));
ret = device_read(keyboard->handle, &event, sizeof(event), 0);
if (ret < 0)
return -1;
switch (event.type) {
case EV_KEY:
if ( (event.value) > 0 ) {
return keyboard->key_down(event.code);
} else {
return keyboard->key_up(event.code);
}
default:
break;
}
return -1;
}
#else

#ifndef DEV_NAME
#define DEV_NAME "tty7"
#endif
Expand All @@ -75,6 +18,9 @@ int view_keyboard_open(view_keyboard_t *keyboard)
keyboard->ledstate = 0;
int flags = TTYFLG_NOWAIT; // no block
device_devctl(keyboard->handle, TIOCSFLGS, (unsigned long) &flags);
/* 选择当前tty为当前tty */
flags = TTYIO_RAW;
device_devctl(keyboard->handle, TTYIO_SELECT, (unsigned long) &flags);
return 0;
}

Expand All @@ -95,7 +41,6 @@ int view_keyboard_read(view_keyboard_t *keyboard)
int ret = 0;
ret = device_read(keyboard->handle, ttybuf, 2, 0);
if (ret < 0) {

return -1;
}
if (!ttybuf[1]) {
Expand All @@ -107,4 +52,3 @@ int view_keyboard_read(view_keyboard_t *keyboard)
}
return -1;
}
#endif
3 changes: 2 additions & 1 deletion src/include/sys/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
#define TTYIO_SETPOS CONIO_SETPOS
#define TTYIO_GETPOS CONIO_GETPOS
#define TTYIO_HOLDER DEVCTL_CODE('t', 1)
#define TTYIO_SELECT DEVCTL_CODE('t', 2)
#define TIOCGPTN DEVCTL_CODE('t', 5) /* get presudo tty number */
#define TIOCSPTLCK DEVCTL_CODE('t', 6) /* set presudo tty lock */
#define TIOCSFLGS DEVCTL_CODE('t', 7) /* set flags */
#define TIOCGFLGS DEVCTL_CODE('t', 8) /* get flags */
#define TIOCGFG DEVCTL_CODE('t', 9) /* get front group task */

#define TTYIO_RAW 7

/* tty flags */
#define TTYFLG_ECHO 0x01
Expand Down
Binary file modified user/bin/desktop/desktop
Binary file not shown.
4 changes: 3 additions & 1 deletion user/bin/desktop/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ int open_desktop()
int main(int argc, char *argv[])
{
// 分离当前的tty和键盘
// ioctl(STDIN_FILENO, TTYIO_DETACH, 0);
/*unsigned long raw = TTYIO_RAW;
ioctl(STDIN_FILENO, TTYIO_SELECT, &raw);
*/
if (open_desktop() < 0)
return -1;
return 0;
Expand Down
25 changes: 17 additions & 8 deletions user/bin/infones/infoNES_System_Book.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
#define SOUND_DEVICE "sb16"
#endif /* CONFIG_SOUND */


#define _USE_XTK_MMAP
// #define _USE_FPS_CALC

#ifdef _USE_FPS_CALC
#define FPS_TIMEOUT 1000
#endif

#define MAX_DELAYS 10

#define DELAY_MS 0
void start_application( char *filename );
int InfoNES_Load( const char *pszFileName );
void InfoNES_Main();
Expand Down Expand Up @@ -59,8 +66,6 @@ WORD NesPalette[ 64 ] =
short final_wave[2048];
int sound_fd;

#define XTK_USE_MMAP

xtk_spirit_t *window;
xtk_surface_t *surface;
xtk_surface_t *tmp_surface;
Expand Down Expand Up @@ -99,7 +104,7 @@ int main(int argc, char **argv)
void exit_application()
{
InfoNES_Fin(); /* 结束nes */
#ifdef XTK_USE_MMAP
#ifdef _USE_XTK_MMAP
xtk_window_munmap(XTK_WINDOW(window));
#endif
xtk_main_quit();
Expand Down Expand Up @@ -142,7 +147,7 @@ void start_application( char *filename )
printf("[infones] create window failed!\n");
return;
}
#ifdef XTK_USE_MMAP
#ifdef _USE_XTK_MMAP
if (xtk_window_mmap(XTK_WINDOW(window)) < 0) {
xtk_spirit_destroy(window);
return;
Expand Down Expand Up @@ -170,7 +175,9 @@ void start_application( char *filename )
/* 设置键盘按键信号 */
xtk_signal_connect(window, "key_press", win_key_press, NULL);
xtk_signal_connect(window, "key_release", win_key_release, NULL);
#ifdef _USE_FPS_CALC
xtk_window_add_timer(XTK_WINDOW(window), FPS_TIMEOUT, win_timer, NULL);
#endif
InfoNES_Main();
} else {
printf("[infones] InfoNES_Load failed!\n");
Expand Down Expand Up @@ -278,6 +285,7 @@ static bool win_key_release(xtk_spirit_t *spirit, xtk_event_t *event, void *arg)
return true;
}

#ifdef _USE_FPS_CALC
static int win_fps = 0;
static bool win_timer(xtk_spirit_t *spirit, uint32_t tmid, void *arg)
{
Expand All @@ -289,7 +297,7 @@ static bool win_timer(xtk_spirit_t *spirit, uint32_t tmid, void *arg)
win_fps = 0;
return true; // 需要继续产生
}

#endif
/*===================================================================*/
/* */
/* LoadSRAM() : Load a SRAM */
Expand Down Expand Up @@ -598,7 +606,7 @@ void InfoNES_LoadFrame()
}
}
// 调整到窗口surface
#ifndef XTK_USE_MMAP
#ifndef _USE_XTK_MMAP
xtk_surface_blit_scaled(tmp_surface, NULL, surface, NULL);
xtk_window_flip(XTK_WINDOW(window));
#else
Expand All @@ -607,7 +615,9 @@ void InfoNES_LoadFrame()
xtk_surface_blit_scaled(tmp_surface, NULL, surface, &rect);
xtk_window_refresh(XTK_WINDOW(window), 0, 0, window->width, window->height);
#endif
#ifdef _USE_FPS_CALC
win_fps++;
#endif
/*
* 延时计算算法:
* base = 原始大小
Expand All @@ -617,7 +627,6 @@ void InfoNES_LoadFrame()
* coutn = distacne / single
* delay = max_delay - count
*/
#define MAX_DELAYS 10
uint32_t distance_bytes = (window->width, window->height * sizeof(uint32_t)) - (NES_DISP_HEIGHT * NES_DISP_WIDTH * 4);
uint32_t single = distance_bytes / MAX_DELAYS;
uint32_t countn = 0;
Expand Down
6 changes: 4 additions & 2 deletions user/bin/sh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main(int argc, char *argv[])
print_logo();

/* 启动自行服务 */
#if 1
#if 0
char *args[2] = {"desktop", NULL};
pid = create_process(args, environ, 0);
#endif
Expand Down Expand Up @@ -124,7 +124,9 @@ void readline(char *buf, uint32_t count)
char *pos = buf;
while (len < count)
{
read(STDIN_FILENO, pos, 1);
if (read(STDIN_FILENO, pos, 1) < 0)
continue;

if (*pos == '\n') {
*(pos) = '\0'; // 修改成0
break;
Expand Down
2 changes: 2 additions & 0 deletions user/libs/xlibc/include/sys/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ extern "C" {
#define TTYIO_SETPOS CONIO_SETPOS
#define TTYIO_GETPOS CONIO_GETPOS
#define TTYIO_HOLDER DEVCTL_CODE('t', 1)
#define TTYIO_SELECT DEVCTL_CODE('t', 2)
#define TIOCGPTN DEVCTL_CODE('t', 5) /* get presudo tty number */
#define TIOCSPTLCK DEVCTL_CODE('t', 6) /* set presudo tty lock */
#define TIOCSFLGS DEVCTL_CODE('t', 7) /* set flags */
#define TIOCGFLGS DEVCTL_CODE('t', 8) /* get flags */
#define TIOCGFG DEVCTL_CODE('t', 9) /* get front group task */
#define TTYIO_RAW 7

/* tty flags */
#define TTYFLG_ECHO 0x01
Expand Down
Loading

0 comments on commit 55e3497

Please sign in to comment.