camera zoom fix, upgrade giflib, configure.ac ix86 probe tweaks, any python
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / messages.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <string.h>
23 #include "bcipc.h"
24 #include "language.h"
25 #include "messages.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30
31 Messages::Messages(int input_flag, int output_flag, int id)
32 {
33         if(id == -1)
34         {
35                 msgid = msgget(IPC_PRIVATE, IPC_CREAT | 0777);
36                 client = 0;
37         }
38         else
39         {
40                 this->msgid = id;
41                 client = 1;
42         }
43
44         this->input_flag = input_flag;
45         this->output_flag = output_flag;
46         bc_enter_msg_id(msgid);
47 }
48
49 Messages::~Messages()
50 {
51         if(!client)
52         {
53                 msgctl(msgid, IPC_RMID, NULL);
54                 bc_remove_msg_id(msgid);
55         }
56 }
57
58 char* Messages::get_message_buffer()
59 {
60         return buffer.text;
61 }
62
63 int Messages::read_message(char *text)
64 {
65         buffer.mtype = input_flag;
66
67         if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
68         {
69                 printf(_("receive message failed\n"));
70                 sleep(1);     // don't flood the screen during the loop
71                 return -1;
72         }
73
74 //printf("%d %d\n", buffer.text[0], buffer.mtype);
75         strcpy(text, buffer.text);
76         return 0;
77 }
78
79 long Messages::read_message()
80 {
81         buffer.mtype = input_flag;
82
83         if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
84         {
85                 printf(_("receive message failed\n"));
86                 sleep(1);
87                 return -1;
88         }
89         return atol(buffer.text);
90 }
91
92 float Messages::read_message_f()
93 {
94         float value;
95         char *data = read_message_raw();
96         sscanf(data, "%f", &value);
97         return value;
98 }
99
100 char* Messages::read_message_raw()
101 {
102         buffer.mtype = input_flag;
103
104         if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
105         {
106                 printf(_("receive message failed\n"));
107                 sleep(1);
108                 return "RECIEVE MESSAGE FAILED";
109         }
110         else
111                 return buffer.text;
112 }
113
114 int Messages::read_message(long *value1, long *value2)
115 {
116         char *data = read_message_raw();
117         sscanf(data, "%ld %ld", value1, value2);
118         return 0;
119 }
120
121 int Messages::read_message_f(float *value1, float *value2)
122 {
123         char *data = read_message_raw();
124         sscanf(data, "%f %f", value1, value2);
125         return 0;
126 }
127
128 int Messages::read_message(long *command, long *value1, long *value2)
129 {
130         char *data = read_message_raw();
131         sscanf(data, "%ld %ld %ld", command, value1, value2);
132         return 0;
133 }
134
135 int Messages::read_message(long *command, long *value1, long *value2, long *value3)
136 {
137         char *data = read_message_raw();
138         sscanf(data, "%ld %ld %ld %ld", command, value1, value2, value3);
139         return 0;
140 }
141
142 int Messages::read_message_f(float *value1, float *value2, float *value3)
143 {
144         char *data = read_message_raw();
145         sscanf(data, "%f %f %f", value1, value2, value3);
146         return 0;
147 }
148
149 int Messages::read_message_f(float *value1, float *value2, float *value3, float *value4)
150 {
151         char *data = read_message_raw();
152         sscanf(data, "%f %f %f %f", value1, value2, value3, value4);
153         return 0;
154 }
155
156 int Messages::read_message(int *command, char *text)
157 {
158         int i, j;
159
160         char *data = read_message_raw();
161         sscanf(data, "%d", command);
162 // find the start of the text
163         for(i = 0; i < MESSAGESIZE && data[i] != ' '; i++) { ; }
164 // get the space
165         i++;
166 // copy the text part
167         for(j = 0; (text[j] = data[i]) != 0; i++, j++) { ; }
168         return 0;
169 }
170
171
172 int Messages::write_message(char *text)
173 {
174         buffer.mtype = output_flag;
175         strcpy(buffer.text, text);
176
177         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(text) + 1, 0)) < 0) printf(_("send message failed\n"));
178         return 0;
179 }
180
181 int Messages::write_message_raw()
182 {
183         buffer.mtype = output_flag;
184
185         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) printf(_("send message failed\n"));
186         return 0;
187 }
188
189 int Messages::write_message_flagged(int output_flag, int command)
190 {
191         buffer.mtype = output_flag;
192         sprintf(buffer.text, "%d", command);
193
194         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) printf(_("send message failed\n"));
195         return 0;
196 }
197
198 int Messages::write_message(int number)
199 {
200         sprintf(buffer.text, "%d", number);
201         buffer.mtype = output_flag;
202         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) perror(_("Messages::write_message"));
203         return 0;
204 }
205
206 int Messages::write_message_f(float number)
207 {
208         sprintf(buffer.text, "%f", number);
209         buffer.mtype = output_flag;
210         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) perror(_("Messages::write_message"));
211         return 0;
212 }
213
214 int Messages::write_message(int command, char *text)
215 {
216         sprintf(buffer.text, "%d %s", command, text);
217         return write_message_raw();
218 }
219
220 int Messages::write_message(long command, long value)
221 {
222         sprintf(buffer.text, "%ld %ld", command, value);
223         return write_message_raw();
224 }
225
226 int Messages::write_message_f(float value1, float value2)
227 {
228         sprintf(buffer.text, "%f %f", value1, value2);
229         return write_message_raw();
230 }
231
232 int Messages::write_message(long command, long value1, long value2)
233 {
234         sprintf(buffer.text, "%ld %ld %ld", command, value1, value2);
235         return write_message_raw();
236 }
237
238 int Messages::write_message(long command, long value1, long value2, long value3)
239 {
240         sprintf(buffer.text, "%ld %ld %ld %ld", command, value1, value2, value3);
241         return write_message_raw();
242 }
243
244 int Messages::write_message_f(float value1, float value2, float value3, float value4)
245 {
246         sprintf(buffer.text, "%f %f %f %f", value1, value2, value3, value4);
247         return write_message_raw();
248 }
249
250 int Messages::write_message_f(float value1, float value2, float value3)
251 {
252         sprintf(buffer.text, "%f %f %f", value1, value2, value3);
253         return write_message_raw();
254 }
255
256 int Messages::get_id()
257 {
258         return msgid;
259 }