moved 2.1-staging to trunk @ rev 1765
[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 <stdint.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24
25 #include <pbd/base_ui.h>
26 #include <pbd/error.h>
27 #include <pbd/compose.h>
28 #include <pbd/failed_constructor.h>
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace PBD;
34         
35 uint32_t BaseUI::rt_bit = 1;
36 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
37
38 BaseUI::BaseUI (string str, bool with_signal_pipe)
39         : _name (str)
40 {
41         /* odd pseudo-singleton semantics */
42
43         base_ui_instance = this;
44
45         signal_pipe[0] = -1;
46         signal_pipe[1] = -1;
47
48         if (with_signal_pipe) {
49                 if (setup_signal_pipe ()) {
50                         throw failed_constructor ();
51                 }
52         }
53 }
54
55 BaseUI::~BaseUI()
56 {
57         if (signal_pipe[0] >= 0) {
58                 close (signal_pipe[0]);
59         } 
60
61         if (signal_pipe[1] >= 0) {
62                 close (signal_pipe[1]);
63         } 
64 }
65
66 BaseUI::RequestType
67 BaseUI::new_request_type ()
68 {
69         RequestType rt;
70
71         /* XXX catch out-of-range */
72
73         rt = RequestType (rt_bit);
74         rt_bit <<= 1;
75
76         return rt;
77 }
78
79 int
80 BaseUI::setup_signal_pipe ()
81 {
82         /* setup the pipe that other threads send us notifications/requests
83            through.
84         */
85
86         if (pipe (signal_pipe)) {
87                 error << string_compose (_("%1-UI: cannot create error signal pipe (%2)"), _name, std::strerror (errno))
88                       << endmsg;
89
90                 return -1;
91         }
92
93         if (fcntl (signal_pipe[0], F_SETFL, O_NONBLOCK)) {
94                 error << string_compose (_("%1-UI: cannot set O_NONBLOCK on signal read pipe (%2)"), _name, std::strerror (errno))
95                       << endmsg;
96                 return -1;
97         }
98
99         if (fcntl (signal_pipe[1], F_SETFL, O_NONBLOCK)) {
100                 error << string_compose (_("%1-UI: cannot set O_NONBLOCK on signal write pipe (%2)"), _name, std::strerror (errno))
101                       << endmsg;
102                 return -1;
103         }
104
105         return 0;
106 }
107