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