Hello all,
I am new to GTK+ and hence I am going to ask you the experts one of my issue that I am kind of hit the wall...
I am designing an application that deals with an analog (telephoney Modem). The idea is when the user clicks on a button the modem will dial a specific number and reads a remote user input from the telephone keypress. Also, there is a hangup button that users could push to end the call at any time.
My issue is, I start dialing the phone number and the rest of the GUI is all blocked. so I have introduced two threads invoked by button clicks, one for dialing and the other for hanging up the line.
Code:
1 #include <all-required-headers.h>
2
3 GtkWidget* button;
4 GtkWidget* button1;
5 pthread_t a_thread;
6 pthread_t b_thread;
7 pthread_mutex_t mutex;
8
9 int modem_fd;
10
11 void* threadfunc_dial(void* arg);
12 void* threadfunc_hangup(void* arg);
13
14 void destroy(GtkWidget* widget, gpointer data)
15 {
16 gtk_main_quit();
17 }
18
19 void* threadfunc_dial(void* arg)
20 {
21 pthread_mutex_lock(&mutex);
22 printf("threadfunc_call: dialing modem now..\n");
23
24 gdk_threads_enter();
25 dial_number("4164161234"); // phone to dial out
26 gdk_threads_leave();
27
28 pthread_mutex_unlock(&mutex);
29 return NULL;
30 }
31
32 void* threadfunc_hangup(void* arg)
33 {
34 pthread_mutex_lock(&mutex);
35 printf("threadfunc_call: dialing modem now..\n");
36
37 gdk_threads_enter();
38 write_port("ATH",modem_fd);
39 gdk_threads_leave();
40
41 pthread_mutex_unlock(&mutex);
42 return NULL;
43 }
44
45 void buttonclicked(GtkWidget* widget, gpointer data)
46 {
47 int res;
48 printf("starting DIAL thread ..\n");
49 res = pthread_create(&a_thread, NULL, threadfunc_dial, NULL);
50 if(res != 0)
51 {
52 perror("thread creation failed");
53 exit(-1);
54 }
55 }
56
57 void buttonclicked1(GtkWidget* widget, gpointer data)
58 {
59 int res;
60 printf("starting HANGUP thread ..\n");
61 res = pthread_create(&b_thread, NULL, threadfunc_hangup, NULL);
62 if(res != 0)
63 {
64 perror("thread creation failed");
65 exit(-1);
66 }
67 }
68
69 int main(int argc, char* argv[])
70 {
71 if (!g_thread_supported ()){ g_thread_init(NULL); }
72 gdk_threads_init();
73 gdk_threads_enter();
74 gtk_init(&argc, &argv);
75
76 GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
77 gtk_signal_connect(GTK_OBJECT (window), "destroy",
78 GTK_SIGNAL_FUNC(destroy), NULL);
79
80 gtk_container_set_border_width(GTK_CONTAINER (window), 10);
81
82 /* add a button to do something usefull */
83 GtkWidget* vbox1 = gtk_vbox_new( 0, 2);
84 button = gtk_button_new_with_label("Dial Phone");
85 button1 = gtk_button_new_with_label("Hangup Phone 1");
86
87 gtk_signal_connect(GTK_OBJECT (button),"clicked",GTK_SIGNAL_FUNC(buttonclicked), NULL);
88 gtk_signal_connect(GTK_OBJECT (button1),"clicked",GTK_SIGNAL_FUNC(buttonclicked1), NULL);
89
90 gtk_container_add(GTK_CONTAINER(window), vbox1);
91 gtk_container_add(GTK_CONTAINER(vbox1), button);
92 gtk_container_add(GTK_CONTAINER(vbox1), button1);
93
94 /* show everything */
95 gtk_widget_show_all(window);
96
97 /* main loop */
98 gtk_main ();
99 gdk_threads_leave();
100
101 return 0;
102 }
103
104 int write_port()
105 {
106 // this function write data to the serial port
107 }
108
109 int read_port(int fd, unsigned char *result)
110 {
111 // this function will read the data from the serial port
112 }
113
114 int dtmf_detection( char *res)
115 {
116 // this function will decode the DTMF signals
117 }
118
119 // this function will dial the provided number
120 int dial_number(char *number)
121 {
122
123 write_port(number);
124 read_port();
125 dtmf_detection();
126 return 0;
127 }
Please let me know what/where I am making the mistake. I have looked at few different examples and put together this test. I have tested all my modem related code in separate unit tests.
Thank you so much for your helps!!!
Nikke