Skip to content

Instantly share code, notes, and snippets.

@pimpreneil
Last active August 23, 2016 10:10
Show Gist options
  • Save pimpreneil/e83bf7608651c3291bf50cb3dee37cf8 to your computer and use it in GitHub Desktop.
Save pimpreneil/e83bf7608651c3291bf50cb3dee37cf8 to your computer and use it in GitHub Desktop.
VLC path to XScreen as Loop
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vlc/vlc.h>
#include <signal.h>
#include <dirent.h>
#include <string.h>
#include <X11/Xlib.h>
#include <glib.h>
#define UNUSED(x) (void)(x)
static const char* vlcArguments[] = {
"--ignore-config",
"--no-media-library",
"--no-one-instance",
"--no-osd",
"--no-snapshot-preview",
"--no-stats",
"--no-audio",
"--playlist-tree",
"--no-video-title-show",
"--no-xlib"
};
libvlc_instance_t *instance = NULL;
libvlc_media_list_t *medialist = NULL;
libvlc_media_list_player_t *listplayer = NULL;
Display *dis = NULL;
int screen;
Window win;
static void wait_playing(libvlc_media_player_t *mp)
{
libvlc_state_t state;
do {
state = libvlc_media_player_get_state (mp);
usleep(1000);
} while(state != libvlc_Paused && state != libvlc_Stopped && state != libvlc_Ended && state != libvlc_Error);
}
void sig_handler(int signo) {
UNUSED(signo);
if(listplayer != NULL && instance != NULL && dis != NULL) {
libvlc_media_list_player_release(listplayer);
libvlc_release(instance);
XDestroyWindow(dis,win);
XCloseDisplay(dis);
printf("%s\n", "relased!");
}
exit(0);
}
int main(int argc, char *argv[]) {
GError *error = NULL;
/* Handle app break */
signal(SIGINT, sig_handler);
/* Handle the app options */
GOptionContext *g_option_context;
char **args = NULL;
gint n_args = 0;
GOptionEntry options_table[] = {
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, "[path xscreen]. e.g. /home/images :0.0"},
{NULL}
};
g_option_context = g_option_context_new ("- VLC path to XScreen as Loop");
g_option_context_add_main_entries(g_option_context, options_table, NULL);
g_option_context_set_help_enabled(g_option_context, TRUE);
if (!g_option_context_parse(g_option_context, &argc, &argv, &error)) {
g_option_context_free(g_option_context);
g_printerr("%s\n", error->message);
exit(1);
}
g_option_context_free(g_option_context);
if (args) {
while (args[n_args] != NULL) {
n_args++;
}
}
if (n_args < 2) {
g_printerr("Usage: vlc-loop-screen [path xscreen]. e.g. /home/images :0.0\n");
exit(1);
}
/* Create a XWindow to allow display the VLC window on fullscreen on a given XDisplay */
unsigned long black,white;
XInitThreads();
dis = XOpenDisplay(args[1]);
screen = DefaultScreen(dis);
black = BlackPixel(dis,screen);
white = WhitePixel(dis, screen);
win = XCreateSimpleWindow(dis, DefaultRootWindow(dis), 0, 0, 300, 200, 5, white, black);
Atom wm_state = XInternAtom(dis, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(dis, "_NET_WM_STATE_FULLSCREEN", False);
XEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = win;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1;
xev.xclient.data.l[1] = fullscreen;
xev.xclient.data.l[2] = 0;
XMapWindow(dis, win);
XSendEvent(dis, DefaultRootWindow(dis), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
XSync(dis, false);
XUnlockDisplay(dis);
/* Initialize the VLC instance and media list */
char* path = args[0];
DIR *d = opendir(path);
struct dirent *dir;
char* curfile;
instance = libvlc_new(sizeof(vlcArguments) / sizeof(vlcArguments[0]), vlcArguments);
libvlc_media_list_t *medialist = libvlc_media_list_new(instance);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
asprintf(&curfile, "%s/%s", path, dir->d_name);
libvlc_media_t *media = libvlc_media_new_path(instance, curfile);
libvlc_media_list_add_media(medialist, media);
libvlc_media_release(media);
}
}
closedir(d);
}
/* Go ahead and play the media list in loop mode */
listplayer = libvlc_media_list_player_new(instance);
libvlc_media_player_t *player = libvlc_media_player_new(instance);
libvlc_media_list_player_set_media_player(listplayer, player);
libvlc_media_player_set_xwindow (player, win);
libvlc_media_list_player_set_media_list(listplayer, medialist);
libvlc_media_list_player_set_playback_mode(listplayer, libvlc_playback_mode_loop);
libvlc_media_list_player_play(listplayer);
while(1) {
usleep(1000);
}
return 0;
}
all: vlc-loop-screen
vlc-loop-screen:
gcc -o vlc-loop-screen vlc-loop-screen.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0 -lX11 -lvlc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment