annotate src/cons_supp.c @ 1347:aaa88d805aac

Refactory composite() to smaller functions
author Thinker K.F. Li <thinker@codemud.net>
date Sat, 12 Feb 2011 18:21:16 +0800
parents 17cbb862a8c6
children
rev   line source
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 #include <stdio.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
2 #include <stdlib.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3 #include <string.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4 #include <unistd.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
5 #include <fcntl.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
6 #include <poll.h>
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
7 #include "mb_graph_engine.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
8 #include "mb_redraw_man.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
9 #include "mb_timer.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
10 #include "mb_cons_supp.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11 #include "mb_backend.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
12 #include "mb_backend_utils.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13 #include "config.h"
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15 #define ERR -1
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16 #define OK 0
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18 #define FALSE 0
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 #define TRUE 1
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21 #define ASSERT(x)
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 ONLY_MOUSE_MOVE_RAW 1
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 typedef int keysym;
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 static mb_timer_factory_t *_timer_factory = &tman_timer_factory;
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 /*! \ingroup console_kb
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 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
32 struct _cons_kb_info {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
33 int kb_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 int keycode_min, keycode_max;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
36 int ksym_per_code;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37 keysym *syms;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 subject_t *kbevents;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39 observer_factory_t *observer_factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41 typedef struct _cons_kb_info cons_kb_info_t;
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 /* @} */
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 struct _cons_supp_runtime {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
46 MB_DISPLAY display;
1117
1de8bb740c46 Add pre-created window for _cons_supp_new_with_win()
Thinker K.F. Li <thinker@codemud.net>
parents: 1116
diff changeset
47 MB_WINDOW win;
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
48
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
49 mbe_surface_t *surface;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
50 mbe_t *cr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51 redraw_man_t *rdman;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 mb_img_ldr_t *img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
53 int w, h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
54
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
55 cons_kb_info_t kbinfo;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
56 mb_IO_man_t *io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
57 mb_timer_man_t *timer_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
58
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59 #ifndef ONLY_MOUSE_MOVE_RAW
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 /* States */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
61 shape_t *last;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
62 #endif
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
63
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
64 /* For handle connection */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
65 int io_hdl;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
66
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 * Following variables are used by handle_single_cons_event()
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
69 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
70 int last_evt_type; /* Type of last event */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
71 int ex1, ey1, ex2, ey2; /* Aggregate expose events */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
72 int mx, my; /* Position of last motion event */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73 int mbut_state; /* Button state of last motion event */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 typedef struct _cons_supp_runtime cons_supp_runtime_t;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
77 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
78
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
79 /*! \defgroup cons_supp_io IO manager for console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
80 * @{
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 #define MAX_MONITORS 200
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
83
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
84 typedef struct {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
85 int type;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
86 int fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
87 mb_IO_cb_t cb;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88 void *data;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
89 } monitor_t;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
90
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91 struct _cons_supp_IO_man {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 mb_IO_man_t io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 monitor_t monitors[MAX_MONITORS];
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94 int n_monitor;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95 };
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 static int _cons_supp_io_man_reg(struct _mb_IO_man *io_man,
1118
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
98 int fd, MB_IO_TYPE type,
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
99 mb_IO_cb_t cb, void *data);
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
100 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
101 static mb_IO_man_t *_cons_supp_io_man_new(void);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 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
103
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104 static mb_IO_factory_t _cons_supp_default_io_factory = {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
105 _cons_supp_io_man_new,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
106 _cons_supp_io_man_free
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
107 };
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108 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
109
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110 static struct _cons_supp_IO_man _default_io_man = {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
111 {_cons_supp_io_man_reg, _cons_supp_io_man_unreg},
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
112 {}, /* monitors */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
113 0 /* n_monitor */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
114 };
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 static mb_IO_man_t *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
117 _cons_supp_io_man_new(void) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
118 return (mb_IO_man_t *)&_default_io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
119 }
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 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
122 _cons_supp_io_man_free(mb_IO_man_t *io_man) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
123 }
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 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126 _cons_supp_io_man_reg(struct _mb_IO_man *io_man,
1118
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
127 int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data) {
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
128 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
129 int i;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
130
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
131 for(i = 0; i < cmb_io_man->n_monitor; i++) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
132 if (cmb_io_man->monitors[i].type == MB_IO_DUMMY)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
133 break;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
134 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
135 if (i == MAX_MONITORS)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
136 return ERR;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
137
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
138 cmb_io_man->monitors[i].type = type;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
139 cmb_io_man->monitors[i].fd = fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
140 cmb_io_man->monitors[i].cb = cb;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
141 cmb_io_man->monitors[i].data = data;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
142 i++;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
143 if(i > cmb_io_man->n_monitor)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
144 cmb_io_man->n_monitor = i;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
145 return i - 1;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
146 }
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 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
149 _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
150 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
151
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
152 ASSERT(io_hdl < cmb_io_man->n_monitor);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
153 cmb_io_man->monitors[io_hdl].type = MB_IO_DUMMY;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
154 }
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 /*! \brief Handle connection coming data and timeout of timers.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
157 *
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 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
160 _cons_supp_event_loop(mb_rt_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
161 struct _cons_supp_runtime *cmb_rt = (struct _cons_supp_runtime *)rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
162 struct _cons_supp_IO_man *io_man =
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
163 (struct _cons_supp_IO_man *)cmb_rt->io_man;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
164 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
165 redraw_man_t *rdman;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
166 mb_tman_t *tman = tman_timer_man_get_tman(timer_man);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
167 mb_timeval_t now, tmo;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
168 struct timeval tv;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
169 fd_set rfds, wfds;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
170 int nfds = 0;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
171 int r, r1,i;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
172
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
173 rdman = mb_runtime_rdman(rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
174 rdman_redraw_all(rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
175
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
176 _cons_supp_handle_cons_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
177
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
178 while(1) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
179 FD_ZERO(&rfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
180 FD_ZERO(&wfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
181 for(i = 0; i < io_man->n_monitor; i++) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
182 if(io_man->monitors[i].type == MB_IO_R ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
183 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
184 FD_SET(io_man->monitors[i].fd, &rfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
185 nfds = MB_MAX(nfds, io_man->monitors[i].fd + 1);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
186 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
187 if(io_man->monitors[i].type == MB_IO_W ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
188 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
189 FD_SET(io_man->monitors[i].fd, &wfds);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
190 nfds = MB_MAX(nfds, io_man->monitors[i].fd + 1);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
191 }
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 get_now(&now);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
195 r = mb_tman_next_timeout(tman, &now, &tmo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
196
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
197 if(r == 0) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
198 tv.tv_sec = MB_TIMEVAL_SEC(&tmo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
199 tv.tv_usec = MB_TIMEVAL_USEC(&tmo);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
200 r1 = select(nfds, &rfds, NULL, NULL, &tv);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
201 } else
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
202 r1 = select(nfds, &rfds, NULL, NULL, NULL);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
203
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
204 if(r1 == -1) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
205 perror("select");
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
206 break;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
207 }
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 if(r1 == 0) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
210 get_now(&now);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
211 mb_tman_handle_timeout(tman, &now);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
212 rdman_redraw_changed(rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
213 } else {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
214 for(i = 0; i < io_man->n_monitor; i++) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
215 if(io_man->monitors[i].type == MB_IO_R ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
216 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
217 if(FD_ISSET(io_man->monitors[i].fd, &rfds))
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
218 io_man->monitors[i].cb(i, io_man->monitors[i].fd,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
219 MB_IO_R,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
220 io_man->monitors[i].data);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
221 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
222 if(io_man->monitors[i].type == MB_IO_W ||
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
223 io_man->monitors[i].type == MB_IO_RW) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
224 if(FD_ISSET(io_man->monitors[i].fd, &wfds))
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
225 io_man->monitors[i].cb(i, io_man->monitors[i].fd,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
226 MB_IO_W,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
227 io_man->monitors[i].data);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
228 }
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 /*! \defgroup console_kb Console Keyboard Handling
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
237 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
238 * Accept keyboard events from console and delivery it to
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
239 * application through observer pattern. There is a subject,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
240 * per X-connection, for that.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
241 * @{
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 static int keycode2sym(cons_kb_info_t *kbinfo, unsigned int keycode) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
244 /* TODO: implement keycode to key symbol translation */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
245 return 0;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
246 }
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 static int cons_kb_init(cons_kb_info_t *kbinfo, MB_DISPLAY display,
1118
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
249 redraw_man_t *rdman) {
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
250 int n_syms;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
251 observer_factory_t *factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
252 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
253
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
254 /* TODO: set keycode_min, keycode_max and syms */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
255 if((int)display != -1)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
256 kbinfo->kb_fd = (int)display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
257 else
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
258 kbinfo->kb_fd = STDIN_FILENO;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
259
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
260 factory = rdman_get_observer_factory(rdman);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
261 kbinfo->kbevents = subject_new(factory, kbinfo, OBJT_KB);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
262 if(kbinfo->kbevents == NULL)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
263 return ERR;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
264 /*! \todo Make sure observer_factory is still need. */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
265 kbinfo->observer_factory = factory;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
266
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
267 return OK;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
268 }
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 static void cons_kb_destroy(cons_kb_info_t *kbinfo) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
271 subject_free(kbinfo->kbevents);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
272 }
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 /*! \brief Notify observers of the shape at specified
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
276 * position for mouse event.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
277 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
278 * 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
279 * with SUBF_STOP_PROPAGATE flag. The subject of mouse event
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
280 * for a shape is returned by sh_get_mouse_event_subject().
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
281 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
282 static void notify_coord_or_shape(redraw_man_t *rdman,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
283 mb_obj_t *obj,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
284 co_aix x, co_aix y, int etype,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
285 unsigned int state,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
286 unsigned int button) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
287 mouse_event_t mouse_event;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
288 subject_t *subject;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
289
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
290 mouse_event.event.type = etype;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
291 mouse_event.x = x;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
292 mouse_event.y = y;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
293 mouse_event.but_state = state;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
294 mouse_event.button = button;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
295
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
296 if(IS_MBO_SHAPES(obj))
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
297 subject = sh_get_mouse_event_subject((shape_t *)obj);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
298 else
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
299 subject = coord_get_mouse_event((coord_t *)obj);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
300
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
301 subject_notify(subject, (event_t *)&mouse_event);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
302 }
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 /*! \brief Handle keyboard event and maintain internal states.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
305 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
306 * It keeps internal state in rt to improve performance.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
307 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
308 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
309 handle_single_cons_event(cons_supp_runtime_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
310 /* TODO: handle keyboard and mouse events. */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
311 printf("handle_single_cons_event() will be implemented later\n");
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
312 }
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 /*! \brief Call when no more event in an event iteration.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
315 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
316 * No more event means event queue is emplty. This function will
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
317 * perform some actions according current internal state.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
318 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
319 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
320 no_more_event(cons_supp_runtime_t *rt) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
321 }
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 /*! \brief Dispatch all console events in the queue.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
324 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
325 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
326 int console_fd = (int)cmb_rt->display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
327 struct pollfd pfd = {console_fd, POLLIN, 0};
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
328 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
329
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
330 while((r = poll(&pfd, 1, 0)) > 0) {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
331 handle_single_cons_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
332 }
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
333 no_more_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
334 }
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 static void
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
337 _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
338 cons_supp_runtime_t *cmb_rt = (cons_supp_runtime_t *)data;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
339
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
340 _cons_supp_handle_cons_event(cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
341 }
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 /*! \brief Initialize a MadButterfy runtime for Xlib.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
344 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
345 * 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
346 * 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
347 * server and an opened window.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
348 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
349 * 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
350 * - w, h
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
351 * - win
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
352 * - display
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
353 * - visual
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
354 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
355 static int
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
356 _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
357 mb_img_ldr_t *img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
358 int w, h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
359 int console_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
360
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
361 w = cmb_rt->w;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
362 h = cmb_rt->h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
363
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
364 mbe_init();
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
365
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
366 cmb_rt->surface =
1117
1de8bb740c46 Add pre-created window for _cons_supp_new_with_win()
Thinker K.F. Li <thinker@codemud.net>
parents: 1116
diff changeset
367 mbe_win_surface_create(cmb_rt->display, cmb_rt->win,
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
368 MB_IFMT_ARGB32, w, h);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
369
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
370 cmb_rt->cr = mbe_create(cmb_rt->surface);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
371
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
372 cmb_rt->rdman = (redraw_man_t *)malloc(sizeof(redraw_man_t));
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
373 redraw_man_init(cmb_rt->rdman, cmb_rt->cr, NULL);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
374 cmb_rt->rdman->w = w;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
375 cmb_rt->rdman->h = h;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
376 /* FIXME: This is a wired loopback reference. This is inly
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
377 * 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
378 * animation. We should relocate the tman to the
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
379 * redraw_man_t instead.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
380 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
381 cmb_rt->rdman->rt = cmb_rt;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
382
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
383 cmb_rt->io_man = mb_io_man_new(_io_factory);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
384 cmb_rt->timer_man = mb_timer_man_new(_timer_factory);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
385
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
386 img_ldr = simple_mb_img_ldr_new("");
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
387 cmb_rt->img_ldr = img_ldr;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
388 /*! \todo Remove rdman_set_img_ldr() */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
389 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
390
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
391 #ifndef ONLY_MOUSE_MOVE_RAW
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
392 cmb_rt->last = NULL;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
393 #endif
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
394
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
395 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
396
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
397 console_fd = (int)cmb_rt->display;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
398 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
399 MB_IO_R,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
400 _cons_supp_handle_connection,
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
401 cmb_rt);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
402
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
403 return OK;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
404 }
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 /*! \brief Initialize a MadButterfy runtime for console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
407 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
408 * It setups a runtime environment to run MadButterfly with console.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
409 * Users should specify width and height of the opening window.
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
410 *
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
411 * \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
412 */
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
413 static int _cons_supp_init(cons_supp_runtime_t *cmb_rt,
1118
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
414 const char *display_name,
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
415 int w, int h) {
1116
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
416 int r;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
417 int console_fd;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
418
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
419 memset(cmb_rt, 0, sizeof(cons_supp_runtime_t));
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
420
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
421 if(display_name == NULL || strlen(display_name) == 0)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
422 console_fd = STDIN_FILENO;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
423 else {
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
424 console_fd = open(display_name, O_RDONLY);
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
425 if(console_fd == -1)
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
426 return ERR;
9163eefa0039 console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
427 }
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 cmb_rt->display = (MB_DISPLAY)console_fd;
1117
1de8bb740c46 Add pre-created window for _cons_supp_new_with_win()
Thinker K.F. Li <thinker@codemud.net>
parents: 1116
diff changeset
430 cmb_rt->win = NULL;
1116
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,
1118
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
451 MB_DISPLAY display, MB_WINDOW win) {
1116
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
1117
1de8bb740c46 Add pre-created window for _cons_supp_new_with_win()
Thinker K.F. Li <thinker@codemud.net>
parents: 1116
diff changeset
525 /*! \brief Create a new runtime for existed window for console.
1116
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,
1118
b319cbbf35c2 Reformat the source for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1117
diff changeset
601 mb_IO_cb_t cb, void *data)
1116
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 };
1119
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
657
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
658 /*
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
659 * Local Variables:
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
660 * indent-tabs-mode: t
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
661 * tab-width: 8
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
662 * c-basic-offset: 4
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
663 * c-file-style:"stroustrup"
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
664 * fill-column:79
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
665 * End:
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
666 */
17cbb862a8c6 Change modeline for cons_supp.c
Thinker K.F. Li <thinker@codemud.net>
parents: 1118
diff changeset
667 // vim: sw=4:ts=8:sts=4:textwidth=79