summaryrefslogtreecommitdiff
path: root/src/lib/vog_main.c
blob: f3fe6e9c6beea64c4a0f748c5701fd15fafd0b99 (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
#include "vog_private.h"

static int _vog_init_count = 0;
int _vog_log_dom_global = -1;

int VOG_EVENT_GAME_ADD = 0,
    VOG_EVENT_GAME_DEL = 0,
    VOG_EVENT_GAME_UPD = 0;

static void
_vog_games_free_cb(void *data)
{
   Vog_Game *vg;

   EINA_SAFETY_ON_NULL_RETURN(data);

   vg = data;
   vog_game_free(vg);
}

void
vog_game_free(Vog_Game *vg)
{
   EINA_SAFETY_ON_NULL_RETURN(vg);

   free((char *)vg->name);
   free((char *)vg->description);
   eina_stringshare_replace(&vg->genre, NULL);
   efreet_desktop_free(vg->ed);
   free(vg);
}

Vog_Game *
vog_game_new(const char *path)
{
   Vog_Game *vg;
   Efreet_Desktop *ed;

   EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);

   ed = efreet_desktop_get(path);
   if (!ed)
     {
        ERR("Failed to load desktop file %s", path);
        return NULL;
     }

   vg = calloc(1, sizeof(Vog_Game));
   if (!vg)
     {
        ERR("Failed to allocate Vog_Game structure");
        goto ed_free;
     }


   vg->ed = ed;
   vg->name = strdup(ed->name);
   if (!vg->name)
     {
        ERR("Failed to allocate vg->name");
        goto vg_free;
     }

   return vg;

vg_free:
   free(vg);
ed_free:
   efreet_desktop_free(ed);
return NULL;
}

void
vog_free(Vog *vog)
{
   EINA_SAFETY_ON_NULL_RETURN(vog);

   eina_hash_free(vog->games);
   ecore_event_handler_del(vog->ev.efc);
   ecore_event_handler_del(vog->ev.efd);
   ecore_event_handler_del(vog->ev.efm);
   eio_monitor_del(vog->ev.desktopdir);
   eina_stringshare_replace(&vog->dir.desktop, NULL);
   free(vog);
}

Vog *
vog_new(void)
{
   Vog *vog;

   vog = calloc(1, sizeof(Vog));
   if (!vog)
     {
        ERR("Failed to allocate Vog structure");
        return NULL;
     }

   vog->dir.desktop = eina_stringshare_add(PKG_DATA_DIR"/games/");
   if (!vog->dir.desktop)
     {
        ERR("Failed to allocate vog->dir.desktop!");
        goto vog_free;
     }


   /* Put inotify code */
   vog->ev.desktopdir = eio_monitor_add(vog->dir.desktop);
   if (!vog->ev.desktopdir)
     {
        ERR("Failed to put monitor on %s", vog->dir.desktop);
        goto vog_free_desktopdir;
     }

#define _MON(_a, _b, _c, _d)                                                   \
   _a = ecore_event_handler_add(EIO_MONITOR_##_b, _c, _d);
   _MON(vog->ev.efc, FILE_CREATED, vog_event_action, vog);
   _MON(vog->ev.efd, FILE_DELETED, vog_event_action, vog);
   _MON(vog->ev.efm, FILE_CLOSED, vog_event_action, vog);
#undef _MON

   if ((!vog->ev.efc) || (!vog->ev.efd) || (!vog->ev.efm))
     goto vog_events_del;

   vog->games = eina_hash_string_superfast_new(_vog_games_free_cb);
   if (!vog->games)
     goto vog_events_del;

   eio_file_direct_ls(vog->dir.desktop,
                      vog_event_filter_cb,
                      vog_event_main_cb,
                      vog_event_done_cb,
                      vog_event_error_cb,
                      vog);

   return vog;
vog_events_del:
   if (vog->ev.efc) ecore_event_handler_del(vog->ev.efc);
   if (vog->ev.efd) ecore_event_handler_del(vog->ev.efd);
   if (vog->ev.efm) ecore_event_handler_del(vog->ev.efm);
   if (vog->ev.desktopdir) eio_monitor_del(vog->ev.desktopdir);
vog_free_desktopdir:
   eina_stringshare_replace(&vog->dir.desktop, NULL);
vog_free:
   free(vog);
   return NULL;
}

int
vog_init(void)
{
   if (++_vog_init_count != 1)
     return _vog_init_count;

   if (!eina_init())
     {
        fprintf(stderr, "Vog can not initialize Eina\n");
        return --_vog_init_count;
     }

   _vog_log_dom_global = eina_log_domain_register("libvog", EINA_COLOR_RED);
   if (_vog_log_dom_global < 0)
     {
        EINA_LOG_ERR("Vog can not create a general log domain");
        goto shutdown_eina;
     }

   if (!ecore_init())
     {
        ERR("Can not initialize Ecore");
        goto unregister_log_domain;
     }

   if (!eio_init())
     {
        ERR("Can not initialize Eio");
        goto shutdown_ecore;
     }

   if (!efreet_init())
     {
        ERR("Can not initialize Efreet");
        goto shutdown_eio;
     }

   VOG_EVENT_GAME_ADD = ecore_event_type_new();
   VOG_EVENT_GAME_DEL = ecore_event_type_new();
   VOG_EVENT_GAME_UPD = ecore_event_type_new();

   return _vog_init_count;
shutdown_eio:
   eio_shutdown();
shutdown_ecore:
   ecore_shutdown();
unregister_log_domain:
   eina_log_domain_unregister(_vog_log_dom_global);
   _vog_log_dom_global = -1;
shutdown_eina:
   eina_shutdown();
   return --_vog_init_count;
}

int
vog_shutdown(void)
{
   if (_vog_init_count <= 0)
     {
        fprintf(stderr, "Vog init count not greater than 0 in shutdown.");
        return 0;
     }

   if (--_vog_init_count != 0)
     return _vog_init_count;

   efreet_shutdown();
   ecore_shutdown();
   eina_log_domain_unregister(_vog_log_dom_global);
   _vog_log_dom_global = -1;
   eina_shutdown();
   return _vog_init_count;
}