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

This is an example for TV Service's dynamic resizing overlay window.

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 Liu Kun k2.liu@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;
gint first_dst_x = 0;
gint first_dst_y = 0;
gint first_dst_width = 320;
gint first_dst_height = 180;
static guint tr_width = 1920;
static guint tr_height = 1080;
GC gc;
XImage *xim = NULL;
static int size = 1;
static int position = 1;
void
print_usage (char * argv)
{
printf ("%s {frequency} {modulation_type} {apid} {vpid} {ppid}\n", argv);
printf ("i.e) SRC-NJ: %s 713000000 7 36 33 32\n", argv);
printf ("i.e) HQ: %s 473000000 7 20 17 16\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)
{
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;
}
static Window
createWindow (Display * dpy)
{
Window window_id;
GC gc;
XEvent event;
XSizeHints my_hints = {0};
XSetWindowAttributes attr;
gint first_dst_x = 0;
gint first_dst_y = 0;
gint first_dst_width = 1920;
gint first_dst_height = 1080;
attr.override_redirect = TRUE;
window_id = XCreateWindow (dpy, RootWindow(dpy, 0), first_dst_x, first_dst_y, first_dst_width, first_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 = first_dst_x;
my_hints.y = first_dst_y;
my_hints.width = first_dst_width;
my_hints.height = first_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, first_dst_x, first_dst_y, first_dst_width, first_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;
}
gboolean
resize_window (gpointer user_data)
{
if (size) {
first_dst_width += 16 * 2;
first_dst_height += 9 * 2;
if (((first_dst_x + first_dst_width) >= 1920) ||
((first_dst_y + first_dst_height) >= 1080)) {
size = 0;
position = g_random_int()%4;
}
} else {
first_dst_width -= 16*2;
first_dst_height -= 9*2;
if (first_dst_width <= 480 || first_dst_height <= 270) {
size = 1;
position = g_random_int() % 4;
}
}
switch (position) {
case 0:
first_dst_x += 16;
first_dst_y += 9;
break;
case 1:
first_dst_x += 16;
first_dst_y -= 9;
break;
case 2:
first_dst_x -= 16;
first_dst_y -= 9;
break;
case 3:
first_dst_x -= 16;
first_dst_y += 9;
break;
default :
break;
}
if (first_dst_x < 0)
first_dst_x = 0;
if (first_dst_y < 0)
first_dst_y = 0;
if (first_dst_x > 960)
first_dst_x = 960;
if (first_dst_y > 540)
first_dst_y = 540;
XMoveResizeWindow (dpy, window, first_dst_x, first_dst_y, first_dst_width, first_dst_height);
XPutImage (dpy, window, gc, xim, 0, 0, 0, 0, tr_width, tr_height);
XFlush (dpy);
XSync (dpy, 0);
g_usleep (1000 * 10);
return TRUE;
}
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 temp_freq = 0;
gint temp_mod = 0;
gint apid = 0;
gint vpid = 0;
gint ppid = 0;
gint rc = TVS_ERROR_OK;
if(argc == 6) {
temp_freq = atoi (argv[1]);
temp_mod = atoi (argv[2]);
apid = atoi (argv[3]);
vpid = atoi (argv[4]);
ppid = atoi (argv[5]);
} else {
print_usage (argv[0]);
return -1;
}
dpy = XOpenDisplay (NULL);
window = createWindow (dpy);
gc = XCreateGC (dpy, window, 0, 0);
xim = make_transparent_image (dpy, tr_width, tr_height);
XPutImage (dpy, window, gc, xim, 0, 0, 0, 0, tr_width, tr_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;
}
g_idle_add (resize_window, NULL);
tv_service_live_register_callback (live_handle, app_callback, NULL);
tv_service_live_tune_full (live_handle, temp_freq, temp_mod, apid, vpid, ppid);
g_timeout_add_seconds (200, exit_loop, NULL);
g_main_loop_run (loop);
if (live_handle)
tv_service_live_destroy (live_handle);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
return 0;
}