Skip to content

Commit

Permalink
Got this thing finally closing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
falkTX committed Oct 12, 2011
1 parent ee13318 commit f2cd001
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 109 deletions.
35 changes: 21 additions & 14 deletions sixad/bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,32 @@ void l2cap_accept(int ctl, int csk, int isk, int debug, int legacy)
if (!legacy && req.vendor == 0x054c && req.product == 0x0268) {
if (debug) syslog(LOG_INFO, "Will initiate Sixaxis now");

dup2(intr_socket, 1);
close(intr_socket);
dup2(ctrl_socket, 0);
close(ctrl_socket);

// New proccess for sixad-sixaxis
if (fork()) {
pid_t pid = fork();

if (pid == 0) {
// close(ctl);
// close(csk);
// close(isk);

dup2(ctrl_socket, 0);
close(ctrl_socket);
dup2(intr_socket, 1);
close(intr_socket);

char bda[18];
ba2str(&addr_dst, bda);

char cmd[64];
strcpy(cmd, "/usr/sbin/sixad-sixaxis ");
strcat(cmd, bda);
strcat(cmd, " ");
strcat(cmd, debug ? "1" : "0");
const char* uinput_sixaxis_cmd = "/usr/sbin/sixad-sixaxis";
const char* debug_mode = debug ? "1" : "0";

if (!system(cmd)) {
syslog(LOG_INFO, "cannot exec '%s'", cmd);
const char* argv[] = { uinput_sixaxis_cmd, bda, debug_mode, NULL };
char* envp[] = { NULL };

if (execve(argv[0], (char* const*)argv, envp) < 0) {
syslog(LOG_INFO, "cannot exec %s", uinput_sixaxis_cmd);
close(1);
close(0);
}
}

Expand All @@ -241,7 +249,6 @@ void l2cap_accept(int ctl, int csk, int isk, int debug, int legacy)
if (err < 0)
syslog(LOG_ERR, "HID create error %d (%s)", errno, strerror(errno));
close(intr_socket);
sleep(1);
close(ctrl_socket);
}
return;
Expand Down
9 changes: 5 additions & 4 deletions sixad/shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/socket.h>

volatile bool __active = false;
volatile bool __io_canceled = false;
static volatile sig_atomic_t __active = false;
static volatile sig_atomic_t __io_canceled = false;

bool was_active()
{
return __active;
}

void set_active(bool active)
void set_active(int active)
{
__active = active;
}
Expand All @@ -45,7 +46,7 @@ bool io_canceled()

void sig_term(int /* sig */)
{
__io_canceled = true;
__io_canceled = 1;
}

void open_log(const char *app_name)
Expand Down
2 changes: 1 addition & 1 deletion sixad/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct device_settings {
};

bool was_active();
void set_active(bool active);
void set_active(int active);

bool io_canceled();
void sig_term(int sig);
Expand Down
28 changes: 17 additions & 11 deletions sixad/sixad-bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ static void cmd_reset(int ctl, int hdev)
}
#endif

static void sig_hup(int sig)
{
}

int main(int argc, char *argv[])
{
struct sigaction sa;
Expand Down Expand Up @@ -86,17 +90,6 @@ int main(int argc, char *argv[])
return 1;
}

memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDSTOP;

sa.sa_handler = sig_term;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);

sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);

if (remote) {
// BD Remote only

Expand Down Expand Up @@ -127,6 +120,19 @@ int main(int argc, char *argv[])
return 1;
}

memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDSTOP;

sa.sa_handler = sig_term;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = sig_hup;
sigaction(SIGHUP, &sa, NULL);

sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);

syslog(LOG_INFO, "sixad started, press the PS button now");

hid_server(ctl, csk, isk, debug, legacy);
Expand Down
16 changes: 9 additions & 7 deletions sixad/sixad-raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main(int argc, char **argv)
{
int i, fd, nr;
unsigned char buf[128];
struct uinput_fd ufd;
struct uinput_fd *ufd;
struct device_settings settings;

if (argc < 2) {
Expand Down Expand Up @@ -60,9 +60,9 @@ int main(int argc, char **argv)

ufd = uinput_open(DEV_TYPE_SIXAXIS, "hidraw", settings);

if (ufd.js < 0 || ufd.mk < 0) {
if (ufd->js < 0 || ufd->mk < 0) {
return 1;
} else if (ufd.js == 0 && ufd.mk == 0) {
} else if (ufd->js == 0 && ufd->mk == 0) {
syslog(LOG_ERR, "sixaxis config has no joystick or input mode selected - please choose one!");
return 1;
}
Expand All @@ -87,18 +87,20 @@ int main(int argc, char **argv)
msg = false;
}

if (settings.joystick.enabled) do_joystick(ufd.js, buf, settings.joystick);
if (settings.input.enabled) do_input(ufd.mk, buf, settings.input);
if (settings.joystick.enabled) do_joystick(ufd->js, buf, settings.joystick);
if (settings.input.enabled) do_input(ufd->mk, buf, settings.input);
}

if (settings.joystick.enabled) {
uinput_close(ufd.js, 0);
uinput_close(ufd->js, 0);
}
if (settings.input.enabled) {
uinput_close(ufd.mk, 0);
uinput_close(ufd->mk, 0);
}

std::cerr << "sixad-raw::read(buf) - connection has been broken" << std::endl;

delete ufd;

return 0;
}
20 changes: 11 additions & 9 deletions sixad/sixad-remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int timeout = 30;

volatile bool active = false;

struct uinput_fd ufd;
struct uinput_fd *ufd;

static int get_time()
{
Expand Down Expand Up @@ -77,9 +77,9 @@ static void process_remote(struct device_settings settings, const char *mac, int
if (br < 0) {
break;
} else if (br==13 && buf[0]==0xa1 && buf[1]==0x01) { //only continue if we've got a Remote
if (settings.joystick.enabled) do_joystick(ufd.js, buf, settings.joystick);
if (settings.remote.enabled) do_remote(ufd.mk, buf, modes);
if (settings.input.enabled) do_input(ufd.mk, buf, settings.input);
if (settings.joystick.enabled) do_joystick(ufd->js, buf, settings.joystick);
if (settings.remote.enabled) do_remote(ufd->mk, buf, modes);
if (settings.input.enabled) do_input(ufd->mk, buf, settings.input);
} else {
if (debug) syslog(LOG_ERR, "Non-Remote packet received and ignored (0x%02x|0x%02x|0x%02x)", buf[0], buf[1], buf[2]);
}
Expand Down Expand Up @@ -117,9 +117,9 @@ int main(int argc, char *argv[])

ufd = uinput_open(DEV_TYPE_REMOTE, mac, settings);

if (ufd.js < 0 || ufd.mk < 0) {
if (ufd->js < 0 || ufd->mk < 0) {
return 1;
} else if (ufd.js == 0 && ufd.mk == 0) {
} else if (ufd->js == 0 && ufd->mk == 0) {
syslog(LOG_ERR, "remote config has no joystick or input mode selected - please choose one!");
return 1;
}
Expand Down Expand Up @@ -156,7 +156,7 @@ int main(int argc, char *argv[])
p[1].fd = 1;
p[1].events = POLLIN | POLLERR | POLLHUP;

p[2].fd = ufd.mk ? ufd.mk : ufd.js;
p[2].fd = ufd->mk ? ufd->mk : ufd->js;
p[2].events = POLLIN | POLLERR | POLLHUP;

while (!io_canceled()) {
Expand Down Expand Up @@ -184,11 +184,13 @@ int main(int argc, char *argv[])
if (debug) syslog(LOG_INFO, "Closing uinput...");

if (settings.joystick.enabled) {
uinput_close(ufd.js, debug);
uinput_close(ufd->js, debug);
}
if (settings.remote.enabled || settings.input.enabled) {
uinput_close(ufd.mk, debug);
uinput_close(ufd->mk, debug);
}

delete ufd;

if (debug) syslog(LOG_INFO, "Done");

Expand Down
Loading

0 comments on commit f2cd001

Please sign in to comment.