~noskcaj/ubuntu/vivid/lyricue/3.7.1

« back to all changes in this revision

Viewing changes to src/lyricue_display.c

  • Committer: Package Import Robot
  • Author(s): Ilya Barygin
  • Date: 2011-11-06 16:24:28 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20111106162428-o9tmwfr5hbq1884c
Tags: 3.2.1-0ubuntu1
* New upstream release (LP: #389654).
* Packaging based on upstream's PPA:
  - lists of (build-)dependencies updated
  - menu file dropped
  - postinst/postrm scripts removed, lyricue guides setup on first run
  - debian/rules: use dh sequencer
* Generate manpage with pod2man.
* extra-docs.patch: don't install some unneded files.
* manpage-name.patch: make manpage valid.
* desktop.patch: make desktop files valid.
* Convert copyright file to DEP5 format.
* Use source format 3.0 (quilt).
* Update homepage and watch file.
* Bump Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *            lyricue_display.c
 
3
 *
 
4
 *  Tue Jul 20 15:49:24 2010
 
5
 *  Copyright  2010  Chris Debenham
 
6
 *  <chris@adebenham.com>
 
7
 ****************************************************************************/
 
8
 
 
9
/*
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
         * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Library General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
23
 */
 
24
 
 
25
#include "lyricue_display.h"
 
26
 
 
27
extern MYSQL *lyricDb;
 
28
extern gchar *bible_name;
 
29
extern gfloat stage_width;
 
30
extern gfloat stage_height;
 
31
extern gint bg_is_video;
 
32
extern ClutterActor *background;
 
33
 
 
34
gint blanked_state = BLANK_NONE;
 
35
gchar *default_bg = NULL;
 
36
gchar *current_bg = NULL;
 
37
gchar *temp_bg = NULL;
 
38
int current_item = 0;
 
39
int current_list = 0;
 
40
GHashTable *config = NULL;
 
41
#define SERVER_PORT 2346
 
42
 
 
43
// Command line options
 
44
gboolean windowed = FALSE;
 
45
gboolean debugging = FALSE;
 
46
int server_port = SERVER_PORT;
 
47
gchar *dbhostname = "localhost";
 
48
gchar *geometry = NULL;
 
49
unsigned long windowid = 0;
 
50
 
 
51
static GOptionEntry entries[] = {
 
52
    {"window", 'w', 0, G_OPTION_ARG_NONE, &windowed, "Run in a window", NULL},
 
53
    {"remote", 'r', 0, G_OPTION_ARG_STRING, &dbhostname, "Database hostname", NULL},
 
54
    {"geometry", 'g', 0, G_OPTION_ARG_STRING, &geometry, "Window Geometry", NULL},
 
55
    {"port", 'p', 0, G_OPTION_ARG_INT, &server_port, "Port to listen on", NULL},
 
56
    {"miniview", 'm', 0, G_OPTION_ARG_INT, &windowid, "Embed in windowid", NULL},
 
57
    {"debug", 'd', 0, G_OPTION_ARG_NONE, &debugging, "Enable debug output", NULL},
 
58
    {NULL}
 
59
};
 
60
 
 
61
 
 
62
int
 
63
main (int argc, char *argv[])
 
64
{
 
65
    unsetenv("LIBGL_ALWAYS_INDIRECT");
 
66
    setenv("CLUTTER_DISABLE_MIPMAPPED_TEXT","1",0);
 
67
    bindtextdomain (GETTEXT_PACKAGE, NULL);
 
68
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
69
    textdomain (GETTEXT_PACKAGE);
 
70
 
 
71
    g_type_init ();
 
72
    GError *error = NULL;
 
73
    GOptionContext *context;
 
74
 
 
75
    context = g_option_context_new ("- Lyricue display");
 
76
    g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
 
77
    g_option_context_set_ignore_unknown_options(context, TRUE);
 
78
    if (!g_option_context_parse (context, &argc, &argv, &error)) {
 
79
        g_print ("option parsing failed: %s\n", error->message);
 
80
        exit (1);
 
81
    }
 
82
    int ret = db_select ();
 
83
    if (ret) {
 
84
        // Really should handle this ;)
 
85
    }
 
86
    load_configuration (lyricDb);
 
87
    bible_load ((gchar *) g_hash_table_lookup (config, "DefBible"));
 
88
 
 
89
 
 
90
    // Setup network
 
91
    GSocketService *service = g_socket_service_new ();
 
92
    GInetAddress *address = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
 
93
    g_snprintf(argv[0],29,"Lyricue Display on port %04d",server_port);
 
94
    l_debug("Process Name:%s",argv[0]);
 
95
 
 
96
    if (!g_socket_listener_add_inet_port
 
97
        (G_SOCKET_LISTENER (service), server_port, NULL, NULL)) {
 
98
        l_debug ("Unable to listen on port %d", server_port);
 
99
        return EXIT_FAILURE;
 
100
    }
 
101
    g_object_unref (address);
 
102
    g_socket_service_start (service);
 
103
    g_signal_connect (service, "incoming", G_CALLBACK (new_connection), NULL);
 
104
 
 
105
    // Setup tracker entry in DB
 
106
    do_query(lyricDb, "DELETE FROM playlists WHERE id=-1");
 
107
    do_query(lyricDb, "INSERT INTO playlists SET id=-1,ref=0,title=''");
 
108
 
 
109
    ret = create_main_window (argc, argv);
 
110
 
 
111
    if (windowid == 0) {
 
112
        clutter_main ();
 
113
    } else {
 
114
        gtk_main();
 
115
    }
 
116
    ret = db_deselect ();
 
117
 
 
118
    return EXIT_SUCCESS;
 
119
}
 
120
 
 
121
gboolean
 
122
network_read (GIOChannel * source, GIOCondition cond, gpointer data)
 
123
{
 
124
    GString *s = g_string_new (NULL);
 
125
    GError *error = NULL;
 
126
    GIOStatus ret = g_io_channel_read_line_string (source, s, NULL, &error);
 
127
 
 
128
    if (ret == G_IO_STATUS_EOF) {
 
129
        l_debug ("eof");
 
130
        return FALSE;
 
131
    } else if (ret == G_IO_STATUS_ERROR) {
 
132
        g_warning ("Error reading: %s\n", error->message);
 
133
        // Drop last reference on connection
 
134
        g_object_unref (data);
 
135
        // Remove the event source
 
136
        return FALSE;
 
137
    } else {
 
138
        s->str = g_strstrip(s->str);
 
139
        if (g_utf8_strlen (s->str, -1) > 0) {
 
140
            handle_command (source, s->str);
 
141
        }
 
142
        g_io_channel_shutdown (source, TRUE, NULL);
 
143
        g_object_unref (data);
 
144
        return FALSE;
 
145
    }
 
146
 
 
147
    return TRUE;
 
148
}
 
149
 
 
150
gboolean
 
151
new_connection (GSocketService * service,
 
152
                GSocketConnection * connection,
 
153
                GObject * source_object, gpointer user_data)
 
154
{
 
155
    GSocketAddress *sockaddr =
 
156
      g_socket_connection_get_remote_address (connection, NULL);
 
157
    GInetAddress *addr =
 
158
      g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sockaddr));
 
159
    guint16 port =
 
160
      g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
 
161
 
 
162
    l_debug ("New Connection from %s:%d", g_inet_address_to_string (addr),
 
163
             port);
 
164
 
 
165
    g_object_ref (connection);
 
166
    GSocket *socket = g_socket_connection_get_socket (connection);
 
167
 
 
168
    gint fd = g_socket_get_fd (socket);
 
169
    GIOChannel *channel = g_io_channel_unix_new (fd);
 
170
    g_io_add_watch (channel, G_IO_IN, (GIOFunc) network_read, connection);
 
171
    return TRUE;
 
172
}
 
173
 
 
174
void
 
175
handle_command (GIOChannel * source, const char *command)
 
176
{
 
177
    l_debug ("Received: %s", command);
 
178
    update_miniview(command);
 
179
    GString *returnstring = NULL;
 
180
    gchar **line = g_strsplit (command, ":", 2);
 
181
    if (line[1] != NULL) {
 
182
        line[0] = g_utf8_strdown (line[0], -1);
 
183
        if (g_strcmp0 (line[0], "preview") == 0) {
 
184
            do_preview (line[1]);
 
185
        } else if (g_strcmp0 (line[0], "status") == 0) {
 
186
            returnstring = do_status ();
 
187
        } else if (g_strcmp0 (line[0], "snapshot") == 0) {
 
188
            do_snapshot ();
 
189
        } else if (g_strcmp0 (line[0], "reconfig") == 0) {
 
190
            do_reconfig ();
 
191
        } else if (g_strcmp0 (line[0], "backdrop") == 0) {
 
192
            do_backdrop (line[1]);
 
193
        } else if (g_strcmp0 (line[0], "blank") == 0) {
 
194
            do_blank (line[1]);
 
195
        } else if (g_strcmp0 (line[0], "change_to_db") == 0) {
 
196
            do_change_to_db (line[1]);
 
197
        } else if (g_strcmp0 (line[0], "next_point") == 0) {
 
198
            do_next_point (line[1]);
 
199
        } else if (g_strcmp0 (line[0], "get") == 0) {
 
200
            do_get (line[1]);
 
201
        } else if (g_strcmp0 (line[0], "display") == 0) {
 
202
            do_display (line[1]);
 
203
        } else if (g_strcmp0 (line[0], "osd") == 0) {
 
204
            do_osd (line[1]);
 
205
        } else if (g_strcmp0 (line[0], "media") == 0) {
 
206
            do_media (line[1]);
 
207
        } else if (g_strcmp0 (line[0], "fade") == 0) {
 
208
            do_fade (line[1]);
 
209
        } else if (g_strcmp0 (line[0], "blur") == 0) {
 
210
            do_blur (line[1]);
 
211
        }
 
212
    }
 
213
    g_strfreev (line);
 
214
    if (returnstring != NULL) {
 
215
        l_debug("The status message sent is: %s",returnstring->str);
 
216
        GIOStatus res = g_io_channel_write_chars (source, returnstring->str,
 
217
                                                  returnstring->len, NULL,
 
218
                                                  NULL);
 
219
        g_string_free (returnstring, TRUE);
 
220
        if (res != G_IO_STATUS_NORMAL)
 
221
            return;
 
222
        /* force flushing of the write buffer */
 
223
        res = g_io_channel_flush (source, NULL);
 
224
    }
 
225
    update_tracker();
 
226
}
 
227
 
 
228
void
 
229
do_media (const char *options)
 
230
{
 
231
    if (bg_is_video) {
 
232
        gchar **line = g_strsplit (options, ":", 2);
 
233
        if (g_ascii_strncasecmp (line[0], "pause", 5) == 0) {
 
234
            media_pause ();
 
235
        } else if (g_ascii_strncasecmp (line[0], "skip", 4) == 0) {
 
236
            media_skip (atoi(line[1]));
 
237
        }
 
238
        g_strfreev (line);
 
239
    }
 
240
}
 
241
 
 
242
void
 
243
do_fade (const char *options)
 
244
{
 
245
    fade_backdrop(atoi(options));
 
246
}
 
247
 
 
248
void
 
249
do_blur (const char *options)
 
250
{
 
251
    blur_backdrop(atoi(options));
 
252
}
 
253
 
 
254
void
 
255
do_preview (const char *options)
 
256
{
 
257
    gchar **line = g_strsplit (options, ":", 2);
 
258
    gboolean wrap = TRUE;
 
259
    unblank();
 
260
    if (g_strcmp0(line[0], "ignore") != 0 ) {
 
261
        gchar **extras = g_strsplit(parse_special(line[0]), "\n", 4);
 
262
        if ((g_strv_length(extras) == 6) && (g_strcmp0(extras[3], "nowrap") == 0)) {
 
263
            wrap = FALSE;
 
264
        }
 
265
        set_headtext (parse_special(extras[0]), 0, 1);
 
266
 
 
267
        if (g_strv_length(extras) >= 3) {
 
268
            GString *footer = g_string_new (NULL);
 
269
 
 
270
            if (g_utf8_strlen(extras[2],10) != 0) {
 
271
                g_string_printf(footer, "%s %s - %s",
 
272
                   gettext("Written by "),
 
273
                   extras[1],
 
274
                   extras[2]);
 
275
            } else {
 
276
                if (g_utf8_strlen(extras[1],10) != 0) {
 
277
                    g_string_printf(footer, "%s %s",
 
278
                        gettext("Written by "),
 
279
                        extras[1]);
 
280
                } else {
 
281
                    g_string_assign(footer,"");
 
282
                }
 
283
            }
 
284
            set_foottext(footer->str,0,1);
 
285
            g_string_free(footer, TRUE);
 
286
        }
 
287
        g_strfreev(extras);
 
288
 
 
289
    }
 
290
    set_maintext (parse_special(line[1]), 0, wrap);
 
291
    g_strfreev(line);
 
292
}
 
293
 
 
294
GString *
 
295
do_status ()
 
296
{
 
297
    l_debug ("Return status");
 
298
    GString *ret = g_string_new (NULL);
 
299
    g_string_printf (ret, "Status,W:%.0f,H:%.0f,F:%s,B:%s\n", stage_width,
 
300
                     stage_height, (gchar *) g_hash_table_lookup (config,
 
301
                                                                  "Main"),
 
302
                     bible_name);
 
303
    return ret;
 
304
}
 
305
 
 
306
void
 
307
do_snapshot ()
 
308
{
 
309
    l_debug ("do_snapshot not implemented");
 
310
}
 
311
 
 
312
void
 
313
do_reconfig ()
 
314
{
 
315
    l_debug ("do_reconfig");
 
316
    load_configuration (lyricDb);
 
317
}
 
318
 
 
319
void
 
320
do_backdrop (const char *options)
 
321
{
 
322
    l_debug ("do_backdrop: %s", options);
 
323
    gchar **line = g_strsplit (options, ":", 2);
 
324
    temp_bg = NULL;
 
325
    default_bg = parse_special(line[0]);
 
326
    change_backdrop (default_bg, TRUE);
 
327
    g_strfreev (line);
 
328
}
 
329
 
 
330
void
 
331
unblank()
 
332
{
 
333
    if (blanked_state == BLANK_BG) {
 
334
        change_backdrop (temp_bg, TRUE);
 
335
    }
 
336
    blanked_state = BLANK_NONE;
 
337
}
 
338
 
 
339
void
 
340
do_blank (const char *options)
 
341
{
 
342
    l_debug ("do_blank: %s", options);
 
343
    gchar **line = g_strsplit (options, ":", 2);
 
344
    if (strlen(options) <= 1) {
 
345
        options = NULL;
 
346
    }
 
347
 
 
348
    if (blanked_state == BLANK_BG) {
 
349
        l_debug("Re-show text");
 
350
        do_display("current");
 
351
    } else if ((blanked_state == BLANK_TEXT) && options != NULL) {
 
352
        l_debug("clear text and set BG");
 
353
        temp_bg = current_bg;
 
354
        change_backdrop (line[0], TRUE);
 
355
        blanked_state = BLANK_BG;
 
356
    } else if ((blanked_state == BLANK_TEXT) && options == NULL) {
 
357
        l_debug("Re-show text - 2");
 
358
        do_display("current");
 
359
    } else if (options != NULL) {
 
360
        l_debug("clear text and set BG - 2");
 
361
        temp_bg = current_bg;
 
362
        change_backdrop (line[0], TRUE);
 
363
        set_maintext ("", 0, FALSE);
 
364
        set_headtext ("", 0, FALSE);
 
365
        set_foottext ("", 0, FALSE);
 
366
        blanked_state = BLANK_BG;
 
367
    } else {
 
368
        l_debug("Clear text");
 
369
        set_maintext ("", 0, FALSE);
 
370
        set_headtext ("", 0, FALSE);
 
371
        set_foottext ("", 0, FALSE);
 
372
        blanked_state = BLANK_TEXT;
 
373
    }
 
374
    g_strfreev(line);
 
375
}
 
376
 
 
377
void
 
378
do_change_to_db (const char *options)
 
379
{
 
380
    l_debug ("do_change_to_db: %s", options);
 
381
    bible_load (options);
 
382
}
 
383
 
 
384
void
 
385
do_next_point (const char *options)
 
386
{
 
387
    l_debug ("do_next_point not implemented");
 
388
}
 
389
 
 
390
void
 
391
do_get (const char *options)
 
392
{
 
393
    l_debug ("do_get not implemented");
 
394
}
 
395
 
 
396
void
 
397
do_osd (const char *options)
 
398
{
 
399
    l_debug ("do_osd");
 
400
    if (options != NULL) {
 
401
        gchar **line = g_strsplit (options, ":", 2);
 
402
        int speed = 10000;
 
403
        if(g_strcmp0(line[0],"slow") == 0) {
 
404
            speed = 20000;
 
405
        } else if(g_strcmp0(line[0],"fast") == 0) {
 
406
            speed = 5000;
 
407
        } else if(g_strcmp0(line[0],"default") == 0) {
 
408
            speed = 10000;
 
409
        } else {
 
410
            speed = atoi(line[0]);
 
411
        }
 
412
        gchar *text = parse_special(line[1]);
 
413
        set_osd(speed, text);
 
414
    }
 
415
}
 
416
 
 
417
void
 
418
do_display (const char *options)
 
419
{
 
420
    l_debug ("do_display");
 
421
    if (options != NULL) {
 
422
        gchar **line = g_strsplit (options, ":", 2);
 
423
        unblank();
 
424
        MYSQL_ROW row;
 
425
        MYSQL_RES *result;
 
426
        gboolean bg_changed = FALSE;
 
427
        do_query (lyricDb, "SELECT playlist FROM playlist WHERE playorder=%d",
 
428
                  current_item);
 
429
        result = mysql_store_result (lyricDb);
 
430
        row = mysql_fetch_row (result);
 
431
                if (row != NULL) {
 
432
                current_list = atoi (row[0]);
 
433
                }
 
434
        mysql_free_result (result);
 
435
 
 
436
        gchar *command = g_utf8_strdown (line[0], -1);
 
437
 
 
438
        if (g_strcmp0 (command, "playlist") == 0) {
 
439
            current_list = atoi (line[1]);
 
440
 
 
441
        } else if (g_strcmp0 (command, "current") == 0) {
 
442
            // Clear text and then redisplay same page
 
443
            set_maintext ("", 0, FALSE);
 
444
            set_headtext ("", 0, FALSE);
 
445
            set_foottext ("", 0, FALSE);
 
446
            if (g_strcmp0(line[1], "nobg") == 0) {
 
447
                bg_changed=TRUE;
 
448
            }
 
449
        } else if (g_strcmp0 (command, "next_page") == 0) {
 
450
            do_query (lyricDb,
 
451
                      "SELECT MIN(playorder) FROM playlist WHERE playlist=%d AND playorder > %d ORDER BY playorder",
 
452
                      current_list, current_item);
 
453
            result = mysql_store_result (lyricDb);
 
454
            row = mysql_fetch_row (result);
 
455
            mysql_free_result (result);
 
456
            if (row[0]) {
 
457
                // Show next page
 
458
                current_item = atoi (row[0]);
 
459
            } else {
 
460
                // End of song reached
 
461
                gchar **loop = g_strsplit (line[1], ";", 2);
 
462
                if (g_strcmp0(loop[0], "loop") == 0) {
 
463
                    // Looping
 
464
                    int loop_parent = 0;
 
465
                    if (loop[1] != NULL) {
 
466
                        loop_parent = atoi(loop[1]);
 
467
                    }
 
468
                    if (loop_parent == 0) {
 
469
                        l_debug ("Looping a song, back to page 1");
 
470
                        do_query (lyricDb,
 
471
                              "SELECT MIN(playorder) FROM playlist WHERE playlist=%d",
 
472
                                current_list);
 
473
                        result = mysql_store_result (lyricDb);
 
474
                        row = mysql_fetch_row (result);
 
475
                        mysql_free_result (result);
 
476
                        if (row[0] != NULL) {
 
477
                            current_item = atoi (row[0]);
 
478
                        }
 
479
                    } else {
 
480
                        l_debug ("Looping a sublist");
 
481
                        do_query (lyricDb,
 
482
                              "SELECT MIN(p1.playorder) FROM playlist AS p1, playlist AS p2 WHERE p1.playorder>p2.playorder AND p2.type='play' AND p2.data=%d AND p1.playlist=%d", current_list, loop_parent);
 
483
                        result = mysql_store_result (lyricDb);
 
484
                        row = mysql_fetch_row (result);
 
485
                        mysql_free_result (result);
 
486
                        if (row[0] != NULL) {
 
487
                            current_item = atoi (row[0]);
 
488
                        } else {
 
489
                            // Loop back to top of parent
 
490
                            do_query (lyricDb,
 
491
                              "SELECT MIN(playorder) FROM playlist WHERE playlist=%d",
 
492
                                loop_parent);
 
493
                            result = mysql_store_result (lyricDb);
 
494
                            row = mysql_fetch_row (result);
 
495
                            mysql_free_result (result);
 
496
                            if (row[0] != NULL) {
 
497
                                current_item = atoi (row[0]);
 
498
                            }
 
499
                        }
 
500
                    }
 
501
                } else {
 
502
                    // Jump to next song
 
503
                    do_display ("next_song:0");
 
504
                    return;
 
505
                }
 
506
            }
 
507
 
 
508
        } else if (g_strcmp0 (command, "prev_page") == 0) {
 
509
            do_query (lyricDb,
 
510
                      "SELECT MAX(playorder) FROM playlist WHERE playlist=%d AND playorder < %d ORDER BY playorder",
 
511
                      current_list, current_item);
 
512
            result = mysql_store_result (lyricDb);
 
513
            row = mysql_fetch_row (result);
 
514
            mysql_free_result (result);
 
515
            if (row[0]) {
 
516
                current_item = atoi (row[0]);
 
517
            } else {
 
518
                if (g_strcmp0 (line[1], "loop")) {
 
519
                    do_query (lyricDb,
 
520
                              "SELECT MAX(playorder) FROM playlist WHERE playlist=%d",
 
521
                              current_list);
 
522
                    result = mysql_store_result (lyricDb);
 
523
                    row = mysql_fetch_row (result);
 
524
                    mysql_free_result (result);
 
525
                    if (row[0] != NULL) {
 
526
                        current_item = atoi (row[0]);
 
527
                    }
 
528
                }
 
529
            }
 
530
 
 
531
        } else if (g_strcmp0 (command, "next_song") == 0) {
 
532
            do_query (lyricDb,
 
533
                      "SELECT a.playorder,a.playlist FROM playlist AS a, playlist AS b WHERE a.data=b.playlist AND a.type=\"play\" AND b.playorder=%d",
 
534
                      current_item);
 
535
            result = mysql_store_result (lyricDb);
 
536
            row = mysql_fetch_row (result);
 
537
            mysql_free_result (result);
 
538
                
 
539
                        if (row && (row[0] != NULL)) {
 
540
                current_item = atoi (row[0]);
 
541
                current_list = atoi (row[1]);
 
542
            }
 
543
                do_query (lyricDb,
 
544
                  "SELECT MIN(playorder) FROM playlist WHERE playorder > %d AND playlist=%d",
 
545
                  current_item, current_list);
 
546
                result = mysql_store_result (lyricDb);
 
547
            row = mysql_fetch_row (result);
 
548
            mysql_free_result (result);
 
549
                        if (row[0] != NULL) {
 
550
                    current_item = atoi (row[0]);
 
551
                        }
 
552
 
 
553
        } else if (g_strcmp0 (command, "prev_song") == 0) {
 
554
            do_query (lyricDb,
 
555
                      "SELECT a.playorder,a.playlist FROM playlist AS a, playlist AS b WHERE a.data=b.playlist AND a.type=\"play\" AND b.playorder=%d",
 
556
                      current_item);
 
557
            result = mysql_store_result (lyricDb);
 
558
            row = mysql_fetch_row (result);
 
559
            mysql_free_result (result);
 
560
                        if (row && (row[0] != NULL)) {
 
561
                    current_item = atoi (row[0]);
 
562
                current_list = atoi (row[1]);
 
563
            }
 
564
            do_query (lyricDb,
 
565
                  "SELECT MAX(playorder) FROM playlist WHERE playorder < %d AND playlist=%d",
 
566
                  current_item, current_list);
 
567
            result = mysql_store_result (lyricDb);
 
568
            row = mysql_fetch_row (result);
 
569
            mysql_free_result (result);
 
570
                        if (row[0] != NULL) {
 
571
                    current_item = atoi (row[0]);
 
572
                        }
 
573
 
 
574
        } else if (g_strcmp0 (command, "page") == 0) {
 
575
            do_query (lyricDb,
 
576
                      "SELECT playorder FROM playlist WHERE playlist=%d",
 
577
                      current_list);
 
578
            result = mysql_store_result (lyricDb);
 
579
            int count = 0;
 
580
            while ((count < atoi (line[1]))
 
581
                   && (row = mysql_fetch_row (result))) {
 
582
                count++;
 
583
            }
 
584
            if (row && (row[0] != NULL)) {
 
585
                current_item = atoi (row[0]);
 
586
            }
 
587
        } else {
 
588
            current_item = atoi (command);
 
589
        }
 
590
 
 
591
        do_query (lyricDb,
 
592
                  "SELECT type,data,transition FROM playlist WHERE playorder=%d",
 
593
                  current_item);
 
594
        result = mysql_store_result (lyricDb);
 
595
        row = mysql_fetch_row (result);
 
596
        if (row != NULL) {
 
597
            gchar *type = g_strdup (row[0]);
 
598
            gchar *data = g_strdup (row[1]);
 
599
            gchar *lyrics = "";
 
600
            gchar *header = "";
 
601
            gchar *footer = "";
 
602
            gboolean wrap = TRUE;
 
603
            int transition = atoi (row[2]);
 
604
 
 
605
            if (g_strcmp0 (type, "back") == 0) {
 
606
                default_bg = g_strdup(data);
 
607
                change_backdrop (default_bg, TRUE);
 
608
                bg_changed = TRUE;
 
609
                g_strfreev(line);
 
610
            } else if (g_strcmp0 (type, "file") == 0) {
 
611
                change_backdrop (data, FALSE);
 
612
                bg_changed = TRUE;
 
613
                g_strfreev(line);
 
614
            } else if (g_strcmp0 (type, "imag") == 0) {
 
615
                change_backdrop (data, FALSE);
 
616
                bg_changed = TRUE;
 
617
            } else if (g_strcmp0 (type, "vers") == 0) {
 
618
                do_query (lyricDb,
 
619
                          "SELECT title FROM playlist,playlists WHERE playlist.playlist=playlists.id AND playorder=%d",
 
620
                          current_item);
 
621
                result = mysql_store_result (lyricDb);
 
622
                row = mysql_fetch_row (result);
 
623
                gchar **selected = g_strsplit (data, ":", 2);
 
624
                gchar *verse = NULL;
 
625
                if (selected[1] == NULL) {
 
626
                    selected = g_strsplit (row[0], ":", 3);
 
627
                    gchar **datasplt = g_strsplit (data, "-", 2);
 
628
                    GString *verseref = g_string_new (NULL);
 
629
                    g_string_printf (verseref, "%s:%s:%s-%s:%s", selected[0],
 
630
                                     selected[1], datasplt[0], selected[1],
 
631
                                     datasplt[1]);
 
632
                    verse = do_grab_verse (g_string_free (verseref, FALSE));
 
633
                } else {
 
634
                    // Broken
 
635
                }
 
636
 
 
637
                if (verse != NULL) {
 
638
                    lyrics = g_strdup(verse);
 
639
                }
 
640
                header = g_strdup(row[0]);
 
641
                footer = g_strdup(bible_name);
 
642
                wrap = TRUE;
 
643
            } else if ((g_strcmp0 (type, "play") == 0) ||
 
644
                       (g_strcmp0 (type, "sub") == 0)) {
 
645
                do_query (lyricDb,
 
646
                          "SELECT playorder FROM playlist WHERE playlist=%s ORDER BY playorder",
 
647
                          data);
 
648
                result = mysql_store_result (lyricDb);
 
649
                row = mysql_fetch_row (result);
 
650
                do_display (row[0]);
 
651
                return;
 
652
            } else {            // Song page
 
653
                do_query (lyricDb,
 
654
                          "SELECT title,artist,lyrics,copyright FROM lyricMain AS l, page AS pa WHERE pa.songid=l.id AND pa.pageid=%s",
 
655
                          data);
 
656
                result = mysql_store_result (lyricDb);
 
657
                row = mysql_fetch_row (result);
 
658
                mysql_free_result (result);
 
659
                if (row != NULL) {
 
660
                    gchar *title = g_strdup (row[0]);
 
661
                    gchar *artist = g_strdup (row[1]);
 
662
                    gchar *lyrictmp = g_strdup (row[2]);
 
663
                    gchar *copyright = g_strdup (row[3]);
 
664
                    GString *foot = g_string_new (NULL);
 
665
                    if (g_utf8_strlen (artist, 10) != 0) {
 
666
                        g_string_printf (foot, "Written by %s", artist);
 
667
                    }
 
668
                    if (g_utf8_strlen (copyright, 10) != 0) {
 
669
                        g_string_append_printf (foot, " - %s", copyright);
 
670
                    }
 
671
                    lyrics = g_strdup(lyrictmp);
 
672
                    header = g_strdup(title);
 
673
                    footer = g_strdup(foot->str);
 
674
                    g_string_free (foot, TRUE);
 
675
                }
 
676
            }
 
677
 
 
678
            // Look for associated background image
 
679
            if (!bg_changed) {
 
680
                int res = do_query (lyricDb,
 
681
                                    "SELECT imagename FROM associations WHERE playlist=%d",
 
682
                                    current_item);
 
683
                int bg_changed = FALSE;
 
684
                if (res == 0) {
 
685
                    result = mysql_store_result (lyricDb);
 
686
                    row = mysql_fetch_row (result);
 
687
                    if (row != NULL) {
 
688
                        change_backdrop (row[0], TRUE);
 
689
                        bg_changed = TRUE;
 
690
                    }
 
691
                    mysql_free_result (result);
 
692
                }
 
693
                if (!bg_changed) {
 
694
                    res =
 
695
                      do_query (lyricDb,
 
696
                                "SELECT a.imagename,q.data FROM associations as a, playlist AS p, playlist AS q WHERE p.type='play' AND p.data=q.playlist and a.playlist=p.playorder AND q.playorder=%d",
 
697
                                current_item);
 
698
                    if (res == 0) {
 
699
                        result = mysql_store_result (lyricDb);
 
700
                        row = mysql_fetch_row (result);
 
701
                        mysql_free_result (result);
 
702
                        if (row != NULL) {
 
703
                            change_backdrop (row[0], TRUE);
 
704
                            bg_changed = TRUE;
 
705
                        }
 
706
                    }
 
707
                }
 
708
                if (!bg_changed && (g_strcmp0(default_bg, current_bg) != 0)) {
 
709
                    l_debug("Reset bg to default");
 
710
                    change_backdrop(default_bg, TRUE);
 
711
                }
 
712
            }
 
713
 
 
714
            set_maintext(parse_special(lyrics), transition, wrap);
 
715
            set_headtext(parse_special(header), transition, wrap);
 
716
            set_foottext(parse_special(footer), transition, wrap);
 
717
        }
 
718
    }
 
719
 
 
720
 
 
721
}
 
722
 
 
723
gboolean
 
724
update_tracker ()
 
725
{
 
726
    //l_debug ("Updating tracker");
 
727
 
 
728
    // Only do if this is main server
 
729
    if (server_port == SERVER_PORT) {
 
730
        GString *title = g_string_new (NULL);
 
731
        if (blanked_state == BLANK_BG) {
 
732
            g_string_assign (title, "blank_bg");
 
733
        } else if (blanked_state == BLANK_TEXT) {
 
734
            g_string_assign (title, "blank_text");
 
735
        }
 
736
        if (bg_is_video) {
 
737
            g_string_append_printf (title, "%.0f;%.0f;%d",
 
738
                                    clutter_media_get_progress (CLUTTER_MEDIA
 
739
                                                                (background))
 
740
                                    *
 
741
                                    clutter_media_get_duration (CLUTTER_MEDIA
 
742
                                                                (background)),
 
743
                                    clutter_media_get_duration (CLUTTER_MEDIA
 
744
                                                                (background)),
 
745
                                    clutter_media_get_playing (CLUTTER_MEDIA
 
746
                                                               (background)));
 
747
        } else {
 
748
            g_string_append (title, "0;0;0");
 
749
        }
 
750
        do_query (lyricDb,
 
751
                  "UPDATE playlists SET ref = %d, title = \"%s\" WHERE id=-1",
 
752
                  current_item, g_string_free (title, FALSE));
 
753
    }
 
754
        return TRUE;
 
755
}
 
756
 
 
757
void
 
758
update_miniview (const char *command)
 
759
{
 
760
    if (server_port == SERVER_PORT) {
 
761
l_debug("miniview time");
 
762
        GSocketClient *client = g_socket_client_new();
 
763
        GSocketConnection *conn = g_socket_client_connect_to_host ( client, dbhostname, 2348, NULL, NULL);
 
764
        if (conn != NULL) {
 
765
            GOutputStream *out = g_io_stream_get_output_stream (G_IO_STREAM(conn));
 
766
            g_output_stream_write(out, command, strlen(command), NULL, NULL);
 
767
            g_object_unref(conn);
 
768
        }
 
769
        g_object_unref(client);
 
770
    }
 
771
}