You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mt...@apache.org on 2003/02/13 18:45:30 UTC

cvs commit: jakarta-commons-sandbox/daemon/src/native/nt/procrun procgui.c

mturk       2003/02/13 09:45:30

  Added:       daemon/src/native/nt/procrun procgui.c
  Log:
  The WIN GUI system try and console interface.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/daemon/src/native/nt/procrun/procgui.c
  
  Index: procgui.c
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * Portions of this software are based upon public domain software
   * originally written at the National Center for Supercomputing Applications,
   * University of Illinois, Urbana-Champaign.
   */
  
  /* ====================================================================
   * procrun.
   *
   * Contributed by Mladen Turk <mt...@mappingsoft.com>
   *
   * 05 Aug 2002
   * ==================================================================== 
   */
  
  #if defined(PROCRUN_WINAPP)
  
  #ifndef STRICT
  #define STRICT
  #endif
  #ifndef OEMRESOURCE
  #define OEMRESOURCE
  #endif
  
  #include <windows.h>
  #include <windowsx.h>
  #include <commctrl.h>
  #include <objbase.h>
  #include <shlobj.h>
  #include <stdlib.h>
  #include <stdio.h>
  
  #include <Shlwapi.h>
  #include <io.h>
  #include <fcntl.h>
  #include <process.h>
  #include <time.h>
  #include <stdarg.h>
  #include <jni.h>
   
  #include "procrun.h"
  
  #define WM_TRAYMESSAGE         (WM_APP+1)
  #define MAX_LOADSTRING         200
  #define MAX_LISTCOUNT          8192
  #define CONWRAP_SUCCESS        0
  #define CONWRAP_ENOARGS        1
  #define CONWRAP_EARG           2
  #define CONWRAP_EFATAL         3
  
  extern int g_proc_mode;
  /* The main envronment for services */
  extern procrun_t *g_env;
  
  int                    ac_use_try = 0;
  int                    ac_use_dlg = 0;
  int                    ac_use_show = 0;
  HINSTANCE              ac_instance;
  
  static HICON           ac_main_icon;
  static UINT            ac_taskbar_created;
  static HWND            ac_main_hwnd;
  static HWND            ac_list_hwnd;
  static char            *ac_stdout_lines[MAX_LISTCOUNT + 1];
  char                   *ac_cmdline;
  
  
  void ac_add_list_string(const char *str, int len)
  {
      static int nqueue = 0;
      static int nlen = 0, olen = 0;
      int i;
  
      if (str) {
          if (nqueue < MAX_LISTCOUNT) {
              ac_stdout_lines[nqueue++] = strdup(str);
          }
          else
              return;
          nlen = max(nlen, len);
      }
      if (!ac_list_hwnd || !nqueue)
          return;
  
      for (i = 0; i < nqueue; i++)
          ListBox_AddString(ac_list_hwnd, ac_stdout_lines[i]);
      
      if (olen < nlen) {
          olen = nlen;
          SendMessage(ac_list_hwnd, LB_SETHORIZONTALEXTENT, (WPARAM) 10 * olen, (LPARAM) 0);
      }
      for (i = 0; i < nqueue; i++) {
          free(ac_stdout_lines[i]);
          ac_stdout_lines[i] = NULL;
      }
      nqueue = 0;
  }
  
  static void ac_show_try_icon(HWND hwnd, DWORD message, const char *tip)
  {
      
      NOTIFYICONDATA nid;
      if (!ac_use_try)
          return;
  
      ZeroMemory(&nid,sizeof(nid));
      nid.cbSize = sizeof(NOTIFYICONDATA);
      nid.hWnd = hwnd;
      nid.uID = 0xFF;
      nid.uFlags = NIF_ICON | NIF_MESSAGE;
      if (tip)
          nid.uFlags |= NIF_TIP;
  
      nid.uCallbackMessage = WM_TRAYMESSAGE;
      
      if (message != NIM_DELETE)
          nid.hIcon = ac_main_icon;
      else
          nid.hIcon = NULL;
      if (tip)
          strcpy(nid.szTip, tip);
  
      Shell_NotifyIcon(message, &nid);
  }
  
  static void ac_append_menu_item(HMENU menu, UINT menu_id, char *name, int isdef, int enabled)
  {
      MENUITEMINFO mii;
      
      ZeroMemory(&mii, sizeof(MENUITEMINFO));
      mii.cbSize = sizeof(MENUITEMINFO);
      mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
      if (strlen(name)) {
          mii.fType = MFT_STRING;
          mii.wID = menu_id;
          if (isdef)
              mii.fState = MFS_DEFAULT;
          if (!enabled)
              mii.fState |= MFS_DISABLED;
          mii.dwTypeData = name;
      }
      else
          mii.fType = MFT_SEPARATOR;
      InsertMenuItem(menu, menu_id, FALSE, &mii);
  }
  
  static void ac_show_try_menu(HWND hwnd)
  {
      HMENU menu;
      POINT pt;
      char tmp[MAX_LOADSTRING];
  
      if (!ac_use_try)
          return;
      menu = CreatePopupMenu();
      if (menu) {
          if (ac_use_dlg) {
              ac_append_menu_item(menu, IDM_CONSOLE, "Open Console Monitor", 1, 1);
              ac_append_menu_item(menu, 0, "", 0, 1);
          }
          strcpy(tmp, "Exit Service: ");
          strcat(tmp, g_env->m->service.name);
          ac_append_menu_item(menu, IDM_EXIT,  tmp, 0, 1);
  
          if (!SetForegroundWindow(hwnd))
              SetForegroundWindow(NULL);
          GetCursorPos(&pt);
          TrackPopupMenu(menu, TPM_LEFTALIGN|TPM_RIGHTBUTTON, 
                         pt.x, pt.y, 0, hwnd, NULL);
          DestroyMenu(menu);
      }
  }
  
  static void ac_center_window(HWND hwnd)
  {
     RECT    rc, rw;
     int     cw, ch;
     int     x, y;
     if (!ac_use_try)
         return;
  
     /* Get the Height and Width of the child window */
     GetWindowRect(hwnd, &rc);
     cw = rc.right - rc.left;
     ch = rc.bottom - rc.top;
  
     /* Get the limits of the 'workarea' */
     if (!SystemParametersInfo(
              SPI_GETWORKAREA,
              sizeof(RECT),
              &rw, 0)) {
        rw.left = rw.top = 0;
        rw.right = GetSystemMetrics(SM_CXSCREEN);
        rw.bottom = GetSystemMetrics(SM_CYSCREEN);
     }
  
     /* Calculate new X and Y position*/
     x = (rw.right - cw)/2;
     y = (rw.bottom - ch)/2;
     SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
  }
  
  
  LRESULT CALLBACK ac_console_dlg_proc(HWND hdlg, UINT message, WPARAM wparam, LPARAM lparam)
  {
  
      RECT r, m;
      static      HWND  status_bar; 
  
      switch (message) {
          case WM_INITDIALOG:
             ac_list_hwnd = GetDlgItem(hdlg, IDL_STDOUT); 
             status_bar = CreateStatusWindow(0x0800 /* SBT_TOOLTIPS */
                                            | WS_CHILD | WS_VISIBLE,
                                              ac_cmdline, hdlg, IDC_STATBAR);
             GetWindowRect(status_bar, &r);
             GetClientRect(hdlg, &m);
             MoveWindow(ac_list_hwnd, 0, 0, m.right - m.left, m.bottom - abs((r.top - r.bottom)), TRUE);
             ac_add_list_string(NULL, 0);
             break;
          case WM_SIZE:
              switch (LOWORD(wparam)) { 
                  case SIZE_MINIMIZED:
                      EndDialog(hdlg, TRUE); 
                      return TRUE; 
                  break;
                  default:
                      GetWindowRect(status_bar, &r);
                      MoveWindow(status_bar, 0, HIWORD(lparam) - (r.top - r.bottom),
                                                LOWORD(lparam), (r.top - r.bottom), TRUE);
                      GetClientRect(hdlg, &m);
                      MoveWindow(ac_list_hwnd, 0, 0, LOWORD(lparam), HIWORD(lparam) - abs((r.top - r.bottom)), TRUE);
                      break;
              }
          break;
          case WM_QUIT:
          case WM_CLOSE: 
              EndDialog(hdlg, TRUE);
              ac_list_hwnd = NULL;
              return TRUE;
          default:
              return FALSE;
      }
  
      return FALSE;
  }
  
  
  LRESULT CALLBACK ac_main_wnd_proc(HWND hwnd, UINT message,
                            WPARAM wparam, LPARAM lparam)
  {
      if (message == ac_taskbar_created) {
          /* restore the tray icon on shell restart */
          ac_show_try_icon(hwnd, NIM_ADD, g_env->m->service.description);
          return DefWindowProc(hwnd, message, wparam, lparam);
      }
      switch (message) {
          case WM_CREATE:
              ac_show_try_icon(hwnd, NIM_ADD, g_env->m->service.description);
              if (ac_use_show)
                  DialogBox(ac_instance, MAKEINTRESOURCE(IDD_DLGCONSOLE),
                         hwnd, (DLGPROC)ac_console_dlg_proc);
  
              break;
          case WM_DESTROY:
          case WM_QUIT:
              ac_show_try_icon(hwnd, NIM_DELETE, NULL);
              SetEvent(g_env->m->events[0]);
              break;
          case WM_TRAYMESSAGE:
              switch(lparam) {
                  case WM_LBUTTONUP:
                  case WM_RBUTTONUP:
                      ac_show_try_menu(hwnd);
                  break;    
              }
              break;
          case WM_COMMAND:
              switch (LOWORD(wparam)) {
                 case IDM_EXIT:
                     ac_show_try_icon(hwnd, NIM_DELETE, NULL);
                     SetEvent(g_env->m->events[0]);
  
                     PostQuitMessage(CONWRAP_SUCCESS);
                     return TRUE;
                     break;
                 case IDM_CONSOLE:
                     DialogBox(ac_instance, MAKEINTRESOURCE(IDD_DLGCONSOLE),
                         hwnd, (DLGPROC)ac_console_dlg_proc);
                     
                     break;
              }
          default:
              return DefWindowProc(hwnd, message, wparam, lparam);
      }
  
      return FALSE;
  }
  
  /* Create main invisible window */
  static HWND ac_create_main_window(HINSTANCE instance, const char *wclass, const char *title)
  {
      HWND       hwnd = NULL;
      WNDCLASSEX wcex;
  
      wcex.cbSize = sizeof(WNDCLASSEX); 
  
      wcex.style          = CS_HREDRAW | CS_VREDRAW;
      wcex.lpfnWndProc    = ac_main_wnd_proc;
      wcex.cbClsExtra     = 0;
      wcex.cbWndExtra     = 0;
      wcex.hInstance      = instance;
      wcex.hIcon          = ac_main_icon = (HICON)LoadImage(instance, MAKEINTRESOURCE(IDI_ICOCONWRAP),
                                                  IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
      wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
      wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
      wcex.lpszMenuName   = 0;
      wcex.lpszClassName  = wclass;
      wcex.hIconSm        = (HICON)LoadImage(instance, MAKEINTRESOURCE(IDI_ICOCONWRAP),
                                             IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
  
      if (RegisterClassEx(&wcex))
          hwnd = CreateWindow(wclass, title,
                               0, 0, 0, 0, 0,
                               NULL, NULL, instance, NULL);
  
      return hwnd;
  
  }
  
  DWORD WINAPI gui_thread(LPVOID param)
  {
      DWORD rv = 0;
      MSG   msg;
      procrun_t *env = (procrun_t *)param;
      char cname[MAX_LOADSTRING];
  
      if (!param)
          return -1;
      strcpy(cname, env->m->service.name);
      strcat(cname, "_CLASS");
      if (env->m->service.description)
          ac_cmdline = env->m->service.description;
      else
          ac_cmdline = env->m->service.name;
  
      ac_main_hwnd = ac_create_main_window(ac_instance, cname, 
                                           env->m->service.name);
  
      InitCommonControls();
  
      if (ac_main_hwnd) {
          if (ac_use_try)
              ac_taskbar_created = RegisterWindowMessage("TaskbarCreated");
          while (GetMessage(&msg, NULL, 0, 0)) {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
          }    
      }
      SetEvent(env->m->events[0]);
      return rv;
  }
  
  #endif /* PROCRUN_WINAPP */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org