home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 4 / AUCD4.iso / acorn / documents / appnotes / 231-245 / 237c.arc / Disc / c / Capture
Encoding:
Text File  |  1993-06-04  |  6.9 KB  |  279 lines

  1. /*
  2.  * Name   : Capture.c
  3.  * Desc   : C Module task that captures the screen
  4.  * Author : James Bye
  5.  * Date   : 3rd June 1993
  6.  *
  7.  *
  8.  * **********************************************
  9.  * * Disclaimer                                 *
  10.  * * ----------                                 *
  11.  * *                                            *
  12.  * * The software is provided "as is" Acorn     *
  13.  * * Computers Limited ("Acorn") makes no       *
  14.  * * warranty, express or implied, of the       *
  15.  * * merchantability of this software or its    *
  16.  * * fitness for any particular purpose. In no  *
  17.  * * circumstances shall Acorn be liable for any*
  18.  * * damage, loss of profits, or any indirect or*
  19.  * * consequential loss arising out of the use  *
  20.  * * of this software or inability to use this  *
  21.  * * software, even if Acorn has been advised of*
  22.  * * the possibility of such loss.              *
  23.  * **********************************************
  24.  */
  25.  
  26. #include "wimp.h"        /*  access to WIMP SWIs                      */
  27. #include "wimpt.h"       /*  wimp task facilities                     */
  28. #include "win.h"         /*  registering window handlers              */
  29. #include "event.h"       /*  poll loops, etc                          */
  30. #include "baricon.h"     /*  putting icon on icon bar                 */
  31. #include "sprite.h"      /*  sprite operations                        */
  32. #include "werr.h"        /*  error reporting                          */
  33. #include "res.h"         /*  access to resources                      */
  34. #include "resspr.h"      /*  sprite resources                         */
  35. #include "flex.h"        /*  dynamic mem alloc from WIMP              */
  36. #include "template.h"    /*  reading in template file                 */
  37. #include "bbc.h"         /*  olde-style graphics routines             */
  38. #include "colourtran.h"  /*  interface to colour translation module   */
  39. #include "os.h"          /*  low-level RISCOS access                  */
  40. #include "dbox.h"        /*  dialogue box handling                    */
  41. #include "saveas.h"      /*  data export from dbox by icon dragging   */
  42. #include "visdelay.h"    /*  show the hourglass for delay             */
  43.  
  44.  
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <stdio.h>
  48.  
  49. #include "kernel.h"
  50.  
  51. #define TRACE 0
  52. #include "trace.h"
  53.  
  54.  
  55. /*-- menu definitions --*/
  56.  
  57. #define m_IconBar_Title "Capture"
  58. #define m_IconBar_Hits  ">Info,Quit"
  59. #define m_IconBar_Info  1
  60. #define m_IconBar_Quit  2
  61.  
  62. /*-- static variables --*/
  63.  
  64. static menu    iconbar_menu;
  65. static wimp_i  iconbar_icon;
  66. static char   *iconbar_buffer     = NULL;
  67. static int     iconbar_buffer_len = 8;
  68. static sprite_area *my_area;
  69. static wimp_t task_handle = 0;
  70.  
  71.  
  72. /**************************************************
  73.  * Static functions                               *
  74.  **************************************************/
  75.  
  76. /*
  77.  * save proc
  78.  */
  79.  
  80. static BOOL save_proc ( char *filename, void *handle )
  81. {                             
  82.   /*-- use sprite op to save sprite area --*/
  83.  
  84.   wimpt_complain(sprite_area_save(my_area,filename));
  85.  
  86.   /*-- free the workspace --*/
  87.  
  88.   flex_free((flex_ptr)&my_area);
  89.  
  90.   return(TRUE);
  91. }
  92.  
  93. /*
  94.  * grabs a copy of the screen
  95.  */
  96.  
  97. #define SAFE_GUARD  (50*1024)  /*-- 50k safe-gaurd --*/
  98.  
  99. static void grab_screen ( void )
  100. {
  101. int screen_x,screen_y,x0,y0,x1,y1;
  102. int size;    
  103. int xeig,yeig;
  104. os_error *err = NULL;
  105.  
  106.   /*-- first we must read mode variables for current mode --*/
  107.  
  108.   screen_x = bbc_vduvar(bbc_XWindLimit);
  109.   screen_y = bbc_vduvar(bbc_YWindLimit);
  110.   xeig     = bbc_vduvar(bbc_XEigFactor);
  111.   yeig     = bbc_vduvar(bbc_YEigFactor);
  112.  
  113.   /*-- calculate a bounding box for the screen --*/
  114.  
  115.   x0 = 0;
  116.   y0 = 0;
  117.   x1 = screen_x;
  118.   y1 = screen_y;
  119.  
  120.   /*-- calculate size of sprite area  --*/
  121.  
  122.   size = (screen_x * screen_y) + sizeof(sprite_area) + SAFE_GUARD;
  123.  
  124.   /*-- allocate ourselves a sprite area and init --*/
  125.  
  126.   if(!flex_alloc((flex_ptr)&my_area,size))
  127.   {
  128.     werr(0,"Not enough space for sprite area");
  129.     return;
  130.   }                                             
  131.  
  132.   my_area->size   = size;
  133.   my_area->sproff = 16;
  134.   sprite_area_initialise(my_area,size);
  135.  
  136.   /*-- now we capture the screen as a sprite --*/
  137.  
  138.   err = sprite_get_given(my_area,"screen",sprite_haspalette,
  139.                          x0,y0,x1<<xeig,y1<<yeig);
  140.  
  141.   if(err != NULL)
  142.   {
  143.     wimpt_complain(err);
  144.     flex_free((flex_ptr)&my_area);
  145.     return;
  146.   }
  147.   
  148.   /*-- adjust text on iconbar (abit pointless really!) --*/
  149.  
  150.   strcpy(iconbar_buffer,"Save");
  151.   wimp_set_icon_state(-1,iconbar_icon,0,0);   
  152.   
  153.   /*-- now we will save the sprite using saveas --*/
  154.     
  155.   saveas(0xFF9,"ScreenShot",size,save_proc,NULL,NULL,NULL);
  156.  
  157.   /*-- adjust text on iconbar (abit pointless really!) --*/
  158.  
  159.   strcpy(iconbar_buffer,"Ready");
  160.   wimp_set_icon_state(-1,iconbar_icon,0,0);   
  161. }
  162.     
  163.  
  164. /*
  165.  * shows the info box
  166.  */
  167.  
  168. extern void show_info ( void )
  169. {
  170. dbox d = dbox_new("info");
  171.  
  172.   if(d == NULL)
  173.     werr(1,"No memory for DBox");
  174.   dbox_show(d);
  175.   dbox_fillin(d);
  176.   dbox_dispose(&d);              
  177. }                                
  178.  
  179.  
  180. /*
  181.  * menu handler for the iconbar
  182.  */
  183.  
  184. static void iconbar_menu_events ( void *handle, char *hit )
  185. {                          
  186.   handle = handle;
  187.   switch(hit[0])
  188.   {
  189.      case m_IconBar_Info : show_info();
  190.                            break;
  191.      case m_IconBar_Quit : task_handle = 0;
  192.                            exit(0);
  193.                            break;
  194.      default             : break;
  195.   }
  196. }
  197.  
  198.  
  199. /*
  200.  * iconbar click proc
  201.  */
  202.  
  203. static void click_proc ( wimp_i i )
  204. {
  205.   i = i;
  206.  
  207.   /*-- grab a copy of the screen -*/
  208.  
  209.   grab_screen();
  210. }
  211.  
  212.  
  213. /*
  214.  * service call handler
  215.  */
  216.  
  217. #define Service_Memory       0x11
  218.  
  219. extern void MyService ( int service_number, _kernel_swi_regs *r, void *pw )
  220. {
  221.   pw = pw;
  222.  
  223.   /*-- keep application workspace (r2 holds CAO pointer) */
  224.  
  225.   switch(service_number)
  226.   {
  227.     case Service_Memory : if(r->r[2] == (int)Image__RO_Base)
  228.                           {
  229.                             r->r[1] = 0; /*-- refuse to release app. workspace --*/
  230.                           }
  231.                           break;
  232.   }
  233. }
  234.  
  235.  
  236. /**************************************************
  237.  * Main C function                                *
  238.  **************************************************/
  239.  
  240. int main ( void )
  241. {
  242.   /*-- init ourselves with the wimp --*/
  243.  
  244.   wimpt_init("Capture");
  245.   task_handle = wimpt_task();
  246.   res_init("Capture");
  247.   resspr_init();
  248.   flex_init();
  249.   template_init();
  250.   dbox_init();
  251.   visdelay_init();
  252.  
  253.   event_setmask(wimp_EMPTRLEAVE | wimp_EMPTRENTER);
  254.  
  255.   trace_on();
  256.  
  257.  
  258.   /*-- place the icon on the iconbar --*/
  259.  
  260.   iconbar_buffer = malloc(iconbar_buffer_len);
  261.   strcpy(iconbar_buffer,"Ready");
  262.  
  263.   iconbar_icon = baricon_textandsprite("!capture",iconbar_buffer,iconbar_buffer_len,
  264.                                           1,click_proc);
  265.  
  266.   /*-- create & attach the menu to the iconbar --*/
  267.                 
  268.   iconbar_menu = menu_new(m_IconBar_Title,m_IconBar_Hits);
  269.   event_attachmenu(win_ICONBAR,iconbar_menu,iconbar_menu_events,NULL);
  270.  
  271.   /*-- inifinite polling loop --*/
  272.  
  273.   while(TRUE)
  274.     event_process();
  275. }
  276.  
  277. /*-- end --*/
  278.  
  279.