annotate src/X_supp.c @ 192:54fdc2a65242

Remove factory from observer APIs. - Save factory that used to new a subject as subject's attribute. - Get factory from subject himself instead of passing as an argument. - It make API of observer more simple and clean.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 18 Nov 2008 21:42:30 +0800
parents 530bb7728546
children 29e1b2bffe4c
rev   line source
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
2 #include <stdlib.h>
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
3 #include <string.h>
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <X11/Xlib.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <X11/Xutil.h>
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
6 #include <cairo.h>
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
7 #include <cairo-xlib.h>
186
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
8 #include "mb_redraw_man.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
9 #include "mb_timer.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
10 #include "mb_X_supp.h"
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
12 #define ERR -1
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
13 #define OK 0
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
14
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
15 /*! \ingroup xkb
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
16 * @{
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
17 */
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
18 struct _X_kb_info {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
19 int keycode_min, keycode_max;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
20 int ksym_per_code;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
21 KeySym *syms;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
22 subject_t *kbevents;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
23 ob_factory_t *ob_factory;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
24 };
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
25
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
26 /* @} */
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
27
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
28 struct _X_MB_runtime {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
29 Display *display;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
30 Window win;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
31 Visual *visual;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
32 cairo_surface_t *surface, *backend_surface;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
33 cairo_t *cr, *backend_cr;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
34 redraw_man_t *rdman;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
35 mb_tman_t *tman;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
36 int w, h;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
37
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
38 X_kb_info_t kbinfo;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
39
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
40 /* States */
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
41 shape_t *last;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
42 };
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
43
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
44 /*! \defgroup xkb X Keyboard Handling
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
45 *
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
46 * Accept keyboard events from X server and delivery it to
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
47 * application through observer pattern. There is a subject,
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
48 * per X-connection, for that.
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
49 * @{
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
50 */
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
51 static int keycode2sym(X_kb_info_t *kbinfo, unsigned int keycode) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
52 int sym_idx;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
53 int sym;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
54
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
55 sym_idx = kbinfo->ksym_per_code * (keycode - kbinfo->keycode_min);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
56 sym = kbinfo->syms[sym_idx];
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
57 return sym;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
58 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
59
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
60 static int X_kb_init(X_kb_info_t *kbinfo, Display *display,
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
61 redraw_man_t *rdman) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
62 int n_syms;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
63 ob_factory_t *factory;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
64 int r;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
65
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
66 r = XDisplayKeycodes(display,
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
67 &kbinfo->keycode_min,
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
68 &kbinfo->keycode_max);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
69 if(r == 0)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
70 return ERR;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
71
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
72 n_syms = kbinfo->keycode_max - kbinfo->keycode_min + 1;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
73 kbinfo->syms = XGetKeyboardMapping(display, kbinfo->keycode_min,
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
74 n_syms,
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
75 &kbinfo->ksym_per_code);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
76 if(kbinfo->syms == NULL)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
77 return ERR;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
78
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
79 factory = rdman_get_ob_factory(rdman);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
80 kbinfo->kbevents = subject_new(factory, kbinfo, OBJT_KB);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
81 if(kbinfo->kbevents == NULL)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
82 return ERR;
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
83 /*! \todo Make sure ob_factory is still need. */
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
84 kbinfo->ob_factory = factory;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
85
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
86 return OK;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
87 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
88
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
89 static void X_kb_destroy(X_kb_info_t *kbinfo) {
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
90 subject_free(kbinfo->kbevents);
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
91 XFree(kbinfo->syms);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
92 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
93
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
94 /*! \brief Accept X keyboard events from handle_x_event() and dispatch it.
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
95 */
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
96 static void X_kb_handle_event(X_kb_info_t *kbinfo, XKeyEvent *xkey) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
97 unsigned int code;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
98 int sym;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
99 X_kb_event_t event;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
100
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
101 code = xkey->keycode;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
102 sym = keycode2sym(kbinfo, code);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
103 if(xkey->type == KeyPress)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
104 event.event.type = EVT_KB_PRESS;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
105 else if(xkey->type == KeyRelease)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
106 event.event.type = EVT_KB_RELEASE;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
107 event.event.tgt = event.event.cur_tgt = kbinfo->kbevents;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
108 event.keycode = code;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
109 event.sym = sym;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
110
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
111 subject_notify(kbinfo->kbevents, &event.event);
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
112 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
113
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
114 /* @} */
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
116 static unsigned int get_button_state(unsigned int state) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
117 unsigned int but = 0;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
118
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
119 if(state & Button1Mask)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
120 but |= MOUSE_BUT1;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
121 if(state & Button2Mask)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
122 but |= MOUSE_BUT2;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
123 if(state & Button3Mask)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
124 but |= MOUSE_BUT3;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
125
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
126 return but;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
127 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
128
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
129 static unsigned int get_button(unsigned int button) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
130 switch(button) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
131 case Button1:
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
132 return MOUSE_BUT1;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
133 case Button2:
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
134 return MOUSE_BUT2;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
135 case Button3:
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
136 return MOUSE_BUT3;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
137 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
138 return 0;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
139 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
140
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
141 /*! \brief Notify observers of the shape at specified
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
142 * position for mouse event.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
143 *
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
144 * Observers of parent shapes may be called if the subject is not
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
145 * with SUBF_STOP_PROPAGATE flag. The subject of mouse event
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
146 * for a shape is returned by sh_get_mouse_event_subject().
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
147 */
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
148 static void notify_shapes(redraw_man_t *rdman,
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
149 shape_t *shape,
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
150 co_aix x, co_aix y, int etype,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
151 unsigned int state,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
152 unsigned int button) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
153 mouse_event_t mouse_event;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
154 subject_t *subject;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
155
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
156 mouse_event.event.type = etype;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
157 mouse_event.x = x;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
158 mouse_event.y = y;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
159 mouse_event.but_state = state;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
160 mouse_event.button = button;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
161
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
162 subject = sh_get_mouse_event_subject(shape);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
163
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
164 subject_notify(subject, (event_t *)&mouse_event);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
165 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
166
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
167 /*! \brief Dispatch all X events in the queue.
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
168 */
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
169 static void handle_x_event(X_MB_runtime_t *rt) {
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
170 Display *display = rt->display;
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
171 redraw_man_t *rdman = rt->rdman;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 XEvent evt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 XMotionEvent *mevt;
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
174 XButtonEvent *bevt;
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
175 XExposeEvent *eevt;
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
176 XKeyEvent *xkey;
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
177 co_aix x, y, w, h;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
178
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
179 int eflag = 0;
82
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
180 int ex1=0, ey1=0, ex2=0, ey2=0;
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
181
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
182 shape_t *shape;
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
183
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
184 unsigned int state, button;
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
185 int in_stroke;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
186 int r;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
188 while(XEventsQueued(display, QueuedAfterReading) > 0) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
189 r = XNextEvent(display, &evt);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
190 if(r == -1)
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
191 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
192
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
193 switch(evt.type) {
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
194 case ButtonPress:
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
195 bevt = (XButtonEvent *)&evt;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
196 x = bevt->x;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
197 y = bevt->y;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
198 state = get_button_state(bevt->state);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
199 button = get_button(bevt->button);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
200
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
201 shape = find_shape_at_pos(rdman, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
202 &in_stroke);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
203 if(shape)
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
204 notify_shapes(rdman, shape, x, y, EVT_MOUSE_BUT_PRESS,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
205 state, button);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
206 break;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
207
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
208 case ButtonRelease:
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
209 bevt = (XButtonEvent *)&evt;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
210 x = bevt->x;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
211 y = bevt->y;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
212 state = get_button_state(bevt->state);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
213 button = get_button(bevt->button);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
214
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
215 shape = find_shape_at_pos(rdman, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
216 &in_stroke);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
217 if(shape)
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
218 notify_shapes(rdman, shape, x, y, EVT_MOUSE_BUT_RELEASE,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
219 state, button);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
220 break;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
221
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
222 case MotionNotify:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
223 mevt = (XMotionEvent *)&evt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
224 x = mevt->x;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
225 y = mevt->y;
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
226 state = get_button_state(mevt->state);
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
227
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
228 shape = find_shape_at_pos(rdman, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
229 &in_stroke);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
230 if(shape != NULL) {
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
231 if(rt->last != shape) {
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
232 if(rt->last)
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
233 notify_shapes(rdman, rt->last, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
234 EVT_MOUSE_OUT, state, 0);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
235 notify_shapes(rdman, shape, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
236 EVT_MOUSE_OVER, state, 0);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
237 rt->last = shape;
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
238 } else
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
239 notify_shapes(rdman, shape, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
240 EVT_MOUSE_MOVE, state, 0);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
241 } else {
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
242 if(rt->last) {
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
243 notify_shapes(rdman, rt->last, x, y,
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
244 EVT_MOUSE_OUT, state, 0);
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
245 rt->last = NULL;
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
246 }
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
247 }
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
248 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
249
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
250 case KeyPress:
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
251 case KeyRelease:
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
252 xkey = &evt.xkey;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
253 X_kb_handle_event(&rt->kbinfo, xkey);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
254 break;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
255
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
256 case Expose:
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
257 eevt = &evt.xexpose;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
258 x = eevt->x;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
259 y = eevt->y;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
260 w = eevt->width;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
261 h = eevt->height;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
262
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
263 if(eflag) {
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
264 if(x < ex1)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
265 ex1 = x;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
266 if(y < ey1)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
267 ey1 = y;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
268 if((x + w) > ex2)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
269 ex2 = x + w;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
270 if((y + h) > ey2)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
271 ey2 = y + h;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
272 } else {
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
273 ex1 = x;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
274 ey1 = y;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
275 ex2 = x + w;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
276 ey2 = y + h;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
277 eflag = 1;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
278 }
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
279 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
280 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
281 }
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
282 if(eflag) {
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
283 rdman_redraw_area(rdman, ex1, ey1, (ex2 - ex1), (ey2 - ey1));
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
284 eflag = 0;
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
285 }
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
286 XFlush(display);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
287 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
288
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
289 /*! \brief Handle connection coming data and timeout of timers.
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
290 *
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
291 * \param display is a Display returned by XOpenDisplay().
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
292 * \param rdman is a redraw manager.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
293 * \param tman is a timer manager.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
294 *
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
295 * The display is managed by specified rdman and tman. rdman draws
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
296 * on the display, and tman trigger actions according timers.
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
297 */
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
298 void X_MB_handle_connection(X_MB_runtime_t *rt) {
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
299 Display *display = rt->display;
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
300 redraw_man_t *rdman = rt->rdman;
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
301 mb_tman_t *tman = rt->tman;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
302 int fd;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
303 mb_timeval_t now, tmo;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
304 struct timeval tv;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
305 fd_set rfds;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
306 int nfds;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
307 int r, r1;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
308
142
3efe77007127 Do not redraw all at begining, waiting for explosure.
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
309 handle_x_event(rt);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
310
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
311 fd = XConnectionNumber(display);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
312 nfds = fd + 1;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
313 while(1) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
314 FD_ZERO(&rfds);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
315 FD_SET(fd, &rfds);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
316
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
317 get_now(&now);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
318 r = mb_tman_next_timeout(tman, &now, &tmo);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
319
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
320 if(r == 0) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
321 tv.tv_sec = MB_TIMEVAL_SEC(&tmo);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
322 tv.tv_usec = MB_TIMEVAL_USEC(&tmo);
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
323 r1 = select(nfds, &rfds, NULL, NULL, &tv);
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
324 } else
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
325 r1 = select(nfds, &rfds, NULL, NULL, NULL);
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
326
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
327 if(r1 == -1) {
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
328 perror("select");
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
329 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
330 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
331
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
332 if(r1 == 0) {
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
333 get_now(&now);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
334 mb_tman_handle_timeout(tman, &now);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
335 rdman_redraw_changed(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
336 XFlush(display);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
337 } else if(FD_ISSET(fd, &rfds)){
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
338 handle_x_event(rt);
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
339 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
340 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
341 }
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
342
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
343 #define ERR -1
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
344 #define OK 0
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
345
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
346 static int X_init_connection(const char *display_name,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
347 int w, int h,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
348 Display **displayp,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
349 Visual **visualp,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
350 Window *winp) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
351 Display *display;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
352 Window root, win;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
353 Visual *visual;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
354 int screen;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
355 XSetWindowAttributes wattr;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
356 int depth;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
357 int x, y;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
358 int r;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
359
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
360 display = XOpenDisplay(display_name);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
361 if(display == NULL)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
362 return ERR;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
363
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
364 screen = DefaultScreen(display);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
365 root = DefaultRootWindow(display);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
366 visual = DefaultVisual(display, screen);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
367 depth = DefaultDepth(display, screen);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
368 wattr.override_redirect = False;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
369 x = 10;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
370 y = 10;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
371 win = XCreateWindow(display, root,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
372 x, y,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
373 w, h,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
374 1, depth, InputOutput, visual,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
375 CWOverrideRedirect, &wattr);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
376 r = XMapWindow(display, win);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
377 if(r == -1) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
378 XCloseDisplay(display);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
379 return ERR;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
380 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
381
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
382 XSelectInput(display, win, PointerMotionMask | ExposureMask |
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
383 ButtonPressMask | ButtonReleaseMask |
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
384 KeyPressMask | KeyReleaseMask);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
385 XFlush(display);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
386
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
387 *displayp = display;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
388 *visualp = visual;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
389 *winp = win;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
390
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
391 return OK;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
392 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
393
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
394 /*! \brief Initialize a MadButterfy runtime for Xlib.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
395 *
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
396 * It setups a runtime environment to run MadButterfly with Xlib.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
397 * Users should specify width and height of the opening window.
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
398 */
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
399 static int X_MB_init(const char *display_name,
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
400 int w, int h, X_MB_runtime_t *xmb_rt) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
401 memset(xmb_rt, 0, sizeof(X_MB_runtime_t));
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
402
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
403 xmb_rt->w = w;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
404 xmb_rt->h = h;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
405 X_init_connection(display_name, w, h, &xmb_rt->display,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
406 &xmb_rt->visual, &xmb_rt->win);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
407
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
408 xmb_rt->surface =
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
409 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
410
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
411 xmb_rt->backend_surface =
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
412 cairo_xlib_surface_create(xmb_rt->display,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
413 xmb_rt->win,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
414 xmb_rt->visual,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
415 w, h);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
416
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
417 xmb_rt->cr = cairo_create(xmb_rt->surface);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
418 xmb_rt->backend_cr = cairo_create(xmb_rt->backend_surface);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
419
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
420 cairo_set_source_surface(xmb_rt->backend_cr, xmb_rt->surface, 0, 0);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
421
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
422 xmb_rt->rdman = (redraw_man_t *)malloc(sizeof(redraw_man_t));
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
423 redraw_man_init(xmb_rt->rdman, xmb_rt->cr, xmb_rt->backend_cr);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
424
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
425 xmb_rt->tman = mb_tman_new();
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
426
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
427 xmb_rt->last = NULL;
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 82
diff changeset
428
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
429 X_kb_init(&xmb_rt->kbinfo, xmb_rt->display, xmb_rt->rdman);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
430
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
431 return OK;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
432 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
433
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
434 static void X_MB_destroy(X_MB_runtime_t *xmb_rt) {
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
435 if(xmb_rt->rdman) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
436 redraw_man_destroy(xmb_rt->rdman);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
437 free(xmb_rt->rdman);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
438 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
439
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
440 if(xmb_rt->tman)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
441 mb_tman_free(xmb_rt->tman);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
442
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
443 if(xmb_rt->cr)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
444 cairo_destroy(xmb_rt->cr);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
445 if(xmb_rt->backend_cr)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
446 cairo_destroy(xmb_rt->backend_cr);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
447
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
448 if(xmb_rt->surface)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
449 cairo_surface_destroy(xmb_rt->surface);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
450 if(xmb_rt->backend_surface)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
451 cairo_surface_destroy(xmb_rt->backend_surface);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
452
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
453 if(xmb_rt->display)
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
454 XCloseDisplay(xmb_rt->display);
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
455
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
456 X_kb_destroy(&xmb_rt->kbinfo);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
457 }
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
458
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
459 X_MB_runtime_t *X_MB_new(const char *display_name, int w, int h) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
460 X_MB_runtime_t *rt;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
461 int r;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
462
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
463 rt = O_ALLOC(X_MB_runtime_t);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
464 if(rt == NULL)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
465 return NULL;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
466
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
467 r = X_MB_init(display_name, w, h, rt);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
468 if(r != OK)
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
469 return NULL;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
470
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
471 return rt;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
472 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
473
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
474 void X_MB_free(X_MB_runtime_t *rt) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
475 X_MB_destroy(rt);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
476 free(rt);
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
477 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
478
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
479 subject_t *X_MB_kbevents(X_MB_runtime_t *xmb_rt) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
480 return xmb_rt->kbinfo.kbevents;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
481 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
482
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
483 redraw_man_t *X_MB_rdman(X_MB_runtime_t *xmb_rt) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
484 return xmb_rt->rdman;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
485 }
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
486
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
487 mb_tman_t *X_MB_tman(X_MB_runtime_t *xmb_rt) {
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
488 return xmb_rt->tman;
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
489 }
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
490
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
491 ob_factory_t *X_MB_ob_factory(X_MB_runtime_t *xmb_rt) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
492 ob_factory_t *factory;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
493
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
494 factory = rdman_get_ob_factory(xmb_rt->rdman);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
495 return factory;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
496 }