Skip to content

Commit 251b085

Browse files
committed
- Refactor most 'printf' invocations to use xdotool_output which has will
fflush after every write. - Add xdotool_debug for debug output when 'DEBUG' is present in env. git-svn-id: https://semicomplete.googlecode.com/svn/xdotool@3185 66067f73-fe4c-0410-82e9-b9c6d0c95a22
1 parent 79ff691 commit 251b085

12 files changed

+45
-16
lines changed

CHANGELIST

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Should fix http://code.google.com/p/semicomplete/issues/detail?id=37
1010
Reported by ryandesign.com
1111
- Add support for typing UTF-8 characters. Patch from Joseph Krahn.
12+
- Make all output call fflush to send data immediately (for pipes). Reported
13+
by Andreas Wagner on the mailing list.
1214
@ TODO: Add search --desktop
1315
@ TODO: Search shortcutting (break after N matches) (Requested by Anthony Thyssen)
1416
@ TODO: Get window position and size (Requested by Anthony Thyssen via mailing list)

cmd_behave.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ int cmd_behave(context_t *context) {
143143
}
144144

145145
if (ret != XDO_SUCCESS) {
146-
printf("Command failed.\n");
146+
xdotool_output(context, "Command failed.");
147147
}
148148
}
149149
return ret;

cmd_get_desktop.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int cmd_get_desktop(context_t *context) {
3030
consume_args(context, optind);
3131

3232
ret = xdo_get_current_desktop(context->xdo, &desktop);
33-
printf("%ld\n", desktop);
33+
xdotool_output(context, "%ld", desktop);
3434

3535
return ret;
3636
}

cmd_get_desktop_for_window.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int cmd_get_desktop_for_window(context_t *context) {
3939

4040
window_each(context, window_arg, {
4141
ret = xdo_get_desktop_for_window(context->xdo, window, &desktop);
42-
printf("%ld\n", desktop);
42+
xdotool_output(context, "%ld", desktop);
4343
}); /* window_each(...) */
4444
return ret;
4545
}

cmd_get_desktop_viewport.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ int cmd_get_desktop_viewport(context_t *context) {
3636
ret = xdo_get_desktop_viewport(context->xdo, &x, &y);
3737

3838
if (shell_output) {
39-
printf("X=%d\n", x);
40-
printf("Y=%d\n", y);
39+
xdotool_output(context, "X=%d", x);
40+
xdotool_output(context, "Y=%d", y);
4141
} else {
42-
printf("%d,%d\n", x, y);
42+
xdotool_output(context, "%d,%d", x, y);
4343
}
4444

4545
return ret;

cmd_get_num_desktops.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ int cmd_get_num_desktops(context_t *context) {
3636

3737
ret = xdo_get_number_of_desktops(context->xdo, &ndesktops);
3838

39-
printf("%ld\n", ndesktops);
39+
xdotool_output(context, "%ld", ndesktops);
4040
return ret;
4141
}

cmd_getmouselocation.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ int cmd_getmouselocation(context_t *context) {
4040
ret = xdo_mouselocation2(context->xdo, &x, &y, &screen_num, &window);
4141

4242
if (output_shell) {
43-
printf("X=%d\n", x);
44-
printf("Y=%d\n", y);
45-
printf("SCREEN=%d\n", screen_num);
46-
printf("WINDOW=%d\n", window);
43+
xdotool_output(context, "X=%d", x);
44+
xdotool_output(context, "Y=%d", y);
45+
xdotool_output(context, "SCREEN=%d", screen_num);
46+
xdotool_output(context, "WINDOW=%d", window);
4747
} else {
48-
printf("x:%d y:%d screen:%d window:%ld\n", x, y, screen_num, window);
48+
xdotool_output(context, "x:%d y:%d screen:%d window:%ld", x, y, screen_num, window);
4949
}
5050
return ret;
5151
}

cmd_getwindowname.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int cmd_getwindowname(context_t *context) {
4141

4242
window_each(context, window_arg, {
4343
xdo_get_window_name(context->xdo, window, &name, &name_len, &name_type);
44-
printf("%.*s\n", name_len, name);
44+
xdotool_output(context, "%.*s", name_len, name);
4545
XFree(name);
4646
}); /* window_each(...) */
4747
return EXIT_SUCCESS;

cmd_getwindowpid.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int cmd_getwindowpid(context_t *context) {
4444
fprintf(stderr, "window %ld has no pid associated with it.\n", window);
4545
return EXIT_FAILURE;
4646
} else {
47-
printf("%d\n", pid);
47+
xdotool_output(context, "%d", pid);
4848
}
4949
}); /* window_each(...) */
5050
return EXIT_SUCCESS;

xdo.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ int xdo_window_find_client(const xdo_t *xdo, Window window, Window *window_ret,
12101210
done = True; /* recursion should end us */
12111211
for (i = 0; i < nchildren && !done; i++) {
12121212
ret = xdo_window_find_client(xdo, children[i], &window, direction);
1213-
printf("findclient: %ld\n", window);
1213+
fprintf(stderr, "findclient: %ld\n", window);
12141214
if (ret == XDO_SUCCESS) {
12151215
*window_ret = window;
12161216
break;

xdo_cmd.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ extern int window_is_valid(context_t *context, const char *window_arg);
3131
extern int window_get_arg(context_t *context, int min_arg, int window_arg_pos,
3232
const char **window_arg);
3333

34+
extern void xdotool_debug(context_t *context, const char *format, ...);
35+
3436
#endif /* _XDO_CMD_H_ */

xdotool.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <strings.h>
2424
#include <errno.h>
2525
#include <ctype.h>
26+
#include <stdarg.h>
2627

2728
#include "xdo.h"
2829
#include "xdotool.h"
@@ -39,6 +40,8 @@ int window_get_arg(context_t *context, int min_arg, int window_arg_pos,
3940
const char **window_arg);
4041
int window_is_valid(context_t *context, const char *window_arg);
4142
int is_command(char* cmd);
43+
void xdotool_debug(context_t *context, const char *format, ...);
44+
void xdotool_output(context_t *context, const char *format, ...);
4245

4346
void consume_args(context_t *context, int argc) {
4447
if (argc > context->argc) {
@@ -514,6 +517,9 @@ int context_execute(context_t *context) {
514517
if (!strcasecmp(dispatch[i].name, cmd)) {
515518
cmd_found = 1;
516519
optind = 0;
520+
if (context->debug) {
521+
fprintf(stderr, "command: %s\n", cmd);
522+
}
517523
ret = dispatch[i].func(context);
518524
}
519525
}
@@ -543,10 +549,29 @@ int cmd_help(context_t *context) {
543549
}
544550

545551
int cmd_version(context_t *context) {
546-
printf("xdotool version %s\n", xdo_version());
552+
xdotool_output(context, "xdotool version %s", xdo_version());
547553
if (context != NULL) {
548554
consume_args(context, 1);
549555
}
550556

551557
return 0;
552558
}
559+
560+
void xdotool_debug(context_t *context, const char *format, ...) {
561+
va_list args;
562+
563+
va_start(args, format);
564+
if (context->debug) {
565+
vfprintf(stderr, format, args);
566+
fprintf(stderr, "\n");
567+
}
568+
} /* xdotool_debug */
569+
570+
void xdotool_output(context_t *context, const char *format, ...) {
571+
va_list args;
572+
573+
va_start(args, format);
574+
vfprintf(stdout, format, args);
575+
fprintf(stdout, "\n");
576+
fflush(stdout);
577+
} /* xdotool_output */

0 commit comments

Comments
 (0)