]> git.sesse.net Git - vlc/blob - modules/gui/beos/MessagesWindow.cpp
Move the messages update function to another thread. It prevents it from
[vlc] / modules / gui / beos / MessagesWindow.cpp
1 /*****************************************************************************
2  * MessagesWindow.cpp: beos interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: MessagesWindow.cpp,v 1.7 2003/02/10 15:23:46 titer Exp $
6  *
7  * Authors: Eric Petit <titer@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /* BeOS headers */
25 #include <InterfaceKit.h>
26 #include <SupportKit.h>
27
28 /* VLC headers */
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 /* BeOS module headers */
33 #include "VlcWrapper.h"
34 #include "InterfaceWindow.h"
35 #include "MessagesWindow.h"
36
37 static int UpdateMessages( intf_thread_t * p_intf );
38
39 /*****************************************************************************
40  * MessagesWindow::MessagesWindow
41  *****************************************************************************/
42 MessagesWindow::MessagesWindow( intf_thread_t * p_intf,
43                                 BRect frame, const char * name )
44         : BWindow( frame, name, B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
45                B_NOT_ZOOMABLE )
46 {
47         this->p_intf = p_intf;
48         
49         BRect rect, textRect;
50         
51         rect = Bounds();
52         rect.right -= B_V_SCROLL_BAR_WIDTH;
53         textRect = rect;
54         textRect.InsetBy( 5, 5 );
55         fMessagesView = new BTextView( rect, "messages", textRect,
56                                        B_FOLLOW_ALL, B_WILL_DRAW );
57         fMessagesView->MakeEditable( false );
58         fMessagesView->SetStylable( true );
59         fScrollView = new BScrollView( "scrollview", fMessagesView, B_WILL_DRAW,
60                                        B_FOLLOW_ALL, false, true );
61         fScrollBar = fScrollView->ScrollBar( B_VERTICAL );
62         AddChild( fScrollView );
63         
64     /* start window thread in hidden state */
65     Hide();
66     Show();
67
68     /* update it */
69     if( vlc_thread_create( p_intf, "update messages", UpdateMessages,
70                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
71     {
72         msg_Err( p_intf, "cannot create update messages thread" );
73     }
74 }
75
76 /*****************************************************************************
77  * MessagesWindow::~MessagesWindow
78  *****************************************************************************/
79 MessagesWindow::~MessagesWindow()
80 {
81     vlc_thread_join( p_intf );
82 }
83
84 /*****************************************************************************
85  * MessagesWindow::FrameResized
86  *****************************************************************************/
87 void MessagesWindow::FrameResized( float, float )
88 {
89     BRect rect = fMessagesView->Bounds();
90     rect.InsetBy( 5, 5 );
91     fMessagesView->SetTextRect( rect );
92 }
93
94 /*****************************************************************************
95  * MessagesWindow::QuitRequested
96  *****************************************************************************/
97 bool MessagesWindow::QuitRequested()
98 {
99     Hide();
100     return false;
101 }
102
103 /*****************************************************************************
104  * MessagesWindow::ReallyQuit
105  *****************************************************************************/
106 void MessagesWindow::ReallyQuit()
107 {
108     Lock();
109     Hide();
110     Quit();
111 }
112
113 /*****************************************************************************
114  * UpdateMessages
115  *****************************************************************************/
116 static int UpdateMessages( intf_thread_t * p_intf )
117 {
118     /* workaround: wait a bit or it'll crash */
119     msleep( 500000 );
120
121     intf_sys_t * p_sys = (intf_sys_t*)p_intf->p_sys;
122     msg_subscription_t * p_sub = p_sys->p_sub;
123     MessagesWindow * messagesWindow = p_sys->p_window->fMessagesWindow;
124     BTextView * messagesView = messagesWindow->fMessagesView;
125     BScrollBar * scrollBar = messagesWindow->fScrollBar;
126
127     while( !p_intf->b_die )
128     {
129         int i_start, oldLength;
130         char * psz_module_type = NULL;
131         rgb_color red = { 200, 0, 0 };
132         rgb_color gray = { 150, 150, 150 };
133         rgb_color green = { 0, 150, 0 };
134         rgb_color orange = { 230, 180, 00 };
135         rgb_color color;
136     
137         vlc_mutex_lock( p_sub->p_lock );
138         int i_stop = *p_sub->pi_stop;
139         vlc_mutex_unlock( p_sub->p_lock );
140
141         if( p_sub->i_start != i_stop )
142         {
143             for( i_start = p_sub->i_start;
144                  i_start != i_stop;
145                  i_start = (i_start+1) % VLC_MSG_QSIZE )
146             {
147                 /* Add message */
148                 switch( p_sub->p_msg[i_start].i_type )
149                 {
150                     case VLC_MSG_INFO: color = green; break;
151                     case VLC_MSG_WARN: color = orange; break;
152                     case VLC_MSG_ERR: color = red; break;
153                     case VLC_MSG_DBG: color = gray; break;
154                 }
155             
156                 switch( p_sub->p_msg[i_start].i_object_type )
157                 {
158                     case VLC_OBJECT_ROOT: psz_module_type = "root"; break;
159                     case VLC_OBJECT_VLC: psz_module_type = "vlc"; break;
160                     case VLC_OBJECT_MODULE: psz_module_type = "module"; break;
161                     case VLC_OBJECT_INTF: psz_module_type = "interface"; break;
162                     case VLC_OBJECT_PLAYLIST: psz_module_type = "playlist"; break;
163                     case VLC_OBJECT_ITEM: psz_module_type = "item"; break;
164                     case VLC_OBJECT_INPUT: psz_module_type = "input"; break;
165                     case VLC_OBJECT_DECODER: psz_module_type = "decoder"; break;
166                     case VLC_OBJECT_VOUT: psz_module_type = "video output"; break;
167                     case VLC_OBJECT_AOUT: psz_module_type = "audio output"; break;
168                     case VLC_OBJECT_SOUT: psz_module_type = "stream output"; break;
169                 }
170    
171                 if ( messagesView->LockLooper() )
172                 {
173                     oldLength = messagesView->TextLength();
174                     BString string;
175                     string << p_sub->p_msg[i_start].psz_module << " " << psz_module_type << " : " <<
176                         p_sub->p_msg[i_start].psz_msg << "\n";
177                     messagesView->Insert( string.String() );
178                     messagesView->SetFontAndColor( oldLength,
179                                                    messagesView->TextLength(),
180                                                    NULL, 0, &color );
181                     messagesView->Draw( messagesView->Bounds() );
182                         messagesView->UnlockLooper();
183                 }
184             
185                 /* Scroll at the end */
186                 if( scrollBar->LockLooper() )
187                 {
188                     float min, max;
189                     scrollBar->GetRange( &min, &max );
190                     scrollBar->SetValue( max );
191                     scrollBar->UnlockLooper();
192                 }
193             }
194
195             vlc_mutex_lock( p_sub->p_lock );
196             p_sub->i_start = i_start;
197             vlc_mutex_unlock( p_sub->p_lock );
198         }
199         /* Wait a bit */
200         msleep( INTF_IDLE_SLEEP );
201     }
202     return 0;
203 }