add new sigc++2 directory
[ardour.git] / libs / pbd / base_ui.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstring>
21 #include <stdint.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25
26 #include <pbd/base_ui.h>
27 #include <pbd/error.h>
28 #include <pbd/compose.h>
29 #include <pbd/failed_constructor.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace PBD;
35         
36 uint32_t BaseUI::rt_bit = 1;
37 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
38
39 BaseUI::BaseUI (string str, bool with_signal_pipe)
40         : _name (str)
41 {
42         /* odd pseudo-singleton semantics */
43
44         base_ui_instance = this;
45
46         signal_pipe[0] = -1;
47         signal_pipe[1] = -1;
48
49         if (with_signal_pipe) {
50                 if (setup_signal_pipe ()) {
51                         throw failed_constructor ();
52                 }
53         }
54 }
55
56 BaseUI::~BaseUI()
57 {
58         if (signal_pipe[0] >= 0) {
59                 close (signal_pipe[0]);
60         } 
61
62         if (signal_pipe[1] >= 0) {
63                 close (signal_pipe[1]);
64         } 
65 }
66
67 BaseUI::RequestType
68 BaseUI::new_request_type ()
69 {
70         RequestType rt;
71
72         /* XXX catch out-of-range */
73
74         rt = RequestType (rt_bit);
75         rt_bit <<= 1;
76
77         return rt;
78 }
79
80 int
81 BaseUI::setup_signal_pipe ()
82 {
83         /* setup the pipe that other threads send us notifications/requests
84            through.
85         */
86
87         if (pipe (signal_pipe)) {
88                 error << string_compose (_("%1-UI: cannot create error signal pipe (%2)"), _name, std::strerror (errno))
89                       << endmsg;
90
91                 return -1;
92         }
93
94         if (fcntl (signal_pipe[0], F_SETFL, O_NONBLOCK)) {
95                 error << string_compose (_("%1-UI: cannot set O_NONBLOCK on signal read pipe (%2)"), _name, std::strerror (errno))
96                       << endmsg;
97                 return -1;
98         }
99
100         if (fcntl (signal_pipe[1], F_SETFL, O_NONBLOCK)) {
101                 error << string_compose (_("%1-UI: cannot set O_NONBLOCK on signal write pipe (%2)"), _name, std::strerror (errno))
102                       << endmsg;
103                 return -1;
104         }
105
106         return 0;
107 }
108