add support for keypad
[snom-frontend] / snom.pl
CommitLineData
a3855c1d 1#!/bin/sh
824f34a5 2#$Id: snom.pl,v 1.5 2006-05-21 12:42:40 simigern Exp $
a3855c1d 3
4PATH=/opt/csw/bin:/opt/local/bin:/usr/bin:/bin exec perl -w -x "$0" "$@"
5
6#!perl
7
8use Glib qw/TRUE FALSE/;
9use Gtk2 '-init';
10use Gtk2::Gdk::Keysyms;
11use strict;
12
13use LWP::UserAgent;
14my $ua = LWP::UserAgent->new;
ee7ae952 15#$ua->credentials("${ARGV[0]}:80","snom","root","geheim");
a3855c1d 16
ee7ae952 17set_locale Gtk2;
a3855c1d 18
19sub snom_key(@) {
20 (my $key) = @_;
21 $key='%23' if ($key eq '#');
22 my $req = HTTP::Request->new(GET => "http://${ARGV[0]}/command.htm?key=${key}");
23 $ua->request($req);
24}
25
26sub snom_number(@) {
27 (my $number) = @_;
28 my $req = HTTP::Request->new(GET => "http://${ARGV[0]}/command.htm?number=${number}");
29 $ua->request($req);
30}
31
32if (!defined($ARGV[0])) {
33 print STDERR "Usage: ${0} snom.phone.address\n";
34 exit 1
35}
36
37my $window = Gtk2::Window->new('toplevel');
38$window->set_title("snom");
39$window->signal_connect(delete_event => \&CloseAppWindow);
40$window->signal_connect(destroy => sub { Gtk2->main_quit; });
41my $kphandler = $window->signal_connect(key_press_event => \&KBDInput);
42$window->set_border_width(15);
43$window->set_resizable(FALSE);
44
45my $keypad = Gtk2::Table->new(4, 3, 1);
46$keypad->set_row_spacings(2);
47$keypad->set_col_spacings(2);
48my %key = ();
49my $n = 0;
50foreach my $i (1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#') {
51 $key{$i} = Gtk2::Button->new(" ${i} ");
52 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
53 $keypad->attach_defaults($key{$i}, (($n)%3), (($n)%3)+1, int(($n)/3), int((($n)/3)+1));
54 $key{$i}->show();
55 $n++;
56}
57$keypad->show();
58
59my $fkeys = Gtk2::Table->new(6, 2, 1);
60$fkeys->set_row_spacings(2);
61$fkeys->set_col_spacings(2);
62$n = 0;
63foreach my $i qw(Redial Settings Directory Help Menu snom _ _ Conference Transfer Hold DND) {
64 if ($i ne '_') {
65 $key{$i} = Gtk2::Button->new("${i}");
66 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
67 $fkeys->attach_defaults($key{$i}, (($n)%2), (($n)%2)+1, int(($n)/2), int((($n)/2)+1));
68 $key{$i}->show();
69 }
70 $n++;
71}
72$fkeys->show();
73
74my $pkeys = Gtk2::Table->new(6, 2, 1);
75$pkeys->set_row_spacings(2);
76$pkeys->set_col_spacings(2);
77$n = 0;
78foreach my $i qw(P7 P1 P8 P2 P9 P3 P10 P4 P11 P5 P12 P6) {
79 $key{$i} = Gtk2::Button->new("${i}");
80 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
81 $pkeys->attach_defaults($key{$i}, (($n+1)%2), (($n+1)%2)+1, int($n/2), int($n/2)+1);
82 $key{$i}->show();
83 $n++;
84}
85$pkeys->show();
86
87my $softkeys = Gtk2::Table->new(1, 4);
88$softkeys->set_col_spacings(6);
89$n = 0;
90foreach my $i qw(F1 F2 F3 F4) {
91 $key{$i} = Gtk2::Button->new("${i}");
92 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
93 $softkeys->attach_defaults($key{$i}, $n, $n+1, 0, 1);
94 $key{$i}->show();
95 $n++;
96}
97$softkeys->show();
98
99my $navi = Gtk2::Table->new(3, 5);
100$n = 0;
101foreach my $i qw(_ _ ^ _ _ X < _ > OK _ _ V _ _) {
102 if ( $i ne '_' ) {
103 $key{$i} = Gtk2::Button->new("${i}");
104 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
105 $navi->attach_defaults($key{$i}, (($n)%5), (($n)%5)+1, int(($n)/5), int((($n)/5)+1));
106 $key{$i}->show();
107 }
108 $n++;
109}
110$navi->show();
111
112my $output = Gtk2::Table->new(2, 3);
113$output->set_row_spacings(2);
114$output->set_col_spacings(2);
115$n = 0;
116foreach my $i qw(- + _ Mute Speaker Headset) {
117 if ( $i ne '_' ) {
118 $key{$i} = Gtk2::Button->new("${i}");
119 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
120 $output->attach_defaults($key{$i}, (($n)%3), (($n)%3)+1, int(($n)/3), int((($n)/3)+1));
121 $key{$i}->show();
122 }
123 $n++;
124}
125$output->show();
126
127my $special= Gtk2::Table->new(1, 2);
128$special->set_col_spacings(2);
129$n = 0;
130foreach my $i qw(Record Retrieve) {
131 $key{$i} = Gtk2::Button->new("${i}");
132 $key{$i}->signal_connect(clicked => \&KeyPressed, $i);
133 $special->attach_defaults($key{$i}, $n, $n+1, 0, 1);
134 $key{$i}->show();
135 $n++;
136}
137$special->show();
138
139my $display = Gtk2::Entry->new();
140$display->signal_connect(activate => \&DialNumber);
141my $kph_blocked = FALSE;
a3855c1d 142$display->signal_connect(enter_notify_event => \&DisplayMouseOver, TRUE);
a3855c1d 143$display->signal_connect(leave_notify_event => \&DisplayMouseOver, FALSE);
144$display->show();
145
146my $dispbox = Gtk2::VBox->new(0, 5);
147$dispbox->pack_start($display, 0, 0, 0);
148$dispbox->pack_start($softkeys, 0, 0, 0);
149$dispbox->pack_start($navi, 0, 0, 0);
150$dispbox->show();
151
152my $outbox = Gtk2::VBox->new(0, 0);
153$outbox->pack_end($output, 0, 0, 0);
154$outbox->show();
155
156my $specialbox = Gtk2::VBox->new(0, 0);
157$specialbox->pack_end($special, 0, 0, 0);
158$specialbox->show();
159
160my $ubox = Gtk2::HBox->new(0, 30);
161$ubox->pack_start($outbox, 0, 0, 0);
162$ubox->pack_start($dispbox, 0, 0, 0);
163$ubox->pack_start($specialbox, 0, 0, 0);
164$ubox->show();
165
166
167my $kbbox = Gtk2::HBox->new(0, 30);
168$kbbox->pack_start($keypad, 1, 1, 0);
169$kbbox->pack_start($fkeys, 0, 0, 0);
170$kbbox->show();
171
172my $kvbox = Gtk2::VBox->new(0, 5);
173$kvbox->pack_end($kbbox, 0, 0, 0);
174$kvbox->show();
175
176my $lbox = Gtk2::HBox->new(0, 30);
177$lbox->pack_start($kvbox, 1, 1, 0);
178$lbox->pack_start($pkeys, 0, 0, 0);
179$lbox->show();
180
181my $mainbox = Gtk2::VBox->new(0,5);
182$mainbox->pack_start($ubox, 0, 0, 0);
183$mainbox->pack_start($lbox, 0, 0, 0);
184$mainbox->show();
185
186$window->add($mainbox);
187$window->show();
188
bfe77af1 189$key{OK}->grab_focus();
190
a3855c1d 191# Gtk2 event loop
192Gtk2->main;
193
194# Should never get here
195exit( 0 );
196
197
198sub KeyPressed
199{
200 my ($button, $text) = @_;
201 foreach my $i (1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#',
202 'F1', 'F2', 'F3', 'F4',
203 'P1', 'P2', 'P3', 'P4', 'P5', 'P6',
204 'P7', 'P8', 'P9', 'P10', 'P11', 'P12') {
205 if ($i eq $text) {
206 snom_key($i);
f73d9f87 207 return TRUE;
a3855c1d 208 }
209 }
210
211 snom_key("REDIAL") if ( $text eq "Redial" );
212 snom_key("SETTINGS") if ( $text eq "Settings" );
213 snom_key("F_ADR_BOOK") if ( $text eq "Directory" );
214 snom_key("HELP") if ( $text eq "Help" );
215 snom_key("MENU") if ( $text eq "Menu" );
216 snom_key("SNOM") if ( $text eq "snom" );
217 snom_key("CONFERENCE") if ( $text eq "Conference" );
218 snom_key("TRANSFER") if ( $text eq "Transfer" );
219 snom_key("F_R") if ( $text eq "Hold" );
220 snom_key("DND") if ( $text eq "DND" );
221
222 snom_key("MUTE") if ( $text eq "Mute" );
223 snom_key("SPEAKER") if ( $text eq "Speaker" );
224 snom_key("HEADSET") if ( $text eq "Headset" );
225
226 snom_key("CANCEL") if ( $text eq "X" );
227 snom_key("ENTER") if ( $text eq "OK" );
228 snom_key("UP") if ( $text eq "^" );
229 snom_key("DOWN") if ( $text eq "V" );
230 snom_key("LEFT") if ( $text eq "<" );
231 snom_key("RIGHT") if ( $text eq ">" );
232
233 snom_key("F_REC") if ( $text eq "Record" );
234 snom_key("F_RETRIEVE") if ( $text eq "Retrieve" );
235
236 snom_key("VOLUME_UP") if ( $text eq "+" );
237 snom_key("VOLUME_DOWN") if ( $text eq "-" );
f73d9f87 238 return TRUE;
a3855c1d 239}
240
241sub DialNumber
242{
243 my ($entry) = @_;
f73d9f87 244
a3855c1d 245 snom_number($entry->get_text());
f73d9f87 246 return TRUE;
a3855c1d 247}
248
249sub DisplayMouseOver
250{
251 my ($widget, $event, $active) = @_;
252
253 if ($active) {
254 $widget->grab_focus();
255 if (!$kph_blocked) {
256 $window->signal_handler_block($kphandler);
257 $kph_blocked=TRUE;
258 }
259 } else {
260 $key{OK}->grab_focus();
261 if ($kph_blocked) {
262 $window->signal_handler_unblock($kphandler);
263 $kph_blocked=FALSE;
264 }
265 }
266
267 return TRUE;
268}
269
270sub KBDInput
271{
272 my ($widget, $event) = @_;
273
274 my $keyval = $event->keyval;
824f34a5 275 foreach my $i (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) {
276 if ($keyval == $Gtk2::Gdk::Keysyms{$i} || $keyval == $Gtk2::Gdk::Keysyms{"KP_${i}"}) {
277 snom_key($i);
278 return TRUE;
279 }
280 }
281
282 foreach my $i ('F1', 'F2', 'F3', 'F4') {
a3855c1d 283 if ($keyval == $Gtk2::Gdk::Keysyms{$i}) {
284 snom_key($i);
285 return TRUE;
286 }
287 }
288
289 if ($keyval == $Gtk2::Gdk::Keysyms{Return}) {snom_key("ENTER"); return TRUE;}
290 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Enter}) {snom_key("ENTER"); return TRUE;}
291 if ($keyval == $Gtk2::Gdk::Keysyms{Escape}) {snom_key("CANCEL"); return TRUE;}
292 if ($keyval == $Gtk2::Gdk::Keysyms{Left}) {snom_key("LEFT"); return TRUE;}
293 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Left}) {snom_key("LEFT"); return TRUE;}
294 if ($keyval == $Gtk2::Gdk::Keysyms{Right}) {snom_key("RIGHT"); return TRUE;}
295 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Right}) {snom_key("RIGHT"); return TRUE;}
296 if ($keyval == $Gtk2::Gdk::Keysyms{Up}) {snom_key("UP"); return TRUE;}
297 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Up}) {snom_key("UP"); return TRUE;}
298 if ($keyval == $Gtk2::Gdk::Keysyms{Down}) {snom_key("DOWN"); return TRUE;}
299 if ($keyval == $Gtk2::Gdk::Keysyms{KP_Down}) {snom_key("DOWN"); return TRUE;}
300 return TRUE;
301}
302
303### Callback function to close the window
304sub CloseAppWindow
305{
306 $window->destroy;
307 return TRUE;
308}
309
310
311# END EXAMPLE PROGRAM
Impressum, Datenschutz