tv-service  0.1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tv_service_live_overlay_tune_test.c

This is an example of how to use the TV Service's tune up/down APIs.

Author
Jae-Il Jung ji013.nosp@m.0.ju.nosp@m.ng@sa.nosp@m.msun.nosp@m.g.com
/* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* @author Jae-Il Jung <ji0130.jung@samsung.com>
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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;
static TvServiceLive live_handle = 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);
TvServiceAntenna antenna_type;
gint res;
res = tv_service_live_get_antenna_type (live_handle, &antenna_type);
if (res < 0) {
printf ("failed to get antenna type");
return FALSE;
}
res = tv_service_live_tune_down (live_handle,
if (res < 0) {
printf ("failed to tune down");
return FALSE;
}
res = tv_service_live_get_service_id (live_handle, &service_id);
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);
TvServiceAntenna antenna_type;
gint res;
res = tv_service_live_get_antenna_type (live_handle, &antenna_type);
if (res != 0) {
printf ("failed to get antenna type");
return FALSE;
}
res = tv_service_live_tune_up (live_handle,
if (res != 0) {
printf ("failed to tune up");
return FALSE;
}
res = tv_service_live_get_service_id (live_handle, &service_id);
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)
tv_service_live_destroy (live_handle);
g_main_loop_quit (loop);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
exit (0);
return;
}
static void
app_callback (TvServiceLiveEvent event, gpointer user_data, const gpointer data)
{
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;
gint rc = TVS_ERROR_OK;
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);
rc = tv_service_live_create (&live_handle);
if (rc != TVS_ERROR_OK)
{
return rc;
}
signal (SIGINT,signal_handle);
rc = tv_service_live_set_window_overlay (live_handle, (gpointer)window);
if (rc != TVS_ERROR_OK)
{
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);
}
tv_service_live_register_callback (live_handle, app_callback, NULL);
tv_service_live_tune (live_handle, service_id);
g_timeout_add_seconds (total_time + 30, exit_loop, NULL);
g_main_loop_run (loop);
if (live_handle)
tv_service_live_destroy (live_handle);
XFreeGC (dpy, gc);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
return 0;
}