#########################################################################
##                                                                     ##
##  I N T E R S T E L L A R   C O N S O L E                            ##
##                         displays a square black mask on the screen  ##
##            Copyright (C) 2025- Misato Observatory, Wakayama, Japan  ##
##                                                                     ##
##  This software is available under the GPL (General Public License)  ##
##                                                                     ##
#########################################################################


####################################################################
##                                                                ##
##                       P R O C E D U R E S                      ##
##                                                                ##
####################################################################

#
# Server URL, directories, etc.
#
source local-config.tcl

#
# Import base procedures
#
source base-proc.tcl

#
# Inf. loop
#
proc watch_stellarium {} {

  global stellarium_status_url

  do_http_get $stellarium_status_url line result
  if { $line eq "" } {
    exit
  }

  after 5000 watch_stellarium

}

#
# Create black window
#
proc create_window { top } {

  global init_width
  global init_height
  global width
  global height
  global pos_x
  global pos_y

  # create window
  catch {destroy $top} wid
  toplevel $top

  wm resizable $top 0 0

  wm overrideredirect $top 1
  wm attributes $top -topmost yes
  wm geometry $top ${init_width}x${init_height}

  update

  after 1000

  set s [format "+%d+%d" $pos_x $pos_y]
  wm geometry $top $s

  update

  if { 0 < $width && 0 < $height } {
    wm geometry $top ${width}x${height}
  }

  update

}


####################################################################
##                                                                ##
##                             M A I N                            ##
##                                                                ##
####################################################################

#
# Server URL
# 
set stellarium_status_url "$stellarium_url/api/main/status"

# for overrideredirect
option add *highlightThickness "0"

#
# Colors, Size of window
#
option add *background "#000000"
option add *foreground "#000000"

set init_width  256
set init_height 64
set width 0
set height 0
set pos_x 0
set pos_y 0

# ID of window
set top ".topwin"

# hide default wish window
wm state . withdrawn

if { 0 < $argc } {

  # Parsing args

  if { 0 < $argc } {
    set arg [lindex $argv 0]
    set params [split $arg ","]
    if { 4 <= [llength $params] } {
      set width [lindex $params 0]
      set height [lindex $params 1]
      set pos_x [lindex $params 2]
      set pos_y [lindex $params 3]
    }
  }

}

create_window $top

# display main window
#wm state . normal

# inf. loop
watch_stellarium

