1package AmphetaDesk::AmphetaDesk::OS::MacOSX;
2###############################################################################
3# This package handles all the GUI routines for the MacOSX operating system.  #
4# It requires various modules to be compiled into the runtime wrapper script  #
5# - these modules are assumed to be there. If not, oopsy, we got problems.    #
6#                                                                             #
7# LIST OF ROUTINES BELOW:                                                     #
8#   gui_init - start the gui and display the window.                          #
9#   gui_listen - listen for window events for our gui.                        #
10#   gui_note - send a note to our gui window.                                 #
11#   open_url - open a url in the system's default browser.                    #
12#   _things - various routines that control the gui and should be private.    #
13###############################################################################
14
15use strict;
16use AmphetaDesk::AmphetaDesk::Settings;
17use AmphetaDesk::AmphetaDesk::Utilities;
18use Fcntl;
19require Exporter;
20use vars qw( @ISA @EXPORT );
21@ISA = qw( Exporter );
22@EXPORT = qw( gui_init gui_listen gui_note open_url );
23
24# our event hooks. if we receive a request from the
25# OS X wrapper that matches one of these, then we call
26# the matching routine. note that our() is fine here
27# since OS X comes with Perl 5.6+ anyways.
28our %cmds = ( REFRESH => \&_gui_cmd_refresh );
29
30###############################################################################
31# gui_init - start the gui and display the window.                            #
32###############################################################################
33# USAGE:                                                                      #
34#   gui_init;                                                                 #
35#                                                                             #
36# NOTES:                                                                      #
37#   This routine loads specific libraries specific to the OS and attempts to  #
38#   do everything necessary to start up a GUI window and start listening for  #
39#   events. Further down in this file, you should see supplementary GUI       #
40#   routines (starting with _) that are part of the GUI happenings this       #
41#   routine inits.                                                            #
42#                                                                             #
43# RETURNS:                                                                    #
44#   1; this routine always returns happily.                                   #
45###############################################################################
46
47sub gui_init {
48
49   # set reading from STDIN to non-blocking.
50   my $flags = 0;
51   fcntl(STDIN, F_GETFL, $flags)
52      or die "Couldn't get flags for STDIN : $!\n";
53   $flags |= O_NONBLOCK;
54   fcntl(STDIN, F_SETFL, $flags)
55     or die "Couldn't set flags for STDIN: $!\n";
56
57   return 1;
58
59}
60
61###############################################################################
62# gui_listen - listen for window events for our gui.                          #
63###############################################################################
64# USAGE:                                                                      #
65#   gui_listen;                                                               #
66#                                                                             #
67# NOTES:                                                                      #
68#   This routine checks the event queue and sees if there is anything that    #
69#   needs to be done. It's called from the main loop of our program.          #
70#                                                                             #
71# RETURNS:                                                                    #
72#   1; this routine always returns happily.                                   #
73###############################################################################
74
75sub gui_listen {
76
77   my $lin;
78
79   # set to non-blocking in gui_init().
80   if (defined ($lin=<STDIN>)) {
81
82      # parse out the command prefix, keyword, and args
83      chomp $lin; my ($prefix, $cmd, @args) = split(/ /, $lin);
84
85      # only accept input with prefix 'CMD'. if the subroutine
86      # for the CMD is found, we rush off to execution.
87      if ((uc $prefix) eq 'CMD') {
88         $cmds{uc $cmd}->()
89            if (defined $cmds{uc $cmd});
90      }
91   }
92
93   return 1;
94
95}
96
97###############################################################################
98# gui_note - send a note to our gui window.                                   #
99###############################################################################
100# USAGE:                                                                      #
101#   gui_note("This is a gui window line. Yup.");                              #
102#                                                                             #
103# NOTES:                                                                      #
104#   Much like note(), only we send our message to our os specific gui window. #
105#                                                                             #
106# RETURNS:                                                                    #
107#   1; this routine always returns happily.                                   #
108###############################################################################
109
110sub gui_note {
111
112   # eventually, this will do something a bit more meaningful.
113   my ($message) = @_; print STDOUT $message . "\n"; return 1;
114
115}
116
117###############################################################################
118# open_url - open a url in the system's default browser.                      #
119###############################################################################
120# USAGE:                                                                      #
121#    open_url( );                                                             #
122#                                                                             #
123# OS SPECIFIC NOTES:                                                          #
124#    Since it's nigh near impossible for us to figure out the default browser #
125#    much less the path to said browser in MacOSX, we simply display a        #
126#    message telling the user what to do.                                     #
127#                                                                             #
128#                                                                             #
129# RETURNS:                                                                    #
130#    1; we instruct the user to open their browser if we can't.               #
131###############################################################################
132
133sub open_url {
134
135   # construct our url.
136   my $url = "http://127.0.0.1:" . get_setting("urls_port") . "/index.html";
137
138   # we spit out our suggestion just to catch all instances.
139   note("GUI_CMD OPEN_URL $url", 1) if $ENV{DYLD_LIBRARY_PATH};
140   note("If your browser doesn't load, go to <$url>.", 1);
141
142   return 1;
143
144}
145
146###############################################################################
147# _things - various routines that control the gui and should be private.      #
148###############################################################################
149# USAGE:                                                                      #
150#    These are internally called by the GUI and shouldn't be publically       #
151#    used. So stop poking around here. Sheesh. Flippin' nosy people. Sigh.    #
152###############################################################################
153
154sub _gui_cmd_refresh {
155
156   # helloooooooo, nurse!
157   note("Refreshing the channel list...",                                                   1);
158   note("--------------------------------------------------------------------------------", 1);
159
160   # do the dirty deed of download and open.
161   my $url = "http://127.0.0.1:" . get_setting("urls_port") . "/index.html";
162   AmphetaDesk::AmphetaDesk::MyChannels::download_my_channels();
163   note("GUI_CMD OPEN_URL $url", 1) if $ENV{DYLD_LIBRARY_PATH};
164
165   return 1;
166
167}
168
1691;
170