summaryrefslogtreecommitdiff
path: root/main.c
blob: c141875d0eccb97d9819cde09096a70c1737c24d (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
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009  Alexey Yakovenko

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "interface.h"
#include "support.h"
#include "playlist.h"
#include "playback.h"
#include "unistd.h"
#include "threading.h"
#include "messagepump.h"
#include "messages.h"
#include "gtkplaylist.h"
#include "codec.h"
#include "streamer.h"

GtkWidget *mainwin;
GtkWidget *searchwin;
gtkplaylist_t main_playlist;
gtkplaylist_t search_playlist;

int psdl_terminate = 0;

// update status bar and window title
static int sb_context_id = -1;
static char sb_text[512];
static float last_songpos = -1;

void
update_songinfo (void) {
    if (!mainwin) {
        return;
    }
    char sbtext_new[512] = "-";
    float songpos = last_songpos;
    if (p_ispaused ()) {
        strcpy (sbtext_new, "Paused");
    }
    else if (p_isstopped ()) {
        strcpy (sbtext_new, "Stopped");
    }
    else if (playlist_current.codec) {
        codec_lock ();
        codec_t *c = playlist_current.codec;
        int minpos = c->info.position / 60;
        int secpos = c->info.position - minpos * 60;
        int mindur = playlist_current.duration / 60;
        int secdur = playlist_current.duration - mindur * 60;
        const char *mode = c->info.channels == 1 ? "Mono" : "Stereo";
        int samplerate = c->info.samplesPerSecond;
        int bitspersample = c->info.bitsPerSample;
        songpos = c->info.position;
        codec_unlock ();

        snprintf (sbtext_new, 512, "[%s] %dHz | %d bit | %s | %d:%02d / %d:%02d | %d songs total | streambuffer: %d%%", playlist_current.filetype ? playlist_current.filetype:"-", samplerate, bitspersample, mode, minpos, secpos, mindur, secdur, pl_getcount (), streamer_get_fill_level ());
    }

    if (strcmp (sbtext_new, sb_text)) {
        strcpy (sb_text, sbtext_new);

        // form statusline
        GDK_THREADS_ENTER();
        // FIXME: don't update if window is not visible
        GtkStatusbar *sb = GTK_STATUSBAR (lookup_widget (mainwin, "statusbar"));
        if (sb_context_id == -1) {
            sb_context_id = gtk_statusbar_get_context_id (sb, "msg");
        }

        gtk_statusbar_pop (sb, sb_context_id);
        gtk_statusbar_push (sb, sb_context_id, sb_text);

        GDK_THREADS_LEAVE();
    }

    if (songpos != last_songpos) {
        void seekbar_draw (GtkWidget *widget);
        void seekbar_expose (GtkWidget *widget, int x, int y, int w, int h);
        if (mainwin) {
            GDK_THREADS_ENTER();
            GtkWidget *widget = lookup_widget (mainwin, "seekbar");
            seekbar_draw (widget);
            seekbar_expose (widget, 0, 0, widget->allocation.width, widget->allocation.height);
            GDK_THREADS_LEAVE();
            last_songpos = songpos;
        }
    }
}

void
psdl_thread (uintptr_t ctx) {
    p_play ();
    while (!psdl_terminate) {
        uint32_t msg;
        uintptr_t ctx;
        uint32_t p1;
        uint32_t p2;
        while (messagepump_pop(&msg, &ctx, &p1, &p2) != -1) {
            switch (msg) {
            case M_SONGCHANGED:
                GDK_THREADS_ENTER();
                // update window title
                int from = p1;
                int to = p2;
                if (from >= 0 || to >= 0) {
                    if (to >= 0) {
                        playItem_t *it = pl_get_for_idx (to);
                        char str[600];
                        char dname[512];
                        pl_format_item_display_name (it, dname, 512);
                        snprintf (str, 600, "DeaDBeeF - %s", dname);
                        gtk_window_set_title (GTK_WINDOW (mainwin), str);
                    }
                    else {
                        gtk_window_set_title (GTK_WINDOW (mainwin), "DeaDBeeF");
                    }
                }
                // update playlist view
                gtkpl_songchanged (&main_playlist, p1, p2);
                GDK_THREADS_LEAVE();
                break;
            case M_PLAYSONG:
                GDK_THREADS_ENTER();
                gtkpl_playsong (&main_playlist);
                GDK_THREADS_LEAVE();
                break;
            case M_PLAYSONGNUM:
                GDK_THREADS_ENTER();
                gtkpl_playsongnum (p1);
                GDK_THREADS_LEAVE();
                break;
            case M_STOPSONG:
                p_stop ();
                GDK_THREADS_ENTER();
                gtkpl_redraw_pl_row (&main_playlist, main_playlist.row);
                GDK_THREADS_LEAVE();
                break;
            case M_NEXTSONG:
                GDK_THREADS_ENTER();
                p_stop ();
                pl_nextsong (1);
                GDK_THREADS_LEAVE();
                break;
            case M_PREVSONG:
                GDK_THREADS_ENTER();
                p_stop ();
                pl_prevsong ();
                GDK_THREADS_LEAVE();
                break;
            case M_PAUSESONG:
                if (p_ispaused ()) {
                    p_unpause ();
                }
                else {
                    p_pause ();
                }

                GDK_THREADS_ENTER();
                gtkpl_redraw_pl_row (&main_playlist, main_playlist.row);
                GDK_THREADS_LEAVE();
                break;
            case M_PLAYRANDOM:
                GDK_THREADS_ENTER();
                gtkpl_randomsong ();
                GDK_THREADS_LEAVE();
                break;
            case M_SONGSEEK:
                if (playlist_current.codec) {
                    p_pause ();
                    codec_lock ();
                    playlist_current.codec->seek (p1 / 1000.f);
                    codec_unlock ();
                    p_unpause ();
                }
                break;
            case M_ADDDIR:
                // long time processing
                gtkpl_add_dir (&main_playlist, (char *)ctx);
                break;
            case M_ADDFILES:
                gtkpl_add_files (&main_playlist, (GSList *)ctx);
                break;
            case M_FMDRAGDROP:
                gtkpl_add_fm_dropped_files (&main_playlist, (char *)ctx, p1, p2);
                break;
            }
        }
        usleep(10000);
        update_songinfo ();
    }
}

int
main (int argc, char *argv[]) {
    char *homedir = getenv ("HOME");
    if (!homedir) {
        fprintf (stderr, "unable to find home directory. stopping.\n");
        return -1;
    }
    char defpl[1024];
    snprintf (defpl, 1024, "%s/.config/deadbeef/default.dbpl", homedir);
    char confdir[1024];
    snprintf (confdir, 1024, "%s/.config", homedir);
    mkdir (confdir, 0755);
    char dbconfdir[1024];
    snprintf (dbconfdir, 1024, "%s/.config/deadbeef", homedir);
    mkdir (dbconfdir, 0755);

    gtkpl_init ();

    messagepump_init ();
    codec_init_locking ();
    streamer_init ();
    p_init ();
    thread_start (psdl_thread, 0);

    g_thread_init (NULL);
    add_pixmap_directory ("/usr/share/deadbeef/images");
    gdk_threads_init ();
    gdk_threads_enter ();
    gtk_set_locale ();
    gtk_init (&argc, &argv);

    pl_load (defpl);
    mainwin = create_mainwin ();
    searchwin = create_searchwin ();
    gtk_window_set_transient_for (GTK_WINDOW (searchwin), GTK_WINDOW (mainwin));
    extern void main_playlist_init (GtkWidget *widget);
    main_playlist_init (lookup_widget (mainwin, "playlist"));
    extern void search_playlist_init (GtkWidget *widget);
    search_playlist_init (lookup_widget (mainwin, "searchlist"));
    gtk_widget_show (mainwin);
    gtk_main ();
    mainwin = NULL;
    gdk_threads_leave ();
    messagepump_free ();
    psdl_terminate = 1;
    p_free ();
    streamer_free ();
    codec_free_locking ();

    pl_save (defpl);
    pl_free ();
    return 0;
}