annotate src/cons_supp.c @ 1116:9163eefa0039

console backend
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 08 Dec 2010 23:41:15 +0800
parents
children 1de8bb740c46
rev   line source
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
2 // vim: sw=4:ts=8:sts=4
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3 #include <stdio.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4 #include <stdlib.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
5 #include <string.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
6 #include <unistd.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
7 #include <fcntl.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
8 #include <poll.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
9 #include "mb_graph_engine.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
10 #include "mb_redraw_man.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11 #include "mb_timer.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
12 #include "mb_cons_supp.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13 #include "mb_backend.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14 #include "mb_backend_utils.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15 #include "config.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17 #define ERR -1
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18 #define OK 0
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20 #define FALSE 0
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21 #define TRUE 1
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
22
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
23 #define ASSERT(x)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
24
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
25 #define ONLY_MOUSE_MOVE_RAW 1
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
26
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
27 typedef int keysym;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
28
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
29 static mb_timer_factory_t *_timer_factory = &tman_timer_factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
30
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
31 /*! \ingroup console_kb
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
32 * @{
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
33 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 struct _cons_kb_info {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 int kb_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
36
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37 int keycode_min, keycode_max;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 int ksym_per_code;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39 keysym *syms;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40 subject_t *kbevents;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41 observer_factory_t *observer_factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
42 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
43 typedef struct _cons_kb_info cons_kb_info_t;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
44
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
45 /* @} */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
46
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
47 struct _cons_supp_runtime {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
48 MB_DISPLAY display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
49
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
50 mbe_surface_t *surface;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51 mbe_t *cr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 redraw_man_t *rdman;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
53 mb_img_ldr_t *img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
54 int w, h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
55
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
56 cons_kb_info_t kbinfo;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
57 mb_IO_man_t *io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
58 mb_timer_man_t *timer_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 #ifndef ONLY_MOUSE_MOVE_RAW
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
61 /* States */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
62 shape_t *last;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
63 #endif
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
64
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
65 /* For handle connection */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
66 int io_hdl;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
67
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
68 /*
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
69 * Following variables are used by handle_single_cons_event()
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
70 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
71 int last_evt_type; /* Type of last event */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
72 int ex1, ey1, ex2, ey2; /* Aggregate expose events */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73 int mx, my; /* Position of last motion event */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74 int mbut_state; /* Button state of last motion event */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76 typedef struct _cons_supp_runtime cons_supp_runtime_t;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
77
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
78 static void _cons_supp_handle_cons_event(cons_supp_runtime_t *rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
79
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
80 /*! \defgroup cons_supp_io IO manager for console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
81 * @{
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
82 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
83 #define MAX_MONITORS 200
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
84
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
85 typedef struct {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
86 int type;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
87 int fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88 mb_IO_cb_t cb;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
89 void *data;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
90 } monitor_t;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 struct _cons_supp_IO_man {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 mb_IO_man_t io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94 monitor_t monitors[MAX_MONITORS];
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95 int n_monitor;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
96 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
97
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
98 static int _cons_supp_io_man_reg(struct _mb_IO_man *io_man,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
99 int fd, MB_IO_TYPE type,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
100 mb_IO_cb_t cb, void *data);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
101 static void _cons_supp_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 static mb_IO_man_t *_cons_supp_io_man_new(void);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
103 static void _cons_supp_io_man_free(mb_IO_man_t *io_man);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
105 static mb_IO_factory_t _cons_supp_default_io_factory = {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
106 _cons_supp_io_man_new,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
107 _cons_supp_io_man_free
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
109 static mb_IO_factory_t *_io_factory = &_cons_supp_default_io_factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
111 static struct _cons_supp_IO_man _default_io_man = {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
112 {_cons_supp_io_man_reg, _cons_supp_io_man_unreg},
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
113 {}, /* monitors */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
114 0 /* n_monitor */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
115 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
117 static mb_IO_man_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
118 _cons_supp_io_man_new(void) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
119 return (mb_IO_man_t *)&_default_io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
120 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
121
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
122 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
123 _cons_supp_io_man_free(mb_IO_man_t *io_man) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
124 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
125
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
127 _cons_supp_io_man_reg(struct _mb_IO_man *io_man,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
128 int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
129 struct _cons_supp_IO_man *cmb_io_man = (struct _cons_supp_IO_man *)io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
130 int i;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
131
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
132 for(i = 0; i < cmb_io_man->n_monitor; i++) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
133 if (cmb_io_man->monitors[i].type == MB_IO_DUMMY)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
134 break;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
135 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
136 if (i == MAX_MONITORS)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
137 return ERR;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
138
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
139 cmb_io_man->monitors[i].type = type;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
140 cmb_io_man->monitors[i].fd = fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
141 cmb_io_man->monitors[i].cb = cb;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
142 cmb_io_man->monitors[i].data = data;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
143 i++;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
144 if(i > cmb_io_man->n_monitor)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
145 cmb_io_man->n_monitor = i;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
146 return i - 1;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
147 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
148
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
149 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
150 _cons_supp_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
151 struct _cons_supp_IO_man *cmb_io_man = (struct _cons_supp_IO_man *)io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
152
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
153 ASSERT(io_hdl < cmb_io_man->n_monitor);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
154 cmb_io_man->monitors[io_hdl].type = MB_IO_DUMMY;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
155 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
156
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
157 /*! \brief Handle connection coming data and timeout of timers.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
158 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
159 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
160 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
161 _cons_supp_event_loop(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
162 struct _cons_supp_runtime *cmb_rt = (struct _cons_supp_runtime *)rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
163 struct _cons_supp_IO_man *io_man =
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
164 (struct _cons_supp_IO_man *)cmb_rt->io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
165 mb_timer_man_t *timer_man = (mb_timer_man_t *)cmb_rt->timer_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
166 redraw_man_t *rdman;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
167 mb_tman_t *tman = tman_timer_man_get_tman(timer_man);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
168 mb_timeval_t now, tmo;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
169 struct timeval tv;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
170 fd_set rfds, wfds;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
171 int nfds = 0;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
172 int r, r1,i;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
173
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
174 rdman = mb_runtime_rdman(rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
175 rdman_redraw_all(rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
176
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
177 _cons_supp_handle_cons_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
178
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
179 while(1) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
180 FD_ZERO(&rfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
181 FD_ZERO(&wfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
182 for(i = 0; i < io_man->n_monitor; i++) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
183 if(io_man->monitors[i].type == MB_IO_R ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
184 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
185 FD_SET(io_man->monitors[i].fd, &rfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
186 nfds = MB_MAX(nfds, io_man->monitors[i].fd + 1);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
187 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
188 if(io_man->monitors[i].type == MB_IO_W ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
189 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
190 FD_SET(io_man->monitors[i].fd, &wfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
191 nfds = MB_MAX(nfds, io_man->monitors[i].fd + 1);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
192 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
193 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
194
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
195 get_now(&now);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
196 r = mb_tman_next_timeout(tman, &now, &tmo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
197
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
198 if(r == 0) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
199 tv.tv_sec = MB_TIMEVAL_SEC(&tmo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
200 tv.tv_usec = MB_TIMEVAL_USEC(&tmo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
201 r1 = select(nfds, &rfds, NULL, NULL, &tv);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
202 } else
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
203 r1 = select(nfds, &rfds, NULL, NULL, NULL);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
204
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
205 if(r1 == -1) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
206 perror("select");
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
207 break;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
208 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
209
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
210 if(r1 == 0) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
211 get_now(&now);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
212 mb_tman_handle_timeout(tman, &now);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
213 rdman_redraw_changed(rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
214 } else {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
215 for(i = 0; i < io_man->n_monitor; i++) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
216 if(io_man->monitors[i].type == MB_IO_R ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
217 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
218 if(FD_ISSET(io_man->monitors[i].fd, &rfds))
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
219 io_man->monitors[i].cb(i, io_man->monitors[i].fd,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
220 MB_IO_R,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
221 io_man->monitors[i].data);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
222 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
223 if(io_man->monitors[i].type == MB_IO_W ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
224 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
225 if(FD_ISSET(io_man->monitors[i].fd, &wfds))
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
226 io_man->monitors[i].cb(i, io_man->monitors[i].fd,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
227 MB_IO_W,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
228 io_man->monitors[i].data);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
229 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
230 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
231 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
232 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
233 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
234
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
235 /* @} */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
236
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
237 /*! \defgroup console_kb Console Keyboard Handling
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
238 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
239 * Accept keyboard events from console and delivery it to
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
240 * application through observer pattern. There is a subject,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
241 * per X-connection, for that.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
242 * @{
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
243 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
244 static int keycode2sym(cons_kb_info_t *kbinfo, unsigned int keycode) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
245 /* TODO: implement keycode to key symbol translation */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
246 return 0;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
247 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
248
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
249 static int cons_kb_init(cons_kb_info_t *kbinfo, MB_DISPLAY display,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
250 redraw_man_t *rdman) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
251 int n_syms;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
252 observer_factory_t *factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
253 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
254
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
255 /* TODO: set keycode_min, keycode_max and syms */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
256 if((int)display != -1)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
257 kbinfo->kb_fd = (int)display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
258 else
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
259 kbinfo->kb_fd = STDIN_FILENO;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
260
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
261 factory = rdman_get_observer_factory(rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
262 kbinfo->kbevents = subject_new(factory, kbinfo, OBJT_KB);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
263 if(kbinfo->kbevents == NULL)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
264 return ERR;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
265 /*! \todo Make sure observer_factory is still need. */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
266 kbinfo->observer_factory = factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
267
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
268 return OK;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
269 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
270
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
271 static void cons_kb_destroy(cons_kb_info_t *kbinfo) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
272 subject_free(kbinfo->kbevents);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
273 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
274 /* @} */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
275
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
276 /*! \brief Notify observers of the shape at specified
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
277 * position for mouse event.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
278 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
279 * Observers of parent shapes may be called if the subject is not
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
280 * with SUBF_STOP_PROPAGATE flag. The subject of mouse event
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
281 * for a shape is returned by sh_get_mouse_event_subject().
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
282 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
283 static void notify_coord_or_shape(redraw_man_t *rdman,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
284 mb_obj_t *obj,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
285 co_aix x, co_aix y, int etype,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
286 unsigned int state,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
287 unsigned int button) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
288 mouse_event_t mouse_event;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
289 subject_t *subject;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
290
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
291 mouse_event.event.type = etype;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
292 mouse_event.x = x;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
293 mouse_event.y = y;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
294 mouse_event.but_state = state;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
295 mouse_event.button = button;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
296
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
297 if(IS_MBO_SHAPES(obj))
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
298 subject = sh_get_mouse_event_subject((shape_t *)obj);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
299 else
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
300 subject = coord_get_mouse_event((coord_t *)obj);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
301
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
302 subject_notify(subject, (event_t *)&mouse_event);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
303 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
304
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
305 /*! \brief Handle keyboard event and maintain internal states.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
306 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
307 * It keeps internal state in rt to improve performance.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
308 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
309 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
310 handle_single_cons_event(cons_supp_runtime_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
311 /* TODO: handle keyboard and mouse events. */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
312 printf("handle_single_cons_event() will be implemented later\n");
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
313 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
314
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
315 /*! \brief Call when no more event in an event iteration.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
316 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
317 * No more event means event queue is emplty. This function will
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
318 * perform some actions according current internal state.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
319 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
320 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
321 no_more_event(cons_supp_runtime_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
322 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
323
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
324 /*! \brief Dispatch all console events in the queue.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
325 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
326 static void _cons_supp_handle_cons_event(cons_supp_runtime_t *cmb_rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
327 int console_fd = (int)cmb_rt->display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
328 struct pollfd pfd = {console_fd, POLLIN, 0};
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
329 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
330
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
331 while((r = poll(&pfd, 1, 0)) > 0) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
332 handle_single_cons_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
333 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
334 no_more_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
335 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
336
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
337 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
338 _cons_supp_handle_connection(int hdl, int fd, MB_IO_TYPE type, void *data) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
339 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *)data;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
340
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
341 _cons_supp_handle_cons_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
342 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
343
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
344 /*! \brief Initialize a MadButterfy runtime for Xlib.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
345 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
346 * This one is very like _cons_supp_init(), except it accepts a
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
347 * cons_supp_runtime_t object initialized with a display connected to a X
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
348 * server and an opened window.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
349 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
350 * Following field of the cons_supp_runtime_t object should be initialized.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
351 * - w, h
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
352 * - win
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
353 * - display
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
354 * - visual
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
355 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
356 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
357 _cons_supp_init_with_win_internal(cons_supp_runtime_t *cmb_rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
358 mb_img_ldr_t *img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
359 int w, h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
360 int console_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
361
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
362 w = cmb_rt->w;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
363 h = cmb_rt->h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
364
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
365 mbe_init();
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
366
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
367 cmb_rt->surface =
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
368 mbe_win_surface_create(cmb_rt->display, NULL,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
369 MB_IFMT_ARGB32, w, h);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
370
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
371 cmb_rt->cr = mbe_create(cmb_rt->surface);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
372
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
373 cmb_rt->rdman = (redraw_man_t *)malloc(sizeof(redraw_man_t));
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
374 redraw_man_init(cmb_rt->rdman, cmb_rt->cr, NULL);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
375 cmb_rt->rdman->w = w;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
376 cmb_rt->rdman->h = h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
377 /* FIXME: This is a wired loopback reference. This is inly
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
378 * required when we need to get the cmb_rt->tman for the
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
379 * animation. We should relocate the tman to the
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
380 * redraw_man_t instead.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
381 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
382 cmb_rt->rdman->rt = cmb_rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
383
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
384 cmb_rt->io_man = mb_io_man_new(_io_factory);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
385 cmb_rt->timer_man = mb_timer_man_new(_timer_factory);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
386
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
387 img_ldr = simple_mb_img_ldr_new("");
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
388 cmb_rt->img_ldr = img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
389 /*! \todo Remove rdman_set_img_ldr() */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
390 rdman_set_img_ldr(cmb_rt->rdman, img_ldr); /* this is ncessary? */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
391
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
392 #ifndef ONLY_MOUSE_MOVE_RAW
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
393 cmb_rt->last = NULL;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
394 #endif
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
395
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
396 cons_kb_init(&cmb_rt->kbinfo, cmb_rt->display, cmb_rt->rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
397
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
398 console_fd = (int)cmb_rt->display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
399 cmb_rt->io_hdl = mb_io_man_reg(cmb_rt->io_man, console_fd,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
400 MB_IO_R,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
401 _cons_supp_handle_connection,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
402 cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
403
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
404 return OK;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
405 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
406
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
407 /*! \brief Initialize a MadButterfy runtime for console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
408 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
409 * It setups a runtime environment to run MadButterfly with console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
410 * Users should specify width and height of the opening window.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
411 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
412 * \param display_name is actually the path to the console/input device.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
413 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
414 static int _cons_supp_init(cons_supp_runtime_t *cmb_rt,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
415 const char *display_name,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
416 int w, int h) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
417 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
418 int console_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
419
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
420 memset(cmb_rt, 0, sizeof(cons_supp_runtime_t));
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
421
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
422 if(display_name == NULL || strlen(display_name) == 0)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
423 console_fd = STDIN_FILENO;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
424 else {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
425 console_fd = open(display_name, O_RDONLY);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
426 if(console_fd == -1)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
427 return ERR;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
428 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
429
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
430 cmb_rt->display = (MB_DISPLAY)console_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
431 cmb_rt->w = w;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
432 cmb_rt->h = h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
433
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
434 r = _cons_supp_init_with_win_internal(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
435
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
436 return r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
437 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
438
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
439 /*! \brief Initialize a MadButterfly runtime for a window of console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
440 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
441 * This function is equivalent to _cons_supp_init() with fixed width
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
442 * and height. Since, there is no window for console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
443 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
444 * Runtimes initialized with this function should be destroyed with
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
445 * cons_supp_destroy_keep_win().
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
446 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
447 * \param display is actually a file descriptor of console (input device).
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
448 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
449 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
450 _cons_supp_init_with_win(cons_supp_runtime_t *cmb_rt,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
451 MB_DISPLAY display, MB_WINDOW win) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
452 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
453
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
454 memset(cmb_rt, 0, sizeof(cons_supp_runtime_t));
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
455
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
456 cmb_rt->display = display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
457 cmb_rt->w = 800;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
458 cmb_rt->h = 600;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
459
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
460 r = _cons_supp_init_with_win_internal(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
461
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
462 return r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
463 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
464
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
465 static void cons_supp_destroy_keep_win(cons_supp_runtime_t *cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
466
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
467 static void cons_supp_destroy(cons_supp_runtime_t *cmb_rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
468 int console_fd = cmb_rt = (int)cmb_rt->display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
469
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
470 close(console_fd);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
471 cons_supp_destroy_keep_win(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
472 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
473
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
474 /*! \brief Destroy a MadButterfly runtime initialized with
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
475 * _cons_supp_init_with_win().
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
476 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
477 * Destroying a runtime with this function prevent the window and
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
478 * display associated with the runtime being closed.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
479 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
480 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
481 cons_supp_destroy_keep_win(cons_supp_runtime_t *cmb_rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
482 if(cmb_rt->rdman) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
483 redraw_man_destroy(cmb_rt->rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
484 free(cmb_rt->rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
485 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
486
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
487 if(cmb_rt->io_hdl)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
488 mb_io_man_unreg(cmb_rt->io_man, cmb_rt->io_hdl);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
489
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
490 if(cmb_rt->io_man)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
491 mb_io_man_free(_io_factory, cmb_rt->io_man);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
492 if(cmb_rt->timer_man)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
493 mb_timer_man_free(_timer_factory, cmb_rt->timer_man);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
494
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
495 if(cmb_rt->img_ldr)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
496 MB_IMG_LDR_FREE(cmb_rt->img_ldr);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
497
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
498 if(cmb_rt->cr)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
499 mbe_destroy(cmb_rt->cr);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
500
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
501 if(cmb_rt->surface)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
502 mbe_surface_destroy(cmb_rt->surface);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
503
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
504 cons_kb_destroy(&cmb_rt->kbinfo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
505 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
506
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
507 static mb_rt_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
508 _cons_supp_new(const char *display_name, int w, int h) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
509 cons_supp_runtime_t *rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
510 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
511
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
512 rt = O_ALLOC(cons_supp_runtime_t);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
513 if(rt == NULL)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
514 return NULL;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
515
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
516 r = _cons_supp_init(rt, display_name, w, h);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
517 if(r != OK) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
518 free(rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
519 return NULL;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
520 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
521
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
522 return (mb_rt_t *)rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
523 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
524
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
525 /*! \brief Create a new runtime for existed window for X.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
526 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
527 * The object returned by this function must be free with
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
528 * _cons_supp_free_keep_win() to prevent the window from closed.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
529 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
530 static mb_rt_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
531 _cons_supp_new_with_win(MB_DISPLAY display, MB_WINDOW win) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
532 cons_supp_runtime_t *rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
533 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
534
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
535 rt = O_ALLOC(cons_supp_runtime_t);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
536 if(rt == NULL)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
537 return NULL;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
538
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
539 r = _cons_supp_init_with_win(rt, display, win);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
540 if(r != OK) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
541 free(rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
542 return NULL;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
543 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
544
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
545 return (mb_rt_t *)rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
546 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
547
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
548 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
549 _cons_supp_free(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
550 cons_supp_destroy((cons_supp_runtime_t *) rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
551 free(rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
552 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
553
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
554 /*! \brief Free runtime created with _cons_supp_new_with_win().
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
555 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
556 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
557 _cons_supp_free_keep_win(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
558 cons_supp_destroy_keep_win((cons_supp_runtime_t *) rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
559 free(rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
560 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
561
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
562 static subject_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
563 _cons_supp_kbevents(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
564 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
565 return cmb_rt->kbinfo.kbevents;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
566 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
567
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
568 static redraw_man_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
569 _cons_supp_rdman(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
570 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
571 return cmb_rt->rdman;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
572 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
573
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
574 static mb_timer_man_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
575 _cons_supp_timer_man(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
576 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
577 return cmb_rt->timer_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
578 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
579
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
580 static observer_factory_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
581 _cons_supp_observer_factory(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
582 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
583 observer_factory_t *factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
584
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
585 factory = rdman_get_observer_factory(cmb_rt->rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
586 return factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
587 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
588
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
589 static mb_img_ldr_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
590 _cons_supp_img_ldr(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
591 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
592 mb_img_ldr_t *img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
593
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
594 img_ldr = cmb_rt->img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
595
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
596 return img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
597 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
598
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
599 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
600 _cons_supp_add_event(mb_rt_t *rt, int fd, MB_IO_TYPE type,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
601 mb_IO_cb_t cb, void *data)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
602 {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
603 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
604 mb_IO_man_t *io_man = cmb_rt->io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
605 int hdl;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
606
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
607 hdl = mb_io_man_reg(io_man, fd, type, cb, data);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
608 return hdl;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
609 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
610
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
611 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
612 _cons_supp_remove_event(mb_rt_t *rt, int hdl)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
613 {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
614 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *) rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
615 mb_IO_man_t *io_man = cmb_rt->io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
616
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
617 mb_io_man_unreg(io_man, hdl);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
618 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
619
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
620 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
621 _cons_supp_flush(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
622 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *)rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
623
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
624 mbe_flush(cmb_rt->cr);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
625 return OK;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
626 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
627
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
628 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
629 _cons_supp_reg_IO_factory(mb_IO_factory_t *io_factory) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
630 _io_factory = io_factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
631 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
632
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
633 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
634 _cons_supp_reg_timer_factory(mb_timer_factory_t *timer_factory) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
635 _timer_factory = timer_factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
636 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
637
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
638 mb_backend_t mb_dfl_backend = { _cons_supp_new,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
639 _cons_supp_new_with_win,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
640
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
641 _cons_supp_free,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
642 _cons_supp_free_keep_win,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
643 _cons_supp_add_event,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
644 _cons_supp_remove_event,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
645 _cons_supp_event_loop,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
646 _cons_supp_flush,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
647
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
648 _cons_supp_kbevents,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
649 _cons_supp_rdman,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
650 _cons_supp_timer_man,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
651 _cons_supp_observer_factory,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
652 _cons_supp_img_ldr,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
653
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
654 _cons_supp_reg_IO_factory,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
655 _cons_supp_reg_timer_factory,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
656 };