bed8711795a88901f0e1594babd0437951341df4
[platform/core/multimedia/libmedia-thumbnail.git] / server / thumb-server.c
1 /*
2  * media-thumbnail-server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-debug.h"
24 #include "media-thumb-ipc.h"
25 #include "media-thumb-util.h"
26 #include "thumb-server-internal.h"
27
28 #include <pthread.h>
29 #include <heynoti.h>
30 #include <vconf.h>
31 //#include <signal.h>
32 //#include <glib-unix.h>
33 #include <Ecore.h>
34 #include <Ecore_Evas.h>
35
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40
41 #define LOG_TAG "MEDIA_THUMBNAIL_SERVER"
42 #define POWEROFF_NOTI_NAME "power_off_start" /*poeroff noti from system-server*/
43
44
45 extern GMainLoop *g_thumb_server_mainloop;
46
47 static void _media_thumb_signal_handler(void *user_data)
48 {
49         thumb_dbg("Singal Hander for HEYNOTI \"power_off_start\"");
50
51         if (g_thumb_server_mainloop)
52                 ecore_main_loop_quit();
53         else
54                 exit(1);
55
56         return;
57 }
58
59 int main()
60 {
61         
62         int sockfd = -1;
63
64     GSource *source = NULL;
65         GIOChannel *channel = NULL;
66         GMainContext *context = NULL;
67
68         /*heynoti for power off*/
69         int err = 0;
70         int heynoti_id = heynoti_init();
71
72         if (heynoti_id < 0) {
73                 thumb_err("heynoti_init failed");
74         } else {
75                 err = heynoti_subscribe(heynoti_id, POWEROFF_NOTI_NAME, _media_thumb_signal_handler, NULL);
76                 if (err < 0) {
77                         thumb_err("heynoti_attach_handler failed: %d", err);
78                 } else {
79                         err = heynoti_attach_handler(heynoti_id);
80                         if (err < 0)
81                                 thumb_err("heynoti_attach_handler failed: %d", err);
82                 }
83         }
84
85         /* Set VCONFKEY_SYSMAN_MMC_FORMAT callback to get noti for SD card format */
86         err = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_FORMAT, (vconf_callback_fn) _thumb_daemon_vconf_cb, NULL);
87         if (err == -1)
88                 thumb_err("vconf_notify_key_changed : %s fails", VCONFKEY_SYSMAN_MMC_FORMAT);
89
90         /* Set VCONFKEY_SYSMAN_MMC_STATUS callback to get noti when SD card is ejected */
91         err = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, (vconf_callback_fn) _thumb_daemon_mmc_eject_vconf_cb, NULL);
92         if (err == -1)
93                 thumb_err("vconf_notify_key_changed : %s fails", VCONFKEY_SYSMAN_MMC_STATUS);
94
95         /* Create and bind new UDP socket */
96         if (!_thumb_server_prepare_socket(&sockfd)) {
97                 thumb_err("Failed to create socket\n");
98                 return -1;
99         }
100
101         context = g_main_context_default ();
102         
103         /* Create new channel to watch udp socket */
104         channel = g_io_channel_unix_new(sockfd);
105         source = g_io_create_watch(channel, G_IO_IN);
106
107         /* Set callback to be called when socket is readable */
108         g_source_set_callback(source, (GSourceFunc)_thumb_server_read_socket, NULL, NULL);
109         g_source_attach(source, context);
110
111         GSource *source_evas_init = NULL;
112         source_evas_init = g_idle_source_new ();
113         g_source_set_callback (source_evas_init, _thumb_daemon_start_jobs, NULL, NULL);
114         g_source_attach (source_evas_init, context);
115
116 /*      Would be used when glib 2.32 is installed
117         GSource *sig_handler_src = NULL;
118         sig_handler_src = g_unix_signal_source_new (SIGTERM);
119         g_source_set_callback(sig_handler_src, (GSourceFunc)_media_thumb_signal_handler, NULL, NULL);
120         g_source_attach(sig_handler_src, context);
121 */
122         ecore_evas_init();
123
124         thumb_dbg("************************************");
125         thumb_dbg("*** Thumbnail server is running ***");
126         thumb_dbg("************************************");
127
128         ecore_main_loop_begin();
129
130         thumb_dbg("Thumbnail server is shutting down...");
131
132         g_io_channel_shutdown(channel,  FALSE, NULL);
133         g_io_channel_unref(channel);
134         _thumb_daemon_finish_jobs();
135
136         return 0;
137 }