1 | #! perl # mandatory depends=login |
1 | #! perl # mandatory depends=login |
2 | |
2 | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
4 | # ws_n id # widgetset new |
4 | # ws_n ws # widgetset new |
5 | # ws_d id # widgetset destroy |
5 | # ws_d ws # widgetset destroy |
6 | # ws_c ws id class args # widgetset create |
6 | # ws_c ws id class args # widgetset create |
7 | # w_c id [rid] name args # widget method call |
7 | # w_c id rid name args # widget method call |
8 | # w_s id name value # widget member set |
8 | # w_s id @attr # widget member set |
9 | # w_g id rid name # widget member get |
9 | # w_g id rid @attr # widget member get |
10 | # |
10 | # |
11 | # and expects the following exti message types |
11 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
12 | # w_r rid res # widget call return |
13 | # w_e id name args # widget_event |
13 | # w_e id name args # widget_event |
14 | |
14 | |
|
|
15 | our $DEBUG = 1; |
|
|
16 | |
15 | cf::client->attach ( |
17 | cf::client->attach ( |
16 | on_connect => sub { |
18 | on_connect => sub { |
17 | my ($ns) = @_; |
19 | my ($ns) = @_; |
18 | |
20 | |
|
|
21 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
22 | |
19 | $ns->{id} = "a"; |
23 | $ns->{id} = "a"; |
|
|
24 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
|
|
25 | # cannot deserialise ATM |
|
|
26 | undef |
|
|
27 | }); |
20 | }, |
28 | }, |
21 | ); |
29 | ); |
22 | |
30 | |
23 | sub csc_update_stats { |
31 | sub csc_update_stats { |
24 | my ($ns) = @_; |
32 | my ($ns) = @_; |
… | |
… | |
60 | [5, "Pow"], |
68 | [5, "Pow"], |
61 | [6, "Cha"], |
69 | [6, "Cha"], |
62 | ) { |
70 | ) { |
63 | my ($x, $label) = @$_; |
71 | my ($x, $label) = @$_; |
64 | |
72 | |
65 | $statstable->add ($x, 0, $ws->new (Label => |
73 | $statstable->add_at ($x, 0, $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
74 | can_hover => 1, can_events => 1, |
67 | align => +1, text => $label, tooltip => "#stat_$label", |
75 | align => +1, text => $label, tooltip => "#stat_$label", |
68 | )); |
76 | )); |
69 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
77 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
70 | can_hover => 1, can_events => 1, |
78 | can_hover => 1, can_events => 1, |
71 | align => +1, template => "88", tooltip => "#stat_$label", |
79 | align => +1, template => "88", tooltip => "#stat_$label", |
72 | )); |
80 | )); |
73 | } |
81 | } |
74 | |
82 | |
… | |
… | |
84 | return unless $cf::CFG{devel}; |
92 | return unless $cf::CFG{devel}; |
85 | |
93 | |
86 | my $ns = $pl->ns; |
94 | my $ns = $pl->ns; |
87 | |
95 | |
88 | return unless $ns->{can_widget}; |
96 | return unless $ns->{can_widget}; |
89 | |
|
|
90 | csc_start $ns; |
97 | #csc_start $ns; |
91 | }, |
98 | }, |
92 | ); |
99 | ); |
93 | |
100 | |
94 | cf::register_exticmd w_e => sub { |
101 | cf::register_exticmd w_e => sub { |
95 | my ($ns, $pkt) = @_; |
102 | my ($ns, $id, $name, $args) = @_; |
96 | |
103 | |
97 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
104 | if (my $w = $ns->{widget}{$id}) { |
98 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
105 | if (my $cb = $w->{ev}{$name}) { |
99 | $_->($w, @{ $pkt->{args} || [] }) |
106 | $_->($w, @$args) |
100 | for @$cb; |
107 | for @$cb; |
101 | } |
108 | } |
102 | } |
109 | } |
103 | |
110 | |
104 | () |
111 | () |
105 | }; |
112 | }; |
106 | |
113 | |
107 | cf::register_exticmd w_r => sub { |
114 | cf::register_exticmd w_r => sub { |
108 | my ($ns, $pkt) = @_; |
115 | my ($ns, $rid, $res) = @_; |
109 | |
116 | |
110 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
117 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
111 | $cb->(@{$pkt->{res} || [] }); |
118 | $cb->(@$res); |
112 | } |
119 | } |
113 | |
120 | |
114 | () |
121 | () |
115 | }; |
122 | }; |
116 | |
123 | |
… | |
… | |
120 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
121 | |
128 | |
122 | my $ws = bless { |
129 | my $ws = bless { |
123 | id => $id, |
130 | id => $id, |
124 | ns => $self, |
131 | ns => $self, |
125 | w => {}, |
132 | _w => {}, |
126 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
127 | |
134 | |
128 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => $id); |
129 | |
136 | |
130 | $ws |
137 | $ws |
131 | } |
138 | } |
132 | |
139 | |
133 | ############################################################################# |
140 | ############################################################################# |
… | |
… | |
139 | } |
146 | } |
140 | |
147 | |
141 | sub destroy { |
148 | sub destroy { |
142 | my ($self) = @_; |
149 | my ($self) = @_; |
143 | |
150 | |
144 | $self->msg (ws_d => id => $self->{id}); |
151 | $self->msg (ws_d => $self->{id}); |
145 | delete $self->{ns}; |
152 | delete $self->{ns}; |
146 | $_->destroy |
153 | $_->destroy |
147 | for values %{ $self->{w} }; |
154 | for values %{ $self->{w} }; |
148 | } |
155 | } |
149 | |
156 | |
150 | sub msg { |
157 | sub msg { |
151 | my ($self, $type, %msg) = @_; |
158 | my ($self, @msg) = @_; |
152 | |
159 | |
153 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
154 | $msg{msgtype} = $type; |
161 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
155 | $ns->send_packet ("ext " . cf::to_json \%msg); |
162 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
156 | } |
163 | } |
157 | } |
164 | } |
158 | |
165 | |
159 | sub new { |
166 | sub new { |
160 | my ($self, $class, %args) = @_; |
167 | my ($self, $class, %args) = @_; |
161 | |
168 | |
162 | my $id = ++$self->{ns}{id}; |
169 | my $id = ++$self->{ns}{id}; |
163 | |
170 | |
164 | my $proxy = $self->{w}{$id} = bless { |
171 | my $proxy = bless { |
165 | id => $id, |
172 | id => $id, |
166 | }, "ext::widget::proxy"; |
173 | }, "ext::widget::proxy"; |
167 | |
174 | |
|
|
175 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
168 | Scalar::Util::weaken ($proxy->{ws} = $self); |
176 | Scalar::Util::weaken ($proxy->{ws} = $self); |
169 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
177 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
170 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
178 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
171 | |
179 | |
172 | for my $ev (grep /^on_/, keys %args) { |
180 | for my $ev (grep /^on_/, keys %args) { |
173 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
181 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
174 | $args{$ev} = 0; |
182 | $args{$ev} = 0; |
175 | } |
183 | } |
176 | |
184 | |
177 | $self->msg (ws_c => |
185 | $self->msg (ws_c => |
178 | ws => $self->{w}{id}, |
186 | $self->{id}, |
179 | id => $id, |
187 | $id, |
180 | class => $class, |
188 | $class, |
181 | args => \%args, |
189 | \%args, |
182 | ); |
190 | ); |
183 | |
191 | |
184 | $proxy |
192 | $proxy |
185 | } |
193 | } |
186 | |
194 | |
… | |
… | |
192 | my ($self) = @_; |
200 | my ($self) = @_; |
193 | |
201 | |
194 | delete $self->{ns}{widget}{$self->{id}}; |
202 | delete $self->{ns}{widget}{$self->{id}}; |
195 | |
203 | |
196 | if (my $ws = $self->{ws}) { |
204 | if (my $ws = $self->{ws}) { |
|
|
205 | $self->msg (w_c => 0, "destroy"); |
197 | delete $ws->{w}{$self->{id}}; |
206 | delete $ws->{_w}{$self->{id}}; |
198 | $self->msg (w_c => name => "destroy"); |
|
|
199 | } |
207 | } |
200 | } |
208 | } |
201 | |
209 | |
202 | sub msg { |
210 | sub msg { |
203 | my ($self, $type, %msg) = @_; |
211 | my ($self, $type, @arg) = @_; |
204 | |
212 | |
205 | if (my $ws = $self->{ws}) { |
213 | if (my $ws = $self->{ws}) { |
206 | $ws->msg ($type, |
214 | $ws->msg ($type, $self->{id}, @arg); |
207 | %msg, |
|
|
208 | id => $self->{id}, |
|
|
209 | ); |
|
|
210 | } |
215 | } |
211 | } |
216 | } |
212 | |
217 | |
213 | sub msg_cb { |
218 | sub msg_cb { |
214 | my ($self, $cb, $type, %msg) = @_; |
219 | my ($self, $cb, $type, @arg) = @_; |
215 | |
220 | |
216 | if (my $ws = $self->{ws}) { |
221 | if (my $ws = $self->{ws}) { |
217 | |
|
|
218 | my $rid = ++$ws->{ns}{id}; |
222 | my $rid = ++$ws->{ns}{id}; |
219 | |
223 | |
220 | $self->msg ($type, %msg, rid => $rid); |
224 | $self->msg ($type, $rid, @arg); |
221 | |
225 | |
222 | if ($cb) { |
226 | if ($cb) { |
223 | $ws->{ns}{widget_return}{$rid} = $cb; |
227 | $ws->{ns}{widget_return}{$rid} = $cb; |
224 | } else { |
228 | } else { |
225 | # synchronous case |
229 | # synchronous case |
… | |
… | |
238 | |
242 | |
239 | () |
243 | () |
240 | } |
244 | } |
241 | |
245 | |
242 | sub set { |
246 | sub set { |
243 | my ($self, $member, $value) = @_; |
247 | my ($self, @kv) = @_; |
244 | |
248 | |
245 | $self->msg (w_s => name => $member, value => $value); |
249 | $self->msg (w_s => \@kv); |
246 | } |
250 | } |
247 | |
251 | |
248 | sub get { |
252 | sub get { |
249 | my ($self, $member, $cb) = @_; |
253 | my ($self, $member, $cb) = @_; |
250 | |
254 | |
251 | $self->msg_cb ($cb, w_g => name => $member); |
255 | $self->msg_cb ($cb, w_g => [$member]); |
252 | } |
256 | } |
253 | |
257 | |
254 | sub TO_JSON { |
258 | sub TO_JSON { |
255 | { __widget_ref__ => $_[0]{id} } |
259 | { __w_ => $_[0]{id} } |
256 | } |
260 | } |
257 | |
261 | |
258 | our $AUTOLOAD; |
262 | our $AUTOLOAD; |
259 | |
263 | |
260 | sub AUTOLOAD { |
264 | sub AUTOLOAD { |
261 | $AUTOLOAD =~ s/^.*:// |
265 | $AUTOLOAD =~ s/^.*:// |
262 | or return; |
266 | or return; |
263 | |
267 | |
264 | my $self = shift; |
268 | my $self = shift; |
265 | |
269 | |
266 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
270 | $self->msg (w_c => 0, $AUTOLOAD, \@_); |
267 | |
271 | |
268 | () |
272 | () |
269 | } |
273 | } |
270 | |
274 | |