chansrv: changes for running as user, not root

master
Jay Sorg 13 years ago
parent 10e283ed16
commit bae50c9ca1

@ -29,7 +29,6 @@
#include "list.h" #include "list.h"
#include "file.h" #include "file.h"
#include "file_loc.h" #include "file_loc.h"
#include "log.h"
static struct trans* g_lis_trans = 0; static struct trans* g_lis_trans = 0;
static struct trans* g_con_trans = 0; static struct trans* g_con_trans = 0;
@ -108,7 +107,7 @@ send_init_response_message(void)
{ {
struct stream * s = (struct stream *)NULL; struct stream * s = (struct stream *)NULL;
log_message(LOG_LEVEL_INFO,"send_init_response_message:"); LOGM((LOG_LEVEL_INFO,"send_init_response_message:"))
s = trans_get_out_s(g_con_trans, 8192); s = trans_get_out_s(g_con_trans, 8192);
if (s == 0) if (s == 0)
{ {
@ -129,7 +128,7 @@ send_channel_setup_response_message(void)
{ {
struct stream * s = (struct stream *)NULL; struct stream * s = (struct stream *)NULL;
log_message(LOG_LEVEL_DEBUG, "send_channel_setup_response_message:"); LOGM((LOG_LEVEL_DEBUG, "send_channel_setup_response_message:"));
s = trans_get_out_s(g_con_trans, 8192); s = trans_get_out_s(g_con_trans, 8192);
if (s == 0) if (s == 0)
{ {
@ -150,7 +149,7 @@ send_channel_data_response_message(void)
{ {
struct stream * s = (struct stream *)NULL; struct stream * s = (struct stream *)NULL;
log_message(LOG_LEVEL_DEBUG, "send_channel_data_response_message:"); LOGM((LOG_LEVEL_DEBUG, "send_channel_data_response_message:"));
s = trans_get_out_s(g_con_trans, 8192); s = trans_get_out_s(g_con_trans, 8192);
if (s == 0) if (s == 0)
{ {
@ -169,7 +168,7 @@ send_channel_data_response_message(void)
static int APP_CC static int APP_CC
process_message_init(struct stream* s) process_message_init(struct stream* s)
{ {
log_message(LOG_LEVEL_DEBUG,"process_message_init:"); LOGM((LOG_LEVEL_DEBUG, "process_message_init:"));
return send_init_response_message(); return send_init_response_message();
} }
@ -190,9 +189,10 @@ process_message_channel_setup(struct stream* s)
g_cliprdr_chan_id = -1; g_cliprdr_chan_id = -1;
g_rdpsnd_chan_id = -1; g_rdpsnd_chan_id = -1;
g_rdpdr_chan_id = -1; g_rdpdr_chan_id = -1;
log_message(LOG_LEVEL_DEBUG, "process_message_channel_setup:"); LOGM((LOG_LEVEL_DEBUG, "process_message_channel_setup:"));
in_uint16_le(s, num_chans); in_uint16_le(s, num_chans);
log_message(LOG_LEVEL_DEBUG,"process_message_channel_setup: num_chans %d", num_chans); LOGM((LOG_LEVEL_DEBUG, "process_message_channel_setup: num_chans %d",
num_chans));
for (index = 0; index < num_chans; index++) for (index = 0; index < num_chans; index++)
{ {
ci = &(g_chan_items[g_num_chan_items]); ci = &(g_chan_items[g_num_chan_items]);
@ -200,8 +200,8 @@ process_message_channel_setup(struct stream* s)
in_uint8a(s, ci->name, 8); in_uint8a(s, ci->name, 8);
in_uint16_le(s, ci->id); in_uint16_le(s, ci->id);
in_uint16_le(s, ci->flags); in_uint16_le(s, ci->flags);
log_message(LOG_LEVEL_DEBUG, "process_message_channel_setup: chan name '%s' " LOGM((LOG_LEVEL_DEBUG, "process_message_channel_setup: chan name '%s' "
"id %d flags %8.8x", ci->name, ci->id, ci->flags); "id %d flags %8.8x", ci->name, ci->id, ci->flags));
if (g_strcasecmp(ci->name, "cliprdr") == 0) if (g_strcasecmp(ci->name, "cliprdr") == 0)
{ {
g_cliprdr_index = g_num_chan_items; g_cliprdr_index = g_num_chan_items;
@ -250,8 +250,8 @@ process_message_channel_data(struct stream* s)
in_uint16_le(s, chan_flags); in_uint16_le(s, chan_flags);
in_uint16_le(s, length); in_uint16_le(s, length);
in_uint32_le(s, total_length); in_uint32_le(s, total_length);
log_message(LOG_LEVEL_DEBUG,"process_message_channel_data: chan_id %d " LOGM((LOG_LEVEL_DEBUG,"process_message_channel_data: chan_id %d "
"chan_flags %d", chan_id, chan_flags); "chan_flags %d", chan_id, chan_flags));
rv = send_channel_data_response_message(); rv = send_channel_data_response_message();
if (rv == 0) if (rv == 0)
{ {
@ -322,17 +322,15 @@ process_message(void)
rv = process_message_channel_data_response(s); rv = process_message_channel_data_response(s);
break; break;
default: default:
log_message(LOG_LEVEL_ERROR, "process_message: error in process_message ", LOGM((LOG_LEVEL_ERROR, "process_message: error in process_message ",
"unknown msg %d", id); "unknown msg %d", id));
break; break;
} }
if (rv != 0) if (rv != 0)
{ {
break; break;
} }
else { s->p = next_msg;
s->p = next_msg;
}
} }
return rv; return rv;
} }
@ -355,7 +353,7 @@ my_trans_data_in(struct trans* trans)
{ {
return 1; return 1;
} }
log_message(LOG_LEVEL_DEBUG,"my_trans_data_in:"); LOGM((LOG_LEVEL_DEBUG, "my_trans_data_in:"));
s = trans_get_in_s(trans); s = trans_get_in_s(trans);
in_uint32_le(s, id); in_uint32_le(s, id);
in_uint32_le(s, size); in_uint32_le(s, size);
@ -388,7 +386,7 @@ my_trans_conn_in(struct trans* trans, struct trans* new_trans)
{ {
return 1; return 1;
} }
log_message(LOG_LEVEL_DEBUG, "my_trans_conn_in:"); LOGM((LOG_LEVEL_DEBUG, "my_trans_conn_in:"));
g_con_trans = new_trans; g_con_trans = new_trans;
g_con_trans->trans_data_in = my_trans_data_in; g_con_trans->trans_data_in = my_trans_data_in;
g_con_trans->header_size = 8; g_con_trans->header_size = 8;
@ -412,7 +410,8 @@ setup_listen(void)
if (g_use_unix_socket) if (g_use_unix_socket)
{ {
g_lis_trans = trans_create(2, 8192, 8192); g_lis_trans = trans_create(2, 8192, 8192);
g_snprintf(port, 255, "/tmp/.xrdp/xrdp_chansrv_socket_%d", 7200 + g_display_num); g_snprintf(port, 255, "/tmp/.xrdp/xrdp_chansrv_socket_%d",
7200 + g_display_num);
} }
else else
{ {
@ -423,7 +422,8 @@ setup_listen(void)
error = trans_listen(g_lis_trans, port); error = trans_listen(g_lis_trans, port);
if (error != 0) if (error != 0)
{ {
log_message(LOG_LEVEL_ERROR, "setup_listen: trans_listen failed for port %s", port); LOGM((LOG_LEVEL_ERROR, "setup_listen: trans_listen failed for port %s",
port));
return 1; return 1;
} }
return 0; return 0;
@ -439,7 +439,7 @@ channel_thread_loop(void* in_val)
int error = 0; int error = 0;
THREAD_RV rv = 0; THREAD_RV rv = 0;
log_message(LOG_LEVEL_INFO, "channel_thread_loop: thread start"); LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread start"));
rv = 0; rv = 0;
error = setup_listen(); error = setup_listen();
if (error == 0) if (error == 0)
@ -453,7 +453,7 @@ channel_thread_loop(void* in_val)
{ {
if (g_is_wait_obj_set(g_term_event)) if (g_is_wait_obj_set(g_term_event))
{ {
log_message(LOG_LEVEL_INFO, "channel_thread_loop: g_term_event set"); LOGM((LOG_LEVEL_INFO, "channel_thread_loop: g_term_event set"));
clipboard_deinit(); clipboard_deinit();
sound_deinit(); sound_deinit();
dev_redir_deinit(); dev_redir_deinit();
@ -463,15 +463,16 @@ channel_thread_loop(void* in_val)
{ {
if (trans_check_wait_objs(g_lis_trans) != 0) if (trans_check_wait_objs(g_lis_trans) != 0)
{ {
log_message(LOG_LEVEL_INFO, "channel_thread_loop: trans_check_wait_objs error"); LOGM((LOG_LEVEL_INFO, "channel_thread_loop: "
"trans_check_wait_objs error"));
} }
} }
if (g_con_trans != 0) if (g_con_trans != 0)
{ {
if (trans_check_wait_objs(g_con_trans) != 0) if (trans_check_wait_objs(g_con_trans) != 0)
{ {
log_message(LOG_LEVEL_INFO, "channel_thread_loop: " LOGM((LOG_LEVEL_INFO, "channel_thread_loop: "
"trans_check_wait_objs error resetting"); "trans_check_wait_objs error resetting"));
clipboard_deinit(); clipboard_deinit();
sound_deinit(); sound_deinit();
dev_redir_deinit(); dev_redir_deinit();
@ -504,7 +505,7 @@ channel_thread_loop(void* in_val)
g_lis_trans = 0; g_lis_trans = 0;
trans_delete(g_con_trans); trans_delete(g_con_trans);
g_con_trans = 0; g_con_trans = 0;
log_message(LOG_LEVEL_INFO, "channel_thread_loop: thread stop"); LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread stop"));
g_set_wait_obj(g_thread_done_event); g_set_wait_obj(g_thread_done_event);
return rv; return rv;
} }
@ -513,7 +514,7 @@ channel_thread_loop(void* in_val)
void DEFAULT_CC void DEFAULT_CC
term_signal_handler(int sig) term_signal_handler(int sig)
{ {
log_message(LOG_LEVEL_INFO,"term_signal_handler: got signal %d", sig); LOGM((LOG_LEVEL_INFO, "term_signal_handler: got signal %d", sig));
g_set_wait_obj(g_term_event); g_set_wait_obj(g_term_event);
} }
@ -521,7 +522,7 @@ term_signal_handler(int sig)
void DEFAULT_CC void DEFAULT_CC
nil_signal_handler(int sig) nil_signal_handler(int sig)
{ {
log_message(LOG_LEVEL_INFO, "nil_signal_handler: got signal %d", sig); LOGM((LOG_LEVEL_INFO, "nil_signal_handler: got signal %d", sig));
g_set_wait_obj(g_term_event); g_set_wait_obj(g_term_event);
} }
@ -637,13 +638,16 @@ main(int argc, char** argv)
int pid = 0; int pid = 0;
char text[256] = ""; char text[256] = "";
char* display_text = (char *)NULL; char* display_text = (char *)NULL;
enum logReturns error; #if XRDP_CHANNEL_LOG
char cfg_file[256]; char cfg_file[256];
enum logReturns error;
#endif
g_init("xrdp-chansrv"); /* os_calls */ g_init("xrdp-chansrv"); /* os_calls */
read_ini(); read_ini();
pid = g_getpid(); pid = g_getpid();
#if XRDP_CHANNEL_LOG
/* starting logging subsystem */ /* starting logging subsystem */
g_snprintf(cfg_file, 255, "%s/sesman.ini", XRDP_CFG_PATH); g_snprintf(cfg_file, 255, "%s/sesman.ini", XRDP_CFG_PATH);
error = log_start(cfg_file,"XRDP-Chansrv"); error = log_start(cfg_file,"XRDP-Chansrv");
@ -665,22 +669,22 @@ main(int argc, char** argv)
g_deinit(); g_deinit();
g_exit(1); g_exit(1);
} }
log_message(LOG_LEVEL_ALWAYS,"main: app started pid %d(0x%8.8x)", pid, pid); LOGM((LOG_LEVEL_ALWAYS, "main: app started pid %d(0x%8.8x)", pid, pid));
#endif
/* set up signal handler */ /* set up signal handler */
g_signal_kill(term_signal_handler); /* SIGKILL */ g_signal_kill(term_signal_handler); /* SIGKILL */
g_signal_terminate(term_signal_handler); /* SIGTERM */ g_signal_terminate(term_signal_handler); /* SIGTERM */
g_signal_user_interrupt(term_signal_handler); /* SIGINT */ g_signal_user_interrupt(term_signal_handler); /* SIGINT */
g_signal_pipe(nil_signal_handler); /* SIGPIPE */ g_signal_pipe(nil_signal_handler); /* SIGPIPE */
display_text = g_getenv("DISPLAY"); display_text = g_getenv("DISPLAY");
log_message(LOG_LEVEL_INFO, "main: DISPLAY env var set to %s", display_text); LOGM((LOG_LEVEL_INFO, "main: DISPLAY env var set to %s", display_text));
get_display_num_from_display(display_text); get_display_num_from_display(display_text);
if (g_display_num == 0) if (g_display_num == 0)
{ {
log_message(LOG_LEVEL_ERROR, "main: error, display is zero"); LOGM((LOG_LEVEL_ERROR, "main: error, display is zero"));
return 1; return 1;
} }
log_message(LOG_LEVEL_INFO,"main: using DISPLAY %d", g_display_num); LOGM((LOG_LEVEL_INFO, "main: using DISPLAY %d", g_display_num));
g_snprintf(text, 255, "xrdp_chansrv_%8.8x_main_term", pid); g_snprintf(text, 255, "xrdp_chansrv_%8.8x_main_term", pid);
g_term_event = g_create_wait_obj(text); g_term_event = g_create_wait_obj(text);
g_snprintf(text, 255, "xrdp_chansrv_%8.8x_thread_done", pid); g_snprintf(text, 255, "xrdp_chansrv_%8.8x_thread_done", pid);
@ -690,7 +694,7 @@ main(int argc, char** argv)
{ {
if (g_obj_wait(&g_term_event, 1, 0, 0, 0) != 0) if (g_obj_wait(&g_term_event, 1, 0, 0, 0) != 0)
{ {
log_message(LOG_LEVEL_ERROR, "main: error, g_obj_wait failed"); LOGM((LOG_LEVEL_ERROR, "main: error, g_obj_wait failed"));
break; break;
} }
} }
@ -699,13 +703,13 @@ main(int argc, char** argv)
/* wait for thread to exit */ /* wait for thread to exit */
if (g_obj_wait(&g_thread_done_event, 1, 0, 0, 0) != 0) if (g_obj_wait(&g_thread_done_event, 1, 0, 0, 0) != 0)
{ {
log_message(LOG_LEVEL_ERROR, "main: error, g_obj_wait failed"); LOGM((LOG_LEVEL_ERROR, "main: error, g_obj_wait failed"));
break; break;
} }
} }
/* cleanup */ /* cleanup */
main_cleanup(); main_cleanup();
log_message(LOG_LEVEL_INFO, "main: app exiting pid %d(0x%8.8x)", pid, pid); LOGM((LOG_LEVEL_INFO, "main: app exiting pid %d(0x%8.8x)", pid, pid));
g_deinit(); g_deinit();
return 0; return 0;
} }

@ -5,6 +5,8 @@
#include "arch.h" #include "arch.h"
#include "parse.h" #include "parse.h"
#define XRDP_CHANNEL_LOG 0
struct chan_item struct chan_item
{ {
int id; int id;
@ -28,4 +30,11 @@ main_cleanup(void);
} \ } \
} }
#if XRDP_CHANNEL_LOG
#include "log.h"
#define LOGM(_args) do { log_message _args ; } while (0)
#else
#define LOGM(_args)
#endif
#endif #endif

@ -29,7 +29,6 @@
#include "parse.h" #include "parse.h"
#include "os_calls.h" #include "os_calls.h"
#include "chansrv.h" #include "chansrv.h"
#include "log.h"
#include "clipboard.h" #include "clipboard.h"
static Atom g_clipboard_atom = 0; static Atom g_clipboard_atom = 0;
@ -80,7 +79,7 @@ clipboard_error_handler(Display* dis, XErrorEvent* xer)
char text[256]; char text[256];
XGetErrorText(dis, xer->error_code, text, 255); XGetErrorText(dis, xer->error_code, text, 255);
log_message(LOG_LEVEL_ERROR,"error [%s]", text); LOGM((LOG_LEVEL_ERROR,"error [%s]", text));
return 0; return 0;
} }
@ -91,7 +90,7 @@ clipboard_error_handler(Display* dis, XErrorEvent* xer)
int DEFAULT_CC int DEFAULT_CC
clipboard_fatal_handler(Display* dis) clipboard_fatal_handler(Display* dis)
{ {
log_message(LOG_LEVEL_ALWAYS,"fatal error, exiting"); LOGM((LOG_LEVEL_ALWAYS, "fatal error, exiting"));
main_cleanup(); main_cleanup();
return 0; return 0;
} }
@ -141,7 +140,7 @@ clipboard_init(void)
int ver_min; int ver_min;
Status st; Status st;
log_message(LOG_LEVEL_DEBUG,"xrdp-chansrv: in clipboard_init"); LOGM((LOG_LEVEL_DEBUG, "xrdp-chansrv: in clipboard_init"));
if (g_clip_up) if (g_clip_up)
{ {
return 0; return 0;
@ -155,7 +154,7 @@ clipboard_init(void)
g_display = XOpenDisplay(0); g_display = XOpenDisplay(0);
if (g_display == 0) if (g_display == 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_init: XOpenDisplay failed"); LOGM((LOG_LEVEL_ERROR, "clipboard_init: XOpenDisplay failed"));
rv = 1; rv = 1;
} }
if (rv == 0) if (rv == 0)
@ -163,7 +162,7 @@ clipboard_init(void)
g_x_socket = XConnectionNumber(g_display); g_x_socket = XConnectionNumber(g_display);
if (g_x_socket == 0) if (g_x_socket == 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_init: XConnectionNumber failed"); LOGM((LOG_LEVEL_ERROR, "clipboard_init: XConnectionNumber failed"));
rv = 2; rv = 2;
} }
g_x_wait_obj = g_create_wait_obj_from_socket(g_x_socket, 0); g_x_wait_obj = g_create_wait_obj_from_socket(g_x_socket, 0);
@ -173,7 +172,7 @@ clipboard_init(void)
g_clipboard_atom = XInternAtom(g_display, "CLIPBOARD", False); g_clipboard_atom = XInternAtom(g_display, "CLIPBOARD", False);
if (g_clipboard_atom == None) if (g_clipboard_atom == None)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_init: XInternAtom failed"); LOGM((LOG_LEVEL_ERROR, "clipboard_init: XInternAtom failed"));
rv = 3; rv = 3;
} }
} }
@ -181,15 +180,17 @@ clipboard_init(void)
{ {
if (!XFixesQueryExtension(g_display, &g_xfixes_event_base, &dummy)) if (!XFixesQueryExtension(g_display, &g_xfixes_event_base, &dummy))
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_init: no xfixes"); LOGM((LOG_LEVEL_ERROR, "clipboard_init: no xfixes"));
rv = 5; rv = 5;
} }
} }
if (rv == 0) if (rv == 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_init: g_xfixes_event_base %d", g_xfixes_event_base); LOGM((LOG_LEVEL_ERROR, "clipboard_init: g_xfixes_event_base %d",
g_xfixes_event_base));
st = XFixesQueryVersion(g_display, &ver_maj, &ver_min); st = XFixesQueryVersion(g_display, &ver_maj, &ver_min);
log_message(LOG_LEVEL_ERROR,"clipboard_init st %d, maj %d min %d", st, ver_maj, ver_min); LOGM((LOG_LEVEL_ERROR, "clipboard_init st %d, maj %d min %d", st,
ver_maj, ver_min));
g_screen_num = DefaultScreen(g_display); g_screen_num = DefaultScreen(g_display);
g_screen = ScreenOfDisplay(g_display, g_screen_num); g_screen = ScreenOfDisplay(g_display, g_screen_num);
g_clip_property_atom = XInternAtom(g_display, "XRDP_CLIP_PROPERTY_ATOM", g_clip_property_atom = XInternAtom(g_display, "XRDP_CLIP_PROPERTY_ATOM",
@ -223,13 +224,13 @@ clipboard_init(void)
out_uint32_le(s, 0); /* extra 4 bytes ? */ out_uint32_le(s, 0); /* extra 4 bytes ? */
s_mark_end(s); s_mark_end(s);
size = (int)(s->end - s->data); size = (int)(s->end - s->data);
log_message(LOG_LEVEL_DEBUG,"clipboard_init: data out, sending " LOGM((LOG_LEVEL_DEBUG, "clipboard_init: data out, sending "
"CLIPRDR_CONNECT (clip_msg_id = 1)"); "CLIPRDR_CONNECT (clip_msg_id = 1)"));
rv = send_channel_data(g_cliprdr_chan_id, s->data, size); rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
if (rv != 0) if (rv != 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_init: send_channel_data failed " LOGM((LOG_LEVEL_ERROR, "clipboard_init: send_channel_data failed "
"rv = %d", rv); "rv = %d", rv));
rv = 4; rv = 4;
} }
free_stream(s); free_stream(s);
@ -242,7 +243,7 @@ clipboard_init(void)
} }
else else
{ {
log_message(LOG_LEVEL_ERROR,"xrdp-chansrv: clipboard_init: error on exit"); LOGM((LOG_LEVEL_ERROR, "xrdp-chansrv: clipboard_init: error on exit"));
} }
return rv; return rv;
} }
@ -284,10 +285,11 @@ clipboard_send_data_request(void)
int size; int size;
int rv; int rv;
log_message(LOG_LEVEL_DEBUG,"clipboard_send_data_request:"); LOGM((LOG_LEVEL_DEBUG, "clipboard_send_data_request:"));
if (!g_got_format_announce) if (!g_got_format_announce)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_send_data_request: error, no format announce"); LOGM((LOG_LEVEL_ERROR, "clipboard_send_data_request: error, "
"no format announce"));
return 0; return 0;
} }
g_got_format_announce = 0; g_got_format_announce = 0;
@ -299,8 +301,8 @@ clipboard_send_data_request(void)
out_uint32_le(s, 0x0d); out_uint32_le(s, 0x0d);
s_mark_end(s); s_mark_end(s);
size = (int)(s->end - s->data); size = (int)(s->end - s->data);
log_message(LOG_LEVEL_DEBUG,"clipboard_send_data_request: data out, sending " LOGM((LOG_LEVEL_DEBUG,"clipboard_send_data_request: data out, sending "
"CLIPRDR_DATA_REQUEST (clip_msg_id = 4)"); "CLIPRDR_DATA_REQUEST (clip_msg_id = 4)"));
rv = send_channel_data(g_cliprdr_chan_id, s->data, size); rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s); free_stream(s);
return rv; return rv;
@ -322,8 +324,8 @@ clipboard_send_format_ack(void)
out_uint32_le(s, 0); /* extra 4 bytes ? */ out_uint32_le(s, 0); /* extra 4 bytes ? */
s_mark_end(s); s_mark_end(s);
size = (int)(s->end - s->data); size = (int)(s->end - s->data);
log_message(LOG_LEVEL_DEBUG,"clipboard_send_format_ack: data out, sending " LOGM((LOG_LEVEL_DEBUG,"clipboard_send_format_ack: data out, sending "
"CLIPRDR_FORMAT_ACK (clip_msg_id = 3)"); "CLIPRDR_FORMAT_ACK (clip_msg_id = 3)"));
rv = send_channel_data(g_cliprdr_chan_id, s->data, size); rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s); free_stream(s);
return rv; return rv;
@ -346,8 +348,8 @@ clipboard_send_format_announce(void)
out_uint8s(s, 0x90); out_uint8s(s, 0x90);
s_mark_end(s); s_mark_end(s);
size = (int)(s->end - s->data); size = (int)(s->end - s->data);
log_message(LOG_LEVEL_DEBUG,"clipboard_send_format_announce: data out, sending " LOGM((LOG_LEVEL_DEBUG,"clipboard_send_format_announce: data out, sending "
"CLIPRDR_FORMAT_ANNOUNCE (clip_msg_id = 2)"); "CLIPRDR_FORMAT_ANNOUNCE (clip_msg_id = 2)"));
rv = send_channel_data(g_cliprdr_chan_id, s->data, size); rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s); free_stream(s);
return rv; return rv;
@ -401,7 +403,7 @@ clipboard_send_data_response(void)
num_chars = g_mbstowcs(0, g_last_clip_data, 0); num_chars = g_mbstowcs(0, g_last_clip_data, 0);
if (num_chars < 0) if (num_chars < 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_send_data_response: bad string"); LOGM((LOG_LEVEL_ERROR, "clipboard_send_data_response: bad string"));
num_chars = 0; num_chars = 0;
} }
} }
@ -415,16 +417,16 @@ clipboard_send_data_response(void)
out_uint32_le(s, num_chars * 2 + 2); /* length */ out_uint32_le(s, num_chars * 2 + 2); /* length */
if (clipboard_out_unicode(s, g_last_clip_data, num_chars) != num_chars * 2) if (clipboard_out_unicode(s, g_last_clip_data, num_chars) != num_chars * 2)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_send_data_response: error " LOGM((LOG_LEVEL_ERROR,"clipboard_send_data_response: error "
"clipboard_out_unicode didn't write right number of bytes"); "clipboard_out_unicode didn't write right number of bytes"));
} }
out_uint16_le(s, 0); /* nil for string */ out_uint16_le(s, 0); /* nil for string */
out_uint32_le(s, 0); out_uint32_le(s, 0);
s_mark_end(s); s_mark_end(s);
size = (int)(s->end - s->data); size = (int)(s->end - s->data);
log_message(LOG_LEVEL_DEBUG,"clipboard_send_data_response: data out, sending " LOGM((LOG_LEVEL_DEBUG,"clipboard_send_data_response: data out, sending "
"CLIPRDR_DATA_RESPONSE (clip_msg_id = 5) size %d num_chars %d", "CLIPRDR_DATA_RESPONSE (clip_msg_id = 5) size %d num_chars %d",
size, num_chars); size, num_chars));
rv = send_channel_data(g_cliprdr_chan_id, s->data, size); rv = send_channel_data(g_cliprdr_chan_id, s->data, size);
free_stream(s); free_stream(s);
return rv; return rv;
@ -494,14 +496,16 @@ static int APP_CC
clipboard_process_format_announce(struct stream* s, int clip_msg_status, clipboard_process_format_announce(struct stream* s, int clip_msg_status,
int clip_msg_len) int clip_msg_len)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_process_format_announce: CLIPRDR_FORMAT_ANNOUNCE"); LOGM((LOG_LEVEL_DEBUG, "clipboard_process_format_announce: "
"CLIPRDR_FORMAT_ANNOUNCE"));
//g_hexdump(s->p, s->end - s->p); //g_hexdump(s->p, s->end - s->p);
clipboard_send_format_ack(); clipboard_send_format_ack();
g_got_format_announce = 1; g_got_format_announce = 1;
g_data_in_up_to_date = 0; g_data_in_up_to_date = 0;
if (clipboard_set_selection_owner() != 0) if (clipboard_set_selection_owner() != 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_process_format_announce: XSetSelectionOwner failed"); LOGM((LOG_LEVEL_ERROR, "clipboard_process_format_announce: "
"XSetSelectionOwner failed"));
} }
return 0; return 0;
} }
@ -511,7 +515,7 @@ static int APP_CC
clipboard_prcoess_format_ack(struct stream* s, int clip_msg_status, clipboard_prcoess_format_ack(struct stream* s, int clip_msg_status,
int clip_msg_len) int clip_msg_len)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_prcoess_format_ack: CLIPRDR_FORMAT_ACK"); LOGM((LOG_LEVEL_DEBUG,"clipboard_prcoess_format_ack: CLIPRDR_FORMAT_ACK"));
//g_hexdump(s->p, s->end - s->p); //g_hexdump(s->p, s->end - s->p);
return 0; return 0;
} }
@ -521,7 +525,8 @@ static int APP_CC
clipboard_process_data_request(struct stream* s, int clip_msg_status, clipboard_process_data_request(struct stream* s, int clip_msg_status,
int clip_msg_len) int clip_msg_len)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_process_data_request: CLIPRDR_DATA_REQUEST"); LOGM((LOG_LEVEL_DEBUG,"clipboard_process_data_request: "
"CLIPRDR_DATA_REQUEST"));
//g_hexdump(s->p, s->end - s->p); //g_hexdump(s->p, s->end - s->p);
clipboard_send_data_response(); clipboard_send_data_response();
return 0; return 0;
@ -539,7 +544,8 @@ clipboard_process_data_response(struct stream* s, int clip_msg_status,
int index; int index;
int data_in_len; int data_in_len;
log_message(LOG_LEVEL_DEBUG,"clipboard_process_data_response: CLIPRDR_DATA_RESPONSE"); LOGM((LOG_LEVEL_DEBUG,"clipboard_process_data_response: "
"CLIPRDR_DATA_RESPONSE"));
g_waiting_for_data_response = 0; g_waiting_for_data_response = 0;
len = (int)(s->end - s->p); len = (int)(s->end - s->p);
if (len < 1) if (len < 1)
@ -590,8 +596,8 @@ clipboard_process_data_response(struct stream* s, int clip_msg_status,
lxev = &(g_selection_request_event[index]); lxev = &(g_selection_request_event[index]);
clipboard_provide_selection(lxev, lxev->target, 8, g_data_in, clipboard_provide_selection(lxev, lxev->target, 8, g_data_in,
data_in_len); data_in_len);
log_message(LOG_LEVEL_DEBUG,"clipboard_process_data_response: requestor %d data_in_len %d", LOGM((LOG_LEVEL_DEBUG,"clipboard_process_data_response: requestor %d "
lxev->requestor, data_in_len); "data_in_len %d", lxev->requestor, data_in_len));
} }
} }
g_selection_request_event_count = 0; g_selection_request_event_count = 0;
@ -657,7 +663,8 @@ clipboard_data_in(struct stream* s, int chan_id, int chan_flags, int length,
clip_msg_len); clip_msg_len);
break; break;
default: default:
log_message(LOG_LEVEL_ERROR,"clipboard_data_in: unknown clip_msg_id %d", clip_msg_id); LOGM((LOG_LEVEL_ERROR, "clipboard_data_in: unknown clip_msg_id %d",
clip_msg_id));
break; break;
} }
XFlush(g_display); XFlush(g_display);
@ -685,13 +692,13 @@ clipboard_event_selection_owner_notify(XEvent* xevent)
XFixesSelectionNotifyEvent* lxevent; XFixesSelectionNotifyEvent* lxevent;
lxevent = (XFixesSelectionNotifyEvent*)xevent; lxevent = (XFixesSelectionNotifyEvent*)xevent;
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_owner_notify: " LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_owner_notify: "
"window %d subtype %d owner %d g_wnd %d", "window %d subtype %d owner %d g_wnd %d",
lxevent->window, lxevent->subtype, lxevent->owner, g_wnd); lxevent->window, lxevent->subtype, lxevent->owner, g_wnd));
if (lxevent->owner == g_wnd) if (lxevent->owner == g_wnd)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_owner_notify: skipping, " LOGM((LOG_LEVEL_DEBUG,"clipboard_event_selection_owner_notify: skipping, "
"onwer == g_wnd"); "onwer == g_wnd"));
g_got_selection = 1; g_got_selection = 1;
return 0; return 0;
} }
@ -811,7 +818,10 @@ clipboard_event_selection_notify(XEvent* xevent)
int* atoms; int* atoms;
Atom type; Atom type;
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_notify:"); LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_notify:"));
data_size = 0;
n_items = 0;
fmt = 0;
convert_to_string = 0; convert_to_string = 0;
convert_to_utf8 = 0; convert_to_utf8 = 0;
send_format_announce = 0; send_format_announce = 0;
@ -821,8 +831,8 @@ clipboard_event_selection_notify(XEvent* xevent)
lxevent = (XSelectionEvent*)xevent; lxevent = (XSelectionEvent*)xevent;
if (lxevent->property == None) if (lxevent->property == None)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_notify: clip could " LOGM((LOG_LEVEL_ERROR, "clipboard_event_selection_notify: clip could "
"not be converted"); "not be converted"));
rv = 1; rv = 1;
} }
if (rv == 0) if (rv == 0)
@ -832,8 +842,8 @@ clipboard_event_selection_notify(XEvent* xevent)
&n_items, &data, &data_size); &n_items, &data, &data_size);
if (rv != 0) if (rv != 0)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_notify: " LOGM((LOG_LEVEL_ERROR, "clipboard_event_selection_notify: "
"clipboard_get_window_property failed error %d", rv); "clipboard_get_window_property failed error %d", rv));
} }
XDeleteProperty(g_display, lxevent->requestor, lxevent->property); XDeleteProperty(g_display, lxevent->requestor, lxevent->property);
} }
@ -849,8 +859,8 @@ clipboard_event_selection_notify(XEvent* xevent)
for (index = 0; index < n_items; index++) for (index = 0; index < n_items; index++)
{ {
atom = atoms[index]; atom = atoms[index];
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_notify: %d %s %d", atom, LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_notify: %d %s %d",
XGetAtomName(g_display, atom), XA_STRING); atom, XGetAtomName(g_display, atom), XA_STRING));
if (atom == g_utf8_atom) if (atom == g_utf8_atom)
{ {
convert_to_utf8 = 1; convert_to_utf8 = 1;
@ -863,15 +873,15 @@ clipboard_event_selection_notify(XEvent* xevent)
} }
else else
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_notify: error, target is " LOGM((LOG_LEVEL_ERROR, "clipboard_event_selection_notify: error, "
"'TARGETS' and type[%d] or fmt[%d] not right, should be " "target is 'TARGETS' and type[%d] or fmt[%d] not right, "
"type[%d], fmt[%d]", type, fmt, XA_ATOM, 32); "should be type[%d], fmt[%d]", type, fmt, XA_ATOM, 32));
} }
} }
else if (lxevent->target == g_utf8_atom) else if (lxevent->target == g_utf8_atom)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_notify: UTF8_STRING data_size %d", LOGM((LOG_LEVEL_DEBUG,"clipboard_event_selection_notify: UTF8_STRING "
data_size); "data_size %d", data_size));
g_free(g_last_clip_data); g_free(g_last_clip_data);
g_last_clip_size = data_size; g_last_clip_size = data_size;
g_last_clip_data = g_malloc(g_last_clip_size + 1, 0); g_last_clip_data = g_malloc(g_last_clip_size + 1, 0);
@ -882,8 +892,8 @@ clipboard_event_selection_notify(XEvent* xevent)
} }
else if (lxevent->target == XA_STRING) else if (lxevent->target == XA_STRING)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_notify: XA_STRING data_size %d", LOGM((LOG_LEVEL_DEBUG,"clipboard_event_selection_notify: XA_STRING "
data_size); "data_size %d", data_size));
g_free(g_last_clip_data); g_free(g_last_clip_data);
g_last_clip_size = data_size; g_last_clip_size = data_size;
g_last_clip_data = g_malloc(g_last_clip_size + 1, 0); g_last_clip_data = g_malloc(g_last_clip_size + 1, 0);
@ -894,12 +904,14 @@ clipboard_event_selection_notify(XEvent* xevent)
} }
else else
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_notify: unknown target"); LOGM((LOG_LEVEL_ERROR, "clipboard_event_selection_notify: "
"unknown target"));
} }
} }
else else
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_notify: unknown selection"); LOGM((LOG_LEVEL_ERROR,"clipboard_event_selection_notify: "
"unknown selection"));
} }
} }
if (convert_to_utf8) if (convert_to_utf8)
@ -941,7 +953,7 @@ clipboard_event_selection_notify(XEvent* xevent)
static int APP_CC static int APP_CC
clipboard_event_selection_request(XEvent* xevent) clipboard_event_selection_request(XEvent* xevent)
{ {
XEvent xev; //XEvent xev;
XSelectionRequestEvent* lxev; XSelectionRequestEvent* lxev;
tui32 ui32[8]; tui32 ui32[8];
Atom type; Atom type;
@ -951,19 +963,21 @@ clipboard_event_selection_request(XEvent* xevent)
char* xdata; char* xdata;
lxev = (XSelectionRequestEvent*)xevent; lxev = (XSelectionRequestEvent*)xevent;
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: g_wnd %d, " LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: g_wnd %d, "
".requestor %d .owner %d .selection %d '%s' .target %d .property %d", ".requestor %d .owner %d .selection %d '%s' .target %d .property %d",
g_wnd, lxev->requestor, lxev->owner, lxev->selection, g_wnd, lxev->requestor, lxev->owner, lxev->selection,
XGetAtomName(g_display, lxev->selection), XGetAtomName(g_display, lxev->selection),
lxev->target, lxev->property); lxev->target, lxev->property));
if (lxev->property == None) if (lxev->property == None)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: lxev->property is None"); LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: "
"lxev->property is None"));
} }
else if (lxev->target == g_targets_atom) else if (lxev->target == g_targets_atom)
{ {
/* requestor is asking what the selection can be converted to */ /* requestor is asking what the selection can be converted to */
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: g_targets_atom"); LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: "
"g_targets_atom"));
ui32[0] = g_targets_atom; ui32[0] = g_targets_atom;
ui32[1] = g_timestamp_atom; ui32[1] = g_timestamp_atom;
ui32[2] = g_multiple_atom; ui32[2] = g_multiple_atom;
@ -974,29 +988,33 @@ clipboard_event_selection_request(XEvent* xevent)
else if (lxev->target == g_timestamp_atom) else if (lxev->target == g_timestamp_atom)
{ {
/* requestor is asking the time I got the selection */ /* requestor is asking the time I got the selection */
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: g_timestamp_atom"); LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: "
"g_timestamp_atom"));
ui32[0] = g_selection_time; ui32[0] = g_selection_time;
return clipboard_provide_selection(lxev, XA_INTEGER, 32, (char*)ui32, 1); return clipboard_provide_selection(lxev, XA_INTEGER, 32, (char*)ui32, 1);
} }
else if (lxev->target == g_multiple_atom) else if (lxev->target == g_multiple_atom)
{ {
/* target, property pairs */ /* target, property pairs */
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: g_multiple_atom"); LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: "
"g_multiple_atom"));
#if 0
if (clipboard_get_window_property(xev.xselection.requestor, if (clipboard_get_window_property(xev.xselection.requestor,
xev.xselection.property, xev.xselection.property,
&type, &fmt, &n_items, &xdata, &type, &fmt, &n_items, &xdata,
&xdata_size) == 0) &xdata_size) == 0)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: g_multiple_atom " LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: g_multiple_atom "
"n_items %d", n_items); "n_items %d", n_items));
/* todo */ /* todo */
g_free(xdata); g_free(xdata);
} }
#endif
} }
else if ((lxev->target == XA_STRING) || (lxev->target == g_utf8_atom)) else if ((lxev->target == XA_STRING) || (lxev->target == g_utf8_atom))
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_request: %s", LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_request: %s",
XGetAtomName(g_display, lxev->target)); XGetAtomName(g_display, lxev->target)));
if (g_data_in_up_to_date) if (g_data_in_up_to_date)
{ {
return clipboard_provide_selection(lxev, lxev->target, 8, return clipboard_provide_selection(lxev, lxev->target, 8,
@ -1004,7 +1022,8 @@ clipboard_event_selection_request(XEvent* xevent)
} }
if (g_selection_request_event_count > 10) if (g_selection_request_event_count > 10)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_request: error, too many requests"); LOGM((LOG_LEVEL_ERROR, "clipboard_event_selection_request: error, "
"too many requests"));
} }
else else
{ {
@ -1022,8 +1041,8 @@ clipboard_event_selection_request(XEvent* xevent)
} }
else else
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_event_selection_request: unknown " LOGM((LOG_LEVEL_ERROR,"clipboard_event_selection_request: unknown "
"target %s", XGetAtomName(g_display, lxev->target)); "target %s", XGetAtomName(g_display, lxev->target)));
} }
clipboard_refuse_selection(lxev); clipboard_refuse_selection(lxev);
return 0; return 0;
@ -1044,7 +1063,7 @@ clipboard_event_selection_request(XEvent* xevent)
static int APP_CC static int APP_CC
clipboard_event_selection_clear(XEvent* xevent) clipboard_event_selection_clear(XEvent* xevent)
{ {
log_message(LOG_LEVEL_DEBUG,"clipboard_event_selection_clear:"); LOGM((LOG_LEVEL_DEBUG, "clipboard_event_selection_clear:"));
return 0; return 0;
} }
@ -1105,7 +1124,7 @@ clipboard_check_wait_objs(void)
if (XPending(g_display) < 1) if (XPending(g_display) < 1)
{ {
/* something is wrong, should not get here */ /* something is wrong, should not get here */
log_message(LOG_LEVEL_ERROR,"clipboard_check_wait_objs: sck closed"); LOGM((LOG_LEVEL_ERROR, "clipboard_check_wait_objs: sck closed"));
return 0; return 0;
} }
if (g_waiting_for_data_response) if (g_waiting_for_data_response)
@ -1114,8 +1133,8 @@ clipboard_check_wait_objs(void)
g_waiting_for_data_response_time; g_waiting_for_data_response_time;
if (time_diff > 1000) if (time_diff > 1000)
{ {
log_message(LOG_LEVEL_ERROR,"clipboard_check_wait_objs: warning, waiting for " LOGM((LOG_LEVEL_ERROR, "clipboard_check_wait_objs: warning, "
"data response too long"); "waiting for data response too long"));
} }
} }
while (XPending(g_display) > 0) while (XPending(g_display) > 0)
@ -1144,8 +1163,8 @@ clipboard_check_wait_objs(void)
clipboard_event_selection_owner_notify(&xevent); clipboard_event_selection_owner_notify(&xevent);
break; break;
} }
log_message(LOG_LEVEL_ERROR,"clipboard_check_wait_objs unknown type %d", LOGM((LOG_LEVEL_ERROR, "clipboard_check_wait_objs unknown type %d",
xevent.type); xevent.type));
break; break;
} }
} }

@ -17,6 +17,7 @@
*/ */
#include "sound.h" #include "sound.h"
#include "thread_calls.h"
extern int g_rdpsnd_chan_id; /* in chansrv.c */ extern int g_rdpsnd_chan_id; /* in chansrv.c */
extern int g_display_num; /* in chansrv.c */ extern int g_display_num; /* in chansrv.c */
@ -26,6 +27,11 @@ static struct trans *g_audio_c_trans = 0; // connection
static int g_training_sent_time = 0; static int g_training_sent_time = 0;
static int g_cBlockNo = 0; static int g_cBlockNo = 0;
#if defined(XRDP_SIMPLESOUND)
static void* DEFAULT_CC
read_raw_audio_data(void* arg);
#endif
/*****************************************************************************/ /*****************************************************************************/
static int APP_CC static int APP_CC
sound_send_server_formats(void) sound_send_server_formats(void)
@ -164,7 +170,7 @@ sound_send_wave_data(char* data, int data_bytes)
/* part one of 2 PDU wave info */ /* part one of 2 PDU wave info */
LOG(3, ("sound_send_wave_data: sending %d bytes", data_bytes)); LOG(10, ("sound_send_wave_data: sending %d bytes", data_bytes));
make_stream(s); make_stream(s);
init_stream(s, data_bytes); init_stream(s, data_bytes);
@ -177,7 +183,7 @@ sound_send_wave_data(char* data, int data_bytes)
g_cBlockNo++; g_cBlockNo++;
out_uint8(s, g_cBlockNo); out_uint8(s, g_cBlockNo);
LOG(3, ("sound_send_wave_data: sending time %d, g_cBlockNo %d", LOG(10, ("sound_send_wave_data: sending time %d, g_cBlockNo %d",
time & 0xffff, g_cBlockNo & 0xff)); time & 0xffff, g_cBlockNo & 0xff));
out_uint8s(s, 3); out_uint8s(s, 3);
@ -227,7 +233,7 @@ sound_process_wave_confirm(struct stream* s, int size)
in_uint16_le(s, wTimeStamp); in_uint16_le(s, wTimeStamp);
in_uint8(s, cConfirmedBlockNo); in_uint8(s, cConfirmedBlockNo);
LOG(3, ("sound_process_wave_confirm: wTimeStamp %d, cConfirmedBlockNo %d", LOG(10, ("sound_process_wave_confirm: wTimeStamp %d, cConfirmedBlockNo %d",
wTimeStamp, cConfirmedBlockNo)); wTimeStamp, cConfirmedBlockNo));
return 0; return 0;
@ -330,9 +336,8 @@ sound_init(void)
#if defined(XRDP_SIMPLESOUND) #if defined(XRDP_SIMPLESOUND)
// start thread to read raw audio data from pulseaudio device /* start thread to read raw audio data from pulseaudio device */
pthread_create(&thread, 0, read_raw_audio_data, NULL); tc_thread_create(read_raw_audio_data, 0);
pthread_detach(thread);
#endif #endif
@ -434,113 +439,114 @@ sound_check_wait_objs(void)
#if defined(XRDP_SIMPLESOUND) #if defined(XRDP_SIMPLESOUND)
static int DEFAULT_CC
sttrans_data_in(struct trans* self)
{
LOG(0, ("sttrans_data_in:\n"));
return 0;
}
/** /**
* read raw audio data from pulseaudio device and write it * read raw audio data from pulseaudio device and write it
* to a unix domain socket on which trans server is listening * to a unix domain socket on which trans server is listening
*/ */
static void * static void* DEFAULT_CC
read_raw_audio_data(void* arg) read_raw_audio_data(void* arg)
{ {
struct sockaddr_un serv_addr;
pa_sample_spec samp_spec; pa_sample_spec samp_spec;
pa_simple* simple = NULL; pa_simple* simple = NULL;
uint32_t bytes_read; uint32_t bytes_read;
uint8_t audio_buf[AUDIO_BUF_SIZE + 8];
char* cptr; char* cptr;
int i; int i;
int error; int error;
int skt_fd; struct trans* strans;
char path[256];
struct stream* outs;
// create client socket strans = trans_create(TRANS_MODE_UNIX, 8192, 8192);
if ((skt_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) if (strans == 0)
{ {
LOG(0, ("read_raw_audio_data: error creating unix domain socket\n")); LOG(0, ("read_raw_audio_data: trans_create failed\n"));
return NULL; return 0;
} }
strans->trans_data_in = sttrans_data_in;
// setup server address and bind to it g_snprintf(path, 255, "/tmp/xrdp_chansrv_audio_socket_%d", g_display_num);
memset(&serv_addr, 0, sizeof(struct sockaddr_un)); if (trans_connect(strans, "", path, 100) != 0)
serv_addr.sun_family = AF_UNIX;
g_snprintf(serv_addr.sun_path, 255, "/tmp/xrdp_chansrv_audio_socket_%d", g_display_num);
if (connect(skt_fd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr_un)) < 0)
{ {
LOG(0, ("read_raw_audio_data: error connecting to server\n")); LOG(0, ("read_raw_audio_data: trans_connect failed\n"));
close(skt_fd); trans_delete(strans);
return NULL; return 0;
} }
// setup audio format /* setup audio format */
samp_spec.format = PA_SAMPLE_S16LE; samp_spec.format = PA_SAMPLE_S16LE;
samp_spec.rate = 44100; samp_spec.rate = 44100;
samp_spec.channels = 2; samp_spec.channels = 2;
// if we are root, then for first 8 seconds connection to pulseaudo server /* if we are root, then for first 8 seconds connection to pulseaudo server
// fails; if we are non-root, then connection succeeds on first attempt; fails; if we are non-root, then connection succeeds on first attempt;
// for now we have changed code to be non-root, but this may change in the for now we have changed code to be non-root, but this may change in the
// future - so pretend we are root and try connecting to pulseaudio server future - so pretend we are root and try connecting to pulseaudio server
// for upto one minute for upto one minute */
for (i = 0; i < 60; i++) for (i = 0; i < 60; i++)
{ {
simple = pa_simple_new(NULL, "xrdp", PA_STREAM_RECORD, NULL, simple = pa_simple_new(NULL, "xrdp", PA_STREAM_RECORD, NULL,
"record", &samp_spec, NULL, NULL, &error); "record", &samp_spec, NULL, NULL, &error);
if (simple) if (simple)
{ {
// connected to pulseaudio server /* connected to pulseaudio server */
LOG(0, ("read_raw_audio_data: connected to pulseaudio server\n")); LOG(0, ("read_raw_audio_data: connected to pulseaudio server\n"));
break; break;
} }
LOG(0, ("read_raw_audio_data: ERROR creating PulseAudio async interface\n")); LOG(0, ("read_raw_audio_data: ERROR creating PulseAudio async interface\n"));
LOG(0, ("read_raw_audio_data: %s\n", pa_strerror(error))); LOG(0, ("read_raw_audio_data: %s\n", pa_strerror(error)));
sleep(1); g_sleep(1000);
} }
if (i == 60) if (i == 60)
{ {
// failed to connect to audio server /* failed to connect to audio server */
close(skt_fd); trans_delete(strans);
return NULL; return NULL;
} }
// insert header just once /* insert header just once */
cptr = audio_buf; outs = trans_get_out_s(strans, 8192);
ins_uint32_le(cptr, 0); out_uint32_le(outs, 0);
ins_uint32_le(cptr, AUDIO_BUF_SIZE + 8); out_uint32_le(outs, AUDIO_BUF_SIZE + 8);
cptr = outs->p;
out_uint8s(outs, AUDIO_BUF_SIZE);
s_mark_end(outs);
while (1) while (1)
{ {
// read a block of raw audio data... /* read a block of raw audio data... */
if ((bytes_read = pa_simple_read(simple, cptr, AUDIO_BUF_SIZE, &error)) < 0) g_memset(cptr, 0, 4);
bytes_read = pa_simple_read(simple, cptr, AUDIO_BUF_SIZE, &error);
if (bytes_read < 0)
{ {
LOG(0, ("read_raw_audio_data: ERROR reading from pulseaudio stream\n")); LOG(0, ("read_raw_audio_data: ERROR reading from pulseaudio stream\n"));
LOG(0, ("read_raw_audio_data: %s\n", pa_strerror(error))); LOG(0, ("read_raw_audio_data: %s\n", pa_strerror(error)));
break; break;
} }
/* bug workaround:
// bug workaround: even when there is no audio data, pulseaudio is returning without
// even when there is no audio data, pulseaudio is returning without errors but the data itself is zero; we use this zero data to
// errors but the data itself is zero; we use this zero data to determine that there is no audio data present */
// determine that there is no audio data present
if (*cptr == 0 && *(cptr + 1) == 0 && *(cptr + 2) == 0 && *(cptr + 3) == 0) if (*cptr == 0 && *(cptr + 1) == 0 && *(cptr + 2) == 0 && *(cptr + 3) == 0)
{ {
usleep(10000); g_sleep(10);
continue; continue;
} }
if (trans_force_write_s(strans, outs) != 0)
// ... and write it to a unix domain socket
if (write(skt_fd, audio_buf, AUDIO_BUF_SIZE + 8) < 0)
{ {
LOG(0, ("read_raw_audio_data: ERROR writing audio data to server\n")); LOG(0, ("read_raw_audio_data: ERROR writing audio data to server\n"));
break; break;
} }
} }
done:
pa_simple_free(simple); pa_simple_free(simple);
close(skt_fd); trans_delete(strans);
return NULL; return NULL;
} }

@ -19,9 +19,6 @@
#ifndef _SOUND_H_ #ifndef _SOUND_H_
#define _SOUND_H_ #define _SOUND_H_
#include <sys/socket.h>
#include <sys/un.h>
#if defined(XRDP_SIMPLESOUND) #if defined(XRDP_SIMPLESOUND)
#include <pulse/simple.h> #include <pulse/simple.h>
#include <pulse/error.h> #include <pulse/error.h>
@ -43,29 +40,6 @@
#define print_got_here() #define print_got_here()
#endif #endif
/**
* insert a uint32_t value into specified byte array
*
* @param p pointer to byte array
* @param v value to insert into byte array
*/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define ins_uint32_le(p, v) \
{ \
*p++ = (unsigned char) v; \
*p++ = (unsigned char) (v >> 8); \
*p++ = (unsigned char) (v >> 16); \
*p++ = (unsigned char) (v >> 24); \
}
#else
#define ins_uint32_le(p, v) \
{ \
*((uint32_t *) p) = v; \
p += 4; \
}
#endif
#define AUDIO_BUF_SIZE 2048 #define AUDIO_BUF_SIZE 2048
#define SNDC_CLOSE 0x01 #define SNDC_CLOSE 0x01
@ -93,9 +67,4 @@ int APP_CC
sound_data_in(struct stream* s, int chan_id, int chan_flags, sound_data_in(struct stream* s, int chan_id, int chan_flags,
int length, int total_length); int length, int total_length);
#if defined(XRDP_SIMPLESOUND)
static void*
read_raw_audio_data(void* arg);
#endif
#endif #endif

Loading…
Cancel
Save