r269@gandalf: fugalh | 2006-08-03 20:18:05 -0600
[ardour.git] / libs / pbd / base_ui.cc
1 #include <stdint.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <errno.h>
5
6 #include <pbd/base_ui.h>
7 #include <pbd/error.h>
8 #include <pbd/compose.h>
9 #include <pbd/failed_constructor.h>
10
11 #include "i18n.h"
12
13 using namespace std;
14 using namespace PBD;
15         
16 uint32_t BaseUI::rt_bit = 1;
17 BaseUI::RequestType BaseUI::CallSlot = BaseUI::new_request_type();
18
19 BaseUI::BaseUI (string str, bool with_signal_pipe)
20         : _name (str)
21 {
22         /* odd pseudo-singleton semantics */
23
24         base_ui_instance = this;
25
26         signal_pipe[0] = -1;
27         signal_pipe[1] = -1;
28
29         if (with_signal_pipe) {
30                 if (setup_signal_pipe ()) {
31                         throw failed_constructor ();
32                 }
33         }
34 }
35
36 BaseUI::~BaseUI()
37 {
38         if (signal_pipe[0] >= 0) {
39                 close (signal_pipe[0]);
40         } 
41
42         if (signal_pipe[1] >= 0) {
43                 close (signal_pipe[1]);
44         } 
45 }
46
47 BaseUI::RequestType
48 BaseUI::new_request_type ()
49 {
50         RequestType rt;
51
52         /* XXX catch out-of-range */
53
54         rt = RequestType (rt_bit);
55         rt_bit <<= 1;
56
57         return rt;
58 }
59
60 int
61 BaseUI::setup_signal_pipe ()
62 {
63         /* setup the pipe that other threads send us notifications/requests
64            through.
65         */
66
67         if (pipe (signal_pipe)) {
68                 error << string_compose (_("%1-UI: cannot create error signal pipe (%2)"), _name, std::strerror (errno))
69                       << endmsg;
70
71                 return -1;
72         }
73
74         if (fcntl (signal_pipe[0], F_SETFL, O_NONBLOCK)) {
75                 error << string_compose (_("%1-UI: cannot set O_NONBLOCK on signal read pipe (%2)"), _name, std::strerror (errno))
76                       << endmsg;
77                 return -1;
78         }
79
80         if (fcntl (signal_pipe[1], F_SETFL, O_NONBLOCK)) {
81                 error << string_compose (_("%1-UI: cannot set O_NONBLOCK on signal write pipe (%2)"), _name, std::strerror (errno))
82                       << endmsg;
83                 return -1;
84         }
85
86         return 0;
87 }
88