[afa6e74] | 1 | #include <io/io.h>
|
---|
| 2 | #include <io/stream.h>
|
---|
| 3 | #include <string.h>
|
---|
| 4 | #include <malloc.h>
|
---|
| 5 | #include <libc.h>
|
---|
| 6 | #include <ipc/ipc.h>
|
---|
| 7 | #include <ipc/ns.h>
|
---|
| 8 | #include <ipc/fb.h>
|
---|
| 9 | #include <ipc/services.h>
|
---|
| 10 |
|
---|
| 11 | #define FDS 32
|
---|
| 12 |
|
---|
[04552a80] | 13 | typedef struct stream_t {
|
---|
[afa6e74] | 14 | pwritefn_t w;
|
---|
| 15 | preadfn_t r;
|
---|
| 16 | void * param;
|
---|
[04552a80] | 17 | } stream_t;
|
---|
[afa6e74] | 18 |
|
---|
| 19 |
|
---|
[04552a80] | 20 | typedef struct vfb_descriptor_t {
|
---|
[afa6e74] | 21 | int phone;
|
---|
| 22 | int vfb;
|
---|
[04552a80] | 23 | } vfb_descriptor_t;
|
---|
[afa6e74] | 24 |
|
---|
| 25 |
|
---|
[04552a80] | 26 | stream_t streams[FDS] = {{0, 0, 0}};
|
---|
[afa6e74] | 27 |
|
---|
| 28 | /*
|
---|
| 29 | ssize_t write_stdout(void *param, const void * buf, size_t count);
|
---|
| 30 | ssize_t write_stdout(void *param, const void * buf, size_t count)
|
---|
| 31 | {
|
---|
| 32 | return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
---|
| 33 | }*/
|
---|
| 34 |
|
---|
[04552a80] | 35 | static void vfb_send_char(vfb_descriptor_t *d, char c)
|
---|
[afa6e74] | 36 | {
|
---|
| 37 | ipcarg_t r0,r1;
|
---|
[04552a80] | 38 | ipc_call_sync_2(d->phone, FB_PUTCHAR, d->vfb, c, &r0, &r1);
|
---|
[afa6e74] | 39 | }
|
---|
| 40 |
|
---|
[04552a80] | 41 | static ssize_t write_vfb(void *param, const void *buf, size_t count)
|
---|
[afa6e74] | 42 | {
|
---|
| 43 | int i;
|
---|
[04552a80] | 44 | for (i = 0; i < count; i++)
|
---|
| 45 | vfb_send_char((vfb_descriptor_t *) param, ((char *) buf)[i]);
|
---|
[afa6e74] | 46 |
|
---|
| 47 | return count;
|
---|
| 48 | //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
[04552a80] | 52 | static ssize_t write_stderr(void *param, const void *buf, size_t count)
|
---|
[afa6e74] | 53 | {
|
---|
| 54 | return count;
|
---|
| 55 | //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | stream_t open_vfb(void)
|
---|
| 60 | {
|
---|
| 61 | stream_t stream;
|
---|
| 62 | vfb_descriptor_t *vfb;
|
---|
| 63 | int phoneid;
|
---|
| 64 | int res;
|
---|
| 65 | ipcarg_t vfb_no;
|
---|
| 66 |
|
---|
[04552a80] | 67 | while ((phoneid = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
|
---|
[afa6e74] | 68 | volatile int a;
|
---|
[04552a80] | 69 |
|
---|
| 70 | for (a = 0; a < 1048576; a++);
|
---|
[afa6e74] | 71 | }
|
---|
| 72 |
|
---|
[04552a80] | 73 | ipc_call_sync(phoneid, FB_GET_VFB, 0, &vfb_no);
|
---|
| 74 | vfb = malloc(sizeof(vfb_descriptor_t));
|
---|
[afa6e74] | 75 |
|
---|
[04552a80] | 76 | vfb->phone = phoneid;
|
---|
| 77 | vfb->vfb = vfb_no;
|
---|
[afa6e74] | 78 |
|
---|
[04552a80] | 79 | stream.w = write_vfb;
|
---|
| 80 | stream.param = vfb;
|
---|
[afa6e74] | 81 | return stream;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
[04552a80] | 85 | fd_t open(const char *fname, int flags)
|
---|
[afa6e74] | 86 | {
|
---|
[04552a80] | 87 | int c = 0;
|
---|
| 88 |
|
---|
| 89 | while (((streams[c].w) || (streams[c].r)) && (c < FDS))
|
---|
| 90 | c++;
|
---|
| 91 | if (c == FDS)
|
---|
| 92 | return EMFILE;
|
---|
| 93 |
|
---|
| 94 | if (!strcmp(fname, "stdin")) {
|
---|
| 95 | streams[c].r = (preadfn_t)1;
|
---|
[afa6e74] | 96 | return c;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[04552a80] | 99 | if (!strcmp(fname, "stdout")) {
|
---|
| 100 | //streams[c].w = write_stdout;
|
---|
[afa6e74] | 101 | //return c;
|
---|
[04552a80] | 102 | streams[c] = open_vfb();
|
---|
[afa6e74] | 103 | return c;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[04552a80] | 106 | if (!strcmp(fname, "stderr")) {
|
---|
| 107 | streams[c].w = write_stderr;
|
---|
[afa6e74] | 108 | return c;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
[04552a80] | 113 | ssize_t write(int fd, const void *buf, size_t count)
|
---|
[afa6e74] | 114 | {
|
---|
[04552a80] | 115 | if (fd < FDS)
|
---|
| 116 | return streams[fd].w(streams[fd].param, buf, count);
|
---|
| 117 |
|
---|
[afa6e74] | 118 | return 0;
|
---|
| 119 | }
|
---|