Last active
April 13, 2019 12:54
-
-
Save bathtime/9302e7396e1fcc4d3adede59daca7861 to your computer and use it in GitHub Desktop.
Launch Youtube from Primary/Clipboard Buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Compile with: | |
gcc -O3 -Wall youtube.c -o youtube -lX11 | |
*/ | |
#include<X11/Xlib.h> | |
#include<unistd.h> | |
#include<stdio.h> | |
void fast_memcpy(char *str, const char *str2) | |
{ | |
int i = 0; | |
while(str2[i] != '\0') | |
{ | |
str[i] = str2[i]; | |
++i; | |
} | |
str[i] = '\0'; | |
} | |
inline size_t fast_strlen(char *str) | |
{ | |
int i = 0; | |
while(str[i] != '\0') | |
{ | |
// Buffer could be huge, so don't count more than we have to | |
if (i > 43) | |
return 0; | |
++i; | |
} | |
// Don't count returns | |
if (str[i - 1] == '\n' || str[i - 1] == '\r') | |
--i; | |
return i; | |
} | |
int fast_memcmp(char *str, const char *str2, int length) | |
{ | |
for(;--length > -1; ) | |
if(str[length] != str2[length]) | |
return 1; | |
return 0; | |
} | |
unsigned int fast_memcmpcpy(char *str, const char *str2) | |
{ | |
/* Compare strings and only copy if they differ */ | |
int i = 0; | |
// Compare | |
while(str2[i] != '\0') | |
{ | |
// A char doesn't match, so copy whole string to compare | |
if (str[i] != str2[i]) | |
{ | |
fast_memcpy(str, str2); | |
return 1; | |
} | |
++i; | |
} | |
// If we made it here, there is a match, so no need to copy | |
return 0; | |
} | |
// Function to capture clipboard buffers | |
char * PrintSelection(Display *display, Window window, const char *bufname, const char *fmtname) | |
{ | |
char *result; | |
unsigned long ressize, restail; | |
int resbits; | |
Atom bufid = XInternAtom(display, bufname, False), | |
fmtid = XInternAtom(display, fmtname, False), | |
propid = XInternAtom(display, "XSEL_DATA", False); | |
XEvent event; | |
XConvertSelection(display, bufid, fmtid, propid, window, CurrentTime); | |
do | |
XNextEvent(display, &event); | |
while (event.type != SelectionNotify || event.xselection.selection != bufid); | |
if (event.xselection.property) | |
{ | |
XGetWindowProperty(display, window, propid, 0, 536870911, False, AnyPropertyType, &fmtid, &resbits, &ressize, &restail, (unsigned char**)&result); | |
int resultLen = fast_strlen(result); | |
if (resultLen < 27 || resultLen > 43) | |
return ""; | |
if (result[resultLen - 1] == '\n') | |
result[resultLen - 1] = '\0'; | |
else if (result[resultLen] == '\n') | |
result[resultLen] = '\0'; | |
return result; | |
} | |
return ""; | |
} | |
int main() { | |
// Buffer capture variables | |
Display *display = XOpenDisplay(NULL); | |
unsigned short color = BlackPixel(display, DefaultScreen(display)); | |
Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, color, color); | |
// STRING or UTF8_STRING | |
char clip[64]; // 0 = CLIPBOARD, 1=PRIMARY | |
fast_memcpy(clip, PrintSelection(display, window, "PRIMARY", "STRING")); | |
puts("https://www.youtube.com/watch?v=WeOuKguaBec"); | |
puts("https://www.youtube.com/watch?v=pGe3u5rLGQc"); | |
puts("https://youtu.be/xWNnq6vQfGg"); | |
do{ | |
if(fast_memcmpcpy(clip, PrintSelection(display, window, "PRIMARY", "STRING")) && ((!fast_memcmp(clip, "https://youtu.be/", 17) && fast_strlen(clip) == 28) || (!fast_memcmp(clip, "https://www.youtube.com/watch?v=", 31) && fast_strlen(clip) == 43))) | |
{ | |
int pid = fork(); | |
if (pid == 0) | |
execl("/usr/bin/vlc", "vlc", "--play-and-exit", clip, (char *) NULL); | |
printf("Playing \'%s\'...\n", clip); | |
} | |
sleep(1); | |
}while(1); | |
XDestroyWindow(display, window); | |
XCloseDisplay(display); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment