Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor WINDOWID & use xdotool automatically #106

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions string_builder.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "string_builder.h"

Expand Down Expand Up @@ -72,3 +73,27 @@ StringBuilder_write(StringBuilder *sb, const char *s)
StringBuilder_write_char(sb, s[i]);
}
}

void
StringBuilder_trim(StringBuilder *sb)
{
int start, end;
for (start = 0; start < sb->size; start++) {
if (!isspace(sb->s[start])) {
break;
}
}
for (end = sb->size-2; end > start; end--) {
if (!isspace(sb->s[end])) {
break;
}
}
int size = end - start + 1;
char *s = StringBuilder_malloc(size);
strncpy(s, sb->s + start, size);
s[size] = '\0';
free(sb->s);
sb->s = s;
sb->cap = size;
sb->size = size;
}
1 change: 1 addition & 0 deletions string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void StringBuilder_free(StringBuilder *sb);
char * StringBuilder_str(StringBuilder *sb);
void StringBuilder_write_char(StringBuilder *sb, char c);
void StringBuilder_write(StringBuilder *sb, const char *s);
void StringBuilder_trim(StringBuilder *sb);

#endif /* STRING_BUILDER_H */

19 changes: 3 additions & 16 deletions ttygif.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ take_snapshot_darwin(const char *img_path, Options o)
int
take_snapshot_linux(const char *img_path, Options o)
{
static char cmd [256];
static char cmd [1024];

// ensure text has been written before taking screenshot
usleep(50000);
Expand Down Expand Up @@ -317,23 +317,10 @@ main (int argc, char **argv)

#ifdef OS_DARWIN
options.img_ext = "png";
const char *terminal_app = getenv("TERM_PROGRAM");
if (terminal_app == NULL || !strlen(terminal_app)) {
fatalf("Error: TERM_PROGRAM environment variable was empty.");
}
if (strcmp(terminal_app, "Apple_Terminal") == 0) {
terminal_app = "Terminal.app";
}
int window_id = osx_get_window_id(terminal_app);
char window_id_buffer[256];
sprintf(window_id_buffer, "%d", window_id);
options.window_id = window_id_buffer;
options.window_id = osx_get_window_id();
#else
options.img_ext = "xwd";
options.window_id = getenv("WINDOWID");
if (options.window_id == NULL || !strlen(options.window_id)) {
fatalf("Error: WINDOWID environment variable was empty.");
}
options.window_id = linux_get_window_id();
#endif

if (options.debug) {
Expand Down
57 changes: 44 additions & 13 deletions utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "string_builder.h"

void fatalf(const char *format, ...)
Expand All @@ -12,28 +13,58 @@ void fatalf(const char *format, ...)
exit(EXIT_FAILURE);
}

int osx_get_window_id(const char *app_name)
int exec_command(StringBuilder *sb, const char *command)
{
char command[1024];
sprintf(command,
"osascript -so -e 'tell app \"%s\" to id of window 1' 2> /dev/null",
app_name);

FILE *fp = popen(command, "r");
if (fp == NULL) {
fatalf("Error: failed to run command: %s", command);
return -1;
}

int window_id;
if (fscanf(fp, "%d", &window_id) != 1) {
fatalf("Error: failed to parse window id: %s", command);
char buffer[1024];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
StringBuilder_write(sb, buffer);
}
return pclose(fp);
}

pclose(fp);
char * osx_get_window_id()
{
const char *term = getenv("TERM_PROGRAM");
if (term == NULL || !strlen(term)) {
fatalf("Error: TERM_PROGRAM environment variable was empty.");
}
if (strcmp(term, "Apple_Terminal") == 0) {
term = "Terminal.app";
}
char command[1024];
sprintf(command,
"osascript -so -e 'tell app \"%s\" to id of window 1' 2> /dev/null",
term);
StringBuilder *sb = StringBuilder_new();
int code = exec_command(sb, command);
if (code != 0) {
fatalf("failed to run command: %s", command);
}
StringBuilder_trim(sb);
return StringBuilder_str(sb);
}

return window_id;
char * linux_get_window_id()
{
StringBuilder *sb = StringBuilder_new();
const char *window_id = getenv("WINDOWID");
if (window_id != NULL && strlen(window_id)) {
StringBuilder_write(sb, window_id);
return StringBuilder_str(sb);
}
int code = exec_command(sb, "xdotool getwindowfocus");
if (code != 0) {
fatalf("Error: WINDOWID environment variable was empty.");
}
StringBuilder_trim(sb);
return StringBuilder_str(sb);
}


int exec_with_output(const char *command)
{
FILE *fp = popen(command, "r");
Expand Down
3 changes: 2 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#define _UTILS_H

void fatalf(const char *format, ...);
int osx_get_window_id(const char *app_name);
char * osx_get_window_id();
char * linux_get_window_id();
int exec_with_output(const char *command);

#endif /* _UTILS_H */