system-controller: add screen resource support
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = true
2 verbose = 0
3
4 m = murphy.get()
5
6 -- try loading the various logging plugins
7 m:try_load_plugin('systemd')
8 m:try_load_plugin('dlog')
9
10 -- load the console plugin
11 m:try_load_plugin('console')
12
13 m:try_load_plugin('console.disabled', 'webconsole', {
14                   address = 'wsck:127.0.0.1:3000/murphy',
15                   httpdir = '/usr/share/murphy/webconsole' });
16
17 -- load the dbus plugin
18 if m:plugin_exists('dbus') then
19     m:load_plugin('dbus')
20 end
21
22 -- load the native resource plugin
23 if m:plugin_exists('resource-native') then
24     m:load_plugin('resource-native')
25     m:info("native resource plugin loaded")
26 else
27     m:info("No native resource plugin found...")
28 end
29
30 -- load the dbus resource plugin
31 m:try_load_plugin('resource-dbus', {
32     dbus_bus = "system",
33     dbus_service = "org.Murphy",
34     dbus_track = true,
35     default_zone = "driver",
36     default_class = "implicit"
37 })
38
39 -- load the domain control plugin
40 if m:plugin_exists('domain-control') then
41     m:load_plugin('domain-control')
42 else
43     m:info("No domain-control plugin found...")
44 end
45
46 -- load the AMB plugin
47 if m:plugin_exists('amb') then
48     m:load_plugin('amb')
49 else
50     m:info("No amb plugin found...")
51 end
52
53 -- load the ASM resource plugin
54 if m:plugin_exists('resource-asm') then
55     m:load_plugin('resource-asm', {
56         zone = "driver",
57         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
58         ignored_argv0 = "WebProcess"
59     })
60 else
61     m:info("No audio session manager plugin found...")
62 end
63
64 if m:plugin_exists('ivi-resource-manager') then
65     m:load_plugin('ivi-resource-manager')
66     with_system_controller = false
67 end
68
69 -- define application classes
70 application_class {
71     name     = "interrupt",
72     priority = 99,
73     modal    = true ,
74     share    = false,
75     order    = "fifo"
76 }
77
78 application_class {
79     name     = "emergency",
80     priority = 80,
81     modal    = false,
82     share    = false,
83     order    = "fifo"
84 }
85 application_class {
86     name     = "alert",
87     priority = 51,
88     modal    = false,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "navigator",
95     priority = 50,
96     modal    = false,
97     share    = true,
98     order    = "fifo"
99 }
100
101 application_class {
102     name     = "phone",
103     priority = 6 ,
104     modal    = false,
105     share    = true ,
106     order    = "lifo"
107 }
108 application_class {
109     name     = "camera",
110     priority = 5,
111     modal    = false,
112     share    = false,
113     order    = "lifo"
114 }
115
116 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
117 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
118 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
119 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
120 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
121 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
122
123 -- define zone attributes
124 zone.attributes {
125     type = {mdb.string, "common", "rw"},
126     location = {mdb.string, "anywhere", "rw"}
127 }
128
129 -- define zones
130 zone {
131      name = "driver",
132      attributes = {
133          type = "common",
134          location = "front-left"
135      }
136 }
137
138 zone {
139      name = "passanger1",
140      attributes = {
141          type = "private",
142          location = "front-right"
143      }
144 }
145
146 zone {
147      name = "passanger2",
148      attributes = {
149          type = "private",
150          location = "back-left"
151      }
152 }
153
154 zone {
155      name = "passanger3",
156      attributes = {
157          type = "private",
158          location = "back-right"
159      }
160 }
161
162 zone {
163      name = "passanger4",
164      attributes = {
165          type = "private",
166          location = "back-left"
167      }
168 }
169
170
171 -- define resource classes
172 if not m:plugin_exists('ivi-resource-manager') then
173    resource.class {
174         name = "audio_playback",
175         shareable = true,
176         attributes = {
177             role = { mdb.string, "music", "rw" },
178             pid = { mdb.string, "<unknown>", "rw" },
179             policy = { mdb.string, "relaxed", "rw" }
180         }
181    }
182 end
183
184 resource.class {
185      name = "audio_recording",
186      shareable = true,
187      attributes = {
188          role   = { mdb.string, "music"    , "rw" },
189          pid    = { mdb.string, "<unknown>", "rw" },
190          policy = { mdb.string, "relaxed"  , "rw" }
191      }
192 }
193
194 resource.class {
195      name = "video_playback",
196      shareable = false,
197 }
198
199 resource.class {
200      name = "video_recording",
201      shareable = false
202 }
203
204 if not m:plugin_exists('ivi-resource-manager') then
205 resource.method.veto = {
206     function(zone, rset, grant, owners)
207         rset_priority = application_class[rset.application_class].priority
208
209         owner_id = owners.audio_playback.resource_set
210         rset_id = rset.id
211
212         if (rset_priority >= 50 and owner_id ~= rset_id) then
213             print("*** resource-set "..rset_id.." - veto")
214             return false
215         end
216
217         return true
218     end
219 }
220 end
221
222 -- test for creating selections
223 mdb.select {
224            name = "audio_owner",
225            table = "audio_playback_owner",
226            columns = {"application_class"},
227            condition = "zone_name = 'driver'"
228 }
229
230 mdb.select {
231            name = "vehicle_speed",
232            table = "amb_vehicle_speed",
233            columns = {"value"},
234            condition = "key = 'VehicleSpeed'"
235 }
236
237 element.lua {
238    name    = "speed2volume",
239    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
240    outputs = {  mdb.table { name = "speedvol",
241                             index = {"zone", "device"},
242                             columns = {{"zone", mdb.string, 16},
243                                        {"device", mdb.string, 16},
244                                        {"value", mdb.floating}},
245                             create = true
246                            }
247              },
248    oldvolume = 0.0,
249    update  = function(self)
250                 speed = self.inputs.speed.single_value
251                 if (speed) then
252                     volume = (speed - 144.0) / 7.0
253                 else
254                     volume = 0.0
255                 end
256                 diff = volume - self.oldvolume
257                 if (diff*diff > self.inputs.param) then
258                     print("*** element "..self.name.." update "..volume)
259                     self.oldvolume = volume
260                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
261                 end
262              end
263 }
264
265 -- Night mode processing chain
266
267 mdb.select {
268     name = "exterior_brightness",
269     table = "amb_exterior_brightness",
270     columns = { "value" },
271     condition = "key = 'ExteriorBrightness'"
272 }
273
274 element.lua {
275     name    = "nightmode",
276     inputs  = { brightness = mdb.select.exterior_brightness },
277     oldmode = -1;
278     outputs = {
279         mdb.table {
280             name = "amb_nightmode",
281             index = { "id" },
282             create = true,
283             columns = {
284                 { "id", mdb.unsigned },
285                 { "night_mode", mdb.unsigned }
286             }
287         }
288     },
289     update = function(self)
290         -- This is a trivial function to calculate night mode. Later, we will
291         -- need a better threshold value and hysteresis to prevent oscillation.
292
293         brightness = self.inputs.brightness.single_value
294
295         if not brightness then
296             return
297         end
298
299         print("*** element "..self.name.." update brightness: "..brightness)
300
301         if brightness > 300 then
302             mode = 0
303         else
304             mode = 1
305         end
306
307         print("*** resulting mode: ".. mode)
308
309         if not (mode == self.oldmode) then
310             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
311         end
312
313         self.oldmode = mode
314     end
315 }
316
317 mdb.select {
318     name = "select_night_mode",
319     table = "amb_nightmode",
320     columns = { "night_mode" },
321     condition = "id = 0"
322 }
323
324 sink.lua {
325     name = "night_mode",
326     inputs = { owner = mdb.select.select_night_mode },
327     property = "NightMode",
328     type = "b",
329     initiate = builtin.method.amb_initiate,
330     update = builtin.method.amb_update
331 }
332
333
334 -- Driving mode processing chain
335
336
337 element.lua {
338     name    = "drivingmode",
339     inputs  = { speed = mdb.select.vehicle_speed },
340     oldmode = -1;
341     outputs = {
342         mdb.table {
343             name = "amb_drivingmode",
344             index = { "id" },
345             create = true,
346             columns = {
347                 { "id", mdb.unsigned },
348                 { "driving_mode", mdb.unsigned }
349             }
350         }
351     },
352     update = function(self)
353
354         speed = self.inputs.speed.single_value
355
356         if not speed then
357             return
358         end
359
360         if speed == 0 then
361             mode = 0
362         else
363             mode = 1
364         end
365
366         if not (mode == self.oldmode) then
367             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
368         end
369
370         self.oldmode = mode
371     end
372 }
373
374 mdb.select {
375     name = "select_driving_mode",
376     table = "amb_drivingmode",
377     columns = { "driving_mode" },
378     condition = "id = 0"
379 }
380
381 sink.lua {
382     name = "driving_mode",
383     inputs = { owner = mdb.select.select_driving_mode },
384     property = "DrivingMode",
385     type = "u",
386     initiate = builtin.method.amb_initiate,
387     update = builtin.method.amb_update
388 }
389
390 -- turn signals (left, right)
391
392 mdb.select {
393     name = "winker",
394     table = "amb_turn_signal",
395     columns = { "value" },
396     condition = "key = 'TurnSignal'"
397 }
398
399 -- regulation (on), use "select_driving_mode"
400
401 -- shift position (parking, reverse, other)
402
403 mdb.select {
404     name = "gear_position",
405     table = "amb_gear_position",
406     columns = { "value" },
407     condition = "key = 'GearPosition'"
408 }
409
410 -- cameras (back, front, left, right)
411
412 element.lua {
413     name    = "camera_state",
414     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
415     oldmode = -1;
416     outputs = {
417         mdb.table {
418             name = "target_camera_state",
419             index = { "id" },
420             create = true,
421             columns = {
422                 { "id", mdb.unsigned },
423                 { "front_camera", mdb.unsigned },
424                 { "back_camera", mdb.unsigned },
425                 { "right_camera", mdb.unsigned },
426                 { "left_camera", mdb.unsigned }
427             }
428         }
429     },
430     update = function(self)
431
432         front_camera = 0
433         back_camera = 0
434         right_camera = 0
435         left_camera = 0
436
437         if self.inputs.gear == 128 then
438             back_camera = 1
439         elseif self.inputs.winker == 1 then
440             right_camera = 1
441         elseif self.inputs.winker == 2 then
442             left_camera = 1
443         end
444
445         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
446
447     end
448 }
449
450 -- load the telephony plugin
451 m:try_load_plugin('telephony')
452
453
454 -- system controller test setup
455
456 if not with_system_controller or not m:plugin_exists('system-controller') then
457    return
458 end
459
460 m:load_plugin('system-controller')
461
462 window_manager_operation_names = {
463     [1] = "create",
464     [2] = "destroy"
465 }
466
467 function window_manager_operation_name(oper)
468     local name = window_manager_operation_names[oper]
469     if name then return name end
470     return "<unknown " .. tostring(oper) .. ">"
471 end
472
473 window_operation_names = {
474     [1] = "create",
475     [2] = "destroy",
476     [3] = "name_change",
477     [4] = "visible",
478     [5] = "configure",
479     [6] = "active"
480 }
481
482 function window_operation_name(oper)
483     local name = window_operation_names[oper]
484     if name then return name end
485     return "<unknown " .. tostring(oper) .. ">"
486 end
487
488 layer_operation_names = {
489     [1] = "create",
490     [2] = "destroy",
491     [3] = "visible"
492 }
493
494 function layer_operation_name(oper)
495     local name = layer_operation_names[oper]
496     if name then return name end
497     return "<unknown " .. tostring(oper) .. ">"
498 end
499
500 command_names = {
501     [0x00001] = "send_appid",
502     [0x10001] = "create",
503     [0x10002] = "destroy",
504     [0x10003] = "show",
505     [0x10004] = "hide",
506     [0x10005] = "move",
507     [0x10006] = "change_active",
508     [0x10007] = "change_layer",
509     [0x10008] = "change_attr",
510     [0x10009] = "name",
511     [0x10011] = "map_thumb",
512     [0x10012] = "unmap_thumb",
513     [0x10020] = "show layer",
514     [0x10021] = "hide_layer",
515     [0x10022] = "change_layer_attr",
516     [0x20001] = "add_input",
517     [0x20002] = "del_input",
518     [0x20003] = "send_input",
519     [0x40001] = "acquire_res",
520     [0x40002] = "release_res",
521     [0x40003] = "deprive_res",
522     [0x40004] = "waiting_res",
523     [0x40005] = "revert_res",
524     [0x40011] = "create_res",
525     [0x40012] = "destroy_res",
526     [0x50001] = "set_region",
527     [0x50002] = "unset_region",
528     [0x60001] = "change_state"
529 }
530
531 function command_name(command)
532     local name = command_names[command]
533     if name then return name end
534     return "<unknown " .. tostring(command) .. ">"
535 end
536
537 input_layer = {
538    [3] = true, -- input
539    [4] = true, -- touch
540    [5] = true  -- cursor
541 }
542
543 resmgr = resource_manager {
544   screen_event_handler = function(self, ev)
545                              local event = ev.event
546                              local surface = ev.surface
547
548                              if event == "grant" then
549                                  if verbose > 0 then
550                                     print("*** make visible surface "..surface)
551                                  end
552                                  local a = animation({})
553                                  local r = m:JSON({surface = surface,
554                                                    visible = 1,
555                                                    raise   = 1})
556                                  wmgr:window_request(r,a,0)
557                                  elseif event == "revoke" then
558                                  if verbose > 0 then
559                                     print("*** hide surface "..surface)
560                                  end
561                                  local a = animation({})
562                                  local r = m:JSON({surface = ev.surface,
563                                                    visible = 0})
564                                  wmgr:window_request(r,a,0)
565                              else
566                                  if verbose > 0 then
567                                     print("*** resource event: "..tostring(ev))
568                                  end
569                              end
570                          end
571 }
572
573 resclnt = resource_client {}
574
575 wmgr = window_manager {
576   geometry = function(self, w,h, v)
577                   if type(v) == "function" then
578                       return v(w,h)
579                   end
580                   return v
581              end,
582
583   application = function(self, appid)
584                      if appid then
585                          local app = application_lookup(appid)
586                          if not app then
587                              app = application_lookup("default")
588                          end
589                          return app
590                      end
591                      return { privileges = {screen="none", audio="none"} }
592                 end,
593
594   outputs = { { name  = "Center",
595                 id    = 0,
596                 zone  = "driver",
597                 areas = { Status = {
598                               id     = 0,
599                               pos_x  = 0,
600                               pos_y  = 0,
601                               width  = function(w,h) return w end,
602                               height = 64
603                           },
604                           Full = {
605                               id     = 1,
606                               pos_x  = 0,
607                               pos_y  = 64,
608                               width  = function(w,h) return w end,
609                               height = function(w,h) return h-64-128 end
610                           },
611                           Upper = {
612                               id     = 2,
613                               pos_x  = 0,
614                               pos_y  = 64,
615                               width  = function(w,h) return w end,
616                               height = function(w,h) return (h-64-128)/2 end
617                           },
618                           Lower = {
619                               id     = 3,
620                               pos_x  = 0,
621                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
622                               width  = function(w,h) return w end,
623                               height = function(w,h) return (h-64-128)/2 end
624                           },
625                           UpperLeft = {
626                               id     = 4,
627                               pos_x  = 0,
628                               pos_y  = 64,
629                               width  = function(w,h) return w/2 end,
630                               height = function(w,h) return (h-64-128)/2 end
631                           },
632                           UpperRight = {
633                               id     = 5,
634                               pos_x  = function(w,h) return w/2 end,
635                               pos_y  = 64,
636                               width  = function(w,h) return w/2 end,
637                               height = function(w,h) return (h-64-128)/2 end
638                           },
639                           LowerLeft = {
640                               id     = 6,
641                               pos_x  = 0,
642                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
643                               width  = function(w,h) return w/2 end,
644                               height = function(w,h) return (h-64-128)/2 end
645                           },
646                           LowerRight = {
647                               id     = 7,
648                               pos_x  = function(w,h) return w/2 end,
649                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
650                               width  = function(w,h) return w/2 end,
651                               height = function(w,h) return (h-64-128)/2 end
652                           },
653                           SysApp = {
654                               id     = 8,
655                               pos_x  = 0,
656                               pos_y  = 64,
657                               width  = function(w,h) return w end,
658                               height = function(w,h) return h-64-128 end
659                           },
660                           ["SysApp.Left"] = {
661                               id     = 9,
662                               pos_x  = 0,
663                               pos_y  = 64,
664                               width  = function(w,h) return w/2-181 end,
665                               height = function(w,h) return h-64-128 end
666                           },
667                           ["SysApp.Right"] = {
668                               id     = 10,
669                               pos_x  = function(w,h) return w/2+181 end,
670                               pos_y  = 64,
671                               width  = function(w,h) return w/2-181 end,
672                               height = function(w,h) return h-64-128 end
673                           },
674                         }
675                },
676                { name  = "Mid",
677                  zone  = "driver",
678                  id    = 1
679                }
680
681   },
682   layers = { {      0, "Background"   , 1 },
683              {      1, "Application"  , 2 },
684              {      2, "Softkeyboard" , 4 },
685              {      3, "HomeScreen"   , 2 },
686              {      4, "ControlBar"   , 2 },
687              {      5, "InterruptApp" , 2 },
688              {      6, "OnScreen"     , 2 },
689              {    101, "Input"        , 3 },
690              {    102, "Cursor"       , 5 },
691              {    103, "Startup"      , 6 },
692              { 0x1000, "Background"   , 1 },
693              { 0x2000, "Normal"       , 2 },
694              { 0x3000, "Fullscreen"   , 2 },
695              { 0x4000, "InputPanel"   , 3 },
696              { 0xA000, "Touch"        , 4 },
697              { 0xB000, "Cursor"       , 5 },
698              { 0xC000, "Startup"      , 6 }
699   },
700
701
702   manager_update = function(self, oper)
703                        if verbose > 0 then
704                            print("### <== WINDOW MANAGER UPDATE:" ..
705                                  window_manager_operation_name(oper))
706                        end
707                        if oper == 1 then
708                            local umask = window_mask { raise   = true,
709                                                        visible = true,
710                                                        active  = true }
711                            local rmask = window_mask { active  = true }
712                            local req = m:JSON({
713                                        passthrough_update  = umask:tointeger(),
714                                        passthrough_request = rmask:tointeger()
715                            })
716                            self:manager_request(req)
717                        end
718                    end,
719
720   window_update = function(self, oper, win, mask)
721                       if verbose > 0 then
722                           print("### <== WINDOW UPDATE oper:" ..
723                                 window_operation_name(oper) ..
724                                 " mask: " .. tostring(mask))
725                           if verbose > 1 then
726                               print(win)
727                           end
728                       end
729
730                       local arg = m:JSON({ surface = win.surface,
731                                            winname = win.name,
732                       })
733                       local command = 0
734
735                       if oper == 1 then -- create
736                            local layertype = win.layertype
737                            if layertype and input_layer[layertype] then
738                                if verbose > 0 then
739                                    print("ignoring input panel creation")
740                                end
741                                return
742                            end
743                            command     = 0x10001
744                       elseif oper == 2 then -- destroy
745                            command     = 0x10002
746                       elseif oper == 3 then  -- namechange
747                            command     = 0x10009
748                       elseif oper == 4 or oper == 5 then --visible or configure
749                            command     = 0x10008
750                            arg.zone    = win.area
751                            arg.node    = win.node
752                            arg.layer   = win.layer
753                            arg.pos_x   = win.pos_x
754                            arg.pos_y   = win.pos_y
755                            arg.width   = win.width
756                            arg.height  = win.height
757                            arg.raise   = win.raise
758                            arg.visible = win.visible
759                            arg.active  = win.active
760                       elseif oper == 6 then -- active
761                            command     = 0x10006
762                            arg.active  = win.active
763                       else
764                            if verbose > 0 then
765                                print("### nothing to do")
766                            end
767                            return
768                       end
769
770                       local msg = m:JSON({ command = command,
771                                            appid   = win.appid,
772                                            pid     = win.pid,
773                                            arg     = arg
774                       })
775                       if verbose > 0 then
776                           print("### <== sending " ..
777                                 command_name(msg.command) ..
778                                 " window message to '" .. win.name .. "'")
779                           if verbose > 1 then
780                               print(msg)
781                           end
782                       end
783                       sc:send_message(sysctlid, msg)
784
785                       if oper == 1 then -- create
786                           local i = input_layer[win.layertype]
787                           local p = self:application(win.appid)
788                           local s = p.privileges.screen
789
790                           if s == "system" then
791                               local a = animation({})
792                               local r = m:JSON({surface = win.surface,
793                                                 visible = 1,
794                                                 raise   = 1})
795                               self:window_request(r,a,0)
796                           else
797                               if i then
798                                   if verbose > 0 then
799                                       print("do not make resource for " ..
800                                             "input window")
801                                   end
802                               else
803                                   resclnt:resource_set_create("screen",
804                                                                "driver",
805                                                                win.appid,
806                                                                win.surface)
807                               end
808                           end
809                       elseif oper == 2 then -- destroy
810                           resclnt:resource_set_destroy("screen", win.surface)
811                       elseif oper == 6 then -- active
812                           if win.active then
813                               local i = input_layer[win.layertype]
814                               local p = self:application(win.appid)
815                               local s = p.privileges.screen
816                               local surface = win.surface
817                               if not i and s ~= "system" then
818                                  resclnt:resource_set_acquire("screen",surface)
819                                  resmgr:window_raise(win.appid, surface, 1)
820                               end
821                           end
822                       end
823                   end,
824
825   layer_update = function(self, oper, layer, mask)
826                       if verbose > 0 then
827                           print("### LAYER UPDATE:" .. 
828                                 layer_operation_name(oper) ..
829                                 " mask: " .. tostring(mask))
830                           if verbose > 1 then
831                               print(layer)
832                           end
833                       end
834                       if oper == 3 then -- visible
835                          local command = 0x10022
836                          local msg = m:JSON({
837                                          command = command,
838                                          appid = "",
839                                          arg = m:JSON({layer = layer.id,
840                                                        visible = layer.visible
841                                          })
842                                 })
843                          if verbose > 0 then
844                             print("### <== sending "..command_name(command)..
845                                   " layer message")
846                             if verbose > 1 then
847                                 print(msg)
848                             end
849                          end
850                          sc:send_message(sysctlid, msg)
851                       else
852                            if verbose > 0 then
853                                print("### nothing to do")
854                            end
855                       end
856                  end,
857
858   output_update = function(self, oper, out, mask)
859                       local idx = out.index
860                       if verbose > 0 then
861                           print("### OUTPUT UPDATE:" .. oper ..
862                                 " mask: "..tostring(mask))
863                       end
864                       print(out)
865                       local outdef = self.outputs[idx+1]
866                       if (oper == 1) then -- create
867                           if outdef then
868                               self:output_request(m:JSON({index = idx,
869                                                           id    = outdef.id,
870                                                           name  = outdef.name
871                                                           }))
872                           end
873                       elseif (oper == 5) then -- done
874                           local ads = outdef.areas
875                           local on = outdef.name
876                           if ads then
877                               for name,ad in pairs(ads) do
878                                   local can = wmgr:canonical_name(on.."."..name)
879                                   local a = m:JSON({name   = name,
880                                                     output = out.index})
881                                   for fld,val in pairs(ad) do
882                                       a[fld] = self:geometry(out.width,
883                                                              out.height,
884                                                              val) 
885                                    end
886                                    self:area_create(a)
887                                    resmgr:area_create(area[can], outdef.zone)
888                               end
889                           end
890                       end
891                   end
892 }
893
894
895 sc = m:get_system_controller()
896
897 -- resource sets
898 sets = {}
899
900 connected = false
901 sysctlid = ""
902
903 -- these shoud be before wmgr:connect() is called
904 if verbose > 0 then
905    print("====== creating applications ======")
906 end
907 application {
908     appid          = "default",
909     area           = "Center.Full",
910     privileges     = { screen = "none", audio = "none" },
911     resource_class = "player",
912     screen_priority = 0
913 }
914
915 application {
916     appid           = "weston",
917     area            = "Center.Full",
918     privileges      = { screen = "system", audio = "none" },
919     resource_class  = "implicit",
920     screen_priority = 30
921 }
922
923 application {
924     appid           = "org.tizen.ico.homescreen",
925     area            = "Center.Full",
926     privileges      = { screen = "system", audio = "system" },
927     resource_class  = "player",
928     screen_priority = 20
929 }
930
931 application {
932     appid           = "org.tizen.ico.statusbar",
933     area            = "Center.Status",
934     privileges      = { screen = "system", audio = "none" },
935     resource_class  = "player",
936     screen_priority = 20 
937 }
938
939
940 if sc then
941     sc.client_handler = function (self, cid, msg)
942         local command = msg.command
943         if verbose > 0 then
944             print('### ==> client handler:')
945             if verbose > 1 then
946                 print(msg)
947             end
948         end
949         if not connected then
950             print('Setting sysctlid='..msg.appid)
951             sysctlid = msg.appid
952             print('Trying to connect to wayland...')
953             connected = wmgr:connect()
954         end
955         if connected and command then
956             if command == 1 then -- send_appid
957                 local reply = m:JSON({ command = 0x60001,
958                                        arg     = m:JSON({ stateid = 1,
959                                                           state   = 0})
960                                      })
961                  if verbose > 0 then
962                      print("### <== sending " ..
963                            command_name(command) .. " message")
964                      if verbose > 1 then
965                          print(reply)
966                      end
967                  end
968                  sc:send_message(sysctlid, reply)
969
970                  reply = m:JSON({ command = 0x60001,
971                                   arg     = m:JSON({ stateid = 2,
972                                                      state   = 0})
973                                 })
974                  if verbose > 0 then
975                      print("### <== sending " ..
976                            command_name(command) .. " message")
977                      if verbose > 1 then
978                          print(reply)
979                      end
980                  end
981                  sc:send_message(sysctlid, reply)
982             end
983         end
984     end
985
986     sc.generic_handler = function (self, cid, msg)
987         if verbose > 0 then
988             print('### ==> generic handler:')
989             if verbose > 1 then
990                print(msg)
991             end
992         end
993     end
994
995     sc.window_handler = function (self, cid, msg)
996         if verbose > 0 then
997             print('### ==> received ' ..
998                    command_name(msg.command) .. ' message')
999             if verbose > 1 then
1000                 print(tostring(msg))
1001             end
1002         end
1003
1004         local a = animation({})
1005         local nores = false
1006         if msg.command == 0x10003 then       -- ico SHOW command
1007             local raise_mask = 0x01000000
1008             local lower_mask = 0x02000000
1009             local nores_mask = 0x40000000
1010             local time_mask  = 0x00ffffff
1011             msg.arg.visible = 1
1012             if msg.arg then
1013                 local time = 200
1014                 if  msg.arg.anim_time then
1015                     local t = msg.arg.anim_time
1016                     time = m:AND(t, time_mask)
1017                     nores = m:AND(t, nores_mask)
1018                     if m:AND(t, raise_mask) then
1019                         msg.arg.raise = 1
1020                     elseif m:AND(t, lower_mask) then
1021                         msg.arg.raise = 0
1022                     end
1023                 end
1024                 if msg.arg.anim_name then
1025                     a.show = { msg.arg.anim_name, time }
1026                     print('time: ' .. tostring(a.show[2]))
1027                 end
1028             end
1029             if not nores then
1030                 local p = wmgr:application(msg.appid)
1031                 local s = p.privileges.screen
1032                 if s == "system" then
1033                     nores = true
1034                 end
1035             end
1036             if verbose > 2 then
1037                 print('### ==> SHOW')
1038                 print(tostring(msg.arg))
1039             end
1040             if nores then
1041                 wmgr:window_request(msg.arg, a, 0)
1042             else
1043                 local surface = msg.arg.surface
1044                 resclnt:resource_set_acquire("screen", surface)
1045                 resmgr:window_raise(msg.appid, surface, 1)
1046             end
1047         elseif msg.command == 0x10004 then   -- ico HIDE command
1048             local raise_mask = 0x01000000
1049             local lower_mask = 0x02000000
1050             local nores_mask = 0x40000000
1051             local time_mask  = 0x00ffffff
1052             msg.arg.visible = 0
1053             if msg.arg then
1054                 local time = 200
1055                 if msg.arg.anim_time then
1056                     local t = msg.arg.anim_time
1057                     time = m:AND(t, time_mask)
1058                     nores = m:AND(t, nores_mask)
1059                 end
1060                 if msg.arg.anim_name then
1061                     a.hide = { msg.arg.anim_name, time }
1062                     print('hide animation time: ' .. tostring(a.hide[2]))
1063                 end
1064             end
1065             if not nores then
1066                 local p = wmgr:application(msg.appid)
1067                 local s = p.privileges.screen
1068                 if s == "system" then
1069                     nores = true
1070                 end
1071             end
1072             if verbose > 2 then
1073                 print('### ==> HIDE REQUEST')
1074                 print(tostring(msg.arg))
1075             end
1076             if nores then
1077                 wmgr:window_request(msg.arg, a, 0)
1078             else
1079                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1080             end
1081         elseif msg.command == 0x10005 then   -- ico MOVE
1082             if verbose > 2 then
1083                 print('### ==> MOVE REQUEST')
1084                 print(tostring(msg.arg))
1085             end
1086             wmgr:window_request(msg.arg, a, 0)
1087             -- TODO: handle if area changed
1088         elseif msg.command == 0x10006 then   -- ico ACTIVE
1089             if not msg.arg.active then
1090                 msg.arg.active = 3 -- pointer + keyboard
1091             end
1092             if verbose > 2 then
1093                 print('### ==> ACTIVE REQUEST')
1094                 print(tostring(msg.arg))
1095             end
1096             wmgr:window_request(msg.arg, a, 0)
1097         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1098             if verbose > 2 then
1099                 print('### ==> CHANGE_LAYER REQUEST')
1100                 print(tostring(msg.arg))
1101             end
1102             --[[
1103             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1104                 print("do not change layer for other than cursor or touch")
1105                 return
1106             end
1107             --]]
1108             wmgr:window_request(msg.arg, a, 0)
1109         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1110             local framerate = msg.arg.framerate
1111             if not framerate or framerate < 0 then
1112                 framerate = 0
1113             end
1114             msg.arg.map = 1
1115             if verbose > 2 then
1116                 print('### ==> MAP_THUMB REQUEST')
1117                 print(msg.arg)
1118                 print('framerate: '..framerate)
1119             end
1120             wmgr:window_request(msg.arg, a, framerate)
1121         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1122             msg.arg.map = 0
1123             if verbose > 2 then
1124                 print('### ==> UNMAP_THUMB REQUEST')
1125                 print(msg.arg)
1126             end
1127             wmgr:window_request(msg.arg, a, 0)
1128         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1129             msg.arg.visible = 1
1130             if verbose > 2 then
1131                 print('### ==> SHOW_LAYER REQUEST')
1132                 print(msg.arg)
1133             end
1134             wmgr:layer_request(msg.arg)
1135         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1136             msg.arg.visible = 0
1137             if verbose > 2 then
1138                 print('### ==> HIDE_LAYER REQUEST')
1139                 print(msg.arg)
1140             end
1141             wmgr:layer_request(msg.arg)
1142         end
1143     end
1144
1145     sc.input_handler = function (self, cid, msg)
1146         if verbose > 0 then
1147             print('### ==> input handler: ' .. command_name(msg.comand))
1148             if verbose > 1 then
1149                 print(msg)
1150             end
1151         end
1152     end
1153
1154     sc.user_handler = function (self, cid, msg)
1155         if verbose > 0 then
1156             print('### ==> user handler: ' .. command_name(msg.command))
1157             if verbose > 1 then
1158                 print(msg)
1159             end
1160         end
1161     end
1162
1163     sc.resource_handler = function (self, cid, msg)
1164         if verbose > 0 then
1165             print('### ==> resource handler: ' .. command_name(msg.command))
1166             if verbose > 1 then
1167                 print(msg)
1168             end
1169         end
1170
1171         createResourceSet = function (ctl, client, msg)
1172             cb = function(rset, data)
1173                 print("> resource callback")
1174
1175                 -- type is either basic (0) or interrupt (1)
1176                 requestType = 0
1177                 if msg.res.type then
1178                     requestType = msg.res.type
1179                 end
1180
1181                 if rset.acquired then
1182                     cmd = 0x00040001 -- acquire
1183                 else
1184                     cmd = 0x00040002 -- release
1185                 end
1186
1187                 reply = m.JSON({
1188                         appid = data.client,
1189                         command = cmd,
1190                         res = {
1191                             type = requestType
1192                         }
1193                     })
1194
1195                 if rset.resources.audio_playback then
1196                     reply.res.sound = {
1197                         zone = "driver",
1198                         name = msg.appid,
1199                         adjust = 0,
1200                         -- id = "0"
1201                     }
1202                 end
1203
1204                 if rset.resources.display then
1205                     reply.res.window = {
1206                         zone = "driver",
1207                         name = msg.appid,
1208                         -- id = "0"
1209                     }
1210                 end
1211
1212                 if rset.resources.input then
1213                     reply.res.input = {
1214                         name = msg.appid,
1215                         event = 0
1216                     }
1217                 end
1218                 print("sending message to client: " .. data.client)
1219
1220                 if sc:send_message(data.client, reply) then
1221                     print('*** reply OK')
1222                 else
1223                     print('*** reply FAILED')
1224                 end
1225             end
1226
1227             rset = m:ResourceSet({
1228                     application_class = "player",
1229                     zone = "driver", -- msg.zone ("full")
1230                     callback = cb
1231                 })
1232
1233             rset.data = {
1234                 cid = cid,
1235                 ctl = ctl
1236             }
1237
1238             if msg.res.sound then
1239                 rset:addResource({
1240                         resource_name = "audio_playback"
1241                     })
1242                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1243                 rset.resources.audio_playback.attributes.appid = msg.appid
1244                 print("sound name: " .. msg.res.sound.name)
1245                 print("sound zone:" .. msg.res.sound.zone)
1246                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1247                 if msg.res.sound.id then
1248                     print("sound id: " .. msg.res.sound.id)
1249                 end
1250             end
1251
1252             if msg.res.input then
1253                 rset:addResource({
1254                         resource_name = "input"
1255                     })
1256                 rset.resources.input.attributes.pid = tostring(msg.pid)
1257                 rset.resources.input.attributes.appid = msg.appid
1258                 print("input name: " .. msg.res.sound.name)
1259                 print("input event:" .. tostring(msg.res.input.event))
1260             end
1261
1262             if msg.res.window then
1263                 rset:addResource({
1264                         resource_name = "display"
1265                     })
1266                 rset.resources.display.attributes.pid = tostring(msg.pid)
1267                 rset.resources.display.attributes.appid = msg.appid
1268                 print("display name: " .. msg.res.display.name)
1269                 print("display zone:" .. msg.res.display.zone)
1270                 if msg.res.display.id then
1271                     print("display id: " .. msg.res.display.id)
1272                 end
1273             end
1274
1275             return rset
1276         end
1277
1278         -- parse the message
1279
1280         -- fields common to all messages:
1281         --      msg.command
1282         --      msg.appid
1283         --      msg.pid
1284
1285         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1286             print("command CREATE")
1287
1288             if not sets.cid then
1289                 sets.cid = createResourceSet(self, cid, msg)
1290             end
1291
1292         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1293             print("command DESTROY")
1294
1295             if sets.cid then
1296                 sets.cid:release()
1297             end
1298
1299             sets.cid = nil -- garbage collecting
1300
1301         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1302             print("command ACQUIRE")
1303
1304             if not sets.cid then
1305                 sets.cid = createResourceSet(self, cid, msg)
1306             end
1307
1308             sets.cid:acquire()
1309
1310         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1311             print("command RELEASE")
1312
1313             if sets.cid then
1314                 sets.cid:release()
1315             end
1316
1317         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1318             print("command DEPRIVE")
1319
1320         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1321             print("command WAITING")
1322
1323         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1324             print("command REVERT")
1325         end
1326     end
1327
1328     sc.inputdev_handler = function (self, cid, msg)
1329         if verbose > 0 then
1330             print('*** inputdev handler: ' .. command_name(msg.command))
1331             if verbose > 1 then
1332                 print(msg)
1333             end
1334         end
1335     end
1336 end