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