This is an example of how to use the TV Service's tune up/down APIs.
#include <time.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "tv_service_logger.h"
static GMainLoop *loop = NULL;
guintptr window;
Display *dpy = NULL;
guint dst_x = 0;
guint dst_y = 0;
guint dst_width = 1920;
guint dst_height = 1080;
gint service_id = 1;
void
print_usage (char * argv)
{
printf ("You should run this program after scanning\n");
printf ("%s {service_id} {tune_up_num} {tune_down_num} \n", argv);
printf ("i.e) %s 1 2 3\n", argv);
return;
}
gboolean
exit_loop (gpointer user_data)
{
g_main_loop_quit (loop);
return FALSE;
}
static XImage *
make_transparent_image (Display * dpy, int width, int height)
{
XImage *xim = NULL;
xim = XCreateImage (dpy, DefaultVisualOfScreen (DefaultScreenOfDisplay (dpy)),
24, ZPixmap, 0, NULL, width, height, 32, 0);
xim->data = (char *) calloc(1, xim->bytes_per_line * xim->height);
return xim;
}
gboolean
tune_down (gpointer user_data)
{
printf ("tune down: from %d", service_id);
gint res;
if (res < 0) {
printf ("failed to get antenna type");
return FALSE;
}
if (res < 0) {
printf ("failed to tune down");
return FALSE;
}
if (res < 0) {
printf ("failed to get the current service_id");
return FALSE;
}
printf (" to %d\n", service_id);
return FALSE;
}
gboolean
tune_up (gpointer user_data)
{
printf("tune up: from %d", service_id);
gint res;
if (res != 0) {
printf ("failed to get antenna type");
return FALSE;
}
if (res != 0) {
printf ("failed to tune up");
return FALSE;
}
if (res < 0) {
printf ("failed to get the current service_id");
return FALSE;
}
printf (" to %d\n", service_id);
return FALSE;
}
static Window
createWindow (Display * dpy)
{
Window window_id;
GC gc;
XEvent event;
XSizeHints my_hints = {0};
XSetWindowAttributes attr;
attr.override_redirect = TRUE;
window_id = XCreateWindow (dpy, RootWindow(dpy, 0), dst_x, dst_y, dst_width, dst_height, 0,
CopyFromParent, InputOutput, CopyFromParent, CWOverrideRedirect, &attr);
printf ("create_window - XCreateWindow, window id[%ld]", window_id);
my_hints.flags = PPosition | PSize;
my_hints.x = dst_x;
my_hints.y = dst_y;
my_hints.width = dst_width;
my_hints.height = dst_height;
XSetNormalHints (dpy, window_id, &my_hints);
XSelectInput (dpy, window_id, ExposureMask);
printf ("create_window - XSelectInput");
XMapWindow (dpy, window_id);
printf ("create_window - XMapWindow");
gc = XCreateGC (dpy, window_id, 0, 0);
printf ("create_window - XCreateGC");
XSetForeground (dpy, gc, 0xffffff);
printf ("create_window - XSetForeground");
XNextEvent (dpy, &event);
if (event.type == Expose) {
XFillRectangle (dpy, window_id, gc, dst_x, dst_y, dst_width, dst_height);
}
XFlush (dpy);
XSync (dpy, 0);
XFreeGC (dpy, gc);
printf ("create_window - XSync");
return window_id;
}
void
signal_handle (int signum)
{
printf ("In signal handle!\n");
if (live_handle)
g_main_loop_quit (loop);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
exit (0);
return;
}
static void
{
switch (event) {
{
gboolean *lock_status = (gboolean*) data;
printf ("tv_service_callback: get tuner locked callback:%d\n", *lock_status);
}
break;
printf ("event channel lock\n");
break;
printf ("event channel unlock\n");
break;
printf ("event auto destroy\n");
live_handle = NULL;
exit_loop (NULL);
break;
default:
printf ("get callback_type: %d\n", event);
}
return;
}
int
main (int argc, char ** argv)
{
gint i;
guint tune_up_num;
guint tune_down_num;
guint total_time = 5;
GC gc;
XImage *xim = NULL;
if (argc == 4) {
service_id = atoi (argv[1]);
tune_up_num = atoi (argv[2]);
tune_down_num = atoi (argv[3]);
} else {
print_usage (argv[0]);
return -1;
}
dpy = XOpenDisplay (NULL);
window = createWindow (dpy);
gc = XCreateGC (dpy, window, 0, 0);
xim = make_transparent_image (dpy, dst_width, dst_height);
XPutImage (dpy, window, gc, xim, 0, 0, dst_x, dst_y, dst_width, dst_height);
XSync (dpy, False);
loop = g_main_loop_new (NULL, FALSE);
{
return rc;
}
signal (SIGINT,signal_handle);
{
return rc;
}
for (i = 1; i <= tune_up_num; i++) {
total_time += 8 * i;
g_timeout_add_seconds (total_time, tune_up, live_handle);
}
for (i = 1; i <= tune_down_num; i++) {
total_time += 8 * i;
g_timeout_add_seconds (total_time, tune_down, live_handle);
}
g_timeout_add_seconds (total_time + 30, exit_loop, NULL);
g_main_loop_run (loop);
if (live_handle)
XFreeGC (dpy, gc);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
return 0;
}