summaryrefslogtreecommitdiff
path: root/src/livebox.c
blob: 228780d74000cc9dab2fcda387dc79903abe6f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.tizenopensource.org/license
 *
 * 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 <stdio.h>
#include <errno.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strdup */
#include <libgen.h>

#include <dlog.h>
#include <livebox-service.h>
#include <provider.h>
#include <provider_buffer.h>

#include "debug.h"
#include "livebox.h"
#include "dlist.h"
#include "util.h"

#define EAPI __attribute__((visibility("default")))

#define FILE_SCHEMA	"file://"

/*!
 * \brief This function is defined by the data-provider-slave
 */
extern const char *livebox_find_pkgname(const char *filename);
extern int livebox_request_update_by_id(const char *uri);

struct block {
	unsigned int idx;

	char *type;
	char *part;
	char *data;
	char *option;
	char *id;
	char *file;
	char *target_id;
};

struct livebox_desc {
	FILE *fp;
	int for_pd;

	unsigned int last_idx;

	struct dlist *block_list;
};

struct livebox_buffer_data {
	int is_pd;
	int accelerated;
};

EAPI const int DONE = 0x00;
EAPI const int OUTPUT_UPDATED = 0x02;
EAPI const int USE_NET = 0x04;

EAPI const int NEED_TO_SCHEDULE = 0x01;
EAPI const int NEED_TO_CREATE = 0x01;
EAPI const int NEED_TO_DESTROY = 0x01;
EAPI const int NEED_TO_UPDATE = 0x01;

EAPI const int LB_SYS_EVENT_FONT_CHANGED = 0x01;
EAPI const int LB_SYS_EVENT_LANG_CHANGED = 0x02;
EAPI const int LB_SYS_EVENT_TIME_CHANGED = 0x04;
EAPI const int LB_SYS_EVENT_REGION_CHANGED = 0x08;
EAPI const int LB_SYS_EVENT_PAUSED = 0x0100;
EAPI const int LB_SYS_EVENT_RESUMED = 0x0200;

EAPI struct livebox_desc *livebox_desc_open(const char *filename, int for_pd)
{
	struct livebox_desc *handle;
	char *new_fname;

	handle = calloc(1, sizeof(*handle));
	if (!handle) {
		ErrPrint("Error: %s\n", strerror(errno));
		return NULL;
	}

	if (for_pd) {
		int len;
		len = strlen(filename) + strlen(".desc") + 1;
		new_fname = malloc(len);
		if (!new_fname) {
			ErrPrint("Error: %s\n", strerror(errno));
			free(handle);
			return NULL;
		}
		snprintf(new_fname, len, "%s.desc", filename);
	} else {
		new_fname = strdup(filename);
		if (!new_fname) {
			ErrPrint("Error: %s\n", strerror(errno));
			free(handle);
			return NULL;
		}
	}

	DbgPrint("Open a new file: %s\n", new_fname);
	handle->fp = fopen(new_fname, "w+t");
	free(new_fname);
	if (!handle->fp) {
		ErrPrint("Failed to open a file: %s\n", strerror(errno));
		free(handle);
		return NULL;
	}

	return handle;
}

EAPI int livebox_desc_close(struct livebox_desc *handle)
{
	struct dlist *l;
	struct dlist *n;
	struct block *block;

	if (!handle)
		return -EINVAL;

	DbgPrint("Close and flush\n");
	dlist_foreach_safe(handle->block_list, l, n, block) {
		handle->block_list = dlist_remove(handle->block_list, l);

		DbgPrint("{\n");
		fprintf(handle->fp, "{\n");
		if (block->type) {
			fprintf(handle->fp, "type=%s\n", block->type);
			DbgPrint("type=%s\n", block->type);
		}

		if (block->part) {
			fprintf(handle->fp, "part=%s\n", block->part);
			DbgPrint("part=%s\n", block->part);
		}

		if (block->data) {
			fprintf(handle->fp, "data=%s\n", block->data);
			DbgPrint("data=%s\n", block->data);
		}

		if (block->option) {
			fprintf(handle->fp, "option=%s\n", block->option);
			DbgPrint("option=%s\n", block->option);
		}

		if (block->id) {
			fprintf(handle->fp, "id=%s\n", block->id);
			DbgPrint("id=%s\n", block->id);
		}

		if (block->target_id) {
			fprintf(handle->fp, "target=%s\n", block->target_id);
			DbgPrint("target=%s\n", block->target_id);
		}

		fprintf(handle->fp, "}\n");
		DbgPrint("}\n");

		free(block->type);
		free(block->part);
		free(block->data);
		free(block->option);
		free(block->id);
		free(block->target_id);
		free(block);
	}

	fclose(handle->fp);
	free(handle);
	return 0;
}

EAPI int livebox_desc_set_category(struct livebox_desc *handle, const char *id, const char *category)
{
	struct block *block;

	if (!handle || !category)
		return -EINVAL;

	block = calloc(1, sizeof(*block));
	if (!block)
		return -ENOMEM;

	block->type = strdup(LB_DESC_TYPE_INFO);
	if (!block->type) {
		free(block);
		return -ENOMEM;
	}

	block->part = strdup("category");
	if (!block->part) {
		free(block->type);
		free(block);
		return -ENOMEM;
	}

	block->data = strdup(category);
	if (!block->data) {
		free(block->type);
		free(block->part);
		free(block);
		return -ENOMEM;
	}

	if (id) {
		block->id = strdup(id);
		if (!block->id) {
			free(block->data);
			free(block->type);
			free(block->part);
			free(block);
			return -ENOMEM;
		}
	}

	block->idx = handle->last_idx++;
	handle->block_list = dlist_append(handle->block_list, block);
	return block->idx;
}

EAPI int livebox_desc_set_size(struct livebox_desc *handle, const char *id, int w, int h)
{
	struct block *block;
	char buffer[BUFSIZ];

	if (!handle)
		return -EINVAL;

	block = calloc(1, sizeof(*block));
	if (!block)
		return -ENOMEM;

	block->type = strdup(LB_DESC_TYPE_INFO);
	if (!block->type) {
		free(block);
		return -ENOMEM;
	}

	block->part = strdup("size");
	if (!block->part) {
		free(block->type);
		free(block);
		return -ENOMEM;
	}

	if (id) {
		block->id = strdup(id);
		if (!block->id) {
			free(block->part);
			free(block->type);
			free(block);
			return -ENOMEM;
		}
	}

	snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
	block->data = strdup(buffer);
	if (!block->data) {
		free(block->part);
		free(block->type);
		free(block);
		return -ENOMEM;
	}

	block->idx = handle->last_idx++;
	handle->block_list = dlist_append(handle->block_list, block);
	return block->idx;
}

EAPI char *livebox_util_nl2br(const char *str)
{
	int len;
	register int i;
	char *ret;
	char *ptr;

	if (!str)
		return NULL;

	len = strlen(str);
	if (!len)
		return NULL;

	ret = malloc(len + 1);
	if (!ret)
		return NULL;

	ptr = ret;
	i = 0;
	while (*str) {
		switch (*str) {
		case '\n':
			if (len - i < 5) {
				char *tmp;
				len += len;

				tmp = realloc(ret, len + 1);
				if (!tmp) {
					free(ret);
					return NULL;
				}

				ret = tmp;
				ptr = tmp + i;
			}

			strcpy(ptr, "<br>");
			ptr += 4;
			i += 4;
			break;
		default:
			*ptr++ = *str;
			i++;
			break;
		}

		str++;
	}
	*ptr = '\0';

	return ret;
}

EAPI int livebox_desc_set_id(struct livebox_desc *handle, int idx, const char *id)
{
	struct dlist *l;
	struct block *block;

	dlist_foreach(handle->block_list, l, block) {
		if (block->idx == idx) {
			if (strcasecmp(block->type, LB_DESC_TYPE_SCRIPT)) {
				ErrPrint("Invalid block is used\n");
				return -EINVAL;
			}

			free(block->target_id);
			block->target_id = NULL;

			if (!id || !strlen(id))
				return 0;

			block->target_id = strdup(id);
			if (!block->target_id) {
				ErrPrint("Heap: %s\n", strerror(errno));
				return -ENOMEM;
			}

			return 0;
		}
	}

	return -ENOENT;
}

/*!
 * \return idx
 */
EAPI int livebox_desc_add_block(struct livebox_desc *handle, const char *id, const char *type, const char *part, const char *data, const char *option)
{
	struct block *block;

	if (!handle || !type)
		return -EINVAL;

	if (!part)
		part = "";

	if (!data)
		data = "";

	block = calloc(1, sizeof(*block));
	if (!block)
		return -ENOMEM;

	block->type = strdup(type);
	if (!block->type) {
		free(block);
		return -ENOMEM;
	}

	block->part = strdup(part);
	if (!block->part) {
		free(block->type);
		free(block);
		return -ENOMEM;
	}

	block->data = strdup(data);
	if (!block->data) {
		free(block->type);
		free(block->part);
		free(block);
		return -ENOMEM;
	}

	if (option) {
		block->option = strdup(option);
		if (!block->option) {
			free(block->data);
			free(block->type);
			free(block->part);
			free(block);
			return -ENOMEM;
		}
	}

	if (id) {
		block->id = strdup(id);
		if (!block->id) {
			free(block->option);
			free(block->data);
			free(block->type);
			free(block->part);
			free(block);
			return -ENOMEM;
		}
	}

	block->idx = handle->last_idx++;
	handle->block_list = dlist_append(handle->block_list, block);
	return block->idx;
}

EAPI int livebox_desc_del_block(struct livebox_desc *handle, int idx)
{
	struct dlist *l;
	struct block *block;

	dlist_foreach(handle->block_list, l, block) {
		if (block->idx == idx) {
			handle->block_list = dlist_remove(handle->block_list, l);
			free(block->type);
			free(block->part);
			free(block->data);
			free(block->option);
			free(block->id);
			free(block->target_id);
			free(block);
			return 0;
		}
	}

	return -ENOENT;
}

EAPI struct livebox_buffer *livebox_acquire_buffer(const char *filename, int is_pd, int width, int height, int (*handler)(struct livebox_buffer *, enum buffer_event, double, double, double, void *), void *data)
{
	struct livebox_buffer_data *user_data;
	const char *pkgname;
	struct livebox_buffer *handle;
	char *uri;
	int uri_len;

	if (!filename || !width || !height) {
		ErrPrint("Invalid argument: %p(%dx%d)\n", filename, width, height);
		return NULL;
	}

	user_data = calloc(1, sizeof(*user_data));
	if (!user_data) {
		ErrPrint("Heap: %s\n", strerror(errno));
		return NULL;
	}

	user_data->is_pd = is_pd;

	uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
	uri = malloc(uri_len);
	if (!uri) {
		ErrPrint("Heap: %s\n", strerror(errno));
		free(user_data);
		return NULL;
	}

	snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
	pkgname = livebox_find_pkgname(uri);
	if (!pkgname) {
		ErrPrint("Invalid Request\n");
		free(user_data);
		free(uri);
		return NULL;
	}

	handle = provider_buffer_acquire((!!is_pd) ? TYPE_PD : TYPE_LB, pkgname, uri, width, height, sizeof(int), handler, data);
	DbgPrint("Acquire buffer for PD(%s), %s, %p\n", pkgname, uri, handle);
	free(uri);

	(void)provider_buffer_set_user_data(handle, user_data);
	return handle;
}

EAPI int livebox_request_update(const char *filename)
{
	int uri_len;
	char *uri;
	int ret;

	if (!filename) {
		ErrPrint("Invalid argument\n");
		return -EINVAL;
	}

	uri_len = strlen(filename) + strlen(FILE_SCHEMA) + 1;
	uri = malloc(uri_len);
	if (!uri) {
		ErrPrint("Heap: %s\n", strerror(errno));
		return -ENOMEM;
	}

	snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
	ret = livebox_request_update_by_id(uri);
	free(uri);
	return ret;
}

EAPI unsigned long livebox_pixmap_id(struct livebox_buffer *handle)
{
	return provider_buffer_pixmap_id(handle);
}

EAPI int livebox_release_buffer(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;

	if (!handle)
		return -EINVAL;

	user_data = provider_buffer_user_data(handle);
	if (user_data) {
		free(user_data);
		provider_buffer_set_user_data(handle, NULL);
	}

	DbgPrint("Release buffer\n");
	return provider_buffer_release(handle);
}

EAPI void *livebox_ref_buffer(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;
	void *data;
	int w, h, size;
	int ret;

	if (!handle)
		return NULL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data)
		return NULL;

	if (user_data->accelerated) {
		DbgPrint("H/W accelerated buffer is allocated\n");
		return NULL;
	}

	ret = provider_buffer_get_size(handle, &w, &h, &size);

	data = provider_buffer_ref(handle);
	if (data && !ret && w > 0 && h > 0 && size > 0) {
		memset(data, 0, w * h * size);
		provider_buffer_sync(handle);
	}

	DbgPrint("Ref buffer %ds%d(%d)\n", w, h, size);
	return data;
}

EAPI int livebox_unref_buffer(void *buffer)
{
	if (!buffer)
		return -EINVAL;

	DbgPrint("Unref buffer\n");
	return provider_buffer_unref(buffer);
}

EAPI int livebox_sync_buffer(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;
	const char *pkgname;
	const char *id;

	if (!handle)
		return -EINVAL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data) {
		ErrPrint("Invalid buffer\n");
		return -EINVAL;
	}

	if (user_data->accelerated) {
		DbgPrint("H/W Buffer allocated. skip the sync buffer\n");
		return 0;
	}

	pkgname = provider_buffer_pkgname(handle);
	if (!pkgname) {
		ErrPrint("Invalid buffer handler\n");
		return -EINVAL;
	}

	id = provider_buffer_id(handle);
	if (!id) {
		ErrPrint("Invalid buffer handler\n");
		return -EINVAL;
	}

	DbgPrint("Sync buffer\n");
	provider_buffer_sync(handle);

	if (user_data->is_pd) {
		if (provider_send_desc_updated(pkgname, id, NULL) < 0)
			ErrPrint("Failed to send PD updated (%s)\n", id);
	} else {
		int w;
		int h;
		int pixel_size;

		if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
			ErrPrint("Failed to get size (%s)\n", id);

		if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
			ErrPrint("Failed to send updated (%s)\n", id);
	}
	return 0;
}

EAPI int livebox_support_hw_buffer(struct livebox_buffer *handle)
{
	if (!handle)
		return -EINVAL;

	return provider_buffer_pixmap_is_support_hw(handle);
}

EAPI int livebox_create_hw_buffer(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;
	int ret;

	if (!handle)
		return -EINVAL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data)
		return -EINVAL;

	if (user_data->accelerated)
		return -EALREADY;

	ret = provider_buffer_pixmap_create_hw(handle);
	user_data->accelerated = (ret == 0);
	return ret;
}

EAPI int livebox_destroy_hw_buffer(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;
	if (!handle)
		return -EINVAL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data || !user_data->accelerated)
		return -EINVAL;

	user_data->accelerated = 0;

	return provider_buffer_pixmap_destroy_hw(handle);
}

EAPI void *livebox_buffer_hw_buffer(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;

	if (!handle)
		return NULL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data || !user_data->accelerated)
		return -EINVAL;

	return provider_buffer_pixmap_hw_addr(handle);
}

EAPI int livebox_buffer_pre_render(struct livebox_buffer *handle)
{
	struct livebox_buffer_data *user_data;

	if (!handle)
		return -EINVAL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data)
		return -EINVAL;

	if (!user_data->accelerated)
		return 0;

	/*!
	 * \note
	 * Do preprocessing for accessing the H/W render buffer
	 */
	return provider_buffer_pre_render(handle);
}

EAPI int livebox_buffer_post_render(struct livebox_buffer *handle)
{
	int ret;
	const char *pkgname;
	const char *id;
	struct livebox_buffer_data *user_data;

	if (!handle)
		return -EINVAL;

	user_data = provider_buffer_user_data(handle);
	if (!user_data)
		return -EINVAL;

	if (!user_data->accelerated)
		return 0;

	pkgname = provider_buffer_pkgname(handle);
	if (!pkgname) {
		ErrPrint("Invalid buffer handle\n");
		return -EINVAL;
	}

	id = provider_buffer_id(handle);
	if (!id) {
		ErrPrint("Invalid buffer handler\n");
		return -EINVAL;
	}

	ret = provider_buffer_post_render(handle);
	if (ret < 0) {
		ErrPrint("Failed to post render processing\n");
		return ret;
	}

	if (user_data->is_pd == 1) {
		if (provider_send_desc_updated(pkgname, id, NULL) < 0)
			ErrPrint("Failed to send PD updated (%s)\n", id);
	} else {
		int w;
		int h;
		int pixel_size;

		if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
			ErrPrint("Failed to get size (%s)\n", id);

		if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
			ErrPrint("Failed to send updated (%s)\n", id);
	}

	return 0;
}

/* End of a file */